Blame SOURCES/sqlite-3.26.0-CVE-2019-13734.patch

4bd5ce
From 5f4ce30babee8085fc36680c6103d9a06be49ef7 Mon Sep 17 00:00:00 2001
4bd5ce
From: Ondrej Dubaj <odubaj@redhat.com>
4bd5ce
Date: Thu, 2 Jan 2020 11:58:39 +0100
4bd5ce
Subject: [PATCH] More improvements to shadow table corruption detection in
4bd5ce
 FTS3.
4bd5ce
4bd5ce
---
4bd5ce
 ext/fts3/fts3.c       |  4 ++++
4bd5ce
 ext/fts3/fts3Int.h    | 10 ++++++++++
4bd5ce
 ext/fts3/fts3_write.c | 14 +++++++++++---
4bd5ce
 3 files changed, 25 insertions(+), 3 deletions(-)
4bd5ce
4bd5ce
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
4bd5ce
index 6d6bd46..84fc8a5 100644
4bd5ce
--- a/ext/fts3/fts3.c
4bd5ce
+++ b/ext/fts3/fts3.c
4bd5ce
@@ -1460,6 +1460,10 @@ static int fts3InitVtab(
4bd5ce
   fts3DatabasePageSize(&rc, p);
4bd5ce
   p->nNodeSize = p->nPgsz-35;
4bd5ce
 
4bd5ce
+#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
4bd5ce
+  p->nMergeCount = FTS3_MERGE_COUNT;
4bd5ce
+#endif
4bd5ce
+
4bd5ce
   /* Declare the table schema to SQLite. */
4bd5ce
   fts3DeclareVtab(&rc, p);
4bd5ce
 
4bd5ce
diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h
4bd5ce
index 6f5a7a0..0d1b491 100644
4bd5ce
--- a/ext/fts3/fts3Int.h
4bd5ce
+++ b/ext/fts3/fts3Int.h
4bd5ce
@@ -287,9 +287,19 @@ struct Fts3Table {
4bd5ce
   /* True to disable the incremental doclist optimization. This is controled
4bd5ce
   ** by special insert command 'test-no-incr-doclist'.  */
4bd5ce
   int bNoIncrDoclist;
4bd5ce
+
4bd5ce
+  /* Number of segments in a level */
4bd5ce
+  int nMergeCount;
4bd5ce
 #endif
4bd5ce
 };
4bd5ce
 
4bd5ce
+/* Macro to find the number of segments to merge */
4bd5ce
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
4bd5ce
+# define MergeCount(P) ((P)->nMergeCount)
4bd5ce
+#else
4bd5ce
+# define MergeCount(P) FTS3_MERGE_COUNT
4bd5ce
+#endif
4bd5ce
+
4bd5ce
 /*
4bd5ce
 ** When the core wants to read from the virtual table, it creates a
4bd5ce
 ** virtual table cursor (an instance of the following structure) using
4bd5ce
diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c
4bd5ce
index 8624329..d57d265 100644
4bd5ce
--- a/ext/fts3/fts3_write.c
4bd5ce
+++ b/ext/fts3/fts3_write.c
4bd5ce
@@ -1152,7 +1152,7 @@ static int fts3AllocateSegdirIdx(
4bd5ce
     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
4bd5ce
     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
4bd5ce
     */
4bd5ce
-    if( iNext>=FTS3_MERGE_COUNT ){
4bd5ce
+    if( iNext>=MergeCount(p) ){
4bd5ce
       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
4bd5ce
       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
4bd5ce
       *piIdx = 0;
4bd5ce
@@ -4259,6 +4259,10 @@ static int fts3IncrmergeLoad(
4bd5ce
       int i;
4bd5ce
       int nHeight = (int)aRoot[0];
4bd5ce
       NodeWriter *pNode;
4bd5ce
+      if( nHeight<1 || nHeight>FTS_MAX_APPENDABLE_HEIGHT ){
4bd5ce
+        sqlite3_reset(pSelect);
4bd5ce
+        return FTS_CORRUPT_VTAB;
4bd5ce
+      }
4bd5ce
 
4bd5ce
       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
4bd5ce
       pWriter->iStart = iStart;
4bd5ce
@@ -5007,7 +5011,7 @@ static int fts3DoIncrmerge(
4bd5ce
   const char *zParam              /* Nul-terminated string containing "A,B" */
4bd5ce
 ){
4bd5ce
   int rc;
4bd5ce
-  int nMin = (FTS3_MERGE_COUNT / 2);
4bd5ce
+  int nMin = (MergeCount(p) / 2);
4bd5ce
   int nMerge = 0;
4bd5ce
   const char *z = zParam;
4bd5ce
 
4bd5ce
@@ -5052,7 +5056,7 @@ static int fts3DoAutoincrmerge(
4bd5ce
   int rc = SQLITE_OK;
4bd5ce
   sqlite3_stmt *pStmt = 0;
4bd5ce
   p->nAutoincrmerge = fts3Getint(&zParam);
4bd5ce
-  if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
4bd5ce
+  if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){
4bd5ce
     p->nAutoincrmerge = 8;
4bd5ce
   }
4bd5ce
   if( !p->bHasStat ){
4bd5ce
@@ -5340,6 +5344,10 @@ static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
4bd5ce
     }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
4bd5ce
       p->bNoIncrDoclist = atoi(&zVal[21]);
4bd5ce
       rc = SQLITE_OK;
4bd5ce
+    }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
4bd5ce
+      v = atoi(&zVal[11]);
4bd5ce
+      if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
4bd5ce
+      rc = SQLITE_OK;
4bd5ce
     }
4bd5ce
 #endif
4bd5ce
   }
4bd5ce
-- 
4bd5ce
2.19.1
4bd5ce