tpm-dev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. struct file_priv {
  24. struct tpm_chip *chip;
  25. /* Data passed to and from the tpm via the read/write calls */
  26. atomic_t data_pending;
  27. struct mutex buffer_mutex;
  28. struct timer_list user_read_timer; /* user needs to claim result */
  29. struct work_struct work;
  30. u8 data_buffer[TPM_BUFSIZE];
  31. };
  32. static void user_reader_timeout(unsigned long ptr)
  33. {
  34. struct file_priv *priv = (struct file_priv *)ptr;
  35. pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
  36. task_tgid_nr(current));
  37. schedule_work(&priv->work);
  38. }
  39. static void timeout_work(struct work_struct *work)
  40. {
  41. struct file_priv *priv = container_of(work, struct file_priv, work);
  42. mutex_lock(&priv->buffer_mutex);
  43. atomic_set(&priv->data_pending, 0);
  44. memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
  45. mutex_unlock(&priv->buffer_mutex);
  46. }
  47. static int tpm_open(struct inode *inode, struct file *file)
  48. {
  49. struct tpm_chip *chip =
  50. container_of(inode->i_cdev, struct tpm_chip, cdev);
  51. struct file_priv *priv;
  52. /* It's assured that the chip will be opened just once,
  53. * by the check of is_open variable, which is protected
  54. * by driver_lock. */
  55. if (test_and_set_bit(0, &chip->is_open)) {
  56. dev_dbg(&chip->dev, "Another process owns this TPM\n");
  57. return -EBUSY;
  58. }
  59. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  60. if (priv == NULL) {
  61. clear_bit(0, &chip->is_open);
  62. return -ENOMEM;
  63. }
  64. priv->chip = chip;
  65. atomic_set(&priv->data_pending, 0);
  66. mutex_init(&priv->buffer_mutex);
  67. setup_timer(&priv->user_read_timer, user_reader_timeout,
  68. (unsigned long)priv);
  69. INIT_WORK(&priv->work, timeout_work);
  70. file->private_data = priv;
  71. return 0;
  72. }
  73. static ssize_t tpm_read(struct file *file, char __user *buf,
  74. size_t size, loff_t *off)
  75. {
  76. struct file_priv *priv = file->private_data;
  77. ssize_t ret_size;
  78. int rc;
  79. del_singleshot_timer_sync(&priv->user_read_timer);
  80. flush_work(&priv->work);
  81. ret_size = atomic_read(&priv->data_pending);
  82. if (ret_size > 0) { /* relay data */
  83. ssize_t orig_ret_size = ret_size;
  84. if (size < ret_size)
  85. ret_size = size;
  86. mutex_lock(&priv->buffer_mutex);
  87. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  88. memset(priv->data_buffer, 0, orig_ret_size);
  89. if (rc)
  90. ret_size = -EFAULT;
  91. mutex_unlock(&priv->buffer_mutex);
  92. }
  93. atomic_set(&priv->data_pending, 0);
  94. return ret_size;
  95. }
  96. static ssize_t tpm_write(struct file *file, const char __user *buf,
  97. size_t size, loff_t *off)
  98. {
  99. struct file_priv *priv = file->private_data;
  100. size_t in_size = size;
  101. ssize_t out_size;
  102. /* cannot perform a write until the read has cleared
  103. either via tpm_read or a user_read_timer timeout.
  104. This also prevents splitted buffered writes from blocking here.
  105. */
  106. if (atomic_read(&priv->data_pending) != 0)
  107. return -EBUSY;
  108. if (in_size > TPM_BUFSIZE)
  109. return -E2BIG;
  110. mutex_lock(&priv->buffer_mutex);
  111. if (copy_from_user
  112. (priv->data_buffer, (void __user *) buf, in_size)) {
  113. mutex_unlock(&priv->buffer_mutex);
  114. return -EFAULT;
  115. }
  116. /* atomic tpm command send and result receive. We only hold the ops
  117. * lock during this period so that the tpm can be unregistered even if
  118. * the char dev is held open.
  119. */
  120. if (tpm_try_get_ops(priv->chip)) {
  121. mutex_unlock(&priv->buffer_mutex);
  122. return -EPIPE;
  123. }
  124. out_size = tpm_transmit(priv->chip, priv->data_buffer,
  125. sizeof(priv->data_buffer), 0);
  126. tpm_put_ops(priv->chip);
  127. if (out_size < 0) {
  128. mutex_unlock(&priv->buffer_mutex);
  129. return out_size;
  130. }
  131. atomic_set(&priv->data_pending, out_size);
  132. mutex_unlock(&priv->buffer_mutex);
  133. /* Set a timeout by which the reader must come claim the result */
  134. mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
  135. return in_size;
  136. }
  137. /*
  138. * Called on file close
  139. */
  140. static int tpm_release(struct inode *inode, struct file *file)
  141. {
  142. struct file_priv *priv = file->private_data;
  143. del_singleshot_timer_sync(&priv->user_read_timer);
  144. flush_work(&priv->work);
  145. file->private_data = NULL;
  146. atomic_set(&priv->data_pending, 0);
  147. clear_bit(0, &priv->chip->is_open);
  148. kfree(priv);
  149. return 0;
  150. }
  151. const struct file_operations tpm_fops = {
  152. .owner = THIS_MODULE,
  153. .llseek = no_llseek,
  154. .open = tpm_open,
  155. .read = tpm_read,
  156. .write = tpm_write,
  157. .release = tpm_release,
  158. };