2023-09-29 06:32:22

by Kees Cook

[permalink] [raw]
Subject: [PATCH v4 4/6] binfmt_elf: Use elf_load() for library

While load_elf_library() is a libc5-ism, we can still replace most of
its contents with elf_load() as well, further simplifying the code.

Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: [email protected]
Cc: [email protected]
Suggested-by: Eric Biederman <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
---
fs/binfmt_elf.c | 23 ++++-------------------
1 file changed, 4 insertions(+), 19 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index db47cb802f89..f8b4747f87ed 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1351,30 +1351,15 @@ static int load_elf_library(struct file *file)
eppnt++;

/* Now use mmap to map the library into memory. */
- error = vm_mmap(file,
- ELF_PAGESTART(eppnt->p_vaddr),
- (eppnt->p_filesz +
- ELF_PAGEOFFSET(eppnt->p_vaddr)),
+ error = elf_load(file, ELF_PAGESTART(eppnt->p_vaddr),
+ eppnt,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED_NOREPLACE | MAP_PRIVATE,
- (eppnt->p_offset -
- ELF_PAGEOFFSET(eppnt->p_vaddr)));
- if (error != ELF_PAGESTART(eppnt->p_vaddr))
- goto out_free_ph;
+ 0);

- elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
- if (padzero(elf_bss)) {
- error = -EFAULT;
+ if (error != ELF_PAGESTART(eppnt->p_vaddr))
goto out_free_ph;
- }

- len = ELF_PAGEALIGN(eppnt->p_filesz + eppnt->p_vaddr);
- bss = ELF_PAGEALIGN(eppnt->p_memsz + eppnt->p_vaddr);
- if (bss > len) {
- error = vm_brk(len, bss - len);
- if (error)
- goto out_free_ph;
- }
error = 0;

out_free_ph:
--
2.34.1


2023-09-29 14:30:26

by Pedro Falcato

[permalink] [raw]
Subject: Re: [PATCH v4 4/6] binfmt_elf: Use elf_load() for library

On Fri, Sep 29, 2023 at 4:24 AM Kees Cook <[email protected]> wrote:
>
> While load_elf_library() is a libc5-ism, we can still replace most of
> its contents with elf_load() as well, further simplifying the code.

While I understand you want to break as little as possible (as the ELF
loader maintainer), I'm wondering if we could axe CONFIG_USELIB
altogether? Since CONFIG_BINFMT_AOUT also got axed. Does this have
users anywhere?

--
Pedro

2023-09-29 15:58:09

by Eric W. Biederman

[permalink] [raw]
Subject: Re: [PATCH v4 4/6] binfmt_elf: Use elf_load() for library

Pedro Falcato <[email protected]> writes:

> On Fri, Sep 29, 2023 at 4:24 AM Kees Cook <[email protected]> wrote:
>>
>> While load_elf_library() is a libc5-ism, we can still replace most of
>> its contents with elf_load() as well, further simplifying the code.
>
> While I understand you want to break as little as possible (as the ELF
> loader maintainer), I'm wondering if we could axe CONFIG_USELIB
> altogether? Since CONFIG_BINFMT_AOUT also got axed. Does this have
> users anywhere?

As I recall:
- libc4 was a.out and used uselib.
- libc5 was elf and used uselib.
- libc6 is elf and has never used uselib.

Anything using libc5 is extremely rare. It is an entire big process to
see if there are any users in existence.

In the meantime changing load_elf_library to use elf_load removes any
maintenance burden.

Eric

2023-09-29 18:28:43

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v4 4/6] binfmt_elf: Use elf_load() for library

On Fri, Sep 29, 2023 at 01:12:13PM +0100, Pedro Falcato wrote:
> On Fri, Sep 29, 2023 at 4:24 AM Kees Cook <[email protected]> wrote:
> >
> > While load_elf_library() is a libc5-ism, we can still replace most of
> > its contents with elf_load() as well, further simplifying the code.
>
> While I understand you want to break as little as possible (as the ELF
> loader maintainer), I'm wondering if we could axe CONFIG_USELIB
> altogether? Since CONFIG_BINFMT_AOUT also got axed. Does this have
> users anywhere?

I can't even find a libc5 image I can test. :P

I made it non-default in '22:

7374fa33dc2d ("init/Kconfig: remove USELIB syscall by default")

I'm not sure we can drop it entirely, though.

--
Kees Cook