Skip to contentSkip to conversation

fs/binfmt_elf.c: disallow zero entry point address

PATCH Superseded H.J. Lu 3 participants 11 messages Dec 11, 2021 – Dec 13, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

H.J. Lu

author
#0 lore raw

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

According to gABI, the entry point address in the ELF header gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. Update the ELF loader to disallow an ELF binary with zero entry point address. This fixes:

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

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

$ readelf -h load.so | grep "Entry point address:"
  Entry point address:               0x0
$ ./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..bb427c97dc02 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 == 0)
+		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 8 lines · H.J. Lu

According to gABI, the entry point address in the ELF header gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. Update the ELF loader to disallow an ELF binary with zero entry point address. This fixes:

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

Quoted 7 lines

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

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

Why not let it segfault?

Quoted 2 lines
fs/binfmt_elf.c +2 -0
+	if (elf_ex->e_entry == 0)
+		goto out;
author
#2 lore raw
Quoted 13 lines · Alexey Dobriyan
Quoted 11 lines · H.J. Lu

According to gABI, the entry point address in the ELF header gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. Update the ELF loader to disallow an ELF binary with zero entry point address. This fixes:

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

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

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

Why not let it segfault?

Quoted 2 lines
fs/binfmt_elf.c +2 -0
+     if (elf_ex->e_entry == 0)
+             goto out;

Why let it segfault?

Signature · 1 line
H.J.
#3 lore raw
Quoted 6 lines · H.J. Lu
Quoted 2 lines · Alexey Dobriyan

Why not let it segfault?

Why let it segfault?

That's not my main worry - what if somebody has a code section with a zero vaddr and intentionally put the entry at the beginning?

Maybe it's not supposed to work by some paper standatd, but afaik currently it _would_ work.

All these things are relative to the load address, so a zero e_entry doesn't mean NULL, and may be a perfectly valid address.

No?

Linus

author
#4 lore raw
Quoted 12 lines · Linus Torvalds
Quoted 6 lines · H.J. Lu
Quoted 2 lines · Alexey Dobriyan

Why not let it segfault?

Why let it segfault?

That's not my main worry - what if somebody has a code section with a zero vaddr and intentionally put the entry at the beginning?

Quoted 7 lines

Maybe it's not supposed to work by some paper standatd, but afaik currently it _would_ work.

All these things are relative to the load address, so a zero e_entry doesn't mean NULL, and may be a perfectly valid address.

No?

According to the ELF specification, zero entry point value means there is no entry point. Such ELF binary doesn't conform to the ELF specification.

Signature · 1 line
H.J.
#5 lore raw
Quoted 4 lines · H.J. Lu

According to the ELF specification, zero entry point value means there is no entry point. Such ELF binary doesn't conform to the ELF specification.

Nobody cares about paper specifications.

All that matters is REALITY.

So let me quote my email again, since you clearly didn't actually read it (read that "maybe it's not supposed to work" part):

Quoted 5 lines

That's not my main worry - what if somebody has a code section with a zero vaddr and intentionally put the entry at the beginning?

Maybe it's not supposed to work by some paper standatd, but afaik currently it _would_ work.

I'm not sure this can happen currently (maybe all tools effectively make it so that the ELF headers etc are part of the loaded image).

But no, paper specifications have absolutely no meaning if they don't match realty.

And the reality is that I don't think we've ever checked e_entry being zero, which means that maybe people have used it.

So convince me that the above cannot happen. I'm perfectly willing to be convinced, but "some random paper standard that we've never followed" is not the thing to quote.

Linus

author
#6 lore raw
Quoted 26 lines · Linus Torvalds
Quoted 4 lines · H.J. Lu

According to the ELF specification, zero entry point value means there is no entry point. Such ELF binary doesn't conform to the ELF specification.

Nobody cares about paper specifications.

All that matters is REALITY.

So let me quote my email again, since you clearly didn't actually read it (read that "maybe it's not supposed to work" part):

Quoted 5 lines

That's not my main worry - what if somebody has a code section with a zero vaddr and intentionally put the entry at the beginning?

Maybe it's not supposed to work by some paper standatd, but afaik currently it _would_ work.

I'm not sure this can happen currently (maybe all tools effectively make it so that the ELF headers etc are part of the loaded image).

But no, paper specifications have absolutely no meaning if they don't match realty.

Quoted 7 lines

And the reality is that I don't think we've ever checked e_entry being zero, which means that maybe people have used it.

So convince me that the above cannot happen. I'm perfectly willing to be convinced, but "some random paper standard that we've never followed" is not the thing to quote.

On Linux, the start of the first PT_LOAD segment is the ELF header and the address 0 points to the ELF magic bytes which isn't a valid code sequence.

Signature · 1 line
H.J.
#7 lore raw

[ Crossed emails ]

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 which isn't a valid code sequence.

Yeah, then I think a much more valid argument (and patch) is _that_ argument.

So that kind of explanation, along with a patch more along the line of that

        if (elf_ex->e_entry < header_sizes)
                goto out;

I suggested, and not talking about paper standards that may or may not be relevant.

That would be much more palatable to me - it's a _technical_ argument, not a "some paper standard that we clearly have never followed" argument.

Linus

author
#8 lore raw
Quoted 15 lines · Linus Torvalds

[ Crossed emails ]

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 which isn't a valid code sequence.

Yeah, then I think a much more valid argument (and patch) is _that_ argument.

So that kind of explanation, along with a patch more along the line of that

        if (elf_ex->e_entry < header_sizes)
                goto out;
Quoted 8 lines

I suggested, and not talking about paper standards that may or may not be relevant.

That would be much more palatable to me - it's a _technical_ argument, not a "some paper standard that we clearly have never followed" argument.

Linus

I sent out the v2 patch with

	if (elf_ex->e_entry < sizeof(*elf_ex))
		goto out;

H.J.

#9 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

I'm not sure this can happen currently (maybe all tools effectively make it so that the ELF headers etc are part of the loaded image).

Side note: if that ends up being the case (ie e_entry always effectively relative to the head of the image), then I think a better fix would be to make that explicit, something like

        if (elf_ex->e_entry < header_sizes)
                goto out;

but the logic on exactly how things get loaded is so messy that I'm not sure just what the situation is.

We've had things like old tool chains generate messy binaries before, to the point where we've had to revert much more important changes (ie the whole mess with MAP_FIXED_NOREPLACE and overlapping sections).

Linus

#10 lore raw
In reply to H.J. Lu
Quoted 19 lines · H.J. Lu
Quoted 18 lines · Alexey Dobriyan
Quoted 15 lines · H.J. Lu

According to gABI, the entry point address in the ELF header gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. Update the ELF loader to disallow an ELF binary with zero entry point address. This fixes:

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

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

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

Why not let it segfault?

Quoted 2 lines
fs/binfmt_elf.c +2 -0
+     if (elf_ex->e_entry == 0)
+             goto out;

Why let it segfault?

Such babysitting adds a branch for everyone to catch small number of binaries.

e_entry can point to kernelspace, and it should segfault on the first instruction (correctly).

It can iincorrectly point to unmapped area or VMA with wrong permissions, with the same effect. But now check is more costly.