Blame SOURCES/sqlite-3.26.0-CVE-2020-9327.patch

11c328
From 2d788539b0018d34d3cabb328387ba6bec41ec42 Mon Sep 17 00:00:00 2001
11c328
From: Ondrej Dubaj <odubaj@redhat.com>
11c328
Date: Thu, 26 Mar 2020 09:43:43 +0100
11c328
Subject: [PATCH] NULL pointer dereference and segmentation fault because of
11c328
 generated column optimizations
11c328
11c328
Take care when checking the table of a TK_COLUMN expression node to
11c328
see if the table is a virtual table to first ensure that the
11c328
Expr.y.pTab pointer is not null due to generated column optimizations.
11c328
---
11c328
 src/expr.c      | 13 ++++++++++---
11c328
 src/sqliteInt.h |  3 +++
11c328
 src/whereexpr.c | 12 ++++++++----
11c328
 3 files changed, 21 insertions(+), 7 deletions(-)
11c328
11c328
diff --git a/src/expr.c b/src/expr.c
11c328
index b081ca2..5f98f76 100644
11c328
--- a/src/expr.c
11c328
+++ b/src/expr.c
11c328
@@ -4901,18 +4901,25 @@ static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
11c328
     case TK_LT:
11c328
     case TK_LE:
11c328
     case TK_GT:
11c328
-    case TK_GE:
11c328
+    case TK_GE: {
11c328
+      Expr *pLeft = pExpr->pLeft;
11c328
+      Expr *pRight = pExpr->pRight;
11c328
       testcase( pExpr->op==TK_EQ );
11c328
       testcase( pExpr->op==TK_NE );
11c328
       testcase( pExpr->op==TK_LT );
11c328
       testcase( pExpr->op==TK_LE );
11c328
       testcase( pExpr->op==TK_GT );
11c328
       testcase( pExpr->op==TK_GE );
11c328
-      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
11c328
-       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
11c328
+      /* The y.pTab=0 assignment in wherecode.c always happens after the
11c328
+      ** impliesNotNullRow() test */
11c328
+      if( (pLeft->op==TK_COLUMN && ALWAYS(pLeft->y.pTab!=0)
11c328
+                               && IsVirtual(pLeft->y.pTab))
11c328
+       || (pRight->op==TK_COLUMN && ALWAYS(pRight->y.pTab!=0)
11c328
+                               && IsVirtual(pRight->y.pTab))
11c328
       ){
11c328
        return WRC_Prune;
11c328
       }
11c328
+    }
11c328
     default:
11c328
       return WRC_Continue;
11c328
   }
11c328
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
11c328
index 051aa40..5f5f3cc 100644
11c328
--- a/src/sqliteInt.h
11c328
+++ b/src/sqliteInt.h
11c328
@@ -2014,8 +2014,11 @@ struct Table {
11c328
 */
11c328
 #ifndef SQLITE_OMIT_VIRTUALTABLE
11c328
 #  define IsVirtual(X)      ((X)->nModuleArg)
11c328
+#  define ExprIsVtab(X)  \
11c328
+              ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
11c328
 #else
11c328
 #  define IsVirtual(X)      0
11c328
+#  define ExprIsVtab(X)     0
11c328
 #endif
11c328
 
11c328
 /*
11c328
diff --git a/src/whereexpr.c b/src/whereexpr.c
11c328
index dbb7f0d..9d2813a 100644
11c328
--- a/src/whereexpr.c
11c328
+++ b/src/whereexpr.c
11c328
@@ -382,7 +382,8 @@ static int isAuxiliaryVtabOperator(
11c328
     **       MATCH(expression,vtab_column)
11c328
     */
11c328
     pCol = pList->a[1].pExpr;
11c328
-    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
11c328
+    testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
11c328
+    if( ExprIsVtab(pCol) ){
11c328
       for(i=0; i
11c328
         if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
11c328
           *peOp2 = aOp[i].eOp2;
11c328
@@ -404,7 +405,8 @@ static int isAuxiliaryVtabOperator(
11c328
     ** with function names in an arbitrary case.
11c328
     */
11c328
     pCol = pList->a[0].pExpr;
11c328
-    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
11c328
+    testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
11c328
+    if( ExprIsVtab(pCol) ){
11c328
       sqlite3_vtab *pVtab;
11c328
       sqlite3_module *pMod;
11c328
       void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
11c328
@@ -427,10 +429,12 @@ static int isAuxiliaryVtabOperator(
11c328
     int res = 0;
11c328
     Expr *pLeft = pExpr->pLeft;
11c328
     Expr *pRight = pExpr->pRight;
11c328
-    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
11c328
+    testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
11c328
+    if( ExprIsVtab(pLeft) ){
11c328
       res++;
11c328
     }
11c328
-    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
11c328
+    testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
11c328
+    if( pRight && ExprIsVtab(pRight) ){
11c328
       res++;
11c328
       SWAP(Expr*, pLeft, pRight);
11c328
     }
11c328
-- 
11c328
2.24.1
11c328