tpm-dev-common.c 6.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2004 IBM Corporation
 * Authors:
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Copyright (C) 2013 Obsidian Research Corp
 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
 *
 * Device file system interface to the TPM
 */
15
#include <linux/poll.h>
16 17
#include <linux/slab.h>
#include <linux/uaccess.h>
18
#include <linux/workqueue.h>
19 20 21
#include "tpm.h"
#include "tpm-dev.h"

22 23 24
static struct workqueue_struct *tpm_dev_wq;
static DEFINE_MUTEX(tpm_dev_wq_lock);

25 26 27
static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
				u8 *buf, size_t bufsiz)
{
28 29
	struct tpm_header *header = (void *)buf;
	ssize_t ret, len;
30

31 32 33 34 35 36 37 38 39 40 41 42
	ret = tpm2_prepare_space(chip, space, buf, bufsiz);
	/* If the command is not implemented by the TPM, synthesize a
	 * response with a TPM2_RC_COMMAND_CODE return for user-space.
	 */
	if (ret == -EOPNOTSUPP) {
		header->length = cpu_to_be32(sizeof(*header));
		header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
		header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
						  TSS2_RESMGR_TPM_RC_LAYER);
		ret = sizeof(*header);
	}
	if (ret)
43
		goto out_rc;
44

45
	len = tpm_transmit(chip, buf, bufsiz);
46 47 48 49 50 51
	if (len < 0)
		ret = len;

	if (!ret)
		ret = tpm2_commit_space(chip, space, buf, &len);

52
out_rc:
53
	return ret ? ret : len;
54 55 56
}

static void tpm_dev_async_work(struct work_struct *work)
57 58 59 60 61 62 63
{
	struct file_priv *priv =
			container_of(work, struct file_priv, async_work);
	ssize_t ret;

	mutex_lock(&priv->buffer_mutex);
	priv->command_enqueued = false;
64 65 66 67 68 69
	ret = tpm_try_get_ops(priv->chip);
	if (ret) {
		priv->response_length = ret;
		goto out;
	}

70 71
	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
			       sizeof(priv->data_buffer));
72
	tpm_put_ops(priv->chip);
73 74 75 76 77 78 79

	/*
	 * If ret is > 0 then tpm_dev_transmit returned the size of the
	 * response. If ret is < 0 then tpm_dev_transmit failed and
	 * returned an error code.
	 */
	if (ret != 0) {
T
Tadeusz Struk 已提交
80
		priv->response_length = ret;
81 82
		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
	}
83
out:
84 85 86 87
	mutex_unlock(&priv->buffer_mutex);
	wake_up_interruptible(&priv->async_wait);
}

88
static void user_reader_timeout(struct timer_list *t)
89
{
90
	struct file_priv *priv = from_timer(priv, t, user_read_timer);
91 92 93 94

	pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
		task_tgid_nr(current));

95
	schedule_work(&priv->timeout_work);
96 97
}

98
static void tpm_timeout_work(struct work_struct *work)
99
{
100 101
	struct file_priv *priv = container_of(work, struct file_priv,
					      timeout_work);
102 103

	mutex_lock(&priv->buffer_mutex);
T
Tadeusz Struk 已提交
104 105
	priv->response_read = true;
	priv->response_length = 0;
106 107
	memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
	mutex_unlock(&priv->buffer_mutex);
108
	wake_up_interruptible(&priv->async_wait);
109 110 111
}

void tpm_common_open(struct file *file, struct tpm_chip *chip,
112
		     struct file_priv *priv, struct tpm_space *space)
113 114
{
	priv->chip = chip;
115
	priv->space = space;
T
Tadeusz Struk 已提交
116
	priv->response_read = true;
117

118
	mutex_init(&priv->buffer_mutex);
119
	timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
120
	INIT_WORK(&priv->timeout_work, tpm_timeout_work);
121
	INIT_WORK(&priv->async_work, tpm_dev_async_work);
122
	init_waitqueue_head(&priv->async_wait);
123 124 125 126 127 128 129
	file->private_data = priv;
}

