From: Jan Janssen Date: Tue, 26 Oct 2021 10:33:43 +0000 (+0200) Subject: sd-boot: Check for existence of required pe sections X-Git-Tag: v250-rc1~355^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=65ff3d2626b6838b6c709eba6c6333c85f6c8d3b;p=thirdparty%2Fsystemd.git sd-boot: Check for existence of required pe sections --- diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index b2b763beee1..296efdd4892 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -2026,16 +2026,23 @@ static void config_entry_add_linux( return; for (;;) { + enum { + SECTION_CMDLINE, + SECTION_OSREL, + _SECTION_MAX, + }; + + static const CHAR8* const sections[_SECTION_MAX + 1] = { + [SECTION_CMDLINE] = (const CHAR8 *) ".cmdline", + [SECTION_OSREL] = (const CHAR8 *) ".osrel", + NULL, + }; + _cleanup_freepool_ CHAR16 *os_name_pretty = NULL, *os_name = NULL, *os_id = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL, *os_image_version = NULL; _cleanup_freepool_ CHAR8 *content = NULL; - const CHAR8 *sections[] = { - (CHAR8 *)".osrel", - (CHAR8 *)".cmdline", - NULL - }; - UINTN offs[ELEMENTSOF(sections)-1] = {}; - UINTN szs[ELEMENTSOF(sections)-1] = {}; + UINTN offs[_SECTION_MAX] = {}; + UINTN szs[_SECTION_MAX] = {}; CHAR8 *line; UINTN pos = 0; CHAR8 *key, *value; @@ -2054,11 +2061,11 @@ static void config_entry_add_linux( continue; /* look for .osrel and .cmdline sections in the .efi binary */ - err = pe_file_locate_sections(linux_dir, f->FileName, sections, offs, szs); - if (EFI_ERROR(err)) + err = pe_file_locate_sections(linux_dir, f->FileName, (const CHAR8**) sections, offs, szs); + if (EFI_ERROR(err) || szs[SECTION_OSREL] == 0) continue; - err = file_read(linux_dir, f->FileName, offs[0], szs[0], &content, NULL); + err = file_read(linux_dir, f->FileName, offs[SECTION_OSREL], szs[SECTION_OSREL], &content, NULL); if (EFI_ERROR(err)) continue; @@ -2122,21 +2129,24 @@ static void config_entry_add_linux( path, os_image_version ?: (os_version ?: (os_version_id ? : os_build_id))); + config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi"); + + if (szs[SECTION_CMDLINE] == 0) + continue; + FreePool(content); content = NULL; /* read the embedded cmdline file */ - err = file_read(linux_dir, f->FileName, offs[1], szs[1], &content, NULL); + err = file_read(linux_dir, f->FileName, offs[SECTION_CMDLINE], szs[SECTION_CMDLINE], &content, NULL); if (!EFI_ERROR(err)) { /* chomp the newline */ - if (content[szs[1]-1] == '\n') - content[szs[1]-1] = '\0'; + if (content[szs[SECTION_CMDLINE] - 1] == '\n') + content[szs[SECTION_CMDLINE] - 1] = '\0'; entry->options = stra_to_str(content); } - - config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi"); } } } diff --git a/src/boot/efi/stub.c b/src/boot/efi/stub.c index 256aa21827f..52d26e7cd2b 100644 --- a/src/boot/efi/stub.c +++ b/src/boot/efi/stub.c @@ -154,7 +154,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { _SECTION_MAX, }; - const CHAR8* const sections[] = { + static const CHAR8* const sections[_SECTION_MAX + 1] = { [SECTION_CMDLINE] = (const CHAR8*) ".cmdline", [SECTION_LINUX] = (const CHAR8*) ".linux", [SECTION_INITRD] = (const CHAR8*) ".initrd", @@ -187,8 +187,11 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err); err = pe_memory_locate_sections(loaded_image->ImageBase, (const CHAR8**) sections, addrs, szs); - if (EFI_ERROR(err)) + if (EFI_ERROR(err) || szs[SECTION_LINUX] == 0) { + if (!EFI_ERROR(err)) + err = EFI_NOT_FOUND; return log_error_status_stall(err, L"Unable to locate embedded .linux section: %r", err); + } /* Show splash screen as early as possible */ graphics_splash((const UINT8*) loaded_image->ImageBase + addrs[SECTION_SPLASH], szs[SECTION_SPLASH], NULL);