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

d22c23
Subject: [PATCH] Prevent aliases of window functions expressions from being
d22c23
 used as arguments to aggregate or other window functions.
d22c23
d22c23
---
d22c23
 src/resolve.c       |  21 ++++++---
d22c23
 src/sqliteInt.h     |   2 +
d22c23
 test/windowerr.tcl  |  59 ++++++++++++++++++++++++++
d22c23
 test/windowerr.test | 99 ++++++++++++++++++++++++++++++++++++++++++
d22c23
 4 files changed, 176 insertions(+), 5 deletions(-)
d22c23
 create mode 100644 test/windowerr.tcl
d22c23
 create mode 100644 test/windowerr.test
d22c23
d22c23
diff --git a/src/resolve.c b/src/resolve.c
d22c23
index 0c7dfc0..cdcf4d9 100644
d22c23
--- a/src/resolve.c
d22c23
+++ b/src/resolve.c
d22c23
@@ -436,6 +436,10 @@ static int lookupName(
d22c23
             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
d22c23
             return WRC_Abort;
d22c23
           }
d22c23
+          if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
d22c23
+            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
d22c23
+            return WRC_Abort;
d22c23
+          }
d22c23
           if( sqlite3ExprVectorSize(pOrig)!=1 ){
d22c23
             sqlite3ErrorMsg(pParse, "row value misused");
d22c23
             return WRC_Abort;
d22c23
@@ -707,6 +711,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
d22c23
       const char *zId;            /* The function name. */
d22c23
       FuncDef *pDef;              /* Information about the function */
d22c23
       u8 enc = ENC(pParse->db);   /* The database encoding */
d22c23
+      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
d22c23
 
d22c23
       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
d22c23
       zId = pExpr->u.zToken;
d22c23
@@ -828,8 +833,11 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
d22c23
           pNC->nErr++;
d22c23
         }
d22c23
         if( is_agg ){
d22c23
+          /* Window functions may not be arguments of aggregate functions.
d22c23
+          ** Or arguments of other window functions. But aggregate functions
d22c23
+          ** may be arguments for window functions.  */
d22c23
 #ifndef SQLITE_OMIT_WINDOWFUNC
d22c23
-          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
d22c23
+          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
d22c23
 #else
d22c23
           pNC->ncFlags &= ~NC_AllowAgg;
d22c23
 #endif
d22c23
@@ -850,7 +858,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
d22c23
             pExpr->y.pWin->pNextWin = pSel->pWin;
d22c23
             pSel->pWin = pExpr->y.pWin;
d22c23
           }
d22c23
-          pNC->ncFlags |= NC_AllowWin;
d22c23
+          pNC->ncFlags |= NC_HasWin;
d22c23
         }else
d22c23
 #endif /* SQLITE_OMIT_WINDOWFUNC */
d22c23
         {
d22c23
@@ -868,8 +876,8 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
d22c23
             pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
d22c23
 
d22c23
           }
d22c23
-          pNC->ncFlags |= NC_AllowAgg;
d22c23
         }
d22c23
+        pNC->ncFlags |= savedAllowFlags;
d22c23
       }
