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