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

094f44
From 7d47517d579601bb6e59e33bf0896f0ed36aa0aa Mon Sep 17 00:00:00 2001
094f44
From: Ondrej Dubaj <odubaj@redhat.com>
094f44
Date: Mon, 20 Jan 2020 09:34:41 +0100
094f44
Subject: [PATCH] Continue to back away from the LEFT JOIN optimization of
094f44
 check-in
094f44
094f44
by disallowing query flattening if the outer query is DISTINCT.  Without this fix,
094f44
if an index scan is run on the table within the view on the right-hand side of the
094f44
LEFT JOIN, stale result registers might be accessed yielding incorrect results,
094f44
and/or an OP_IfNullRow opcode might be invoked on the un-opened table, resulting
094f44
in a NULL-pointer dereference.  This problem was found by the Yongheng and Rui fuzzer.
094f44
---
094f44
 src/select.c   |  8 ++++++--
094f44
 test/join.test | 13 +++++++++++++
094f44
 2 files changed, 19 insertions(+), 2 deletions(-)
094f44
094f44
diff --git a/src/select.c b/src/select.c
094f44
index c60ff27..0205a08 100644
094f44
--- a/src/select.c
094f44
+++ b/src/select.c
094f44
@@ -3569,6 +3569,7 @@ static void substSelect(
094f44
 **        (3b) the FROM clause of the subquery may not contain a virtual
094f44
 **             table and
094f44
 **        (3c) the outer query may not be an aggregate.
094f44
+**        (3d) the outer query may not be DISTINCT.
094f44
 **
094f44
 **   (4)  The subquery can not be DISTINCT.
094f44
 **
094f44
@@ -3765,8 +3766,11 @@ static int flattenSubquery(
094f44
   */
094f44
   if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
094f44
     isLeftJoin = 1;
094f44
-    if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
094f44
-      /*  (3a)             (3c)     (3b) */
094f44
+    if( pSubSrc->nSrc>1                   /* (3a) */
094f44
+     || isAgg                             /* (3b) */
094f44
+     || IsVirtual(pSubSrc->a[0].pTab)     /* (3c) */
094f44
+     || (p->selFlags & SF_Distinct)!=0    /* (3d) */
094f44
+    ){
094f44
       return 0;
094f44
     }
094f44
   }
094f44
diff --git a/test/join.test b/test/join.test
094f44
index 8c6f463..8c6a53d 100644
094f44
--- a/test/join.test
094f44
+++ b/test/join.test
094f44
@@ -844,4 +844,17 @@ do_execsql_test join-15.110 {
094f44
    ORDER BY a1, a2, a3, a4, a5;
094f44
 } {1 {} {} {} {} 1 11 {} {} {} 1 12 {} {} {} 1 12 121 {} {} 1 13 {} {} {}}
094f44
 
094f44
+# 2019-12-18 problem with a LEFT JOIN where the RHS is a view.
094f44
+# Detected by Yongheng and Rui.
094f44
+# Follows from the optimization attempt of check-in 41c27bc0ff1d3135
094f44
+# on 2017-04-18
094f44
+#
094f44
+reset_db
094f44
+do_execsql_test join-22.10 {
094f44
+  CREATE TABLE t0(a, b);
094f44
+  CREATE INDEX t0a ON t0(a);
094f44
+  INSERT INTO t0 VALUES(10,10),(10,11),(10,12);
094f44
+  SELECT DISTINCT c FROM t0 LEFT JOIN (SELECT a+1 AS c FROM t0) ORDER BY c ;
094f44
+} {11}
094f44
+
094f44
 finish_test
094f44
-- 
094f44
2.19.1
094f44