^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/binfmt_script.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1996 Martin von Löwis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * original #!-checking implemented by tytso.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/binfmts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static inline const char *next_non_spacetab(const char *first, const char *last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) for (; first <= last; first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (!spacetab(*first))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static inline const char *next_terminator(const char *first, const char *last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (; first <= last; first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (spacetab(*first) || !*first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int load_script(struct linux_binprm *bprm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const char *i_name, *i_sep, *i_arg, *i_end, *buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Not ours to exec if we don't start with "#!". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * This section handles parsing the #! line into separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * interpreter path and argument strings. We must be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * because bprm->buf is not yet guaranteed to be NUL-terminated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * (though the buffer will have trailing NUL padding when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * file size was smaller than the buffer size).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * We do not want to exec a truncated interpreter path, so either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * we find a newline (which indicates nothing is truncated), or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * we find a space/tab/NUL after the interpreter path (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * itself may be preceded by spaces/tabs). Truncating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * arguments is fine: the interpreter can re-read the script to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * parse them on its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) buf_end = bprm->buf + sizeof(bprm->buf) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!i_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) i_end = next_non_spacetab(bprm->buf + 2, buf_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!i_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -ENOEXEC; /* Entire buf is spaces/tabs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * If there is no later space/tab/NUL we must assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * interpreter path is truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!next_terminator(i_end, buf_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) i_end = buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Trim any trailing spaces/tabs from i_end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) while (spacetab(i_end[-1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) i_end--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Skip over leading spaces/tabs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) i_name = next_non_spacetab(bprm->buf+2, i_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!i_name || (i_name == i_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -ENOEXEC; /* No interpreter name found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Is there an optional argument? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) i_arg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) i_sep = next_terminator(i_name, i_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (i_sep && (*i_sep != '\0'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) i_arg = next_non_spacetab(i_sep, i_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * If the script filename will be inaccessible after exec, typically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * up now (on the assumption that the interpreter will want to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * this file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * OK, we've parsed out the interpreter name and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * (optional) argument.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Splice in (1) the interpreter's name for argv[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * (2) (optional) argument to interpreter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * (3) filename of shell script (replace argv[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * This is done in reverse order, because of how the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * user environment and arguments are stored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) retval = remove_arg_zero(bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) retval = copy_string_kernel(bprm->interp, bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bprm->argc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *((char *)i_end) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (i_arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) *((char *)i_sep) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) retval = copy_string_kernel(i_arg, bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) bprm->argc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) retval = copy_string_kernel(i_name, bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bprm->argc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) retval = bprm_change_interp(i_name, bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * OK, now restart the process with the interpreter's dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) file = open_exec(i_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (IS_ERR(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) bprm->interpreter = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static struct linux_binfmt script_format = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .load_binary = load_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int __init init_script_binfmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) register_binfmt(&script_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void __exit exit_script_binfmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unregister_binfmt(&script_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) core_initcall(init_script_binfmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) module_exit(exit_script_binfmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_LICENSE("GPL");