• Eric W. Biederman's avatar
    exec/binfmt_script: Don't modify bprm->buf and then return -ENOEXEC · ccbb18b6
    Eric W. Biederman authored
    The return code -ENOEXEC serves to tell search_binary_handler that it
    should continue searching for the binfmt to handle a given file.  This
    makes return -ENOEXEC with a bprm->buf that is needed to continue the
    search problematic.
    
    The current binfmt_script manages to escape problems as it closes and
    clears bprm->file before return -ENOEXEC with bprm->buf modified.
    This prevents search_binary_handler from looping as it explicitly
    handles a NULL bprm->file.
    
    I plan on moving all of the bprm->file managment into fs/exec.c and out
    of the binary handlers so this will become a problem.
    
    Move closing bprm->file and the test for BINPRM_PATH_INACCESSIBLE
    down below the last return of -ENOEXEC.
    
    Introduce i_sep and i_end to track the end of the first argument and
    the end of the parameters respectively.  Using those, constification
    of all char * pointers, and the helpers next_terminator and
    next_non_spacetab guarantee the parameter parsing will not modify
    bprm->buf.
    
    Only modify bprm->buf to terminate the strings i_arg and i_name with
    '\0' for passing to copy_strings_kernel.
    
    When replacing loops with next_non_spacetab and next_terminator care
    has been take that the logic of the parsing code (short of replacing
    characters by '\0') remains the same.
    
    Link: https://lkml.kernel.org/r/874ksczru6.fsf_-_@x220.int.ebiederm.org
    
    Acked-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    Reviewed-by: default avatarKees Cook <keescook@chromium.org>
    Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
    ccbb18b6
binfmt_script.c 4.36 KB