560f4e
From 20cf100cddc27a1e7202413261c93b78142899e8 Mon Sep 17 00:00:00 2001
560f4e
From: Oliver Kiddle <okiddle@yahoo.co.uk>
560f4e
Date: Sat, 24 Mar 2018 15:04:39 +0100
560f4e
Subject: [PATCH] 42519, CVE-2018-1083: check bounds on PATH_MAX-sized buffer
560f4e
 used for file completion candidates
560f4e
560f4e
Upstream-commit: 259ac472eac291c8c103c7a0d8a4eaf3c2942ed7
560f4e
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
560f4e
560f4e
Also picked a fix for buffer size off-by-one from upstream commit
560f4e
a62e1640bcafbb82d86ea8d8ce057a83c4683d60 to fix the following defect
560f4e
newly detected by Coverity Analysis:
560f4e
560f4e
Error: OVERRUN (CWE-119):
560f4e
zsh-5.0.2/Src/Zle/compctl.c:2160: cond_at_most: Checking "pathpreflen > 4096" implies that "pathpreflen" may be up to 4096 on the false branch.
560f4e
zsh-5.0.2/Src/Zle/compctl.c:2172: overrun-buffer-arg: Overrunning array "p" of 4096 bytes by passing it to a function which accesses it at byte offset 4096 using argument "pathpreflen + 1" (which evaluates to 4097). [Note: The source code implementation of the function has been overridden by a builtin model.]
560f4e
---
560f4e
 Src/Zle/compctl.c | 8 +++++++-
560f4e
 1 file changed, 7 insertions(+), 1 deletion(-)
560f4e
560f4e
diff --git a/Src/Zle/compctl.c b/Src/Zle/compctl.c
560f4e
index 5d67137..5e636ef 100644
560f4e
--- a/Src/Zle/compctl.c
560f4e
+++ b/Src/Zle/compctl.c
560f4e
@@ -2136,7 +2136,7 @@ gen_matches_files(int dirs, int execs, int all)
560f4e
 {
560f4e
     DIR *d;
560f4e
     struct stat buf;
560f4e
-    char *n, p[PATH_MAX], *q = NULL, *e, *pathpref;
560f4e
+    char *n, p[PATH_MAX+1], *q = NULL, *e, *pathpref;
560f4e
     LinkList l = NULL;
560f4e
     int ns = 0, ng = opts[NULLGLOB], test, aw = addwhat, pathpreflen;
560f4e
 
560f4e
@@ -2157,6 +2157,8 @@ gen_matches_files(int dirs, int execs, int all)
560f4e
     if (prpre && *prpre) {
560f4e
 	pathpref = dupstring(prpre);
560f4e
 	unmetafy(pathpref, &pathpreflen);
560f4e
+	if (pathpreflen > PATH_MAX)
560f4e
+	    return;
560f4e
 	/* system needs NULL termination, not provided by unmetafy */
560f4e
 	pathpref[pathpreflen] = '\0';
560f4e
     } else {
560f4e
@@ -2199,6 +2201,8 @@ gen_matches_files(int dirs, int execs, int all)
560f4e
 		     * the path buffer by appending the filename.       */
560f4e
 		    ums = dupstring(n);
560f4e
 		    unmetafy(ums, &umlen);
560f4e
+		    if (umlen + pathpreflen + 1 > PATH_MAX)
560f4e
+			continue;
560f4e
 		    memcpy(q, ums, umlen);
560f4e
 		    q[umlen] = '\0';
560f4e
 		    /* And do the stat. */
560f4e
@@ -2213,6 +2217,8 @@ gen_matches_files(int dirs, int execs, int all)
560f4e
 			/* We have to test for a path suffix. */
560f4e
 			int o = strlen(p), tt;
560f4e
 
560f4e
+			if (o + strlen(psuf) > PATH_MAX)
560f4e
+			    continue;
560f4e
 			/* Append it to the path buffer. */
560f4e
 			strcpy(p + o, psuf);
560f4e
 
560f4e
-- 
560f4e
2.14.3
560f4e