Hi,

On Sun, Jun 21, 2026 at 04:53:49PM +0200, Mark Wielaard wrote:
> Both big endian architectures fail with:
> 
> FAIL: elf-from-memory
> =====================
> PASS: wrapping e_phoff handled without OOB read
> FAIL: legitimate image rejected
> FAIL elf-from-memory (exit status: 1)
> 
> I think that is because elf_from_memory only expects same endian ELF.
> Or the testcase has to xlate the Ehdr and Phdr before memcpy them to
> the backing memory array?

Testing the attached.
https://builder.sourceware.org/buildbot/#/changes/120607

Cheers,

Mark
>From ad3083f104d22c964988378b127c436cb3ef5532 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <[email protected]>
Date: Sun, 21 Jun 2026 17:19:18 +0200
Subject: [PATCH] tests: Make elf-from-memory testcase work on big endian
 setups

We are providing structs for x86_64 (little endian). So make sure the
Ehdr and Phdr are always xlated to ELFDATA2LSB.

        * tests/elf-from-memory.c (main): Call elf64_xlatetom.

Signed-off-by: Mark Wielaard <[email protected]>
---
 tests/elf-from-memory.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/tests/elf-from-memory.c b/tests/elf-from-memory.c
index cc9a83eeaac0..8a82ce290a4d 100644
--- a/tests/elf-from-memory.c
+++ b/tests/elf-from-memory.c
@@ -77,6 +77,15 @@ main (void)
 
   init_ehdr (&ehdr);
   ehdr.e_phoff = (Elf64_Off) (0ULL - (uint64_t) sizeof (Elf64_Phdr));
+  /* Make sure data is in LSB order.  */
+  Elf_Data xlate_data_ehdr =
+    {
+      .d_type = ELF_T_EHDR,
+      .d_buf = &ehdr,
+      .d_size = sizeof ehdr,
+      .d_version = EV_CURRENT,
+    };
+  elf64_xlatetom (&xlate_data_ehdr, &xlate_data_ehdr, ELFDATA2LSB);
   memset (backing, 0, sizeof backing);
   memcpy (backing, &ehdr, sizeof ehdr);
   Elf *elf = elf_from_remote_memory (0, 4096, NULL, read_mem, NULL);
@@ -91,15 +100,26 @@ main (void)
 
   init_ehdr (&ehdr);
   ehdr.e_phoff = sizeof (Elf64_Ehdr);
+  /* Make sure data is in LSB order.  */
+  elf64_xlatetom (&xlate_data_ehdr, &xlate_data_ehdr, ELFDATA2LSB);
   Elf64_Phdr phdr;
   memset (&phdr, 0, sizeof phdr);
   phdr.p_type = PT_LOAD;
   phdr.p_filesz = BACKSZ;
   phdr.p_memsz = BACKSZ;
   phdr.p_align = 4096;
+  /* Make sure data is in LSB order.  */
+  Elf_Data xlate_data_phdr =
+    {
+      .d_type = ELF_T_PHDR,
+      .d_buf = &phdr,
+      .d_size = sizeof phdr,
+      .d_version = EV_CURRENT,
+    };
+  elf64_xlatetom (&xlate_data_phdr, &xlate_data_phdr, ELFDATA2LSB);
   memset (backing, 0, sizeof backing);
   memcpy (backing, &ehdr, sizeof ehdr);
-  memcpy (backing + ehdr.e_phoff, &phdr, sizeof phdr);
+  memcpy (backing + sizeof (Elf64_Ehdr), &phdr, sizeof phdr);
   elf = elf_from_remote_memory (0, 4096, NULL, read_mem, NULL);
   GElf_Ehdr got;
   if (elf == NULL || gelf_getehdr (elf, &got) == NULL || got.e_phnum != 1)
-- 
2.54.0

Reply via email to