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