Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 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 |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 14 | */ |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 15 | #include <linux/poll.h> |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/uaccess.h> |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 18 | #include <linux/workqueue.h> |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 19 | #include "tpm.h" |
| 20 | #include "tpm-dev.h" |
| 21 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 22 | static struct workqueue_struct *tpm_dev_wq; |
| 23 | static DEFINE_MUTEX(tpm_dev_wq_lock); |
| 24 | |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 25 | static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space, |
| 26 | u8 *buf, size_t bufsiz) |
| 27 | { |
Jarkko Sakkinen | 29b47ce | 2018-11-03 05:22:36 +0200 | [diff] [blame] | 28 | struct tpm_header *header = (void *)buf; |
| 29 | ssize_t ret, len; |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 30 | |
Jarkko Sakkinen | 29b47ce | 2018-11-03 05:22:36 +0200 | [diff] [blame] | 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) |
Jarkko Sakkinen | 2f25740 | 2018-11-04 20:01:42 +0200 | [diff] [blame] | 43 | goto out_rc; |
Jarkko Sakkinen | 29b47ce | 2018-11-03 05:22:36 +0200 | [diff] [blame] | 44 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 45 | len = tpm_transmit(chip, buf, bufsiz); |
Jarkko Sakkinen | 29b47ce | 2018-11-03 05:22:36 +0200 | [diff] [blame] | 46 | if (len < 0) |
| 47 | ret = len; |
| 48 | |
| 49 | if (!ret) |
| 50 | ret = tpm2_commit_space(chip, space, buf, &len); |
| 51 | |
Jarkko Sakkinen | 2f25740 | 2018-11-04 20:01:42 +0200 | [diff] [blame] | 52 | out_rc: |
Jarkko Sakkinen | 29b47ce | 2018-11-03 05:22:36 +0200 | [diff] [blame] | 53 | return ret ? ret : len; |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void tpm_dev_async_work(struct work_struct *work) |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 57 | { |
| 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; |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 64 | ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer, |
| 65 | sizeof(priv->data_buffer)); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 66 | tpm_put_ops(priv->chip); |
| 67 | if (ret > 0) { |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 68 | priv->response_length = ret; |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 69 | mod_timer(&priv->user_read_timer, jiffies + (120 * HZ)); |
| 70 | } |
| 71 | mutex_unlock(&priv->buffer_mutex); |
| 72 | wake_up_interruptible(&priv->async_wait); |
| 73 | } |
| 74 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 75 | static void user_reader_timeout(struct timer_list *t) |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 76 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 77 | struct file_priv *priv = from_timer(priv, t, user_read_timer); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 78 | |
| 79 | pr_warn("TPM user space timeout is deprecated (pid=%d)\n", |
| 80 | task_tgid_nr(current)); |
| 81 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 82 | schedule_work(&priv->timeout_work); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 85 | static void tpm_timeout_work(struct work_struct *work) |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 86 | { |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 87 | struct file_priv *priv = container_of(work, struct file_priv, |
| 88 | timeout_work); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 89 | |
| 90 | mutex_lock(&priv->buffer_mutex); |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 91 | priv->response_read = true; |
| 92 | priv->response_length = 0; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 93 | memset(priv->data_buffer, 0, sizeof(priv->data_buffer)); |
| 94 | mutex_unlock(&priv->buffer_mutex); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 95 | wake_up_interruptible(&priv->async_wait); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void tpm_common_open(struct file *file, struct tpm_chip *chip, |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 99 | struct file_priv *priv, struct tpm_space *space) |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 100 | { |
| 101 | priv->chip = chip; |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 102 | priv->space = space; |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 103 | priv->response_read = true; |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 104 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 105 | mutex_init(&priv->buffer_mutex); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 106 | timer_setup(&priv->user_read_timer, user_reader_timeout, 0); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 107 | INIT_WORK(&priv->timeout_work, tpm_timeout_work); |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 108 | INIT_WORK(&priv->async_work, tpm_dev_async_work); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 109 | init_waitqueue_head(&priv->async_wait); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 110 | file->private_data = priv; |
| 111 | } |
| 112 | |
| 113 | ssize_t tpm_common_read(struct file *file, char __user *buf, |
| 114 | size_t size, loff_t *off) |
| 115 | { |
| 116 | struct file_priv *priv = file->private_data; |
Tadeusz Struk | 3ab2011 | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 117 | ssize_t ret_size = 0; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 118 | int rc; |
| 119 | |
Tadeusz Struk | 3ab2011 | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 120 | mutex_lock(&priv->buffer_mutex); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 121 | |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 122 | if (priv->response_length) { |
| 123 | priv->response_read = true; |
| 124 | |
| 125 | ret_size = min_t(ssize_t, size, priv->response_length); |
| 126 | if (!ret_size) { |
| 127 | priv->response_length = 0; |
| 128 | goto out; |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 129 | } |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 130 | |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 131 | rc = copy_to_user(buf, priv->data_buffer + *off, ret_size); |
| 132 | if (rc) { |
| 133 | memset(priv->data_buffer, 0, TPM_BUFSIZE); |
| 134 | priv->response_length = 0; |
| 135 | ret_size = -EFAULT; |
| 136 | } else { |
| 137 | memset(priv->data_buffer + *off, 0, ret_size); |
| 138 | priv->response_length -= ret_size; |
| 139 | *off += ret_size; |
| 140 | } |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 143 | out: |
| 144 | if (!priv->response_length) { |
| 145 | *off = 0; |
| 146 | del_singleshot_timer_sync(&priv->user_read_timer); |
| 147 | flush_work(&priv->timeout_work); |
| 148 | } |
Tadeusz Struk | 3ab2011 | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 149 | mutex_unlock(&priv->buffer_mutex); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 150 | return ret_size; |
| 151 | } |
| 152 | |
| 153 | ssize_t tpm_common_write(struct file *file, const char __user *buf, |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 154 | size_t size, loff_t *off) |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 155 | { |
| 156 | struct file_priv *priv = file->private_data; |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 157 | int ret = 0; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 158 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 159 | if (size > TPM_BUFSIZE) |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 160 | return -E2BIG; |
| 161 | |
| 162 | mutex_lock(&priv->buffer_mutex); |
| 163 | |
Tadeusz Struk | 3ab2011 | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 164 | /* Cannot perform a write until the read has cleared either via |
| 165 | * tpm_read or a user_read_timer timeout. This also prevents split |
| 166 | * buffered writes from blocking here. |
| 167 | */ |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 168 | if ((!priv->response_read && priv->response_length) || |
| 169 | priv->command_enqueued) { |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 170 | ret = -EBUSY; |
| 171 | goto out; |
Tadeusz Struk | 3ab2011 | 2018-05-22 14:37:18 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 174 | if (copy_from_user(priv->data_buffer, buf, size)) { |
| 175 | ret = -EFAULT; |
| 176 | goto out; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 179 | if (size < 6 || |
| 180 | size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) { |
| 181 | ret = -EINVAL; |
| 182 | goto out; |
Alexander Steffen | ee70bc1 | 2017-09-08 17:21:32 +0200 | [diff] [blame] | 183 | } |
| 184 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 185 | /* atomic tpm command send and result receive. We only hold the ops |
| 186 | * lock during this period so that the tpm can be unregistered even if |
| 187 | * the char dev is held open. |
| 188 | */ |
| 189 | if (tpm_try_get_ops(priv->chip)) { |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 190 | ret = -EPIPE; |
| 191 | goto out; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 192 | } |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 193 | |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 194 | priv->response_length = 0; |
| 195 | priv->response_read = false; |
| 196 | *off = 0; |
| 197 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 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 | |
Jarkko Sakkinen | c4df71d | 2018-11-03 04:46:50 +0200 | [diff] [blame] | 211 | ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer, |
| 212 | sizeof(priv->data_buffer)); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 213 | tpm_put_ops(priv->chip); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 214 | |
| 215 | if (ret > 0) { |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 216 | priv->response_length = ret; |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 217 | mod_timer(&priv->user_read_timer, jiffies + (120 * HZ)); |
| 218 | ret = size; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 219 | } |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 220 | out: |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 221 | mutex_unlock(&priv->buffer_mutex); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 222 | return ret; |
| 223 | } |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 224 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 225 | __poll_t tpm_common_poll(struct file *file, poll_table *wait) |
| 226 | { |
| 227 | struct file_priv *priv = file->private_data; |
| 228 | __poll_t mask = 0; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 229 | |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 230 | poll_wait(file, &priv->async_wait, wait); |
Tadeusz Struk | 7110629 | 2019-03-27 11:32:38 -0700 | [diff] [blame] | 231 | mutex_lock(&priv->buffer_mutex); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 232 | |
Tadeusz Struk | 7110629 | 2019-03-27 11:32:38 -0700 | [diff] [blame] | 233 | /* |
| 234 | * The response_length indicates if there is still response |
| 235 | * (or part of it) to be consumed. Partial reads decrease it |
| 236 | * by the number of bytes read, and write resets it the zero. |
| 237 | */ |
| 238 | if (priv->response_length) |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 239 | mask = EPOLLIN | EPOLLRDNORM; |
| 240 | else |
| 241 | mask = EPOLLOUT | EPOLLWRNORM; |
| 242 | |
Tadeusz Struk | 7110629 | 2019-03-27 11:32:38 -0700 | [diff] [blame] | 243 | mutex_unlock(&priv->buffer_mutex); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 244 | return mask; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Called on file close |
| 249 | */ |
| 250 | void tpm_common_release(struct file *file, struct file_priv *priv) |
| 251 | { |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 252 | flush_work(&priv->async_work); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 253 | del_singleshot_timer_sync(&priv->user_read_timer); |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 254 | flush_work(&priv->timeout_work); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 255 | file->private_data = NULL; |
Tadeusz Struk | 9488585 | 2018-11-21 06:50:37 -0800 | [diff] [blame] | 256 | priv->response_length = 0; |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 257 | } |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 258 | |
| 259 | int __init tpm_dev_common_init(void) |
| 260 | { |
| 261 | tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0); |
| 262 | |
| 263 | return !tpm_dev_wq ? -ENOMEM : 0; |
| 264 | } |
| 265 | |
| 266 | void __exit tpm_dev_common_exit(void) |
| 267 | { |
| 268 | if (tpm_dev_wq) { |
| 269 | destroy_workqueue(tpm_dev_wq); |
| 270 | tpm_dev_wq = NULL; |
| 271 | } |
| 272 | } |