blob: eea42cc204363307157ff20188cf75e00a99366d [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf4e0c1c12016-03-04 01:09:58 +01002/*
3 * EFI image loader
4 *
5 * based partly on wine code
6 *
7 * Copyright (c) 2016 Alexander Graf
Alexander Graf4e0c1c12016-03-04 01:09:58 +01008 */
9
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Alexander Graf4e0c1c12016-03-04 01:09:58 +010012#include <efi_loader.h>
AKASHI Takahiro0e104e32020-04-14 11:51:44 +090013#include <malloc.h>
Alexander Graf4e0c1c12016-03-04 01:09:58 +010014#include <pe.h>
AKASHI Takahiro0e104e32020-04-14 11:51:44 +090015#include <sort.h>
Heinrich Schuchardt2aa09422020-05-07 17:57:43 +020016#include <crypto/pkcs7_parser.h>
Patrick Wildtfbc745c2020-05-07 02:17:14 +020017#include <linux/err.h>
Alexander Graf4e0c1c12016-03-04 01:09:58 +010018
Rob Clarkc84c1102017-09-13 18:05:38 -040019const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020020const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
21const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID;
22const efi_guid_t efi_guid_loaded_image_device_path =
23 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
Rob Clark53142032017-09-13 18:05:34 -040024const efi_guid_t efi_simple_file_system_protocol_guid =
25 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
26const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010027
Ivan Gorinov749d5c12018-04-05 18:32:06 -070028static int machines[] = {
Alexander Graf8aec3082018-06-18 17:22:57 +020029#if defined(__aarch64__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070030 IMAGE_FILE_MACHINE_ARM64,
Alexander Graf8aec3082018-06-18 17:22:57 +020031#elif defined(__arm__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070032 IMAGE_FILE_MACHINE_ARM,
33 IMAGE_FILE_MACHINE_THUMB,
34 IMAGE_FILE_MACHINE_ARMNT,
35#endif
36
Alexander Graf8aec3082018-06-18 17:22:57 +020037#if defined(__x86_64__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070038 IMAGE_FILE_MACHINE_AMD64,
Alexander Graf8aec3082018-06-18 17:22:57 +020039#elif defined(__i386__)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070040 IMAGE_FILE_MACHINE_I386,
41#endif
42
Alexander Graf8aec3082018-06-18 17:22:57 +020043#if defined(__riscv) && (__riscv_xlen == 32)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070044 IMAGE_FILE_MACHINE_RISCV32,
45#endif
46
Alexander Graf8aec3082018-06-18 17:22:57 +020047#if defined(__riscv) && (__riscv_xlen == 64)
Ivan Gorinov749d5c12018-04-05 18:32:06 -070048 IMAGE_FILE_MACHINE_RISCV64,
49#endif
50 0 };
51
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010052/**
53 * efi_print_image_info() - print information about a loaded image
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020054 *
55 * If the program counter is located within the image the offset to the base
56 * address is shown.
57 *
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020058 * @obj: EFI object
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020059 * @image: loaded image
60 * @pc: program counter (use NULL to suppress offset output)
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010061 * Return: status code
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020062 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020063static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
64 struct efi_loaded_image *image,
65 void *pc)
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020066{
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020067 printf("UEFI image");
68 printf(" [0x%p:0x%p]",
AKASHI Takahiro068879a2018-10-11 04:09:58 -070069 image->image_base, image->image_base + image->image_size - 1);
70 if (pc && pc >= image->image_base &&
71 pc < image->image_base + image->image_size)
72 printf(" pc=0x%zx", pc - image->image_base);
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020073 if (image->file_path)
74 printf(" '%pD'", image->file_path);
75 printf("\n");
76 return EFI_SUCCESS;
77}
78
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +010079/**
80 * efi_print_image_infos() - print information about all loaded images
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020081 *
82 * @pc: program counter (use NULL to suppress offset output)
83 */
84void efi_print_image_infos(void *pc)
85{
86 struct efi_object *efiobj;
87 struct efi_handler *handler;
88
89 list_for_each_entry(efiobj, &efi_obj_list, link) {
90 list_for_each_entry(handler, &efiobj->protocols, link) {
91 if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
92 efi_print_image_info(
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +020093 (struct efi_loaded_image_obj *)efiobj,
Heinrich Schuchardt83d96b72018-04-05 11:56:21 +020094 handler->protocol_interface, pc);
95 }
96 }
97 }
98}
99
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +0100100/**
101 * efi_loader_relocate() - relocate UEFI binary
102 *
103 * @rel: pointer to the relocation table
104 * @rel_size: size of the relocation table in bytes
105 * @efi_reloc: actual load address of the image
106 * @pref_address: preferred load address of the image
107 * Return: status code
108 */
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200109static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700110 unsigned long rel_size, void *efi_reloc,
111 unsigned long pref_address)
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100112{
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700113 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100114 const IMAGE_BASE_RELOCATION *end;
115 int i;
116
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700117 if (delta == 0)
118 return EFI_SUCCESS;
119
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100120 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
Heinrich Schuchardte9644cc2019-02-16 15:36:33 +0100121 while (rel < end && rel->SizeOfBlock) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100122 const uint16_t *relocs = (const uint16_t *)(rel + 1);
123 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
124 while (i--) {
Alexander Graf07032252016-08-18 23:45:18 +0200125 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100126 rel->VirtualAddress;
127 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100128 uint64_t *x64 = efi_reloc + offset;
129 uint32_t *x32 = efi_reloc + offset;
130 uint16_t *x16 = efi_reloc + offset;
131
132 switch (type) {
133 case IMAGE_REL_BASED_ABSOLUTE:
134 break;
135 case IMAGE_REL_BASED_HIGH:
136 *x16 += ((uint32_t)delta) >> 16;
137 break;
138 case IMAGE_REL_BASED_LOW:
139 *x16 += (uint16_t)delta;
140 break;
141 case IMAGE_REL_BASED_HIGHLOW:
142 *x32 += (uint32_t)delta;
143 break;
144 case IMAGE_REL_BASED_DIR64:
145 *x64 += (uint64_t)delta;
146 break;
Alexander Graf71f91202018-06-05 19:20:32 +0200147#ifdef __riscv
148 case IMAGE_REL_BASED_RISCV_HI20:
149 *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
150 (*x32 & 0x00000fff);
151 break;
152 case IMAGE_REL_BASED_RISCV_LOW12I:
153 case IMAGE_REL_BASED_RISCV_LOW12S:
154 /* We know that we're 4k aligned */
155 if (delta & 0xfff) {
156 printf("Unsupported reloc offset\n");
157 return EFI_LOAD_ERROR;
158 }
159 break;
160#endif
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100161 default:
162 printf("Unknown Relocation off %x type %x\n",
163 offset, type);
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200164 return EFI_LOAD_ERROR;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100165 }
166 relocs++;
167 }
168 rel = (const IMAGE_BASE_RELOCATION *)relocs;
169 }
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200170 return EFI_SUCCESS;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100171}
172
173void __weak invalidate_icache_all(void)
174{
175 /* If the system doesn't support icache_all flush, cross our fingers */
176}
177
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +0100178/**
179 * efi_set_code_and_data_type() - determine the memory types to be used for code
180 * and data.
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100181 *
Heinrich Schuchardt0fcdb7a2019-02-16 15:22:13 +0100182 * @loaded_image_info: image descriptor
183 * @image_type: field Subsystem of the optional header for
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100184 * Windows specific field
185 */
186static void efi_set_code_and_data_type(
187 struct efi_loaded_image *loaded_image_info,
188 uint16_t image_type)
189{
190 switch (image_type) {
191 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
192 loaded_image_info->image_code_type = EFI_LOADER_CODE;
193 loaded_image_info->image_data_type = EFI_LOADER_DATA;
194 break;
195 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
196 loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
197 loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
198 break;
199 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
Heinrich Schuchardt467fd7a2018-01-31 18:45:35 +0000200 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100201 loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
202 loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
203 break;
204 default:
205 printf("%s: invalid image type: %u\n", __func__, image_type);
206 /* Let's assume it is an application */
207 loaded_image_info->image_code_type = EFI_LOADER_CODE;
208 loaded_image_info->image_data_type = EFI_LOADER_DATA;
209 break;
210 }
211}
212
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900213#ifdef CONFIG_EFI_SECURE_BOOT
214/**
Heinrich Schuchardt72bc7142020-05-30 06:44:48 +0200215 * cmp_pe_section() - compare virtual addresses of two PE image sections
216 * @arg1: pointer to pointer to first section header
217 * @arg2: pointer to pointer to second section header
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900218 *
Heinrich Schuchardt72bc7142020-05-30 06:44:48 +0200219 * Compare the virtual addresses of two sections of an portable executable.
220 * The arguments are defined as const void * to allow usage with qsort().
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900221 *
Heinrich Schuchardt72bc7142020-05-30 06:44:48 +0200222 * Return: -1 if the virtual address of arg1 is less than that of arg2,
223 * 0 if the virtual addresses are equal, 1 if the virtual address
224 * of arg1 is greater than that of arg2.
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900225 */
226static int cmp_pe_section(const void *arg1, const void *arg2)
227{
228 const IMAGE_SECTION_HEADER *section1, *section2;
229
230 section1 = *((const IMAGE_SECTION_HEADER **)arg1);
231 section2 = *((const IMAGE_SECTION_HEADER **)arg2);
232
233 if (section1->VirtualAddress < section2->VirtualAddress)
234 return -1;
235 else if (section1->VirtualAddress == section2->VirtualAddress)
236 return 0;
237 else
238 return 1;
239}
240
241/**
Heinrich Schuchardtda6d6072020-05-30 05:48:08 +0200242 * efi_image_parse() - parse a PE image
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900243 * @efi: Pointer to image
244 * @len: Size of @efi
245 * @regp: Pointer to a list of regions
246 * @auth: Pointer to a pointer to authentication data in PE
247 * @auth_len: Size of @auth
248 *
249 * Parse image binary in PE32(+) format, assuming that sanity of PE image
250 * has been checked by a caller.
251 * On success, an address of authentication data in @efi and its size will
252 * be returned in @auth and @auth_len, respectively.
253 *
254 * Return: true on success, false on error
255 */
256bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
257 WIN_CERTIFICATE **auth, size_t *auth_len)
258{
259 struct efi_image_regions *regs;
260 IMAGE_DOS_HEADER *dos;
261 IMAGE_NT_HEADERS32 *nt;
262 IMAGE_SECTION_HEADER *sections, **sorted;
263 int num_regions, num_sections, i;
264 int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY;
265 u32 align, size, authsz, authoff;
266 size_t bytes_hashed;
267
268 dos = (void *)efi;
269 nt = (void *)(efi + dos->e_lfanew);
AKASHI Takahiro0d5eba42020-07-08 14:01:53 +0900270 authoff = 0;
271 authsz = 0;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900272
273 /*
274 * Count maximum number of regions to be digested.
275 * We don't have to have an exact number here.
276 * See efi_image_region_add()'s in parsing below.
277 */
278 num_regions = 3; /* for header */
279 num_regions += nt->FileHeader.NumberOfSections;
280 num_regions++; /* for extra */
281
282 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions,
283 1);
284 if (!regs)
285 goto err;
286 regs->max = num_regions;
287
288 /*
289 * Collect data regions for hash calculation
290 * 1. File headers
291 */
292 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
293 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
294 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
295
296 /* Skip CheckSum */
297 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
298 if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
299 efi_image_region_add(regs,
AKASHI Takahirob69546f2020-05-08 14:51:59 +0900300 &opt->Subsystem,
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900301 efi + opt->SizeOfHeaders, 0);
302 } else {
303 /* Skip Certificates Table */
304 efi_image_region_add(regs,
AKASHI Takahirob69546f2020-05-08 14:51:59 +0900305 &opt->Subsystem,
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900306 &opt->DataDirectory[ctidx], 0);
307 efi_image_region_add(regs,
308 &opt->DataDirectory[ctidx] + 1,
309 efi + opt->SizeOfHeaders, 0);
AKASHI Takahiro0d5eba42020-07-08 14:01:53 +0900310
311 authoff = opt->DataDirectory[ctidx].VirtualAddress;
312 authsz = opt->DataDirectory[ctidx].Size;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900313 }
314
315 bytes_hashed = opt->SizeOfHeaders;
316 align = opt->FileAlignment;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900317 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
318 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
319
AKASHI Takahiro0d5eba42020-07-08 14:01:53 +0900320 /* Skip CheckSum */
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900321 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
AKASHI Takahiro0d5eba42020-07-08 14:01:53 +0900322 if (nt->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
323 efi_image_region_add(regs,
324 &opt->Subsystem,
325 efi + opt->SizeOfHeaders, 0);
326 } else {
327 /* Skip Certificates Table */
328 efi_image_region_add(regs, &opt->Subsystem,
329 &opt->DataDirectory[ctidx], 0);
330 efi_image_region_add(regs,
331 &opt->DataDirectory[ctidx] + 1,
332 efi + opt->SizeOfHeaders, 0);
333
334 authoff = opt->DataDirectory[ctidx].VirtualAddress;
335 authsz = opt->DataDirectory[ctidx].Size;
336 }
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900337
338 bytes_hashed = opt->SizeOfHeaders;
339 align = opt->FileAlignment;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900340 } else {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900341 EFI_PRINT("%s: Invalid optional header magic %x\n", __func__,
342 nt->OptionalHeader.Magic);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900343 goto err;
344 }
345
346 /* 2. Sections */
347 num_sections = nt->FileHeader.NumberOfSections;
348 sections = (void *)((uint8_t *)&nt->OptionalHeader +
349 nt->FileHeader.SizeOfOptionalHeader);
350 sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections);
351 if (!sorted) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900352 EFI_PRINT("%s: Out of memory\n", __func__);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900353 goto err;
354 }
355
356 /*
357 * Make sure the section list is in ascending order.
358 */
359 for (i = 0; i < num_sections; i++)
360 sorted[i] = &sections[i];
361 qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section);
362
363 for (i = 0; i < num_sections; i++) {
364 if (!sorted[i]->SizeOfRawData)
365 continue;
366
367 size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1);
368 efi_image_region_add(regs, efi + sorted[i]->PointerToRawData,
369 efi + sorted[i]->PointerToRawData + size,
370 0);
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900371 EFI_PRINT("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
372 i, sorted[i]->Name,
373 sorted[i]->PointerToRawData,
374 sorted[i]->PointerToRawData + size,
375 sorted[i]->VirtualAddress,
376 sorted[i]->VirtualAddress
377 + sorted[i]->Misc.VirtualSize);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900378
379 bytes_hashed += size;
380 }
381 free(sorted);
382
383 /* 3. Extra data excluding Certificates Table */
384 if (bytes_hashed + authsz < len) {
Heinrich Schuchardtd054aec2020-07-07 07:23:44 +0200385 EFI_PRINT("extra data for hash: %zu\n",
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900386 len - (bytes_hashed + authsz));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900387 efi_image_region_add(regs, efi + bytes_hashed,
388 efi + len - authsz, 0);
389 }
390
391 /* Return Certificates Table */
392 if (authsz) {
393 if (len < authoff + authsz) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900394 EFI_PRINT("%s: Size for auth too large: %u >= %zu\n",
395 __func__, authsz, len - authoff);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900396 goto err;
397 }
398 if (authsz < sizeof(*auth)) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900399 EFI_PRINT("%s: Size for auth too small: %u < %zu\n",
400 __func__, authsz, sizeof(*auth));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900401 goto err;
402 }
403 *auth = efi + authoff;
404 *auth_len = authsz;
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900405 EFI_PRINT("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff,
406 authsz);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900407 } else {
408 *auth = NULL;
409 *auth_len = 0;
410 }
411
412 *regp = regs;
413
414 return true;
415
416err:
417 free(regs);
418
419 return false;
420}
421
422/**
Heinrich Schuchardtda6d6072020-05-30 05:48:08 +0200423 * efi_image_unsigned_authenticate() - authenticate unsigned image with
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900424 * SHA256 hash
425 * @regs: List of regions to be verified
426 *
427 * If an image is not signed, it doesn't have a signature. In this case,
428 * its message digest is calculated and it will be compared with one of
429 * hash values stored in signature databases.
430 *
431 * Return: true if authenticated, false if not
432 */
433static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs)
434{
435 struct efi_signature_store *db = NULL, *dbx = NULL;
436 bool ret = false;
437
438 dbx = efi_sigstore_parse_sigdb(L"dbx");
439 if (!dbx) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900440 EFI_PRINT("Getting signature database(dbx) failed\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900441 goto out;
442 }
443
444 db = efi_sigstore_parse_sigdb(L"db");
445 if (!db) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900446 EFI_PRINT("Getting signature database(db) failed\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900447 goto out;
448 }
449
450 /* try black-list first */
AKASHI Takahirode924072020-07-08 14:01:57 +0900451 if (efi_signature_lookup_digest(regs, dbx)) {
452 EFI_PRINT("Image is not signed and its digest found in \"dbx\"\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900453 goto out;
454 }
455
456 /* try white-list */
AKASHI Takahirode924072020-07-08 14:01:57 +0900457 if (efi_signature_lookup_digest(regs, db))
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900458 ret = true;
459 else
AKASHI Takahirode924072020-07-08 14:01:57 +0900460 EFI_PRINT("Image is not signed and its digest not found in \"db\" or \"dbx\"\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900461
462out:
463 efi_sigstore_free(db);
464 efi_sigstore_free(dbx);
465
466 return ret;
467}
468
469/**
Heinrich Schuchardtda6d6072020-05-30 05:48:08 +0200470 * efi_image_authenticate() - verify a signature of signed image
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900471 * @efi: Pointer to image
472 * @efi_size: Size of @efi
473 *
474 * A signed image should have its signature stored in a table of its PE header.
475 * So if an image is signed and only if if its signature is verified using
476 * signature databases, an image is authenticated.
477 * If an image is not signed, its validity is checked by using
478 * efi_image_unsigned_authenticated().
479 * TODO:
480 * When AuditMode==0, if the image's signature is not found in
481 * the authorized database, or is found in the forbidden database,
482 * the image will not be started and instead, information about it
483 * will be placed in this table.
484 * When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created
485 * in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found
486 * in the certificate table of every image that is validated.
487 *
488 * Return: true if authenticated, false if not
489 */
490static bool efi_image_authenticate(void *efi, size_t efi_size)
491{
492 struct efi_image_regions *regs = NULL;
493 WIN_CERTIFICATE *wincerts = NULL, *wincert;
494 size_t wincerts_len;
495 struct pkcs7_message *msg = NULL;
496 struct efi_signature_store *db = NULL, *dbx = NULL;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900497 void *new_efi = NULL;
AKASHI Takahiroeab12752020-07-08 14:01:52 +0900498 u8 *auth, *wincerts_end;
499 size_t new_efi_size, auth_size;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900500 bool ret = false;
501
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900502 debug("%s: Enter, %d\n", __func__, ret);
503
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900504 if (!efi_secure_boot_enabled())
505 return true;
506
507 /*
508 * Size must be 8-byte aligned and the trailing bytes must be
509 * zero'ed. Otherwise hash value may be incorrect.
510 */
511 if (efi_size & 0x7) {
512 new_efi_size = (efi_size + 0x7) & ~0x7ULL;
513 new_efi = calloc(new_efi_size, 1);
514 if (!new_efi)
515 return false;
516 memcpy(new_efi, efi, efi_size);
517 efi = new_efi;
518 efi_size = new_efi_size;
519 }
520
521 if (!efi_image_parse(efi, efi_size, &regs, &wincerts,
522 &wincerts_len)) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900523 EFI_PRINT("Parsing PE executable image failed\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900524 goto err;
525 }
526
527 if (!wincerts) {
528 /* The image is not signed */
529 ret = efi_image_unsigned_authenticate(regs);
530
531 goto err;
532 }
533
534 /*
535 * verify signature using db and dbx
536 */
537 db = efi_sigstore_parse_sigdb(L"db");
538 if (!db) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900539 EFI_PRINT("Getting signature database(db) failed\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900540 goto err;
541 }
542
543 dbx = efi_sigstore_parse_sigdb(L"dbx");
544 if (!dbx) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900545 EFI_PRINT("Getting signature database(dbx) failed\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900546 goto err;
547 }
548
AKASHI Takahiro16411802020-08-14 14:39:23 +0900549 if (efi_signature_lookup_digest(regs, dbx)) {
550 EFI_PRINT("Image's digest was found in \"dbx\"\n");
551 goto err;
552 }
553
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900554 /*
555 * go through WIN_CERTIFICATE list
556 * NOTE:
557 * We may have multiple signatures either as WIN_CERTIFICATE's
558 * in PE header, or as pkcs7 SignerInfo's in SignedData.
559 * So the verification policy here is:
560 * - Success if, at least, one of signatures is verified
AKASHI Takahiro16411802020-08-14 14:39:23 +0900561 * - unless signature is rejected explicitly with its digest.
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900562 */
AKASHI Takahiro16411802020-08-14 14:39:23 +0900563
AKASHI Takahiroeab12752020-07-08 14:01:52 +0900564 for (wincert = wincerts, wincerts_end = (u8 *)wincerts + wincerts_len;
565 (u8 *)wincert < wincerts_end;
566 wincert = (WIN_CERTIFICATE *)
567 ((u8 *)wincert + ALIGN(wincert->dwLength, 8))) {
568 if ((u8 *)wincert + sizeof(*wincert) >= wincerts_end)
569 break;
570
571 if (wincert->dwLength <= sizeof(*wincert)) {
572 EFI_PRINT("dwLength too small: %u < %zu\n",
573 wincert->dwLength, sizeof(*wincert));
574 continue;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900575 }
AKASHI Takahiroeab12752020-07-08 14:01:52 +0900576
577 EFI_PRINT("WIN_CERTIFICATE_TYPE: 0x%x\n",
578 wincert->wCertificateType);
579
580 auth = (u8 *)wincert + sizeof(*wincert);
581 auth_size = wincert->dwLength - sizeof(*wincert);
582 if (wincert->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
583 if (auth + sizeof(efi_guid_t) >= wincerts_end)
584 break;
585
586 if (auth_size <= sizeof(efi_guid_t)) {
587 EFI_PRINT("dwLength too small: %u < %zu\n",
588 wincert->dwLength, sizeof(*wincert));
589 continue;
590 }
591 if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) {
592 EFI_PRINT("Certificate type not supported: %pUl\n",
593 auth);
594 continue;
595 }
596
597 auth += sizeof(efi_guid_t);
598 auth_size -= sizeof(efi_guid_t);
599 } else if (wincert->wCertificateType
600 != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
601 EFI_PRINT("Certificate type not supported\n");
602 continue;
603 }
604
605 msg = pkcs7_parse_message(auth, auth_size);
Patrick Wildtfbc745c2020-05-07 02:17:14 +0200606 if (IS_ERR(msg)) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900607 EFI_PRINT("Parsing image's signature failed\n");
Patrick Wildtfbc745c2020-05-07 02:17:14 +0200608 msg = NULL;
AKASHI Takahiroeab12752020-07-08 14:01:52 +0900609 continue;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900610 }
611
AKASHI Takahirode924072020-07-08 14:01:57 +0900612 /*
613 * NOTE:
614 * UEFI specification defines two signature types possible
615 * in signature database:
616 * a. x509 certificate, where a signature in image is
617 * a message digest encrypted by RSA public key
618 * (EFI_CERT_X509_GUID)
619 * b. bare hash value of message digest
620 * (EFI_CERT_SHAxxx_GUID)
621 *
622 * efi_signature_verify() handles case (a), while
623 * efi_signature_lookup_digest() handles case (b).
624 *
625 * There is a third type:
626 * c. message digest of a certificate
627 * (EFI_CERT_X509_SHAAxxx_GUID)
628 * This type of signature is used only in revocation list
629 * (dbx) and handled as part of efi_signatgure_verify().
630 */
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900631 /* try black-list first */
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900632 if (efi_signature_verify_one(regs, msg, dbx)) {
AKASHI Takahirocb0b4112020-06-09 14:09:35 +0900633 EFI_PRINT("Signature was rejected by \"dbx\"\n");
AKASHI Takahiro16411802020-08-14 14:39:23 +0900634 continue;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900635 }
636
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900637 if (!efi_signature_check_signers(msg, dbx)) {
638 EFI_PRINT("Signer(s) in \"dbx\"\n");
AKASHI Takahiro16411802020-08-14 14:39:23 +0900639 continue;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900640 }
AKASHI Takahirode924072020-07-08 14:01:57 +0900641
642 /* try white-list */
AKASHI Takahiro16411802020-08-14 14:39:23 +0900643 if (efi_signature_verify(regs, msg, db, dbx)) {
644 ret = true;
645 break;
646 }
AKASHI Takahirode924072020-07-08 14:01:57 +0900647
648 debug("Signature was not verified by \"db\"\n");
649
AKASHI Takahiro16411802020-08-14 14:39:23 +0900650 if (efi_signature_lookup_digest(regs, db)) {
651 ret = true;
652 break;
653 }
AKASHI Takahirode924072020-07-08 14:01:57 +0900654
655 debug("Image's digest was not found in \"db\" or \"dbx\"\n");
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900656 }
657
658err:
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900659 efi_sigstore_free(db);
660 efi_sigstore_free(dbx);
661 pkcs7_free_message(msg);
662 free(regs);
663 free(new_efi);
664
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900665 debug("%s: Exit, %d\n", __func__, ret);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900666 return ret;
667}
668#else
669static bool efi_image_authenticate(void *efi, size_t efi_size)
670{
671 return true;
672}
673#endif /* CONFIG_EFI_SECURE_BOOT */
674
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100675/**
676 * efi_load_pe() - relocate EFI binary
677 *
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100678 * This function loads all sections from a PE binary into a newly reserved
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100679 * piece of memory. On success the entry point is returned as handle->entry.
680 *
681 * @handle: loaded image handle
682 * @efi: pointer to the EFI binary
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900683 * @efi_size: size of @efi binary
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100684 * @loaded_image_info: loaded image protocol
685 * Return: status code
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100686 */
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900687efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
688 void *efi, size_t efi_size,
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100689 struct efi_loaded_image *loaded_image_info)
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100690{
691 IMAGE_NT_HEADERS32 *nt;
692 IMAGE_DOS_HEADER *dos;
693 IMAGE_SECTION_HEADER *sections;
694 int num_sections;
695 void *efi_reloc;
696 int i;
697 const IMAGE_BASE_RELOCATION *rel;
698 unsigned long rel_size;
699 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700700 uint64_t image_base;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100701 unsigned long virt_size = 0;
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700702 int supported = 0;
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900703 efi_status_t ret;
704
705 /* Sanity check for a file header */
706 if (efi_size < sizeof(*dos)) {
707 printf("%s: Truncated DOS Header\n", __func__);
708 ret = EFI_LOAD_ERROR;
709 goto err;
710 }
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100711
712 dos = efi;
713 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
714 printf("%s: Invalid DOS Signature\n", __func__);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900715 ret = EFI_LOAD_ERROR;
716 goto err;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100717 }
718
Heinrich Schuchardt5605e662020-05-30 07:35:59 +0200719 /*
720 * Check if the image section header fits into the file. Knowing that at
721 * least one section header follows we only need to check for the length
722 * of the 64bit header which is longer than the 32bit header.
723 */
724 if (efi_size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS64)) {
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900725 printf("%s: Invalid offset for Extended Header\n", __func__);
726 ret = EFI_LOAD_ERROR;
727 goto err;
728 }
729
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100730 nt = (void *) ((char *)efi + dos->e_lfanew);
731 if (nt->Signature != IMAGE_NT_SIGNATURE) {
732 printf("%s: Invalid NT Signature\n", __func__);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900733 ret = EFI_LOAD_ERROR;
734 goto err;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100735 }
736
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700737 for (i = 0; machines[i]; i++)
738 if (machines[i] == nt->FileHeader.Machine) {
739 supported = 1;
740 break;
741 }
742
743 if (!supported) {
744 printf("%s: Machine type 0x%04x is not supported\n",
745 __func__, nt->FileHeader.Machine);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900746 ret = EFI_LOAD_ERROR;
747 goto err;
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700748 }
749
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100750 num_sections = nt->FileHeader.NumberOfSections;
751 sections = (void *)&nt->OptionalHeader +
752 nt->FileHeader.SizeOfOptionalHeader;
753
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900754 if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections
755 - efi)) {
756 printf("%s: Invalid number of sections: %d\n",
757 __func__, num_sections);
758 ret = EFI_LOAD_ERROR;
759 goto err;
760 }
761
762 /* Authenticate an image */
763 if (efi_image_authenticate(efi, efi_size))
764 handle->auth_status = EFI_IMAGE_AUTH_PASSED;
765 else
766 handle->auth_status = EFI_IMAGE_AUTH_FAILED;
767
768 /* Calculate upper virtual address boundary */
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100769 for (i = num_sections - 1; i >= 0; i--) {
770 IMAGE_SECTION_HEADER *sec = &sections[i];
771 virt_size = max_t(unsigned long, virt_size,
772 sec->VirtualAddress + sec->Misc.VirtualSize);
773 }
774
775 /* Read 32/64bit specific header bits */
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700776 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100777 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
778 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700779 image_base = opt->ImageBase;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100780 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt37587522019-05-01 20:07:04 +0200781 handle->image_type = opt->Subsystem;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100782 efi_reloc = efi_alloc(virt_size,
783 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100784 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100785 printf("%s: Could not allocate %lu bytes\n",
786 __func__, virt_size);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900787 ret = EFI_OUT_OF_RESOURCES;
788 goto err;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100789 }
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100790 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100791 rel_size = opt->DataDirectory[rel_idx].Size;
792 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200793 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov749d5c12018-04-05 18:32:06 -0700794 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100795 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700796 image_base = opt->ImageBase;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100797 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt37587522019-05-01 20:07:04 +0200798 handle->image_type = opt->Subsystem;
Heinrich Schuchardtfaef0e72018-01-19 20:24:41 +0100799 efi_reloc = efi_alloc(virt_size,
800 loaded_image_info->image_code_type);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100801 if (!efi_reloc) {
Heinrich Schuchardtb10f2992017-12-22 19:16:57 +0100802 printf("%s: Could not allocate %lu bytes\n",
803 __func__, virt_size);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900804 ret = EFI_OUT_OF_RESOURCES;
805 goto err;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100806 }
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +0100807 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100808 rel_size = opt->DataDirectory[rel_idx].Size;
809 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt4c07b8e2018-04-03 22:29:32 +0200810 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100811 } else {
812 printf("%s: Invalid optional header magic %x\n", __func__,
813 nt->OptionalHeader.Magic);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900814 ret = EFI_LOAD_ERROR;
815 goto err;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100816 }
817
AKASHI Takahiro068879a2018-10-11 04:09:58 -0700818 /* Copy PE headers */
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900819 memcpy(efi_reloc, efi,
820 sizeof(*dos)
821 + sizeof(*nt)
822 + nt->FileHeader.SizeOfOptionalHeader
823 + num_sections * sizeof(IMAGE_SECTION_HEADER));
AKASHI Takahiro068879a2018-10-11 04:09:58 -0700824
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100825 /* Load sections into RAM */
826 for (i = num_sections - 1; i >= 0; i--) {
827 IMAGE_SECTION_HEADER *sec = &sections[i];
828 memset(efi_reloc + sec->VirtualAddress, 0,
829 sec->Misc.VirtualSize);
830 memcpy(efi_reloc + sec->VirtualAddress,
831 efi + sec->PointerToRawData,
832 sec->SizeOfRawData);
833 }
834
835 /* Run through relocations */
Ivan Gorinovc7270bc2018-05-02 16:36:02 -0700836 if (efi_loader_relocate(rel, rel_size, efi_reloc,
837 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200838 efi_free_pages((uintptr_t) efi_reloc,
839 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900840 ret = EFI_LOAD_ERROR;
841 goto err;
xypron.glpk@gmx.de6ec97d22017-07-04 00:12:58 +0200842 }
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100843
844 /* Flush cache */
Simon Glassd25bb302016-11-07 08:47:04 -0700845 flush_cache((ulong)efi_reloc,
Alexander Graf59254d62018-04-23 07:59:47 +0200846 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100847 invalidate_icache_all();
848
849 /* Populate the loaded image interface bits */
AKASHI Takahiro068879a2018-10-11 04:09:58 -0700850 loaded_image_info->image_base = efi_reloc;
851 loaded_image_info->image_size = virt_size;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100852
AKASHI Takahiro0e104e32020-04-14 11:51:44 +0900853 if (handle->auth_status == EFI_IMAGE_AUTH_PASSED)
854 return EFI_SUCCESS;
855 else
856 return EFI_SECURITY_VIOLATION;
857
858err:
859 return ret;
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100860}