Skip to contentSkip to conversation

fs/binfmt_elf.c: disallow invalid entry point address

PATCH In review H.J. Lu 2 participants 2 messages Dec 12, 2021 – Dec 13, 2021

Subsystems (2)
lore
Share & tools
lore

H.J. Lu

author
#0 lore raw

PATCH v2 1/1 fs/binfmt_elf.c: disallow invalid entry point address

On Linux, the start of the first PT_LOAD segment is the ELF header and the address 0 points to the ELF magic bytes. Update the ELF loader to disallow ELF binaries with entry point address smaller than the ELF header size. This fixes:

https://bugzilla.kernel.org/show_bug.cgi?id=215303

Tested by booting Fedora 35 and running a shared library with invalid entry point address:

$ readelf -h load.so | grep "Entry point address:"
  Entry point address:               0x4
$ ./load.so
bash: ./load.so: cannot execute binary file: Exec format error
$
---
 fs/binfmt_elf.c | 2 ++
 1 file changed, 2 insertions(+)
fs/binfmt_elf.c +2 -0
index bd78587194dc..7f035022131b 100644
@@ -850,6 +850,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 
 	if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN)
 		goto out;
+	if (elf_ex->e_entry < sizeof(*elf_ex))
+		goto out;
 	if (!elf_check_arch(elf_ex))
 		goto out;
 	if (elf_check_fdpic(elf_ex))
Signature · 1 line
2.33.1
#1 lore raw
Quoted 4 lines · H.J. Lu

On Linux, the start of the first PT_LOAD segment is the ELF header and the address 0 points to the ELF magic bytes. Update the ELF loader to disallow ELF binaries with entry point address smaller than the ELF header size.

I kind of get why that was suggested but there is most definitely no requirement for the program headers to be loaded let alone be at any particular virtual address. We could be talking about a static elf binary.

Quoted 4 lines

This fixes:

https://bugzilla.kernel.org/show_bug.cgi?id=215303

Quoted 8 lines

Tested by booting Fedora 35 and running a shared library with invalid entry point address:

$ readelf -h load.so | grep "Entry point address:"
  Entry point address:               0x4
$ ./load.so
bash: ./load.so: cannot execute binary file: Exec format error
$

Is the point of this keeping shared libraries from executing? What is gained by this patch?

Eric

Quoted 18 lines
---
 fs/binfmt_elf.c | 2 ++
 1 file changed, 2 insertions(+)
fs/binfmt_elf.c +2 -0
index bd78587194dc..7f035022131b 100644
@@ -850,6 +850,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 
 	if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN)
 		goto out;
+	if (elf_ex->e_entry < sizeof(*elf_ex))
+		goto out;
 	if (!elf_check_arch(elf_ex))
 		goto out;
 	if (elf_check_fdpic(elf_ex))