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