common.c 4.4 KB

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