Commit 3a2e7f47 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by Linus Torvalds

binfmt_misc.c: avoid potential kernel stack overflow

This can be triggered with root help only, but...

Register the ":text:E::txt::/root/cat.txt:' rule in binfmt_misc (by root) and
try launching the cat.txt file (by anyone) :) The result is - the endless
recursion in the load_misc_binary -> open_exec -> load_misc_binary chain and
stack overflow.

There's a similar problem with binfmt_script, and there's a sh_bang memner on
linux_binprm structure to handle this, but simply raising this in binfmt_misc
may break some setups when the interpreter of some misc binaries is a script.

So the proposal is to turn sh_bang into a bit, add a new one (the misc_bang)
and raise it in load_misc_binary.  After this, even if we set up the misc ->
script -> misc loop for binfmts one of them will step on its own bang and
exit.
Signed-off-by: default avatarPavel Emelyanov <xemul@openvz.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent cbd9b67b
...@@ -43,7 +43,7 @@ static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs) ...@@ -43,7 +43,7 @@ static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
return -ENOEXEC; return -ENOEXEC;
} }
bprm->sh_bang++; /* Well, the bang-shell is implicit... */ bprm->sh_bang = 1; /* Well, the bang-shell is implicit... */
allow_write_access(bprm->file); allow_write_access(bprm->file);
fput(bprm->file); fput(bprm->file);
bprm->file = NULL; bprm->file = NULL;
......
...@@ -115,6 +115,12 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs) ...@@ -115,6 +115,12 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
if (!enabled) if (!enabled)
goto _ret; goto _ret;
retval = -ENOEXEC;
if (bprm->misc_bang)
goto _ret;
bprm->misc_bang = 1;
/* to keep locking time low, we copy the interpreter string */ /* to keep locking time low, we copy the interpreter string */
read_lock(&entries_lock); read_lock(&entries_lock);
fmt = check_file(bprm); fmt = check_file(bprm);
......
...@@ -29,7 +29,7 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs) ...@@ -29,7 +29,7 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
* Sorta complicated, but hopefully it will work. -TYT * Sorta complicated, but hopefully it will work. -TYT
*/ */
bprm->sh_bang++; bprm->sh_bang = 1;
allow_write_access(bprm->file); allow_write_access(bprm->file);
fput(bprm->file); fput(bprm->file);
bprm->file = NULL; bprm->file = NULL;
......
...@@ -34,7 +34,8 @@ struct linux_binprm{ ...@@ -34,7 +34,8 @@ struct linux_binprm{
#endif #endif
struct mm_struct *mm; struct mm_struct *mm;
unsigned long p; /* current top of mem */ unsigned long p; /* current top of mem */
int sh_bang; unsigned int sh_bang:1,
misc_bang:1;
struct file * file; struct file * file;
int e_uid, e_gid; int e_uid, e_gid;
kernel_cap_t cap_inheritable, cap_permitted; kernel_cap_t cap_inheritable, cap_permitted;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment