021e4f
From ddb6c5b4c0ab9c6a7404112d367f0c7cc400ceec Mon Sep 17 00:00:00 2001
021e4f
From: Anthony Sottile <asottile@umich.edu>
021e4f
Date: Mon, 3 Sep 2018 14:39:25 +0000
021e4f
Subject: [PATCH] CVE-2018-0502, CVE-2018-13259: Fix two security issues in
021e4f
 shebang line parsing.
021e4f
021e4f
See NEWS for more information.
021e4f
021e4f
Patch by Anthony Sottile and Buck Evan.
021e4f
021e4f
Upstream-commit: 1c4c7b6a4d17294df028322b70c53803a402233d
021e4f
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
021e4f
---
021e4f
 Etc/FAQ.yo             |  2 +-
021e4f
 Src/exec.c             | 36 ++++++++++++++++++++----------------
021e4f
 Test/A05execution.ztst | 22 ++++++++++++++++++++++
021e4f
 3 files changed, 43 insertions(+), 17 deletions(-)
021e4f
021e4f
diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
021e4f
index 72ff7fa..8552fe7 100644
021e4f
--- a/Etc/FAQ.yo
021e4f
+++ b/Etc/FAQ.yo
021e4f
@@ -306,7 +306,7 @@ sect(On what machines will it run?)
021e4f
 
021e4f
 sect(What's the latest version?)
021e4f
 
021e4f
-  Zsh 5.5.1 is the latest production version.  For details of all the
021e4f
+  Zsh 5.6 is the latest production version.  For details of all the
021e4f
   changes, see the NEWS file in the source distribution.
021e4f
 
021e4f
   A beta of the next version is sometimes available.  Development of zsh is
021e4f
diff --git a/Src/exec.c b/Src/exec.c
021e4f
index 216057a..0908a1a 100644
021e4f
--- a/Src/exec.c
021e4f
+++ b/Src/exec.c
021e4f
@@ -453,7 +453,7 @@ execcursh(Estate state, int do_exec)
021e4f
 
021e4f
 /* execve after handling $_ and #! */
021e4f
 
021e4f
-#define POUNDBANGLIMIT 64
021e4f
+#define POUNDBANGLIMIT 128
021e4f
 
021e4f
 /**/
021e4f
 static int
021e4f
@@ -494,18 +494,20 @@ zexecve(char *pth, char **argv, char **newenvp)
021e4f
 	if ((fd = open(pth, O_RDONLY|O_NOCTTY)) >= 0) {
021e4f
 	    argv0 = *argv;
021e4f
 	    *argv = pth;
021e4f
-	    execvebuf[0] = '\0';
021e4f
+	    memset(execvebuf, '\0', POUNDBANGLIMIT + 1);
021e4f
 	    ct = read(fd, execvebuf, POUNDBANGLIMIT);
021e4f
 	    close(fd);
021e4f
 	    if (ct >= 0) {
021e4f
-		if (execvebuf[0] == '#') {
021e4f
-		    if (execvebuf[1] == '!') {
021e4f
-			for (t0 = 0; t0 != ct; t0++)
021e4f
-			    if (execvebuf[t0] == '\n')
021e4f
-				break;
021e4f
+		if (ct >= 2 && execvebuf[0] == '#' && execvebuf[1] == '!') {
021e4f
+		    for (t0 = 0; t0 != ct; t0++)
021e4f
+			if (execvebuf[t0] == '\n')
021e4f
+			    break;
021e4f
+		    if (t0 == ct)
021e4f
+			zerr("%s: bad interpreter: %s: %e", pth,
021e4f
+			     execvebuf + 2, eno);
021e4f
+		    else {
021e4f
 			while (inblank(execvebuf[t0]))
021e4f
 			    execvebuf[t0--] = '\0';
021e4f
-			execvebuf[POUNDBANGLIMIT] = '\0';
021e4f
 			for (ptr = execvebuf + 2; *ptr && *ptr == ' '; ptr++);
021e4f
 			for (ptr2 = ptr; *ptr && *ptr != ' '; ptr++);
021e4f
 			if (eno == ENOENT) {
021e4f
@@ -514,10 +516,16 @@ zexecve(char *pth, char **argv, char **newenvp)
021e4f
 				*ptr = '\0';
021e4f
 			    if (*ptr2 != '/' &&
021e4f
 				(pprog = pathprog(ptr2, NULL))) {
021e4f
-				argv[-2] = ptr2;
021e4f
-				argv[-1] = ptr + 1;
021e4f
-				winch_unblock();
021e4f
-				execve(pprog, argv - 2, newenvp);
021e4f
+				if (ptr == execvebuf + t0 + 1) {
021e4f
+				    argv[-1] = ptr2;
021e4f
+				    winch_unblock();
021e4f
+				    execve(pprog, argv - 1, newenvp);
021e4f
+				} else {
021e4f
+				    argv[-2] = ptr2;
021e4f
+				    argv[-1] = ptr + 1;
021e4f
+				    winch_unblock();
021e4f
+				    execve(pprog, argv - 2, newenvp);
021e4f
+				}
021e4f
 			    }
021e4f
 			    zerr("%s: bad interpreter: %s: %e", pth, ptr2,
021e4f
 				 eno);
021e4f
@@ -532,10 +540,6 @@ zexecve(char *pth, char **argv, char **newenvp)
021e4f
 			    winch_unblock();
021e4f
 			    execve(ptr2, argv - 1, newenvp);
021e4f
 			}
021e4f
-		    } else if (eno == ENOEXEC) {
021e4f
-			argv[-1] = "sh";
021e4f
-			winch_unblock();
021e4f
-			execve("/bin/sh", argv - 1, newenvp);
021e4f
 		    }
021e4f
 		} else if (eno == ENOEXEC) {
021e4f
 		    for (t0 = 0; t0 != ct; t0++)
021e4f
diff --git a/Test/A05execution.ztst b/Test/A05execution.ztst
021e4f
index 0804691..fb39d05 100644
021e4f
--- a/Test/A05execution.ztst
021e4f
+++ b/Test/A05execution.ztst
021e4f
@@ -12,7 +12,14 @@
021e4f
 
021e4f
   print '#!/bin/sh\necho This is dir2' >dir2/tstcmd
021e4f
 
021e4f
+  print -n '#!sh\necho This is slashless' >tstcmd-slashless
021e4f
+  print -n '#!echo foo\necho This is arg' >tstcmd-arg
021e4f
+  print '#!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnyyy' >tstcmd-interp-too-long
021e4f
+  print '#!/bin/sh\necho should not execute; exit 1' >xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
021e4f
+
021e4f
   chmod 755 tstcmd dir1/tstcmd dir2/tstcmd
021e4f
+  chmod 755 tstcmd-slashless tstcmd-arg tstcmd-interp-too-long
021e4f
+  chmod 755 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
021e4f
 
021e4f
 %test
021e4f
   ./tstcmd
021e4f
@@ -33,6 +40,21 @@
021e4f
 0:path (2)
021e4f
 >This is top
021e4f
 
021e4f
+  PATH=/bin:${ZTST_testdir}/command.tmp/ tstcmd-slashless
021e4f
+0:path (3)
021e4f
+>This is slashless
021e4f
+
021e4f
+  PATH=/bin:${ZTST_testdir}/command.tmp tstcmd-arg
021e4f
+0:path (4)
021e4f
+*>foo */command.tmp/tstcmd-arg
021e4f
+
021e4f
+  path=(/bin ${ZTST_testdir}/command.tmp/)
021e4f
+  tstcmd-interp-too-long 2>&1; echo "status $?"
021e4f
+  path=($storepath)
021e4f
+0:path (5)
021e4f
+*>*tstcmd-interp-too-long: bad interpreter: x*xn: no such file or directory
021e4f
+>status 127
021e4f
+
021e4f
   functst() { print $# arguments:; print -l $*; }
021e4f
   functst "Eines Morgens" "als Gregor Samsa"
021e4f
   functst ""
021e4f
-- 
021e4f
2.17.1
021e4f