tpm2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2016 IBM Corporation
  3. *
  4. * Authors:
  5. * Nayna Jain <nayna@linux.vnet.ibm.com>
  6. *
  7. * Access to TPM 2.0 event log as written by Firmware.
  8. * It assumes that writer of event log has followed TCG Specification
  9. * for Family "2.0" and written the event data in little endian.
  10. * With that, it doesn't need any endian conversion for structure
  11. * content.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/seq_file.h>
  19. #include <linux/fs.h>
  20. #include <linux/security.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/tpm_eventlog.h>
  24. #include "../tpm.h"
  25. #include "common.h"
  26. /*
  27. * calc_tpm2_event_size() - calculate the event size, where event
  28. * is an entry in the TPM 2.0 event log. The event is of type Crypto
  29. * Agile Log Entry Format as defined in TCG EFI Protocol Specification
  30. * Family "2.0".
  31. * @event: event whose size is to be calculated.
  32. * @event_header: the first event in the event log.
  33. *
  34. * Returns size of the event. If it is an invalid event, returns 0.
  35. */
  36. static size_t calc_tpm2_event_size(struct tcg_pcr_event2 *event,
  37. struct tcg_pcr_event *event_header)
  38. {
  39. struct tcg_efi_specid_event *efispecid;
  40. struct tcg_event_field *event_field;
  41. void *marker;
  42. void *marker_start;
  43. u32 halg_size;
  44. size_t size;
  45. u16 halg;
  46. int i;
  47. int j;
  48. marker = event;
  49. marker_start = marker;
  50. marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
  51. + sizeof(event->count);
  52. efispecid = (struct tcg_efi_specid_event *)event_header->event;
  53. /* Check if event is malformed. */
  54. if (event->count > efispecid->num_algs)
  55. return 0;
  56. for (i = 0; i < event->count; i++) {
  57. halg_size = sizeof(event->digests[i].alg_id);
  58. memcpy(&halg, marker, halg_size);
  59. marker = marker + halg_size;
  60. for (j = 0; j < efispecid->num_algs; j++) {
  61. if (halg == efispecid->digest_sizes[j].alg_id) {
  62. marker +=
  63. efispecid->digest_sizes[j].digest_size;
  64. break;
  65. }
  66. }
  67. /* Algorithm without known length. Such event is unparseable. */
  68. if (j == efispecid->num_algs)
  69. return 0;
  70. }
  71. event_field = (struct tcg_event_field *)marker;
  72. marker = marker + sizeof(event_field->event_size)
  73. + event_field->event_size;
  74. size = marker - marker_start;
  75. if ((event->event_type == 0) && (event_field->event_size == 0))
  76. return 0;
  77. return size;
  78. }
  79. static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
  80. {
  81. struct tpm_chip *chip = m->private;
  82. struct tpm_bios_log *log = &chip->log;
  83. void *addr = log->bios_event_log;
  84. void *limit = log->bios_event_log_end;
  85. struct tcg_pcr_event *event_header;
  86. struct tcg_pcr_event2 *event;
  87. size_t size;
  88. int i;
  89. event_header = addr;
  90. size = sizeof(struct tcg_pcr_event) - sizeof(event_header->event)
  91. + event_header->event_size;
  92. if (*pos == 0) {
  93. if (addr + size < limit) {
  94. if ((event_header->event_type == 0) &&
  95. (event_header->event_size == 0))
  96. return NULL;
  97. return SEQ_START_TOKEN;
  98. }
  99. }
  100. if (*pos > 0) {
  101. addr += size;
  102. event = addr;
  103. size = calc_tpm2_event_size(event, event_header);
  104. if ((addr + size >= limit) || (size == 0))
  105. return NULL;
  106. }
  107. for (i = 0; i < (*pos - 1); i++) {
  108. event = addr;
  109. size = calc_tpm2_event_size(event, event_header);
  110. if ((addr + size >= limit) || (size == 0))
  111. return NULL;
  112. addr += size;
  113. }
  114. return addr;
  115. }
  116. static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
  117. loff_t *pos)
  118. {
  119. struct tcg_pcr_event *event_header;
  120. struct tcg_pcr_event2 *event;
  121. struct tpm_chip *chip = m->private;
  122. struct tpm_bios_log *log = &chip->log;
  123. void *limit = log->bios_event_log_end;
  124. size_t event_size;
  125. void *marker;
  126. event_header = log->bios_event_log;
  127. if (v == SEQ_START_TOKEN) {
  128. event_size = sizeof(struct tcg_pcr_event) -
  129. sizeof(event_header->event) + event_header->event_size;
  130. marker = event_header;
  131. } else {
  132. event = v;
  133. event_size = calc_tpm2_event_size(event, event_header);
  134. if (event_size == 0)
  135. return NULL;
  136. marker = event;
  137. }
  138. marker = marker + event_size;
  139. if (marker >= limit)
  140. return NULL;
  141. v = marker;
  142. event = v;
  143. event_size = calc_tpm2_event_size(event, event_header);
  144. if (((v + event_size) >= limit) || (event_size == 0))
  145. return NULL;
  146. (*pos)++;
  147. return v;
  148. }
  149. static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
  150. {
  151. }
  152. static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
  153. {
  154. struct tpm_chip *chip = m->private;
  155. struct tpm_bios_log *log = &chip->log;
  156. struct tcg_pcr_event *event_header = log->bios_event_log;
  157. struct tcg_pcr_event2 *event = v;
  158. void *temp_ptr;
  159. size_t size;
  160. if (v == SEQ_START_TOKEN) {
  161. size = sizeof(struct tcg_pcr_event) -
  162. sizeof(event_header->event) + event_header->event_size;
  163. temp_ptr = event_header;
  164. if (size > 0)
  165. seq_write(m, temp_ptr, size);
  166. } else {
  167. size = calc_tpm2_event_size(event, event_header);
  168. temp_ptr = event;
  169. if (size > 0)
  170. seq_write(m, temp_ptr, size);
  171. }
  172. return 0;
  173. }
  174. const struct seq_operations tpm2_binary_b_measurements_seqops = {
  175. .start = tpm2_bios_measurements_start,
  176. .next = tpm2_bios_measurements_next,
  177. .stop = tpm2_bios_measurements_stop,
  178. .show = tpm2_binary_bios_measurements_show,
  179. };