tpm.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'M', 'e', 'm', 'o', 'r', 'y', 'O', 'v', 'e', 'r', 'w', 'r', 'i', 't',
  19. 'e', 'R', 'e', 'q', 'u', 'e', 's', 't', 'C', 'o', 'n', 't', 'r', 'o',
  20. 'l', 0
  21. };
  22. #define MEMORY_ONLY_RESET_CONTROL_GUID \
  23. EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
  24. #define get_efi_var(name, vendor, ...) \
  25. efi_call_runtime(get_variable, \
  26. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  27. __VA_ARGS__)
  28. #define set_efi_var(name, vendor, ...) \
  29. efi_call_runtime(set_variable, \
  30. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  31. __VA_ARGS__)
  32. /*
  33. * Enable reboot attack mitigation. This requests that the firmware clear the
  34. * RAM on next reboot before proceeding with boot, ensuring that any secrets
  35. * are cleared. If userland has ensured that all secrets have been removed
  36. * from RAM before reboot it can simply reset this variable.
  37. */
  38. void efi_enable_reset_attack_mitigation(efi_system_table_t *sys_table_arg)
  39. {
  40. u8 val = 1;
  41. efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
  42. efi_status_t status;
  43. unsigned long datasize = 0;
  44. status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  45. NULL, &datasize, NULL);
  46. if (status == EFI_NOT_FOUND)
  47. return;
  48. set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
  49. EFI_VARIABLE_NON_VOLATILE |
  50. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  51. EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
  52. }
  53. #endif
  54. void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg)
  55. {
  56. efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
  57. efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
  58. efi_status_t status;
  59. efi_physical_addr_t log_location, log_last_entry;
  60. struct linux_efi_tpm_eventlog *log_tbl;
  61. unsigned long first_entry_addr, last_entry_addr;
  62. size_t log_size, last_entry_size;
  63. efi_bool_t truncated;
  64. void *tcg2_protocol;
  65. status = efi_call_early(locate_protocol, &tcg2_guid, NULL,
  66. &tcg2_protocol);
  67. if (status != EFI_SUCCESS)
  68. return;
  69. status = efi_call_proto(efi_tcg2_protocol, get_event_log, tcg2_protocol,
  70. EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2,
  71. &log_location, &log_last_entry, &truncated);
  72. if (status != EFI_SUCCESS)
  73. return;
  74. if (!log_location)
  75. return;
  76. first_entry_addr = (unsigned long) log_location;
  77. /*
  78. * We populate the EFI table even if the logs are empty.
  79. */
  80. if (!log_last_entry) {
  81. log_size = 0;
  82. } else {
  83. last_entry_addr = (unsigned long) log_last_entry;
  84. /*
  85. * get_event_log only returns the address of the last entry.
  86. * We need to calculate its size to deduce the full size of
  87. * the logs.
  88. */
  89. last_entry_size = sizeof(struct tcpa_event) +
  90. ((struct tcpa_event *) last_entry_addr)->event_size;
  91. log_size = log_last_entry - log_location + last_entry_size;
  92. }
  93. /* Allocate space for the logs and copy them. */
  94. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  95. sizeof(*log_tbl) + log_size,
  96. (void **) &log_tbl);
  97. if (status != EFI_SUCCESS) {
  98. efi_printk(sys_table_arg,
  99. "Unable to allocate memory for event log\n");
  100. return;
  101. }
  102. memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
  103. log_tbl->size = log_size;
  104. log_tbl->version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  105. memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
  106. status = efi_call_early(install_configuration_table,
  107. &linux_eventlog_guid, log_tbl);
  108. if (status != EFI_SUCCESS)
  109. goto err_free;
  110. return;
  111. err_free:
  112. efi_call_early(free_pool, log_tbl);
  113. }
  114. void efi_retrieve_tpm2_eventlog(efi_system_table_t *sys_table_arg)
  115. {
  116. /* Only try to retrieve the logs in 1.2 format. */
  117. efi_retrieve_tpm2_eventlog_1_2(sys_table_arg);
  118. }