da6346
From 20cf100cddc27a1e7202413261c93b78142899e8 Mon Sep 17 00:00:00 2001
da6346
From: Oliver Kiddle <okiddle@yahoo.co.uk>
da6346
Date: Sat, 24 Mar 2018 15:04:39 +0100
da6346
Subject: [PATCH] 42519, CVE-2018-1083: check bounds on PATH_MAX-sized buffer
da6346
 used for file completion candidates
da6346
da6346
Upstream-commit: 259ac472eac291c8c103c7a0d8a4eaf3c2942ed7
da6346
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
da6346
da6346
Also picked a fix for buffer size off-by-one from upstream commit
da6346
a62e1640bcafbb82d86ea8d8ce057a83c4683d60 to fix the following defect
da6346
newly detected by Coverity Analysis:
da6346
da6346
Error: OVERRUN (CWE-119):
da6346
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.
da6346
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.]
da6346
---
da6346
 Src/Zle/compctl.c | 8 +++++++-
da6346
 1 file changed, 7 insertions(+), 1 deletion(-)
da6346
da6346
diff --git a/Src/Zle/compctl.c b/Src/Zle/compctl.c
da6346
index 5d67137..5e636ef 100644
da6346
--- a/Src/Zle/compctl.c
da6346
+++ b/Src/Zle/compctl.c
da6346
@@ -2136,7 +2136,7 @@ gen_matches_files(int dirs, int execs, int all)
da6346
 {
da6346
     DIR *d;
da6346
     struct stat buf;
da6346
-    char *n, p[PATH_MAX], *q = NULL, *e, *pathpref;
da6346
+    char *n, p[PATH_MAX+1], *q = NULL, *e, *pathpref;
da6346
     LinkList l = NULL;
da6346
     int ns = 0, ng = opts[NULLGLOB], test, aw = addwhat, pathpreflen;
da6346
 
da6346
@@ -2157,6 +2157,8 @@ gen_matches_files(int dirs, int execs, int all)
da6346
     if (prpre && *prpre) {
da6346
 	pathpref = dupstring(prpre);
da6346
 	unmetafy(pathpref, &pathpreflen);
da6346
+	if (pathpreflen > PATH_MAX)
da6346
+	    return;
da6346
 	/* system needs NULL termination, not provided by unmetafy */
da6346
 	pathpref[pathpreflen] = '\0';
da6346
     } else {
da6346
@@ -2199,6 +2201,8 @@ gen_matches_files(int dirs, int execs, int all)
da6346
 		     * the path buffer by appending the filename.       */
da6346
 		    ums = dupstring(n);
da6346
 		    unmetafy(ums, &umlen);
da6346
+		    if (umlen + pathpreflen + 1 > PATH_MAX)
da6346
+			continue;
da6346
 		    memcpy(q, ums, umlen);
da6346
 		    q[umlen] = '\0';
da6346
 		    /* And do the stat. */
da6346
@@ -2213,6 +2217,8 @@ gen_matches_files(int dirs, int execs, int all)
da6346
 			/* We have to test for a path suffix. */
da6346
 			int o = strlen(p), tt;
da6346
 
da6346
+			if (o + strlen(psuf) > PATH_MAX)
da6346
+			    continue;
da6346
 			/* Append it to the path buffer. */
da6346
 			strcpy(p + o, psuf);
da6346
 
da6346
-- 
da6346
2.14.3
da6346