From: Mark Wielaard Date: Tue, 7 Apr 2026 13:25:53 +0000 (+0200) Subject: libebl: Explicitly check Ehdr can be used in eblopenbackend X-Git-Tag: elfutils-0.195~14 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=f0f77b818f99de2e75d98758f96cb2b6da87fe65;p=thirdparty%2Felfutils.git libebl: Explicitly check Ehdr can be used in eblopenbackend Make sure Ehdr is really read into memory, don't use internal elf state. * libebl/eblopenbackend.c (openbackend): Use gelf_getehdr instead of accessing elf state ehdr field directly. https://sourceware.org/bugzilla/show_bug.cgi?id=34007 Signed-off-by: Mark Wielaard --- diff --git a/libebl/eblopenbackend.c b/libebl/eblopenbackend.c index dbe4b947..e2a50321 100644 --- a/libebl/eblopenbackend.c +++ b/libebl/eblopenbackend.c @@ -309,17 +309,9 @@ openbackend (Elf *elf, const char *emulation, GElf_Half machine) /* Well, we know the emulation name now. */ result->emulation = machines[cnt].emulation; - /* We access some data structures directly. Make sure the 32 and - 64 bit variants are laid out the same. */ - eu_static_assert (offsetof (Elf32_Ehdr, e_machine) - == offsetof (Elf64_Ehdr, e_machine)); - eu_static_assert (sizeof (((Elf32_Ehdr *) 0)->e_machine) - == sizeof (((Elf64_Ehdr *) 0)->e_machine)); - eu_static_assert (offsetof (Elf, state.elf32.ehdr) - == offsetof (Elf, state.elf64.ehdr)); - /* Prefer taking the information from the ELF file. */ - if (elf == NULL) + GElf_Ehdr ehdr; + if (elf == NULL || gelf_getehdr (elf, &ehdr) == NULL) { result->machine = machines[cnt].em; result->class = machines[cnt].class; @@ -327,9 +319,9 @@ openbackend (Elf *elf, const char *emulation, GElf_Half machine) } else { - result->machine = elf->state.elf32.ehdr->e_machine; - result->class = elf->state.elf32.ehdr->e_ident[EI_CLASS]; - result->data = elf->state.elf32.ehdr->e_ident[EI_DATA]; + result->machine = ehdr.e_machine; + result->class = ehdr.e_ident[EI_CLASS]; + result->data = ehdr.e_ident[EI_DATA]; } if (machines[cnt].init &&