Skip to content

Commit 42a7a2a

Browse files
thejhgregkh
authored andcommitted
fs/binfmt_flat.c: make load_flat_shared_library() work
commit 867bfa4 upstream. 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. Link: http://lkml.kernel.org/r/20190524201817.16509-1-jannh@google.com Fixes: 287980e ("remove lots of IS_ERR_VALUE abuses") Signed-off-by: Jann Horn <jannh@google.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Kees Cook <keescook@chromium.org> Cc: Nicolas Pitre <nicolas.pitre@linaro.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Greg Ungerer <gerg@linux-m68k.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 730855b commit 42a7a2a

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

fs/binfmt_flat.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,14 @@ static int load_flat_file(struct linux_binprm *bprm,
859859

860860
static int load_flat_shared_library(int id, struct lib_info *libs)
861861
{
862+
/*
863+
* This is a fake bprm struct; only the members "buf", "file" and
864+
* "filename" are actually used.
865+
*/
862866
struct linux_binprm bprm;
863867
int res;
864868
char buf[16];
869+
loff_t pos = 0;
865870

866871
memset(&bprm, 0, sizeof(bprm));
867872

@@ -875,25 +880,11 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
875880
if (IS_ERR(bprm.file))
876881
return res;
877882

878-
bprm.cred = prepare_exec_creds();
879-
res = -ENOMEM;
880-
if (!bprm.cred)
881-
goto out;
882-
883-
/* We don't really care about recalculating credentials at this point
884-
* as we're past the point of no return and are dealing with shared
885-
* libraries.
886-
*/
887-
bprm.cred_prepared = 1;
883+
res = kernel_read(bprm.file, pos, bprm.buf, BINPRM_BUF_SIZE);
888884

889-
res = prepare_binprm(&bprm);
890-
891-
if (!res)
885+
if (res >= 0)
892886
res = load_flat_file(&bprm, libs, id, NULL);
893887

894-
abort_creds(bprm.cred);
895-
896-
out:
897888
allow_write_access(bprm.file);
898889
fput(bprm.file);
899890

0 commit comments

Comments
 (0)