2 * Copyright (C) 2015, 2016 IBM Corporation
3 * Copyright (C) 2016 Intel Corporation
5 * Author: Stefan Berger <stefanb@us.ibm.com>
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 * Device driver for vTPM (vTPM proxy driver)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, version 2 of the
18 #include <linux/types.h>
19 #include <linux/spinlock.h>
20 #include <linux/uaccess.h>
21 #include <linux/wait.h>
22 #include <linux/miscdevice.h>
23 #include <linux/vtpm_proxy.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <linux/poll.h>
27 #include <linux/compat.h>
31 #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
34 struct tpm_chip
*chip
;
36 u32 flags
; /* public API flags */
40 struct mutex buf_lock
; /* protect buffer and flags */
42 long state
; /* internal state */
43 #define STATE_OPENED_FLAG BIT(0)
44 #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
45 #define STATE_REGISTERED_FLAG BIT(2)
47 size_t req_len
; /* length of queued TPM request */
48 size_t resp_len
; /* length of queued TPM response */
49 u8 buffer
[TPM_BUFSIZE
]; /* request/response buffer */
51 struct work_struct work
; /* task that retrieves TPM timeouts */
54 /* all supported flags */
55 #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
57 static struct workqueue_struct
*workqueue
;
59 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
);
62 * Functions related to 'server side'
66 * vtpm_proxy_fops_read - Read TPM commands on 'server side'
70 * @count: number of bytes to read
74 * Number of bytes read or negative error code
76 static ssize_t
vtpm_proxy_fops_read(struct file
*filp
, char __user
*buf
,
77 size_t count
, loff_t
*off
)
79 struct proxy_dev
*proxy_dev
= filp
->private_data
;
83 sig
= wait_event_interruptible(proxy_dev
->wq
,
84 proxy_dev
->req_len
!= 0 ||
85 !(proxy_dev
->state
& STATE_OPENED_FLAG
));
89 mutex_lock(&proxy_dev
->buf_lock
);
91 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
92 mutex_unlock(&proxy_dev
->buf_lock
);
96 len
= proxy_dev
->req_len
;
99 mutex_unlock(&proxy_dev
->buf_lock
);
100 pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
105 rc
= copy_to_user(buf
, proxy_dev
->buffer
, len
);
106 memset(proxy_dev
->buffer
, 0, len
);
107 proxy_dev
->req_len
= 0;
110 proxy_dev
->state
|= STATE_WAIT_RESPONSE_FLAG
;
112 mutex_unlock(&proxy_dev
->buf_lock
);
121 * vtpm_proxy_fops_write - Write TPM responses on 'server side'
123 * @filp: file pointer
125 * @count: number of bytes to write
129 * Number of bytes read or negative error value
131 static ssize_t
vtpm_proxy_fops_write(struct file
*filp
, const char __user
*buf
,
132 size_t count
, loff_t
*off
)
134 struct proxy_dev
*proxy_dev
= filp
->private_data
;
136 mutex_lock(&proxy_dev
->buf_lock
);
138 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
139 mutex_unlock(&proxy_dev
->buf_lock
);
143 if (count
> sizeof(proxy_dev
->buffer
) ||
144 !(proxy_dev
->state
& STATE_WAIT_RESPONSE_FLAG
)) {
145 mutex_unlock(&proxy_dev
->buf_lock
);
149 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
151 proxy_dev
->req_len
= 0;
153 if (copy_from_user(proxy_dev
->buffer
, buf
, count
)) {
154 mutex_unlock(&proxy_dev
->buf_lock
);
158 proxy_dev
->resp_len
= count
;
160 mutex_unlock(&proxy_dev
->buf_lock
);
162 wake_up_interruptible(&proxy_dev
->wq
);
168 * vtpm_proxy_fops_poll - Poll status on 'server side'
170 * @filp: file pointer
175 static unsigned int vtpm_proxy_fops_poll(struct file
*filp
, poll_table
*wait
)
177 struct proxy_dev
*proxy_dev
= filp
->private_data
;
180 poll_wait(filp
, &proxy_dev
->wq
, wait
);
184 mutex_lock(&proxy_dev
->buf_lock
);
186 if (proxy_dev
->req_len
)
187 ret
|= POLLIN
| POLLRDNORM
;
189 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
))
192 mutex_unlock(&proxy_dev
->buf_lock
);
198 * vtpm_proxy_fops_open - Open vTPM device on 'server side'
200 * @filp: file pointer
202 * Called when setting up the anonymous file descriptor
204 static void vtpm_proxy_fops_open(struct file
*filp
)
206 struct proxy_dev
*proxy_dev
= filp
->private_data
;
208 proxy_dev
->state
|= STATE_OPENED_FLAG
;
212 * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
213 * Call to undo vtpm_proxy_fops_open
215 *@proxy_dev: tpm proxy device
217 static void vtpm_proxy_fops_undo_open(struct proxy_dev
*proxy_dev
)
219 mutex_lock(&proxy_dev
->buf_lock
);
221 proxy_dev
->state
&= ~STATE_OPENED_FLAG
;
223 mutex_unlock(&proxy_dev
->buf_lock
);
225 /* no more TPM responses -- wake up anyone waiting for them */
226 wake_up_interruptible(&proxy_dev
->wq
);
230 * vtpm_proxy_fops_release - Close 'server side'
233 * @filp: file pointer
237 static int vtpm_proxy_fops_release(struct inode
*inode
, struct file
*filp
)
239 struct proxy_dev
*proxy_dev
= filp
->private_data
;
241 filp
->private_data
= NULL
;
243 vtpm_proxy_delete_device(proxy_dev
);
248 static const struct file_operations vtpm_proxy_fops
= {
249 .owner
= THIS_MODULE
,
251 .read
= vtpm_proxy_fops_read
,
252 .write
= vtpm_proxy_fops_write
,
253 .poll
= vtpm_proxy_fops_poll
,
254 .release
= vtpm_proxy_fops_release
,
258 * Functions invoked by the core TPM driver to send TPM commands to
259 * 'server side' and receive responses from there.
263 * Called when core TPM driver reads TPM responses from 'server side'
265 * @chip: tpm chip to use
266 * @buf: receive buffer
267 * @count: bytes to read
269 * Number of TPM response bytes read, negative error value otherwise
271 static int vtpm_proxy_tpm_op_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
273 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
277 mutex_lock(&proxy_dev
->buf_lock
);
279 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
280 mutex_unlock(&proxy_dev
->buf_lock
);
284 len
= proxy_dev
->resp_len
;
287 "Invalid size in recv: count=%zd, resp_len=%zd\n",
293 memcpy(buf
, proxy_dev
->buffer
, len
);
294 proxy_dev
->resp_len
= 0;
297 mutex_unlock(&proxy_dev
->buf_lock
);
303 * Called when core TPM driver forwards TPM requests to 'server side'.
305 * @chip: tpm chip to use
307 * @count: bytes to send
310 * 0 in case of success, negative error value otherwise.
312 static int vtpm_proxy_tpm_op_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
314 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
317 if (count
> sizeof(proxy_dev
->buffer
)) {
319 "Invalid size in send: count=%zd, buffer size=%zd\n",
320 count
, sizeof(proxy_dev
->buffer
));
324 mutex_lock(&proxy_dev
->buf_lock
);
326 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
327 mutex_unlock(&proxy_dev
->buf_lock
);
331 proxy_dev
->resp_len
= 0;
333 proxy_dev
->req_len
= count
;
334 memcpy(proxy_dev
->buffer
, buf
, count
);
336 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
338 mutex_unlock(&proxy_dev
->buf_lock
);
340 wake_up_interruptible(&proxy_dev
->wq
);
345 static void vtpm_proxy_tpm_op_cancel(struct tpm_chip
*chip
)
350 static u8
vtpm_proxy_tpm_op_status(struct tpm_chip
*chip
)
352 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
354 if (proxy_dev
->resp_len
)
355 return VTPM_PROXY_REQ_COMPLETE_FLAG
;
360 static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
362 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
365 mutex_lock(&proxy_dev
->buf_lock
);
367 ret
= !(proxy_dev
->state
& STATE_OPENED_FLAG
);
369 mutex_unlock(&proxy_dev
->buf_lock
);
374 static const struct tpm_class_ops vtpm_proxy_tpm_ops
= {
375 .flags
= TPM_OPS_AUTO_STARTUP
,
376 .recv
= vtpm_proxy_tpm_op_recv
,
377 .send
= vtpm_proxy_tpm_op_send
,
378 .cancel
= vtpm_proxy_tpm_op_cancel
,
379 .status
= vtpm_proxy_tpm_op_status
,
380 .req_complete_mask
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
381 .req_complete_val
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
382 .req_canceled
= vtpm_proxy_tpm_req_canceled
,
386 * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
387 * retrieval of timeouts and durations.
390 static void vtpm_proxy_work(struct work_struct
*work
)
392 struct proxy_dev
*proxy_dev
= container_of(work
, struct proxy_dev
,
396 rc
= tpm_chip_register(proxy_dev
->chip
);
398 vtpm_proxy_fops_undo_open(proxy_dev
);
400 proxy_dev
->state
|= STATE_REGISTERED_FLAG
;
404 * vtpm_proxy_work_stop: make sure the work has finished
406 * This function is useful when user space closed the fd
407 * while the driver still determines timeouts.
409 static void vtpm_proxy_work_stop(struct proxy_dev
*proxy_dev
)
411 vtpm_proxy_fops_undo_open(proxy_dev
);
412 flush_work(&proxy_dev
->work
);
416 * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
418 static inline void vtpm_proxy_work_start(struct proxy_dev
*proxy_dev
)
420 queue_work(workqueue
, &proxy_dev
->work
);
424 * Code related to creation and deletion of device pairs
426 static struct proxy_dev
*vtpm_proxy_create_proxy_dev(void)
428 struct proxy_dev
*proxy_dev
;
429 struct tpm_chip
*chip
;
432 proxy_dev
= kzalloc(sizeof(*proxy_dev
), GFP_KERNEL
);
433 if (proxy_dev
== NULL
)
434 return ERR_PTR(-ENOMEM
);
436 init_waitqueue_head(&proxy_dev
->wq
);
437 mutex_init(&proxy_dev
->buf_lock
);
438 INIT_WORK(&proxy_dev
->work
, vtpm_proxy_work
);
440 chip
= tpm_chip_alloc(NULL
, &vtpm_proxy_tpm_ops
);
443 goto err_proxy_dev_free
;
445 dev_set_drvdata(&chip
->dev
, proxy_dev
);
447 proxy_dev
->chip
= chip
;
458 * Undo what has been done in vtpm_create_proxy_dev
460 static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev
*proxy_dev
)
462 put_device(&proxy_dev
->chip
->dev
); /* frees chip */
467 * Create a /dev/tpm%d and 'server side' file descriptor pair
470 * Returns file pointer on success, an error value otherwise
472 static struct file
*vtpm_proxy_create_device(
473 struct vtpm_proxy_new_dev
*vtpm_new_dev
)
475 struct proxy_dev
*proxy_dev
;
479 if (vtpm_new_dev
->flags
& ~VTPM_PROXY_FLAGS_ALL
)
480 return ERR_PTR(-EOPNOTSUPP
);
482 proxy_dev
= vtpm_proxy_create_proxy_dev();
483 if (IS_ERR(proxy_dev
))
484 return ERR_CAST(proxy_dev
);
486 proxy_dev
->flags
= vtpm_new_dev
->flags
;
488 /* setup an anonymous file for the server-side */
489 fd
= get_unused_fd_flags(O_RDWR
);
492 goto err_delete_proxy_dev
;
495 file
= anon_inode_getfile("[vtpms]", &vtpm_proxy_fops
, proxy_dev
,
499 goto err_put_unused_fd
;
502 /* from now on we can unwind with put_unused_fd() + fput() */
503 /* simulate an open() on the server side */
504 vtpm_proxy_fops_open(file
);
506 if (proxy_dev
->flags
& VTPM_PROXY_FLAG_TPM2
)
507 proxy_dev
->chip
->flags
|= TPM_CHIP_FLAG_TPM2
;
509 vtpm_proxy_work_start(proxy_dev
);
511 vtpm_new_dev
->fd
= fd
;
512 vtpm_new_dev
->major
= MAJOR(proxy_dev
->chip
->dev
.devt
);
513 vtpm_new_dev
->minor
= MINOR(proxy_dev
->chip
->dev
.devt
);
514 vtpm_new_dev
->tpm_num
= proxy_dev
->chip
->dev_num
;
521 err_delete_proxy_dev
:
522 vtpm_proxy_delete_proxy_dev(proxy_dev
);
528 * Counter part to vtpm_create_device.
530 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
)
532 vtpm_proxy_work_stop(proxy_dev
);
535 * A client may hold the 'ops' lock, so let it know that the server
536 * side shuts down before we try to grab the 'ops' lock when
537 * unregistering the chip.
539 vtpm_proxy_fops_undo_open(proxy_dev
);
541 if (proxy_dev
->state
& STATE_REGISTERED_FLAG
)
542 tpm_chip_unregister(proxy_dev
->chip
);
544 vtpm_proxy_delete_proxy_dev(proxy_dev
);
548 * Code related to the control device /dev/vtpmx
552 * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl
554 * @ioctl: the ioctl number
555 * @arg: pointer to the struct vtpmx_proxy_new_dev
557 * Creates an anonymous file that is used by the process acting as a TPM to
558 * communicate with the client processes. The function will also add a new TPM
559 * device through which data is proxied to this TPM acting process. The caller
560 * will be provided with a file descriptor to communicate with the clients and
561 * major and minor numbers for the TPM device.
563 static long vtpmx_ioc_new_dev(struct file
*file
, unsigned int ioctl
,
566 void __user
*argp
= (void __user
*)arg
;
567 struct vtpm_proxy_new_dev __user
*vtpm_new_dev_p
;
568 struct vtpm_proxy_new_dev vtpm_new_dev
;
569 struct file
*vtpm_file
;
571 if (!capable(CAP_SYS_ADMIN
))
574 vtpm_new_dev_p
= argp
;
576 if (copy_from_user(&vtpm_new_dev
, vtpm_new_dev_p
,
577 sizeof(vtpm_new_dev
)))
580 vtpm_file
= vtpm_proxy_create_device(&vtpm_new_dev
);
581 if (IS_ERR(vtpm_file
))
582 return PTR_ERR(vtpm_file
);
584 if (copy_to_user(vtpm_new_dev_p
, &vtpm_new_dev
,
585 sizeof(vtpm_new_dev
))) {
586 put_unused_fd(vtpm_new_dev
.fd
);
591 fd_install(vtpm_new_dev
.fd
, vtpm_file
);
596 * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
599 * Returns 0 on success, a negative error code otherwise.
601 static long vtpmx_fops_ioctl(struct file
*f
, unsigned int ioctl
,
605 case VTPM_PROXY_IOC_NEW_DEV
:
606 return vtpmx_ioc_new_dev(f
, ioctl
, arg
);
613 static long vtpmx_fops_compat_ioctl(struct file
*f
, unsigned int ioctl
,
616 return vtpmx_fops_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
620 static const struct file_operations vtpmx_fops
= {
621 .owner
= THIS_MODULE
,
622 .unlocked_ioctl
= vtpmx_fops_ioctl
,
624 .compat_ioctl
= vtpmx_fops_compat_ioctl
,
626 .llseek
= noop_llseek
,
629 static struct miscdevice vtpmx_miscdev
= {
630 .minor
= MISC_DYNAMIC_MINOR
,
635 static int vtpmx_init(void)
637 return misc_register(&vtpmx_miscdev
);
640 static void vtpmx_cleanup(void)
642 misc_deregister(&vtpmx_miscdev
);
645 static int __init
vtpm_module_init(void)
651 pr_err("couldn't create vtpmx device\n");
655 workqueue
= create_workqueue("tpm-vtpm");
657 pr_err("couldn't create workqueue\n");
659 goto err_vtpmx_cleanup
;
670 static void __exit
vtpm_module_exit(void)
672 destroy_workqueue(workqueue
);
676 module_init(vtpm_module_init
);
677 module_exit(vtpm_module_exit
);
679 MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
680 MODULE_DESCRIPTION("vTPM Driver");
681 MODULE_VERSION("0.1");
682 MODULE_LICENSE("GPL");