blob: 1784530b8387bb46bec8694a49ddb5385833be80 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
James Bottomleyecb38e22017-01-10 19:08:53 -08002/*
3 * Copyright (C) 2004 IBM Corporation
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 *
10 * Copyright (C) 2013 Obsidian Research Corp
11 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 *
13 * Device file system interface to the TPM
James Bottomleyecb38e22017-01-10 19:08:53 -080014 */
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070015#include <linux/poll.h>
James Bottomleyecb38e22017-01-10 19:08:53 -080016#include <linux/slab.h>
17#include <linux/uaccess.h>
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070018#include <linux/workqueue.h>
James Bottomleyecb38e22017-01-10 19:08:53 -080019#include "tpm.h"
20#include "tpm-dev.h"
21
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070022static struct workqueue_struct *tpm_dev_wq;
23static DEFINE_MUTEX(tpm_dev_wq_lock);
24
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +020025static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
26 u8 *buf, size_t bufsiz)
27{
Jarkko Sakkinen29b47ce2018-11-03 05:22:36 +020028 struct tpm_header *header = (void *)buf;
29 ssize_t ret, len;
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +020030
Jarkko Sakkinen29b47ce2018-11-03 05:22:36 +020031 ret = tpm2_prepare_space(chip, space, buf, bufsiz);
32 /* If the command is not implemented by the TPM, synthesize a
33 * response with a TPM2_RC_COMMAND_CODE return for user-space.
34 */
35 if (ret == -EOPNOTSUPP) {
36 header->length = cpu_to_be32(sizeof(*header));
37 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
38 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
39 TSS2_RESMGR_TPM_RC_LAYER);
40 ret = sizeof(*header);
41 }
42 if (ret)
Jarkko Sakkinen2f257402018-11-04 20:01:42 +020043 goto out_rc;
Jarkko Sakkinen29b47ce2018-11-03 05:22:36 +020044
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +020045 len = tpm_transmit(chip, buf, bufsiz);
Jarkko Sakkinen29b47ce2018-11-03 05:22:36 +020046 if (len < 0)
47 ret = len;
48
49 if (!ret)
50 ret = tpm2_commit_space(chip, space, buf, &len);
51
Jarkko Sakkinen2f257402018-11-04 20:01:42 +020052out_rc:
Jarkko Sakkinen29b47ce2018-11-03 05:22:36 +020053 return ret ? ret : len;
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +020054}
55
56static void tpm_dev_async_work(struct work_struct *work)
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070057{
58 struct file_priv *priv =
59 container_of(work, struct file_priv, async_work);
60 ssize_t ret;
61
62 mutex_lock(&priv->buffer_mutex);
63 priv->command_enqueued = false;
Tadeusz Strukd23d1242019-12-12 09:48:47 -080064 ret = tpm_try_get_ops(priv->chip);
65 if (ret) {
66 priv->response_length = ret;
67 goto out;
68 }
69
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +020070 ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
71 sizeof(priv->data_buffer));
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070072 tpm_put_ops(priv->chip);
73 if (ret > 0) {
Tadeusz Struk94885852018-11-21 06:50:37 -080074 priv->response_length = ret;
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070075 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
76 }
Tadeusz Strukd23d1242019-12-12 09:48:47 -080077out:
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070078 mutex_unlock(&priv->buffer_mutex);
79 wake_up_interruptible(&priv->async_wait);
80}
81
Kees Cooke99e88a2017-10-16 14:43:17 -070082static void user_reader_timeout(struct timer_list *t)
James Bottomleyecb38e22017-01-10 19:08:53 -080083{
Kees Cooke99e88a2017-10-16 14:43:17 -070084 struct file_priv *priv = from_timer(priv, t, user_read_timer);
James Bottomleyecb38e22017-01-10 19:08:53 -080085
86 pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
87 task_tgid_nr(current));
88
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070089 schedule_work(&priv->timeout_work);
James Bottomleyecb38e22017-01-10 19:08:53 -080090}
91
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070092static void tpm_timeout_work(struct work_struct *work)
James Bottomleyecb38e22017-01-10 19:08:53 -080093{
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070094 struct file_priv *priv = container_of(work, struct file_priv,
95 timeout_work);
James Bottomleyecb38e22017-01-10 19:08:53 -080096
97 mutex_lock(&priv->buffer_mutex);
Tadeusz Struk94885852018-11-21 06:50:37 -080098 priv->response_read = true;
99 priv->response_length = 0;
James Bottomleyecb38e22017-01-10 19:08:53 -0800100 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
101 mutex_unlock(&priv->buffer_mutex);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700102 wake_up_interruptible(&priv->async_wait);
James Bottomleyecb38e22017-01-10 19:08:53 -0800103}
104
105void tpm_common_open(struct file *file, struct tpm_chip *chip,
Tadeusz Strukc3d477a2018-09-10 10:18:28 -0700106 struct file_priv *priv, struct tpm_space *space)
James Bottomleyecb38e22017-01-10 19:08:53 -0800107{
108 priv->chip = chip;
Tadeusz Strukc3d477a2018-09-10 10:18:28 -0700109 priv->space = space;
Tadeusz Struk94885852018-11-21 06:50:37 -0800110 priv->response_read = true;
Tadeusz Strukc3d477a2018-09-10 10:18:28 -0700111
James Bottomleyecb38e22017-01-10 19:08:53 -0800112 mutex_init(&priv->buffer_mutex);
Kees Cooke99e88a2017-10-16 14:43:17 -0700113 timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700114 INIT_WORK(&priv->timeout_work, tpm_timeout_work);
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +0200115 INIT_WORK(&priv->async_work, tpm_dev_async_work);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700116 init_waitqueue_head(&priv->async_wait);
James Bottomleyecb38e22017-01-10 19:08:53 -0800117 file->private_data = priv;
118}
119
120ssize_t tpm_common_read(struct file *file, char __user *buf,
121 size_t size, loff_t *off)
122{
123 struct file_priv *priv = file->private_data;
Tadeusz Struk3ab20112018-05-22 14:37:18 -0700124 ssize_t ret_size = 0;
James Bottomleyecb38e22017-01-10 19:08:53 -0800125 int rc;
126
Tadeusz Struk3ab20112018-05-22 14:37:18 -0700127 mutex_lock(&priv->buffer_mutex);
James Bottomleyecb38e22017-01-10 19:08:53 -0800128
Tadeusz Struk94885852018-11-21 06:50:37 -0800129 if (priv->response_length) {
130 priv->response_read = true;
131
132 ret_size = min_t(ssize_t, size, priv->response_length);
Tadeusz Struka430e672020-01-07 14:04:48 -0800133 if (ret_size <= 0) {
Tadeusz Struk94885852018-11-21 06:50:37 -0800134 priv->response_length = 0;
135 goto out;
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700136 }
James Bottomleyecb38e22017-01-10 19:08:53 -0800137
Tadeusz Struk94885852018-11-21 06:50:37 -0800138 rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
139 if (rc) {
140 memset(priv->data_buffer, 0, TPM_BUFSIZE);
141 priv->response_length = 0;
142 ret_size = -EFAULT;
143 } else {
144 memset(priv->data_buffer + *off, 0, ret_size);
145 priv->response_length -= ret_size;
146 *off += ret_size;
147 }
James Bottomleyecb38e22017-01-10 19:08:53 -0800148 }
149
Tadeusz Struk94885852018-11-21 06:50:37 -0800150out:
151 if (!priv->response_length) {
152 *off = 0;
153 del_singleshot_timer_sync(&priv->user_read_timer);
154 flush_work(&priv->timeout_work);
155 }
Tadeusz Struk3ab20112018-05-22 14:37:18 -0700156 mutex_unlock(&priv->buffer_mutex);
James Bottomleyecb38e22017-01-10 19:08:53 -0800157 return ret_size;
158}
159
160ssize_t tpm_common_write(struct file *file, const char __user *buf,
Tadeusz Strukc3d477a2018-09-10 10:18:28 -0700161 size_t size, loff_t *off)
James Bottomleyecb38e22017-01-10 19:08:53 -0800162{
163 struct file_priv *priv = file->private_data;
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700164 int ret = 0;
James Bottomleyecb38e22017-01-10 19:08:53 -0800165
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700166 if (size > TPM_BUFSIZE)
James Bottomleyecb38e22017-01-10 19:08:53 -0800167 return -E2BIG;
168
169 mutex_lock(&priv->buffer_mutex);
170
Tadeusz Struk3ab20112018-05-22 14:37:18 -0700171 /* Cannot perform a write until the read has cleared either via
172 * tpm_read or a user_read_timer timeout. This also prevents split
173 * buffered writes from blocking here.
174 */
Tadeusz Struk94885852018-11-21 06:50:37 -0800175 if ((!priv->response_read && priv->response_length) ||
176 priv->command_enqueued) {
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700177 ret = -EBUSY;
178 goto out;
Tadeusz Struk3ab20112018-05-22 14:37:18 -0700179 }
180
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700181 if (copy_from_user(priv->data_buffer, buf, size)) {
182 ret = -EFAULT;
183 goto out;
James Bottomleyecb38e22017-01-10 19:08:53 -0800184 }
185
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700186 if (size < 6 ||
187 size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
188 ret = -EINVAL;
189 goto out;
Alexander Steffenee70bc12017-09-08 17:21:32 +0200190 }
191
Tadeusz Struk94885852018-11-21 06:50:37 -0800192 priv->response_length = 0;
193 priv->response_read = false;
194 *off = 0;
195
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700196 /*
197 * If in nonblocking mode schedule an async job to send
198 * the command return the size.
199 * In case of error the err code will be returned in
200 * the subsequent read call.
201 */
202 if (file->f_flags & O_NONBLOCK) {
203 priv->command_enqueued = true;
204 queue_work(tpm_dev_wq, &priv->async_work);
205 mutex_unlock(&priv->buffer_mutex);
206 return size;
207 }
208
James Bottomley78628402020-05-28 11:10:57 -0700209 /* atomic tpm command send and result receive. We only hold the ops
210 * lock during this period so that the tpm can be unregistered even if
211 * the char dev is held open.
212 */
213 if (tpm_try_get_ops(priv->chip)) {
214 ret = -EPIPE;
215 goto out;
216 }
217
Jarkko Sakkinenc4df71d2018-11-03 04:46:50 +0200218 ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
219 sizeof(priv->data_buffer));
James Bottomleyecb38e22017-01-10 19:08:53 -0800220 tpm_put_ops(priv->chip);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700221
222 if (ret > 0) {
Tadeusz Struk94885852018-11-21 06:50:37 -0800223 priv->response_length = ret;
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700224 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
225 ret = size;
James Bottomleyecb38e22017-01-10 19:08:53 -0800226 }
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700227out:
James Bottomleyecb38e22017-01-10 19:08:53 -0800228 mutex_unlock(&priv->buffer_mutex);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700229 return ret;
230}
James Bottomleyecb38e22017-01-10 19:08:53 -0800231
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700232__poll_t tpm_common_poll(struct file *file, poll_table *wait)
233{
234 struct file_priv *priv = file->private_data;
235 __poll_t mask = 0;
James Bottomleyecb38e22017-01-10 19:08:53 -0800236
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700237 poll_wait(file, &priv->async_wait, wait);
Tadeusz Struk71106292019-03-27 11:32:38 -0700238 mutex_lock(&priv->buffer_mutex);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700239
Tadeusz Struk71106292019-03-27 11:32:38 -0700240 /*
241 * The response_length indicates if there is still response
242 * (or part of it) to be consumed. Partial reads decrease it
243 * by the number of bytes read, and write resets it the zero.
244 */
245 if (priv->response_length)
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700246 mask = EPOLLIN | EPOLLRDNORM;
247 else
248 mask = EPOLLOUT | EPOLLWRNORM;
249
Tadeusz Struk71106292019-03-27 11:32:38 -0700250 mutex_unlock(&priv->buffer_mutex);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700251 return mask;
James Bottomleyecb38e22017-01-10 19:08:53 -0800252}
253
254/*
255 * Called on file close
256 */
257void tpm_common_release(struct file *file, struct file_priv *priv)
258{
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700259 flush_work(&priv->async_work);
James Bottomleyecb38e22017-01-10 19:08:53 -0800260 del_singleshot_timer_sync(&priv->user_read_timer);
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700261 flush_work(&priv->timeout_work);
James Bottomleyecb38e22017-01-10 19:08:53 -0800262 file->private_data = NULL;
Tadeusz Struk94885852018-11-21 06:50:37 -0800263 priv->response_length = 0;
James Bottomleyecb38e22017-01-10 19:08:53 -0800264}
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -0700265
266int __init tpm_dev_common_init(void)
267{
268 tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
269
270 return !tpm_dev_wq ? -ENOMEM : 0;
271}
272
273void __exit tpm_dev_common_exit(void)
274{
275 if (tpm_dev_wq) {
276 destroy_workqueue(tpm_dev_wq);
277 tpm_dev_wq = NULL;
278 }
279}