Blame SOURCES/sqlite-3.26-CVE-2019-13752.patch

032533
From 92b243715eea17997ed9707540757d0667ad9eb2 Mon Sep 17 00:00:00 2001
032533
From: Ondrej Dubaj <odubaj@redhat.com>
032533
Date: Thu, 2 Jan 2020 09:54:41 +0100
032533
Subject: [PATCH] Improved detection of corrupt shadow tables in FTS3. Enable
032533
 the debugging special-inserts for FTS3 for both SQLITE_DEBUG and SQLITE_TEST.
032533
032533
Resolves: CVE-2019-13752
032533
---
032533
 ext/fts3/fts3.c       |  2 +-
032533
 ext/fts3/fts3Int.h    |  2 +-
032533
 ext/fts3/fts3_write.c | 42 +++++++++++++++++++++++++++---------------
032533
 3 files changed, 29 insertions(+), 17 deletions(-)
032533
032533
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
032533
index f6fb931..6d6bd46 100644
032533
--- a/ext/fts3/fts3.c
032533
+++ b/ext/fts3/fts3.c
032533
@@ -4304,7 +4304,7 @@ static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
032533
   int bIncrOk = (bOptOk 
032533
    && pCsr->bDesc==pTab->bDescIdx 
032533
    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
032533
-#ifdef SQLITE_TEST
032533
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
032533
    && pTab->bNoIncrDoclist==0
032533
 #endif
032533
   );
032533
diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h
032533
index 077bad7..6f5a7a0 100644
032533
--- a/ext/fts3/fts3Int.h
032533
+++ b/ext/fts3/fts3Int.h
032533
@@ -283,7 +283,7 @@ struct Fts3Table {
032533
   int mxSavepoint;       /* Largest valid xSavepoint integer */
032533
 #endif
032533
 
032533
-#ifdef SQLITE_TEST
032533
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
032533
   /* True to disable the incremental doclist optimization. This is controled
032533
   ** by special insert command 'test-no-incr-doclist'.  */
032533
   int bNoIncrDoclist;
032533
diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c
032533
index 8fc6589..ee668aa 100644
032533
--- a/ext/fts3/fts3_write.c
032533
+++ b/ext/fts3/fts3_write.c
032533
@@ -23,7 +23,7 @@
032533
 #include <string.h>
032533
 #include <assert.h>
032533
 #include <stdlib.h>
032533
-
032533
+#include <stdio.h>
032533
 
032533
 #define FTS_MAX_APPENDABLE_HEIGHT 16
032533
 
032533
@@ -2021,6 +2021,11 @@ static int fts3NodeAddTerm(
032533
     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
032533
     nSuffix = nTerm-nPrefix;
032533
 
032533
+    /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of 
032533
+    ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
032533
+    ** compared with BINARY collation. This indicates corruption.  */
032533
+    if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
032533
+
032533
     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
032533
     if( nReq<=p->nNodeSize || !pTree->zTerm ){
032533
 
032533
@@ -2309,9 +2314,11 @@ static int fts3SegWriterAdd(
032533
   /* Append the prefix-compressed term and doclist to the buffer. */
032533
   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
032533
   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
032533
+  assert( nSuffix>0 );
032533
   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
032533
   nData += nSuffix;
032533
   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
032533
+  assert( nDoclist>0 );
032533
   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
032533
   pWriter->nData = nData + nDoclist;
032533
 
032533
@@ -2331,6 +2338,7 @@ static int fts3SegWriterAdd(
032533
       pWriter->zTerm = zNew;
032533
     }
032533
     assert( pWriter->zTerm==pWriter->zMalloc );
032533
+    assert( nTerm>0 );
032533
     memcpy(pWriter->zTerm, zTerm, nTerm);
032533
   }else{
032533
     pWriter->zTerm = (char *)zTerm;
032533
@@ -2639,6 +2647,7 @@ static int fts3MsrBufferData(
032533
     pMsr->aBuffer = pNew;
032533
   }
032533
 
032533
+  assert( nList>0 );
032533
   memcpy(pMsr->aBuffer, pList, nList);
032533
   return SQLITE_OK;
032533
 }
032533
@@ -3821,6 +3830,7 @@ static int fts3IncrmergePush(
032533
     ** be added to.  */
032533
     nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
032533
     nSuffix = nTerm - nPrefix;
032533
+    if( NEVER(nSuffix<=0) ) return FTS_CORRUPT_VTAB;
032533
     nSpace  = sqlite3Fts3VarintLen(nPrefix);
032533
     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
032533
 
032533
@@ -5300,7 +5310,7 @@ static int fts3DoIntegrityCheck(
032533
 ** meaningful value to insert is the text 'optimize'.
032533
 */
032533
 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
032533
-  int rc;                         /* Return Code */
032533
+  int rc = SQLITE_ERROR;           /* Return Code */
032533
   const char *zVal = (const char *)sqlite3_value_text(pVal);
032533
   int nVal = sqlite3_value_bytes(pVal);
032533
 
032533
@@ -5316,21 +5326,23 @@ static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
032533
     rc = fts3DoIncrmerge(p, &zVal[6]);
032533
   }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
032533
     rc = fts3DoAutoincrmerge(p, &zVal[10]);
032533
-#ifdef SQLITE_TEST
032533
-  }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
032533
-    p->nNodeSize = atoi(&zVal[9]);
032533
-    rc = SQLITE_OK;
032533
-  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
032533
-    p->nMaxPendingData = atoi(&zVal[11]);
032533
-    rc = SQLITE_OK;
032533
-  }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
032533
-    p->bNoIncrDoclist = atoi(&zVal[21]);
032533
-    rc = SQLITE_OK;
032533
-#endif
032533
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
032533
   }else{
032533
-    rc = SQLITE_ERROR;
032533
+        int v;
032533
+    if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
032533
+      v = atoi(&zVal[9]);
032533
+      if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
032533
+      rc = SQLITE_OK;
032533
+    }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
032533
+      v = atoi(&zVal[11]);
032533
+      if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v;
032533
+      rc = SQLITE_OK;
032533
+    }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
032533
+      p->bNoIncrDoclist = atoi(&zVal[21]);
032533
+      rc = SQLITE_OK;
032533
+    }
032533
+#endif
032533
   }
032533
-
032533
   return rc;
032533
 }
032533
 
032533
-- 
032533
2.19.1
032533