tpm_eventlog.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_TPM_EVENTLOG_H__
  3. #define __LINUX_TPM_EVENTLOG_H__
  4. #include <linux/tpm.h>
  5. #define TCG_EVENT_NAME_LEN_MAX 255
  6. #define MAX_TEXT_EVENT 1000 /* Max event string length */
  7. #define ACPI_TCPA_SIG "TCPA" /* 0x41504354 /'TCPA' */
  8. #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x1
  9. #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 0x2
  10. #ifdef CONFIG_PPC64
  11. #define do_endian_conversion(x) be32_to_cpu(x)
  12. #else
  13. #define do_endian_conversion(x) x
  14. #endif
  15. enum bios_platform_class {
  16. BIOS_CLIENT = 0x00,
  17. BIOS_SERVER = 0x01,
  18. };
  19. struct tcpa_event {
  20. u32 pcr_index;
  21. u32 event_type;
  22. u8 pcr_value[20]; /* SHA1 */
  23. u32 event_size;
  24. u8 event_data[];
  25. };
  26. enum tcpa_event_types {
  27. PREBOOT = 0,
  28. POST_CODE,
  29. UNUSED,
  30. NO_ACTION,
  31. SEPARATOR,
  32. ACTION,
  33. EVENT_TAG,
  34. SCRTM_CONTENTS,
  35. SCRTM_VERSION,
  36. CPU_MICROCODE,
  37. PLATFORM_CONFIG_FLAGS,
  38. TABLE_OF_DEVICES,
  39. COMPACT_HASH,
  40. IPL,
  41. IPL_PARTITION_DATA,
  42. NONHOST_CODE,
  43. NONHOST_CONFIG,
  44. NONHOST_INFO,
  45. };
  46. struct tcpa_pc_event {
  47. u32 event_id;
  48. u32 event_size;
  49. u8 event_data[];
  50. };
  51. enum tcpa_pc_event_ids {
  52. SMBIOS = 1,
  53. BIS_CERT,
  54. POST_BIOS_ROM,
  55. ESCD,
  56. CMOS,
  57. NVRAM,
  58. OPTION_ROM_EXEC,
  59. OPTION_ROM_CONFIG,
  60. OPTION_ROM_MICROCODE = 10,
  61. S_CRTM_VERSION,
  62. S_CRTM_CONTENTS,
  63. POST_CONTENTS,
  64. HOST_TABLE_OF_DEVICES,
  65. };
  66. /* http://www.trustedcomputinggroup.org/tcg-efi-protocol-specification/ */
  67. struct tcg_efi_specid_event_algs {
  68. u16 alg_id;
  69. u16 digest_size;
  70. } __packed;
  71. #define TCG_SPECID_SIG "Spec ID Event03"
  72. struct tcg_efi_specid_event_head {
  73. u8 signature[16];
  74. u32 platform_class;
  75. u8 spec_version_minor;
  76. u8 spec_version_major;
  77. u8 spec_errata;
  78. u8 uintnsize;
  79. u32 num_algs;
  80. struct tcg_efi_specid_event_algs digest_sizes[];
  81. } __packed;
  82. struct tcg_pcr_event {
  83. u32 pcr_idx;
  84. u32 event_type;
  85. u8 digest[20];
  86. u32 event_size;
  87. u8 event[];
  88. } __packed;
  89. struct tcg_event_field {
  90. u32 event_size;
  91. u8 event[];
  92. } __packed;
  93. struct tcg_pcr_event2_head {
  94. u32 pcr_idx;
  95. u32 event_type;
  96. u32 count;
  97. struct tpm_digest digests[];
  98. } __packed;
  99. struct tcg_algorithm_size {
  100. u16 algorithm_id;
  101. u16 algorithm_size;
  102. };
  103. struct tcg_algorithm_info {
  104. u8 signature[16];
  105. u32 platform_class;
  106. u8 spec_version_minor;
  107. u8 spec_version_major;
  108. u8 spec_errata;
  109. u8 uintn_size;
  110. u32 number_of_algorithms;
  111. struct tcg_algorithm_size digest_sizes[];
  112. };
  113. #ifndef TPM_MEMREMAP
  114. #define TPM_MEMREMAP(start, size) NULL
  115. #endif
  116. #ifndef TPM_MEMUNMAP
  117. #define TPM_MEMUNMAP(start, size) do{} while(0)
  118. #endif
  119. /**
  120. * __calc_tpm2_event_size - calculate the size of a TPM2 event log entry
  121. * @event: Pointer to the event whose size should be calculated
  122. * @event_header: Pointer to the initial event containing the digest lengths
  123. * @do_mapping: Whether or not the event needs to be mapped
  124. *
  125. * The TPM2 event log format can contain multiple digests corresponding to
  126. * separate PCR banks, and also contains a variable length of the data that
  127. * was measured. This requires knowledge of how long each digest type is,
  128. * and this information is contained within the first event in the log.
  129. *
  130. * We calculate the length by examining the number of events, and then looking
  131. * at each event in turn to determine how much space is used for events in
  132. * total. Once we've done this we know the offset of the data length field,
  133. * and can calculate the total size of the event.
  134. *
  135. * Return: size of the event on success, 0 on failure
  136. */
  137. static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event,
  138. struct tcg_pcr_event *event_header,
  139. bool do_mapping)
  140. {
  141. struct tcg_efi_specid_event_head *efispecid;
  142. struct tcg_event_field *event_field;
  143. void *mapping = NULL;
  144. int mapping_size;
  145. void *marker;
  146. void *marker_start;
  147. u32 halg_size;
  148. size_t size;
  149. u16 halg;
  150. int i;
  151. int j;
  152. u32 count, event_type;
  153. const u8 zero_digest[sizeof(event_header->digest)] = {0};
  154. marker = event;
  155. marker_start = marker;
  156. marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
  157. + sizeof(event->count);
  158. /* Map the event header */
  159. if (do_mapping) {
  160. mapping_size = marker - marker_start;
  161. mapping = TPM_MEMREMAP((unsigned long)marker_start,
  162. mapping_size);
  163. if (!mapping) {
  164. size = 0;
  165. goto out;
  166. }
  167. } else {
  168. mapping = marker_start;
  169. }
  170. event = (struct tcg_pcr_event2_head *)mapping;
  171. /*
  172. * The loop below will unmap these fields if the log is larger than
  173. * one page, so save them here for reference:
  174. */
  175. count = READ_ONCE(event->count);
  176. event_type = READ_ONCE(event->event_type);
  177. /* Verify that it's the log header */
  178. if (event_header->pcr_idx != 0 ||
  179. event_header->event_type != NO_ACTION ||
  180. memcmp(event_header->digest, zero_digest, sizeof(zero_digest))) {
  181. size = 0;
  182. goto out;
  183. }
  184. efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
  185. /*
  186. * Perform validation of the event in order to identify malformed
  187. * events. This function may be asked to parse arbitrary byte sequences
  188. * immediately following a valid event log. The caller expects this
  189. * function to recognize that the byte sequence is not a valid event
  190. * and to return an event size of 0.
  191. */
  192. if (memcmp(efispecid->signature, TCG_SPECID_SIG,
  193. sizeof(TCG_SPECID_SIG)) ||
  194. !efispecid->num_algs || count != efispecid->num_algs) {
  195. size = 0;
  196. goto out;
  197. }
  198. for (i = 0; i < count; i++) {
  199. halg_size = sizeof(event->digests[i].alg_id);
  200. /* Map the digest's algorithm identifier */
  201. if (do_mapping) {
  202. TPM_MEMUNMAP(mapping, mapping_size);
  203. mapping_size = halg_size;
  204. mapping = TPM_MEMREMAP((unsigned long)marker,
  205. mapping_size);
  206. if (!mapping) {
  207. size = 0;
  208. goto out;
  209. }
  210. } else {
  211. mapping = marker;
  212. }
  213. memcpy(&halg, mapping, halg_size);
  214. marker = marker + halg_size;
  215. for (j = 0; j < efispecid->num_algs; j++) {
  216. if (halg == efispecid->digest_sizes[j].alg_id) {
  217. marker +=
  218. efispecid->digest_sizes[j].digest_size;
  219. break;
  220. }
  221. }
  222. /* Algorithm without known length. Such event is unparseable. */
  223. if (j == efispecid->num_algs) {
  224. size = 0;
  225. goto out;
  226. }
  227. }
  228. /*
  229. * Map the event size - we don't read from the event itself, so
  230. * we don't need to map it
  231. */
  232. if (do_mapping) {
  233. TPM_MEMUNMAP(mapping, mapping_size);
  234. mapping_size += sizeof(event_field->event_size);
  235. mapping = TPM_MEMREMAP((unsigned long)marker,
  236. mapping_size);
  237. if (!mapping) {
  238. size = 0;
  239. goto out;
  240. }
  241. } else {
  242. mapping = marker;
  243. }
  244. event_field = (struct tcg_event_field *)mapping;
  245. marker = marker + sizeof(event_field->event_size)
  246. + event_field->event_size;
  247. size = marker - marker_start;
  248. if (event_type == 0 && event_field->event_size == 0)
  249. size = 0;
  250. out:
  251. if (do_mapping)
  252. TPM_MEMUNMAP(mapping, mapping_size);
  253. return size;
  254. }
  255. #endif