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

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