xref: /openbmc/linux/fs/binfmt_script.c (revision 986db2d1)
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)19b5372fe5SKees Cook 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)26b5372fe5SKees Cook 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 {
36d7627467SDavid Howells 	const char *i_name, *i_sep, *i_arg, *i_end, *buf_end;
37b5372fe5SKees Cook 	struct file *file;
381da177e4SLinus Torvalds 	int retval;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	/* Not ours to exec if we don't start with "#!". */
41b5372fe5SKees Cook 	if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
42d7402698SKees Cook 		return -ENOEXEC;
431da177e4SLinus Torvalds 
4451f39a1fSDavid Drysdale 	/*
4551f39a1fSDavid Drysdale 	 * This section handles parsing the #! line into separate
4651f39a1fSDavid Drysdale 	 * interpreter path and argument strings. We must be careful
4751f39a1fSDavid Drysdale 	 * because bprm->buf is not yet guaranteed to be NUL-terminated
4851f39a1fSDavid Drysdale 	 * (though the buffer will have trailing NUL padding when the
4951f39a1fSDavid Drysdale 	 * file size was smaller than the buffer size).
5051f39a1fSDavid Drysdale 	 *
5151f39a1fSDavid Drysdale 	 * We do not want to exec a truncated interpreter path, so either
5251f39a1fSDavid Drysdale 	 * we find a newline (which indicates nothing is truncated), or
5351f39a1fSDavid Drysdale 	 * we find a space/tab/NUL after the interpreter path (which
54b5372fe5SKees Cook 	 * itself may be preceded by spaces/tabs). Truncating the
551da177e4SLinus Torvalds 	 * arguments is fine: the interpreter can re-read the script to
561da177e4SLinus Torvalds 	 * parse them on its own.
571da177e4SLinus Torvalds 	 */
581da177e4SLinus Torvalds 	buf_end = bprm->buf + sizeof(bprm->buf) - 1;
59b5372fe5SKees Cook 	i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
60b5372fe5SKees Cook 	if (!i_end) {
61b5372fe5SKees Cook 		i_end = next_non_spacetab(bprm->buf + 2, buf_end);
62b5372fe5SKees Cook 		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 		 */
68b5372fe5SKees Cook 		if (!next_terminator(i_end, buf_end))
69b5372fe5SKees Cook 			return -ENOEXEC;
70b5372fe5SKees Cook 		i_end = buf_end;
71b5372fe5SKees Cook 	}
72b5372fe5SKees Cook 	/* Trim any trailing spaces/tabs from i_end */
73b5372fe5SKees Cook 	while (spacetab(i_end[-1]))
74b5372fe5SKees Cook 		i_end--;
75b5372fe5SKees Cook 
76b5372fe5SKees Cook 	/* Skip over leading spaces/tabs */
77b5372fe5SKees Cook 	i_name = next_non_spacetab(bprm->buf+2, i_end);
78b5372fe5SKees Cook 	if (!i_name || (i_name == i_end))
79b5372fe5SKees Cook 		return -ENOEXEC; /* No interpreter name found */
80b5372fe5SKees Cook 
81b5372fe5SKees Cook 	/* Is there an optional argument? */
82b5372fe5SKees Cook 	i_arg = NULL;
83b5372fe5SKees Cook 	i_sep = next_terminator(i_name, i_end);
84b5372fe5SKees Cook 	if (i_sep && (*i_sep != '\0'))
85b5372fe5SKees Cook 		i_arg = next_non_spacetab(i_sep, i_end);
86b5372fe5SKees Cook 
87b5372fe5SKees Cook 	/*
881da177e4SLinus Torvalds 	 * If the script filename will be inaccessible after exec, typically
891da177e4SLinus Torvalds 	 * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
901da177e4SLinus Torvalds 	 * up now (on the assumption that the interpreter will want to load
911da177e4SLinus Torvalds 	 * this file).
921da177e4SLinus Torvalds 	 */
931da177e4SLinus Torvalds 	if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
941da177e4SLinus Torvalds 		return -ENOENT;
951da177e4SLinus Torvalds 
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 	 */
1061da177e4SLinus Torvalds 	retval = remove_arg_zero(bprm);
1071da177e4SLinus Torvalds 	if (retval)
1081da177e4SLinus Torvalds 		return retval;
1091da177e4SLinus Torvalds 	retval = copy_string_kernel(bprm->interp, bprm);
1101da177e4SLinus Torvalds 	if (retval < 0)
1111da177e4SLinus Torvalds 		return retval;
1121da177e4SLinus Torvalds 	bprm->argc++;
1131da177e4SLinus Torvalds 	*((char *)i_end) = '\0';
1141da177e4SLinus Torvalds 	if (i_arg) {
1151da177e4SLinus Torvalds 		*((char *)i_sep) = '\0';
1161da177e4SLinus Torvalds 		retval = copy_string_kernel(i_arg, bprm);
117b6a2fea3SOllie Wild 		if (retval < 0)
118b6a2fea3SOllie Wild 			return retval;
119b6a2fea3SOllie Wild 		bprm->argc++;
120986db2d1SChristoph Hellwig 	}
121c2315c18SOleg Nesterov 	retval = copy_string_kernel(i_name, bprm);
122c2315c18SOleg Nesterov 	if (retval)
1231da177e4SLinus Torvalds 		return retval;
1241da177e4SLinus Torvalds 	bprm->argc++;
125986db2d1SChristoph Hellwig 	retval = bprm_change_interp(i_name, bprm);
126c2315c18SOleg Nesterov 	if (retval < 0)
127c2315c18SOleg Nesterov 		return retval;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/*
130986db2d1SChristoph Hellwig 	 * OK, now restart the process with the interpreter's dentry.
131c2315c18SOleg Nesterov 	 */
132c2315c18SOleg Nesterov 	file = open_exec(i_name);
1331da177e4SLinus Torvalds 	if (IS_ERR(file))
134c2315c18SOleg Nesterov 		return PTR_ERR(file);
135b66c5984SKees Cook 
136b66c5984SKees Cook 	bprm->interpreter = file;
1371da177e4SLinus Torvalds 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static struct linux_binfmt script_format = {
141c2315c18SOleg Nesterov 	.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 {
1471da177e4SLinus Torvalds 	register_binfmt(&script_format);
1481da177e4SLinus Torvalds 	return 0;
1493c456bfcSAl Viro }
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");
1598fc3dc5aSAl Viro