Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * TPM handling.
  4 *
  5 * Copyright (C) 2016 CoreOS, Inc
  6 * Copyright (C) 2017 Google, Inc.
  7 *     Matthew Garrett <mjg59@google.com>
  8 *     Thiebaud Weksteen <tweek@google.com>
 
 
 
  9 */
 10#include <linux/efi.h>
 11#include <linux/tpm_eventlog.h>
 12#include <asm/efi.h>
 13
 14#include "efistub.h"
 15
 16#ifdef CONFIG_RESET_ATTACK_MITIGATION
 17static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
 18	L"MemoryOverwriteRequestControl";
 19
 20#define MEMORY_ONLY_RESET_CONTROL_GUID \
 21	EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
 22
 
 
 
 
 
 
 
 
 
 
 23/*
 24 * Enable reboot attack mitigation. This requests that the firmware clear the
 25 * RAM on next reboot before proceeding with boot, ensuring that any secrets
 26 * are cleared. If userland has ensured that all secrets have been removed
 27 * from RAM before reboot it can simply reset this variable.
 28 */
 29void efi_enable_reset_attack_mitigation(void)
 30{
 31	u8 val = 1;
 32	efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
 33	efi_status_t status;
 34	unsigned long datasize = 0;
 35
 36	status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
 37			     NULL, &datasize, NULL);
 38
 39	if (status == EFI_NOT_FOUND)
 40		return;
 41
 42	set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
 43		    EFI_VARIABLE_NON_VOLATILE |
 44		    EFI_VARIABLE_BOOTSERVICE_ACCESS |
 45		    EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
 46}
 47
 48#endif
 49
 50static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_location,
 51				       efi_physical_addr_t log_last_entry,
 52				       efi_bool_t truncated,
 53				       struct efi_tcg2_final_events_table *final_events_table)
 54{
 
 55	efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
 56	efi_status_t status;
 
 57	struct linux_efi_tpm_eventlog *log_tbl = NULL;
 58	unsigned long first_entry_addr, last_entry_addr;
 59	size_t log_size, last_entry_size;
 60	int final_events_size = 0;
 
 
 
 
 
 
 61
 
 
 
 
 
 
 
 
 62	first_entry_addr = (unsigned long) log_location;
 63
 64	/*
 65	 * We populate the EFI table even if the logs are empty.
 66	 */
 67	if (!log_last_entry) {
 68		log_size = 0;
 69	} else {
 70		last_entry_addr = (unsigned long) log_last_entry;
 71		/*
 72		 * get_event_log only returns the address of the last entry.
 73		 * We need to calculate its size to deduce the full size of
 74		 * the logs.
 75		 *
 76		 * CC Event log also uses TCG2 format, handle it same as TPM2.
 77		 */
 78		if (version > EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) {
 79			/*
 80			 * The TCG2 log format has variable length entries,
 81			 * and the information to decode the hash algorithms
 82			 * back into a size is contained in the first entry -
 83			 * pass a pointer to the final entry (to calculate its
 84			 * size) and the first entry (so we know how long each
 85			 * digest is)
 86			 */
 87			last_entry_size =
 88				__calc_tpm2_event_size((void *)last_entry_addr,
 89						    (void *)(long)log_location,
 90						    false);
 91		} else {
 92			last_entry_size = sizeof(struct tcpa_event) +
 93			   ((struct tcpa_event *) last_entry_addr)->event_size;
 94		}
 95		log_size = log_last_entry - log_location + last_entry_size;
 96	}
 97
 98	/* Allocate space for the logs and copy them. */
 99	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
100			     sizeof(*log_tbl) + log_size, (void **)&log_tbl);
 
