1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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>
28
29#include "tpm.h"
30
31#define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
32
33struct proxy_dev {
34 struct tpm_chip *chip;
35
36 u32 flags;
37
38 wait_queue_head_t wq;
39
40 struct mutex buf_lock;
41
42 long state;
43#define STATE_OPENED_FLAG BIT(0)
44#define STATE_WAIT_RESPONSE_FLAG BIT(1)
45#define STATE_REGISTERED_FLAG BIT(2)
46#define STATE_DRIVER_COMMAND BIT(3)
47
48 size_t req_len;
49 size_t resp_len;
50 u8 buffer[TPM_BUFSIZE];
51
52 struct work_struct work;
53};
54
55
56#define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
57
58static struct workqueue_struct *workqueue;
59
60static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf,
78 size_t count, loff_t *off)
79{
80 struct proxy_dev *proxy_dev = filp->private_data;
81 size_t len;
82 int sig, rc;
83
84 sig = wait_event_interruptible(proxy_dev->wq,
85 proxy_dev->req_len != 0 ||
86 !(proxy_dev->state & STATE_OPENED_FLAG));
87 if (sig)
88 return -EINTR;
89
90 mutex_lock(&proxy_dev->buf_lock);
91
92 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
93 mutex_unlock(&proxy_dev->buf_lock);
94 return -EPIPE;
95 }
96
97 len = proxy_dev->req_len;
98
99 if (count < len) {
100 mutex_unlock(&proxy_dev->buf_lock);
101 pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
102 count, len);
103 return -EIO;
104 }
105
106 rc = copy_to_user(buf, proxy_dev->buffer, len);
107 memset(proxy_dev->buffer, 0, len);
108 proxy_dev->req_len = 0;
109
110 if (!rc)
111 proxy_dev->state |= STATE_WAIT_RESPONSE_FLAG;
112
113 mutex_unlock(&proxy_dev->buf_lock);
114
115 if (rc)
116 return -EFAULT;
117
118 return len;
119}
120
121
122
123
124
125
126
127
128
129
130
131
132static ssize_t vtpm_proxy_fops_write(struct file *filp, const char __user *buf,
133 size_t count, loff_t *off)
134{
135 struct proxy_dev *proxy_dev = filp->private_data;
136
137 mutex_lock(&proxy_dev->buf_lock);
138
139 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
140 mutex_unlock(&proxy_dev->buf_lock);
141 return -EPIPE;
142 }
143
144 if (count > sizeof(proxy_dev->buffer) ||
145 !(proxy_dev->state & STATE_WAIT_RESPONSE_FLAG)) {
146 mutex_unlock(&proxy_dev->buf_lock);
147 return -EIO;
148 }
149
150 proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
151
152 proxy_dev->req_len = 0;
153
154 if (copy_from_user(proxy_dev->buffer, buf, count)) {
155 mutex_unlock(&proxy_dev->buf_lock);
156 return -EFAULT;
157 }
158
159 proxy_dev->resp_len = count;
160
161 mutex_unlock(&proxy_dev->buf_lock);
162
163 wake_up_interruptible(&proxy_dev->wq);
164
165 return count;
166}
167
168
169
170
171
172
173
174
175
176static __poll_t vtpm_proxy_fops_poll(struct file *filp, poll_table *wait)
177{
178 struct proxy_dev *proxy_dev = filp->private_data;
179 __poll_t ret;
180
181 poll_wait(filp, &proxy_dev->wq, wait);
182
183 ret = EPOLLOUT;
184
185 mutex_lock(&proxy_dev->buf_lock);
186
187 if (proxy_dev->req_len)
188 ret |= EPOLLIN | EPOLLRDNORM;
189
190 if (!(proxy_dev->state & STATE_OPENED_FLAG))
191 ret |= EPOLLHUP;
192
193 mutex_unlock(&proxy_dev->buf_lock);
194
195 return ret;
196}
197
198
199
200
201
202
203
204
205static void vtpm_proxy_fops_open(struct file *filp)
206{
207 struct proxy_dev *proxy_dev = filp->private_data;
208
209 proxy_dev->state |= STATE_OPENED_FLAG;
210}
211
212
213
214
215
216
217
218static void vtpm_proxy_fops_undo_open(struct proxy_dev *proxy_dev)
219{
220 mutex_lock(&proxy_dev->buf_lock);
221
222 proxy_dev->state &= ~STATE_OPENED_FLAG;
223
224 mutex_unlock(&proxy_dev->buf_lock);
225
226
227 wake_up_interruptible(&proxy_dev->wq);
228}
229
230
231
232
233
234
235
236
237
238static int vtpm_proxy_fops_release(struct inode *inode, struct file *filp)
239{
240 struct proxy_dev *proxy_dev = filp->private_data;
241
242 filp->private_data = NULL;
243
244 vtpm_proxy_delete_device(proxy_dev);
245
246 return 0;
247}
248
249static const struct file_operations vtpm_proxy_fops = {
250 .owner = THIS_MODULE,
251 .llseek = no_llseek,
252 .read = vtpm_proxy_fops_read,
253 .write = vtpm_proxy_fops_write,
254 .poll = vtpm_proxy_fops_poll,
255 .release = vtpm_proxy_fops_release,
256};
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272static int vtpm_proxy_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
273{
274 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
275 size_t len;
276
277
278 mutex_lock(&proxy_dev->buf_lock);
279
280 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
281 mutex_unlock(&proxy_dev->buf_lock);
282 return -EPIPE;
283 }
284
285 len = proxy_dev->resp_len;
286 if (count < len) {
287 dev_err(&chip->dev,
288 "Invalid size in recv: count=%zd, resp_len=%zd\n",
289 count, len);
290 len = -EIO;
291 goto out;
292 }
293
294 memcpy(buf, proxy_dev->buffer, len);
295 proxy_dev->resp_len = 0;
296
297out:
298 mutex_unlock(&proxy_dev->buf_lock);
299
300 return len;
301}
302
303static int vtpm_proxy_is_driver_command(struct tpm_chip *chip,
304 u8 *buf, size_t count)
305{
306 struct tpm_input_header *hdr = (struct tpm_input_header *)buf;
307
308 if (count < sizeof(struct tpm_input_header))
309 return 0;
310
311 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
312 switch (be32_to_cpu(hdr->ordinal)) {
313 case TPM2_CC_SET_LOCALITY:
314 return 1;
315 }
316 } else {
317 switch (be32_to_cpu(hdr->ordinal)) {
318 case TPM_ORD_SET_LOCALITY:
319 return 1;
320 }
321 }
322 return 0;
323}
324
325
326
327
328
329
330
331
332
333
334
335static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t count)
336{
337 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
338 int rc = 0;
339
340 if (count > sizeof(proxy_dev->buffer)) {
341 dev_err(&chip->dev,
342 "Invalid size in send: count=%zd, buffer size=%zd\n",
343 count, sizeof(proxy_dev->buffer));
344 return -EIO;
345 }
346
347 if (!(proxy_dev->state & STATE_DRIVER_COMMAND) &&
348 vtpm_proxy_is_driver_command(chip, buf, count))
349 return -EFAULT;
350
351 mutex_lock(&proxy_dev->buf_lock);
352
353 if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
354 mutex_unlock(&proxy_dev->buf_lock);
355 return -EPIPE;
356 }
357
358 proxy_dev->resp_len = 0;
359
360 proxy_dev->req_len = count;
361 memcpy(proxy_dev->buffer, buf, count);
362
363 proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
364
365 mutex_unlock(&proxy_dev->buf_lock);
366
367 wake_up_interruptible(&proxy_dev->wq);
368
369 return rc;
370}
371
372static void vtpm_proxy_tpm_op_cancel(struct tpm_chip *chip)
373{
374
375}
376
377static u8 vtpm_proxy_tpm_op_status(struct tpm_chip *chip)
378{
379 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
380
381 if (proxy_dev->resp_len)
382 return VTPM_PROXY_REQ_COMPLETE_FLAG;
383
384 return 0;
385}
386
387static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)
388{
389 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
390 bool ret;
391
392 mutex_lock(&proxy_dev->buf_lock);
393
394 ret = !(proxy_dev->state & STATE_OPENED_FLAG);
395
396 mutex_unlock(&proxy_dev->buf_lock);
397
398 return ret;
399}
400
401static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
402{
403 struct tpm_buf buf;
404 int rc;
405 const struct tpm_output_header *header;
406 struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
407
408 if (chip->flags & TPM_CHIP_FLAG_TPM2)
409 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS,
410 TPM2_CC_SET_LOCALITY);
411 else
412 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND,
413 TPM_ORD_SET_LOCALITY);
414 if (rc)
415 return rc;
416 tpm_buf_append_u8(&buf, locality);
417
418 proxy_dev->state |= STATE_DRIVER_COMMAND;
419
420 rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0,
421 TPM_TRANSMIT_NESTED,
422 "attempting to set locality");
423
424 proxy_dev->state &= ~STATE_DRIVER_COMMAND;
425
426 if (rc < 0) {
427 locality = rc;
428 goto out;
429 }
430
431 header = (const struct tpm_output_header *)buf.data;
432 rc = be32_to_cpu(header->return_code);
433 if (rc)
434 locality = -1;
435
436out:
437 tpm_buf_destroy(&buf);
438
439 return locality;
440}
441
442static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
443 .flags = TPM_OPS_AUTO_STARTUP,
444 .recv = vtpm_proxy_tpm_op_recv,
445 .send = vtpm_proxy_tpm_op_send,
446 .cancel = vtpm_proxy_tpm_op_cancel,
447 .status = vtpm_proxy_tpm_op_status,
448 .req_complete_mask = VTPM_PROXY_REQ_COMPLETE_FLAG,
449 .req_complete_val = VTPM_PROXY_REQ_COMPLETE_FLAG,
450 .req_canceled = vtpm_proxy_tpm_req_canceled,
451 .request_locality = vtpm_proxy_request_locality,
452};
453
454
455
456
457
458
459static void vtpm_proxy_work(struct work_struct *work)
460{
461 struct proxy_dev *proxy_dev = container_of(work, struct proxy_dev,
462 work);
463 int rc;
464
465 rc = tpm_chip_register(proxy_dev->chip);
466 if (rc)
467 vtpm_proxy_fops_undo_open(proxy_dev);
468 else
469 proxy_dev->state |= STATE_REGISTERED_FLAG;
470}
471
472
473
474
475
476
477
478static void vtpm_proxy_work_stop(struct proxy_dev *proxy_dev)
479{
480 vtpm_proxy_fops_undo_open(proxy_dev);
481 flush_work(&proxy_dev->work);
482}
483
484
485
486
487static inline void vtpm_proxy_work_start(struct proxy_dev *proxy_dev)
488{
489 queue_work(workqueue, &proxy_dev->work);
490}
491
492
493
494
495static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
496{
497 struct proxy_dev *proxy_dev;
498 struct tpm_chip *chip;
499 int err;
500
501 proxy_dev = kzalloc(sizeof(*proxy_dev), GFP_KERNEL);
502 if (proxy_dev == NULL)
503 return ERR_PTR(-ENOMEM);
504
505 init_waitqueue_head(&proxy_dev->wq);
506 mutex_init(&proxy_dev->buf_lock);
507 INIT_WORK(&proxy_dev->work, vtpm_proxy_work);
508
509 chip = tpm_chip_alloc(NULL, &vtpm_proxy_tpm_ops);
510 if (IS_ERR(chip)) {
511 err = PTR_ERR(chip);
512 goto err_proxy_dev_free;
513 }
514 dev_set_drvdata(&chip->dev, proxy_dev);
515
516 proxy_dev->chip = chip;
517
518 return proxy_dev;
519
520err_proxy_dev_free:
521 kfree(proxy_dev);
522
523 return ERR_PTR(err);
524}
525
526
527
528
529static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
530{
531 put_device(&proxy_dev->chip->dev);
532 kfree(proxy_dev);
533}
534
535
536
537
538
539
540
541static struct file *vtpm_proxy_create_device(
542 struct vtpm_proxy_new_dev *vtpm_new_dev)
543{
544 struct proxy_dev *proxy_dev;
545 int rc, fd;
546 struct file *file;
547
548 if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
549 return ERR_PTR(-EOPNOTSUPP);
550
551 proxy_dev = vtpm_proxy_create_proxy_dev();
552 if (IS_ERR(proxy_dev))
553 return ERR_CAST(proxy_dev);
554
555 proxy_dev->flags = vtpm_new_dev->flags;
556
557
558 fd = get_unused_fd_flags(O_RDWR);
559 if (fd < 0) {
560 rc = fd;
561 goto err_delete_proxy_dev;
562 }
563
564 file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
565 O_RDWR);
566 if (IS_ERR(file)) {
567 rc = PTR_ERR(file);
568 goto err_put_unused_fd;
569 }
570
571
572
573 vtpm_proxy_fops_open(file);
574
575 if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
576 proxy_dev->chip->flags |= TPM_CHIP_FLAG_TPM2;
577
578 vtpm_proxy_work_start(proxy_dev);
579
580 vtpm_new_dev->fd = fd;
581 vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
582 vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
583 vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
584
585 return file;
586
587err_put_unused_fd:
588 put_unused_fd(fd);
589
590err_delete_proxy_dev:
591 vtpm_proxy_delete_proxy_dev(proxy_dev);
592
593 return ERR_PTR(rc);
594}
595
596
597
598
599static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)
600{
601 vtpm_proxy_work_stop(proxy_dev);
602
603
604
605
606
607
608 vtpm_proxy_fops_undo_open(proxy_dev);
609
610 if (proxy_dev->state & STATE_REGISTERED_FLAG)
611 tpm_chip_unregister(proxy_dev->chip);
612
613 vtpm_proxy_delete_proxy_dev(proxy_dev);
614}
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
633 unsigned long arg)
634{
635 void __user *argp = (void __user *)arg;
636 struct vtpm_proxy_new_dev __user *vtpm_new_dev_p;
637 struct vtpm_proxy_new_dev vtpm_new_dev;
638 struct file *vtpm_file;
639
640 if (!capable(CAP_SYS_ADMIN))
641 return -EPERM;
642
643 vtpm_new_dev_p = argp;
644
645 if (copy_from_user(&vtpm_new_dev, vtpm_new_dev_p,
646 sizeof(vtpm_new_dev)))
647 return -EFAULT;
648
649 vtpm_file = vtpm_proxy_create_device(&vtpm_new_dev);
650 if (IS_ERR(vtpm_file))
651 return PTR_ERR(vtpm_file);
652
653 if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
654 sizeof(vtpm_new_dev))) {
655 put_unused_fd(vtpm_new_dev.fd);
656 fput(vtpm_file);
657 return -EFAULT;
658 }
659
660 fd_install(vtpm_new_dev.fd, vtpm_file);
661 return 0;
662}
663
664
665
666
667
668
669
670static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,
671 unsigned long arg)
672{
673 switch (ioctl) {
674 case VTPM_PROXY_IOC_NEW_DEV:
675 return vtpmx_ioc_new_dev(f, ioctl, arg);
676 default:
677 return -ENOIOCTLCMD;
678 }
679}
680
681#ifdef CONFIG_COMPAT
682static long vtpmx_fops_compat_ioctl(struct file *f, unsigned int ioctl,
683 unsigned long arg)
684{
685 return vtpmx_fops_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
686}
687#endif
688
689static const struct file_operations vtpmx_fops = {
690 .owner = THIS_MODULE,
691 .unlocked_ioctl = vtpmx_fops_ioctl,
692#ifdef CONFIG_COMPAT
693 .compat_ioctl = vtpmx_fops_compat_ioctl,
694#endif
695 .llseek = noop_llseek,
696};
697
698static struct miscdevice vtpmx_miscdev = {
699 .minor = MISC_DYNAMIC_MINOR,
700 .name = "vtpmx",
701 .fops = &vtpmx_fops,
702};
703
704static int vtpmx_init(void)
705{
706 return misc_register(&vtpmx_miscdev);
707}
708
709static void vtpmx_cleanup(void)
710{
711 misc_deregister(&vtpmx_miscdev);
712}
713
714static int __init vtpm_module_init(void)
715{
716 int rc;
717
718 rc = vtpmx_init();
719 if (rc) {
720 pr_err("couldn't create vtpmx device\n");
721 return rc;
722 }
723
724 workqueue = create_workqueue("tpm-vtpm");
725 if (!workqueue) {
726 pr_err("couldn't create workqueue\n");
727 rc = -ENOMEM;
728 goto err_vtpmx_cleanup;
729 }
730
731 return 0;
732
733err_vtpmx_cleanup:
734 vtpmx_cleanup();
735
736 return rc;
737}
738
739static void __exit vtpm_module_exit(void)
740{
741 destroy_workqueue(workqueue);
742 vtpmx_cleanup();
743}
744
745module_init(vtpm_module_init);
746module_exit(vtpm_module_exit);
747
748MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
749MODULE_DESCRIPTION("vTPM Driver");
750MODULE_VERSION("0.1");
751MODULE_LICENSE("GPL");
752