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

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