tpm2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 IBM Corporation
  4. *
  5. * Authors:
  6. * Nayna Jain <[email protected]>
  7. *
  8. * Access to TPM 2.0 event log as written by Firmware.
  9. * It assumes that writer of event log has followed TCG Specification
  10. * for Family "2.0" and written the event data in little endian.
  11. * With that, it doesn't need any endian conversion for structure
  12. * content.
  13. */
  14. #include <linux/seq_file.h>
  15. #include <linux/fs.h>
  16. #include <linux/security.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/tpm_eventlog.h>
  20. #include "../tpm.h"
  21. #include "common.h"
  22. /*
  23. * calc_tpm2_event_size() - calculate the event size, where event
  24. * is an entry in the TPM 2.0 event log. The event is of type Crypto
  25. * Agile Log Entry Format as defined in TCG EFI Protocol Specification
  26. * Family "2.0".
  27. * @event: event whose size is to be calculated.
  28. * @event_header: the first event in the event log.
  29. *
  30. * Returns size of the event. If it is an invalid event, returns 0.
  31. */
  32. static size_t calc_tpm2_event_size(struct tcg_pcr_event2_head *event,
  33. struct tcg_pcr_event *event_header)
  34. {
  35. return __calc_tpm2_event_size(event, event_header, false);
  36. }
  37. static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
  38. {
  39. struct tpm_chip *chip = m->private;
  40. struct tpm_bios_log *log = &chip->log;
  41. void *addr = log->bios_event_log;
  42. void *limit = log->bios_event_log_end;
  43. struct tcg_pcr_event *event_header;
  44. struct tcg_pcr_event2_head *event;
  45. size_t size;
  46. int i;
  47. event_header = addr;
  48. size = struct_size(event_header, event, event_header->event_size);
  49. if (*pos == 0) {
  50. if (addr + size < limit) {
  51. if ((event_header->event_type == 0) &&
  52. (event_header->event_size == 0))
  53. return NULL;
  54. return SEQ_START_TOKEN;
  55. }
  56. }
  57. if (*pos > 0) {
  58. addr += size;
  59. event = addr;
  60. size = calc_tpm2_event_size(event, event_header);
  61. if ((addr + size >= limit) || (size == 0))
  62. return NULL;
  63. }
  64. for (i = 0; i < (*pos - 1); i++) {
  65. event = addr;
  66. size = calc_tpm2_event_size(event, event_header);
  67. if ((addr + size >= limit) || (size == 0))
  68. return NULL;
  69. addr += size;
  70. }
  71. return addr;
  72. }
  73. static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
  74. loff_t *pos)
  75. {
  76. struct tcg_pcr_event *event_header;
  77. struct tcg_pcr_event2_head *event;
  78. struct tpm_chip *chip = m->private;
  79. struct tpm_bios_log *log = &chip->log;
  80. void *limit = log->bios_event_log_end;
  81. size_t event_size;
  82. void *marker;
  83. (*pos)++;
  84. event_header = log->bios_event_log;
  85. if (v == SEQ_START_TOKEN) {
  86. event_size = struct_size(event_header, event,
  87. event_header->event_size);
  88. marker = event_header;
  89. } else {
  90. event = v;
  91. event_size = calc_tpm2_event_size(event, event_header);
  92. if (event_size == 0)
  93. return NULL;
  94. marker = event;
  95. }
  96. marker = marker + event_size;
  97. if (marker >= limit)
  98. return NULL;
  99. v = marker;
  100. event = v;
  101. event_size = calc_tpm2_event_size(event, event_header);
  102. if (((v + event_size) >= limit) || (event_size == 0))
  103. return NULL;
  104. return v;
  105. }
  106. static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
  107. {
  108. }
  109. static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
  110. {
  111. struct tpm_chip *chip = m->private;
  112. struct tpm_bios_log *log = &chip->log;
  113. struct tcg_pcr_event *event_header = log->bios_event_log;
  114. struct tcg_pcr_event2_head *event = v;
  115. void *temp_ptr;
  116. size_t size;
  117. if (v == SEQ_START_TOKEN) {
  118. size = struct_size(event_header, event,
  119. event_header->event_size);
  120. temp_ptr = event_header;
  121. if (size > 0)
  122. seq_write(m, temp_ptr, size);
  123. } else {
  124. size = calc_tpm2_event_size(event, event_header);
  125. temp_ptr = event;
  126. if (size > 0)
  127. seq_write(m, temp_ptr, size);
  128. }
  129. return 0;
  130. }
  131. const struct seq_operations tpm2_binary_b_measurements_seqops = {
  132. .start = tpm2_bios_measurements_start,
  133. .next = tpm2_bios_measurements_next,
  134. .stop = tpm2_bios_measurements_stop,
  135. .show = tpm2_binary_bios_measurements_show,
  136. };