From: Stephane Chazelas <stephane@chazelas.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH][nitpick-BUG] shebang less scripts and paths starting with -/+
Date: Sun, 4 Feb 2024 09:06:27 +0000 [thread overview]
Message-ID: <20240204090627.6lpx2ocx36bw5nco@chazelas.org> (raw)
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
next reply other threads:[~2024-02-04 9:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-04 9:06 Stephane Chazelas [this message]
2024-02-10 16:32 ` Stephane Chazelas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240204090627.6lpx2ocx36bw5nco@chazelas.org \
--to=stephane@chazelas.org \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).