common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2005, 2012 IBM Corporation
  3. *
  4. * Authors:
  5. * Kent Yoder <key@linux.vnet.ibm.com>
  6. * Seiji Munetoh <munetoh@jp.ibm.com>
  7. * Stefan Berger <stefanb@us.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. * Nayna Jain <nayna@linux.vnet.ibm.com>
  11. *
  12. * Access to the event log created by a system's firmware / BIOS
  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
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/seq_file.h>
  21. #include <linux/fs.h>
  22. #include <linux/security.h>
  23. #include <linux/module.h>
  24. #include <linux/tpm_eventlog.h>
  25. #include "../tpm.h"
  26. #include "common.h"
  27. static int tpm_bios_measurements_open(struct inode *inode,
  28. struct file *file)
  29. {
  30. int err;
  31. struct seq_file *seq;
  32. struct tpm_chip_seqops *chip_seqops;
  33. const struct seq_operations *seqops;
  34. struct tpm_chip *chip;
  35. inode_lock(inode);
  36. if (!inode->i_private) {
  37. inode_unlock(inode);
  38. return -ENODEV;
  39. }
  40. chip_seqops = (struct tpm_chip_seqops *)inode->i_private;
  41. seqops = chip_seqops->seqops;
  42. chip = chip_seqops->chip;
  43. get_device(&chip->dev);
  44. inode_unlock(inode);
  45. /* now register seq file */
  46. err = seq_open(file, seqops);
  47. if (!err) {
  48. seq = file->private_data;
  49. seq->private = chip;
  50. }
  51. return err;
  52. }
  53. static int tpm_bios_measurements_release(struct inode *inode,
  54. struct file *file)
  55. {
  56. struct seq_file *seq = (struct seq_file *)file->private_data;
  57. struct tpm_chip *chip = (struct tpm_chip *)seq->private;
  58. put_device(&chip->dev);
  59. return seq_release(inode, file);
  60. }
  61. static const struct file_operations tpm_bios_measurements_ops = {
  62. .owner = THIS_MODULE,
  63. .open = tpm_bios_measurements_open,
  64. .read = seq_read,
  65. .llseek = seq_lseek,
  66. .release = tpm_bios_measurements_release,
  67. };
  68. static int tpm_read_log(struct tpm_chip *chip)
  69. {
  70. int rc;
  71. if (chip->log.bios_event_log != NULL) {
  72. dev_dbg(&chip->dev,
  73. "%s: ERROR - event log already initialized\n",
  74. __func__);
  75. return -EFAULT;
  76. }
  77. rc = tpm_read_log_acpi(chip);
  78. if (rc != -ENODEV)
  79. return rc;
  80. rc = tpm_read_log_efi(chip);
  81. if (rc != -ENODEV)
  82. return rc;
  83. return tpm_read_log_of(chip);
  84. }
  85. /*
  86. * tpm_bios_log_setup() - Read the event log from the firmware
  87. * @chip: TPM chip to use.
  88. *
  89. * If an event log is found then the securityfs files are setup to
  90. * export it to userspace, otherwise nothing is done.
  91. *
  92. * Returns -ENODEV if the firmware has no event log or securityfs is not
  93. * supported.
  94. */
  95. int tpm_bios_log_setup(struct tpm_chip *chip)
  96. {
  97. const char *name = dev_name(&chip->dev);
  98. unsigned int cnt;
  99. int log_version;
  100. int rc = 0;
  101. rc = tpm_read_log(chip);
  102. if (rc < 0)
  103. return rc;
  104. log_version = rc;
  105. cnt = 0;
  106. chip->bios_dir[cnt] = securityfs_create_dir(name, NULL);
  107. /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
  108. * compiled out. The caller should ignore the ENODEV return code.
  109. */
  110. if (IS_ERR(chip->bios_dir[cnt]))
  111. goto err;
  112. cnt++;
  113. chip->bin_log_seqops.chip = chip;
  114. if (log_version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
  115. chip->bin_log_seqops.seqops =
  116. &tpm2_binary_b_measurements_seqops;
  117. else
  118. chip->bin_log_seqops.seqops =
  119. &tpm1_binary_b_measurements_seqops;
  120. chip->bios_dir[cnt] =
  121. securityfs_create_file("binary_bios_measurements",
  122. 0440, chip->bios_dir[0],
  123. (void *)&chip->bin_log_seqops,
  124. &tpm_bios_measurements_ops);
  125. if (IS_ERR(chip->bios_dir[cnt]))
  126. goto err;
  127. cnt++;
  128. if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
  129. chip->ascii_log_seqops.chip = chip;
  130. chip->ascii_log_seqops.seqops =
  131. &tpm1_ascii_b_measurements_seqops;
  132. chip->bios_dir[cnt] =
  133. securityfs_create_file("ascii_bios_measurements",
  134. 0440, chip->bios_dir[0],
  135. (void *)&chip->ascii_log_seqops,
  136. &tpm_bios_measurements_ops);
  137. if (IS_ERR(chip->bios_dir[cnt]))
  138. goto err;
  139. cnt++;
  140. }
  141. return 0;
  142. err:
  143. rc = PTR_ERR(chip->bios_dir[cnt]);
  144. chip->bios_dir[cnt] = NULL;
  145. tpm_bios_log_teardown(chip);
  146. return rc;
  147. }
  148. void tpm_bios_log_teardown(struct tpm_chip *chip)
  149. {
  150. int i;
  151. struct inode *inode;
  152. /* securityfs_remove currently doesn't take care of handling sync
  153. * between removal and opening of pseudo files. To handle this, a
  154. * workaround is added by making i_private = NULL here during removal
  155. * and to check it during open(), both within inode_lock()/unlock().
  156. * This design ensures that open() either safely gets kref or fails.
  157. */
  158. for (i = (TPM_NUM_EVENT_LOG_FILES - 1); i >= 0; i--) {
  159. if (chip->bios_dir[i]) {
  160. inode = d_inode(chip->bios_dir[i]);
  161. inode_lock(inode);
  162. inode->i_private = NULL;
  163. inode_unlock(inode);
  164. securityfs_remove(chip->bios_dir[i]);
  165. }
  166. }
  167. }