Linux 4.8-rc8
[linux/fpc-iii.git] / drivers / char / tpm / tpm_vtpm_proxy.c
blob9a940332c15758ae3f818301eea526a79d4dff9d
1 /*
2 * Copyright (C) 2015, 2016 IBM Corporation
4 * Author: Stefan Berger <stefanb@us.ibm.com>
6 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 * Device driver for vTPM (vTPM proxy driver)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2 of the
13 * License.
17 #include <linux/types.h>
18 #include <linux/spinlock.h>
19 #include <linux/uaccess.h>
20 #include <linux/wait.h>
21 #include <linux/miscdevice.h>
22 #include <linux/vtpm_proxy.h>
23 #include <linux/file.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/poll.h>
26 #include <linux/compat.h>
28 #include "tpm.h"
30 #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
32 struct proxy_dev {
33 struct tpm_chip *chip;
35 u32 flags; /* public API flags */
37 wait_queue_head_t wq;
39 struct mutex buf_lock; /* protect buffer and flags */
41 long state; /* internal state */
42 #define STATE_OPENED_FLAG BIT(0)
43 #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
45 size_t req_len; /* length of queued TPM request */
46 size_t resp_len; /* length of queued TPM response */
47 u8 buffer[TPM_BUFSIZE]; /* request/response buffer */
49 struct work_struct work; /* task that retrieves TPM timeouts */
52 /* all supported flags */
53 #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
55 static struct workqueue_struct *workqueue;
57 static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev);
60 * Functions related to 'server side'
63 /**
64 * vtpm_proxy_fops_read - Read TPM commands on 'server side'
66 * Return value:
67 * Number of bytes read or negative error code
69 static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf,
70 size_t count, loff_t *off)
72 struct proxy_dev *proxy_dev = filp->private_data;
73 size_t len;
74 int sig, rc;
76 sig = wait_event_interruptible(proxy_dev->wq,
77 proxy_dev->req_len != 0 ||
78 !(proxy_dev->state & STATE_OPENED_FLAG));
79 if (sig)
80 return -EINTR;
82 mutex_lock(&proxy_dev->buf_lock);
84 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
85 mutex_unlock(&proxy_dev->buf_lock);
86 return -EPIPE;
89 len = proxy_dev->req_len;
91 if (count < len) {
92 mutex_unlock(&proxy_dev->buf_lock);
93 pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
94 count, len);
95 return -EIO;
98 rc = copy_to_user(buf, proxy_dev->buffer, len);
99 memset(proxy_dev->buffer, 0, len);
100 proxy_dev->req_len = 0;
102 if (!rc)
103 proxy_dev->state |= STATE_WAIT_RESPONSE_FLAG;
105 mutex_unlock(&proxy_dev->buf_lock);
107 if (rc)
108 return -EFAULT;
110 return len;
114 * vtpm_proxy_fops_write - Write TPM responses on 'server side'
116 * Return value:
117 * Number of bytes read or negative error value
119 static ssize_t vtpm_proxy_fops_write(struct file *filp, const char __user *buf,
120 size_t count, loff_t *off)
122 struct proxy_dev *proxy_dev = filp->private_data;
124 mutex_lock(&proxy_dev->buf_lock);
126 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
127 mutex_unlock(&proxy_dev->buf_lock);
128 return -EPIPE;
131 if (count > sizeof(proxy_dev->buffer) ||
132 !(proxy_dev->state & STATE_WAIT_RESPONSE_FLAG)) {
133 mutex_unlock(&proxy_dev->buf_lock);
134 return -EIO;
137 proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
139 proxy_dev->req_len = 0;
141 if (copy_from_user(proxy_dev->buffer, buf, count)) {
142 mutex_unlock(&proxy_dev->buf_lock);
143 return -EFAULT;
146 proxy_dev->resp_len = count;
148 mutex_unlock(&proxy_dev->buf_lock);
150 wake_up_interruptible(&proxy_dev->wq);
152 return count;
156 * vtpm_proxy_fops_poll: Poll status on 'server side'
158 * Return value:
159 * Poll flags
161 static unsigned int vtpm_proxy_fops_poll(struct file *filp, poll_table *wait)
163 struct proxy_dev *proxy_dev = filp->private_data;
164 unsigned ret;
166 poll_wait(filp, &proxy_dev->wq, wait);
168 ret = POLLOUT;
170 mutex_lock(&proxy_dev->buf_lock);
172 if (proxy_dev->req_len)
173 ret |= POLLIN | POLLRDNORM;
175 if (!(proxy_dev->state & STATE_OPENED_FLAG))
176 ret |= POLLHUP;
178 mutex_unlock(&proxy_dev->buf_lock);
180 return ret;
184 * vtpm_proxy_fops_open - Open vTPM device on 'server side'
186 * Called when setting up the anonymous file descriptor
188 static void vtpm_proxy_fops_open(struct file *filp)
190 struct proxy_dev *proxy_dev = filp->private_data;
192 proxy_dev->state |= STATE_OPENED_FLAG;
196 * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
198 * Call to undo vtpm_proxy_fops_open
200 static void vtpm_proxy_fops_undo_open(struct proxy_dev *proxy_dev)
202 mutex_lock(&proxy_dev->buf_lock);
204 proxy_dev->state &= ~STATE_OPENED_FLAG;
206 mutex_unlock(&proxy_dev->buf_lock);
208 /* no more TPM responses -- wake up anyone waiting for them */
209 wake_up_interruptible(&proxy_dev->wq);
213 * vtpm_proxy_fops_release: Close 'server side'
215 * Return value:
216 * Always returns 0.
218 static int vtpm_proxy_fops_release(struct inode *inode, struct file *filp)
220 struct proxy_dev *proxy_dev = filp->private_data;
222 filp->private_data = NULL;
224 vtpm_proxy_delete_device(proxy_dev);
226 return 0;
229 static const struct file_operations vtpm_proxy_fops = {
230 .owner = THIS_MODULE,
231 .llseek = no_llseek,
232 .read = vtpm_proxy_fops_read,
233 .write = vtpm_proxy_fops_write,
234 .poll = vtpm_proxy_fops_poll,
235 .release = vtpm_proxy_fops_release,
239 * Functions invoked by the core TPM driver to send TPM commands to
240 * 'server side' and receive responses from there.
244 * Called when core TPM driver reads TPM responses from 'server side'
246 * Return value:
247 * Number of TPM response bytes read, negative error value otherwise
249 static int vtpm_proxy_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
251 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
252 size_t len;
254 /* process gone ? */
255 mutex_lock(&proxy_dev->buf_lock);
257 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
258 mutex_unlock(&proxy_dev->buf_lock);
259 return -EPIPE;
262 len = proxy_dev->resp_len;
263 if (count < len) {
264 dev_err(&chip->dev,
265 "Invalid size in recv: count=%zd, resp_len=%zd\n",
266 count, len);
267 len = -EIO;
268 goto out;
271 memcpy(buf, proxy_dev->buffer, len);
272 proxy_dev->resp_len = 0;
274 out:
275 mutex_unlock(&proxy_dev->buf_lock);
277 return len;
281 * Called when core TPM driver forwards TPM requests to 'server side'.
283 * Return value:
284 * 0 in case of success, negative error value otherwise.
286 static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t count)
288 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
289 int rc = 0;
291 if (count > sizeof(proxy_dev->buffer)) {
292 dev_err(&chip->dev,
293 "Invalid size in send: count=%zd, buffer size=%zd\n",
294 count, sizeof(proxy_dev->buffer));
295 return -EIO;
298 mutex_lock(&proxy_dev->buf_lock);
300 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
301 mutex_unlock(&proxy_dev->buf_lock);
302 return -EPIPE;
305 proxy_dev->resp_len = 0;
307 proxy_dev->req_len = count;
308 memcpy(proxy_dev->buffer, buf, count);
310 proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
312 mutex_unlock(&proxy_dev->buf_lock);
314 wake_up_interruptible(&proxy_dev->wq);
316 return rc;
319 static void vtpm_proxy_tpm_op_cancel(struct tpm_chip *chip)
321 /* not supported */
324 static u8 vtpm_proxy_tpm_op_status(struct tpm_chip *chip)
326 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
328 if (proxy_dev->resp_len)
329 return VTPM_PROXY_REQ_COMPLETE_FLAG;
331 return 0;
334 static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)
336 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
337 bool ret;
339 mutex_lock(&proxy_dev->buf_lock);
341 ret = !(proxy_dev->state & STATE_OPENED_FLAG);
343 mutex_unlock(&proxy_dev->buf_lock);
345 return ret;
348 static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
349 .flags = TPM_OPS_AUTO_STARTUP,
350 .recv = vtpm_proxy_tpm_op_recv,
351 .send = vtpm_proxy_tpm_op_send,
352 .cancel = vtpm_proxy_tpm_op_cancel,
353 .status = vtpm_proxy_tpm_op_status,
354 .req_complete_mask = VTPM_PROXY_REQ_COMPLETE_FLAG,
355 .req_complete_val = VTPM_PROXY_REQ_COMPLETE_FLAG,
356 .req_canceled = vtpm_proxy_tpm_req_canceled,
360 * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
361 * retrieval of timeouts and durations.
364 static void vtpm_proxy_work(struct work_struct *work)
366 struct proxy_dev *proxy_dev = container_of(work, struct proxy_dev,
367 work);
368 int rc;
370 rc = tpm_chip_register(proxy_dev->chip);
371 if (rc)
372 goto err;
374 return;
376 err:
377 vtpm_proxy_fops_undo_open(proxy_dev);
381 * vtpm_proxy_work_stop: make sure the work has finished
383 * This function is useful when user space closed the fd
384 * while the driver still determines timeouts.
386 static void vtpm_proxy_work_stop(struct proxy_dev *proxy_dev)
388 vtpm_proxy_fops_undo_open(proxy_dev);
389 flush_work(&proxy_dev->work);
393 * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
395 static inline void vtpm_proxy_work_start(struct proxy_dev *proxy_dev)
397 queue_work(workqueue, &proxy_dev->work);
401 * Code related to creation and deletion of device pairs
403 static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
405 struct proxy_dev *proxy_dev;
406 struct tpm_chip *chip;
407 int err;
409 proxy_dev = kzalloc(sizeof(*proxy_dev), GFP_KERNEL);
410 if (proxy_dev == NULL)
411 return ERR_PTR(-ENOMEM);
413 init_waitqueue_head(&proxy_dev->wq);
414 mutex_init(&proxy_dev->buf_lock);
415 INIT_WORK(&proxy_dev->work, vtpm_proxy_work);
417 chip = tpm_chip_alloc(NULL, &vtpm_proxy_tpm_ops);
418 if (IS_ERR(chip)) {
419 err = PTR_ERR(chip);
420 goto err_proxy_dev_free;
422 dev_set_drvdata(&chip->dev, proxy_dev);
424 proxy_dev->chip = chip;
426 return proxy_dev;
428 err_proxy_dev_free:
429 kfree(proxy_dev);
431 return ERR_PTR(err);
435 * Undo what has been done in vtpm_create_proxy_dev
437 static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
439 put_device(&proxy_dev->chip->dev); /* frees chip */
440 kfree(proxy_dev);
444 * Create a /dev/tpm%d and 'server side' file descriptor pair
446 * Return value:
447 * Returns file pointer on success, an error value otherwise
449 static struct file *vtpm_proxy_create_device(
450 struct vtpm_proxy_new_dev *vtpm_new_dev)
452 struct proxy_dev *proxy_dev;
453 int rc, fd;
454 struct file *file;
456 if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
457 return ERR_PTR(-EOPNOTSUPP);
459 proxy_dev = vtpm_proxy_create_proxy_dev();
460 if (IS_ERR(proxy_dev))
461 return ERR_CAST(proxy_dev);
463 proxy_dev->flags = vtpm_new_dev->flags;
465 /* setup an anonymous file for the server-side */
466 fd = get_unused_fd_flags(O_RDWR);
467 if (fd < 0) {
468 rc = fd;
469 goto err_delete_proxy_dev;
472 file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
473 O_RDWR);
474 if (IS_ERR(file)) {
475 rc = PTR_ERR(file);
476 goto err_put_unused_fd;
479 /* from now on we can unwind with put_unused_fd() + fput() */
480 /* simulate an open() on the server side */
481 vtpm_proxy_fops_open(file);
483 if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
484 proxy_dev->chip->flags |= TPM_CHIP_FLAG_TPM2;
486 vtpm_proxy_work_start(proxy_dev);
488 vtpm_new_dev->fd = fd;
489 vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
490 vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
491 vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
493 return file;
495 err_put_unused_fd:
496 put_unused_fd(fd);
498 err_delete_proxy_dev:
499 vtpm_proxy_delete_proxy_dev(proxy_dev);
501 return ERR_PTR(rc);
505 * Counter part to vtpm_create_device.
507 static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)
509 vtpm_proxy_work_stop(proxy_dev);
512 * A client may hold the 'ops' lock, so let it know that the server
513 * side shuts down before we try to grab the 'ops' lock when
514 * unregistering the chip.
516 vtpm_proxy_fops_undo_open(proxy_dev);
518 tpm_chip_unregister(proxy_dev->chip);
520 vtpm_proxy_delete_proxy_dev(proxy_dev);
524 * Code related to the control device /dev/vtpmx
528 * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
530 * Return value:
531 * Returns 0 on success, a negative error code otherwise.
533 static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,
534 unsigned long arg)
536 void __user *argp = (void __user *)arg;
537 struct vtpm_proxy_new_dev __user *vtpm_new_dev_p;
538 struct vtpm_proxy_new_dev vtpm_new_dev;
539 struct file *file;
541 switch (ioctl) {
542 case VTPM_PROXY_IOC_NEW_DEV:
543 if (!capable(CAP_SYS_ADMIN))
544 return -EPERM;
545 vtpm_new_dev_p = argp;
546 if (copy_from_user(&vtpm_new_dev, vtpm_new_dev_p,
547 sizeof(vtpm_new_dev)))
548 return -EFAULT;
549 file = vtpm_proxy_create_device(&vtpm_new_dev);
550 if (IS_ERR(file))
551 return PTR_ERR(file);
552 if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
553 sizeof(vtpm_new_dev))) {
554 put_unused_fd(vtpm_new_dev.fd);
555 fput(file);
556 return -EFAULT;
559 fd_install(vtpm_new_dev.fd, file);
560 return 0;
562 default:
563 return -ENOIOCTLCMD;
567 #ifdef CONFIG_COMPAT
568 static long vtpmx_fops_compat_ioctl(struct file *f, unsigned int ioctl,
569 unsigned long arg)
571 return vtpmx_fops_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
573 #endif
575 static const struct file_operations vtpmx_fops = {
576 .owner = THIS_MODULE,
577 .unlocked_ioctl = vtpmx_fops_ioctl,
578 #ifdef CONFIG_COMPAT
579 .compat_ioctl = vtpmx_fops_compat_ioctl,
580 #endif
581 .llseek = noop_llseek,
584 static struct miscdevice vtpmx_miscdev = {
585 .minor = MISC_DYNAMIC_MINOR,
586 .name = "vtpmx",
587 .fops = &vtpmx_fops,
590 static int vtpmx_init(void)
592 return misc_register(&vtpmx_miscdev);
595 static void vtpmx_cleanup(void)
597 misc_deregister(&vtpmx_miscdev);
600 static int __init vtpm_module_init(void)
602 int rc;
604 rc = vtpmx_init();
605 if (rc) {
606 pr_err("couldn't create vtpmx device\n");
607 return rc;
610 workqueue = create_workqueue("tpm-vtpm");
611 if (!workqueue) {
612 pr_err("couldn't create workqueue\n");
613 rc = -ENOMEM;
614 goto err_vtpmx_cleanup;
617 return 0;
619 err_vtpmx_cleanup:
620 vtpmx_cleanup();
622 return rc;
625 static void __exit vtpm_module_exit(void)
627 destroy_workqueue(workqueue);
628 vtpmx_cleanup();
631 module_init(vtpm_module_init);
632 module_exit(vtpm_module_exit);
634 MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
635 MODULE_DESCRIPTION("vTPM Driver");
636 MODULE_VERSION("0.1");
637 MODULE_LICENSE("GPL");