xref: /openbmc/linux/fs/binfmt_script.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/binfmt_script.c
41da177e4SLinus Torvalds  *
596de0e25SJan Engelhardt  *  Copyright (C) 1996  Martin von Löwis
61da177e4SLinus Torvalds  *  original #!-checking implemented by tytso.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/string.h>
111da177e4SLinus Torvalds #include <linux/stat.h>
121da177e4SLinus Torvalds #include <linux/binfmts.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/file.h>
151da177e4SLinus Torvalds #include <linux/err.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds 
spacetab(char c)18b5372fe5SKees Cook static inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
next_non_spacetab(const char * first,const char * last)19ccbb18b6SEric W. Biederman static inline const char *next_non_spacetab(const char *first, const char *last)
20b5372fe5SKees Cook {
21b5372fe5SKees Cook 	for (; first <= last; first++)
22b5372fe5SKees Cook 		if (!spacetab(*first))
23b5372fe5SKees Cook 			return first;
24b5372fe5SKees Cook 	return NULL;
25b5372fe5SKees Cook }
next_terminator(const char * first,const char * last)26ccbb18b6SEric W. Biederman static inline const char *next_terminator(const char *first, const char *last)
27b5372fe5SKees Cook {
28b5372fe5SKees Cook 	for (; first <= last; first++)
29b5372fe5SKees Cook 		if (spacetab(*first) || !*first)
30b5372fe5SKees Cook 			return first;
31b5372fe5SKees Cook 	return NULL;
32b5372fe5SKees Cook }
33b5372fe5SKees Cook 
load_script(struct linux_binprm * bprm)3471613c3bSAl Viro static int load_script(struct linux_binprm *bprm)
351da177e4SLinus Torvalds {
36ccbb18b6SEric W. Biederman 	const char *i_name, *i_sep, *i_arg, *i_end, *buf_end;
371da177e4SLinus Torvalds 	struct file *file;
381da177e4SLinus Torvalds 	int retval;
391da177e4SLinus Torvalds 
40b5372fe5SKees Cook 	/* Not ours to exec if we don't start with "#!". */
41d7402698SKees Cook 	if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
421da177e4SLinus Torvalds 		return -ENOEXEC;
4351f39a1fSDavid Drysdale 
4451f39a1fSDavid Drysdale 	/*
45b5372fe5SKees Cook 	 * This section handles parsing the #! line into separate
46b5372fe5SKees Cook 	 * interpreter path and argument strings. We must be careful
47b5372fe5SKees Cook 	 * because bprm->buf is not yet guaranteed to be NUL-terminated
48b5372fe5SKees Cook 	 * (though the buffer will have trailing NUL padding when the
49b5372fe5SKees Cook 	 * file size was smaller than the buffer size).
50b5372fe5SKees Cook 	 *
51b5372fe5SKees Cook 	 * We do not want to exec a truncated interpreter path, so either
52b5372fe5SKees Cook 	 * we find a newline (which indicates nothing is truncated), or
53b5372fe5SKees Cook 	 * we find a space/tab/NUL after the interpreter path (which
54b5372fe5SKees Cook 	 * itself may be preceded by spaces/tabs). Truncating the
55b5372fe5SKees Cook 	 * arguments is fine: the interpreter can re-read the script to
56b5372fe5SKees Cook 	 * parse them on its own.
57b5372fe5SKees Cook 	 */
58b5372fe5SKees Cook 	buf_end = bprm->buf + sizeof(bprm->buf) - 1;
59ccbb18b6SEric W. Biederman 	i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
60ccbb18b6SEric W. Biederman 	if (!i_end) {
61ccbb18b6SEric W. Biederman 		i_end = next_non_spacetab(bprm->buf + 2, buf_end);
62ccbb18b6SEric W. Biederman 		if (!i_end)
63b5372fe5SKees Cook 			return -ENOEXEC; /* Entire buf is spaces/tabs */
64b5372fe5SKees Cook 		/*
65b5372fe5SKees Cook 		 * If there is no later space/tab/NUL we must assume the
66b5372fe5SKees Cook 		 * interpreter path is truncated.
67b5372fe5SKees Cook 		 */
68ccbb18b6SEric W. Biederman 		if (!next_terminator(i_end, buf_end))
69b5372fe5SKees Cook 			return -ENOEXEC;
70ccbb18b6SEric W. Biederman 		i_end = buf_end;
71b5372fe5SKees Cook 	}
72ccbb18b6SEric W. Biederman 	/* Trim any trailing spaces/tabs from i_end */
73ccbb18b6SEric W. Biederman 	while (spacetab(i_end[-1]))
74ccbb18b6SEric W. Biederman 		i_end--;
75ccbb18b6SEric W. Biederman 
76ccbb18b6SEric W. Biederman 	/* Skip over leading spaces/tabs */
77ccbb18b6SEric W. Biederman 	i_name = next_non_spacetab(bprm->buf+2, i_end);
78ccbb18b6SEric W. Biederman 	if (!i_name || (i_name == i_end))
791da177e4SLinus Torvalds 		return -ENOEXEC; /* No interpreter name found */
80ccbb18b6SEric W. Biederman 
81ccbb18b6SEric W. Biederman 	/* Is there an optional argument? */
821da177e4SLinus Torvalds 	i_arg = NULL;
83ccbb18b6SEric W. Biederman 	i_sep = next_terminator(i_name, i_end);
84ccbb18b6SEric W. Biederman 	if (i_sep && (*i_sep != '\0'))
85ccbb18b6SEric W. Biederman 		i_arg = next_non_spacetab(i_sep, i_end);
86ccbb18b6SEric W. Biederman 
87ccbb18b6SEric W. Biederman 	/*
88ccbb18b6SEric W. Biederman 	 * If the script filename will be inaccessible after exec, typically
89ccbb18b6SEric W. Biederman 	 * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
90ccbb18b6SEric W. Biederman 	 * up now (on the assumption that the interpreter will want to load
91ccbb18b6SEric W. Biederman 	 * this file).
92ccbb18b6SEric W. Biederman 	 */
93ccbb18b6SEric W. Biederman 	if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
94ccbb18b6SEric W. Biederman 		return -ENOENT;
95ccbb18b6SEric W. Biederman 
961da177e4SLinus Torvalds 	/*
971da177e4SLinus Torvalds 	 * OK, we've parsed out the interpreter name and
981da177e4SLinus Torvalds 	 * (optional) argument.
991da177e4SLinus Torvalds 	 * Splice in (1) the interpreter's name for argv[0]
1001da177e4SLinus Torvalds 	 *           (2) (optional) argument to interpreter
1011da177e4SLinus Torvalds 	 *           (3) filename of shell script (replace argv[0])
1021da177e4SLinus Torvalds 	 *
1031da177e4SLinus Torvalds 	 * This is done in reverse order, because of how the
1041da177e4SLinus Torvalds 	 * user environment and arguments are stored.
1051da177e4SLinus Torvalds 	 */
106b6a2fea3SOllie Wild 	retval = remove_arg_zero(bprm);
107b6a2fea3SOllie Wild 	if (retval)
108b6a2fea3SOllie Wild 		return retval;
109*986db2d1SChristoph Hellwig 	retval = copy_string_kernel(bprm->interp, bprm);
110c2315c18SOleg Nesterov 	if (retval < 0)
111c2315c18SOleg Nesterov 		return retval;
1121da177e4SLinus Torvalds 	bprm->argc++;
113ccbb18b6SEric W. Biederman 	*((char *)i_end) = '\0';
1141da177e4SLinus Torvalds 	if (i_arg) {
115ccbb18b6SEric W. Biederman 		*((char *)i_sep) = '\0';
116*986db2d1SChristoph Hellwig 		retval = copy_string_kernel(i_arg, bprm);
117c2315c18SOleg Nesterov 		if (retval < 0)
118c2315c18SOleg Nesterov 			return retval;
1191da177e4SLinus Torvalds 		bprm->argc++;
1201da177e4SLinus Torvalds 	}
121*986db2d1SChristoph Hellwig 	retval = copy_string_kernel(i_name, bprm);
122c2315c18SOleg Nesterov 	if (retval)
123c2315c18SOleg Nesterov 		return retval;
1241da177e4SLinus Torvalds 	bprm->argc++;
125c2315c18SOleg Nesterov 	retval = bprm_change_interp(i_name, bprm);
126b66c5984SKees Cook 	if (retval < 0)
127b66c5984SKees Cook 		return retval;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/*
1301da177e4SLinus Torvalds 	 * OK, now restart the process with the interpreter's dentry.
1311da177e4SLinus Torvalds 	 */
132c2315c18SOleg Nesterov 	file = open_exec(i_name);
1331da177e4SLinus Torvalds 	if (IS_ERR(file))
1341da177e4SLinus Torvalds 		return PTR_ERR(file);
1351da177e4SLinus Torvalds 
136bc2bf338SEric W. Biederman 	bprm->interpreter = file;
137bc2bf338SEric W. Biederman 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static struct linux_binfmt script_format = {
1411da177e4SLinus Torvalds 	.module		= THIS_MODULE,
1421da177e4SLinus Torvalds 	.load_binary	= load_script,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
init_script_binfmt(void)1451da177e4SLinus Torvalds static int __init init_script_binfmt(void)
1461da177e4SLinus Torvalds {
1478fc3dc5aSAl Viro 	register_binfmt(&script_format);
1488fc3dc5aSAl Viro 	return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
exit_script_binfmt(void)1511da177e4SLinus Torvalds static void __exit exit_script_binfmt(void)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	unregister_binfmt(&script_format);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds core_initcall(init_script_binfmt);
1571da177e4SLinus Torvalds module_exit(exit_script_binfmt);
1581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
159