Skip to content

Commit

Permalink
file: don't print ELF flags unless we've verified the bitness.
Browse files Browse the repository at this point in the history
This was the file(1) crash mentioned in #503.
  • Loading branch information
enh-google authored and landley committed Aug 10, 2024
1 parent 17afb6d commit d9d0a1c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tests/file.test
Original file line number Diff line number Diff line change
Expand Up @@ -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" "" ""
Expand Down
3 changes: 2 additions & 1 deletion toys/posix/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d9d0a1c

Please sign in to comment.