tpm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TPM handling.
  4. *
  5. * Copyright (C) 2016 CoreOS, Inc
  6. * Copyright (C) 2017 Google, Inc.
  7. * Matthew Garrett <[email protected]>
  8. * Thiebaud Weksteen <[email protected]>
  9. */
  10. #include <linux/efi.h>
  11. #include <linux/tpm_eventlog.h>
  12. #include <asm/efi.h>
  13. #include "efistub.h"
  14. #ifdef CONFIG_RESET_ATTACK_MITIGATION
  15. static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
  16. L"MemoryOverwriteRequestControl";
  17. #define MEMORY_ONLY_RESET_CONTROL_GUID \
  18. EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
  19. /*
  20. * Enable reboot attack mitigation. This requests that the firmware clear the
  21. * RAM on next reboot before proceeding with boot, ensuring that any secrets
  22. * are cleared. If userland has ensured that all secrets have been removed
  23. * from RAM before reboot it can simply reset this variable.
  24. */
  25. void efi_enable_reset_attack_mitigation(void)
  26. {
  27. u8 val = 1;
  28. efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
  29. efi_status_t status;
  30. unsigned long datasize = 0;
  31. status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  32. NULL, &datasize, NULL);
  33. if (status == EFI_NOT_FOUND)
  34. return;
  35. set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  36. EFI_VARIABLE_NON_VOLATILE |
  37. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  38. EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
  39. }
  40. #endif
  41. void efi_retrieve_tpm2_eventlog(void)
  42. {
  43. efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
  44. efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
  45. efi_status_t status;
  46. efi_physical_addr_t log_location = 0, log_last_entry = 0;
  47. struct linux_efi_tpm_eventlog *log_tbl = NULL;
  48. struct efi_tcg2_final_events_table *final_events_table = NULL;
  49. unsigned long first_entry_addr, last_entry_addr;
  50. size_t log_size, last_entry_size;
  51. efi_bool_t truncated;
  52. int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
  53. efi_tcg2_protocol_t *tcg2_protocol = NULL;
  54. int final_events_size = 0;
  55. status = efi_bs_call(locate_protocol, &tcg2_guid, NULL,
  56. (void **)&tcg2_protocol);
  57. if (status != EFI_SUCCESS)
  58. return;
  59. status = efi_call_proto(tcg2_protocol, get_event_log, version,
  60. &log_location, &log_last_entry, &truncated);
  61. if (status != EFI_SUCCESS || !log_location) {
  62. version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  63. status = efi_call_proto(tcg2_protocol, get_event_log, version,
  64. &log_location, &log_last_entry,
  65. &truncated);
  66. if (status != EFI_SUCCESS || !log_location)
  67. return;
  68. }
  69. first_entry_addr = (unsigned long) log_location;
  70. /*
  71. * We populate the EFI table even if the logs are empty.
  72. */
  73. if (!log_last_entry) {
  74. log_size = 0;
  75. } else {
  76. last_entry_addr = (unsigned long) log_last_entry;
  77. /*
  78. * get_event_log only returns the address of the last entry.
  79. * We need to calculate its size to deduce the full size of
  80. * the logs.
  81. */
  82. if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
  83. /*
  84. * The TCG2 log format has variable length entries,
  85. * and the information to decode the hash algorithms
  86. * back into a size is contained in the first entry -
  87. * pass a pointer to the final entry (to calculate its
  88. * size) and the first entry (so we know how long each
  89. * digest is)
  90. */
  91. last_entry_size =
  92. __calc_tpm2_event_size((void *)last_entry_addr,
  93. (void *)(long)log_location,
  94. false);
  95. } else {
  96. last_entry_size = sizeof(struct tcpa_event) +
  97. ((struct tcpa_event *) last_entry_addr)->event_size;
  98. }
  99. log_size = log_last_entry - log_location + last_entry_size;
  100. }
  101. /* Allocate space for the logs and copy them. */
  102. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
  103. sizeof(*log_tbl) + log_size, (void **)&log_tbl);
  104. if (status != EFI_SUCCESS) {
  105. efi_err("Unable to allocate memory for event log\n");
  106. return;
  107. }
  108. /*
  109. * Figure out whether any events have already been logged to the
  110. * final events structure, and if so how much space they take up
  111. */
  112. if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
  113. final_events_table = get_efi_config_table(LINUX_EFI_TPM_FINAL_LOG_GUID);
  114. if (final_events_table && final_events_table->nr_events) {
  115. struct tcg_pcr_event2_head *header;
  116. int offset;
  117. void *data;
  118. int event_size;
  119. int i = final_events_table->nr_events;
  120. data = (void *)final_events_table;
  121. offset = sizeof(final_events_table->version) +
  122. sizeof(final_events_table->nr_events);
  123. while (i > 0) {
  124. header = data + offset + final_events_size;
  125. event_size = __calc_tpm2_event_size(header,
  126. (void *)(long)log_location,
  127. false);
  128. final_events_size += event_size;
  129. i--;
  130. }
  131. }
  132. memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
  133. log_tbl->size = log_size;
  134. log_tbl->final_events_preboot_size = final_events_size;
  135. log_tbl->version = version;
  136. memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
  137. status = efi_bs_call(install_configuration_table,
  138. &linux_eventlog_guid, log_tbl);
  139. if (status != EFI_SUCCESS)
  140. goto err_free;
  141. return;
  142. err_free:
  143. efi_bs_call(free_pool, log_tbl);
  144. }