Skip to contentSkip to conversation

binfmt_flat: make load_flat_shared_library() work

PATCH Applied Jann Horn 10 participants 18 messages May 24, 2019 – Jun 2, 2019 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 binfmt_flat: make load_flat_shared_library() work

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.
akpm's tree is the right one for this patch, right?

 fs/binfmt_flat.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
fs/binfmt_flat.c +7 -16
index 82a48e830018..e4b59e76afb0 100644
@@ -856,9 +856,14 @@ static int load_flat_file(struct linux_binprm *bprm,
 
 static int load_flat_shared_library(int id, struct lib_info *libs)
 {
+	/*
+	 * This is a fake bprm struct; only the members "buf", "file" and
+	 * "filename" are actually used.
+	 */
 	struct linux_binprm bprm;
 	int res;
 	char buf[16];
+	loff_t pos = 0;
 
 	memset(&bprm, 0, sizeof(bprm));
 
@@ -872,25 +877,11 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 	if (IS_ERR(bprm.file))
 		return res;
 
-	bprm.cred = prepare_exec_creds();
-	res = -ENOMEM;
-	if (!bprm.cred)
-		goto out;
-
-	/* We don't really care about recalculating credentials at this point
-	 * as we're past the point of no return and are dealing with shared
-	 * libraries.
-	 */
-	bprm.called_set_creds = 1;
+	res = kernel_read(bprm.file, bprm.buf, BINPRM_BUF_SIZE, &pos);
 
-	res = prepare_binprm(&bprm);
-
-	if (!res)
+	if (res >= 0)
 		res = load_flat_file(&bprm, libs, id, NULL);
 
-	abort_creds(bprm.cred);
-
-out:
 	allow_write_access(bprm.file);
 	fput(bprm.file);
 
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
#1 lore raw
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 12 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")
Quoted 6 lines

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

author
#2 lore raw
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 12 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")
Quoted 6 lines

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

#3 lore raw
Quoted 28 lines · Jann Horn
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

Last time I played with this, I couldn't figure out the toolchain to produce shared libs. Only static executables worked fine. If I recall, existing binfmt_flat distros don't use shared libs either.

For shared lib support on no-MMU target, binfmt_elf_fdpic is a much saner choice.

Nicolas

#4 lore raw
In reply to Jann Horn
Quoted 28 lines · Jann Horn
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

Regards
Greg

#5 lore raw
Quoted 41 lines · Greg Ungerer
Quoted 26 lines · Jann Horn
Quoted 24 lines · Andrew Morton
Quoted 22 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

Regards
Greg

#6 lore raw
In reply to Jann Horn
Quoted 28 lines · Jann Horn
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

Regards
Greg

#7 lore raw
Quoted 29 lines · Greg Ungerer
Quoted 28 lines · Jann Horn
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

I think Emcraft have a significant customer base running ARM NOMMU Linux, I wonder whether they would have run into this (adding Sergei to Cc).
My suspicion is that they use only binfmt-elf-fdpic, not binfmt-flat.

The only architectures I see that enable binfmt-flat are sh, xtensa and h8300, but only arch/sh uses CONFIG_BINFMT_SHARED_FLAT for a few machine specific configurations, and I'm in turn fairly sure those machines have not run a recent kernel in many years.

The one SH nommu platform likely to have users is j2, and that is probably always used with musl-libc with elf-fdpic (given that Rich Felker maintains both the kernel port and the library).

Arnd

#8 lore raw
Quoted 39 lines · Arnd Bergmann
Quoted 29 lines · Greg Ungerer
Quoted 28 lines · Jann Horn
Quoted 26 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")

---
I only found the bug by looking at the code, I have not verified its existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a look at this patch.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

287980e49ffc was three years ago! Has it really been broken for all that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

Quoted 7 lines

I think Emcraft have a significant customer base running ARM NOMMU Linux, I wonder whether they would have run into this (adding Sergei to Cc).
My suspicion is that they use only binfmt-elf-fdpic, not binfmt-flat.

The only architectures I see that enable binfmt-flat are sh, xtensa and h8300, but only arch/sh uses CONFIG_BINFMT_SHARED_FLAT

m68k uses enables it too. It is the only binary format supported when running no-mmu on m68k. (You can use it with MMU enabled too if you really want too).

The shared flat format has been used on m68k in the past (it was originally developed on m68k platforms). But I haven't used them for a long time (probably 10 years at least) on m68k.

Regards
Greg

#9 lore raw
Quoted 16 lines · Greg Ungerer
Quoted 15 lines · Arnd Bergmann
Quoted 8 lines · Greg Ungerer
Quoted 4 lines · Jann Horn

On Sat, May 25, 2019 at 11:43 PM Andrew Morton <[email protected]> wrote:
Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

I think Emcraft have a significant customer base running ARM NOMMU Linux, I wonder whether they would have run into this (adding Sergei to Cc).
My suspicion is that they use only binfmt-elf-fdpic, not binfmt-flat.

Quoted 6 lines
Quoted 2 lines

The only architectures I see that enable binfmt-flat are sh, xtensa and h8300, but only arch/sh uses CONFIG_BINFMT_SHARED_FLAT

m68k uses enables it too. It is the only binary format supported when running no-mmu on m68k. (You can use it with MMU enabled too if you really want too).

My mistake, I meant to write 'the only architectures /other than m68k/", which you had already mentioned above.

Arnd

#10 lore raw
In reply to Arnd Bergmann

Hello Arnd, all,

Quoted 52 lines · Arnd Bergmann
Quoted 50 lines · Greg Ungerer
Quoted 48 lines · Jann Horn
Quoted 37 lines · Andrew Morton
Quoted 5 lines · On Fri, 24 May 2019 22:18:17 +0200 Jann Horn <[email protected]> wrote:

load_flat_shared_library() is broken: It only calls load_flat_file() if
prepare_binprm() returns zero, but prepare_binprm() returns the number of
bytes read - so this only happens if the file is empty.

ouch.

Quoted 18 lines

Instead, call into load_flat_file() if the number of bytes read is
non-negative. (Even if the number of bytes is zero - in that case,
load_flat_file() will see nullbytes and return a nice -ENOEXEC.)

In addition, remove the code related to bprm creds and stop using
prepare_binprm() - this code is loading a library, not a main executable,
and it only actually uses the members "buf", "file" and "filename" of the
linux_binprm struct. Instead, call kernel_read() directly.

Fixes 287980e49ffc ("remove lots of IS_ERR_VALUE abuses")
Quoted 8 lines

---
I only found the bug by looking at the code, I have not verified its
existence at runtime.
Also, this patch is compile-tested only.
It would be nice if someone who works with nommu Linux could have a
look at this patch.

Quoted 4 lines

287980e49ffc was three years ago! Has it really been broken for all
that time? If so, it seems a good source of freed disk space...

Maybe... but I didn't want to rip it out without having one of the
maintainers confirm that this really isn't likely to be used anymore.

Quoted 8 lines
Quoted 2 lines

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

I think Emcraft have a significant customer base running ARM NOMMU Linux, I wonder whether they would have run into this (adding Sergei to Cc).
My suspicion is that they use only binfmt-elf-fdpic, not binfmt-flat.

We use both, acutally, but all-static. We don't support shared libraries with bFLT or ELF FDPIC.

Kind regards,
Sergei

Quoted 12 lines

The only architectures I see that enable binfmt-flat are sh, xtensa and h8300, but only arch/sh uses CONFIG_BINFMT_SHARED_FLAT for a few machine specific configurations, and I'm in turn fairly sure
those machines have not run a recent kernel in many years.

The one SH nommu platform likely to have users is j2, and that is probably always used with musl-libc with elf-fdpic (given that Rich Felker maintains both the kernel port and the library).

Arnd

#11 lore raw
In reply to Greg Ungerer
Quoted 5 lines · Greg Ungerer
Quoted 2 lines

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

So, they should keep working.

Thanks,
Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
author
#12 lore raw
Quoted 8 lines · John Paul Adrian Glaubitz
Quoted 5 lines · Greg Ungerer
Quoted 2 lines

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

And you're using FLAT shared libraries, not ELF / FDPIC ELF shared libraries? See <https://lore.kernel.org/lkml/[email protected]/>
for context - this thread is about CONFIG_BINFMT_SHARED_FLAT.

#13 lore raw
Quoted 8 lines · Jann Horn
Quoted 4 lines
Quoted 2 lines

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

And you're using FLAT shared libraries, not ELF / FDPIC ELF shared libraries? See <https://lore.kernel.org/lkml/[email protected]/>
for context - this thread is about CONFIG_BINFMT_SHARED_FLAT.

No, we're using ELF binaries only:

root@pacman:~# grep CONFIG_BINFMT /boot/config-$(uname -r)
CONFIG_BINFMT_ELF=y
CONFIG_BINFMT_SCRIPT=y
# CONFIG_BINFMT_FLAT is not set
# CONFIG_BINFMT_AOUT is not set
CONFIG_BINFMT_MISC=m
root@pacman:~#

Thanks,
Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#14 lore raw
In reply to John Paul Adrian Glaubitz
Quoted 8 lines · John Paul Adrian Glaubitz
Quoted 5 lines · Greg Ungerer
Quoted 2 lines

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

When used on no-mmu platforms?

Regards
Greg

Quoted 5 lines

So, they should keep working.

Thanks,
Adrian

#15 lore raw
In reply to John Paul Adrian Glaubitz
Quoted 8 lines · John Paul Adrian Glaubitz
Quoted 5 lines · Greg Ungerer
Quoted 2 lines

Maybe... but I didn't want to rip it out without having one of the maintainers confirm that this really isn't likely to be used anymore.

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

Nope, I don't use non-mmu.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#16 lore raw
Quoted 6 lines · Andreas Schwab
Quoted 4 lines
Quoted 2 lines

I have not used shared libraries on m68k non-mmu setups for a very long time. At least 10 years I would think.

We use shared libraries in Debian on m68k and Andreas Schwab uses them on openSUSE/m68k.

Nope, I don't use non-mmu.

Sorry, I missed the "non-mmu" part in Greg's mail :).

Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#18 lore raw
Quoted 12 lines · Sasha Levin

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag, fixing commit: 287980e49ffc0 remove lots of IS_ERR_VALUE abuses.

The bot has tested the following trees: v5.1.4, v5.0.18, v4.19.45, v4.14.121, v4.9.178.

v5.1.4: Build OK!
v5.0.18: Build OK!
v4.19.45: Build OK!
v4.14.121: Build OK!
v4.9.178: Failed to apply! Possible dependencies:

[...]

Quoted 1 line
    ddb4a1442def2 ("exec: Rename bprm->cred_prepared to called_set_creds")

[...]

Quoted 1 line

How should we proceed with this patch?

I think the dependency is on ddb4a1442def2; but the simplest way is probably to manually adjust the fix, it's basically the same in 4.9.