blob: 5dd601908d524652e41dc5b3813324a62c7f963b [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafcb149c62016-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 Grafcb149c62016-03-04 01:09:58 +01008 */
9
10#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070011#include <cpu_func.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010012#include <efi_loader.h>
AKASHI Takahiro4540dab2020-04-14 11:51:44 +090013#include <malloc.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010014#include <pe.h>
AKASHI Takahiro4540dab2020-04-14 11:51:44 +090015#include <sort.h>
Heinrich Schuchardtd7ca3ce2020-05-07 17:57:43 +020016#include <crypto/pkcs7_parser.h>
Patrick Wildt6f146152020-05-07 02:17:14 +020017#include <linux/err.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010018
Rob Clark9975fe92017-09-13 18:05:38 -040019const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
Heinrich Schuchardtdec88e42019-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 Clark2a920802017-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 Grafcb149c62016-03-04 01:09:58 +010027
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070028static int machines[] = {
Alexander Grafb59f6972018-06-18 17:22:57 +020029#if defined(__aarch64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070030 IMAGE_FILE_MACHINE_ARM64,
Alexander Grafb59f6972018-06-18 17:22:57 +020031#elif defined(__arm__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070032 IMAGE_FILE_MACHINE_ARM,
33 IMAGE_FILE_MACHINE_THUMB,
34 IMAGE_FILE_MACHINE_ARMNT,
35#endif
36
Alexander Grafb59f6972018-06-18 17:22:57 +020037#if defined(__x86_64__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070038 IMAGE_FILE_MACHINE_AMD64,
Alexander Grafb59f6972018-06-18 17:22:57 +020039#elif defined(__i386__)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070040 IMAGE_FILE_MACHINE_I386,
41#endif
42
Alexander Grafb59f6972018-06-18 17:22:57 +020043#if defined(__riscv) && (__riscv_xlen == 32)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070044 IMAGE_FILE_MACHINE_RISCV32,
45#endif
46
Alexander Grafb59f6972018-06-18 17:22:57 +020047#if defined(__riscv) && (__riscv_xlen == 64)
Ivan Gorinov61a5ced2018-04-05 18:32:06 -070048 IMAGE_FILE_MACHINE_RISCV64,
49#endif
50 0 };
51
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010052/**
53 * efi_print_image_info() - print information about a loaded image
Heinrich Schuchardtc9a63f42018-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 Schuchardtc9828742018-09-23 17:21:51 +020058 * @obj: EFI object
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020059 * @image: loaded image
60 * @pc: program counter (use NULL to suppress offset output)
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +010061 * Return: status code
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020062 */
Heinrich Schuchardtc9828742018-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 Schuchardtc9a63f42018-04-05 11:56:21 +020066{
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020067 printf("UEFI image");
68 printf(" [0x%p:0x%p]",
AKASHI Takahiro8458bf62018-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 Schuchardtc9a63f42018-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 Schuchardt1db561e2019-02-16 15:22:13 +010079/**
80 * efi_print_image_infos() - print information about all loaded images
Heinrich Schuchardtc9a63f42018-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 Schuchardtc9828742018-09-23 17:21:51 +020093 (struct efi_loaded_image_obj *)efiobj,
Heinrich Schuchardtc9a63f42018-04-05 11:56:21 +020094 handler->protocol_interface, pc);
95 }
96 }
97 }
98}
99
Heinrich Schuchardt1db561e2019-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.deda684a62017-07-04 00:12:58 +0200109static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700110 unsigned long rel_size, void *efi_reloc,
111 unsigned long pref_address)
Alexander Grafcb149c62016-03-04 01:09:58 +0100112{
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700113 unsigned long delta = (unsigned long)efi_reloc - pref_address;
Alexander Grafcb149c62016-03-04 01:09:58 +0100114 const IMAGE_BASE_RELOCATION *end;
115 int i;
116
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700117 if (delta == 0)
118 return EFI_SUCCESS;
119
Alexander Grafcb149c62016-03-04 01:09:58 +0100120 end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
Heinrich Schuchardt997fc122019-02-16 15:36:33 +0100121 while (rel < end && rel->SizeOfBlock) {
Alexander Grafcb149c62016-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 Grafb1237c62016-08-18 23:45:18 +0200125 uint32_t offset = (uint32_t)(*relocs & 0xfff) +
Alexander Grafcb149c62016-03-04 01:09:58 +0100126 rel->VirtualAddress;
127 int type = *relocs >> EFI_PAGE_SHIFT;
Alexander Grafcb149c62016-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 Grafde452c02018-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 Grafcb149c62016-03-04 01:09:58 +0100161 default:
162 printf("Unknown Relocation off %x type %x\n",
163 offset, type);
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200164 return EFI_LOAD_ERROR;
Alexander Grafcb149c62016-03-04 01:09:58 +0100165 }
166 relocs++;
167 }
168 rel = (const IMAGE_BASE_RELOCATION *)relocs;
169 }
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200170 return EFI_SUCCESS;
Alexander Grafcb149c62016-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 Schuchardt1db561e2019-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 Schuchardt36b41a32018-01-19 20:24:41 +0100181 *
Heinrich Schuchardt1db561e2019-02-16 15:22:13 +0100182 * @loaded_image_info: image descriptor
183 * @image_type: field Subsystem of the optional header for
Heinrich Schuchardt36b41a32018-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 Schuchardt268ec6e2018-01-31 18:45:35 +0000200 case IMAGE_SUBSYSTEM_EFI_ROM:
Heinrich Schuchardt36b41a32018-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 Takahiro4540dab2020-04-14 11:51:44 +0900213#ifdef CONFIG_EFI_SECURE_BOOT
214/**
215 * cmp_pe_section - compare two sections
216 * @arg1: Pointer to pointer to first section
217 * @arg2: Pointer to pointer to second section
218 *
219 * Compare two sections in PE image.
220 *
221 * Return: -1, 0, 1 respectively if arg1 < arg2, arg1 == arg2 or
222 * arg1 > arg2
223 */
224static int cmp_pe_section(const void *arg1, const void *arg2)
225{
226 const IMAGE_SECTION_HEADER *section1, *section2;
227
228 section1 = *((const IMAGE_SECTION_HEADER **)arg1);
229 section2 = *((const IMAGE_SECTION_HEADER **)arg2);
230
231 if (section1->VirtualAddress < section2->VirtualAddress)
232 return -1;
233 else if (section1->VirtualAddress == section2->VirtualAddress)
234 return 0;
235 else
236 return 1;
237}
238
239/**
240 * efi_image_parse - parse a PE image
241 * @efi: Pointer to image
242 * @len: Size of @efi
243 * @regp: Pointer to a list of regions
244 * @auth: Pointer to a pointer to authentication data in PE
245 * @auth_len: Size of @auth
246 *
247 * Parse image binary in PE32(+) format, assuming that sanity of PE image
248 * has been checked by a caller.
249 * On success, an address of authentication data in @efi and its size will
250 * be returned in @auth and @auth_len, respectively.
251 *
252 * Return: true on success, false on error
253 */
254bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
255 WIN_CERTIFICATE **auth, size_t *auth_len)
256{
257 struct efi_image_regions *regs;
258 IMAGE_DOS_HEADER *dos;
259 IMAGE_NT_HEADERS32 *nt;
260 IMAGE_SECTION_HEADER *sections, **sorted;
261 int num_regions, num_sections, i;
262 int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY;
263 u32 align, size, authsz, authoff;
264 size_t bytes_hashed;
265
266 dos = (void *)efi;
267 nt = (void *)(efi + dos->e_lfanew);
268
269 /*
270 * Count maximum number of regions to be digested.
271 * We don't have to have an exact number here.
272 * See efi_image_region_add()'s in parsing below.
273 */
274 num_regions = 3; /* for header */
275 num_regions += nt->FileHeader.NumberOfSections;
276 num_regions++; /* for extra */
277
278 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions,
279 1);
280 if (!regs)
281 goto err;
282 regs->max = num_regions;
283
284 /*
285 * Collect data regions for hash calculation
286 * 1. File headers
287 */
288 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
289 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
290 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
291
292 /* Skip CheckSum */
293 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
294 if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) {
295 efi_image_region_add(regs,
AKASHI Takahiro52d7bfe2020-05-08 14:51:59 +0900296 &opt->Subsystem,
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900297 efi + opt->SizeOfHeaders, 0);
298 } else {
299 /* Skip Certificates Table */
300 efi_image_region_add(regs,
AKASHI Takahiro52d7bfe2020-05-08 14:51:59 +0900301 &opt->Subsystem,
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900302 &opt->DataDirectory[ctidx], 0);
303 efi_image_region_add(regs,
304 &opt->DataDirectory[ctidx] + 1,
305 efi + opt->SizeOfHeaders, 0);
306 }
307
308 bytes_hashed = opt->SizeOfHeaders;
309 align = opt->FileAlignment;
310 authoff = opt->DataDirectory[ctidx].VirtualAddress;
311 authsz = opt->DataDirectory[ctidx].Size;
312 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
313 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
314
315 efi_image_region_add(regs, efi, &opt->CheckSum, 0);
AKASHI Takahiro52d7bfe2020-05-08 14:51:59 +0900316 efi_image_region_add(regs, &opt->Subsystem,
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900317 &opt->DataDirectory[ctidx], 0);
318 efi_image_region_add(regs, &opt->DataDirectory[ctidx] + 1,
319 efi + opt->SizeOfHeaders, 0);
320
321 bytes_hashed = opt->SizeOfHeaders;
322 align = opt->FileAlignment;
323 authoff = opt->DataDirectory[ctidx].VirtualAddress;
324 authsz = opt->DataDirectory[ctidx].Size;
325 } else {
326 debug("%s: Invalid optional header magic %x\n", __func__,
327 nt->OptionalHeader.Magic);
328 goto err;
329 }
330
331 /* 2. Sections */
332 num_sections = nt->FileHeader.NumberOfSections;
333 sections = (void *)((uint8_t *)&nt->OptionalHeader +
334 nt->FileHeader.SizeOfOptionalHeader);
335 sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections);
336 if (!sorted) {
337 debug("%s: Out of memory\n", __func__);
338 goto err;
339 }
340
341 /*
342 * Make sure the section list is in ascending order.
343 */
344 for (i = 0; i < num_sections; i++)
345 sorted[i] = &sections[i];
346 qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section);
347
348 for (i = 0; i < num_sections; i++) {
349 if (!sorted[i]->SizeOfRawData)
350 continue;
351
352 size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1);
353 efi_image_region_add(regs, efi + sorted[i]->PointerToRawData,
354 efi + sorted[i]->PointerToRawData + size,
355 0);
356 debug("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
357 i, sorted[i]->Name,
358 sorted[i]->PointerToRawData,
359 sorted[i]->PointerToRawData + size,
360 sorted[i]->VirtualAddress,
361 sorted[i]->VirtualAddress
362 + sorted[i]->Misc.VirtualSize);
363
364 bytes_hashed += size;
365 }
366 free(sorted);
367
368 /* 3. Extra data excluding Certificates Table */
369 if (bytes_hashed + authsz < len) {
370 debug("extra data for hash: %lu\n",
371 len - (bytes_hashed + authsz));
372 efi_image_region_add(regs, efi + bytes_hashed,
373 efi + len - authsz, 0);
374 }
375
376 /* Return Certificates Table */
377 if (authsz) {
378 if (len < authoff + authsz) {
379 debug("%s: Size for auth too large: %u >= %zu\n",
380 __func__, authsz, len - authoff);
381 goto err;
382 }
383 if (authsz < sizeof(*auth)) {
384 debug("%s: Size for auth too small: %u < %zu\n",
385 __func__, authsz, sizeof(*auth));
386 goto err;
387 }
388 *auth = efi + authoff;
389 *auth_len = authsz;
390 debug("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff, authsz);
391 } else {
392 *auth = NULL;
393 *auth_len = 0;
394 }
395
396 *regp = regs;
397
398 return true;
399
400err:
401 free(regs);
402
403 return false;
404}
405
406/**
407 * efi_image_unsigned_authenticate - authenticate unsigned image with
408 * SHA256 hash
409 * @regs: List of regions to be verified
410 *
411 * If an image is not signed, it doesn't have a signature. In this case,
412 * its message digest is calculated and it will be compared with one of
413 * hash values stored in signature databases.
414 *
415 * Return: true if authenticated, false if not
416 */
417static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs)
418{
419 struct efi_signature_store *db = NULL, *dbx = NULL;
420 bool ret = false;
421
422 dbx = efi_sigstore_parse_sigdb(L"dbx");
423 if (!dbx) {
424 debug("Getting signature database(dbx) failed\n");
425 goto out;
426 }
427
428 db = efi_sigstore_parse_sigdb(L"db");
429 if (!db) {
430 debug("Getting signature database(db) failed\n");
431 goto out;
432 }
433
434 /* try black-list first */
435 if (efi_signature_verify_with_sigdb(regs, NULL, dbx, NULL)) {
436 debug("Image is not signed and rejected by \"dbx\"\n");
437 goto out;
438 }
439
440 /* try white-list */
441 if (efi_signature_verify_with_sigdb(regs, NULL, db, NULL))
442 ret = true;
443 else
444 debug("Image is not signed and not found in \"db\" or \"dbx\"\n");
445
446out:
447 efi_sigstore_free(db);
448 efi_sigstore_free(dbx);
449
450 return ret;
451}
452
453/**
454 * efi_image_authenticate - verify a signature of signed image
455 * @efi: Pointer to image
456 * @efi_size: Size of @efi
457 *
458 * A signed image should have its signature stored in a table of its PE header.
459 * So if an image is signed and only if if its signature is verified using
460 * signature databases, an image is authenticated.
461 * If an image is not signed, its validity is checked by using
462 * efi_image_unsigned_authenticated().
463 * TODO:
464 * When AuditMode==0, if the image's signature is not found in
465 * the authorized database, or is found in the forbidden database,
466 * the image will not be started and instead, information about it
467 * will be placed in this table.
468 * When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created
469 * in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found
470 * in the certificate table of every image that is validated.
471 *
472 * Return: true if authenticated, false if not
473 */
474static bool efi_image_authenticate(void *efi, size_t efi_size)
475{
476 struct efi_image_regions *regs = NULL;
477 WIN_CERTIFICATE *wincerts = NULL, *wincert;
478 size_t wincerts_len;
479 struct pkcs7_message *msg = NULL;
480 struct efi_signature_store *db = NULL, *dbx = NULL;
481 struct x509_certificate *cert = NULL;
482 void *new_efi = NULL;
483 size_t new_efi_size;
484 bool ret = false;
485
486 if (!efi_secure_boot_enabled())
487 return true;
488
489 /*
490 * Size must be 8-byte aligned and the trailing bytes must be
491 * zero'ed. Otherwise hash value may be incorrect.
492 */
493 if (efi_size & 0x7) {
494 new_efi_size = (efi_size + 0x7) & ~0x7ULL;
495 new_efi = calloc(new_efi_size, 1);
496 if (!new_efi)
497 return false;
498 memcpy(new_efi, efi, efi_size);
499 efi = new_efi;
500 efi_size = new_efi_size;
501 }
502
503 if (!efi_image_parse(efi, efi_size, &regs, &wincerts,
504 &wincerts_len)) {
505 debug("Parsing PE executable image failed\n");
506 goto err;
507 }
508
509 if (!wincerts) {
510 /* The image is not signed */
511 ret = efi_image_unsigned_authenticate(regs);
512
513 goto err;
514 }
515
516 /*
517 * verify signature using db and dbx
518 */
519 db = efi_sigstore_parse_sigdb(L"db");
520 if (!db) {
521 debug("Getting signature database(db) failed\n");
522 goto err;
523 }
524
525 dbx = efi_sigstore_parse_sigdb(L"dbx");
526 if (!dbx) {
527 debug("Getting signature database(dbx) failed\n");
528 goto err;
529 }
530
531 /* go through WIN_CERTIFICATE list */
532 for (wincert = wincerts;
533 (void *)wincert < (void *)wincerts + wincerts_len;
534 wincert = (void *)wincert + ALIGN(wincert->dwLength, 8)) {
535 if (wincert->dwLength < sizeof(*wincert)) {
536 debug("%s: dwLength too small: %u < %zu\n",
537 __func__, wincert->dwLength, sizeof(*wincert));
538 goto err;
539 }
540 msg = pkcs7_parse_message((void *)wincert + sizeof(*wincert),
541 wincert->dwLength - sizeof(*wincert));
Patrick Wildt6f146152020-05-07 02:17:14 +0200542 if (IS_ERR(msg)) {
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900543 debug("Parsing image's signature failed\n");
Patrick Wildt6f146152020-05-07 02:17:14 +0200544 msg = NULL;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900545 goto err;
546 }
547
548 /* try black-list first */
549 if (efi_signature_verify_with_sigdb(regs, msg, dbx, NULL)) {
550 debug("Signature was rejected by \"dbx\"\n");
551 goto err;
552 }
553
554 if (!efi_signature_verify_signers(msg, dbx)) {
555 debug("Signer was rejected by \"dbx\"\n");
556 goto err;
557 } else {
558 ret = true;
559 }
560
561 /* try white-list */
562 if (!efi_signature_verify_with_sigdb(regs, msg, db, &cert)) {
563 debug("Verifying signature with \"db\" failed\n");
564 goto err;
565 } else {
566 ret = true;
567 }
568
569 if (!efi_signature_verify_cert(cert, dbx)) {
570 debug("Certificate was rejected by \"dbx\"\n");
571 goto err;
572 } else {
573 ret = true;
574 }
575 }
576
577err:
578 x509_free_certificate(cert);
579 efi_sigstore_free(db);
580 efi_sigstore_free(dbx);
581 pkcs7_free_message(msg);
582 free(regs);
583 free(new_efi);
584
585 return ret;
586}
587#else
588static bool efi_image_authenticate(void *efi, size_t efi_size)
589{
590 return true;
591}
592#endif /* CONFIG_EFI_SECURE_BOOT */
593
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100594/**
595 * efi_load_pe() - relocate EFI binary
596 *
Alexander Grafcb149c62016-03-04 01:09:58 +0100597 * This function loads all sections from a PE binary into a newly reserved
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100598 * piece of memory. On success the entry point is returned as handle->entry.
599 *
600 * @handle: loaded image handle
601 * @efi: pointer to the EFI binary
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900602 * @efi_size: size of @efi binary
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100603 * @loaded_image_info: loaded image protocol
604 * Return: status code
Alexander Grafcb149c62016-03-04 01:09:58 +0100605 */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900606efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
607 void *efi, size_t efi_size,
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100608 struct efi_loaded_image *loaded_image_info)
Alexander Grafcb149c62016-03-04 01:09:58 +0100609{
610 IMAGE_NT_HEADERS32 *nt;
611 IMAGE_DOS_HEADER *dos;
612 IMAGE_SECTION_HEADER *sections;
613 int num_sections;
614 void *efi_reloc;
615 int i;
616 const IMAGE_BASE_RELOCATION *rel;
617 unsigned long rel_size;
618 int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700619 uint64_t image_base;
Alexander Grafcb149c62016-03-04 01:09:58 +0100620 unsigned long virt_size = 0;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700621 int supported = 0;
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900622 efi_status_t ret;
623
624 /* Sanity check for a file header */
625 if (efi_size < sizeof(*dos)) {
626 printf("%s: Truncated DOS Header\n", __func__);
627 ret = EFI_LOAD_ERROR;
628 goto err;
629 }
Alexander Grafcb149c62016-03-04 01:09:58 +0100630
631 dos = efi;
632 if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
633 printf("%s: Invalid DOS Signature\n", __func__);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900634 ret = EFI_LOAD_ERROR;
635 goto err;
636 }
637
638 /* assume sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64) */
639 if (efi_size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS32)) {
640 printf("%s: Invalid offset for Extended Header\n", __func__);
641 ret = EFI_LOAD_ERROR;
642 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100643 }
644
645 nt = (void *) ((char *)efi + dos->e_lfanew);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900646 if ((nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&
647 (efi_size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS64))) {
648 printf("%s: Invalid offset for Extended Header\n", __func__);
649 ret = EFI_LOAD_ERROR;
650 goto err;
651 }
652
Alexander Grafcb149c62016-03-04 01:09:58 +0100653 if (nt->Signature != IMAGE_NT_SIGNATURE) {
654 printf("%s: Invalid NT Signature\n", __func__);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900655 ret = EFI_LOAD_ERROR;
656 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100657 }
658
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700659 for (i = 0; machines[i]; i++)
660 if (machines[i] == nt->FileHeader.Machine) {
661 supported = 1;
662 break;
663 }
664
665 if (!supported) {
666 printf("%s: Machine type 0x%04x is not supported\n",
667 __func__, nt->FileHeader.Machine);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900668 ret = EFI_LOAD_ERROR;
669 goto err;
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700670 }
671
Alexander Grafcb149c62016-03-04 01:09:58 +0100672 num_sections = nt->FileHeader.NumberOfSections;
673 sections = (void *)&nt->OptionalHeader +
674 nt->FileHeader.SizeOfOptionalHeader;
675
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900676 if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections
677 - efi)) {
678 printf("%s: Invalid number of sections: %d\n",
679 __func__, num_sections);
680 ret = EFI_LOAD_ERROR;
681 goto err;
682 }
683
684 /* Authenticate an image */
685 if (efi_image_authenticate(efi, efi_size))
686 handle->auth_status = EFI_IMAGE_AUTH_PASSED;
687 else
688 handle->auth_status = EFI_IMAGE_AUTH_FAILED;
689
690 /* Calculate upper virtual address boundary */
Alexander Grafcb149c62016-03-04 01:09:58 +0100691 for (i = num_sections - 1; i >= 0; i--) {
692 IMAGE_SECTION_HEADER *sec = &sections[i];
693 virt_size = max_t(unsigned long, virt_size,
694 sec->VirtualAddress + sec->Misc.VirtualSize);
695 }
696
697 /* Read 32/64bit specific header bits */
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700698 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100699 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
700 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700701 image_base = opt->ImageBase;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100702 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +0200703 handle->image_type = opt->Subsystem;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100704 efi_reloc = efi_alloc(virt_size,
705 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100706 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100707 printf("%s: Could not allocate %lu bytes\n",
708 __func__, virt_size);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900709 ret = EFI_OUT_OF_RESOURCES;
710 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100711 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100712 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100713 rel_size = opt->DataDirectory[rel_idx].Size;
714 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200715 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Ivan Gorinov61a5ced2018-04-05 18:32:06 -0700716 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Alexander Grafcb149c62016-03-04 01:09:58 +0100717 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700718 image_base = opt->ImageBase;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100719 efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +0200720 handle->image_type = opt->Subsystem;
Heinrich Schuchardt36b41a32018-01-19 20:24:41 +0100721 efi_reloc = efi_alloc(virt_size,
722 loaded_image_info->image_code_type);
Alexander Grafcb149c62016-03-04 01:09:58 +0100723 if (!efi_reloc) {
Heinrich Schuchardte540c482017-12-22 19:16:57 +0100724 printf("%s: Could not allocate %lu bytes\n",
725 __func__, virt_size);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900726 ret = EFI_OUT_OF_RESOURCES;
727 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100728 }
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100729 handle->entry = efi_reloc + opt->AddressOfEntryPoint;
Alexander Grafcb149c62016-03-04 01:09:58 +0100730 rel_size = opt->DataDirectory[rel_idx].Size;
731 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
Heinrich Schuchardt82786752018-04-03 22:29:32 +0200732 virt_size = ALIGN(virt_size, opt->SectionAlignment);
Alexander Grafcb149c62016-03-04 01:09:58 +0100733 } else {
734 printf("%s: Invalid optional header magic %x\n", __func__,
735 nt->OptionalHeader.Magic);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900736 ret = EFI_LOAD_ERROR;
737 goto err;
Alexander Grafcb149c62016-03-04 01:09:58 +0100738 }
739
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700740 /* Copy PE headers */
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900741 memcpy(efi_reloc, efi,
742 sizeof(*dos)
743 + sizeof(*nt)
744 + nt->FileHeader.SizeOfOptionalHeader
745 + num_sections * sizeof(IMAGE_SECTION_HEADER));
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700746
Alexander Grafcb149c62016-03-04 01:09:58 +0100747 /* Load sections into RAM */
748 for (i = num_sections - 1; i >= 0; i--) {
749 IMAGE_SECTION_HEADER *sec = &sections[i];
750 memset(efi_reloc + sec->VirtualAddress, 0,
751 sec->Misc.VirtualSize);
752 memcpy(efi_reloc + sec->VirtualAddress,
753 efi + sec->PointerToRawData,
754 sec->SizeOfRawData);
755 }
756
757 /* Run through relocations */
Ivan Gorinove2dc4222018-05-02 16:36:02 -0700758 if (efi_loader_relocate(rel, rel_size, efi_reloc,
759 (unsigned long)image_base) != EFI_SUCCESS) {
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200760 efi_free_pages((uintptr_t) efi_reloc,
761 (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900762 ret = EFI_LOAD_ERROR;
763 goto err;
xypron.glpk@gmx.deda684a62017-07-04 00:12:58 +0200764 }
Alexander Grafcb149c62016-03-04 01:09:58 +0100765
766 /* Flush cache */
Simon Glassd0d90992016-11-07 08:47:04 -0700767 flush_cache((ulong)efi_reloc,
Alexander Graf89aea432018-04-23 07:59:47 +0200768 ALIGN(virt_size, EFI_CACHELINE_SIZE));
Alexander Grafcb149c62016-03-04 01:09:58 +0100769 invalidate_icache_all();
770
771 /* Populate the loaded image interface bits */
AKASHI Takahiro8458bf62018-10-11 04:09:58 -0700772 loaded_image_info->image_base = efi_reloc;
773 loaded_image_info->image_size = virt_size;
Alexander Grafcb149c62016-03-04 01:09:58 +0100774
AKASHI Takahiro4540dab2020-04-14 11:51:44 +0900775 if (handle->auth_status == EFI_IMAGE_AUTH_PASSED)
776 return EFI_SUCCESS;
777 else
778 return EFI_SECURITY_VIOLATION;
779
780err:
781 return ret;
Alexander Grafcb149c62016-03-04 01:09:58 +0100782}