Skip to content

Commit 294248e

Browse files
committed
Merge branch 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull TPM updates from James Morris: "From Jarkko: This purely a bug fix release. The only major change is to move the event log code to its own subdirectory because there starts to be so much of it" * 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: tpm: fix race condition in tpm_common_write() tpm: reduce polling time to usecs for even finer granularity tpm: replace kmalloc() + memcpy() with kmemdup() tpm: replace kmalloc() + memcpy() with kmemdup() tpm: fix use after free in tpm2_load_context() tpm: reduce poll sleep time in tpm_transmit() tpm_tis: verify locality released before returning from release_locality tpm: tpm_crb: relinquish locality on error path. tpm/st33zp24: Fix spelling mistake in macro ST33ZP24_TISREGISTER_UKNOWN tpm: Move eventlog declarations to its own header tpm: Move shared eventlog functions to common.c tpm: Move eventlog files to a subdirectory tpm: Add explicit endianness cast tpm: st33zp24: remove redundant null check on chip tpm: move the delay_msec increment after sleep in tpm_transmit()
2 parents 00d535a + 2d06236 commit 294248e

File tree

17 files changed

+350
-269
lines changed

17 files changed

+350
-269
lines changed

drivers/char/tpm/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#
55
obj-$(CONFIG_TCG_TPM) += tpm.o
66
tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o \
7-
tpm-dev-common.o tpmrm-dev.o tpm1_eventlog.o tpm2_eventlog.o \
8-
tpm2-space.o
9-
tpm-$(CONFIG_ACPI) += tpm_ppi.o tpm_eventlog_acpi.o
10-
tpm-$(CONFIG_EFI) += tpm_eventlog_efi.o
11-
tpm-$(CONFIG_OF) += tpm_eventlog_of.o
7+
tpm-dev-common.o tpmrm-dev.o eventlog/common.o eventlog/tpm1.o \
8+
eventlog/tpm2.o tpm2-space.o
9+
tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
10+
tpm-$(CONFIG_EFI) += eventlog/efi.o
11+
tpm-$(CONFIG_OF) += eventlog/of.o
1212
obj-$(CONFIG_TCG_TIS_CORE) += tpm_tis_core.o
1313
obj-$(CONFIG_TCG_TIS) += tpm_tis.o
1414
obj-$(CONFIG_TCG_TIS_SPI) += tpm_tis_spi.o

drivers/char/tpm/tpm_eventlog_acpi.c renamed to drivers/char/tpm/eventlog/acpi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <linux/acpi.h>
2828
#include <linux/tpm_eventlog.h>
2929

30-
#include "tpm.h"
30+
#include "../tpm.h"
31+
#include "common.h"
3132

