18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * TPM handling. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2016 CoreOS, Inc 68c2ecf20Sopenharmony_ci * Copyright (C) 2017 Google, Inc. 78c2ecf20Sopenharmony_ci * Matthew Garrett <mjg59@google.com> 88c2ecf20Sopenharmony_ci * Thiebaud Weksteen <tweek@google.com> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci#include <linux/efi.h> 118c2ecf20Sopenharmony_ci#include <linux/tpm_eventlog.h> 128c2ecf20Sopenharmony_ci#include <asm/efi.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "efistub.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#ifdef CONFIG_RESET_ATTACK_MITIGATION 178c2ecf20Sopenharmony_cistatic const efi_char16_t efi_MemoryOverWriteRequest_name[] = 188c2ecf20Sopenharmony_ci L"MemoryOverwriteRequestControl"; 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define MEMORY_ONLY_RESET_CONTROL_GUID \ 218c2ecf20Sopenharmony_ci EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * Enable reboot attack mitigation. This requests that the firmware clear the 258c2ecf20Sopenharmony_ci * RAM on next reboot before proceeding with boot, ensuring that any secrets 268c2ecf20Sopenharmony_ci * are cleared. If userland has ensured that all secrets have been removed 278c2ecf20Sopenharmony_ci * from RAM before reboot it can simply reset this variable. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_civoid efi_enable_reset_attack_mitigation(void) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci u8 val = 1; 328c2ecf20Sopenharmony_ci efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID; 338c2ecf20Sopenharmony_ci efi_status_t status; 348c2ecf20Sopenharmony_ci unsigned long datasize = 0; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid, 378c2ecf20Sopenharmony_ci NULL, &datasize, NULL); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci if (status == EFI_NOT_FOUND) 408c2ecf20Sopenharmony_ci return; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid, 438c2ecf20Sopenharmony_ci EFI_VARIABLE_NON_VOLATILE | 448c2ecf20Sopenharmony_ci EFI_VARIABLE_BOOTSERVICE_ACCESS | 458c2ecf20Sopenharmony_ci EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val); 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#endif 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_civoid efi_retrieve_tpm2_eventlog(void) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID; 538c2ecf20Sopenharmony_ci efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID; 548c2ecf20Sopenharmony_ci efi_status_t status; 558c2ecf20Sopenharmony_ci efi_physical_addr_t log_location = 0, log_last_entry = 0; 568c2ecf20Sopenharmony_ci struct linux_efi_tpm_eventlog *log_tbl = NULL; 578c2ecf20Sopenharmony_ci struct efi_tcg2_final_events_table *final_events_table = NULL; 588c2ecf20Sopenharmony_ci unsigned long first_entry_addr, last_entry_addr; 598c2ecf20Sopenharmony_ci size_t log_size, last_entry_size; 608c2ecf20Sopenharmony_ci efi_bool_t truncated; 618c2ecf20Sopenharmony_ci int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2; 628c2ecf20Sopenharmony_ci efi_tcg2_protocol_t *tcg2_protocol = NULL; 638c2ecf20Sopenharmony_ci int final_events_size = 0; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci status = efi_bs_call(locate_protocol, &tcg2_guid, NULL, 668c2ecf20Sopenharmony_ci (void **)&tcg2_protocol); 678c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 688c2ecf20Sopenharmony_ci return; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci status = efi_call_proto(tcg2_protocol, get_event_log, version, 718c2ecf20Sopenharmony_ci &log_location, &log_last_entry, &truncated); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS || !log_location) { 748c2ecf20Sopenharmony_ci version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; 758c2ecf20Sopenharmony_ci status = efi_call_proto(tcg2_protocol, get_event_log, version, 768c2ecf20Sopenharmony_ci &log_location, &log_last_entry, 778c2ecf20Sopenharmony_ci &truncated); 788c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS || !log_location) 798c2ecf20Sopenharmony_ci return; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci first_entry_addr = (unsigned long) log_location; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci /* 868c2ecf20Sopenharmony_ci * We populate the EFI table even if the logs are empty. 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_ci if (!log_last_entry) { 898c2ecf20Sopenharmony_ci log_size = 0; 908c2ecf20Sopenharmony_ci } else { 918c2ecf20Sopenharmony_ci last_entry_addr = (unsigned long) log_last_entry; 928c2ecf20Sopenharmony_ci /* 938c2ecf20Sopenharmony_ci * get_event_log only returns the address of the last entry. 948c2ecf20Sopenharmony_ci * We need to calculate its size to deduce the full size of 958c2ecf20Sopenharmony_ci * the logs. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { 988c2ecf20Sopenharmony_ci /* 998c2ecf20Sopenharmony_ci * The TCG2 log format has variable length entries, 1008c2ecf20Sopenharmony_ci * and the information to decode the hash algorithms 1018c2ecf20Sopenharmony_ci * back into a size is contained in the first entry - 1028c2ecf20Sopenharmony_ci * pass a pointer to the final entry (to calculate its 1038c2ecf20Sopenharmony_ci * size) and the first entry (so we know how long each 1048c2ecf20Sopenharmony_ci * digest is) 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_ci last_entry_size = 1078c2ecf20Sopenharmony_ci __calc_tpm2_event_size((void *)last_entry_addr, 1088c2ecf20Sopenharmony_ci (void *)(long)log_location, 1098c2ecf20Sopenharmony_ci false); 1108c2ecf20Sopenharmony_ci } else { 1118c2ecf20Sopenharmony_ci last_entry_size = sizeof(struct tcpa_event) + 1128c2ecf20Sopenharmony_ci ((struct tcpa_event *) last_entry_addr)->event_size; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci log_size = log_last_entry - log_location + last_entry_size; 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* Allocate space for the logs and copy them. */ 1188c2ecf20Sopenharmony_ci status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, 1198c2ecf20Sopenharmony_ci sizeof(*log_tbl) + log_size, (void **)&log_tbl); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) { 1228c2ecf20Sopenharmony_ci efi_err("Unable to allocate memory for event log\n"); 1238c2ecf20Sopenharmony_ci return; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* 1278c2ecf20Sopenharmony_ci * Figure out whether any events have already been logged to the 1288c2ecf20Sopenharmony_ci * final events structure, and if so how much space they take up 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) 1318c2ecf20Sopenharmony_ci final_events_table = get_efi_config_table(LINUX_EFI_TPM_FINAL_LOG_GUID); 1328c2ecf20Sopenharmony_ci if (final_events_table && final_events_table->nr_events) { 1338c2ecf20Sopenharmony_ci struct tcg_pcr_event2_head *header; 1348c2ecf20Sopenharmony_ci int offset; 1358c2ecf20Sopenharmony_ci void *data; 1368c2ecf20Sopenharmony_ci int event_size; 1378c2ecf20Sopenharmony_ci int i = final_events_table->nr_events; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci data = (void *)final_events_table; 1408c2ecf20Sopenharmony_ci offset = sizeof(final_events_table->version) + 1418c2ecf20Sopenharmony_ci sizeof(final_events_table->nr_events); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci while (i > 0) { 1448c2ecf20Sopenharmony_ci header = data + offset + final_events_size; 1458c2ecf20Sopenharmony_ci event_size = __calc_tpm2_event_size(header, 1468c2ecf20Sopenharmony_ci (void *)(long)log_location, 1478c2ecf20Sopenharmony_ci false); 1488c2ecf20Sopenharmony_ci final_events_size += event_size; 1498c2ecf20Sopenharmony_ci i--; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci memset(log_tbl, 0, sizeof(*log_tbl) + log_size); 1548c2ecf20Sopenharmony_ci log_tbl->size = log_size; 1558c2ecf20Sopenharmony_ci log_tbl->final_events_preboot_size = final_events_size; 1568c2ecf20Sopenharmony_ci log_tbl->version = version; 1578c2ecf20Sopenharmony_ci memcpy(log_tbl->log, (void *) first_entry_addr, log_size); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci status = efi_bs_call(install_configuration_table, 1608c2ecf20Sopenharmony_ci &linux_eventlog_guid, log_tbl); 1618c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 1628c2ecf20Sopenharmony_ci goto err_free; 1638c2ecf20Sopenharmony_ci return; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cierr_free: 1668c2ecf20Sopenharmony_ci efi_bs_call(free_pool, log_tbl); 1678c2ecf20Sopenharmony_ci} 168