ssize_t tpm_common_read(struct file *file, char __user *buf,
			size_t size, loff_t *off)
{
	struct file_priv *priv = file->private_data;
130
	ssize_t ret_size = 0;
131 132
	int rc;

133
	mutex_lock(&priv->buffer_mutex);
134

T
Tadeusz Struk 已提交
135 136 137 138
	if (priv->response_length) {
		priv->response_read = true;

		ret_size = min_t(ssize_t, size, priv->response_length);
139
		if (ret_size <= 0) {
T
Tadeusz Struk 已提交
140 141
			priv->response_length = 0;
			goto out;
142
		}
143

T
Tadeusz Struk 已提交
144 145 146 147 148 149 150 151 152 153
		rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
		if (rc) {
			memset(priv->data_buffer, 0, TPM_BUFSIZE);
			priv->response_length = 0;
			ret_size = -EFAULT;
		} else {
			memset(priv->data_buffer + *off, 0, ret_size);
			priv->response_length -= ret_size;
			*off += ret_size;
		}
154 155
	}

T
Tadeusz Struk 已提交
156 157 158
out:
	if (!priv->response_length) {
		*off = 0;
159
		del_timer_sync(&priv->user_read_timer);
T
Tadeusz Struk 已提交
160 161
		flush_work(&priv->timeout_work);
	}
162
	mutex_unlock(&priv->buffer_mutex);
163 164 165 166
	return ret_size;
}

ssize_t tpm_common_write(struct file *file, const char __user *buf,
167
			 size_t size, loff_t *off)
168 169
{
	struct file_priv *priv = file->private_data;
170
	int ret = 0;
171

172
	if (size > TPM_BUFSIZE)
173 174 175 176
		return -E2BIG;

	mutex_lock(&priv->buffer_mutex);

177 178 179 180
	/* Cannot perform a write until the read has cleared either via
	 * tpm_read or a user_read_timer timeout. This also prevents split
	 * buffered writes from blocking here.
	 */
T
Tadeusz Struk 已提交
181 182
	if ((!priv->response_read && priv->response_length) ||
	    priv->command_enqueued) {
183 184
		ret = -EBUSY;
		goto out;
185
	}
186

187 188 189
	if (copy_from_user(priv->data_buffer, buf, size)) {
		ret = -EFAULT;
		goto out;
190 191
	}

192 193 194 195
	if (size < 6 ||
	    size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
		ret = -EINVAL;
		goto out;
196 197
	}

T
Tadeusz Struk 已提交
198 199 200 201
	priv->response_length = 0;
	priv->response_read = false;
	*off = 0;

202 203 204 205 206 207 208 209 210
	/*
	 * If in nonblocking mode schedule an async job to send
	 * the command return the size.
	 * In case of error the err code will be returned in
	 * the subsequent read call.
	 */
	if (file->f_flags & O_NONBLOCK) {
		priv->command_enqueued = true;
		queue_work(tpm_dev_wq, &priv->async_work);
211
		mutex_unlock(&priv->buffer_mutex);
212
		return size;
213 214
	}

215 216 217 218 219 220 221 222 223
	/* atomic tpm command send and result receive. We only hold the ops
	 * lock during this period so that the tpm can be unregistered even if
	 * the char dev is held open.
	 */
	if (tpm_try_get_ops(priv->chip)) {
		ret = -EPIPE;
		goto out;
	}

224 225
	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
			       sizeof(priv->data_buffer));
226 227 228
	tpm_put_ops(priv->chip);

	if (ret > 0) {
T
Tadeusz Struk 已提交
229
		priv->response_length = ret;
230 231 232 233
		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
		ret = size;
	}
out:
234
	mutex_unlock(&priv->buffer_mutex);
235 236 237 238 239 240 241 242 243
	return ret;
}

__poll_t tpm_common_poll(struct file *file, poll_table *wait)
{
	struct file_priv *priv = file->private_data;
	__poll_t mask = 0;

	poll_wait(file, &priv->async_wait, wait);
244
	mutex_lock(&priv->buffer_mutex);
245

246 247 248 249 250 251
	/*
	 * The response_length indicates if there is still response
	 * (or part of it) to be consumed. Partial reads decrease it
	 * by the number of bytes read, and write resets it the zero.
	 */
	if (priv->response_length)
252 253 254
		mask = EPOLLIN | EPOLLRDNORM;
	else
		mask = EPOLLOUT | EPOLLWRNORM;
255

256
	mutex_unlock(&priv->buffer_mutex);
257
	return mask;
258 259 260 261 262 263 264
}

/*
 * Called on file close
 */
void tpm_common_release(struct file *file, struct file_priv *priv)
{
265
	flush_work(&priv->async_work);
266
	del_timer_sync(&priv->user_read_timer);
267
	flush_work(&priv->timeout_work);
268
	file->private_data = NULL;
T
Tadeusz Struk 已提交
269
	priv->response_length = 0;
270
}
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

int __init tpm_dev_common_init(void)
{
	tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);

	return !tpm_dev_wq ? -ENOMEM : 0;
}

void __exit tpm_dev_common_exit(void)
{
	if (tpm_dev_wq) {
		destroy_workqueue(tpm_dev_wq);
		tpm_dev_wq = NULL;
	}
}