| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * TPM handling. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * Copyright (C) 2017 Google, Inc. |
|---|
| 6 | 7 | * Matthew Garrett <mjg59@google.com> |
|---|
| 7 | 8 | * 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 | 9 | */ |
|---|
| 12 | 10 | #include <linux/efi.h> |
|---|
| 13 | 11 | #include <linux/tpm_eventlog.h> |
|---|
| .. | .. |
|---|
| 22 | 20 | #define MEMORY_ONLY_RESET_CONTROL_GUID \ |
|---|
| 23 | 21 | EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29) |
|---|
| 24 | 22 | |
|---|
| 25 | | -#define get_efi_var(name, vendor, ...) \ |
|---|
| 26 | | - efi_call_runtime(get_variable, \ |
|---|
| 27 | | - (efi_char16_t *)(name), (efi_guid_t *)(vendor), \ |
|---|
| 28 | | - __VA_ARGS__) |
|---|
| 29 | | - |
|---|
| 30 | | -#define set_efi_var(name, vendor, ...) \ |
|---|
| 31 | | - efi_call_runtime(set_variable, \ |
|---|
| 32 | | - (efi_char16_t *)(name), (efi_guid_t *)(vendor), \ |
|---|
| 33 | | - __VA_ARGS__) |
|---|
| 34 | | - |
|---|
| 35 | 23 | /* |
|---|
| 36 | 24 | * Enable reboot attack mitigation. This requests that the firmware clear the |
|---|
| 37 | 25 | * RAM on next reboot before proceeding with boot, ensuring that any secrets |
|---|
| 38 | 26 | * are cleared. If userland has ensured that all secrets have been removed |
|---|
| 39 | 27 | * from RAM before reboot it can simply reset this variable. |
|---|
| 40 | 28 | */ |
|---|
| 41 | | -void efi_enable_reset_attack_mitigation(efi_system_table_t *sys_table_arg) |
|---|
| 29 | +void efi_enable_reset_attack_mitigation(void) |
|---|
| 42 | 30 | { |
|---|
| 43 | 31 | u8 val = 1; |
|---|
| 44 | 32 | efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID; |
|---|
| .. | .. |
|---|
| 59 | 47 | |
|---|
| 60 | 48 | #endif |
|---|
| 61 | 49 | |
|---|
| 62 | | -static void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg) |
|---|
| 50 | +void efi_retrieve_tpm2_eventlog(void) |
|---|
| 63 | 51 | { |
|---|
| 64 | 52 | efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID; |
|---|
| 65 | 53 | efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID; |
|---|
| 66 | 54 | efi_status_t status; |
|---|
| 67 | 55 | efi_physical_addr_t log_location = 0, log_last_entry = 0; |
|---|
| 68 | 56 | struct linux_efi_tpm_eventlog *log_tbl = NULL; |
|---|
| 57 | + struct efi_tcg2_final_events_table *final_events_table = NULL; |
|---|
| 69 | 58 | unsigned long first_entry_addr, last_entry_addr; |
|---|
| 70 | 59 | size_t log_size, last_entry_size; |
|---|
| 71 | 60 | efi_bool_t truncated; |
|---|
| 72 | | - void *tcg2_protocol = NULL; |
|---|
| 61 | + int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2; |
|---|
| 62 | + efi_tcg2_protocol_t *tcg2_protocol = NULL; |
|---|
| 63 | + int final_events_size = 0; |
|---|
| 73 | 64 | |
|---|
| 74 | | - status = efi_call_early(locate_protocol, &tcg2_guid, NULL, |
|---|
| 75 | | - &tcg2_protocol); |
|---|
| 65 | + status = efi_bs_call(locate_protocol, &tcg2_guid, NULL, |
|---|
| 66 | + (void **)&tcg2_protocol); |
|---|
| 76 | 67 | if (status != EFI_SUCCESS) |
|---|
| 77 | 68 | return; |
|---|
| 78 | 69 | |
|---|
| 79 | | - status = efi_call_proto(efi_tcg2_protocol, get_event_log, tcg2_protocol, |
|---|
| 80 | | - EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2, |
|---|
| 70 | + status = efi_call_proto(tcg2_protocol, get_event_log, version, |
|---|
| 81 | 71 | &log_location, &log_last_entry, &truncated); |
|---|
| 82 | | - if (status != EFI_SUCCESS) |
|---|
| 83 | | - return; |
|---|
| 84 | 72 | |
|---|
| 85 | | - if (!log_location) |
|---|
| 86 | | - return; |
|---|
| 73 | + if (status != EFI_SUCCESS || !log_location) { |
|---|
| 74 | + version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; |
|---|
| 75 | + status = efi_call_proto(tcg2_protocol, get_event_log, version, |
|---|
| 76 | + &log_location, &log_last_entry, |
|---|
| 77 | + &truncated); |
|---|
| 78 | + if (status != EFI_SUCCESS || !log_location) |
|---|
| 79 | + return; |
|---|
| 80 | + |
|---|
| 81 | + } |
|---|
| 82 | + |
|---|
| 87 | 83 | first_entry_addr = (unsigned long) log_location; |
|---|
| 88 | 84 | |
|---|
| 89 | 85 | /* |
|---|
| .. | .. |
|---|
| 98 | 94 | * We need to calculate its size to deduce the full size of |
|---|
| 99 | 95 | * the logs. |
|---|
| 100 | 96 | */ |
|---|
| 101 | | - last_entry_size = sizeof(struct tcpa_event) + |
|---|
| 102 | | - ((struct tcpa_event *) last_entry_addr)->event_size; |
|---|
| 97 | + if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { |
|---|
| 98 | + /* |
|---|
| 99 | + * The TCG2 log format has variable length entries, |
|---|
| 100 | + * and the information to decode the hash algorithms |
|---|
| 101 | + * back into a size is contained in the first entry - |
|---|
| 102 | + * pass a pointer to the final entry (to calculate its |
|---|
| 103 | + * size) and the first entry (so we know how long each |
|---|
| 104 | + * digest is) |
|---|
| 105 | + */ |
|---|
| 106 | + last_entry_size = |
|---|
| 107 | + __calc_tpm2_event_size((void *)last_entry_addr, |
|---|
| 108 | + (void *)(long)log_location, |
|---|
| 109 | + false); |
|---|
| 110 | + } else { |
|---|
| 111 | + last_entry_size = sizeof(struct tcpa_event) + |
|---|
| 112 | + ((struct tcpa_event *) last_entry_addr)->event_size; |
|---|
| 113 | + } |
|---|
| 103 | 114 | log_size = log_last_entry - log_location + last_entry_size; |
|---|
| 104 | 115 | } |
|---|
| 105 | 116 | |
|---|
| 106 | 117 | /* Allocate space for the logs and copy them. */ |
|---|
| 107 | | - status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
|---|
| 108 | | - sizeof(*log_tbl) + log_size, |
|---|
| 109 | | - (void **) &log_tbl); |
|---|
| 118 | + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, |
|---|
| 119 | + sizeof(*log_tbl) + log_size, (void **)&log_tbl); |
|---|
| 110 | 120 | |
|---|
| 111 | 121 | if (status != EFI_SUCCESS) { |
|---|
| 112 | | - efi_printk(sys_table_arg, |
|---|
| 113 | | - "Unable to allocate memory for event log\n"); |
|---|
| 122 | + efi_err("Unable to allocate memory for event log\n"); |
|---|
| 114 | 123 | return; |
|---|
| 124 | + } |
|---|
| 125 | + |
|---|
| 126 | + /* |
|---|
| 127 | + * Figure out whether any events have already been logged to the |
|---|
| 128 | + * final events structure, and if so how much space they take up |
|---|
| 129 | + */ |
|---|
| 130 | + if (version == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) |
|---|
| 131 | + final_events_table = get_efi_config_table(LINUX_EFI_TPM_FINAL_LOG_GUID); |
|---|
| 132 | + if (final_events_table && final_events_table->nr_events) { |
|---|
| 133 | + struct tcg_pcr_event2_head *header; |
|---|
| 134 | + int offset; |
|---|
| 135 | + void *data; |
|---|
| 136 | + int event_size; |
|---|
| 137 | + int i = final_events_table->nr_events; |
|---|
| 138 | + |
|---|
| 139 | + data = (void *)final_events_table; |
|---|
| 140 | + offset = sizeof(final_events_table->version) + |
|---|
| 141 | + sizeof(final_events_table->nr_events); |
|---|
| 142 | + |
|---|
| 143 | + while (i > 0) { |
|---|
| 144 | + header = data + offset + final_events_size; |
|---|
| 145 | + event_size = __calc_tpm2_event_size(header, |
|---|
| 146 | + (void *)(long)log_location, |
|---|
| 147 | + false); |
|---|
| 148 | + final_events_size += event_size; |
|---|
| 149 | + i--; |
|---|
| 150 | + } |
|---|
| 115 | 151 | } |
|---|
| 116 | 152 | |
|---|
| 117 | 153 | memset(log_tbl, 0, sizeof(*log_tbl) + log_size); |
|---|
| 118 | 154 | log_tbl->size = log_size; |
|---|
| 119 | | - log_tbl->version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; |
|---|
| 155 | + log_tbl->final_events_preboot_size = final_events_size; |
|---|
| 156 | + log_tbl->version = version; |
|---|
| 120 | 157 | memcpy(log_tbl->log, (void *) first_entry_addr, log_size); |
|---|
| 121 | 158 | |
|---|
| 122 | | - status = efi_call_early(install_configuration_table, |
|---|
| 123 | | - &linux_eventlog_guid, log_tbl); |
|---|
| 159 | + status = efi_bs_call(install_configuration_table, |
|---|
| 160 | + &linux_eventlog_guid, log_tbl); |
|---|
| 124 | 161 | if (status != EFI_SUCCESS) |
|---|
| 125 | 162 | goto err_free; |
|---|
| 126 | 163 | return; |
|---|
| 127 | 164 | |
|---|
| 128 | 165 | err_free: |
|---|
| 129 | | - efi_call_early(free_pool, log_tbl); |
|---|
| 130 | | -} |
|---|
| 131 | | - |
|---|
| 132 | | -void efi_retrieve_tpm2_eventlog(efi_system_table_t *sys_table_arg) |
|---|
| 133 | | -{ |
|---|
| 134 | | - /* Only try to retrieve the logs in 1.2 format. */ |
|---|
| 135 | | - efi_retrieve_tpm2_eventlog_1_2(sys_table_arg); |
|---|
| 166 | + efi_bs_call(free_pool, log_tbl); |
|---|
| 136 | 167 | } |
|---|