Skip to content

Commit 74673fc

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: - Support for partial reads of /dev/tpm0. - Clean up for TPM 1.x code: move the commands to tpm1-cmd.c and make everything to use the same data structure for building TPM commands i.e. struct tpm_buf. * 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (25 commits) tpm: add support for partial reads tpm: tpm_ibmvtpm: fix kdoc warnings tpm: fix kdoc for tpm2_flush_context_cmd() tpm: tpm_try_transmit() refactor error flow. tpm: use u32 instead of int for PCR index tpm1: reimplement tpm1_continue_selftest() using tpm_buf tpm1: reimplement SAVESTATE using tpm_buf tpm1: rename tpm1_pcr_read_dev to tpm1_pcr_read() tpm1: implement tpm1_pcr_read_dev() using tpm_buf structure tpm: tpm1: rewrite tpm1_get_random() using tpm_buf structure tpm: tpm-space.c remove unneeded semicolon tpm: tpm-interface.c drop unused macros tpm: add tpm_auto_startup() into tpm-interface.c tpm: factor out tpm_startup function tpm: factor out tpm 1.x pm suspend flow into tpm1-cmd.c tpm: move tpm 1.x selftest code from tpm-interface.c tpm1-cmd.c tpm: factor out tpm1_get_random into tpm1-cmd.c tpm: move tpm_getcap to tpm1-cmd.c tpm: move tpm1_pcr_extend to tpm1-cmd.c tpm: factor out tpm_get_timeouts() ...
2 parents 19f2e26 + 0db51ef commit 74673fc

16 files changed

Lines changed: 1133 additions & 1068 deletions

File tree

‎drivers/char/tpm/Makefile‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
# Makefile for the kernel tpm device drivers.
44
#
55
obj-$(CONFIG_TCG_TPM) += tpm.o
6-
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 eventlog/common.o eventlog/tpm1.o \
8-
eventlog/tpm2.o tpm2-space.o
6+
tpm-y := tpm-chip.o
7+
tpm-y += tpm-dev-common.o
8+
tpm-y += tpm-dev.o
9+
tpm-y += tpm-interface.o
10+
tpm-y += tpm1-cmd.o
11+
tpm-y += tpm2-cmd.o
12+
tpm-y += tpmrm-dev.o
13+
tpm-y += tpm2-space.o
14+
tpm-y += tpm-sysfs.o
15+
tpm-y += eventlog/common.o
16+
tpm-y += eventlog/tpm1.o
17+
tpm-y += eventlog/tpm2.o
18+
919
tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
1020
tpm-$(CONFIG_EFI) += eventlog/efi.o
1121
tpm-$(CONFIG_OF) += eventlog/of.o

‎drivers/char/tpm/st33zp24/st33zp24.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int st33zp24_pm_resume(struct device *dev)
649649
} else {
650650
ret = tpm_pm_resume(dev);
651651
if (!ret)
652-
tpm_do_selftest(chip);
652+
tpm1_do_selftest(chip);
653653
}
654654
return ret;
655655
} /* st33zp24_pm_resume() */

‎drivers/char/tpm/tpm-chip.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,9 @@ int tpm_chip_register(struct tpm_chip *chip)
451451
{
452452
int rc;
453453

454-
if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
455-
if (chip->flags & TPM_CHIP_FLAG_TPM2)
456-
rc = tpm2_auto_startup(chip);
457-
else
458-
rc = tpm1_auto_startup(chip);
459-
if (rc)
460-
return rc;
461-
}
454+
rc = tpm_auto_startup(chip);
455+
if (rc)
456+
return rc;
462457

463458
tpm_sysfs_add_device(chip);
464459

‎drivers/char/tpm/tpm-dev-common.c‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void tpm_async_work(struct work_struct *work)
4040

4141
tpm_put_ops(priv->chip);
4242
if (ret > 0) {
43-
priv->data_pending = ret;
43+
priv->response_length = ret;
4444
mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
4545
}
4646
mutex_unlock(&priv->buffer_mutex);
@@ -63,7 +63,8 @@ static void tpm_timeout_work(struct work_struct *work)
6363
timeout_work);
6464

