| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Generic Linux boot protocol using the EFI/PE entry point of the kernel. Passes |
| 5 |
* initrd with the LINUX_INITRD_MEDIA_GUID DevicePath and cmdline with |
| 6 |
* EFI LoadedImageProtocol. |
| 7 |
* |
| 8 |
* This method works for Linux 5.8 and newer on ARM/Aarch64, x86/x68_64 and RISC-V. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "device-path-util.h" |
| 12 |
#include "efi-log.h" |
| 13 |
#include "initrd.h" |
| 14 |
#include "linux.h" |
| 15 |
#include "pe.h" |
| 16 |
#include "proto/device-path.h" |
| 17 |
#include "proto/loaded-image.h" |
| 18 |
#include "proto/memory-attribute.h" |
| 19 |
#include "secure-boot.h" |
| 20 |
#include "shim.h" |
| 21 |
#include "util.h" |
| 22 |
|
| 23 |
typedef struct { |
| 24 |
MEMMAP_DEVICE_PATH memmap_path; |
| 25 |
EFI_DEVICE_PATH end_path; |
| 26 |
} _packed_ KERNEL_FILE_PATH; |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
const void *addr; |
| 30 |
size_t len; |
| 31 |
const EFI_DEVICE_PATH *device_path; |
| 32 |
} ValidationContext; |
| 33 |
|
| 34 |
static bool validate_payload( |
| 35 |
const void *ctx, const EFI_DEVICE_PATH *device_path, const void *file_buffer, size_t file_size) { |
| 36 |
|
| 37 |
const ValidationContext *payload = ASSERT_PTR(ctx); |
| 38 |
|
| 39 |
if (device_path != payload->device_path) |
| 40 |
return false; |
| 41 |
|
| 42 |
/* Security arch (1) protocol does not provide a file buffer. Instead we are supposed to fetch the payload |
| 43 |
* ourselves, which is not needed as we already have everything in memory and the device paths match. */ |
| 44 |
if (file_buffer && (file_buffer != payload->addr || file_size != payload->len)) |
| 45 |
return false; |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
static EFI_STATUS load_via_boot_services( |
| 51 |
EFI_HANDLE parent, |
| 52 |
EFI_LOADED_IMAGE_PROTOCOL* parent_loaded_image, |
| 53 |
uint32_t compat_entry_point, |
| 54 |
const char16_t *cmdline, |
| 55 |
const struct iovec *kernel, |
| 56 |
const struct iovec *initrd, |
| 57 |
KERNEL_FILE_PATH *kernel_file_path) { |
| 58 |
_cleanup_(unload_imagep) EFI_HANDLE kernel_image = NULL; |
| 59 |
EFI_LOADED_IMAGE_PROTOCOL* loaded_image = NULL; |
| 60 |
EFI_STATUS err; |
| 61 |
|
| 62 |
/* When running with shim < v16 and booting a UKI directly from it, without a second stage loader, |
| 63 |
* the shim verify protocol needs to be called or it will raise a security violation when starting |
| 64 |
* the image (e.g.: Fedora Cloud Base UKI). TODO: drop once support for shim < v16 is not needed. */ |
| 65 |
if (!shim_loader_available()) |
| 66 |
install_security_override( |
| 67 |
validate_payload, |
| 68 |
&(ValidationContext) { |
| 69 |
.addr = kernel->iov_base, |
| 70 |
.len = kernel->iov_len, |
| 71 |
.device_path = &kernel_file_path->memmap_path.Header, |
| 72 |
}); |
| 73 |
|
| 74 |
|
| 75 |
err = BS->LoadImage(/* BootPolicy= */ false, |
| 76 |
parent, |
| 77 |
&kernel_file_path->memmap_path.Header, |
| 78 |
kernel->iov_base, |
| 79 |
kernel->iov_len, |
| 80 |
&kernel_image); |
| 81 |
|
| 82 |
if (!shim_loader_available()) |
| 83 |
uninstall_security_override(); |
| 84 |
|
| 85 |
if (err != EFI_SUCCESS) |
| 86 |
return log_error_status(EFI_LOAD_ERROR, "Error loading inner kernel with shim: %m"); |
| 87 |
|
| 88 |
err = BS->HandleProtocol( |
| 89 |
kernel_image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image); |
| 90 |
if (err != EFI_SUCCESS) |
| 91 |
return log_error_status(EFI_LOAD_ERROR, "Error getting kernel image from protocol from shim: %m"); |
| 92 |
|
| 93 |
if (cmdline) { |
| 94 |
loaded_image->LoadOptions = (void *) cmdline; |
| 95 |
loaded_image->LoadOptionsSize = strsize16(loaded_image->LoadOptions); |
| 96 |
} |
| 97 |
|
| 98 |
_cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL; |
| 99 |
err = initrd_register(initrd, &initrd_handle); |
| 100 |
if (err != EFI_SUCCESS) |
| 101 |
return log_error_status(err, "Error registering initrd: %m"); |
| 102 |
|
| 103 |
log_wait(); |
| 104 |
|
| 105 |
err = BS->StartImage(kernel_image, NULL, NULL); |
| 106 |
/* Try calling the kernel compat entry point if one exists. */ |
| 107 |
if (err == EFI_UNSUPPORTED && compat_entry_point > 0) { |
| 108 |
EFI_IMAGE_ENTRY_POINT compat_entry = |
| 109 |
(EFI_IMAGE_ENTRY_POINT) ((const uint8_t *) loaded_image->ImageBase + compat_entry_point); |
| 110 |
err = compat_entry(kernel_image, ST); |
| 111 |
} |
| 112 |
|
| 113 |
return log_error_status(err, "Error starting kernel image with shim: %m"); |
| 114 |
} |
| 115 |
|
| 116 |
static EFI_STATUS memory_mark_ro_x(EFI_MEMORY_ATTRIBUTE_PROTOCOL *memory_proto, struct iovec *nx_section) { |
| 117 |
EFI_STATUS err; |
| 118 |
|
| 119 |
assert(memory_proto); |
| 120 |
assert(nx_section); |
| 121 |
|
| 122 |
/* As per MSFT requirement, memory pages need to be marked W^X, so mark code pages RO+X. |
| 123 |
* Firmwares will start enforcing this at some point in the near-ish future. |
| 124 |
* The kernel needs to mark this as supported explicitly, otherwise it will crash. |
| 125 |
* https://microsoft.github.io/mu/WhatAndWhy/enhancedmemoryprotection/ |
| 126 |
* https://www.kraxel.org/blog/2023/12/uefi-nx-linux-boot/ */ |
| 127 |
|
| 128 |
err = memory_proto->SetMemoryAttributes(memory_proto, POINTER_TO_PHYSICAL_ADDRESS(nx_section->iov_base), nx_section->iov_len, EFI_MEMORY_RO); |
| 129 |
if (err != EFI_SUCCESS) |
| 130 |
return log_error_status(err, "Cannot make kernel image read-only: %m"); |
| 131 |
|
| 132 |
err = memory_proto->ClearMemoryAttributes(memory_proto, POINTER_TO_PHYSICAL_ADDRESS(nx_section->iov_base), nx_section->iov_len, EFI_MEMORY_XP); |
| 133 |
if (err != EFI_SUCCESS) |
| 134 |
return log_error_status(err, "Cannot make kernel image executable: %m"); |
| 135 |
|
| 136 |
return EFI_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
static EFI_STATUS memory_mark_rw_nx(EFI_MEMORY_ATTRIBUTE_PROTOCOL *memory_proto, struct iovec *nx_section) { |
| 140 |
EFI_STATUS err; |
| 141 |
|
| 142 |
assert(memory_proto); |
| 143 |
assert(nx_section); |
| 144 |
|
| 145 |
err = memory_proto->SetMemoryAttributes(memory_proto, POINTER_TO_PHYSICAL_ADDRESS(nx_section->iov_base), nx_section->iov_len, EFI_MEMORY_XP); |
| 146 |
if (err != EFI_SUCCESS) |
| 147 |
return log_error_status(err, "Cannot make kernel image non-executable: %m"); |
| 148 |
|
| 149 |
err = memory_proto->ClearMemoryAttributes(memory_proto, POINTER_TO_PHYSICAL_ADDRESS(nx_section->iov_base), nx_section->iov_len, EFI_MEMORY_RO); |
| 150 |
if (err != EFI_SUCCESS) |
| 151 |
return log_error_status(err, "Cannot make kernel image writable: %m"); |
| 152 |
|
| 153 |
return EFI_SUCCESS; |
| 154 |
} |
| 155 |
|
| 156 |
EFI_STATUS linux_exec( |
| 157 |
EFI_HANDLE parent_image, |
| 158 |
const char16_t *cmdline, |
| 159 |
const struct iovec *kernel, |
| 160 |
const struct iovec *initrd) { |
| 161 |
|
| 162 |
size_t kernel_size_in_memory = 0; |
| 163 |
uint32_t compat_entry_point, entry_point; |
| 164 |
EFI_STATUS err; |
| 165 |
|
| 166 |
assert(parent_image); |
| 167 |
assert(iovec_is_set(kernel)); |
| 168 |
assert(iovec_is_valid(initrd)); |
| 169 |
|
| 170 |
err = pe_kernel_info(kernel->iov_base, &entry_point, &compat_entry_point, &kernel_size_in_memory); |
| 171 |
#if defined(__i386__) || defined(__x86_64__) |
| 172 |
if (err == EFI_UNSUPPORTED) |
| 173 |
/* Kernel is too old to support LINUX_INITRD_MEDIA_GUID, try the deprecated EFI handover |
| 174 |
* protocol. */ |
| 175 |
return linux_exec_efi_handover( |
| 176 |
parent_image, |
| 177 |
cmdline, |
| 178 |
kernel, |
| 179 |
initrd, |
| 180 |
kernel_size_in_memory); |
| 181 |
#endif |
| 182 |
if (err != EFI_SUCCESS) |
| 183 |
return log_error_status(err, "Bad kernel image: %m"); |
| 184 |
|
| 185 |
EFI_LOADED_IMAGE_PROTOCOL *parent_loaded_image; |
| 186 |
err = BS->HandleProtocol( |
| 187 |
parent_image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &parent_loaded_image); |
| 188 |
if (err != EFI_SUCCESS) |
| 189 |
return log_error_status(err, "Cannot get parent loaded image: %m"); |
| 190 |
|
| 191 |
_cleanup_free_ KERNEL_FILE_PATH *kernel_file_path = xnew(KERNEL_FILE_PATH, 1); |
| 192 |
*kernel_file_path = (KERNEL_FILE_PATH) { |
| 193 |
.memmap_path = { |
| 194 |
.Header = { |
| 195 |
.Type = HARDWARE_DEVICE_PATH, |
| 196 |
.SubType = HW_MEMMAP_DP, |
| 197 |
.Length = sizeof(MEMMAP_DEVICE_PATH), |
| 198 |
}, |
| 199 |
.MemoryType = EfiLoaderData, |
| 200 |
.StartingAddress = POINTER_TO_PHYSICAL_ADDRESS(kernel->iov_base), |
| 201 |
.EndingAddress = POINTER_TO_PHYSICAL_ADDRESS(kernel->iov_base) + kernel->iov_len, |
| 202 |
}, |
| 203 |
.end_path = DEVICE_PATH_END_NODE, |
| 204 |
}; |
| 205 |
|
| 206 |
/* If shim provides LoadImage, it comes from the new SHIM_IMAGE_LOADER interface added in shim 16, |
| 207 |
* and implements the following: |
| 208 |
* - shim hashes PE sections of PE binaries it authenticates and stores the hashes in a global |
| 209 |
* database. |
| 210 |
* - shim's LoadImage always verifies PE images against denylists: DBX, MOKX, SBAT. |
| 211 |
* - If the PE image was _not_ authenticated as a PE section it will also: |
| 212 |
* + verify it against allowlists: DB, MOK, |
| 213 |
* + measure it on PCR 4. |
| 214 |
* |
| 215 |
* (Compared to standard UEFI LoadImage(), the patched shim version of LoadImage() is both stricter — |
| 216 |
* as it checks SBAT + MOKX for all PE payloads — and more relaxed — as it disables DB checks for PE |
| 217 |
* payloads it has seen as part of another PE binary before.) |
| 218 |
* |
| 219 |
* In our case, we are loading a PE section that was already authenticated as part of the UKI. In |
| 220 |
* contrast to a normal UEFI LoadImage, shim will verify extra denylists (MOKX, SBAT), but skip all |
| 221 |
* allowlists and measurements. |
| 222 |
* |
| 223 |
* See https://github.com/rhboot/shim/blob/main/README.md#shim-loader-protocol |
| 224 |
*/ |
| 225 |
if (secure_boot_enabled() && (shim_loader_available() || (shim_loaded() && security_override_available()))) |
| 226 |
return load_via_boot_services( |
| 227 |
parent_image, |
| 228 |
parent_loaded_image, |
| 229 |
compat_entry_point, |
| 230 |
cmdline, |
| 231 |
kernel, |
| 232 |
initrd, |
| 233 |
kernel_file_path); |
| 234 |
|
| 235 |
err = pe_kernel_check_no_relocation(kernel->iov_base); |
| 236 |
if (err != EFI_SUCCESS) |
| 237 |
return err; |
| 238 |
|
| 239 |
/* As per MSFT requirement, memory pages need to be marked W^X, so mark code pages RO+X. |
| 240 |
* Firmwares will start enforcing this at some point in the near-ish future. |
| 241 |
* The kernel needs to mark this as supported explicitly, otherwise it will crash. |
| 242 |
* https://microsoft.github.io/mu/WhatAndWhy/enhancedmemoryprotection/ |
| 243 |
* https://www.kraxel.org/blog/2023/12/uefi-nx-linux-boot/ */ |
| 244 |
EFI_MEMORY_ATTRIBUTE_PROTOCOL *memory_proto = NULL; |
| 245 |
_cleanup_free_ struct iovec *nx_sections = NULL; |
| 246 |
size_t n_nx_sections = 0; |
| 247 |
|
| 248 |
if (pe_kernel_check_nx_compat(kernel->iov_base)) { |
| 249 |
/* LocateProtocol() is not quite that quick if you have many protocols, so only look for it |
| 250 |
* if required for NX_COMPAT */ |
| 251 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_MEMORY_ATTRIBUTE_PROTOCOL), /* Registration= */ NULL, (void **) &memory_proto); |
| 252 |
if (err != EFI_SUCCESS) |
| 253 |
/* Only warn if the UEFI should have support in the first place (version >= 2.10) */ |
| 254 |
log_full(err, |
| 255 |
ST->Hdr.Revision >= ((2U << 16) | 100U) ? LOG_WARNING : LOG_DEBUG, |
| 256 |
"No EFI_MEMORY_ATTRIBUTE_PROTOCOL found, skipping NX_COMPAT support."); |
| 257 |
} |
| 258 |
|
| 259 |
const PeSectionHeader *headers; |
| 260 |
size_t n_headers; |
| 261 |
|
| 262 |
/* Do we need to validate anything here? the len? */ |
| 263 |
err = pe_section_table_from_base(kernel->iov_base, &headers, &n_headers); |
| 264 |
if (err != EFI_SUCCESS) |
| 265 |
return log_error_status(err, "Cannot read sections: %m"); |
| 266 |
|
| 267 |
/* Do we need to ensure under 4gb address on x86? */ |
| 268 |
_cleanup_pages_ Pages loaded_kernel_pages = xmalloc_pages( |
| 269 |
AllocateAnyPages, EfiLoaderCode, EFI_SIZE_TO_PAGES(kernel_size_in_memory), 0); |
| 270 |
|
| 271 |
uint8_t* loaded_kernel = PHYSICAL_ADDRESS_TO_POINTER(loaded_kernel_pages.addr); |
| 272 |
FOREACH_ARRAY(h, headers, n_headers) { |
| 273 |
if (h->PointerToRelocations != 0) |
| 274 |
return log_error_status(EFI_LOAD_ERROR, "Inner kernel image contains sections with relocations, which we do not support."); |
| 275 |
if (h->SizeOfRawData == 0) |
| 276 |
continue; |
| 277 |
|
| 278 |
if (UINT32_MAX - h->VirtualAddress < h->SizeOfRawData) |
| 279 |
return log_error_status(EFI_LOAD_ERROR, "Invalid PE section, SizeOfRawData + VirtualAddress, overflows"); |
| 280 |
if (h->VirtualAddress + h->SizeOfRawData > kernel_size_in_memory) |
| 281 |
return log_error_status(EFI_LOAD_ERROR, "Section would write outside of memory"); |
| 282 |
if (h->SizeOfRawData > h->VirtualSize) |
| 283 |
return log_error_status(EFI_LOAD_ERROR, "Invalid PE section, raw data size is greater than virtual size"); |
| 284 |
if (UINT32_MAX - h->PointerToRawData < h->SizeOfRawData) |
| 285 |
return log_error_status(EFI_LOAD_ERROR, "Invalid PE section, PointerToRawData + SizeOfRawData overflows"); |
| 286 |
if (h->PointerToRawData + h->SizeOfRawData > kernel->iov_len) |
| 287 |
return log_error_status(EFI_LOAD_ERROR, "Invalid PE section, raw data extends outside of file"); |
| 288 |
memcpy(loaded_kernel + h->VirtualAddress, |
| 289 |
(const uint8_t*)kernel->iov_base + h->PointerToRawData, |
| 290 |
h->SizeOfRawData); |
| 291 |
memzero(loaded_kernel + h->VirtualAddress + h->SizeOfRawData, |
| 292 |
h->VirtualSize - h->SizeOfRawData); |
| 293 |
|
| 294 |
/* Not a code section? Nothing to do, leave as-is. */ |
| 295 |
if (memory_proto && (h->Characteristics & (PE_CODE|PE_EXECUTE))) { |
| 296 |
nx_sections = xrealloc(nx_sections, n_nx_sections * sizeof(struct iovec), (n_nx_sections + 1) * sizeof(struct iovec)); |
| 297 |
nx_sections[n_nx_sections].iov_base = loaded_kernel + h->VirtualAddress; |
| 298 |
nx_sections[n_nx_sections].iov_len = h->VirtualSize; |
| 299 |
|
| 300 |
err = memory_mark_ro_x(memory_proto, &nx_sections[n_nx_sections]); |
| 301 |
if (err != EFI_SUCCESS) |
| 302 |
return err; |
| 303 |
|
| 304 |
++n_nx_sections; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/* Patch the parent_image(_handle) and parent_loaded_image for the kernel image we are about to execute. |
| 309 |
* We have to do this, because if kernel stub code passes its own handle to certain firmware functions, |
| 310 |
* the firmware could cast EFI_LOADED_IMAGE_PROTOCOL * to a larger struct to access its own private data, |
| 311 |
* and if we allocated a smaller struct, that could cause problems. |
| 312 |
* This is modeled exactly after GRUB behaviour, which has proven to be functional. */ |
| 313 |
EFI_LOADED_IMAGE_PROTOCOL original_parent_loaded_image = *parent_loaded_image; |
| 314 |
parent_loaded_image->FilePath = &kernel_file_path->memmap_path.Header; |
| 315 |
parent_loaded_image->ImageBase = loaded_kernel; |
| 316 |
parent_loaded_image->ImageSize = kernel_size_in_memory; |
| 317 |
|
| 318 |
if (cmdline) { |
| 319 |
parent_loaded_image->LoadOptions = (void *) cmdline; |
| 320 |
parent_loaded_image->LoadOptionsSize = strsize16(parent_loaded_image->LoadOptions); |
| 321 |
} |
| 322 |
|
| 323 |
_cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL; |
| 324 |
err = initrd_register(initrd, &initrd_handle); |
| 325 |
if (err != EFI_SUCCESS) |
| 326 |
return log_error_status(err, "Error registering initrd: %m"); |
| 327 |
|
| 328 |
log_wait(); |
| 329 |
|
| 330 |
if (entry_point > 0) { |
| 331 |
EFI_IMAGE_ENTRY_POINT entry = |
| 332 |
(EFI_IMAGE_ENTRY_POINT) ((const uint8_t *) parent_loaded_image->ImageBase + entry_point); |
| 333 |
err = entry(parent_image, ST); |
| 334 |
} else if (compat_entry_point > 0) { |
| 335 |
/* Try calling the kernel compat entry point if one exists. */ |
| 336 |
EFI_IMAGE_ENTRY_POINT compat_entry = |
| 337 |
(EFI_IMAGE_ENTRY_POINT) ((const uint8_t *) parent_loaded_image->ImageBase + compat_entry_point); |
| 338 |
err = compat_entry(parent_image, ST); |
| 339 |
} |
| 340 |
|
| 341 |
/* Restore */ |
| 342 |
*parent_loaded_image = original_parent_loaded_image; |
| 343 |
|
| 344 |
/* On failure we'll free the buffers. EDK2 requires the memory buffers to be writable and |
| 345 |
* non-executable, as in some configurations it will overwrite them with a fixed pattern, so if the |
| 346 |
* attributes are not restored FreePages() will crash. */ |
| 347 |
for (size_t i = 0; i < n_nx_sections; i++) |
| 348 |
(void) memory_mark_rw_nx(memory_proto, &nx_sections[i]); |
| 349 |
|
| 350 |
return log_error_status(err, "Error starting kernel image: %m"); |
| 351 |
} |