2019-02-02 17:41:15 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-08-25 23:50:15 +08:00
|
|
|
/*
|
|
|
|
* TPM handling.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CoreOS, Inc
|
|
|
|
* Copyright (C) 2017 Google, Inc.
|
|
|
|
* Matthew Garrett <mjg59@google.com>
|
2017-09-20 16:13:39 +08:00
|
|
|
* Thiebaud Weksteen <tweek@google.com>
|
2017-08-25 23:50:15 +08:00
|
|
|
*/
|
|
|
|
#include <linux/efi.h>
|
2017-09-20 16:13:39 +08:00
|
|
|
#include <linux/tpm_eventlog.h>
|
2017-08-25 23:50:15 +08:00
|
|
|
#include <asm/efi.h>
|
|
|
|
|
|
|
|
#include "efistub.h"
|
|
|
|
|
2017-09-20 16:13:39 +08:00
|
|
|
#ifdef CONFIG_RESET_ATTACK_MITIGATION
|
2018-03-12 16:45:00 +08:00
|
|
|
static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
|
|
|
|
L"MemoryOverwriteRequestControl";
|
2017-08-25 23:50:15 +08:00
|
|
|
|
|
|
|
#define MEMORY_ONLY_RESET_CONTROL_GUID \
|
|
|
|
EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable reboot attack mitigation. This requests that the firmware clear the
|
|
|
|
* RAM on next reboot before proceeding with boot, ensuring that any secrets
|
|
|
|
* are cleared. If userland has ensured that all secrets have been removed
|
|
|
|
* from RAM before reboot it can simply reset this variable.
|
|
|
|
*/
|
2019-12-24 23:10:19 +08:00
|
|
|
void efi_enable_reset_attack_mitigation(void)
|
2017-08-25 23:50:15 +08:00
|
|
|
{
|
|
|
|
u8 val = 1;
|
|
|
|
efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
|
|
|
|
efi_status_t status;
|
|
|
|
unsigned long datasize = 0;
|
|
|
|
|
|
|
|
status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
|
|
|
|
NULL, &datasize, NULL);
|
|
|
|
|
|
|
|
if (status == EFI_NOT_FOUND)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
|
|
|
|
EFI_VARIABLE_NON_VOLATILE |
|
|
|
|
EFI_VARIABLE_BOOTSERVICE_ACCESS |
|
|
|
|
EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
|
|
|
|
}
|
2017-09-20 16:13:39 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-12-24 23:10:19 +08:00
|
|
|
void efi_retrieve_tpm2_eventlog(void)
|
2017-09-20 16:13:39 +08:00
|
|
|
{
|
|
|
|
efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
|
|
|
|
efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
|
|
|
|
efi_status_t status;
|
2018-06-22 14:42:22 +08:00
|
|
|
efi_physical_addr_t log_location = 0, log_last_entry = 0;
|
2018-03-13 22:09:21 +08:00
|
|
|
struct linux_efi_tpm_eventlog *log_tbl = NULL;
|
2020-05-12 12:01:13 +08:00
|
|
|
struct efi_tcg2_final_events_table *final_events_table = NULL;
|
2017-09-20 16:13:39 +08:00
|
|
|
unsigned long first_entry_addr, last_entry_addr;
|
|
|
|
size_t log_size, last_entry_size;
|
|
|
|
efi_bool_t truncated;
|
2019-05-21 04:55:01 +08:00
|
|
|
int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
|
2019-12-24 23:10:11 +08:00
|
|
|
efi_tcg2_protocol_t *tcg2_protocol = NULL;
|
2019-06-08 04:51:47 +08:00
|
|
|
int final_events_size = 0;
|
2017-09-20 16:13:39 +08:00
|
|
|
|
efi/libstub: Rename efi_call_early/_runtime macros to be more intuitive
The macros efi_call_early and efi_call_runtime are used to call EFI
boot services and runtime services, respectively. However, the naming
is confusing, given that the early vs runtime distinction may suggest
that these are used for calling the same set of services either early
or late (== at runtime), while in reality, the sets of services they
can be used with are completely disjoint, and efi_call_runtime is also
only usable in 'early' code.
So do a global sweep to replace all occurrences with efi_bs_call or
efi_rt_call, respectively, where BS and RT match the idiom used by
the UEFI spec to refer to boot time or runtime services.
While at it, use 'func' as the macro parameter name for the function
pointers, which is less likely to collide and cause weird build errors.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Borislav Petkov <bp@alien8.de>
Cc: James Morse <james.morse@arm.com>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: https://lkml.kernel.org/r/20191224151025.32482-24-ardb@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-12-24 23:10:23 +08:00
|
|
|
status = efi_bs_call(locate_protocol, &tcg2_guid, NULL,
|
|
|
|
(void **)&tcg2_protocol);
|
2017-09-20 16:13:39 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
2019-12-24 23:10:21 +08:00
|
|
|
status = efi_call_proto(tcg2_protocol, get_event_log, version,
|
|
|
|
&log_location, &log_last_entry, &truncated);
|
2019-05-21 04:55:01 +08:00
|
|
|
|
|
|
|
if (status != EFI_SUCCESS || !log_location) {
|
|
|
|
version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
|
2019-12-24 23:10:21 +08:00
|
|
|
status = efi_call_proto(tcg2_protocol, get_event_log, version,
|
|
|
|
&log_location, &log_last_entry,
|
|
|
|
&truncated);
|
2019-05-21 04:55:01 +08:00
|
|
|
if (status != EFI_SUCCESS || !log_location)
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
2017-09-20 16:13:39 +08:00
|
|
|
|
|
|
|
first_entry_addr = (unsigned long) log_location;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We populate the EFI table even if the logs are empty.
|
|
|
|
*/
|
|
|
|
if (!log_last_entry) {
|
|
|
|
log_size = 0;
|
|
|
|
} else {
|
|
|
|
last_entry_addr = (unsigned long) log_last_entry;
|
|
|
|
/*
|
|
|
|
* get_event_log only returns the address of the last entry.
|
|
|
|
* We need to calculate its size to deduce the full size of
|
|
|
|
* the logs.
|
|
|
|
*/
|
2019-05-21 04:55:01 +08:00
|
|
|
if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
|
|
|
|
/*
|
|
|
|
* The TCG2 log format has variable length entries,
|
|
|
|
* and the information to decode the hash algorithms
|
|
|
|
* back into a size is contained in the first entry -
|
|
|
|
* pass a pointer to the final entry (to calculate its
|
|
|
|
* size) and the first entry (so we know how long each
|
|
|
|
* digest is)
|
|
|
|
*/
|
|
|
|
last_entry_size =
|
|
|
|
__calc_tpm2_event_size((void *)last_entry_addr,
|
|
|
|
(void *)(long)log_location,
|
|
|
|
false);
|
|
|
|
} else {
|
|
|
|
last_entry_size = sizeof(struct tcpa_event) +
|
|
|
|
((struct tcpa_event *) last_entry_addr)->event_size;
|
|
|
|
}
|
2017-09-20 16:13:39 +08:00
|
|
|
log_size = log_last_entry - log_location + last_entry_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space for the logs and copy them. */
|
efi/libstub: Rename efi_call_early/_runtime macros to be more intuitive
The macros efi_call_early and efi_call_runtime are used to call EFI
boot services and runtime services, respectively. However, the naming
is confusing, given that the early vs runtime distinction may suggest
that these are used for calling the same set of services either early
or late (== at runtime), while in reality, the sets of services they
can be used with are completely disjoint, and efi_call_runtime is also
only usable in 'early' code.
So do a global sweep to replace all occurrences with efi_bs_call or
efi_rt_call, respectively, where BS and RT match the idiom used by
the UEFI spec to refer to boot time or runtime services.
While at it, use 'func' as the macro parameter name for the function
pointers, which is less likely to collide and cause weird build errors.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Borislav Petkov <bp@alien8.de>
Cc: James Morse <james.morse@arm.com>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: https://lkml.kernel.org/r/20191224151025.32482-24-ardb@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-12-24 23:10:23 +08:00
|
|
|
status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
|
|
|
|
sizeof(*log_tbl) + log_size, (void **)&log_tbl);
|
2017-09-20 16:13:39 +08:00
|
|
|
|
|
|
|
if (status != EFI_SUCCESS) {
|
2020-05-01 02:28:38 +08:00
|
|
|
efi_err("Unable to allocate memory for event log\n");
|
2017-09-20 16:13:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-08 04:51:47 +08:00
|
|
|
/*
|
|
|
|
* Figure out whether any events have already been logged to the
|
|
|
|
* final events structure, and if so how much space they take up
|
|
|
|
*/
|
2020-05-12 12:01:13 +08:00
|
|
|
if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
|
|
|
|
final_events_table = get_efi_config_table(LINUX_EFI_TPM_FINAL_LOG_GUID);
|
2019-06-08 04:51:47 +08:00
|
|
|
if (final_events_table && final_events_table->nr_events) {
|
|
|
|
struct tcg_pcr_event2_head *header;
|
|
|
|
int offset;
|
|
|
|
void *data;
|
|
|
|
int event_size;
|
|
|
|
int i = final_events_table->nr_events;
|
|
|
|
|
|
|
|
data = (void *)final_events_table;
|
|
|
|
offset = sizeof(final_events_table->version) +
|
|
|
|
sizeof(final_events_table->nr_events);
|
|
|
|
|
|
|
|
while (i > 0) {
|
|
|
|
header = data + offset + final_events_size;
|
|
|
|
event_size = __calc_tpm2_event_size(header,
|
|
|
|
(void *)(long)log_location,
|
|
|
|
false);
|
|
|
|
final_events_size += event_size;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 16:13:39 +08:00
|
|
|
memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
|
|
|
|
log_tbl->size = log_size;
|
2019-06-08 04:51:47 +08:00
|
|
|
log_tbl->final_events_preboot_size = final_events_size;
|
2019-05-21 04:55:01 +08:00
|
|
|
log_tbl->version = version;
|
2017-09-20 16:13:39 +08:00
|
|
|
memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
|
|
|
|
|
efi/libstub: Rename efi_call_early/_runtime macros to be more intuitive
The macros efi_call_early and efi_call_runtime are used to call EFI
boot services and runtime services, respectively. However, the naming
is confusing, given that the early vs runtime distinction may suggest
that these are used for calling the same set of services either early
or late (== at runtime), while in reality, the sets of services they
can be used with are completely disjoint, and efi_call_runtime is also
only usable in 'early' code.
So do a global sweep to replace all occurrences with efi_bs_call or
efi_rt_call, respectively, where BS and RT match the idiom used by
the UEFI spec to refer to boot time or runtime services.
While at it, use 'func' as the macro parameter name for the function
pointers, which is less likely to collide and cause weird build errors.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Borislav Petkov <bp@alien8.de>
Cc: James Morse <james.morse@arm.com>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: https://lkml.kernel.org/r/20191224151025.32482-24-ardb@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-12-24 23:10:23 +08:00
|
|
|
status = efi_bs_call(install_configuration_table,
|
|
|
|
&linux_eventlog_guid, log_tbl);
|
2017-09-20 16:13:39 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto err_free;
|
|
|
|
return;
|
|
|
|
|
|
|
|
err_free:
|
efi/libstub: Rename efi_call_early/_runtime macros to be more intuitive
The macros efi_call_early and efi_call_runtime are used to call EFI
boot services and runtime services, respectively. However, the naming
is confusing, given that the early vs runtime distinction may suggest
that these are used for calling the same set of services either early
or late (== at runtime), while in reality, the sets of services they
can be used with are completely disjoint, and efi_call_runtime is also
only usable in 'early' code.
So do a global sweep to replace all occurrences with efi_bs_call or
efi_rt_call, respectively, where BS and RT match the idiom used by
the UEFI spec to refer to boot time or runtime services.
While at it, use 'func' as the macro parameter name for the function
pointers, which is less likely to collide and cause weird build errors.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Borislav Petkov <bp@alien8.de>
Cc: James Morse <james.morse@arm.com>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: https://lkml.kernel.org/r/20191224151025.32482-24-ardb@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-12-24 23:10:23 +08:00
|
|
|
efi_bs_call(free_pool, log_tbl);
|
2017-09-20 16:13:39 +08:00
|
|
|
}
|