tpm-dev-common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. atomic_set(&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. atomic_set(&priv->data_pending, 0);
  44. mutex_init(&priv->buffer_mutex);
  45. timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
  46. INIT_WORK(&priv->work, timeout_work);
  47. file->private_data = priv;
  48. }
  49. ssize_t tpm_common_read(struct file *file, char __user *buf,
  50. size_t size, loff_t *off)
  51. {
  52. struct file_priv *priv = file->private_data;
  53. ssize_t ret_size;
  54. ssize_t orig_ret_size;
  55. int rc;
  56. del_singleshot_timer_sync(&priv->user_read_timer);
  57. flush_work(&priv->work);
  58. ret_size = atomic_read(&priv->data_pending);
  59. if (ret_size > 0) { /* relay data */
  60. orig_ret_size = ret_size;
  61. if (size < ret_size)
  62. ret_size = size;
  63. mutex_lock(&priv->buffer_mutex);
  64. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  65. memset(priv->data_buffer, 0, orig_ret_size);
  66. if (rc)
  67. ret_size = -EFAULT;
  68. mutex_unlock(&priv->buffer_mutex);
  69. }
  70. atomic_set(&priv->data_pending, 0);
  71. return ret_size;
  72. }
  73. ssize_t tpm_common_write(struct file *file, const char __user *buf,
  74. size_t size, loff_t *off, struct tpm_space *space)
  75. {
  76. struct file_priv *priv = file->private_data;
  77. size_t in_size = size;
  78. ssize_t out_size;
  79. /* Cannot perform a write until the read has cleared either via
  80. * tpm_read or a user_read_timer timeout. This also prevents split
  81. * buffered writes from blocking here.
  82. */
  83. if (atomic_read(&priv->data_pending) != 0)
  84. return -EBUSY;
  85. if (in_size > TPM_BUFSIZE)
  86. return -E2BIG;
  87. mutex_lock(&priv->buffer_mutex);
  88. if (copy_from_user
  89. (priv->data_buffer, (void __user *) buf, in_size)) {
  90. mutex_unlock(&priv->buffer_mutex);
  91. return -EFAULT;
  92. }
  93. if (in_size < 6 ||
  94. in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
  95. mutex_unlock(&priv->buffer_mutex);
  96. return -EINVAL;
  97. }
  98. /* atomic tpm command send and result receive. We only hold the ops
  99. * lock during this period so that the tpm can be unregistered even if
  100. * the char dev is held open.
  101. */
  102. if (tpm_try_get_ops(priv->chip)) {
  103. mutex_unlock(&priv->buffer_mutex);
  104. return -EPIPE;
  105. }
  106. out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
  107. sizeof(priv->data_buffer), 0);
  108. tpm_put_ops(priv->chip);
  109. if (out_size < 0) {
  110. mutex_unlock(&priv->buffer_mutex);
  111. return out_size;
  112. }
  113. atomic_set(&priv->data_pending, out_size);
  114. mutex_unlock(&priv->buffer_mutex);
  115. /* Set a timeout by which the reader must come claim the result */
  116. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  117. return in_size;
  118. }
  119. /*
  120. * Called on file close
  121. */
  122. void tpm_common_release(struct file *file, struct file_priv *priv)
  123. {
  124. del_singleshot_timer_sync(&priv->user_read_timer);
  125. flush_work(&priv->work);
  126. file->private_data = NULL;
  127. atomic_set(&priv->data_pending, 0);
  128. }