d22c23
       /* FIX ME:  Compute pExpr->affinity based on the expected return
d22c23
       ** type of the function 
d22c23
@@ -1573,8 +1581,8 @@ int sqlite3ResolveExprNames(
d22c23
   Walker w;
d22c23
 
d22c23
   if( pExpr==0 ) return SQLITE_OK;
d22c23
-  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
d22c23
-  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
d22c23
+  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
d22c23
+  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
d22c23
   w.pParse = pNC->pParse;
d22c23
   w.xExprCallback = resolveExprStep;
d22c23
   w.xSelectCallback = resolveSelectStep;
d22c23
@@ -1593,6 +1601,9 @@ int sqlite3ResolveExprNames(
d22c23
   if( pNC->ncFlags & NC_HasAgg ){
d22c23
     ExprSetProperty(pExpr, EP_Agg);
d22c23
   }
d22c23
+  if( pNC->ncFlags & NC_HasWin ){
d22c23
+    ExprSetProperty(pExpr, EP_Win);
d22c23
+  }
d22c23
   pNC->ncFlags |= savedHasAgg;
d22c23
   return pNC->nErr>0 || w.pParse->nErr>0;
d22c23
 }
d22c23
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
d22c23
index 5f5f3cc..b7d3571 100644
d22c23
--- a/src/sqliteInt.h
d22c23
+++ b/src/sqliteInt.h
d22c23
@@ -2517,6 +2517,7 @@ struct Expr {
d22c23
 #define EP_Alias     0x400000 /* Is an alias for a result set column */
d22c23
 #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
d22c23
 #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
d22c23
+#define EP_Win      0x8000000 /* Contains window functions */
d22c23
 
d22c23
 /*
d22c23
 ** The EP_Propagate mask is a set of properties that automatically propagate
d22c23
@@ -2773,6 +2774,7 @@ struct NameContext {
d22c23
 #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
d22c23
 #define NC_Complex   0x2000  /* True if a function or subquery seen */
d22c23
 #define NC_AllowWin  0x4000  /* Window functions are allowed here */
d22c23
+#define NC_HasWin    0x8000  /* One or more window functions seen */
d22c23
 
