094f44
From 75525dbdf9b7ed003e343c42710e8b13f73a7607 Mon Sep 17 00:00:00 2001
094f44
From: Ondrej Dubaj <odubaj@redhat.com>
094f44
Date: Thu, 23 Jan 2020 15:08:13 +0100
094f44
Subject: [PATCH] Fix buffer underflows in the zipfile extension associated
094f44
 with zero-length or NULL filename in the ZIP archive. But report on the
094f44
 mailing list by Yongheng and Rui.
094f44
094f44
---
094f44
 ext/misc/zipfile.c | 14 +++++++++-----
094f44
 test/zipfile.test  | 13 +++++++++++++
094f44
 2 files changed, 22 insertions(+), 5 deletions(-)
094f44
094f44
diff --git a/ext/misc/zipfile.c b/ext/misc/zipfile.c
094f44
index e6141ef..7fd4074 100644
094f44
--- a/ext/misc/zipfile.c
094f44
+++ b/ext/misc/zipfile.c
094f44
@@ -1433,8 +1433,8 @@ static int zipfileGetMode(
094f44
 ** identical, ignoring any trailing '/' character in either path.  */
094f44
 static int zipfileComparePath(const char *zA, const char *zB, int nB){
094f44
   int nA = (int)strlen(zA);
094f44
-  if( zA[nA-1]=='/' ) nA--;
094f44
-  if( zB[nB-1]=='/' ) nB--;
094f44
+  if( nA>0 && zA[nA-1]=='/' ) nA--;
094f44
+  if( nB>0 && zB[nB-1]=='/' ) nB--;
094f44
   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
094f44
   return 1;
094f44
 }
094f44
@@ -1628,11 +1628,15 @@ static int zipfileUpdate(
094f44
       ** '/'. This appears to be required for compatibility with info-zip
094f44
       ** (the unzip command on unix). It does not create directories
094f44
       ** otherwise.  */
094f44
-      if( zPath[nPath-1]!='/' ){
094f44
+      if( nPath<=0 || zPath[nPath-1]!='/' ){
094f44
         zFree = sqlite3_mprintf("%s/", zPath);
094f44
-        if( zFree==0 ){ rc = SQLITE_NOMEM; }
094f44
         zPath = (const char*)zFree;
094f44
-        nPath = (int)strlen(zPath);
094f44
+        if( zFree==0 ){
094f44
+          rc = SQLITE_NOMEM;
094f44
+          nPath = 0;
094f44
+        }else{
094f44
+          nPath = (int)strlen(zPath);
094f44
+        }
094f44
       }
094f44
     }
094f44
 
094f44
diff --git a/test/zipfile.test b/test/zipfile.test
094f44
index e4b8088..9f07c0a 100644
094f44
--- a/test/zipfile.test
094f44
+++ b/test/zipfile.test
094f44
@@ -821,4 +821,17 @@ do_execsql_test 14.10 {
094f44
   PRAGMA integrity_check;
094f44
 } {3 ok}
094f44
 
094f44
+# 2019-12-26 More problems in zipfile from the Yongheng and Rui fuzzer
094f44
+#
094f44
+do_execsql_test 15.10 {
094f44
+  DROP TABLE IF EXISTS t1;
094f44
+  CREATE VIRTUAL TABLE t1 USING zipfile(null);
094f44
+  REPLACE INTO t1 VALUES(null,null,0,null,null,null,null);
094f44
+} {}
094f44
+do_execsql_test 15.20 {
094f44
+  DROP TABLE IF EXISTS t2;
094f44
+  CREATE VIRTUAL TABLE t2 USING zipfile(null);
094f44
+  REPLACE INTO t2 values(null,null,null,null,null,10,null);
094f44
+} {}
094f44
+
094f44
 finish_test
094f44
-- 
094f44
2.19.1
094f44