3233
struct acpi_tcpa {
3334
struct acpi_table_header hdr;

drivers/char/tpm/eventlog/common.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright (C) 2005, 2012 IBM Corporation
3+
*
4+
* Authors:
5+
* Kent Yoder <[email protected]>
6+
* Seiji Munetoh <[email protected]>
7+
* Stefan Berger <[email protected]>
8+
* Reiner Sailer <[email protected]>
9+
* Kylene Hall <[email protected]>
10+
* Nayna Jain <[email protected]>
11+
*
12+
* Access to the event log created by a system's firmware / BIOS
13+
*
14+
* This program is free software; you can redistribute it and/or
15+
* modify it under the terms of the GNU General Public License
16+
* as published by the Free Software Foundation; either version
17+
* 2 of the License, or (at your option) any later version.
18+
*
19+
*/
20+
21+
#include <linux/seq_file.h>
22+
#include <linux/fs.h>
23+
#include <linux/security.h>
24+
#include <linux/module.h>
25+
#include <linux/tpm_eventlog.h>
26+
27+
#include "../tpm.h"
28+
#include "common.h"
29+
30+
static int tpm_bios_measurements_open(struct inode *inode,
31+
struct file *file)
32+
{
33+
int err;
34+
struct seq_file *seq;
35+
struct tpm_chip_seqops *chip_seqops;
36+
const struct seq_operations *seqops;
37+
struct tpm_chip *chip;
38+
39+
inode_lock(inode);
40+
if (!inode->i_private) {
41+
inode_unlock(inode);
42+
return -ENODEV;
43+
}
44+
chip_seqops = (struct tpm_chip_seqops *)inode->i_private;
45+
seqops = chip_seqops->seqops;
46+
chip = chip_seqops->chip;
47+
get_device(&chip->dev);
48+
inode_unlock(inode);
49+
50+
/* now register seq file */
51+
err = seq_open(file, seqops);
52+
if (!err) {
53+
seq = file->private_data;
54+
seq->private = chip;
55+
}
56+
57+
return err;
58+
}
59+
60+
static int tpm_bios_measurements_release(struct inode *inode,
61+
struct file *file)
62+
{
63+
struct seq_file *seq = (struct seq_file *)file->private_data;
64+
struct tpm_chip *chip = (struct tpm_chip *)seq->private;
65+
66+
put_device(&chip->dev);
67+
68+
return seq_release(inode, file);
69+
}
70+
71+
static const struct file_operations tpm_bios_measurements_ops = {
72+
.owner = THIS_MODULE,
73+
.open = tpm_bios_measurements_open,
74+
.read = seq_read,
75+
.llseek = seq_lseek,
76+
.release = tpm_bios_measurements_release,
77+
};
78+
79+
static int tpm_read_log(struct tpm_chip *chip)
80+
{
81+
int rc;
82+
83+
if (chip->log.bios_event_log != NULL) {
84+
dev_dbg(&chip->dev,
85+
"%s: ERROR - event log already initialized\n",
86+
__func__);
87+
return -EFAULT;
88+
}
89+
90+
rc = tpm_read_log_acpi(chip);
91+
if (rc != -ENODEV)
92+
return rc;
93+
94+
rc = tpm_read_log_efi(chip);
95+
if (rc != -ENODEV)
96+
return rc;
97+
98+
return tpm_read_log_of(chip);
99+
}
100+
101+
/*
102+
* tpm_bios_log_setup() - Read the event log from the firmware
103+
* @chip: TPM chip to use.
104+
*
105+
* If an event log is found then the securityfs files are setup to
106+
* export it to userspace, otherwise nothing is done.
107+
*
108+
* Returns -ENODEV if the firmware has no event log or securityfs is not
109+
* supported.
110+
*/
111+
int tpm_bios_log_setup(struct tpm_chip *chip)
112+
{
113+
const char *name = dev_name(&chip->dev);
114+
unsigned int cnt;
115+
int log_version;
116+
int rc = 0;
117+
118+
rc = tpm_read_log(chip);
119+
if (rc < 0)
120+
return rc;
121+
log_version = rc;
122+
123+
cnt = 0;
124+
chip->bios_dir[cnt] = securityfs_create_dir(name, NULL);
125+
/* NOTE: securityfs_create_dir can return ENODEV if securityfs is
126+
* compiled out. The caller should ignore the ENODEV return code.
127+
*/
128+
if (IS_ERR(chip->bios_dir[cnt]))
129+
goto err;
130+
cnt++;
131+
132+
chip->bin_log_seqops.chip = chip;
133+
if (log_version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
134+
chip->bin_log_seqops.seqops =
135+
&tpm2_binary_b_measurements_seqops;
136+
else
137+
chip->bin_log_seqops.seqops =
138+
&tpm1_binary_b_measurements_seqops;
139+
140+
141+
chip->bios_dir[cnt] =
142+
securityfs_create_file("binary_bios_measurements",
143+
0440, chip->bios_dir[0],
144+
(void *)&chip->bin_log_seqops,
145+
&tpm_bios_measurements_ops);
146+
if (IS_ERR(chip->bios_dir[cnt]))
147+
goto err;
148+
cnt++;
149+
150+
if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
151+
152+
chip->ascii_log_seqops.chip = chip;
153+
chip->ascii_log_seqops.seqops =
154+
&tpm1_ascii_b_measurements_seqops;
155+
156+
chip->bios_dir[cnt] =
157+
securityfs_create_file("ascii_bios_measurements",
158+
0440, chip->bios_dir[0],
159+
(void *)&chip->ascii_log_seqops,
160+
&tpm_bios_measurements_ops);
161+
if (IS_ERR(chip->bios_dir[cnt]))
162+
goto err;
163+
cnt++;
164+
}
165+
166+
return 0;
167+
168+
err:
169+
rc = PTR_ERR(chip->bios_dir[cnt]);
170+
chip->bios_dir[cnt] = NULL;
171+
tpm_bios_log_teardown(chip);
172+
return rc;
173+
}
174+
175+
void tpm_bios_log_teardown(struct tpm_chip *chip)
176+
{
177+
int i;
178+
struct inode *inode;
179+
180+
/* securityfs_remove currently doesn't take care of handling sync
181+
* between removal and opening of pseudo files. To handle this, a
182+
* workaround is added by making i_private = NULL here during removal
183+
* and to check it during open(), both within inode_lock()/unlock().
184+
* This design ensures that open() either safely gets kref or fails.
185+
*/
186+
for (i = (TPM_NUM_EVENT_LOG_FILES - 1); i >= 0; i--) {
187+
if (chip->bios_dir[i]) {
188+
inode = d_inode(chip->bios_dir[i]);
189+
inode_lock(inode);
190+
inode->i_private = NULL;
191+
inode_unlock(inode);
192+
securityfs_remove(chip->bios_dir[i]);
193+
}
194+
}
195+
}

drivers/char/tpm/eventlog/common.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __TPM_EVENTLOG_COMMON_H__
2+
#define __TPM_EVENTLOG_COMMON_H__
3+
4+
#include "../tpm.h"
5+
6+
extern const struct seq_operations tpm1_ascii_b_measurements_seqops;
7+
extern const struct seq_operations tpm1_binary_b_measurements_seqops;
8+
extern const struct seq_operations tpm2_binary_b_measurements_seqops;
9+
10+
#if defined(CONFIG_ACPI)
11+
int tpm_read_log_acpi(struct tpm_chip *chip);
12+
#else
13+
static inline int tpm_read_log_acpi(struct tpm_chip *chip)
14+
{
15+
return -ENODEV;
16+
}
17+
#endif
18+
#if defined(CONFIG_OF)
19+
int tpm_read_log_of(struct tpm_chip *chip);
20+
#else
21+
static inline int tpm_read_log_of(struct tpm_chip *chip)
22+
{
23+
return -ENODEV;
24+
}
25+
#endif
26+
#if defined(CONFIG_EFI)
27+
int tpm_read_log_efi(struct tpm_chip *chip);
28+
#else
29+
static inline int tpm_read_log_efi(struct tpm_chip *chip)
30+
{
31+
return -ENODEV;
32+
}
33+
#endif
34+
35+
#endif

drivers/char/tpm/tpm_eventlog_efi.c renamed to drivers/char/tpm/eventlog/efi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#include <linux/efi.h>
1515
#include <linux/tpm_eventlog.h>
1616

17-
#include "tpm.h"
17+
#include "../tpm.h"
18+
#include "common.h"
1819

1920
/* read binary bios log from EFI configuration table */
2021
int tpm_read_log_efi(struct tpm_chip *chip)
@@ -50,10 +51,9 @@ int tpm_read_log_efi(struct tpm_chip *chip)
5051
}
5152

