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

c11ca1
Subject: [PATCH] When rewriting a query for window functions, if the rewrite 
c11ca1
changes the depth of TK_AGG_FUNCTION nodes, be sure to adjust the Expr.op2 
c11ca1
field appropriately.
c11ca1
c11ca1
diff --git a/src/resolve.c b/src/resolve.c
c11ca1
index cdcf4d9..c47f6bb 100644
c11ca1
--- a/src/resolve.c
c11ca1
+++ b/src/resolve.c
c11ca1
@@ -24,6 +24,8 @@
c11ca1
 **
c11ca1
 ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
c11ca1
 ** is a helper function - a callback for the tree walker.
c11ca1
+**
c11ca1
+** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
c11ca1
 */
c11ca1
 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
c11ca1
   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
c11ca1
diff --git a/src/select.c b/src/select.c
c11ca1
index a6d1757..6f5570c 100644
c11ca1
--- a/src/select.c
c11ca1
+++ b/src/select.c
c11ca1
@@ -1961,7 +1961,7 @@ int sqlite3ColumnsFromExprList(
c11ca1
         assert( pColExpr!=0 );
c11ca1
       }
c11ca1
       assert( pColExpr->op!=TK_AGG_COLUMN );
c11ca1
-      if( pColExpr->op==TK_COLUMN ){
c11ca1
+      if( pColExpr->op==TK_COLUMN && pColExpr->y.pTab ){
c11ca1
         /* For columns use the column name name */
c11ca1
         int iCol = pColExpr->iColumn;
c11ca1
         Table *pTab = pColExpr->y.pTab;
c11ca1
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
c11ca1
index 1cf6937..ea9a7ae 100644
c11ca1
--- a/src/sqliteInt.h
c11ca1
+++ b/src/sqliteInt.h
c11ca1
@@ -3579,6 +3579,8 @@ void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
c11ca1
 Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
c11ca1
 Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
c11ca1
 void sqlite3WindowFunctions(void);
c11ca1
+int sqlite3WalkerDepthIncrease(Walker*,Select*);
c11ca1
+void sqlite3WalkerDepthDecrease(Walker*,Select*);
c11ca1
 #else
c11ca1
 # define sqlite3WindowDelete(a,b)
c11ca1
 # define sqlite3WindowFunctions()
c11ca1
diff --git a/src/walker.c b/src/walker.c
c11ca1
index c31d94f..8cd3b65 100644
c11ca1
--- a/src/walker.c
c11ca1
+++ b/src/walker.c
c11ca1
@@ -165,3 +165,16 @@ int sqlite3WalkSelect(Walker *pWalker, Select *p){
c11ca1
   }while( p!=0 );
c11ca1
   return WRC_Continue;
c11ca1
 }
c11ca1
+
c11ca1
+/* Increase the walkerDepth when entering a subquery, and
c11ca1
+** descrease when leaving the subquery.
c11ca1
+*/
c11ca1
+int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
c11ca1
+  UNUSED_PARAMETER(pSelect);
c11ca1
+  pWalker->walkerDepth++;
c11ca1
+  return WRC_Continue;
c11ca1
+}
c11ca1
+void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
c11ca1
+  UNUSED_PARAMETER(pSelect);
c11ca1
+  pWalker->walkerDepth--;
c11ca1
+}
c11ca1
\ No newline at end of file
c11ca1
diff --git a/src/window.c b/src/window.c
c11ca1
index c65eadd..48d8090 100644
c11ca1
--- a/src/window.c
c11ca1
+++ b/src/window.c
c11ca1
@@ -738,6 +738,23 @@ static ExprList *exprListAppendList(
c11ca1
   return pList;
c11ca1
 }
c11ca1
 
c11ca1
+/*
c11ca1
+** When rewriting a query, if the new subquery in the FROM clause
c11ca1
+** contains TK_AGG_FUNCTION nodes that refer to an outer query,
c11ca1
+** then we have to increase the Expr->op2 values of those nodes
c11ca1
+** due to the extra subquery layer that was added.
c11ca1
+**
c11ca1
+** See also the incrAggDepth() routine in resolve.c
c11ca1
+*/
c11ca1
+static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
c11ca1
+  if( pExpr->op==TK_AGG_FUNCTION
c11ca1
+   && pExpr->op2>=pWalker->walkerDepth
c11ca1
+  ){
c11ca1
+    pExpr->op2++;
c11ca1
+  }
c11ca1
+  return WRC_Continue;
c11ca1
+}
c11ca1
+
c11ca1
 /*
c11ca1
 ** If the SELECT statement passed as the second argument does not invoke
c11ca1
 ** any SQL window functions, this function is a no-op. Otherwise, it 
c11ca1
@@ -827,14 +844,24 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){
c11ca1
     p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
c11ca1
     assert( p->pSrc || db->mallocFailed );
c11ca1
     if( p->pSrc ){
c11ca1
+      Table *pTab2;
c11ca1
+      Walker w;
c11ca1
       p->pSrc->a[0].pSelect = pSub;
c11ca1
       sqlite3SrcListAssignCursors(pParse, p->pSrc);
c11ca1
-      if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
c11ca1
+      pTab2 = sqlite3ResultSetOfSelect(pParse, pSub);
c11ca1
+      if( pTab2==0 ){
c11ca1
         rc = SQLITE_NOMEM;
c11ca1
       }else{
c11ca1
         pSub->selFlags |= SF_Expanded;
c11ca1
         p->selFlags &= ~SF_Aggregate;
c11ca1
         sqlite3SelectPrep(pParse, pSub, 0);
c11ca1
+        pTab2->tabFlags |= TF_Ephemeral;
c11ca1
+        p->pSrc->a[0].pTab = pTab2;
c11ca1
+        memset(&w, 0, sizeof(w));
c11ca1
+        w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
c11ca1
+        w.xSelectCallback = sqlite3WalkerDepthIncrease;
c11ca1
+        w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
c11ca1
+        sqlite3WalkSelect(&w, pSub);
c11ca1
       }
c11ca1
 
c11ca1
       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
c11ca1
diff --git a/test/window1.test b/test/window1.test
c11ca1
index a8399a8..13ecc32 100644
c11ca1
--- a/test/window1.test
c11ca1
+++ b/test/window1.test
c11ca1
@@ -594,4 +594,20 @@ do_execsql_test 13.5 {
c11ca1
 } {
c11ca1
 }
c11ca1
 
c11ca1
+# 2020-05-23
c11ca1
+# ticket 7a5279a25c57adf1
c11ca1
+#
c11ca1
+reset_db
c11ca1
+do_execsql_test 53.0 {
c11ca1
+  CREATE TABLE a(c UNIQUE);
c11ca1
+  INSERT INTO a VALUES(4),(0),(9),(-9);
c11ca1
+  SELECT a.c
c11ca1
+    FROM a
c11ca1
+    JOIN a AS b ON a.c=4
c11ca1
+    JOIN a AS e ON a.c=e.c
c11ca1
+   WHERE a.c=(SELECT (SELECT coalesce(lead(2) OVER(),0) + sum(d.c))
c11ca1
+                FROM a AS d
c11ca1
+               WHERE a.c);
c11ca1
+} {4 4 4 4}
c11ca1
+
c11ca1
 finish_test