• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
14  */
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/workqueue.h>
19 #include "tpm.h"
20 #include "tpm-dev.h"
21 
22 static struct workqueue_struct *tpm_dev_wq;
23 static DEFINE_MUTEX(tpm_dev_wq_lock);
24 
tpm_dev_transmit(struct tpm_chip * chip,struct tpm_space * space,u8 * buf,size_t bufsiz)25 static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
26 				u8 *buf, size_t bufsiz)
27 {
28 	struct tpm_header *header = (void *)buf;
29 	ssize_t ret, len;
30 
31 	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)
43 		goto out_rc;
44 
45 	len = tpm_transmit(chip, buf, bufsiz);
46 	if (len < 0)
47 		ret = len;
48 
49 	if (!ret)
50 		ret = tpm2_commit_space(chip, space, buf, &len);
51 	else
52 		tpm2_flush_space(chip);
53 
54 out_rc:
55 	return ret ? ret : len;
56 }
57 
tpm_dev_async_work(struct work_struct * work)58 static void tpm_dev_async_work(struct work_struct *work)
59 {
60 	struct file_priv *priv =
61 			container_of(work, struct file_priv, async_work);
62 	ssize_t ret;
63 
64 	mutex_lock(&priv->buffer_mutex);
65 	priv->command_enqueued = false;
66 	ret = tpm_try_get_ops(priv->chip);
67 	if (ret) {
68 		priv->response_length = ret;
69 		goto out;
70 	}
71 
72 	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
73 			       sizeof(priv->data_buffer));
74 	tpm_put_ops(priv->chip);
75 
76 	/*
77 	 * If ret is > 0 then tpm_dev_transmit returned the size of the
78 	 * response. If ret is < 0 then tpm_dev_transmit failed and
79 	 * returned an error code.
80 	 */
81 	if (ret != 0) {
82 		priv->response_length = ret;
83 		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
84 	}
85 out:
86 	mutex_unlock(&priv->buffer_mutex);
87 	wake_up_interruptible(&priv->async_wait);
88 }
89 
user_reader_timeout(struct timer_list * t)90 static void user_reader_timeout(struct timer_list *t)
91 {
92 	struct file_priv *priv = from_timer(priv, t, user_read_timer);
93 
94 	pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
95 		task_tgid_nr(current));
96 
97 	schedule_work(&priv->timeout_work);
98 }
99 
tpm_timeout_work(struct work_struct * work)100 static void tpm_timeout_work(struct work_struct *work)
101 {
102 	struct file_priv *priv = container_of(work, struct file_priv,
103 					      timeout_work);
104 
105 	mutex_lock(&priv->buffer_mutex);
106 	priv->response_read = true;
107 	priv->response_length = 0;
108 	memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
109 	mutex_unlock(&priv->buffer_mutex);
110 	wake_up_interruptible(&priv->async_wait);
111 }
112 
tpm_common_open(struct file * file,struct tpm_chip * chip,struct file_priv * priv,struct tpm_space * space)113 void tpm_common_open(struct file *file, struct tpm_chip *chip,
114 		     struct file_priv *priv, struct tpm_space *space)
115 {
116 	priv->chip = chip;
117 	priv->space = space;
118 	priv->response_read = true;
119 
120 	mutex_init(&priv->buffer_mutex);
121 	timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
122 	INIT_WORK(&priv->timeout_work, tpm_timeout_work);
123 	INIT_WORK(&priv->async_work, tpm_dev_async_work);
124 	init_waitqueue_head(&priv->async_wait);
125 	file->private_data = priv;
126 }
127 
tpm_common_read(struct file * file,char __user * buf,size_t size,loff_t * off)128 ssize_t tpm_common_read(struct file *file, char __user *buf,
129 			size_t size, loff_t *off)
130 {
131 	struct file_priv *priv = file->private_data;
132 	ssize_t ret_size = 0;
133 	int rc;
134 
135 	mutex_lock(&priv->buffer_mutex);
136 
137 	if (priv->response_length) {
138 		priv->response_read = true;
139 
140 		ret_size = min_t(ssize_t, size, priv->response_length);
141 		if (ret_size <= 0) {
142 			priv->response_length = 0;
143 			goto out;
144 		}
145 
146 		rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
147 		if (rc) {
148 			memset(priv->data_buffer, 0, TPM_BUFSIZE);
149 			priv->response_length = 0;
150 			ret_size = -EFAULT;
151 		} else {
152 			memset(priv->data_buffer + *off, 0, ret_size);
153 			priv->response_length -= ret_size;
154 			*off += ret_size;
155 		}
156 	}
157 
158 out:
159 	if (!priv->response_length) {
160 		*off = 0;
161 		del_singleshot_timer_sync(&priv->user_read_timer);
162 		flush_work(&priv->timeout_work);
163 	}
164 	mutex_unlock(&priv->buffer_mutex);
165 	return ret_size;
166 }
167 
tpm_common_write(struct file * file,const char __user * buf,size_t size,loff_t * off)168 ssize_t tpm_common_write(struct file *file, const char __user *buf,
169 			 size_t size, loff_t *off)
170 {
171 	struct file_priv *priv = file->private_data;
172 	int ret = 0;
173 
174 	if (size > TPM_BUFSIZE)
175 		return -E2BIG;
176 
177 	mutex_lock(&priv->buffer_mutex);
178 
179 	/* Cannot perform a write until the read has cleared either via
180 	 * tpm_read or a user_read_timer timeout. This also prevents split
181 	 * buffered writes from blocking here.
182 	 */
183 	if ((!priv->response_read && priv->response_length) ||
184 	    priv->command_enqueued) {
185 		ret = -EBUSY;
186 		goto out;
187 	}
188 
189 	if (copy_from_user(priv->data_buffer, buf, size)) {
190 		ret = -EFAULT;
191 		goto out;
192 	}
193 
194 	if (size < 6 ||
195 	    size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
196 		ret = -EINVAL;
197 		goto out;
198 	}
199 
200 	priv->response_length = 0;
201 	priv->response_read = false;
202 	*off = 0;
203 
204 	/*
205 	 * If in nonblocking mode schedule an async job to send
206 	 * the command return the size.
207 	 * In case of error the err code will be returned in
208 	 * the subsequent read call.
209 	 */
210 	if (file->f_flags & O_NONBLOCK) {
211 		priv->command_enqueued = true;
212 		queue_work(tpm_dev_wq, &priv->async_work);
213 		mutex_unlock(&priv->buffer_mutex);
214 		return size;
215 	}
216 
217 	/* atomic tpm command send and result receive. We only hold the ops
218 	 * lock during this period so that the tpm can be unregistered even if
219 	 * the char dev is held open.
220 	 */
221 	if (tpm_try_get_ops(priv->chip)) {
222 		ret = -EPIPE;
223 		goto out;
224 	}
225 
226 	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
227 			       sizeof(priv->data_buffer));
228 	tpm_put_ops(priv->chip);
229 
230 	if (ret > 0) {
231 		priv->response_length = ret;
232 		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
233 		ret = size;
234 	}
235 out:
236 	mutex_unlock(&priv->buffer_mutex);
237 	return ret;
238 }
239 
tpm_common_poll(struct file * file,poll_table * wait)240 __poll_t tpm_common_poll(struct file *file, poll_table *wait)
241 {
242 	struct file_priv *priv = file->private_data;
243 	__poll_t mask = 0;
244 
245 	poll_wait(file, &priv->async_wait, wait);
246 	mutex_lock(&priv->buffer_mutex);
247 
248 	/*
249 	 * The response_length indicates if there is still response
250 	 * (or part of it) to be consumed. Partial reads decrease it
251 	 * by the number of bytes read, and write resets it the zero.
252 	 */
253 	if (priv->response_length)
254 		mask = EPOLLIN | EPOLLRDNORM;
255 	else
256 		mask = EPOLLOUT | EPOLLWRNORM;
257 
258 	mutex_unlock(&priv->buffer_mutex);
259 	return mask;
260 }
261 
262 /*
263  * Called on file close
264  */
tpm_common_release(struct file * file,struct file_priv * priv)265 void tpm_common_release(struct file *file, struct file_priv *priv)
266 {
267 	flush_work(&priv->async_work);
268 	del_singleshot_timer_sync(&priv->user_read_timer);
269 	flush_work(&priv->timeout_work);
270 	file->private_data = NULL;
271 	priv->response_length = 0;
272 }
273 
tpm_dev_common_init(void)274 int __init tpm_dev_common_init(void)
275 {
276 	tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
277 
278 	return !tpm_dev_wq ? -ENOMEM : 0;
279 }
280 
tpm_dev_common_exit(void)281 void __exit tpm_dev_common_exit(void)
282 {
283 	if (tpm_dev_wq) {
284 		destroy_workqueue(tpm_dev_wq);
285 		tpm_dev_wq = NULL;
286 	}
287 }
288