Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+



Hello,

$ echo echo ++ > ++
$ chmod +x ++
$ strace -qqfe /exec zsh -c 'PATH=; ++'
execve("/usr/bin/zsh", ["zsh", "-c", "PATH=; ++"], 0x7fffadb8e870 /* 55 vars */) = 0
execve("++", ["++"], 0x7ffc57750de8 /* 55 vars */) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["sh", "++"], 0x7ffc57750de8 /* 55 vars */) = 0
sh: 0: Illegal option -+

The path of the script was passed as an option to sh rather than
an operand.

The patch below changes it to:

$ strace -qqfe /exec Src/zsh -c 'PATH=; ++'
execve("Src/zsh", ["Src/zsh", "-c", "PATH=; ++"], 0x7ffd2a923100 /* 55 vars */) = 0
execve("++", ["++"], 0x7ffd8d292448 /* 55 vars */) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["sh", "-", "++"], 0x7ffd8d292448 /* 55 vars */) = 0
++

yash is the only other shell I've found so far that guards
against it. It does add the "-" unconditionally. Here I add it
only if the path starts with - or + which would be extremely
rare to avoid getting closer to the E2BIG limit in the normal
case.

Also note (related though this patch doesn't do anything about
it other than adding the "-"):

$ strace -qqfe /exec zsh -c 'PATH=.; ++'
execve("/usr/bin/zsh", ["zsh", "-c", "PATH=.; ++"], 0x7ffcf4504b50 /* 55 vars */) = 0
execve("++", ["++"], 0x7ffc2a099678 /* 55 vars */) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["sh", "++"], 0x7ffc2a099678 /* 55 vars */) = 0
sh: 0: Illegal option -+

Is that intentional? I'd have expected execve("./++", ["++"]...)
as $path contains the "." relative path there.

$ strace -qqfe /exec zsh -c 'PATH=./.; ++'
execve("/usr/bin/zsh", ["zsh", "-c", "PATH=./.; ++"],
0x7ffddeba1840 /* 55 vars */) = 0
execve("././++", ["++"], 0x7ffc4dfe9ae8 /* 55 vars */) = -1
ENOEXEC (Exec format error)
execve("/bin/sh", ["sh", "././++"], 0x7ffc4dfe9ae8 /* 55 vars
*/) = 0
++

There are a number of scripts in the source distribution whose
shebang is missing the "-", and which this patch doesn't
address. See also
https://unix.stackexchange.com/questions/351729/why-the-in-the-bin-sh-shebang


diff --git a/Src/exec.c b/Src/exec.c
index 7d8135266..294587802 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -612,9 +612,22 @@ zexecve(char *pth, char **argv, char **newenvp)
                         }
                     }
 		    if (!isbinary) {
-			argv[-1] = "sh";
+		        char** args = argv;
+			if (argv[0][0] == '-' || argv[0][0] == '+') {
+			    /*
+			     * guard against +foo or -bar script paths being
+			     * taken as options.  POSIX says the script path
+			     * must be passed as an *operand*. "--" would also
+			     * make sure the next argument is treated as an
+			     * operand with POSIX compliant sh implementations
+			     * but "-" is more portable (to the Bourne shell in
+			     * particular) and shorter.
+			     */
+			    *--args = "-"; 
+			}
+			*--args = "sh";
 			winch_unblock();
-			execve("/bin/sh", argv - 1, newenvp);
+			execve("/bin/sh", args, newenvp);
 		    }
 		}
 	    } else




Messages sorted by: Reverse Date, Date, Thread, Author