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

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