tpm_vtpm_proxy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (C) 2015, 2016 IBM Corporation
  3. * Copyright (C) 2016 Intel Corporation
  4. *
  5. * Author: Stefan Berger <stefanb@us.ibm.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * Device driver for vTPM (vTPM proxy driver)
  10. *
  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
  14. * License.
  15. *
  16. */
  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>
  27. #include "tpm.h"
  28. #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
  29. struct proxy_dev {
  30. struct tpm_chip *chip;
  31. u32 flags; /* public API flags */
  32. wait_queue_head_t wq;
  33. struct mutex buf_lock; /* protect buffer and flags */
  34. long state; /* internal state */
  35. #define STATE_OPENED_FLAG BIT(0)
  36. #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
  37. #define STATE_REGISTERED_FLAG BIT(2)
  38. size_t req_len; /* length of queued TPM request */
  39. size_t resp_len; /* length of queued TPM response */
  40. u8 buffer[TPM_BUFSIZE]; /* request/response buffer */
  41. struct work_struct work; /* task that retrieves TPM timeouts */
  42. };
  43. /* all supported flags */
  44. #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
  45. static struct workqueue_struct *workqueue;
  46. static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev);
  47. /*
  48. * Functions related to 'server side'
  49. */
  50. /**
  51. * vtpm_proxy_fops_read - Read TPM commands on 'server side'
  52. *
  53. * Return value:
  54. * Number of bytes read or negative error code
  55. */
  56. static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf,
  57. size_t count, loff_t *off)
  58. {
  59. struct proxy_dev *proxy_dev = filp->private_data;
  60. size_t len;
  61. int sig, rc;
  62. sig = wait_event_interruptible(proxy_dev->wq,
  63. proxy_dev->req_len != 0 ||
  64. !(proxy_dev->state & STATE_OPENED_FLAG));
  65. if (sig)
  66. return -EINTR;
  67. mutex_lock(&proxy_dev->buf_lock);
  68. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  69. mutex_unlock(&proxy_dev->buf_lock);
  70. return -EPIPE;
  71. }
  72. len = proxy_dev->req_len;
  73. if (count < len) {
  74. mutex_unlock(&proxy_dev->buf_lock);
  75. pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
  76. count, len);
  77. return -EIO;
  78. }
  79. rc = copy_to_user(buf, proxy_dev->buffer, len);
  80. memset(proxy_dev->buffer, 0, len);
  81. proxy_dev->req_len = 0;
  82. if (!rc)
  83. proxy_dev->state |= STATE_WAIT_RESPONSE_FLAG;
  84. mutex_unlock(&proxy_dev->buf_lock);
  85. if (rc)
  86. return -EFAULT;
  87. return len;
  88. }
  89. /**
  90. * vtpm_proxy_fops_write - Write TPM responses on 'server side'
  91. *
  92. * Return value:
  93. * Number of bytes read or negative error value
  94. */
  95. static ssize_t vtpm_proxy_fops_write(struct file *filp, const char __user *buf,
  96. size_t count, loff_t *off)
  97. {
  98. struct proxy_dev *proxy_dev = filp->private_data;
  99. mutex_lock(&proxy_dev->buf_lock);
  100. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  101. mutex_unlock(&proxy_dev->buf_lock);
  102. return -EPIPE;
  103. }
  104. if (count > sizeof(proxy_dev->buffer) ||
  105. !(proxy_dev->state & STATE_WAIT_RESPONSE_FLAG)) {
  106. mutex_unlock(&proxy_dev->buf_lock);
  107. return -EIO;
  108. }
  109. proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
  110. proxy_dev->req_len = 0;
  111. if (copy_from_user(proxy_dev->buffer, buf, count)) {
  112. mutex_unlock(&proxy_dev->buf_lock);
  113. return -EFAULT;
  114. }
  115. proxy_dev->resp_len = count;
  116. mutex_unlock(&proxy_dev->buf_lock);
  117. wake_up_interruptible(&proxy_dev->wq);
  118. return count;
  119. }
  120. /*
  121. * vtpm_proxy_fops_poll: Poll status on 'server side'
  122. *
  123. * Return value:
  124. * Poll flags
  125. */
  126. static unsigned int vtpm_proxy_fops_poll(struct file *filp, poll_table *wait)
  127. {
  128. struct proxy_dev *proxy_dev = filp->private_data;
  129. unsigned ret;
  130. poll_wait(filp, &proxy_dev->wq, wait);
  131. ret = POLLOUT;
  132. mutex_lock(&proxy_dev->buf_lock);
  133. if (proxy_dev->req_len)
  134. ret |= POLLIN | POLLRDNORM;
  135. if (!(proxy_dev->state & STATE_OPENED_FLAG))
  136. ret |= POLLHUP;
  137. mutex_unlock(&proxy_dev->buf_lock);
  138. return ret;
  139. }
  140. /*
  141. * vtpm_proxy_fops_open - Open vTPM device on 'server side'
  142. *
  143. * Called when setting up the anonymous file descriptor
  144. */
  145. static void vtpm_proxy_fops_open(struct file *filp)
  146. {
  147. struct proxy_dev *proxy_dev = filp->private_data;
  148. proxy_dev->state |= STATE_OPENED_FLAG;
  149. }
  150. /**
  151. * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
  152. *
  153. * Call to undo vtpm_proxy_fops_open
  154. */
  155. static void vtpm_proxy_fops_undo_open(struct proxy_dev *proxy_dev)
  156. {
  157. mutex_lock(&proxy_dev->buf_lock);
  158. proxy_dev->state &= ~STATE_OPENED_FLAG;
  159. mutex_unlock(&proxy_dev->buf_lock);
  160. /* no more TPM responses -- wake up anyone waiting for them */
  161. wake_up_interruptible(&proxy_dev->wq);
  162. }
  163. /*
  164. * vtpm_proxy_fops_release: Close 'server side'
  165. *
  166. * Return value:
  167. * Always returns 0.
  168. */
  169. static int vtpm_proxy_fops_release(struct inode *inode, struct file *filp)
  170. {
  171. struct proxy_dev *proxy_dev = filp->private_data;
  172. filp->private_data = NULL;
  173. vtpm_proxy_delete_device(proxy_dev);
  174. return 0;
  175. }
  176. static const struct file_operations vtpm_proxy_fops = {
  177. .owner = THIS_MODULE,
  178. .llseek = no_llseek,
  179. .read = vtpm_proxy_fops_read,
  180. .write = vtpm_proxy_fops_write,
  181. .poll = vtpm_proxy_fops_poll,
  182. .release = vtpm_proxy_fops_release,
  183. };
  184. /*
  185. * Functions invoked by the core TPM driver to send TPM commands to
  186. * 'server side' and receive responses from there.
  187. */
  188. /*
  189. * Called when core TPM driver reads TPM responses from 'server side'
  190. *
  191. * Return value:
  192. * Number of TPM response bytes read, negative error value otherwise
  193. */
  194. static int vtpm_proxy_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  195. {
  196. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  197. size_t len;
  198. /* process gone ? */
  199. mutex_lock(&proxy_dev->buf_lock);
  200. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  201. mutex_unlock(&proxy_dev->buf_lock);
  202. return -EPIPE;
  203. }
  204. len = proxy_dev->resp_len;
  205. if (count < len) {
  206. dev_err(&chip->dev,
  207. "Invalid size in recv: count=%zd, resp_len=%zd\n",
  208. count, len);
  209. len = -EIO;
  210. goto out;
  211. }
  212. memcpy(buf, proxy_dev->buffer, len);
  213. proxy_dev->resp_len = 0;
  214. out:
  215. mutex_unlock(&proxy_dev->buf_lock);
  216. return len;
  217. }
  218. /*
  219. * Called when core TPM driver forwards TPM requests to 'server side'.
  220. *
  221. * Return value:
  222. * 0 in case of success, negative error value otherwise.
  223. */
  224. static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t count)
  225. {
  226. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  227. int rc = 0;
  228. if (count > sizeof(proxy_dev->buffer)) {
  229. dev_err(&chip->dev,
  230. "Invalid size in send: count=%zd, buffer size=%zd\n",
  231. count, sizeof(proxy_dev->buffer));
  232. return -EIO;
  233. }
  234. mutex_lock(&proxy_dev->buf_lock);
  235. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  236. mutex_unlock(&proxy_dev->buf_lock);
  237. return -EPIPE;
  238. }
  239. proxy_dev->resp_len = 0;
  240. proxy_dev->req_len = count;
  241. memcpy(proxy_dev->buffer, buf, count);
  242. proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
  243. mutex_unlock(&proxy_dev->buf_lock);
  244. wake_up_interruptible(&proxy_dev->wq);
  245. return rc;
  246. }
  247. static void vtpm_proxy_tpm_op_cancel(struct tpm_chip *chip)
  248. {
  249. /* not supported */
  250. }
  251. static u8 vtpm_proxy_tpm_op_status(struct tpm_chip *chip)
  252. {
  253. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  254. if (proxy_dev->resp_len)
  255. return VTPM_PROXY_REQ_COMPLETE_FLAG;
  256. return 0;
  257. }
  258. static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)
  259. {
  260. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  261. bool ret;
  262. mutex_lock(&proxy_dev->buf_lock);
  263. ret = !(proxy_dev->state & STATE_OPENED_FLAG);
  264. mutex_unlock(&proxy_dev->buf_lock);
  265. return ret;
  266. }
  267. static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
  268. .flags = TPM_OPS_AUTO_STARTUP,
  269. .recv = vtpm_proxy_tpm_op_recv,
  270. .send = vtpm_proxy_tpm_op_send,
  271. .cancel = vtpm_proxy_tpm_op_cancel,
  272. .status = vtpm_proxy_tpm_op_status,
  273. .req_complete_mask = VTPM_PROXY_REQ_COMPLETE_FLAG,
  274. .req_complete_val = VTPM_PROXY_REQ_COMPLETE_FLAG,
  275. .req_canceled = vtpm_proxy_tpm_req_canceled,
  276. };
  277. /*
  278. * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
  279. * retrieval of timeouts and durations.
  280. */
  281. static void vtpm_proxy_work(struct work_struct *work)
  282. {
  283. struct proxy_dev *proxy_dev = container_of(work, struct proxy_dev,
  284. work);
  285. int rc;
  286. rc = tpm_chip_register(proxy_dev->chip);
  287. if (rc)
  288. vtpm_proxy_fops_undo_open(proxy_dev);
  289. else
  290. proxy_dev->state |= STATE_REGISTERED_FLAG;
  291. }
  292. /*
  293. * vtpm_proxy_work_stop: make sure the work has finished
  294. *
  295. * This function is useful when user space closed the fd
  296. * while the driver still determines timeouts.
  297. */
  298. static void vtpm_proxy_work_stop(struct proxy_dev *proxy_dev)
  299. {
  300. vtpm_proxy_fops_undo_open(proxy_dev);
  301. flush_work(&proxy_dev->work);
  302. }
  303. /*
  304. * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
  305. */
  306. static inline void vtpm_proxy_work_start(struct proxy_dev *proxy_dev)
  307. {
  308. queue_work(workqueue, &proxy_dev->work);
  309. }
  310. /*
  311. * Code related to creation and deletion of device pairs
  312. */
  313. static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
  314. {
  315. struct proxy_dev *proxy_dev;
  316. struct tpm_chip *chip;
  317. int err;
  318. proxy_dev = kzalloc(sizeof(*proxy_dev), GFP_KERNEL);
  319. if (proxy_dev == NULL)
  320. return ERR_PTR(-ENOMEM);
  321. init_waitqueue_head(&proxy_dev->wq);
  322. mutex_init(&proxy_dev->buf_lock);
  323. INIT_WORK(&proxy_dev->work, vtpm_proxy_work);
  324. chip = tpm_chip_alloc(NULL, &vtpm_proxy_tpm_ops);
  325. if (IS_ERR(chip)) {
  326. err = PTR_ERR(chip);
  327. goto err_proxy_dev_free;
  328. }
  329. dev_set_drvdata(&chip->dev, proxy_dev);
  330. proxy_dev->chip = chip;
  331. return proxy_dev;
  332. err_proxy_dev_free:
  333. kfree(proxy_dev);
  334. return ERR_PTR(err);
  335. }
  336. /*
  337. * Undo what has been done in vtpm_create_proxy_dev
  338. */
  339. static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
  340. {
  341. put_device(&proxy_dev->chip->dev); /* frees chip */
  342. kfree(proxy_dev);
  343. }
  344. /*
  345. * Create a /dev/tpm%d and 'server side' file descriptor pair
  346. *
  347. * Return value:
  348. * Returns file pointer on success, an error value otherwise
  349. */
  350. static struct file *vtpm_proxy_create_device(
  351. struct vtpm_proxy_new_dev *vtpm_new_dev)
  352. {
  353. struct proxy_dev *proxy_dev;
  354. int rc, fd;
  355. struct file *file;
  356. if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
  357. return ERR_PTR(-EOPNOTSUPP);
  358. proxy_dev = vtpm_proxy_create_proxy_dev();
  359. if (IS_ERR(proxy_dev))
  360. return ERR_CAST(proxy_dev);
  361. proxy_dev->flags = vtpm_new_dev->flags;
  362. /* setup an anonymous file for the server-side */
  363. fd = get_unused_fd_flags(O_RDWR);
  364. if (fd < 0) {
  365. rc = fd;
  366. goto err_delete_proxy_dev;
  367. }
  368. file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
  369. O_RDWR);
  370. if (IS_ERR(file)) {
  371. rc = PTR_ERR(file);
  372. goto err_put_unused_fd;
  373. }
  374. /* from now on we can unwind with put_unused_fd() + fput() */
  375. /* simulate an open() on the server side */
  376. vtpm_proxy_fops_open(file);
  377. if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
  378. proxy_dev->chip->flags |= TPM_CHIP_FLAG_TPM2;
  379. vtpm_proxy_work_start(proxy_dev);
  380. vtpm_new_dev->fd = fd;
  381. vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
  382. vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
  383. vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
  384. return file;
  385. err_put_unused_fd:
  386. put_unused_fd(fd);
  387. err_delete_proxy_dev:
  388. vtpm_proxy_delete_proxy_dev(proxy_dev);
  389. return ERR_PTR(rc);
  390. }
  391. /*
  392. * Counter part to vtpm_create_device.
  393. */
  394. static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)
  395. {
  396. vtpm_proxy_work_stop(proxy_dev);
  397. /*
  398. * A client may hold the 'ops' lock, so let it know that the server
  399. * side shuts down before we try to grab the 'ops' lock when
  400. * unregistering the chip.
  401. */
  402. vtpm_proxy_fops_undo_open(proxy_dev);
  403. if (proxy_dev->state & STATE_REGISTERED_FLAG)
  404. tpm_chip_unregister(proxy_dev->chip);
  405. vtpm_proxy_delete_proxy_dev(proxy_dev);
  406. }
  407. /*
  408. * Code related to the control device /dev/vtpmx
  409. */
  410. /**
  411. * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl
  412. * @file: /dev/vtpmx
  413. * @ioctl: the ioctl number
  414. * @arg: pointer to the struct vtpmx_proxy_new_dev
  415. *
  416. * Creates an anonymous file that is used by the process acting as a TPM to
  417. * communicate with the client processes. The function will also add a new TPM
  418. * device through which data is proxied to this TPM acting process. The caller
  419. * will be provided with a file descriptor to communicate with the clients and
  420. * major and minor numbers for the TPM device.
  421. */
  422. static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
  423. unsigned long arg)
  424. {
  425. void __user *argp = (void __user *)arg;
  426. struct vtpm_proxy_new_dev __user *vtpm_new_dev_p;
  427. struct vtpm_proxy_new_dev vtpm_new_dev;
  428. struct file *vtpm_file;
  429. if (!capable(CAP_SYS_ADMIN))
  430. return -EPERM;
  431. vtpm_new_dev_p = argp;
  432. if (copy_from_user(&vtpm_new_dev, vtpm_new_dev_p,
  433. sizeof(vtpm_new_dev)))
  434. return -EFAULT;
  435. vtpm_file = vtpm_proxy_create_device(&vtpm_new_dev);
  436. if (IS_ERR(vtpm_file))
  437. return PTR_ERR(vtpm_file);
  438. if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
  439. sizeof(vtpm_new_dev))) {
  440. put_unused_fd(vtpm_new_dev.fd);
  441. fput(vtpm_file);
  442. return -EFAULT;
  443. }
  444. fd_install(vtpm_new_dev.fd, vtpm_file);
  445. return 0;
  446. }
  447. /*
  448. * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
  449. *
  450. * Return value:
  451. * Returns 0 on success, a negative error code otherwise.
  452. */
  453. static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,
  454. unsigned long arg)
  455. {
  456. switch (ioctl) {
  457. case VTPM_PROXY_IOC_NEW_DEV:
  458. return vtpmx_ioc_new_dev(f, ioctl, arg);
  459. default:
  460. return -ENOIOCTLCMD;
  461. }
  462. }
  463. #ifdef CONFIG_COMPAT
  464. static long vtpmx_fops_compat_ioctl(struct file *f, unsigned int ioctl,
  465. unsigned long arg)
  466. {
  467. return vtpmx_fops_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  468. }
  469. #endif
  470. static const struct file_operations vtpmx_fops = {
  471. .owner = THIS_MODULE,
  472. .unlocked_ioctl = vtpmx_fops_ioctl,
  473. #ifdef CONFIG_COMPAT
  474. .compat_ioctl = vtpmx_fops_compat_ioctl,
  475. #endif
  476. .llseek = noop_llseek,
  477. };
  478. static struct miscdevice vtpmx_miscdev = {
  479. .minor = MISC_DYNAMIC_MINOR,
  480. .name = "vtpmx",
  481. .fops = &vtpmx_fops,
  482. };
  483. static int vtpmx_init(void)
  484. {
  485. return misc_register(&vtpmx_miscdev);
  486. }
  487. static void vtpmx_cleanup(void)
  488. {
  489. misc_deregister(&vtpmx_miscdev);
  490. }
  491. static int __init vtpm_module_init(void)
  492. {
  493. int rc;
  494. rc = vtpmx_init();
  495. if (rc) {
  496. pr_err("couldn't create vtpmx device\n");
  497. return rc;
  498. }
  499. workqueue = create_workqueue("tpm-vtpm");
  500. if (!workqueue) {
  501. pr_err("couldn't create workqueue\n");
  502. rc = -ENOMEM;
  503. goto err_vtpmx_cleanup;
  504. }
  505. return 0;
  506. err_vtpmx_cleanup:
  507. vtpmx_cleanup();
  508. return rc;
  509. }
  510. static void __exit vtpm_module_exit(void)
  511. {
  512. destroy_workqueue(workqueue);
  513. vtpmx_cleanup();
  514. }
  515. module_init(vtpm_module_init);
  516. module_exit(vtpm_module_exit);
  517. MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
  518. MODULE_DESCRIPTION("vTPM Driver");
  519. MODULE_VERSION("0.1");
  520. MODULE_LICENSE("GPL");