Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: fix a use after free in readhistfile



Only happens if the history file is corrupted (partial meta sequence).

---

This was originally sent to zsh-security but me and Oliver judged it is
not a security issue. An attacker would need to control your history
file, and that's already not great. And even then, we don't write
anything through the pointer.

 Src/hist.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/Src/hist.c b/Src/hist.c
index c353601bf0..8e5c8c8e9e 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -2683,7 +2683,7 @@ readhistfile(char *fn, int err, int readflags)
     short *words;
     struct stat sb;
     int nwordpos, nwords, bufsiz;
-    int searching, newflags, l, ret, uselex, readbytes;
+    int searching, newflags, l, ret, uselex = 0, remeta = 0, readbytes;
 
     if (!fn && !(fn = getsparam("HISTFILE")))
 	return;
@@ -2735,13 +2735,20 @@ readhistfile(char *fn, int err, int readflags)
 	    newflags |= HIST_MAKEUNIQUE;
 	while (fpos += readbytes, readbytes = 0, (l = readhistline(0, &buf, &bufsiz, in, &readbytes))) {
 	    char *pt;
-	    int remeta = 0;
 
 	    if (l < 0) {
 		zerr("corrupt history file %s", fn);
 		break;
 	    }
 
+	    /*
+	     * Do this after deciding that we aren't breaking out of the loop
+	     * since after the loop, we use start (== pt) one last time before
+	     * the popheap() call, so we better not have freeheap()d the pointer.
+	     */
+	    if (uselex || remeta)
+		freeheap();
+	    remeta = 0;
 	    /*
 	     * Handle the special case that we're reading from an
 	     * old shell with fewer meta characters, so we need to
@@ -2762,8 +2769,9 @@ readhistfile(char *fn, int err, int readflags)
 		}
 	    }
 	    if (remeta) {
-		unmetafy(buf, &remeta);
-		pt = metafy(buf, remeta, META_USEHEAP);
+		int len;
+		unmetafy(buf, &len);
+		pt = metafy(buf, len, META_USEHEAP);
 	    } else {
 		pt = buf;
 	    }
@@ -2839,12 +2847,6 @@ readhistfile(char *fn, int err, int readflags)
 		freehistnode(&he->node);
 		curhist--;
 	    }
-	    /*
-	     * Do this last out of paranoia in case use of
-	     * heap is disguised...
-	     */
-	    if (uselex || remeta)
-		freeheap();
 	    if (errflag & ERRFLAG_INT)
 		break;
 	}
-- 
2.38.1





Messages sorted by: Reverse Date, Date, Thread, Author