]> git.itanic.dy.fi Git - linux-stable/commitdiff
Allow recursion in binfmt_script and binfmt_misc
authorKirill A. Shutemov <kirill@shutemov.name>
Thu, 16 Oct 2008 05:02:39 +0000 (22:02 -0700)
committerGreg Kroah-Hartman <gregkh@suse.de>
Sat, 13 Dec 2008 23:29:33 +0000 (15:29 -0800)
commit bf2a9a39639b8b51377905397a5005f444e9a892 upstream.

binfmt_script and binfmt_misc disallow recursion to avoid stack overflow
using sh_bang and misc_bang.  It causes problem in some cases:

$ echo '#!/bin/ls' > /tmp/t0
$ echo '#!/tmp/t0' > /tmp/t1
$ echo '#!/tmp/t1' > /tmp/t2
$ chmod +x /tmp/t*
$ /tmp/t2
zsh: exec format error: /tmp/t2

Similar problem with binfmt_misc.

This patch introduces field 'recursion_depth' into struct linux_binprm to
track recursion level in binfmt_misc and binfmt_script.  If recursion
level more then BINPRM_MAX_RECURSION it generates -ENOEXEC.

[akpm@linux-foundation.org: make linux_binprm.recursion_depth a uint]
Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
Cc: Pavel Emelyanov <xemul@openvz.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
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@suse.de>
fs/binfmt_em86.c
fs/binfmt_misc.c
fs/binfmt_script.c
include/linux/binfmts.h

index f9c88d0c8cedeabcfdca1a5f81a51e1e293a59ea..32fb00b52cd069ed600d9c1a925f2b831621325e 100644 (file)
@@ -43,7 +43,7 @@ static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
                        return -ENOEXEC;
        }
 
-       bprm->sh_bang = 1;      /* Well, the bang-shell is implicit... */
+       bprm->recursion_depth++; /* Well, the bang-shell is implicit... */
        allow_write_access(bprm->file);
        fput(bprm->file);
        bprm->file = NULL;
index 8d7e88e02e0f9cc13207d86ee090a2594f79497b..f2744ab4e5b35c415724034f3f5a3a328997cf99 100644 (file)
@@ -117,7 +117,7 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
                goto _ret;
 
        retval = -ENOEXEC;
-       if (bprm->misc_bang)
+       if (bprm->recursion_depth > BINPRM_MAX_RECURSION)
                goto _ret;
 
        /* to keep locking time low, we copy the interpreter string */
@@ -197,7 +197,7 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
        if (retval < 0)
                goto _error;
 
-       bprm->misc_bang = 1;
+       bprm->recursion_depth++;
 
        retval = search_binary_handler (bprm, regs);
        if (retval < 0)
index 9e3963f7ebf19f85a1742ff034bbb518d51a6aff..08343505e18455dedebace59a8c489ecb3b1cf7a 100644 (file)
@@ -22,14 +22,15 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
        char interp[BINPRM_BUF_SIZE];
        int retval;
 
-       if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang)) 
+       if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') ||
+           (bprm->recursion_depth > BINPRM_MAX_RECURSION))
                return -ENOEXEC;
        /*
         * This section does the #! interpretation.
         * Sorta complicated, but hopefully it will work.  -TYT
         */
 
-       bprm->sh_bang = 1;
+       bprm->recursion_depth++;
        allow_write_access(bprm->file);
        fput(bprm->file);
        bprm->file = NULL;
index 826f62350805ed782976008db013203061306744..12413a1e27845fcabe6941f2b441aecfea3817e8 100644 (file)
@@ -36,6 +36,7 @@ struct linux_binprm{
        unsigned long p; /* current top of mem */
        unsigned int sh_bang:1,
                     misc_bang:1;
+       unsigned int recursion_depth;
        struct file * file;
        int e_uid, e_gid;
        kernel_cap_t cap_post_exec_permitted;
@@ -58,6 +59,7 @@ struct linux_binprm{
 #define BINPRM_FLAGS_EXECFD_BIT 1
 #define BINPRM_FLAGS_EXECFD (1 << BINPRM_FLAGS_EXECFD_BIT)
 
+#define BINPRM_MAX_RECURSION 4
 
 /*
  * This structure defines the functions that are used to load the binary formats that