d22c23
 /*
d22c23
 ** An instance of the following object describes a single ON CONFLICT
d22c23
diff --git a/test/windowerr.tcl b/test/windowerr.tcl
d22c23
new file mode 100644
d22c23
index 0000000..80f464d
d22c23
--- /dev/null
d22c23
+++ b/test/windowerr.tcl
d22c23
@@ -0,0 +1,59 @@
d22c23
+# 2018 May 19
d22c23
+#
d22c23
+# The author disclaims copyright to this source code.  In place of
d22c23
+# a legal notice, here is a blessing:
d22c23
+#
d22c23
+#    May you do good and not evil.
d22c23
+#    May you find forgiveness for yourself and forgive others.
d22c23
+#    May you share freely, never taking more than you give.
d22c23
+#
d22c23
+#***********************************************************************
d22c23
+#
d22c23
+
d22c23
+source [file join [file dirname $argv0] pg_common.tcl]
d22c23
+
d22c23
+#=========================================================================
d22c23
+
d22c23
+start_test windowerr "2019 March 01"
d22c23
+ifcapable !windowfunc
d22c23
+
d22c23
+execsql_test 1.0 {
d22c23
+  DROP TABLE IF EXISTS t1;
d22c23
+  CREATE TABLE t1(a INTEGER, b INTEGER);
d22c23
+  INSERT INTO t1 VALUES(1, 1);
d22c23
+  INSERT INTO t1 VALUES(2, 2);
d22c23
+  INSERT INTO t1 VALUES(3, 3);
d22c23
+  INSERT INTO t1 VALUES(4, 4);
d22c23
+  INSERT INTO t1 VALUES(5, 5);
d22c23
+}
d22c23
+
d22c23
+foreach {tn frame} {
d22c23
+  1 "ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING"
d22c23
+  2 "ORDER BY a ROWS BETWEEN  1 PRECEDING AND -1 FOLLOWING"
d22c23
+
d22c23
+  3 "ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING"
d22c23
+  4 "ORDER BY a RANGE BETWEEN  1 PRECEDING AND -1 FOLLOWING"
d22c23
+
d22c23
+  5 "ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING"
d22c23
+  6 "ORDER BY a GROUPS BETWEEN  1 PRECEDING AND -1 FOLLOWING"
d22c23
+
d22c23
+  7 "ORDER BY a,b RANGE BETWEEN  1 PRECEDING AND 1 FOLLOWING"
d22c23
+
d22c23
+  8 "PARTITION BY a RANGE BETWEEN  1 PRECEDING AND 1 FOLLOWING"
d22c23
+} {
d22c23
+  errorsql_test 1.$tn "
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    $frame
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+  "
d22c23
+}
d22c23
+errorsql_test 2.1 {
d22c23
+  SELECT sum( sum(a) OVER () ) FROM t1;
d22c23
+}
d22c23
+
d22c23
+errorsql_test 2.2 {
d22c23
+  SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz);
d22c23
+}
d22c23
+
d22c23
+
d22c23
+finish_test
d22c23
diff --git a/test/windowerr.test b/test/windowerr.test
d22c23
new file mode 100644
d22c23
index 0000000..97dae64
d22c23
--- /dev/null
d22c23
+++ b/test/windowerr.test
d22c23
@@ -0,0 +1,99 @@
d22c23
+# 2019 March 01
d22c23
+#
d22c23
+# The author disclaims copyright to this source code.  In place of
d22c23
+# a legal notice, here is a blessing:
d22c23
+#
d22c23
+#    May you do good and not evil.
d22c23
+#    May you find forgiveness for yourself and forgive others.
d22c23
+#    May you share freely, never taking more than you give.
d22c23
+#
d22c23
+#***********************************************************************
d22c23
+# This file implements regression tests for SQLite library.
d22c23
+#
d22c23
+
d22c23
+####################################################
d22c23
+# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED!
d22c23
+####################################################
d22c23
+
d22c23
+set testdir [file dirname $argv0]
d22c23
+source $testdir/tester.tcl
d22c23
+set testprefix windowerr
d22c23
+
d22c23
+ifcapable !windowfunc { finish_test ; return }
d22c23
+do_execsql_test 1.0 {
d22c23
+  DROP TABLE IF EXISTS t1;
d22c23
+  CREATE TABLE t1(a INTEGER, b INTEGER);
d22c23
+  INSERT INTO t1 VALUES(1, 1);
d22c23
+  INSERT INTO t1 VALUES(2, 2);
d22c23
+  INSERT INTO t1 VALUES(3, 3);
d22c23
+  INSERT INTO t1 VALUES(4, 4);
d22c23
+  INSERT INTO t1 VALUES(5, 5);
d22c23
+} {}
d22c23
+
d22c23
+# PG says ERROR:  frame starting offset must not be negative
d22c23
+do_test 1.1 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  frame ending offset must not be negative
d22c23
+do_test 1.2 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a ROWS BETWEEN  1 PRECEDING AND -1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  invalid preceding or following size in window function
d22c23
+do_test 1.3 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  invalid preceding or following size in window function
d22c23
+do_test 1.4 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a RANGE BETWEEN  1 PRECEDING AND -1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  frame starting offset must not be negative
d22c23
+do_test 1.5 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  frame ending offset must not be negative
d22c23
+do_test 1.6 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a GROUPS BETWEEN  1 PRECEDING AND -1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
d22c23
+do_test 1.7 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    ORDER BY a,b RANGE BETWEEN  1 PRECEDING AND 1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
d22c23
+do_test 1.8 { catch { execsql {
d22c23
+  SELECT a, sum(b) OVER (
d22c23
+    PARTITION BY a RANGE BETWEEN  1 PRECEDING AND 1 FOLLOWING
d22c23
+  ) FROM t1 ORDER BY 1
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  aggregate function calls cannot contain window function calls
d22c23
+do_test 2.1 { catch { execsql {
d22c23
+  SELECT sum( sum(a) OVER () ) FROM t1;
d22c23
+} } } 1
d22c23
+
d22c23
+# PG says ERROR:  column "xyz" does not exist
d22c23
+do_test 2.2 { catch { execsql {
d22c23
+  SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz);
d22c23
+} } } 1
d22c23
+
d22c23
+finish_test
d22c23
-- 
d22c23
2.24.1
d22c23