Building the interpreter's argument vector is the bulk of load_misc_binary() and the one part of it that is specific to the classic dispatch. Move it into its own helper so what remains reads as what it is: pick a handler, pick an interpreter, build the invocation, open it. No functional change. Signed-off-by: Christian Brauner (Amutable) --- fs/binfmt_misc.c | 84 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c index 63c10066e3a6..f0ccc5d000f6 100644 --- a/fs/binfmt_misc.c +++ b/fs/binfmt_misc.c @@ -382,40 +382,23 @@ static struct file *entry_open_interpreter(const struct binfmt_misc_entry *e, return no_free_ptr(interp_file); } -/* - * the loader itself +/** + * build_interp_argv - splice the interpreter invocation into the argv + * @bprm: binary that is being executed + * @interpreter: the interpreter selected for this exec + * @flags: invocation flags in effect for this exec + * + * The interpreter becomes argv[0] and the binary its last argument, with an + * optional staged argument in between. The caller's argv[0] is dropped + * unless 'P' keeps it. + * + * Return: 0 on success, a negative error code on failure */ -static int load_misc_binary(struct linux_binprm *bprm) +static int build_interp_argv(struct linux_binprm *bprm, const char *interpreter, + unsigned long flags) { - struct binfmt_misc_entry *fmt __free(put_binfmt_handler) = NULL; - const char *interpreter; - struct file *interp_file; - struct binfmt_misc *misc; - unsigned long flags; int retval; - misc = current_binfmt_misc(); - if (!READ_ONCE(misc->enabled)) - return -ENOEXEC; - - fmt = get_binfmt_handler(misc, bprm); - if (!fmt) - return -ENOEXEC; - - /* Need to be able to load the file after exec */ - if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) - return -ENOENT; - - interpreter = entry_select_interpreter(fmt, bprm); - if (IS_ERR(interpreter)) - return PTR_ERR(interpreter); - - flags = entry_invocation_flags(fmt, bprm); - if (flags & MISC_FMT_CREDENTIALS) - bprm->execfd_creds = 1; - if (flags & MISC_FMT_OPEN_BINARY) - bprm->have_execfd = 1; - /* The entry's own choice - not one accumulated from an earlier level. */ if (flags & MISC_FMT_PRESERVE_ARGV0) { bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0; @@ -451,6 +434,47 @@ static int load_misc_binary(struct linux_binprm *bprm) return retval; bprm->argc++; + return 0; +} + +/* + * the loader itself + */ +static int load_misc_binary(struct linux_binprm *bprm) +{ + struct binfmt_misc_entry *fmt __free(put_binfmt_handler) = NULL; + const char *interpreter; + struct file *interp_file; + struct binfmt_misc *misc; + unsigned long flags; + int retval; + + misc = current_binfmt_misc(); + if (!READ_ONCE(misc->enabled)) + return -ENOEXEC; + + fmt = get_binfmt_handler(misc, bprm); + if (!fmt) + return -ENOEXEC; + + /* Need to be able to load the file after exec */ + if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) + return -ENOENT; + + interpreter = entry_select_interpreter(fmt, bprm); + if (IS_ERR(interpreter)) + return PTR_ERR(interpreter); + + flags = entry_invocation_flags(fmt, bprm); + if (flags & MISC_FMT_CREDENTIALS) + bprm->execfd_creds = 1; + if (flags & MISC_FMT_OPEN_BINARY) + bprm->have_execfd = 1; + + retval = build_interp_argv(bprm, interpreter, flags); + if (retval) + return retval; + /* Update interp in case binfmt_script needs it. */ retval = bprm_change_interp(interpreter, bprm); if (retval < 0) -- 2.53.0