tpm-dev-common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Authors:
  4. * Leendert van Doorn <leendert@watson.ibm.com>
  5. * Dave Safford <safford@watson.ibm.com>
  6. * Reiner Sailer <sailer@watson.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * Copyright (C) 2013 Obsidian Research Corp
  10. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  11. *
  12. * Device file system interface to the TPM
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation, version 2 of the
  17. * License.
  18. *
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include "tpm.h"
  23. #include "tpm-dev.h"
  24. static void user_reader_timeout(struct timer_list *t)
  25. {
  26. struct file_priv *priv = from_timer(priv, t, user_read_timer);
  27. pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
  28. task_tgid_nr(current));
  29. schedule_work(&priv->work);
  30. }
  31. static void timeout_work(struct work_struct *work)
  32. {
  33. struct file_priv *priv = container_of(work, struct file_priv, work);
  34. mutex_lock(&priv->buffer_mutex);
  35. priv->data_pending = 0;
  36. memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
  37. mutex_unlock(&priv->buffer_mutex);
  38. }
  39. void tpm_common_open(struct file *file, struct tpm_chip *chip,
  40. struct file_priv *priv)
  41. {
  42. priv->chip = chip;
  43. mutex_init(&priv->buffer_mutex);
  44. timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
  45. INIT_WORK(&priv->work, timeout_work);
  46. file->private_data = priv;
  47. }
  48. ssize_t tpm_common_read(struct file *file, char __user *buf,
  49. size_t size, loff_t *off)
  50. {
  51. struct file_priv *priv = file->private_data;
  52. ssize_t ret_size = 0;
  53. int rc;
  54. del_singleshot_timer_sync(&priv->user_read_timer);
  55. flush_work(&priv->work);
  56. mutex_lock(&priv->buffer_mutex);
  57. if (priv->data_pending) {
  58. ret_size = min_t(ssize_t, size, priv->data_pending);
  59. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  60. memset(priv->data_buffer, 0, priv->data_pending);
  61. if (rc)
  62. ret_size = -EFAULT;
  63. priv->data_pending = 0;
  64. }
  65. mutex_unlock(&priv->buffer_mutex);
  66. return ret_size;
  67. }
  68. ssize_t tpm_common_write(struct file *file, const char __user *buf,
  69. size_t size, loff_t *off, struct tpm_space *space)
  70. {
  71. struct file_priv *priv = file->private_data;
  72. size_t in_size = size;
  73. ssize_t out_size;
  74. if (in_size > TPM_BUFSIZE)
  75. return -E2BIG;
  76. mutex_lock(&priv->buffer_mutex);
  77. /* Cannot perform a write until the read has cleared either via
  78. * tpm_read or a user_read_timer timeout. This also prevents split
  79. * buffered writes from blocking here.
  80. */
  81. if (priv->data_pending != 0) {
  82. mutex_unlock(&priv->buffer_mutex);
  83. return -EBUSY;
  84. }
  85. if (copy_from_user
  86. (priv->data_buffer, (void __user *) buf, in_size)) {
  87. mutex_unlock(&priv->buffer_mutex);
  88. return -EFAULT;
  89. }
  90. if (in_size < 6 ||
  91. in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
  92. mutex_unlock(&priv->buffer_mutex);
  93. return -EINVAL;
  94. }
  95. /* atomic tpm command send and result receive. We only hold the ops
  96. * lock during this period so that the tpm can be unregistered even if
  97. * the char dev is held open.
  98. */
  99. if (tpm_try_get_ops(priv->chip)) {
  100. mutex_unlock(&priv->buffer_mutex);
  101. return -EPIPE;
  102. }
  103. out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
  104. sizeof(priv->data_buffer), 0);
  105. tpm_put_ops(priv->chip);
  106. if (out_size < 0) {
  107. mutex_unlock(&priv->buffer_mutex);
  108. return out_size;
  109. }
  110. priv->data_pending = out_size;
  111. mutex_unlock(&priv->buffer_mutex);
  112. /* Set a timeout by which the reader must come claim the result */
  113. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  114. return in_size;
  115. }
  116. /*
  117. * Called on file close
  118. */
  119. void tpm_common_release(struct file *file, struct file_priv *priv)
  120. {
  121. del_singleshot_timer_sync(&priv->user_read_timer);
  122. flush_work(&priv->work);
  123. file->private_data = NULL;
  124. priv->data_pending = 0;
  125. }