5253
/* malloc EventLog space */
53-
log->bios_event_log = kmalloc(log_size, GFP_KERNEL);
54+
log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
5455
if (!log->bios_event_log)
5556
goto err_memunmap;
56-
memcpy(log->bios_event_log, log_tbl->log, log_size);
5757
log->bios_event_log_end = log->bios_event_log + log_size;
5858

5959
tpm_log_version = log_tbl->version;

drivers/char/tpm/tpm_eventlog_of.c renamed to drivers/char/tpm/eventlog/of.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include <linux/of.h>
2020
#include <linux/tpm_eventlog.h>
2121

22-
#include "tpm.h"
22+
#include "../tpm.h"
23+
#include "common.h"
2324

2425
int tpm_read_log_of(struct tpm_chip *chip)
2526
{
@@ -56,8 +57,8 @@ int tpm_read_log_of(struct tpm_chip *chip)
5657
* but physical tpm needs the conversion.
5758
*/
5859
if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0) {
59-
size = be32_to_cpup(sizep);
60-
base = be64_to_cpup(basep);
60+
size = be32_to_cpup((__force __be32 *)sizep);
61+
base = be64_to_cpup((__force __be64 *)basep);
6162
} else {
6263
size = *sizep;
6364
base = *basep;
@@ -68,14 +69,12 @@ int tpm_read_log_of(struct tpm_chip *chip)
6869
return -EIO;
6970
}
7071

71-
log->bios_event_log = kmalloc(size, GFP_KERNEL);
72+
log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
7273
if (!log->bios_event_log)
7374
return -ENOMEM;
7475

7576
log->bios_event_log_end = log->bios_event_log + size;
7677

77-
memcpy(log->bios_event_log, __va(base), size);
78-
7978
if (chip->flags & TPM_CHIP_FLAG_TPM2)
8079
return EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
8180
return EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;

0 commit comments

Comments
 (0)