zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+
@ 2024-02-04  9:06 Stephane Chazelas
  2024-02-10 16:32 ` Stephane Chazelas
  0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2024-02-04  9:06 UTC (permalink / raw)
  To: Zsh hackers list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+
  2024-02-04  9:06 [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+ Stephane Chazelas
@ 2024-02-10 16:32 ` Stephane Chazelas
  0 siblings, 0 replies; 2+ messages in thread
From: Stephane Chazelas @ 2024-02-10 16:32 UTC (permalink / raw)
  To: Zsh hackers list

2024-02-04 09:06:27 +0000, Stephane Chazelas:
[...]
> $ 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
> ++
[...]

Sorry, forgot to add a test case:

diff --git a/Src/exec.c b/Src/exec.c
index 7d8135266..9c8bbb458 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
diff --git a/Test/A05execution.ztst b/Test/A05execution.ztst
index bcadc6d56..07a24f9c8 100644
--- a/Test/A05execution.ztst
+++ b/Test/A05execution.ztst
@@ -2,7 +2,7 @@
 
   storepath=($path)
 
-  mkdir command.tmp command.tmp/dir1 command.tmp/dir2
+  mkdir command.tmp command.tmp/dir{1,2} command.tmp/{+,-}dir
 
   cd command.tmp
 
@@ -21,7 +21,10 @@
   print '#!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnyyy' >tstcmd-interp-too-long
   print "#!${sh}\necho should not execute; exit 1" >xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
 
-  chmod 755 tstcmd dir1/tstcmd dir2/tstcmd
+  print 'echo no shebang in -dir' > -dir/tstcmd
+  print 'echo no shebang in +dir' > +dir/tstcmd
+
+  chmod 755 tstcmd dir{1,2}/tstcmd ./{-,+}dir/tstcmd
   chmod 755 tstcmd-slashless tstcmd-arg tstcmd-interp-too-long
   chmod 755 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
 
@@ -422,3 +425,12 @@ F:anonymous function, and a descriptor leak when backgrounding a pipeline
  (exit 4); repeat 0 do done
 0:'repeat 0' resets lastval
 
+ -dir/tstcmd
+ +dir/tstcmd
+ PATH=-dir tstcmd
+ PATH=+dir tstcmd
+0:shebang-less scripts are to be run by sh even when their file paths start with - or + (workers/52515)
+>no shebang in -dir
+>no shebang in +dir
+>no shebang in -dir
+>no shebang in +dir


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-10 16:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-04  9:06 [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+ Stephane Chazelas
2024-02-10 16:32 ` Stephane Chazelas

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).