common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. void tpm_bios_log_setup(struct tpm_chip *chip)
  93. {
  94. const char *name = dev_name(&chip->dev);
  95. unsigned int cnt;
  96. int log_version;
  97. int rc = 0;
  98. if (chip->flags & TPM_CHIP_FLAG_VIRTUAL)
  99. return;
  100. rc = tpm_read_log(chip);
  101. if (rc < 0)
  102. return;
  103. log_version = rc;
  104. cnt = 0;
  105. chip->bios_dir[cnt] = securityfs_create_dir(name, NULL);
  106. /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
  107. * compiled out. The caller should ignore the ENODEV return code.
  108. */
  109. if (IS_ERR(chip->bios_dir[cnt]))
  110. goto err;
  111. cnt++;
  112. chip->bin_log_seqops.chip = chip;
  113. if (log_version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
  114. chip->bin_log_seqops.seqops =
  115. &tpm2_binary_b_measurements_seqops;
  116. else
  117. chip->bin_log_seqops.seqops =
  118. &tpm1_binary_b_measurements_seqops;
  119. chip->bios_dir[cnt] =
  120. securityfs_create_file("binary_bios_measurements",
  121. 0440, chip->bios_dir[0],
  122. (void *)&chip->bin_log_seqops,
  123. &tpm_bios_measurements_ops);
  124. if (IS_ERR(chip->bios_dir[cnt]))
  125. goto err;
  126. cnt++;
  127. if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
  128. chip->ascii_log_seqops.chip = chip;
  129. chip->ascii_log_seqops.seqops =
  130. &tpm1_ascii_b_measurements_seqops;
  131. chip->bios_dir[cnt] =
  132. securityfs_create_file("ascii_bios_measurements",
  133. 0440, chip->bios_dir[0],
  134. (void *)&chip->ascii_log_seqops,
  135. &tpm_bios_measurements_ops);
  136. if (IS_ERR(chip->bios_dir[cnt]))
  137. goto err;
  138. cnt++;
  139. }
  140. return;
  141. err:
  142. chip->bios_dir[cnt] = NULL;
  143. tpm_bios_log_teardown(chip);
  144. return;
  145. }
  146. void tpm_bios_log_teardown(struct tpm_chip *chip)
  147. {
  148. int i;
  149. struct inode *inode;
  150. /* securityfs_remove currently doesn't take care of handling sync
  151. * between removal and opening of pseudo files. To handle this, a
  152. * workaround is added by making i_private = NULL here during removal
  153. * and to check it during open(), both within inode_lock()/unlock().
  154. * This design ensures that open() either safely gets kref or fails.
  155. */
  156. for (i = (TPM_NUM_EVENT_LOG_FILES - 1); i >= 0; i--) {
  157. if (chip->bios_dir[i]) {
  158. inode = d_inode(chip->bios_dir[i]);
  159. inode_lock(inode);
  160. inode->i_private = NULL;
  161. inode_unlock(inode);
  162. securityfs_remove(chip->bios_dir[i]);
  163. }
  164. }
  165. }