This was the file(1) crash mentioned in #503.
---
 tests/file.test   | 3 +++
 toys/posix/file.c | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)
From 950c6d3edbbde6cda00c908e7a4b33ecb3aeed48 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Fri, 2 Aug 2024 16:20:00 -0400
Subject: [PATCH] file: don't print ELF flags unless we've verified the
 bitness.

This was the file(1) crash mentioned in #503.
---
 tests/file.test   | 3 +++
 toys/posix/file.c | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/file.test b/tests/file.test
index a749c695..8dddfe3f 100755
--- a/tests/file.test
+++ b/tests/file.test
@@ -70,6 +70,9 @@ toyonly test_line "Android NDK short ELF note" "elf/ndk-elf-note-short" \
     "ELF shared object, 32-bit LSB arm, EABI5, soft float, dynamic (/system/bin/linker), for Android 28, BuildID=da6a5f4ca8da163b9339326e626d8a3c, stripped\n" "" ""
 toyonly test_line "ELF static fdpic" "elf/fdstatic" \
     "ELF executable (fdpic), 32-bit MSB sh, static, stripped\n" "" ""
+echo -ne '\x7fELF\00000000000000000000000000000000000000000000' > bad-bits
+testing "ELF bad bits" "file bad-bits" "bad-bits: ELF (bad type 12336), (bad class -1) (bad endian 48) unknown arch 12336\n" "" ""
+rm -f bad-bits
 
 testing "broken symlink" "file dangler" "dangler: broken symbolic link to $BROKEN\n" "" ""
 testing "symlink" "file symlink" "symlink: symbolic link to $LINK\n" "" ""
diff --git a/toys/posix/file.c b/toys/posix/file.c
index 30d22495..566daf1d 100644
--- a/toys/posix/file.c
+++ b/toys/posix/file.c
@@ -69,11 +69,12 @@ static void do_elf_file(int fd)
 
   // "x86".
   printf("%s", elf_arch_name(arch = elf_int(toybuf+18, 2)));
-  elf_print_flags(arch, elf_int(toybuf+36+12*bits, 4));
 
   // If what we've seen so far doesn't seem consistent, bail.
   if (bail) goto bad;
 
+  elf_print_flags(arch, elf_int(toybuf+36+12*bits, 4));
+
   // Stash what we need from the header; it's okay to reuse toybuf after this.
   phentsize = elf_int(toybuf+42+12*bits, 2);
   phnum = elf_int(toybuf+44+12*bits, 2);
-- 
2.46.0.rc2.264.g509ed76dc8-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to