101
102	if (status != EFI_SUCCESS) {
103		efi_err("Unable to allocate memory for event log\n");
 
104		return;
105	}
106
107	/*
108	 * Figure out whether any events have already been logged to the
109	 * final events structure, and if so how much space they take up
110	 */
111	if (final_events_table && final_events_table->nr_events) {
112		struct tcg_pcr_event2_head *header;
113		int offset;
114		void *data;
115		int event_size;
116		int i = final_events_table->nr_events;
117
118		data = (void *)final_events_table;
119		offset = sizeof(final_events_table->version) +
120			sizeof(final_events_table->nr_events);
121
122		while (i > 0) {
123			header = data + offset + final_events_size;
124			event_size = __calc_tpm2_event_size(header,
125						   (void *)(long)log_location,
126						   false);
127			final_events_size += event_size;
128			i--;
129		}
130	}
131
132	memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
133	log_tbl->size = log_size;
134	log_tbl->final_events_preboot_size = final_events_size;
135	log_tbl->version = version;
136	memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
137
138	status = efi_bs_call(install_configuration_table,
139			     &linux_eventlog_guid, log_tbl);
140	if (status != EFI_SUCCESS)
141		goto err_free;
142	return;
143
144err_free:
145	efi_bs_call(free_pool, log_tbl);
146}
147
148void efi_retrieve_eventlog(void)
149{
150	struct efi_tcg2_final_events_table *final_events_table = NULL;
151	efi_physical_addr_t log_location = 0, log_last_entry = 0;
152	efi_guid_t tpm2_guid = EFI_TCG2_PROTOCOL_GUID;
153	int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
154	efi_tcg2_protocol_t *tpm2 = NULL;
155	efi_bool_t truncated;
156	efi_status_t status;
157
158	status = efi_bs_call(locate_protocol, &tpm2_guid, NULL, (void **)&tpm2);
159	if (status == EFI_SUCCESS) {
160		status = efi_call_proto(tpm2, get_event_log, version, &log_location,
161					&log_last_entry, &truncated);
162
163		if (status != EFI_SUCCESS || !log_location) {
164			version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
165			status = efi_call_proto(tpm2, get_event_log, version,
166						&log_location, &log_last_entry,
167						&truncated);
168		} else {
169			final_events_table =
170				get_efi_config_table(EFI_TCG2_FINAL_EVENTS_TABLE_GUID);
171		}
172	} else {
173		efi_guid_t cc_guid = EFI_CC_MEASUREMENT_PROTOCOL_GUID;
174		efi_cc_protocol_t *cc = NULL;
175
176		status = efi_bs_call(locate_protocol, &cc_guid, NULL, (void **)&cc);
177		if (status != EFI_SUCCESS)
178			return;
179
180		version = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
181		status = efi_call_proto(cc, get_event_log, version, &log_location,
182					&log_last_entry, &truncated);
183
184		final_events_table =
185			get_efi_config_table(EFI_CC_FINAL_EVENTS_TABLE_GUID);
186	}
187
188	if (status != EFI_SUCCESS || !log_location)
189		return;
190
191	efi_retrieve_tcg2_eventlog(version, log_location, log_last_entry,
192				   truncated, final_events_table);
193}
v4.17
 
  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
 16#include "efistub.h"
 17
 18#ifdef CONFIG_RESET_ATTACK_MITIGATION
 19static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
 20	L"MemoryOverwriteRequestControl";
 21
 22#define MEMORY_ONLY_RESET_CONTROL_GUID \
 23	EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
 24
 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/*
 36 * Enable reboot attack mitigation. This requests that the firmware clear the
 37 * RAM on next reboot before proceeding with boot, ensuring that any secrets
 38 * are cleared. If userland has ensured that all secrets have been removed
 39 * from RAM before reboot it can simply reset this variable.
 40 */
 41void efi_enable_reset_attack_mitigation(efi_system_table_t *sys_table_arg)
 42{
 43	u8 val = 1;
 44	efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
 45	efi_status_t status;
 46	unsigned long datasize = 0;
 47
 48	status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
 49			     NULL, &datasize, NULL);
 50
 51	if (status == EFI_NOT_FOUND)
 52		return;
 53
 54	set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
 55		    EFI_VARIABLE_NON_VOLATILE |
 56		    EFI_VARIABLE_BOOTSERVICE_ACCESS |
 57		    EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
 58}
 59
 60#endif
 61
 62void efi_retrieve_tpm2_eventlog_1_2(efi_system_table_t *sys_table_arg)
 
 
 
 63{
 64	efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
 65	efi_guid_t linux_eventlog_guid = LINUX_EFI_TPM_EVENT_LOG_GUID;
 66	efi_status_t status;
 67	efi_physical_addr_t log_location, log_last_entry;
 68	struct linux_efi_tpm_eventlog *log_tbl = NULL;
 69	unsigned long first_entry_addr, last_entry_addr;
 70	size_t log_size, last_entry_size;
 71	efi_bool_t truncated;
 72	void *tcg2_protocol = NULL;
 73
 74	status = efi_call_early(locate_protocol, &tcg2_guid, NULL,
 75				&tcg2_protocol);
 76	if (status != EFI_SUCCESS)
 77		return;
 78
 79	status = efi_call_proto(efi_tcg2_protocol, get_event_log, tcg2_protocol,
 80				EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2,
 81				&log_location, &log_last_entry, &truncated);
 82	if (status != EFI_SUCCESS)
 83		return;
 84
 85	if (!log_location)
 86		return;
 87	first_entry_addr = (unsigned long) log_location;
 88
 89	/*
 90	 * We populate the EFI table even if the logs are empty.
 91	 */
 92	if (!log_last_entry) {
 93		log_size = 0;
 94	} else {
 95		last_entry_addr = (unsigned long) log_last_entry;
 96		/*
 97		 * get_event_log only returns the address of the last entry.
 98		 * We need to calculate its size to deduce the full size of
 99		 * the logs.
 
 
100		 */
101		last_entry_size = sizeof(struct tcpa_event) +
102			((struct tcpa_event *) last_entry_addr)->event_size;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103		log_size = log_last_entry - log_location + last_entry_size;
104	}
105
106	/* 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);
110
111	if (status != EFI_SUCCESS) {
112		efi_printk(sys_table_arg,
113			   "Unable to allocate memory for event log\n");
114		return;
115	}
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117	memset(log_tbl, 0, sizeof(*log_tbl) + log_size);
118	log_tbl->size = log_size;
119	log_tbl->version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
 
120	memcpy(log_tbl->log, (void *) first_entry_addr, log_size);
121
122	status = efi_call_early(install_configuration_table,
123				&linux_eventlog_guid, log_tbl);
124	if (status != EFI_SUCCESS)
125		goto err_free;
126	return;
127
128err_free:
129	efi_call_early(free_pool, log_tbl);
130}
131
132void 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);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136}