560f4e
From a787fc5c556cbbc7f3de308d25b7527f9da5a0da Mon Sep 17 00:00:00 2001
560f4e
From: "Barton E. Schaefer" <schaefer@zsh.org>
560f4e
Date: Sun, 19 Jan 2014 17:41:06 -0800
560f4e
Subject: [PATCH] 32294: prevent buffer overflow when scanning very long
560f4e
 directory paths for symbolic links
560f4e
560f4e
Upstream-commit: 3e06aeabd8a9e8384ebaa8b08996cd1f64737210
560f4e
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
560f4e
---
560f4e
 Src/utils.c | 31 ++++++++++++++++++-------------
560f4e
 1 file changed, 18 insertions(+), 13 deletions(-)
560f4e
560f4e
diff --git a/Src/utils.c b/Src/utils.c
560f4e
index 20fa59d..a197ef8 100644
560f4e
--- a/Src/utils.c
560f4e
+++ b/Src/utils.c
560f4e
@@ -726,32 +726,36 @@ xsymlinks(char *s)
560f4e
     char **pp, **opp;
560f4e
     char xbuf2[PATH_MAX*2], xbuf3[PATH_MAX*2];
560f4e
     int t0, ret = 0;
560f4e
+    zulong xbuflen = strlen(xbuf);
560f4e
 
560f4e
     opp = pp = slashsplit(s);
560f4e
-    for (; *pp; pp++) {
560f4e
-	if (!strcmp(*pp, ".")) {
560f4e
-	    zsfree(*pp);
560f4e
+    for (; xbuflen < sizeof(xbuf) && *pp; pp++) {
560f4e
+	if (!strcmp(*pp, "."))
560f4e
 	    continue;
560f4e
-	}
560f4e
 	if (!strcmp(*pp, "..")) {
560f4e
 	    char *p;
560f4e
 
560f4e
-	    zsfree(*pp);
560f4e
 	    if (!strcmp(xbuf, "/"))
560f4e
 		continue;
560f4e
 	    if (!*xbuf)
560f4e
 		continue;
560f4e
-	    p = xbuf + strlen(xbuf);
560f4e
-	    while (*--p != '/');
560f4e
+	    p = xbuf + xbuflen;
560f4e
+	    while (*--p != '/')
560f4e
+		xbuflen--;
560f4e
 	    *p = '\0';
560f4e
 	    continue;
560f4e
 	}
560f4e
 	sprintf(xbuf2, "%s/%s", xbuf, *pp);
560f4e
 	t0 = readlink(unmeta(xbuf2), xbuf3, PATH_MAX);
560f4e
 	if (t0 == -1) {
560f4e
-	    strcat(xbuf, "/");
560f4e
-	    strcat(xbuf, *pp);
560f4e
-	    zsfree(*pp);
560f4e
+	    zulong pplen = strlen(*pp) + 1;
560f4e
+	    if ((xbuflen += pplen) < sizeof(xbuf)) {
560f4e
+		strcat(xbuf, "/");
560f4e
+		strcat(xbuf, *pp);
560f4e
+	    } else {
560f4e
+		*xbuf = 0;
560f4e
+		break;
560f4e
+	    }
560f4e
 	} else {
560f4e
 	    ret = 1;
560f4e
 	    metafy(xbuf3, t0, META_NOALLOC);
560f4e
@@ -760,10 +764,9 @@ xsymlinks(char *s)
560f4e
 		xsymlinks(xbuf3 + 1);
560f4e
 	    } else
560f4e
 		xsymlinks(xbuf3);
560f4e
-	    zsfree(*pp);
560f4e
 	}
560f4e
     }
560f4e
-    free(opp);
560f4e
+    freearray(opp);
560f4e
     return ret;
560f4e
 }
560f4e
 
560f4e
@@ -780,8 +783,10 @@ xsymlink(char *s)
560f4e
 	return NULL;
560f4e
     *xbuf = '\0';
560f4e
     xsymlinks(s + 1);
560f4e
-    if (!*xbuf)
560f4e
+    if (!*xbuf) {
560f4e
+	zwarn("path expansion failed, using root directory");
560f4e
 	return ztrdup("/");
560f4e
+    }
560f4e
     return ztrdup(xbuf);
560f4e
 }
560f4e
 
560f4e
-- 
560f4e
2.14.3
560f4e