From: Lennart Poettering Date: Mon, 24 Jun 2024 15:50:34 +0000 (+0200) Subject: pe: use more correct section name comparison function X-Git-Tag: v257-rc1~1041^2~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9e63e28aa652df483f431ab35a5934d77aab95db;p=thirdparty%2Fsystemd.git pe: use more correct section name comparison function we should only compare up to 8 chars --- diff --git a/src/boot/efi/pe.c b/src/boot/efi/pe.c index 34b01561f07..cb75c2b42b8 100644 --- a/src/boot/efi/pe.c +++ b/src/boot/efi/pe.c @@ -155,6 +155,26 @@ static size_t section_table_offset(const DosFileHeader *dos, const PeFileHeader return dos->ExeHeader + offsetof(PeFileHeader, OptionalHeader) + pe->FileHeader.SizeOfOptionalHeader; } +static bool pe_section_name_equal(const char *a, const char *b) { + + if (a == b) + return true; + if (!a != !b) + return false; + + /* Compares up to 8 characters of a and b i.e. the name size limit in the PE section header */ + + for (size_t i = 0; i < sizeof_field(PeSectionHeader, Name); i++) { + if (a[i] != b[i]) + return false; + + if (a[i] == 0) /* Name is shorter than 8 */ + return true; + } + + return true; +} + static void locate_sections( const PeSectionHeader section_table[], size_t n_table, @@ -172,7 +192,7 @@ static void locate_sections( const PeSectionHeader *sect = section_table + i; for (size_t j = 0; sections[j]; j++) { - if (memcmp(sect->Name, sections[j], strlen8(sections[j])) != 0) + if (!pe_section_name_equal((const char*) sect->Name, sections[j])) continue; offsets[j] = in_memory ? sect->VirtualAddress : sect->PointerToRawData;