[PATCH] Add support for shebang lines with multiple arguments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I recently noticed that Linux did not support shebang lines with multiple
arguments, such as the lines below:

#!/usr/bin/ls --color -F

#!/usr/bin/env python -O

I store some local binaries in $HOME/bin and want to use /usr/bin/env with a
program and some arguments, so I made the necessary changes to
fs/binfmt_script.c to support this. I also saw this thread requesting a patch
to fix a stack data disclosure: https://lkml.org/lkml/2012/8/18/75
So I combined my changes with a fix for the stack data disclosure as well.
Well, at least I think it will fix it, the patch needs more testing other than
just me, because this is my first time doing kernel hacking.

Please help me get this patch accepted into the main line. Also, please CC
me personally on follow ups, as I'm not part of the list.

I made the patch against linux-3.11.1-gentoo. This file doesn't seem to have
been touched too recently. In any case, let me know if I should diff against a
different kernel and resubmit.

Thank you,
-Guilherme

--
Guilherme Amadio <guilherme.amadio@xxxxxxxxx
 http://profiles.google.com/guilherme.amadio

--PATCH BEGINS HERE--

Add support for shebang lines with multiple arguments.

Signed-off-by: Guilherme Amadio <guilherme.amadio@xxxxxxxxx>

--- a/fs/binfmt_script.c	2013-09-02 17:46:10.000000000 -0300
+++ b/fs/binfmt_script.c	2013-09-21 00:44:05.516150638 -0300
@@ -16,86 +16,121 @@
 
 static int load_script(struct linux_binprm *bprm)
 {
-	const char *i_arg, *i_name;
-	char *cp;
+	const char *old_interp;
+	char *cp, *start, *end;
 	struct file *file;
-	char interp[BINPRM_BUF_SIZE];
+	char bprm_buf_copy[BINPRM_BUF_SIZE];
 	int retval;
 
 	if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
 		return -ENOEXEC;
-	/*
-	 * This section does the #! interpretation.
-	 * Sorta complicated, but hopefully it will work.  -TYT
+
+	/* Work on a copy of bprm->buf to avoid corrupting it
+	 * and ruining attempts of other handlers for it if we
+	 * are not the right one.
 	 */
+	memcpy(bprm_buf_copy, bprm->buf, BINPRM_BUF_SIZE);
 
-	allow_write_access(bprm->file);
-	fput(bprm->file);
-	bprm->file = NULL;
+	start = bprm_buf_copy+2;
+	end   = bprm_buf_copy+BINPRM_BUF_SIZE-1;
 
-	bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
-	if ((cp = strchr(bprm->buf, '\n')) == NULL)
-		cp = bprm->buf+BINPRM_BUF_SIZE-1;
-	*cp = '\0';
-	while (cp > bprm->buf) {
-		cp--;
-		if ((*cp == ' ') || (*cp == '\t'))
-			*cp = '\0';
-		else
+	/* Find beginning and end of the line */
+	for (cp = start; cp < end; cp++) {
+		if (*cp != ' ' && *cp != '\t') {
+			start = cp;
 			break;
+		}
 	}
-	for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
-	if (*cp == '\0') 
-		return -ENOEXEC; /* No interpreter name found */
-	i_name = cp;
-	i_arg = NULL;
-	for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
-		/* nothing */ ;
-	while ((*cp == ' ') || (*cp == '\t'))
+
+	for (cp = start; cp < end; cp++) {
+		if (*cp == '#' || *cp == '\n') {
+			end = cp;
+			break;
+		}
+	}
+
+	/* No interpreter found, let other handlers try it */
+	if (start == end)
+		return -ENOEXEC;
+
+	*end = '\0';
+
+	/* Find the end of the interpreter name */
+	for (cp = start; cp < end; cp++)
+		if (*cp == ' ' || *cp == '\t')
+			break;
+
+	/* Skip over space until first argument */
+	while (cp < end && (*cp == ' ' || *cp == '\t'))
 		*cp++ = '\0';
-	if (*cp)
-		i_arg = cp;
-	strcpy (interp, i_name);
-	/*
-	 * OK, we've parsed out the interpreter name and
-	 * (optional) argument.
-	 * Splice in (1) the interpreter's name for argv[0]
-	 *           (2) (optional) argument to interpreter
-	 *           (3) filename of shell script (replace argv[0])
-	 *
-	 * This is done in reverse order, because of how the
-	 * user environment and arguments are stored.
+
+	old_interp = bprm->interp;
+	bprm->interp = start;
+
+	/* From this point, we know that we are the correct handler
+	 * for this file. After we modify bprm, we cannot let other
+	 * handlers try anymore. We now need to build a list with
+	 * the new interpreter and the arguments, but in reverse
+	 * order, because of the way user environment and arguments
+	 * are stored.
 	 */
+
 	retval = remove_arg_zero(bprm);
 	if (retval)
 		return retval;
-	retval = copy_strings_kernel(1, &bprm->interp, bprm);
-	if (retval < 0) return retval; 
+	retval = copy_strings_kernel(1, &old_interp, bprm);
+	if (retval < 0)
+		goto out;
 	bprm->argc++;
-	if (i_arg) {
-		retval = copy_strings_kernel(1, &i_arg, bprm);
-		if (retval < 0) return retval; 
+
+	start = cp; cp = end;
+
+	while (cp > start) {
+		const char *arg;
+		/* Back up over trailing space */
+		do {
+			*cp-- = '\0';
+		} while (cp > start && (*cp == ' ' || *cp == '\t'));
+
+		/* Find the beginning of the argument */
+		while (cp >= start && *cp != ' ' && *cp != '\t') cp--;
+
+		arg = cp+1;
+		retval = copy_strings_kernel(1, &arg, bprm);
+		if (retval < 0)
+			goto out;
 		bprm->argc++;
 	}
-	retval = copy_strings_kernel(1, &i_name, bprm);
-	if (retval) return retval; 
-	bprm->argc++;
-	retval = bprm_change_interp(interp, bprm);
-	if (retval < 0)
-		return retval;
 
-	/*
-	 * OK, now restart the process with the interpreter's dentry.
-	 */
-	file = open_exec(interp);
-	if (IS_ERR(file))
-		return PTR_ERR(file);
+	retval = copy_strings_kernel(1, &bprm->interp, bprm);
+	if (retval)
+		goto out;
+	bprm->argc++;
 
+	allow_write_access(bprm->file);
+	fput(bprm->file);
+	bprm->file = NULL;
+	file = open_exec(bprm->interp);
+	if (IS_ERR(file)) {
+		retval = PTR_ERR(file);
+		goto out;
+	}
 	bprm->file = file;
 	retval = prepare_binprm(bprm);
 	if (retval < 0)
-		return retval;
-	return search_binary_handler(bprm);
+		goto out;
+
+	bprm->recursion_depth++;
+	retval = search_binary_handler(bprm);
+
+out:
+	/* Restore old_interp to avoid stack data leak */
+	bprm->interp = old_interp;
+
+	if (retval == -ENOEXEC)
+		return -EINVAL;
+
+	return retval;
 }
 
 static struct linux_binfmt script_format = {

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [Samba]     [Device Mapper]     [CEPH Development]
  Powered by Linux