Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Pavel Emelyanov <xemul@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Pavel Emelyanov and committed by
Linus Torvalds
3a2e7f47 cbd9b67b

+10 -3
+1 -1
fs/binfmt_em86.c
··· 43 43 return -ENOEXEC; 44 44 } 45 45 46 - bprm->sh_bang++; /* Well, the bang-shell is implicit... */ 46 + bprm->sh_bang = 1; /* Well, the bang-shell is implicit... */ 47 47 allow_write_access(bprm->file); 48 48 fput(bprm->file); 49 49 bprm->file = NULL;
+6
fs/binfmt_misc.c
··· 115 115 if (!enabled) 116 116 goto _ret; 117 117 118 + retval = -ENOEXEC; 119 + if (bprm->misc_bang) 120 + goto _ret; 121 + 122 + bprm->misc_bang = 1; 123 + 118 124 /* to keep locking time low, we copy the interpreter string */ 119 125 read_lock(&entries_lock); 120 126 fmt = check_file(bprm);
+1 -1
fs/binfmt_script.c
··· 29 29 * Sorta complicated, but hopefully it will work. -TYT 30 30 */ 31 31 32 - bprm->sh_bang++; 32 + bprm->sh_bang = 1; 33 33 allow_write_access(bprm->file); 34 34 fput(bprm->file); 35 35 bprm->file = NULL;
+2 -1
include/linux/binfmts.h
··· 34 34 #endif 35 35 struct mm_struct *mm; 36 36 unsigned long p; /* current top of mem */ 37 - int sh_bang; 37 + unsigned int sh_bang:1, 38 + misc_bang:1; 38 39 struct file * file; 39 40 int e_uid, e_gid; 40 41 kernel_cap_t cap_inheritable, cap_permitted;