6565
mutex_lock(&priv->buffer_mutex);
66-
priv->data_pending = 0;
66+
priv->response_read = true;
67+
priv->response_length = 0;
6768
memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
6869
mutex_unlock(&priv->buffer_mutex);
6970
wake_up_interruptible(&priv->async_wait);
@@ -74,6 +75,7 @@ void tpm_common_open(struct file *file, struct tpm_chip *chip,
7475
{
7576
priv->chip = chip;
7677
priv->space = space;
78+
priv->response_read = true;
7779

7880
mutex_init(&priv->buffer_mutex);
7981
timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
@@ -90,22 +92,35 @@ ssize_t tpm_common_read(struct file *file, char __user *buf,
9092
ssize_t ret_size = 0;
9193
int rc;
9294

93-
del_singleshot_timer_sync(&priv->user_read_timer);
94-
flush_work(&priv->timeout_work);
9595
mutex_lock(&priv->buffer_mutex);
9696

97-
if (priv->data_pending) {
98-
ret_size = min_t(ssize_t, size, priv->data_pending);
99-
if (ret_size > 0) {
100-
rc = copy_to_user(buf, priv->data_buffer, ret_size);
101-
memset(priv->data_buffer, 0, priv->data_pending);
102-
if (rc)
103-
ret_size = -EFAULT;
97+
if (priv->response_length) {
98+
priv->response_read = true;
99+
100+
ret_size = min_t(ssize_t, size, priv->response_length);
101+
if (!ret_size) {
102+
priv->response_length = 0;
103+
goto out;
104104
}
105105

106-
priv->data_pending = 0;
106+
rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
107+
if (rc) {
108+
memset(priv->data_buffer, 0, TPM_BUFSIZE);
109+
priv->response_length = 0;
110+
ret_size = -EFAULT;
111+
} else {
112+
memset(priv->data_buffer + *off, 0, ret_size);
113+
priv->response_length -= ret_size;
114+
*off += ret_size;
115+
}
107116
}
108117

118+
out:
119+
if (!priv->response_length) {
120+
*off = 0;
121+
del_singleshot_timer_sync(&priv->user_read_timer);
122+
flush_work(&priv->timeout_work);
123+
}
109124
mutex_unlock(&priv->buffer_mutex);
110125
return ret_size;
111126
}
@@ -125,7 +140,8 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
125140
* tpm_read or a user_read_timer timeout. This also prevents split
126141
* buffered writes from blocking here.
127142
*/
128-
if (priv->data_pending != 0 || priv->command_enqueued) {
143+
if ((!priv->response_read && priv->response_length) ||
144+
priv->command_enqueued) {
129145
ret = -EBUSY;
130146
goto out;
131147
}
@@ -150,6 +166,10 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
150166
goto out;
151167
}
152168

169+
priv->response_length = 0;
170+
priv->response_read = false;
171+
*off = 0;
172+
153173
/*
154174
* If in nonblocking mode schedule an async job to send
155175
* the command return the size.
@@ -168,7 +188,7 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
168188
tpm_put_ops(priv->chip);
169189

170190
if (ret > 0) {
171-
priv->data_pending = ret;
191+
priv->response_length = ret;
172192
mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
173193
ret = size;
174194
}
@@ -184,7 +204,7 @@ __poll_t tpm_common_poll(struct file *file, poll_table *wait)
184204

185205
poll_wait(file, &priv->async_wait, wait);
186206

187-
if (priv->data_pending)
207+
if (!priv->response_read || priv->response_length)
188208
mask = EPOLLIN | EPOLLRDNORM;
189209
else
190210
mask = EPOLLOUT | EPOLLWRNORM;
@@ -201,7 +221,7 @@ void tpm_common_release(struct file *file, struct file_priv *priv)
201221
del_singleshot_timer_sync(&priv->user_read_timer);
202222
flush_work(&priv->timeout_work);
203223
file->private_data = NULL;
204-
priv->data_pending = 0;
224+
priv->response_length = 0;
205225
}
206226

207227
int __init tpm_dev_common_init(void)

‎drivers/char/tpm/tpm-dev.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ struct file_priv {
99
struct tpm_chip *chip;
1010
struct tpm_space *space;
1111

12-
/* Holds the amount of data passed or an error code from async op */
13-
ssize_t data_pending;
1412
struct mutex buffer_mutex;
15-
1613
struct timer_list user_read_timer; /* user needs to claim result */
1714
struct work_struct timeout_work;
1815
struct work_struct async_work;
1916
wait_queue_head_t async_wait;
17+
size_t response_length;
18+
bool response_read;
2019
bool command_enqueued;
2120

2221
u8 data_buffer[TPM_BUFSIZE];

0 commit comments

Comments
 (0)