tpm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * TPM handling.
  3. *
  4. * Copyright (C) 2016 CoreOS, Inc
  5. * Copyright (C) 2017 Google, Inc.
  6. * Matthew Garrett <mjg59@google.com>
  7. * Thiebaud Weksteen <tweek@google.com>
  8. *
  9. * This file is part of the Linux kernel, and is made available under the
  10. * terms of the GNU General Public License version 2.
  11. */
  12. #include <linux/efi.h>
  13. #include <linux/tpm_eventlog.h>
  14. #include <asm/efi.h>
  15. #include "efistub.h"
  16. #ifdef CONFIG_RESET_ATTACK_MITIGATION
  17. static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
  18. L"MemoryOverwriteRequestControl";
  19. #define MEMORY_ONLY_RESET_CONTROL_GUID \
  20. EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
  21. #define get_efi_var(name, vendor, ...) \
  22. efi_call_runtime(get_variable, \
  23. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  24. __VA_ARGS__)
  25. #define set_efi_var(name, vendor, ...) \
  26. efi_call_runtime(set_variable, \
  27. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  28. __VA_ARGS__)
  29. /*
  30. * Enable reboot attack mitigation. This requests that the firmware clear the
  31. * RAM on next reboot before proceeding with boot, ensuring that any secrets
  32. * are cleared. If userland has ensured that all secrets have been removed
  33. * from RAM before reboot it can simply reset this variable.
  34. */
  35. void efi_enable_reset_attack_mitigation(efi_system_table_t *sys_table_arg)
  36. {
  37. u8 val = 1;
  38. efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
  39. efi_status_t status;
  40. unsigned long datasize = 0;
  41. status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  42. NULL, &datasize, NULL);
  43. if (status == EFI_NOT_FOUND)
  44. return;
  45. set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  46. EFI_VARIABLE_NON_VOLATILE |
  47. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  48. EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
  49. }
  50. #endif
  51. static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg)
  52. {
  53. efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
  54. efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
  55. efi_status_t status;
  56. efi_physical_addr_t log_location = 0, log_last_entry = 0;
  57. struct linux_efi_tpm_eventlog *log_tbl = NULL;
  58. unsigned long first_entry_addr, last_entry_addr;
  59. size_t log_size, last_entry_size;
  60. efi_bool_t truncated;
  61. void *tcg2_protocol = NULL;
  62. status = efi_call_early(locate_protocol, &tcg2_guid, NULL,
  63. &tcg2_protocol);
  64. if (status != EFI_SUCCESS)
  65. return;
  66. status = efi_call_proto(efi_tcg2_protocol, get_event_log, tcg2_protocol,
  67. EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2,
  68. &log_location, &log_last_entry, &truncated);
  69. if (status != EFI_SUCCESS)
  70. return;
  71. if (!log_location)
  72. return;
  73. first_entry_addr = (unsigned long) log_location;
  74. /*
  75. * We populate the EFI table even if the logs are empty.
  76. */
  77. if (!log_last_entry) {
  78. log_size = 0;
  79. } else {
  80. last_entry_addr = (unsigned long) log_last_entry;
  81. /*
  82. * get_event_log only returns the address of the last entry.
  83. * We need to calculate its size to deduce the full size of
  84. * the logs.
  85. */
  86. last_entry_size = sizeof(struct tcpa_event) +
  87. ((struct tcpa_event *) last_entry_addr)->event_size;
  88. log_size = log_last_entry - log_location + last_entry_size;
  89. }
  90. /* Allocate space for the logs and copy them. */
  91. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  92. sizeof(*log_tbl) + log_size,
  93. (void **) &log_tbl);
  94. if (status != EFI_SUCCESS) {
  95. efi_printk(sys_table_arg,
  96. "Unable to allocate memory for event log\n");
  97. return;
  98. }
  99. memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
  100. log_tbl->size = log_size;
  101. log_tbl->version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  102. memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
  103. status = efi_call_early(install_configuration_table,
  104. &linux_eventlog_guid, log_tbl);
  105. if (status != EFI_SUCCESS)
  106. goto err_free;
  107. return;
  108. err_free:
  109. efi_call_early(free_pool, log_tbl);
  110. }
  111. void efi_retrieve_tpm2_eventlog(efi_system_table_t *sys_table_arg)
  112. {
  113. /* Only try to retrieve the logs in 1.2 format. */
  114. efi_retrieve_tpm2_eventlog_1_2(sys_table_arg);
  115. }