Skip to content

Commit 94a4bc1

Browse files
committed
Allow more scripts without #!
This change modifies the zsh binary safety check surrounding execve() so it can run shell scripts having concatenated binary content. We're using the same safety check as FreeBSD /bin/sh [1]. POSIX was recently revised to require this behavior: "The input file may be of any type, but the initial portion of the file intended to be parsed according to the shell grammar (XREF to XSH 2.10.2 Shell Grammar Rules) shall consist of characters and shall not contain the NUL character. The shell shall not enforce any line length limits." "Earlier versions of this standard required that input files to the shell be text files except that line lengths were unlimited. However, that was overly restrictive in relation to the fact that shells can parse a script without a trailing newline, and in relation to a common practice of concatenating a shell script ending with an 'exit' or 'exec $command' with a binary data payload to form a single-file self-extracting archive." [2] [3] One example use case of such scripts, is the Cosmopolitan C Library [4] which configuse the GNU Linker to output a polyglot shell+binary format that runs on Linux / Mac / Windows / FreeBSD / OpenBSD. [1] freebsd/freebsd-src@9a1cd36 [2] http://austingroupbugs.net/view.php?id=1250 [3] http://austingroupbugs.net/view.php?id=1226#c4394 [4] https://justine.lol/cosmopolitan/index.html
1 parent 07765d5 commit 94a4bc1

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Src/exec.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,29 @@ zexecve(char *pth, char **argv, char **newenvp)
547547
}
548548
}
549549
} else if (eno == ENOEXEC) {
550-
for (t0 = 0; t0 != ct; t0++)
551-
if (!execvebuf[t0])
552-
break;
553-
if (t0 == ct) {
550+
/* Perform binary safety check on classic shell *
551+
* scripts (shebang wasn't introduced until UNIX *
552+
* Seventh Edition). POSIX says we shall allow *
553+
* execution of scripts with concatenated binary *
554+
* and suggests checking a line exists before the *
555+
* first NUL character with a lowercase letter or *
556+
* expansion. This is consistent with FreeBSD sh. */
557+
int isbinary, hasletter;
558+
if (!(ptr2 = memchr(execvebuf, '\0', ct))) {
559+
isbinary = 0;
560+
} else {
561+
isbinary = 1;
562+
hasletter = 0;
563+
for (ptr = execvebuf; ptr < ptr2; ptr++) {
564+
if (islower(*ptr) || *ptr == '$' || *ptr == '`')
565+
hasletter = 1;
566+
if (hasletter && *ptr == '\n') {
567+
isbinary = 0;
568+
break;
569+
}
570+
}
571+
}
572+
if (!isbinary) {
554573
argv[-1] = "sh";
555574
winch_unblock();
556575
execve("/bin/sh", argv - 1, newenvp);

0 commit comments

Comments
 (0)