Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI image loader |
| 4 | * |
| 5 | * based partly on wine code |
| 6 | * |
| 7 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 10 | #define LOG_CATEGORY LOGC_EFI |
| 11 | |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 14 | #include <log.h> |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 16 | #include <pe.h> |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 17 | #include <sort.h> |
AKASHI Takahiro | e669c2d | 2022-07-05 14:48:14 +0900 | [diff] [blame] | 18 | #include <crypto/mscode.h> |
Heinrich Schuchardt | 2aa0942 | 2020-05-07 17:57:43 +0200 | [diff] [blame] | 19 | #include <crypto/pkcs7_parser.h> |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 20 | #include <linux/err.h> |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 21 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 22 | const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID; |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 23 | const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID; |
| 24 | const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
| 25 | const efi_guid_t efi_guid_loaded_image_device_path = |
| 26 | EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 27 | const efi_guid_t efi_simple_file_system_protocol_guid = |
| 28 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
| 29 | const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 30 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 31 | static int machines[] = { |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 32 | #if defined(__aarch64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 33 | IMAGE_FILE_MACHINE_ARM64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 34 | #elif defined(__arm__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 35 | IMAGE_FILE_MACHINE_ARM, |
| 36 | IMAGE_FILE_MACHINE_THUMB, |
| 37 | IMAGE_FILE_MACHINE_ARMNT, |
| 38 | #endif |
| 39 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 40 | #if defined(__x86_64__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 41 | IMAGE_FILE_MACHINE_AMD64, |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 42 | #elif defined(__i386__) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 43 | IMAGE_FILE_MACHINE_I386, |
| 44 | #endif |
| 45 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 46 | #if defined(__riscv) && (__riscv_xlen == 32) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 47 | IMAGE_FILE_MACHINE_RISCV32, |
| 48 | #endif |
| 49 | |
Alexander Graf | 8aec308 | 2018-06-18 17:22:57 +0200 | [diff] [blame] | 50 | #if defined(__riscv) && (__riscv_xlen == 64) |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 51 | IMAGE_FILE_MACHINE_RISCV64, |
| 52 | #endif |
| 53 | 0 }; |
| 54 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 55 | /** |
| 56 | * efi_print_image_info() - print information about a loaded image |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 57 | * |
| 58 | * If the program counter is located within the image the offset to the base |
| 59 | * address is shown. |
| 60 | * |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 61 | * @obj: EFI object |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 62 | * @image: loaded image |
| 63 | * @pc: program counter (use NULL to suppress offset output) |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 64 | * Return: status code |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 65 | */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 66 | static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj, |
| 67 | struct efi_loaded_image *image, |
| 68 | void *pc) |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 69 | { |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 70 | printf("UEFI image"); |
| 71 | printf(" [0x%p:0x%p]", |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 72 | image->image_base, image->image_base + image->image_size - 1); |
| 73 | if (pc && pc >= image->image_base && |
| 74 | pc < image->image_base + image->image_size) |
| 75 | printf(" pc=0x%zx", pc - image->image_base); |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 76 | if (image->file_path) |
| 77 | printf(" '%pD'", image->file_path); |
| 78 | printf("\n"); |
| 79 | return EFI_SUCCESS; |
| 80 | } |
| 81 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 82 | /** |
| 83 | * efi_print_image_infos() - print information about all loaded images |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 84 | * |
| 85 | * @pc: program counter (use NULL to suppress offset output) |
| 86 | */ |
| 87 | void efi_print_image_infos(void *pc) |
| 88 | { |
| 89 | struct efi_object *efiobj; |
| 90 | struct efi_handler *handler; |
| 91 | |
| 92 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 93 | list_for_each_entry(handler, &efiobj->protocols, link) { |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 94 | if (!guidcmp(&handler->guid, &efi_guid_loaded_image)) { |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 95 | efi_print_image_info( |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 96 | (struct efi_loaded_image_obj *)efiobj, |
Heinrich Schuchardt | 83d96b7 | 2018-04-05 11:56:21 +0200 | [diff] [blame] | 97 | handler->protocol_interface, pc); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 103 | /** |
| 104 | * efi_loader_relocate() - relocate UEFI binary |
| 105 | * |
| 106 | * @rel: pointer to the relocation table |
| 107 | * @rel_size: size of the relocation table in bytes |
| 108 | * @efi_reloc: actual load address of the image |
| 109 | * @pref_address: preferred load address of the image |
| 110 | * Return: status code |
| 111 | */ |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 112 | static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 113 | unsigned long rel_size, void *efi_reloc, |
| 114 | unsigned long pref_address) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 115 | { |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 116 | unsigned long delta = (unsigned long)efi_reloc - pref_address; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 117 | const IMAGE_BASE_RELOCATION *end; |
| 118 | int i; |
| 119 | |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 120 | if (delta == 0) |
| 121 | return EFI_SUCCESS; |
| 122 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 123 | end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); |
Heinrich Schuchardt | e9644cc | 2019-02-16 15:36:33 +0100 | [diff] [blame] | 124 | while (rel < end && rel->SizeOfBlock) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 125 | const uint16_t *relocs = (const uint16_t *)(rel + 1); |
| 126 | i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); |
| 127 | while (i--) { |
Alexander Graf | 0703225 | 2016-08-18 23:45:18 +0200 | [diff] [blame] | 128 | uint32_t offset = (uint32_t)(*relocs & 0xfff) + |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 129 | rel->VirtualAddress; |
| 130 | int type = *relocs >> EFI_PAGE_SHIFT; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 131 | uint64_t *x64 = efi_reloc + offset; |
| 132 | uint32_t *x32 = efi_reloc + offset; |
| 133 | uint16_t *x16 = efi_reloc + offset; |
| 134 | |
| 135 | switch (type) { |
| 136 | case IMAGE_REL_BASED_ABSOLUTE: |
| 137 | break; |
| 138 | case IMAGE_REL_BASED_HIGH: |
| 139 | *x16 += ((uint32_t)delta) >> 16; |
| 140 | break; |
| 141 | case IMAGE_REL_BASED_LOW: |
| 142 | *x16 += (uint16_t)delta; |
| 143 | break; |
| 144 | case IMAGE_REL_BASED_HIGHLOW: |
| 145 | *x32 += (uint32_t)delta; |
| 146 | break; |
| 147 | case IMAGE_REL_BASED_DIR64: |
| 148 | *x64 += (uint64_t)delta; |
| 149 | break; |
Alexander Graf | 71f9120 | 2018-06-05 19:20:32 +0200 | [diff] [blame] | 150 | #ifdef __riscv |
| 151 | case IMAGE_REL_BASED_RISCV_HI20: |
| 152 | *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) | |
| 153 | (*x32 & 0x00000fff); |
| 154 | break; |
| 155 | case IMAGE_REL_BASED_RISCV_LOW12I: |
| 156 | case IMAGE_REL_BASED_RISCV_LOW12S: |
| 157 | /* We know that we're 4k aligned */ |
| 158 | if (delta & 0xfff) { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 159 | log_err("Unsupported reloc offset\n"); |
Alexander Graf | 71f9120 | 2018-06-05 19:20:32 +0200 | [diff] [blame] | 160 | return EFI_LOAD_ERROR; |
| 161 | } |
| 162 | break; |
| 163 | #endif |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 164 | default: |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 165 | log_err("Unknown Relocation off %x type %x\n", |
| 166 | offset, type); |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 167 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 168 | } |
| 169 | relocs++; |
| 170 | } |
| 171 | rel = (const IMAGE_BASE_RELOCATION *)relocs; |
| 172 | } |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 173 | return EFI_SUCCESS; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void __weak invalidate_icache_all(void) |
| 177 | { |
| 178 | /* If the system doesn't support icache_all flush, cross our fingers */ |
| 179 | } |
| 180 | |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 181 | /** |
| 182 | * efi_set_code_and_data_type() - determine the memory types to be used for code |
| 183 | * and data. |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 184 | * |
Heinrich Schuchardt | 0fcdb7a | 2019-02-16 15:22:13 +0100 | [diff] [blame] | 185 | * @loaded_image_info: image descriptor |
| 186 | * @image_type: field Subsystem of the optional header for |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 187 | * Windows specific field |
| 188 | */ |
| 189 | static void efi_set_code_and_data_type( |
| 190 | struct efi_loaded_image *loaded_image_info, |
| 191 | uint16_t image_type) |
| 192 | { |
| 193 | switch (image_type) { |
| 194 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 195 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 196 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 197 | break; |
| 198 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 199 | loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE; |
| 200 | loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA; |
| 201 | break; |
| 202 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
Heinrich Schuchardt | 467fd7a | 2018-01-31 18:45:35 +0000 | [diff] [blame] | 203 | case IMAGE_SUBSYSTEM_EFI_ROM: |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 204 | loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE; |
| 205 | loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA; |
| 206 | break; |
| 207 | default: |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 208 | log_err("invalid image type: %u\n", image_type); |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 209 | /* Let's assume it is an application */ |
| 210 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
| 211 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 216 | /** |
| 217 | * efi_image_region_add() - add an entry of region |
| 218 | * @regs: Pointer to array of regions |
| 219 | * @start: Start address of region (included) |
| 220 | * @end: End address of region (excluded) |
| 221 | * @nocheck: flag against overlapped regions |
| 222 | * |
Heinrich Schuchardt | 0ab0037 | 2021-06-09 00:21:24 +0200 | [diff] [blame] | 223 | * Take one entry of region \[@start, @end\[ and insert it into the list. |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 224 | * |
| 225 | * * If @nocheck is false, the list will be sorted ascending by address. |
| 226 | * Overlapping entries will not be allowed. |
| 227 | * |
| 228 | * * If @nocheck is true, the list will be sorted ascending by sequence |
| 229 | * of adding the entries. Overlapping is allowed. |
| 230 | * |
| 231 | * Return: status code |
| 232 | */ |
| 233 | efi_status_t efi_image_region_add(struct efi_image_regions *regs, |
| 234 | const void *start, const void *end, |
| 235 | int nocheck) |
| 236 | { |
| 237 | struct image_region *reg; |
| 238 | int i, j; |
| 239 | |
| 240 | if (regs->num >= regs->max) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 241 | log_err("%s: no more room for regions\n", __func__); |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 242 | return EFI_OUT_OF_RESOURCES; |
| 243 | } |
| 244 | |
| 245 | if (end < start) |
| 246 | return EFI_INVALID_PARAMETER; |
| 247 | |
| 248 | for (i = 0; i < regs->num; i++) { |
| 249 | reg = ®s->reg[i]; |
| 250 | if (nocheck) |
| 251 | continue; |
| 252 | |
| 253 | /* new data after registered region */ |
| 254 | if (start >= reg->data + reg->size) |
| 255 | continue; |
| 256 | |
| 257 | /* new data preceding registered region */ |
| 258 | if (end <= reg->data) { |
| 259 | for (j = regs->num - 1; j >= i; j--) |
| 260 | memcpy(®s->reg[j + 1], ®s->reg[j], |
| 261 | sizeof(*reg)); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | /* new data overlapping registered region */ |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 266 | log_err("%s: new region already part of another\n", __func__); |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 267 | return EFI_INVALID_PARAMETER; |
| 268 | } |
| 269 | |
| 270 | reg = ®s->reg[i]; |
| 271 | reg->data = start; |
| 272 | reg->size = end - start; |
| 273 | regs->num++; |
| 274 | |
| 275 | return EFI_SUCCESS; |
| 276 | } |
| 277 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 278 | /** |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 279 | * cmp_pe_section() - compare virtual addresses of two PE image sections |
| 280 | * @arg1: pointer to pointer to first section header |
| 281 | * @arg2: pointer to pointer to second section header |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 282 | * |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 283 | * Compare the virtual addresses of two sections of an portable executable. |
| 284 | * The arguments are defined as const void * to allow usage with qsort(). |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 285 | * |
Heinrich Schuchardt | 72bc714 | 2020-05-30 06:44:48 +0200 | [diff] [blame] | 286 | * Return: -1 if the virtual address of arg1 is less than that of arg2, |
| 287 | * 0 if the virtual addresses are equal, 1 if the virtual address |
| 288 | * of arg1 is greater than that of arg2. |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 289 | */ |
| 290 | static int cmp_pe_section(const void *arg1, const void *arg2) |
| 291 | { |
| 292 | const IMAGE_SECTION_HEADER *section1, *section2; |
| 293 | |
| 294 | section1 = *((const IMAGE_SECTION_HEADER **)arg1); |
| 295 | section2 = *((const IMAGE_SECTION_HEADER **)arg2); |
| 296 | |
| 297 | if (section1->VirtualAddress < section2->VirtualAddress) |
| 298 | return -1; |
| 299 | else if (section1->VirtualAddress == section2->VirtualAddress) |
| 300 | return 0; |
| 301 | else |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | /** |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 306 | * efi_prepare_aligned_image() - prepare 8-byte aligned image |
| 307 | * @efi: pointer to the EFI binary |
| 308 | * @efi_size: size of @efi binary |
| 309 | * |
| 310 | * If @efi is not 8-byte aligned, this function newly allocates |
| 311 | * the image buffer. |
| 312 | * |
| 313 | * Return: valid pointer to a image, return NULL if allocation fails. |
| 314 | */ |
| 315 | void *efi_prepare_aligned_image(void *efi, u64 *efi_size) |
| 316 | { |
| 317 | size_t new_efi_size; |
| 318 | void *new_efi; |
| 319 | |
| 320 | /* |
| 321 | * Size must be 8-byte aligned and the trailing bytes must be |
| 322 | * zero'ed. Otherwise hash value may be incorrect. |
| 323 | */ |
| 324 | if (!IS_ALIGNED(*efi_size, 8)) { |
| 325 | new_efi_size = ALIGN(*efi_size, 8); |
| 326 | new_efi = calloc(new_efi_size, 1); |
| 327 | if (!new_efi) |
| 328 | return NULL; |
| 329 | memcpy(new_efi, efi, *efi_size); |
| 330 | *efi_size = new_efi_size; |
| 331 | return new_efi; |
| 332 | } else { |
| 333 | return efi; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
Heinrich Schuchardt | da6d607 | 2020-05-30 05:48:08 +0200 | [diff] [blame] | 338 | * efi_image_parse() - parse a PE image |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 339 | * @efi: Pointer to image |
| 340 | * @len: Size of @efi |
| 341 | * @regp: Pointer to a list of regions |
| 342 | * @auth: Pointer to a pointer to authentication data in PE |
| 343 | * @auth_len: Size of @auth |
| 344 | * |
| 345 | * Parse image binary in PE32(+) format, assuming that sanity of PE image |
| 346 | * has been checked by a caller. |
| 347 | * On success, an address of authentication data in @efi and its size will |
| 348 | * be returned in @auth and @auth_len, respectively. |
| 349 | * |
| 350 | * Return: true on success, false on error |
| 351 | */ |
| 352 | bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp, |
| 353 | WIN_CERTIFICATE **auth, size_t *auth_len) |
| 354 | { |
| 355 | struct efi_image_regions *regs; |
| 356 | IMAGE_DOS_HEADER *dos; |
| 357 | IMAGE_NT_HEADERS32 *nt; |
| 358 | IMAGE_SECTION_HEADER *sections, **sorted; |
| 359 | int num_regions, num_sections, i; |
| 360 | int ctidx = IMAGE_DIRECTORY_ENTRY_SECURITY; |
| 361 | u32 align, size, authsz, authoff; |
| 362 | size_t bytes_hashed; |
| 363 | |
| 364 | dos = (void *)efi; |
| 365 | nt = (void *)(efi + dos->e_lfanew); |
AKASHI Takahiro | 0d5eba4 | 2020-07-08 14:01:53 +0900 | [diff] [blame] | 366 | authoff = 0; |
| 367 | authsz = 0; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * Count maximum number of regions to be digested. |
| 371 | * We don't have to have an exact number here. |
| 372 | * See efi_image_region_add()'s in parsing below. |
| 373 | */ |
| 374 | num_regions = 3; /* for header */ |
| 375 | num_regions += nt->FileHeader.NumberOfSections; |
| 376 | num_regions++; /* for extra */ |
| 377 | |
| 378 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * num_regions, |
| 379 | 1); |
| 380 | if (!regs) |
| 381 | goto err; |
| 382 | regs->max = num_regions; |
| 383 | |
| 384 | /* |
| 385 | * Collect data regions for hash calculation |
| 386 | * 1. File headers |
| 387 | */ |
| 388 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
| 389 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
| 390 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
| 391 | |
| 392 | /* Skip CheckSum */ |
| 393 | efi_image_region_add(regs, efi, &opt->CheckSum, 0); |
| 394 | if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { |
| 395 | efi_image_region_add(regs, |
AKASHI Takahiro | b69546f | 2020-05-08 14:51:59 +0900 | [diff] [blame] | 396 | &opt->Subsystem, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 397 | efi + opt->SizeOfHeaders, 0); |
| 398 | } else { |
| 399 | /* Skip Certificates Table */ |
| 400 | efi_image_region_add(regs, |
AKASHI Takahiro | b69546f | 2020-05-08 14:51:59 +0900 | [diff] [blame] | 401 | &opt->Subsystem, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 402 | &opt->DataDirectory[ctidx], 0); |
| 403 | efi_image_region_add(regs, |
| 404 | &opt->DataDirectory[ctidx] + 1, |
| 405 | efi + opt->SizeOfHeaders, 0); |
AKASHI Takahiro | 0d5eba4 | 2020-07-08 14:01:53 +0900 | [diff] [blame] | 406 | |
| 407 | authoff = opt->DataDirectory[ctidx].VirtualAddress; |
| 408 | authsz = opt->DataDirectory[ctidx].Size; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bytes_hashed = opt->SizeOfHeaders; |
| 412 | align = opt->FileAlignment; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 413 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 414 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
| 415 | |
AKASHI Takahiro | 0d5eba4 | 2020-07-08 14:01:53 +0900 | [diff] [blame] | 416 | /* Skip CheckSum */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 417 | efi_image_region_add(regs, efi, &opt->CheckSum, 0); |
AKASHI Takahiro | 0d5eba4 | 2020-07-08 14:01:53 +0900 | [diff] [blame] | 418 | if (nt->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { |
| 419 | efi_image_region_add(regs, |
| 420 | &opt->Subsystem, |
| 421 | efi + opt->SizeOfHeaders, 0); |
| 422 | } else { |
| 423 | /* Skip Certificates Table */ |
| 424 | efi_image_region_add(regs, &opt->Subsystem, |
| 425 | &opt->DataDirectory[ctidx], 0); |
| 426 | efi_image_region_add(regs, |
| 427 | &opt->DataDirectory[ctidx] + 1, |
| 428 | efi + opt->SizeOfHeaders, 0); |
| 429 | |
| 430 | authoff = opt->DataDirectory[ctidx].VirtualAddress; |
| 431 | authsz = opt->DataDirectory[ctidx].Size; |
| 432 | } |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 433 | |
| 434 | bytes_hashed = opt->SizeOfHeaders; |
| 435 | align = opt->FileAlignment; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 436 | } else { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 437 | log_err("%s: Invalid optional header magic %x\n", __func__, |
| 438 | nt->OptionalHeader.Magic); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 439 | goto err; |
| 440 | } |
| 441 | |
| 442 | /* 2. Sections */ |
| 443 | num_sections = nt->FileHeader.NumberOfSections; |
| 444 | sections = (void *)((uint8_t *)&nt->OptionalHeader + |
| 445 | nt->FileHeader.SizeOfOptionalHeader); |
| 446 | sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections); |
| 447 | if (!sorted) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 448 | log_err("%s: Out of memory\n", __func__); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 449 | goto err; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Make sure the section list is in ascending order. |
| 454 | */ |
| 455 | for (i = 0; i < num_sections; i++) |
| 456 | sorted[i] = §ions[i]; |
| 457 | qsort(sorted, num_sections, sizeof(sorted[0]), cmp_pe_section); |
| 458 | |
| 459 | for (i = 0; i < num_sections; i++) { |
| 460 | if (!sorted[i]->SizeOfRawData) |
| 461 | continue; |
| 462 | |
| 463 | size = (sorted[i]->SizeOfRawData + align - 1) & ~(align - 1); |
| 464 | efi_image_region_add(regs, efi + sorted[i]->PointerToRawData, |
| 465 | efi + sorted[i]->PointerToRawData + size, |
| 466 | 0); |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 467 | log_debug("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n", |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame] | 468 | i, sorted[i]->Name, |
| 469 | sorted[i]->PointerToRawData, |
| 470 | sorted[i]->PointerToRawData + size, |
| 471 | sorted[i]->VirtualAddress, |
| 472 | sorted[i]->VirtualAddress |
| 473 | + sorted[i]->Misc.VirtualSize); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 474 | |
| 475 | bytes_hashed += size; |
| 476 | } |
| 477 | free(sorted); |
| 478 | |
| 479 | /* 3. Extra data excluding Certificates Table */ |
| 480 | if (bytes_hashed + authsz < len) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 481 | log_debug("extra data for hash: %zu\n", |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame] | 482 | len - (bytes_hashed + authsz)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 483 | efi_image_region_add(regs, efi + bytes_hashed, |
| 484 | efi + len - authsz, 0); |
| 485 | } |
| 486 | |
| 487 | /* Return Certificates Table */ |
| 488 | if (authsz) { |
| 489 | if (len < authoff + authsz) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 490 | log_err("%s: Size for auth too large: %u >= %zu\n", |
| 491 | __func__, authsz, len - authoff); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 492 | goto err; |
| 493 | } |
| 494 | if (authsz < sizeof(*auth)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 495 | log_err("%s: Size for auth too small: %u < %zu\n", |
| 496 | __func__, authsz, sizeof(*auth)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 497 | goto err; |
| 498 | } |
| 499 | *auth = efi + authoff; |
| 500 | *auth_len = authsz; |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 501 | log_debug("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff, |
AKASHI Takahiro | cb0b411 | 2020-06-09 14:09:35 +0900 | [diff] [blame] | 502 | authsz); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 503 | } else { |
| 504 | *auth = NULL; |
| 505 | *auth_len = 0; |
| 506 | } |
| 507 | |
| 508 | *regp = regs; |
| 509 | |
| 510 | return true; |
| 511 | |
| 512 | err: |
| 513 | free(regs); |
| 514 | |
| 515 | return false; |
| 516 | } |
| 517 | |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 518 | #ifdef CONFIG_EFI_SECURE_BOOT |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 519 | /** |
AKASHI Takahiro | e669c2d | 2022-07-05 14:48:14 +0900 | [diff] [blame] | 520 | * efi_image_verify_digest - verify image's message digest |
| 521 | * @regs: Array of memory regions to digest |
| 522 | * @msg: Signature in pkcs7 structure |
| 523 | * |
| 524 | * @regs contains all the data in a PE image to digest. Calculate |
| 525 | * a hash value based on @regs and compare it with a messaged digest |
| 526 | * in the content (SpcPeImageData) of @msg's contentInfo. |
| 527 | * |
| 528 | * Return: true if verified, false if not |
| 529 | */ |
| 530 | static bool efi_image_verify_digest(struct efi_image_regions *regs, |
| 531 | struct pkcs7_message *msg) |
| 532 | { |
| 533 | struct pefile_context ctx; |
| 534 | void *hash; |
| 535 | int hash_len, ret; |
| 536 | |
| 537 | const void *data; |
| 538 | size_t data_len; |
| 539 | size_t asn1hdrlen; |
| 540 | |
| 541 | /* get pkcs7's contentInfo */ |
| 542 | ret = pkcs7_get_content_data(msg, &data, &data_len, &asn1hdrlen); |
| 543 | if (ret < 0 || !data) |
| 544 | return false; |
| 545 | |
| 546 | /* parse data and retrieve a message digest into ctx */ |
| 547 | ret = mscode_parse(&ctx, data, data_len, asn1hdrlen); |
| 548 | if (ret < 0) |
| 549 | return false; |
| 550 | |
| 551 | /* calculate a hash value of PE image */ |
| 552 | hash = NULL; |
| 553 | if (!efi_hash_regions(regs->reg, regs->num, &hash, ctx.digest_algo, |
| 554 | &hash_len)) |
| 555 | return false; |
| 556 | |
| 557 | /* match the digest */ |
| 558 | if (ctx.digest_len != hash_len || memcmp(ctx.digest, hash, hash_len)) |
| 559 | return false; |
| 560 | |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | /** |
Heinrich Schuchardt | da6d607 | 2020-05-30 05:48:08 +0200 | [diff] [blame] | 565 | * efi_image_authenticate() - verify a signature of signed image |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 566 | * @efi: Pointer to image |
| 567 | * @efi_size: Size of @efi |
| 568 | * |
| 569 | * A signed image should have its signature stored in a table of its PE header. |
| 570 | * So if an image is signed and only if if its signature is verified using |
| 571 | * signature databases, an image is authenticated. |
| 572 | * If an image is not signed, its validity is checked by using |
| 573 | * efi_image_unsigned_authenticated(). |
| 574 | * TODO: |
| 575 | * When AuditMode==0, if the image's signature is not found in |
| 576 | * the authorized database, or is found in the forbidden database, |
| 577 | * the image will not be started and instead, information about it |
| 578 | * will be placed in this table. |
| 579 | * When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created |
| 580 | * in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found |
| 581 | * in the certificate table of every image that is validated. |
| 582 | * |
| 583 | * Return: true if authenticated, false if not |
| 584 | */ |
| 585 | static bool efi_image_authenticate(void *efi, size_t efi_size) |
| 586 | { |
| 587 | struct efi_image_regions *regs = NULL; |
| 588 | WIN_CERTIFICATE *wincerts = NULL, *wincert; |
| 589 | size_t wincerts_len; |
| 590 | struct pkcs7_message *msg = NULL; |
| 591 | struct efi_signature_store *db = NULL, *dbx = NULL; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 592 | void *new_efi = NULL; |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 593 | u8 *auth, *wincerts_end; |
Dan Carpenter | 0c5ff7e | 2023-07-26 09:54:52 +0300 | [diff] [blame] | 594 | u64 new_efi_size = efi_size; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 595 | size_t auth_size; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 596 | bool ret = false; |
| 597 | |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 598 | log_debug("%s: Enter, %d\n", __func__, ret); |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 599 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 600 | if (!efi_secure_boot_enabled()) |
| 601 | return true; |
| 602 | |
Dan Carpenter | 0c5ff7e | 2023-07-26 09:54:52 +0300 | [diff] [blame] | 603 | new_efi = efi_prepare_aligned_image(efi, &new_efi_size); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 604 | if (!new_efi) |
| 605 | return false; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 606 | |
Dan Carpenter | 0c5ff7e | 2023-07-26 09:54:52 +0300 | [diff] [blame] | 607 | if (!efi_image_parse(new_efi, new_efi_size, ®s, &wincerts, |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 608 | &wincerts_len)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 609 | log_err("Parsing PE executable image failed\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 610 | goto out; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* |
| 614 | * verify signature using db and dbx |
| 615 | */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 616 | db = efi_sigstore_parse_sigdb(u"db"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 617 | if (!db) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 618 | log_err("Getting signature database(db) failed\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 619 | goto out; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 620 | } |
| 621 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 622 | dbx = efi_sigstore_parse_sigdb(u"dbx"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 623 | if (!dbx) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 624 | log_err("Getting signature database(dbx) failed\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 625 | goto out; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 626 | } |
| 627 | |
Ilias Apalodimas | 71eae98 | 2022-01-29 00:20:31 +0200 | [diff] [blame] | 628 | if (efi_signature_lookup_digest(regs, dbx, true)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 629 | log_debug("Image's digest was found in \"dbx\"\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 630 | goto out; |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 631 | } |
| 632 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 633 | /* |
| 634 | * go through WIN_CERTIFICATE list |
| 635 | * NOTE: |
| 636 | * We may have multiple signatures either as WIN_CERTIFICATE's |
| 637 | * in PE header, or as pkcs7 SignerInfo's in SignedData. |
| 638 | * So the verification policy here is: |
| 639 | * - Success if, at least, one of signatures is verified |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 640 | * - unless signature is rejected explicitly with its digest. |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 641 | */ |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 642 | |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 643 | for (wincert = wincerts, wincerts_end = (u8 *)wincerts + wincerts_len; |
| 644 | (u8 *)wincert < wincerts_end; |
| 645 | wincert = (WIN_CERTIFICATE *) |
| 646 | ((u8 *)wincert + ALIGN(wincert->dwLength, 8))) { |
| 647 | if ((u8 *)wincert + sizeof(*wincert) >= wincerts_end) |
| 648 | break; |
| 649 | |
| 650 | if (wincert->dwLength <= sizeof(*wincert)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 651 | log_debug("dwLength too small: %u < %zu\n", |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 652 | wincert->dwLength, sizeof(*wincert)); |
| 653 | continue; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 654 | } |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 655 | |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 656 | log_debug("WIN_CERTIFICATE_TYPE: 0x%x\n", |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 657 | wincert->wCertificateType); |
| 658 | |
| 659 | auth = (u8 *)wincert + sizeof(*wincert); |
| 660 | auth_size = wincert->dwLength - sizeof(*wincert); |
| 661 | if (wincert->wCertificateType == WIN_CERT_TYPE_EFI_GUID) { |
| 662 | if (auth + sizeof(efi_guid_t) >= wincerts_end) |
| 663 | break; |
| 664 | |
| 665 | if (auth_size <= sizeof(efi_guid_t)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 666 | log_debug("dwLength too small: %u < %zu\n", |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 667 | wincert->dwLength, sizeof(*wincert)); |
| 668 | continue; |
| 669 | } |
| 670 | if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 671 | log_debug("Certificate type not supported: %pUs\n", |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 672 | auth); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 673 | ret = false; |
| 674 | goto out; |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | auth += sizeof(efi_guid_t); |
| 678 | auth_size -= sizeof(efi_guid_t); |
| 679 | } else if (wincert->wCertificateType |
| 680 | != WIN_CERT_TYPE_PKCS_SIGNED_DATA) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 681 | log_debug("Certificate type not supported\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 682 | ret = false; |
| 683 | goto out; |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | msg = pkcs7_parse_message(auth, auth_size); |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 687 | if (IS_ERR(msg)) { |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 688 | log_err("Parsing image's signature failed\n"); |
Patrick Wildt | fbc745c | 2020-05-07 02:17:14 +0200 | [diff] [blame] | 689 | msg = NULL; |
AKASHI Takahiro | eab1275 | 2020-07-08 14:01:52 +0900 | [diff] [blame] | 690 | continue; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 691 | } |
| 692 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 693 | /* |
AKASHI Takahiro | e669c2d | 2022-07-05 14:48:14 +0900 | [diff] [blame] | 694 | * verify signatures in pkcs7's signedInfos which are |
| 695 | * to authenticate the integrity of pkcs7's contentInfo. |
| 696 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 697 | * NOTE: |
| 698 | * UEFI specification defines two signature types possible |
| 699 | * in signature database: |
| 700 | * a. x509 certificate, where a signature in image is |
| 701 | * a message digest encrypted by RSA public key |
| 702 | * (EFI_CERT_X509_GUID) |
| 703 | * b. bare hash value of message digest |
| 704 | * (EFI_CERT_SHAxxx_GUID) |
| 705 | * |
| 706 | * efi_signature_verify() handles case (a), while |
| 707 | * efi_signature_lookup_digest() handles case (b). |
| 708 | * |
| 709 | * There is a third type: |
| 710 | * c. message digest of a certificate |
| 711 | * (EFI_CERT_X509_SHAAxxx_GUID) |
| 712 | * This type of signature is used only in revocation list |
| 713 | * (dbx) and handled as part of efi_signatgure_verify(). |
| 714 | */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 715 | /* try black-list first */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 716 | if (efi_signature_verify_one(regs, msg, dbx)) { |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 717 | ret = false; |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 718 | log_debug("Signature was rejected by \"dbx\"\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 719 | goto out; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 720 | } |
| 721 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 722 | if (!efi_signature_check_signers(msg, dbx)) { |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 723 | ret = false; |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 724 | log_debug("Signer(s) in \"dbx\"\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 725 | goto out; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 726 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 727 | |
| 728 | /* try white-list */ |
AKASHI Takahiro | e669c2d | 2022-07-05 14:48:14 +0900 | [diff] [blame] | 729 | if (!efi_signature_verify(regs, msg, db, dbx)) { |
| 730 | log_debug("Signature was not verified by \"db\"\n"); |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * now calculate an image's hash value and compare it with |
| 736 | * a messaged digest embedded in pkcs7's contentInfo |
| 737 | */ |
| 738 | if (efi_image_verify_digest(regs, msg)) { |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 739 | ret = true; |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 740 | continue; |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 741 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 742 | |
AKASHI Takahiro | e669c2d | 2022-07-05 14:48:14 +0900 | [diff] [blame] | 743 | log_debug("Message digest doesn't match\n"); |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 744 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 745 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 746 | |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 747 | /* last resort try the image sha256 hash in db */ |
| 748 | if (!ret && efi_signature_lookup_digest(regs, db, false)) |
| 749 | ret = true; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 750 | |
Ilias Apalodimas | e9b3662 | 2022-02-11 09:37:49 +0200 | [diff] [blame] | 751 | out: |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 752 | efi_sigstore_free(db); |
| 753 | efi_sigstore_free(dbx); |
| 754 | pkcs7_free_message(msg); |
| 755 | free(regs); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 756 | if (new_efi != efi) |
| 757 | free(new_efi); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 758 | |
AKASHI Takahiro | e2761e2 | 2022-07-05 14:48:13 +0900 | [diff] [blame] | 759 | log_debug("%s: Exit, %d\n", __func__, ret); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | #else |
| 763 | static bool efi_image_authenticate(void *efi, size_t efi_size) |
| 764 | { |
| 765 | return true; |
| 766 | } |
| 767 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 768 | |
Heinrich Schuchardt | ad6e0f7 | 2021-01-12 12:40:32 +0100 | [diff] [blame] | 769 | |
| 770 | /** |
| 771 | * efi_check_pe() - check if a memory buffer contains a PE-COFF image |
| 772 | * |
| 773 | * @buffer: buffer to check |
| 774 | * @size: size of buffer |
| 775 | * @nt_header: on return pointer to NT header of PE-COFF image |
| 776 | * Return: EFI_SUCCESS if the buffer contains a PE-COFF image |
| 777 | */ |
| 778 | efi_status_t efi_check_pe(void *buffer, size_t size, void **nt_header) |
| 779 | { |
| 780 | IMAGE_DOS_HEADER *dos = buffer; |
| 781 | IMAGE_NT_HEADERS32 *nt; |
| 782 | |
| 783 | if (size < sizeof(*dos)) |
| 784 | return EFI_INVALID_PARAMETER; |
| 785 | |
| 786 | /* Check for DOS magix */ |
| 787 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) |
| 788 | return EFI_INVALID_PARAMETER; |
| 789 | |
| 790 | /* |
| 791 | * Check if the image section header fits into the file. Knowing that at |
| 792 | * least one section header follows we only need to check for the length |
| 793 | * of the 64bit header which is longer than the 32bit header. |
| 794 | */ |
| 795 | if (size < dos->e_lfanew + sizeof(IMAGE_NT_HEADERS32)) |
| 796 | return EFI_INVALID_PARAMETER; |
| 797 | nt = (IMAGE_NT_HEADERS32 *)((u8 *)buffer + dos->e_lfanew); |
| 798 | |
| 799 | /* Check for PE-COFF magic */ |
| 800 | if (nt->Signature != IMAGE_NT_SIGNATURE) |
| 801 | return EFI_INVALID_PARAMETER; |
| 802 | |
| 803 | if (nt_header) |
| 804 | *nt_header = nt; |
| 805 | |
| 806 | return EFI_SUCCESS; |
| 807 | } |
| 808 | |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 809 | /** |
Heinrich Schuchardt | cd86dc9 | 2021-08-29 11:52:44 +0200 | [diff] [blame] | 810 | * section_size() - determine size of section |
| 811 | * |
| 812 | * The size of a section in memory if normally given by VirtualSize. |
| 813 | * If VirtualSize is not provided, use SizeOfRawData. |
| 814 | * |
| 815 | * @sec: section header |
| 816 | * Return: size of section in memory |
| 817 | */ |
| 818 | static u32 section_size(IMAGE_SECTION_HEADER *sec) |
| 819 | { |
| 820 | if (sec->Misc.VirtualSize) |
| 821 | return sec->Misc.VirtualSize; |
| 822 | else |
| 823 | return sec->SizeOfRawData; |
| 824 | } |
| 825 | |
| 826 | /** |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 827 | * efi_load_pe() - relocate EFI binary |
| 828 | * |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 829 | * This function loads all sections from a PE binary into a newly reserved |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 830 | * piece of memory. On success the entry point is returned as handle->entry. |
| 831 | * |
| 832 | * @handle: loaded image handle |
| 833 | * @efi: pointer to the EFI binary |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 834 | * @efi_size: size of @efi binary |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 835 | * @loaded_image_info: loaded image protocol |
| 836 | * Return: status code |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 837 | */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 838 | efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, |
| 839 | void *efi, size_t efi_size, |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 840 | struct efi_loaded_image *loaded_image_info) |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 841 | { |
| 842 | IMAGE_NT_HEADERS32 *nt; |
| 843 | IMAGE_DOS_HEADER *dos; |
| 844 | IMAGE_SECTION_HEADER *sections; |
| 845 | int num_sections; |
| 846 | void *efi_reloc; |
| 847 | int i; |
| 848 | const IMAGE_BASE_RELOCATION *rel; |
| 849 | unsigned long rel_size; |
| 850 | int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 851 | uint64_t image_base; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 852 | unsigned long virt_size = 0; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 853 | int supported = 0; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 854 | efi_status_t ret; |
| 855 | |
Heinrich Schuchardt | ad6e0f7 | 2021-01-12 12:40:32 +0100 | [diff] [blame] | 856 | ret = efi_check_pe(efi, efi_size, (void **)&nt); |
| 857 | if (ret != EFI_SUCCESS) { |
| 858 | log_err("Not a PE-COFF file\n"); |
| 859 | return EFI_LOAD_ERROR; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 862 | for (i = 0; machines[i]; i++) |
| 863 | if (machines[i] == nt->FileHeader.Machine) { |
| 864 | supported = 1; |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | if (!supported) { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 869 | log_err("Machine type 0x%04x is not supported\n", |
| 870 | nt->FileHeader.Machine); |
Heinrich Schuchardt | ad6e0f7 | 2021-01-12 12:40:32 +0100 | [diff] [blame] | 871 | return EFI_LOAD_ERROR; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 874 | num_sections = nt->FileHeader.NumberOfSections; |
| 875 | sections = (void *)&nt->OptionalHeader + |
| 876 | nt->FileHeader.SizeOfOptionalHeader; |
| 877 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 878 | if (efi_size < ((void *)sections + sizeof(sections[0]) * num_sections |
| 879 | - efi)) { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 880 | log_err("Invalid number of sections: %d\n", num_sections); |
Heinrich Schuchardt | ad6e0f7 | 2021-01-12 12:40:32 +0100 | [diff] [blame] | 881 | return EFI_LOAD_ERROR; |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* Authenticate an image */ |
Heinrich Schuchardt | 74a8414 | 2020-08-27 17:51:32 +0200 | [diff] [blame] | 885 | if (efi_image_authenticate(efi, efi_size)) { |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 886 | handle->auth_status = EFI_IMAGE_AUTH_PASSED; |
Heinrich Schuchardt | 74a8414 | 2020-08-27 17:51:32 +0200 | [diff] [blame] | 887 | } else { |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 888 | handle->auth_status = EFI_IMAGE_AUTH_FAILED; |
Heinrich Schuchardt | 74a8414 | 2020-08-27 17:51:32 +0200 | [diff] [blame] | 889 | log_err("Image not authenticated\n"); |
| 890 | } |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 891 | |
| 892 | /* Calculate upper virtual address boundary */ |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 893 | for (i = num_sections - 1; i >= 0; i--) { |
| 894 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
Heinrich Schuchardt | cd86dc9 | 2021-08-29 11:52:44 +0200 | [diff] [blame] | 895 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 896 | virt_size = max_t(unsigned long, virt_size, |
Heinrich Schuchardt | cd86dc9 | 2021-08-29 11:52:44 +0200 | [diff] [blame] | 897 | sec->VirtualAddress + section_size(sec)); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* Read 32/64bit specific header bits */ |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 901 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 902 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
| 903 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 904 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 905 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 906 | handle->image_type = opt->Subsystem; |
Ilias Apalodimas | 6e01c23 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 907 | efi_reloc = efi_alloc_aligned_pages(virt_size, |
| 908 | loaded_image_info->image_code_type, |
| 909 | opt->SectionAlignment); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 910 | if (!efi_reloc) { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 911 | log_err("Out of memory\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 912 | ret = EFI_OUT_OF_RESOURCES; |
| 913 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 914 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 915 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 916 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 917 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
Ivan Gorinov | 749d5c1 | 2018-04-05 18:32:06 -0700 | [diff] [blame] | 918 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 919 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 920 | image_base = opt->ImageBase; |
Heinrich Schuchardt | faef0e7 | 2018-01-19 20:24:41 +0100 | [diff] [blame] | 921 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 922 | handle->image_type = opt->Subsystem; |
Ilias Apalodimas | 6e01c23 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 923 | efi_reloc = efi_alloc_aligned_pages(virt_size, |
| 924 | loaded_image_info->image_code_type, |
| 925 | opt->SectionAlignment); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 926 | if (!efi_reloc) { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 927 | log_err("Out of memory\n"); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 928 | ret = EFI_OUT_OF_RESOURCES; |
| 929 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 930 | } |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 931 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 932 | rel_size = opt->DataDirectory[rel_idx].Size; |
| 933 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
| 934 | } else { |
Heinrich Schuchardt | eb67657 | 2020-08-25 17:51:20 +0000 | [diff] [blame] | 935 | log_err("Invalid optional header magic %x\n", |
| 936 | nt->OptionalHeader.Magic); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 937 | ret = EFI_LOAD_ERROR; |
| 938 | goto err; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Simon Glass | 75be05f | 2023-02-05 15:39:46 -0700 | [diff] [blame] | 941 | #if IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 942 | /* Measure an PE/COFF image */ |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 943 | ret = tcg2_measure_pe_image(efi, efi_size, handle, loaded_image_info); |
| 944 | if (ret == EFI_SECURITY_VIOLATION) { |
| 945 | /* |
| 946 | * TCG2 Protocol is installed but no TPM device found, |
| 947 | * this is not expected. |
| 948 | */ |
| 949 | log_err("PE image measurement failed, no tpm device found\n"); |
| 950 | goto err; |
| 951 | } |
| 952 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 953 | #endif |
| 954 | |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 955 | /* Copy PE headers */ |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 956 | memcpy(efi_reloc, efi, |
| 957 | sizeof(*dos) |
| 958 | + sizeof(*nt) |
| 959 | + nt->FileHeader.SizeOfOptionalHeader |
| 960 | + num_sections * sizeof(IMAGE_SECTION_HEADER)); |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 961 | |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 962 | /* Load sections into RAM */ |
| 963 | for (i = num_sections - 1; i >= 0; i--) { |
| 964 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
Heinrich Schuchardt | cd86dc9 | 2021-08-29 11:52:44 +0200 | [diff] [blame] | 965 | u32 copy_size = section_size(sec); |
| 966 | |
| 967 | if (copy_size > sec->SizeOfRawData) { |
| 968 | copy_size = sec->SizeOfRawData; |
| 969 | memset(efi_reloc + sec->VirtualAddress, 0, |
| 970 | sec->Misc.VirtualSize); |
| 971 | } |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 972 | memcpy(efi_reloc + sec->VirtualAddress, |
| 973 | efi + sec->PointerToRawData, |
Heinrich Schuchardt | cd86dc9 | 2021-08-29 11:52:44 +0200 | [diff] [blame] | 974 | copy_size); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* Run through relocations */ |
Ivan Gorinov | c7270bc | 2018-05-02 16:36:02 -0700 | [diff] [blame] | 978 | if (efi_loader_relocate(rel, rel_size, efi_reloc, |
| 979 | (unsigned long)image_base) != EFI_SUCCESS) { |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 980 | efi_free_pages((uintptr_t) efi_reloc, |
| 981 | (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 982 | ret = EFI_LOAD_ERROR; |
| 983 | goto err; |
xypron.glpk@gmx.de | 6ec97d2 | 2017-07-04 00:12:58 +0200 | [diff] [blame] | 984 | } |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 985 | |
| 986 | /* Flush cache */ |
Simon Glass | d25bb30 | 2016-11-07 08:47:04 -0700 | [diff] [blame] | 987 | flush_cache((ulong)efi_reloc, |
Alexander Graf | 59254d6 | 2018-04-23 07:59:47 +0200 | [diff] [blame] | 988 | ALIGN(virt_size, EFI_CACHELINE_SIZE)); |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 989 | invalidate_icache_all(); |
| 990 | |
| 991 | /* Populate the loaded image interface bits */ |
AKASHI Takahiro | 068879a | 2018-10-11 04:09:58 -0700 | [diff] [blame] | 992 | loaded_image_info->image_base = efi_reloc; |
| 993 | loaded_image_info->image_size = virt_size; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 994 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 995 | if (handle->auth_status == EFI_IMAGE_AUTH_PASSED) |
| 996 | return EFI_SUCCESS; |
| 997 | else |
| 998 | return EFI_SECURITY_VIOLATION; |
| 999 | |
| 1000 | err: |
| 1001 | return ret; |
Alexander Graf | 4e0c1c1 | 2016-03-04 01:09:58 +0100 | [diff] [blame] | 1002 | } |