1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015, 2016 IBM Corporation
4 * Copyright (C) 2016 Intel Corporation
6 * Author: Stefan Berger <stefanb@us.ibm.com>
8 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 * Device driver for vTPM (vTPM proxy driver)
13 #include <linux/types.h>
14 #include <linux/spinlock.h>
15 #include <linux/uaccess.h>
16 #include <linux/wait.h>
17 #include <linux/miscdevice.h>
18 #include <linux/vtpm_proxy.h>
19 #include <linux/file.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/poll.h>
22 #include <linux/compat.h>
26 #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
29 struct tpm_chip
*chip
;
31 u32 flags
; /* public API flags */
35 struct mutex buf_lock
; /* protect buffer and flags */
37 long state
; /* internal state */
38 #define STATE_OPENED_FLAG BIT(0)
39 #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
40 #define STATE_REGISTERED_FLAG BIT(2)
41 #define STATE_DRIVER_COMMAND BIT(3) /* sending a driver specific command */
43 size_t req_len
; /* length of queued TPM request */
44 size_t resp_len
; /* length of queued TPM response */
45 u8 buffer
[TPM_BUFSIZE
]; /* request/response buffer */
47 struct work_struct work
; /* task that retrieves TPM timeouts */
50 /* all supported flags */
51 #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
53 static struct workqueue_struct
*workqueue
;
55 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
);
58 * Functions related to 'server side'
62 * vtpm_proxy_fops_read - Read TPM commands on 'server side'
66 * @count: number of bytes to read
70 * Number of bytes read or negative error code
72 static ssize_t
vtpm_proxy_fops_read(struct file
*filp
, char __user
*buf
,
73 size_t count
, loff_t
*off
)
75 struct proxy_dev
*proxy_dev
= filp
->private_data
;
79 sig
= wait_event_interruptible(proxy_dev
->wq
,
80 proxy_dev
->req_len
!= 0 ||
81 !(proxy_dev
->state
& STATE_OPENED_FLAG
));
85 mutex_lock(&proxy_dev
->buf_lock
);
87 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
88 mutex_unlock(&proxy_dev
->buf_lock
);
92 len
= proxy_dev
->req_len
;
95 mutex_unlock(&proxy_dev
->buf_lock
);
96 pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
101 rc
= copy_to_user(buf
, proxy_dev
->buffer
, len
);
102 memset(proxy_dev
->buffer
, 0, len
);
103 proxy_dev
->req_len
= 0;
106 proxy_dev
->state
|= STATE_WAIT_RESPONSE_FLAG
;
108 mutex_unlock(&proxy_dev
->buf_lock
);
117 * vtpm_proxy_fops_write - Write TPM responses on 'server side'
119 * @filp: file pointer
121 * @count: number of bytes to write
125 * Number of bytes read or negative error value
127 static ssize_t
vtpm_proxy_fops_write(struct file
*filp
, const char __user
*buf
,
128 size_t count
, loff_t
*off
)
130 struct proxy_dev
*proxy_dev
= filp
->private_data
;
132 mutex_lock(&proxy_dev
->buf_lock
);
134 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
135 mutex_unlock(&proxy_dev
->buf_lock
);
139 if (count
> sizeof(proxy_dev
->buffer
) ||
140 !(proxy_dev
->state
& STATE_WAIT_RESPONSE_FLAG
)) {
141 mutex_unlock(&proxy_dev
->buf_lock
);
145 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
147 proxy_dev
->req_len
= 0;
149 if (copy_from_user(proxy_dev
->buffer
, buf
, count
)) {
150 mutex_unlock(&proxy_dev
->buf_lock
);
154 proxy_dev
->resp_len
= count
;
156 mutex_unlock(&proxy_dev
->buf_lock
);
158 wake_up_interruptible(&proxy_dev
->wq
);
164 * vtpm_proxy_fops_poll - Poll status on 'server side'
166 * @filp: file pointer
171 static __poll_t
vtpm_proxy_fops_poll(struct file
*filp
, poll_table
*wait
)
173 struct proxy_dev
*proxy_dev
= filp
->private_data
;
176 poll_wait(filp
, &proxy_dev
->wq
, wait
);
180 mutex_lock(&proxy_dev
->buf_lock
);
182 if (proxy_dev
->req_len
)
183 ret
|= EPOLLIN
| EPOLLRDNORM
;
185 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
))
188 mutex_unlock(&proxy_dev
->buf_lock
);
194 * vtpm_proxy_fops_open - Open vTPM device on 'server side'
196 * @filp: file pointer
198 * Called when setting up the anonymous file descriptor
200 static void vtpm_proxy_fops_open(struct file
*filp
)
202 struct proxy_dev
*proxy_dev
= filp
->private_data
;
204 proxy_dev
->state
|= STATE_OPENED_FLAG
;
208 * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
209 * Call to undo vtpm_proxy_fops_open
211 *@proxy_dev: tpm proxy device
213 static void vtpm_proxy_fops_undo_open(struct proxy_dev
*proxy_dev
)
215 mutex_lock(&proxy_dev
->buf_lock
);
217 proxy_dev
->state
&= ~STATE_OPENED_FLAG
;
219 mutex_unlock(&proxy_dev
->buf_lock
);
221 /* no more TPM responses -- wake up anyone waiting for them */
222 wake_up_interruptible(&proxy_dev
->wq
);
226 * vtpm_proxy_fops_release - Close 'server side'
229 * @filp: file pointer
233 static int vtpm_proxy_fops_release(struct inode
*inode
, struct file
*filp
)
235 struct proxy_dev
*proxy_dev
= filp
->private_data
;
237 filp
->private_data
= NULL
;
239 vtpm_proxy_delete_device(proxy_dev
);
244 static const struct file_operations vtpm_proxy_fops
= {
245 .owner
= THIS_MODULE
,
247 .read
= vtpm_proxy_fops_read
,
248 .write
= vtpm_proxy_fops_write
,
249 .poll
= vtpm_proxy_fops_poll
,
250 .release
= vtpm_proxy_fops_release
,
254 * Functions invoked by the core TPM driver to send TPM commands to
255 * 'server side' and receive responses from there.
259 * Called when core TPM driver reads TPM responses from 'server side'
261 * @chip: tpm chip to use
262 * @buf: receive buffer
263 * @count: bytes to read
265 * Number of TPM response bytes read, negative error value otherwise
267 static int vtpm_proxy_tpm_op_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
269 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
273 mutex_lock(&proxy_dev
->buf_lock
);
275 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
276 mutex_unlock(&proxy_dev
->buf_lock
);
280 len
= proxy_dev
->resp_len
;
283 "Invalid size in recv: count=%zd, resp_len=%zd\n",
289 memcpy(buf
, proxy_dev
->buffer
, len
);
290 proxy_dev
->resp_len
= 0;
293 mutex_unlock(&proxy_dev
->buf_lock
);
298 static int vtpm_proxy_is_driver_command(struct tpm_chip
*chip
,
299 u8
*buf
, size_t count
)
301 struct tpm_header
*hdr
= (struct tpm_header
*)buf
;
303 if (count
< sizeof(struct tpm_header
))
306 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
) {
307 switch (be32_to_cpu(hdr
->ordinal
)) {
308 case TPM2_CC_SET_LOCALITY
:
312 switch (be32_to_cpu(hdr
->ordinal
)) {
313 case TPM_ORD_SET_LOCALITY
:
321 * Called when core TPM driver forwards TPM requests to 'server side'.
323 * @chip: tpm chip to use
325 * @count: bytes to send
328 * 0 in case of success, negative error value otherwise.
330 static int vtpm_proxy_tpm_op_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
332 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
334 if (count
> sizeof(proxy_dev
->buffer
)) {
336 "Invalid size in send: count=%zd, buffer size=%zd\n",
337 count
, sizeof(proxy_dev
->buffer
));
341 if (!(proxy_dev
->state
& STATE_DRIVER_COMMAND
) &&
342 vtpm_proxy_is_driver_command(chip
, buf
, count
))
345 mutex_lock(&proxy_dev
->buf_lock
);
347 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
348 mutex_unlock(&proxy_dev
->buf_lock
);
352 proxy_dev
->resp_len
= 0;
354 proxy_dev
->req_len
= count
;
355 memcpy(proxy_dev
->buffer
, buf
, count
);
357 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
359 mutex_unlock(&proxy_dev
->buf_lock
);
361 wake_up_interruptible(&proxy_dev
->wq
);
366 static void vtpm_proxy_tpm_op_cancel(struct tpm_chip
*chip
)
371 static u8
vtpm_proxy_tpm_op_status(struct tpm_chip
*chip
)
373 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
375 if (proxy_dev
->resp_len
)
376 return VTPM_PROXY_REQ_COMPLETE_FLAG
;
381 static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
383 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
386 mutex_lock(&proxy_dev
->buf_lock
);
388 ret
= !(proxy_dev
->state
& STATE_OPENED_FLAG
);
390 mutex_unlock(&proxy_dev
->buf_lock
);
395 static int vtpm_proxy_request_locality(struct tpm_chip
*chip
, int locality
)
399 const struct tpm_header
*header
;
400 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
402 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
403 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
,
404 TPM2_CC_SET_LOCALITY
);
406 rc
= tpm_buf_init(&buf
, TPM_TAG_RQU_COMMAND
,
407 TPM_ORD_SET_LOCALITY
);
410 tpm_buf_append_u8(&buf
, locality
);
412 proxy_dev
->state
|= STATE_DRIVER_COMMAND
;
414 rc
= tpm_transmit_cmd(chip
, &buf
, 0, "attempting to set locality");
416 proxy_dev
->state
&= ~STATE_DRIVER_COMMAND
;
423 header
= (const struct tpm_header
*)buf
.data
;
424 rc
= be32_to_cpu(header
->return_code
);
429 tpm_buf_destroy(&buf
);
434 static const struct tpm_class_ops vtpm_proxy_tpm_ops
= {
435 .flags
= TPM_OPS_AUTO_STARTUP
,
436 .recv
= vtpm_proxy_tpm_op_recv
,
437 .send
= vtpm_proxy_tpm_op_send
,
438 .cancel
= vtpm_proxy_tpm_op_cancel
,
439 .status
= vtpm_proxy_tpm_op_status
,
440 .req_complete_mask
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
441 .req_complete_val
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
442 .req_canceled
= vtpm_proxy_tpm_req_canceled
,
443 .request_locality
= vtpm_proxy_request_locality
,
447 * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
448 * retrieval of timeouts and durations.
451 static void vtpm_proxy_work(struct work_struct
*work
)
453 struct proxy_dev
*proxy_dev
= container_of(work
, struct proxy_dev
,
457 rc
= tpm_chip_register(proxy_dev
->chip
);
459 vtpm_proxy_fops_undo_open(proxy_dev
);
461 proxy_dev
->state
|= STATE_REGISTERED_FLAG
;
465 * vtpm_proxy_work_stop: make sure the work has finished
467 * This function is useful when user space closed the fd
468 * while the driver still determines timeouts.
470 static void vtpm_proxy_work_stop(struct proxy_dev
*proxy_dev
)
472 vtpm_proxy_fops_undo_open(proxy_dev
);
473 flush_work(&proxy_dev
->work
);
477 * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
479 static inline void vtpm_proxy_work_start(struct proxy_dev
*proxy_dev
)
481 queue_work(workqueue
, &proxy_dev
->work
);
485 * Code related to creation and deletion of device pairs
487 static struct proxy_dev
*vtpm_proxy_create_proxy_dev(void)
489 struct proxy_dev
*proxy_dev
;
490 struct tpm_chip
*chip
;
493 proxy_dev
= kzalloc(sizeof(*proxy_dev
), GFP_KERNEL
);
494 if (proxy_dev
== NULL
)
495 return ERR_PTR(-ENOMEM
);
497 init_waitqueue_head(&proxy_dev
->wq
);
498 mutex_init(&proxy_dev
->buf_lock
);
499 INIT_WORK(&proxy_dev
->work
, vtpm_proxy_work
);
501 chip
= tpm_chip_alloc(NULL
, &vtpm_proxy_tpm_ops
);
504 goto err_proxy_dev_free
;
506 dev_set_drvdata(&chip
->dev
, proxy_dev
);
508 proxy_dev
->chip
= chip
;
519 * Undo what has been done in vtpm_create_proxy_dev
521 static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev
*proxy_dev
)
523 put_device(&proxy_dev
->chip
->dev
); /* frees chip */
528 * Create a /dev/tpm%d and 'server side' file descriptor pair
531 * Returns file pointer on success, an error value otherwise
533 static struct file
*vtpm_proxy_create_device(
534 struct vtpm_proxy_new_dev
*vtpm_new_dev
)
536 struct proxy_dev
*proxy_dev
;
540 if (vtpm_new_dev
->flags
& ~VTPM_PROXY_FLAGS_ALL
)
541 return ERR_PTR(-EOPNOTSUPP
);
543 proxy_dev
= vtpm_proxy_create_proxy_dev();
544 if (IS_ERR(proxy_dev
))
545 return ERR_CAST(proxy_dev
);
547 proxy_dev
->flags
= vtpm_new_dev
->flags
;
549 /* setup an anonymous file for the server-side */
550 fd
= get_unused_fd_flags(O_RDWR
);
553 goto err_delete_proxy_dev
;
556 file
= anon_inode_getfile("[vtpms]", &vtpm_proxy_fops
, proxy_dev
,
560 goto err_put_unused_fd
;
563 /* from now on we can unwind with put_unused_fd() + fput() */
564 /* simulate an open() on the server side */
565 vtpm_proxy_fops_open(file
);
567 if (proxy_dev
->flags
& VTPM_PROXY_FLAG_TPM2
)
568 proxy_dev
->chip
->flags
|= TPM_CHIP_FLAG_TPM2
;
570 vtpm_proxy_work_start(proxy_dev
);
572 vtpm_new_dev
->fd
= fd
;
573 vtpm_new_dev
->major
= MAJOR(proxy_dev
->chip
->dev
.devt
);
574 vtpm_new_dev
->minor
= MINOR(proxy_dev
->chip
->dev
.devt
);
575 vtpm_new_dev
->tpm_num
= proxy_dev
->chip
->dev_num
;
582 err_delete_proxy_dev
:
583 vtpm_proxy_delete_proxy_dev(proxy_dev
);
589 * Counter part to vtpm_create_device.
591 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
)
593 vtpm_proxy_work_stop(proxy_dev
);
596 * A client may hold the 'ops' lock, so let it know that the server
597 * side shuts down before we try to grab the 'ops' lock when
598 * unregistering the chip.
600 vtpm_proxy_fops_undo_open(proxy_dev
);
602 if (proxy_dev
->state
& STATE_REGISTERED_FLAG
)
603 tpm_chip_unregister(proxy_dev
->chip
);
605 vtpm_proxy_delete_proxy_dev(proxy_dev
);
609 * Code related to the control device /dev/vtpmx
613 * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl
615 * @ioctl: the ioctl number
616 * @arg: pointer to the struct vtpmx_proxy_new_dev
618 * Creates an anonymous file that is used by the process acting as a TPM to
619 * communicate with the client processes. The function will also add a new TPM
620 * device through which data is proxied to this TPM acting process. The caller
621 * will be provided with a file descriptor to communicate with the clients and
622 * major and minor numbers for the TPM device.
624 static long vtpmx_ioc_new_dev(struct file
*file
, unsigned int ioctl
,
627 void __user
*argp
= (void __user
*)arg
;
628 struct vtpm_proxy_new_dev __user
*vtpm_new_dev_p
;
629 struct vtpm_proxy_new_dev vtpm_new_dev
;
630 struct file
*vtpm_file
;
632 if (!capable(CAP_SYS_ADMIN
))
635 vtpm_new_dev_p
= argp
;
637 if (copy_from_user(&vtpm_new_dev
, vtpm_new_dev_p
,
638 sizeof(vtpm_new_dev
)))
641 vtpm_file
= vtpm_proxy_create_device(&vtpm_new_dev
);
642 if (IS_ERR(vtpm_file
))
643 return PTR_ERR(vtpm_file
);
645 if (copy_to_user(vtpm_new_dev_p
, &vtpm_new_dev
,
646 sizeof(vtpm_new_dev
))) {
647 put_unused_fd(vtpm_new_dev
.fd
);
652 fd_install(vtpm_new_dev
.fd
, vtpm_file
);
657 * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
660 * Returns 0 on success, a negative error code otherwise.
662 static long vtpmx_fops_ioctl(struct file
*f
, unsigned int ioctl
,
666 case VTPM_PROXY_IOC_NEW_DEV
:
667 return vtpmx_ioc_new_dev(f
, ioctl
, arg
);
673 static const struct file_operations vtpmx_fops
= {
674 .owner
= THIS_MODULE
,
675 .unlocked_ioctl
= vtpmx_fops_ioctl
,
676 .compat_ioctl
= compat_ptr_ioctl
,
677 .llseek
= noop_llseek
,
680 static struct miscdevice vtpmx_miscdev
= {
681 .minor
= MISC_DYNAMIC_MINOR
,
686 static int vtpmx_init(void)
688 return misc_register(&vtpmx_miscdev
);
691 static void vtpmx_cleanup(void)
693 misc_deregister(&vtpmx_miscdev
);
696 static int __init
vtpm_module_init(void)
702 pr_err("couldn't create vtpmx device\n");
706 workqueue
= create_workqueue("tpm-vtpm");
708 pr_err("couldn't create workqueue\n");
710 goto err_vtpmx_cleanup
;
721 static void __exit
vtpm_module_exit(void)
723 destroy_workqueue(workqueue
);
727 module_init(vtpm_module_init
);
728 module_exit(vtpm_module_exit
);
730 MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
731 MODULE_DESCRIPTION("vTPM Driver");
732 MODULE_VERSION("0.1");
733 MODULE_LICENSE("GPL");