Skip to content

Commit 3a2e7f4

Browse files
xemultorvalds
authored andcommitted
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>
1 parent cbd9b67 commit 3a2e7f4

4 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎fs/binfmt_em86.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
4343
return -ENOEXEC;
4444
}
4545

46-
bprm->sh_bang++; /* Well, the bang-shell is implicit... */
46+
bprm->sh_bang = 1; /* Well, the bang-shell is implicit... */
4747
allow_write_access(bprm->file);
4848
fput(bprm->file);
4949
bprm->file = NULL;

‎fs/binfmt_misc.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
115115
if (!enabled)
116116
goto _ret;
117117

118+
retval = -ENOEXEC;
119+
if (bprm->misc_bang)
120+
goto _ret;
121+
122+
bprm->misc_bang = 1;
123+
118124
/* to keep locking time low, we copy the interpreter string */
119125
read_lock(&entries_lock);
120126
fmt = check_file(bprm);

‎fs/binfmt_script.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
2929
* Sorta complicated, but hopefully it will work. -TYT
3030
*/
3131

32-
bprm->sh_bang++;
32+
bprm->sh_bang = 1;
3333
allow_write_access(bprm->file);
3434
fput(bprm->file);
3535
bprm->file = NULL;

‎include/linux/binfmts.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct linux_binprm{
3434
#endif
3535
struct mm_struct *mm;
3636
unsigned long p; /* current top of mem */
37-
int sh_bang;
37+
unsigned int sh_bang:1,
38+
misc_bang:1;
3839
struct file * file;
3940
int e_uid, e_gid;
4041
kernel_cap_t cap_inheritable, cap_permitted;

0 commit comments

Comments
 (0)