14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2ccc829baSMatthew Garrett /*
3ccc829baSMatthew Garrett * TPM handling.
4ccc829baSMatthew Garrett *
5ccc829baSMatthew Garrett * Copyright (C) 2016 CoreOS, Inc
6ccc829baSMatthew Garrett * Copyright (C) 2017 Google, Inc.
7ccc829baSMatthew Garrett * Matthew Garrett <mjg59@google.com>
833b6d034SThiebaud Weksteen * Thiebaud Weksteen <tweek@google.com>
9ccc829baSMatthew Garrett */
10ccc829baSMatthew Garrett #include <linux/efi.h>
1133b6d034SThiebaud Weksteen #include <linux/tpm_eventlog.h>
12ccc829baSMatthew Garrett #include <asm/efi.h>
13ccc829baSMatthew Garrett
14ccc829baSMatthew Garrett #include "efistub.h"
15ccc829baSMatthew Garrett
1633b6d034SThiebaud Weksteen #ifdef CONFIG_RESET_ATTACK_MITIGATION
1736b64976SArd Biesheuvel static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
1836b64976SArd Biesheuvel L"MemoryOverwriteRequestControl";
19ccc829baSMatthew Garrett
20ccc829baSMatthew Garrett #define MEMORY_ONLY_RESET_CONTROL_GUID \
21ccc829baSMatthew Garrett EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
22ccc829baSMatthew Garrett
23ccc829baSMatthew Garrett /*
24ccc829baSMatthew Garrett * Enable reboot attack mitigation. This requests that the firmware clear the
25ccc829baSMatthew Garrett * RAM on next reboot before proceeding with boot, ensuring that any secrets
26ccc829baSMatthew Garrett * are cleared. If userland has ensured that all secrets have been removed
27ccc829baSMatthew Garrett * from RAM before reboot it can simply reset this variable.
28ccc829baSMatthew Garrett */
efi_enable_reset_attack_mitigation(void)29cd33a5c1SArd Biesheuvel void efi_enable_reset_attack_mitigation(void)
30ccc829baSMatthew Garrett {
31ccc829baSMatthew Garrett u8 val = 1;
32ccc829baSMatthew Garrett efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
33ccc829baSMatthew Garrett efi_status_t status;
34ccc829baSMatthew Garrett unsigned long datasize = 0;
35ccc829baSMatthew Garrett
36ccc829baSMatthew Garrett status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
37ccc829baSMatthew Garrett NULL, &datasize, NULL);
38ccc829baSMatthew Garrett
39ccc829baSMatthew Garrett if (status == EFI_NOT_FOUND)
40ccc829baSMatthew Garrett return;
41ccc829baSMatthew Garrett
42ccc829baSMatthew Garrett set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
43ccc829baSMatthew Garrett EFI_VARIABLE_NON_VOLATILE |
44ccc829baSMatthew Garrett EFI_VARIABLE_BOOTSERVICE_ACCESS |
45ccc829baSMatthew Garrett EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
46ccc829baSMatthew Garrett }
4733b6d034SThiebaud Weksteen
4833b6d034SThiebaud Weksteen #endif
4933b6d034SThiebaud Weksteen
efi_retrieve_tpm2_eventlog(void)50cd33a5c1SArd Biesheuvel void efi_retrieve_tpm2_eventlog(void)
5133b6d034SThiebaud Weksteen {
5233b6d034SThiebaud Weksteen efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
5333b6d034SThiebaud Weksteen efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
5433b6d034SThiebaud Weksteen efi_status_t status;
5552e1cf2dSHans de Goede efi_physical_addr_t log_location = 0, log_last_entry = 0;
5679832f0bSArd Biesheuvel struct linux_efi_tpm_eventlog *log_tbl = NULL;
57b4f1874cSLoïc Yhuel struct efi_tcg2_final_events_table *final_events_table = NULL;
5833b6d034SThiebaud Weksteen unsigned long first_entry_addr, last_entry_addr;
5933b6d034SThiebaud Weksteen size_t log_size, last_entry_size;
6033b6d034SThiebaud Weksteen efi_bool_t truncated;
616b032619SMatthew Garrett int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
62960a8d01SArd Biesheuvel efi_tcg2_protocol_t *tcg2_protocol = NULL;
63166a2809SMatthew Garrett int final_events_size = 0;
6433b6d034SThiebaud Weksteen
65966291f6SArd Biesheuvel status = efi_bs_call(locate_protocol, &tcg2_guid, NULL,
66960a8d01SArd Biesheuvel (void **)&tcg2_protocol);
6733b6d034SThiebaud Weksteen if (status != EFI_SUCCESS)
6833b6d034SThiebaud Weksteen return;
6933b6d034SThiebaud Weksteen
7047c0fd39SArd Biesheuvel status = efi_call_proto(tcg2_protocol, get_event_log, version,
7147c0fd39SArd Biesheuvel &log_location, &log_last_entry, &truncated);
726b032619SMatthew Garrett
736b032619SMatthew Garrett if (status != EFI_SUCCESS || !log_location) {
746b032619SMatthew Garrett version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
7547c0fd39SArd Biesheuvel status = efi_call_proto(tcg2_protocol, get_event_log, version,
7647c0fd39SArd Biesheuvel &log_location, &log_last_entry,
7747c0fd39SArd Biesheuvel &truncated);
786b032619SMatthew Garrett if (status != EFI_SUCCESS || !log_location)
7933b6d034SThiebaud Weksteen return;
8033b6d034SThiebaud Weksteen
816b032619SMatthew Garrett }
826b032619SMatthew Garrett
8333b6d034SThiebaud Weksteen first_entry_addr = (unsigned long) log_location;
8433b6d034SThiebaud Weksteen
8533b6d034SThiebaud Weksteen /*
8633b6d034SThiebaud Weksteen * We populate the EFI table even if the logs are empty.
8733b6d034SThiebaud Weksteen */
8833b6d034SThiebaud Weksteen if (!log_last_entry) {
8933b6d034SThiebaud Weksteen log_size = 0;
9033b6d034SThiebaud Weksteen } else {
9133b6d034SThiebaud Weksteen last_entry_addr = (unsigned long) log_last_entry;
9233b6d034SThiebaud Weksteen /*
9333b6d034SThiebaud Weksteen * get_event_log only returns the address of the last entry.
9433b6d034SThiebaud Weksteen * We need to calculate its size to deduce the full size of
9533b6d034SThiebaud Weksteen * the logs.
9633b6d034SThiebaud Weksteen */
976b032619SMatthew Garrett if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
986b032619SMatthew Garrett /*
996b032619SMatthew Garrett * The TCG2 log format has variable length entries,
1006b032619SMatthew Garrett * and the information to decode the hash algorithms
1016b032619SMatthew Garrett * back into a size is contained in the first entry -
1026b032619SMatthew Garrett * pass a pointer to the final entry (to calculate its
1036b032619SMatthew Garrett * size) and the first entry (so we know how long each
1046b032619SMatthew Garrett * digest is)
1056b032619SMatthew Garrett */
1066b032619SMatthew Garrett last_entry_size =
1076b032619SMatthew Garrett __calc_tpm2_event_size((void *)last_entry_addr,
1086b032619SMatthew Garrett (void *)(long)log_location,
1096b032619SMatthew Garrett false);
1106b032619SMatthew Garrett } else {
11133b6d034SThiebaud Weksteen last_entry_size = sizeof(struct tcpa_event) +
11233b6d034SThiebaud Weksteen ((struct tcpa_event *) last_entry_addr)->event_size;
1136b032619SMatthew Garrett }
11433b6d034SThiebaud Weksteen log_size = log_last_entry - log_location + last_entry_size;
11533b6d034SThiebaud Weksteen }
11633b6d034SThiebaud Weksteen
11733b6d034SThiebaud Weksteen /* Allocate space for the logs and copy them. */
118966291f6SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
119966291f6SArd Biesheuvel sizeof(*log_tbl) + log_size, (void **)&log_tbl);
12033b6d034SThiebaud Weksteen
12133b6d034SThiebaud Weksteen if (status != EFI_SUCCESS) {
1228173ec79SArd Biesheuvel efi_err("Unable to allocate memory for event log\n");
12333b6d034SThiebaud Weksteen return;
12433b6d034SThiebaud Weksteen }
12533b6d034SThiebaud Weksteen
126166a2809SMatthew Garrett /*
127166a2809SMatthew Garrett * Figure out whether any events have already been logged to the
128166a2809SMatthew Garrett * final events structure, and if so how much space they take up
129166a2809SMatthew Garrett */
130b4f1874cSLoïc Yhuel if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
131cd33a5c1SArd Biesheuvel final_events_table = get_efi_config_table(LINUX_EFI_TPM_FINAL_LOG_GUID);
132166a2809SMatthew Garrett if (final_events_table && final_events_table->nr_events) {
133166a2809SMatthew Garrett struct tcg_pcr_event2_head *header;
134166a2809SMatthew Garrett int offset;
135166a2809SMatthew Garrett void *data;
136166a2809SMatthew Garrett int event_size;
137166a2809SMatthew Garrett int i = final_events_table->nr_events;
138166a2809SMatthew Garrett
139166a2809SMatthew Garrett data = (void *)final_events_table;
140166a2809SMatthew Garrett offset = sizeof(final_events_table->version) +
141166a2809SMatthew Garrett sizeof(final_events_table->nr_events);
142166a2809SMatthew Garrett
143166a2809SMatthew Garrett while (i > 0) {
144166a2809SMatthew Garrett header = data + offset + final_events_size;
145166a2809SMatthew Garrett event_size = __calc_tpm2_event_size(header,
146166a2809SMatthew Garrett (void *)(long)log_location,
147166a2809SMatthew Garrett false);
148166a2809SMatthew Garrett final_events_size += event_size;
149166a2809SMatthew Garrett i--;
150166a2809SMatthew Garrett }
151166a2809SMatthew Garrett }
152166a2809SMatthew Garrett
15333b6d034SThiebaud Weksteen memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
15433b6d034SThiebaud Weksteen log_tbl->size = log_size;
155166a2809SMatthew Garrett log_tbl->final_events_preboot_size = final_events_size;
1566b032619SMatthew Garrett log_tbl->version = version;
15733b6d034SThiebaud Weksteen memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
15833b6d034SThiebaud Weksteen
159966291f6SArd Biesheuvel status = efi_bs_call(install_configuration_table,
16033b6d034SThiebaud Weksteen &linux_eventlog_guid, log_tbl);
16133b6d034SThiebaud Weksteen if (status != EFI_SUCCESS)
16233b6d034SThiebaud Weksteen goto err_free;
16333b6d034SThiebaud Weksteen return;
16433b6d034SThiebaud Weksteen
16533b6d034SThiebaud Weksteen err_free:
166966291f6SArd Biesheuvel efi_bs_call(free_pool, log_tbl);
16733b6d034SThiebaud Weksteen }
168