Skip to content

Commit 6c954a6

Browse files
committed
Fix two errors with nested CASE/WHEN constructs.
ExecEvalCase() tried to save a cycle or two by passing &econtext->caseValue_isNull as the isNull argument to its sub-evaluation of the CASE value expression. If that subexpression itself contained a CASE, then *isNull was an alias for econtext->caseValue_isNull within the recursive call of ExecEvalCase(), leading to confusion about whether the inner call's caseValue was null or not. In the worst case this could lead to a core dump due to dereferencing a null pointer. Fix by not assigning to the global variable until control comes back from the subexpression. Also, avoid using the passed-in isNull pointer transiently for evaluation of WHEN expressions. (Either one of these changes would have been sufficient to fix the known misbehavior, but it's clear now that each of these choices was in itself dangerous coding practice and best avoided. There do not seem to be any similar hazards elsewhere in execQual.c.) Also, it was possible for inlining of a SQL function that implements the equality operator used for a CASE comparison to result in one CASE expression's CaseTestExpr node being inserted inside another CASE expression. This would certainly result in wrong answers since the improperly nested CaseTestExpr would be caused to return the inner CASE's comparison value not the outer's. If the CASE values were of different data types, a crash might result; moreover such situations could be abused to allow disclosure of portions of server memory. To fix, teach inline_function to check for "bare" CaseTestExpr nodes in the arguments of a function to be inlined, and avoid inlining if there are any. Heikki Linnakangas, Michael Paquier, Tom Lane Report: https://github.com/greenplum-db/gpdb/pull/327 Report: <4DDCEEB8.50602@enterprisedb.com> Security: CVE-2016-5423
1 parent 95a6855 commit 6c954a6

File tree

4 files changed

+185
-5
lines changed

4 files changed

+185
-5
lines changed

src/backend/executor/execQual.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,19 +2945,30 @@ ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
29452945

29462946
/*
29472947
* If there's a test expression, we have to evaluate it and save the value
2948-
* where the CaseTestExpr placeholders can find it. We must save and
2948+
* where the CaseTestExpr placeholders can find it. We must save and
29492949
* restore prior setting of econtext's caseValue fields, in case this node
2950-
* is itself within a larger CASE.
2950+
* is itself within a larger CASE. Furthermore, don't assign to the
2951+
* econtext fields until after returning from evaluation of the test
2952+
* expression. We used to pass &econtext->caseValue_isNull to the
2953+
* recursive call, but that leads to aliasing that variable within said
2954+
* call, which can (and did) produce bugs when the test expression itself
2955+
* contains a CASE.
2956+
*
2957+
* If there's no test expression, we don't actually need to save and
2958+
* restore these fields; but it's less code to just do so unconditionally.
29512959
*/
29522960
save_datum = econtext->caseValue_datum;
29532961
save_isNull = econtext->caseValue_isNull;
29542962

29552963
if (caseExpr->arg)
29562964
{
2965+
bool arg_isNull;
2966+
29572967
econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
29582968
econtext,
2959-
&econtext->caseValue_isNull,
2969+
&arg_isNull,
29602970
NULL);
2971+
econtext->caseValue_isNull = arg_isNull;
29612972
}
29622973

29632974
/*
@@ -2969,18 +2980,19 @@ ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
29692980
{
29702981
CaseWhenState *wclause = lfirst(clause);
29712982
Datum clause_value;
2983+
bool clause_isNull;
29722984

29732985
clause_value = ExecEvalExpr(wclause->expr,
29742986
econtext,
2975-
isNull,
2987+
&clause_isNull,
29762988
NULL);
29772989

29782990
/*
29792991
* if we have a true test, then we return the result, since the case
29802992
* statement is satisfied. A NULL result from the test is not
29812993
* considered true.
29822994
*/
2983-
if (DatumGetBool(clause_value) && !*isNull)
2995+
if (DatumGetBool(clause_value) && !clause_isNull)
29842996
{
29852997
econtext->caseValue_datum = save_datum;
29862998
econtext->caseValue_isNull = save_isNull;

src/backend/optimizer/util/clauses.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ static bool contain_subplans_walker(Node *node, void *context);
9494
static bool contain_mutable_functions_walker(Node *node, void *context);
9595
static bool contain_volatile_functions_walker(Node *node, void *context);
9696
static bool contain_nonstrict_functions_walker(Node *node, void *context);
97+
static bool contain_context_dependent_node(Node *clause);
98+
static bool contain_context_dependent_node_walker(Node *node, int *flags);
9799
static bool contain_leaky_functions_walker(Node *node, void *context);
98100
static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
99101
static List *find_nonnullable_vars_walker(Node *node, bool top_level);
@@ -1186,6 +1188,76 @@ contain_nonstrict_functions_walker(Node *node, void *context)
11861188
context);
11871189
}
11881190

1191+
/*****************************************************************************
1192+
* Check clauses for context-dependent nodes
1193+
*****************************************************************************/
1194+
1195+
/*
1196+
* contain_context_dependent_node
1197+
* Recursively search for context-dependent nodes within a clause.
1198+
*
1199+
* CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
1200+
* not nested within another one, or they'll see the wrong test value. If one
1201+
* appears "bare" in the arguments of a SQL function, then we can't inline the
1202+
* SQL function for fear of creating such a situation.
1203+
*
1204+
* CoerceToDomainValue would have the same issue if domain CHECK expressions
1205+
* could get inlined into larger expressions, but presently that's impossible.
1206+
* Still, it might be allowed in future, or other node types with similar
1207+
* issues might get invented. So give this function a generic name, and set
1208+
* up the recursion state to allow multiple flag bits.
1209+
*/
1210+
static bool
1211+
contain_context_dependent_node(Node *clause)
1212+
{
1213+
int flags = 0;
1214+
1215+
return contain_context_dependent_node_walker(clause, &flags);
1216+
}
1217+
1218+
#define CCDN_IN_CASEEXPR 0x0001 /* CaseTestExpr okay here? */
1219+
1220+
static bool
1221+
contain_context_dependent_node_walker(Node *node, int *flags)
1222+
{
1223+
if (node == NULL)
1224+
return false;
1225+
if (IsA(node, CaseTestExpr))
1226+
return !(*flags & CCDN_IN_CASEEXPR);
1227+
if (IsA(node, CaseExpr))
1228+
{
1229+
CaseExpr *caseexpr = (CaseExpr *) node;
1230+
1231+
/*
1232+
* If this CASE doesn't have a test expression, then it doesn't create
1233+
* a context in which CaseTestExprs should appear, so just fall
1234+
* through and treat it as a generic expression node.
1235+
*/
1236+
if (caseexpr->arg)
1237+
{
1238+
int save_flags = *flags;
1239+
bool res;
1240+
1241+
/*
1242+
* Note: in principle, we could distinguish the various sub-parts
1243+
* of a CASE construct and set the flag bit only for some of them,
1244+
* since we are only expecting CaseTestExprs to appear in the
1245+
* "expr" subtree of the CaseWhen nodes. But it doesn't really
1246+
* seem worth any extra code. If there are any bare CaseTestExprs
1247+
* elsewhere in the CASE, something's wrong already.
1248+
*/
1249+
*flags |= CCDN_IN_CASEEXPR;
1250+
res = expression_tree_walker(node,
1251+
contain_context_dependent_node_walker,
1252+
(void *) flags);
1253+
*flags = save_flags;
1254+
return res;
1255+
}
1256+
}
1257+
return expression_tree_walker(node, contain_context_dependent_node_walker,
1258+
(void *) flags);
1259+
}
1260+
11891261
/*****************************************************************************
11901262
* Check clauses for non-leakproof functions
11911263
*****************************************************************************/
@@ -4087,6 +4159,8 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
40874159
* doesn't work in the general case because it discards information such
40884160
* as OUT-parameter declarations.
40894161
*
4162+
* Also, context-dependent expression nodes in the argument list are trouble.
4163+
*
40904164
* Returns a simplified expression if successful, or NULL if cannot
40914165
* simplify the function.
40924166
*/
@@ -4280,6 +4354,13 @@ inline_function(Oid funcid, Oid result_type, Oid result_collid,
42804354
contain_nonstrict_functions(newexpr))
42814355
goto fail;
42824356

4357+
/*
4358+
* If any parameter expression contains a context-dependent node, we can't
4359+
* inline, for fear of putting such a node into the wrong context.
4360+
*/
4361+
if (contain_context_dependent_node((Node *) args))
4362+
goto fail;
4363+
42834364
/*
42844365
* We may be able to do it; there are still checks on parameter usage to
42854366
* make, but those are most easily done in combination with the actual

src/test/regress/expected/case.out

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,52 @@ SELECT * FROM CASE_TBL;
296296
-8 | 10.1
297297
(4 rows)
298298

299+
--
300+
-- Nested CASE expressions
301+
--
302+
-- This test exercises a bug caused by aliasing econtext->caseValue_isNull
303+
-- with the isNull argument of the inner CASE's ExecEvalCase() call. After
304+
-- evaluating the vol(null) expression in the inner CASE's second WHEN-clause,
305+
-- the isNull flag for the case test value incorrectly became true, causing
306+
-- the third WHEN-clause not to match. The volatile function calls are needed
307+
-- to prevent constant-folding in the planner, which would hide the bug.
308+
CREATE FUNCTION vol(text) returns text as
309+
'begin return $1; end' language plpgsql volatile;
310+
SELECT CASE
311+
(CASE vol('bar')
312+
WHEN 'foo' THEN 'it was foo!'
313+
WHEN vol(null) THEN 'null input'
314+
WHEN 'bar' THEN 'it was bar!' END
315+
)
316+
WHEN 'it was foo!' THEN 'foo recognized'
317+
WHEN 'it was bar!' THEN 'bar recognized'
318+
ELSE 'unrecognized' END;
319+
case
320+
----------------
321+
bar recognized
322+
(1 row)
323+
324+
-- In this case, we can't inline the SQL function without confusing things.
325+
CREATE DOMAIN foodomain AS text;
326+
CREATE FUNCTION volfoo(text) returns foodomain as
327+
'begin return $1::foodomain; end' language plpgsql volatile;
328+
CREATE FUNCTION inline_eq(foodomain, foodomain) returns boolean as
329+
'SELECT CASE $2::text WHEN $1::text THEN true ELSE false END' language sql;
330+
CREATE OPERATOR = (procedure = inline_eq,
331+
leftarg = foodomain, rightarg = foodomain);
332+
SELECT CASE volfoo('bar') WHEN 'foo'::foodomain THEN 'is foo' ELSE 'is not foo' END;
333+
case
334+
------------
335+
is not foo
336+
(1 row)
337+
299338
--
300339
-- Clean up
301340
--
302341
DROP TABLE CASE_TBL;
303342
DROP TABLE CASE2_TBL;
343+
DROP OPERATOR = (foodomain, foodomain);
344+
DROP FUNCTION inline_eq(foodomain, foodomain);
345+
DROP FUNCTION volfoo(text);
346+
DROP DOMAIN foodomain;
347+
DROP FUNCTION vol(text);

src/test/regress/sql/case.sql

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,52 @@ UPDATE CASE_TBL
156156

157157
SELECT * FROM CASE_TBL;
158158

159+
--
160+
-- Nested CASE expressions
161+
--
162+
163+
-- This test exercises a bug caused by aliasing econtext->caseValue_isNull
164+
-- with the isNull argument of the inner CASE's ExecEvalCase() call. After
165+
-- evaluating the vol(null) expression in the inner CASE's second WHEN-clause,
166+
-- the isNull flag for the case test value incorrectly became true, causing
167+
-- the third WHEN-clause not to match. The volatile function calls are needed
168+
-- to prevent constant-folding in the planner, which would hide the bug.
169+
170+
CREATE FUNCTION vol(text) returns text as
171+
'begin return $1; end' language plpgsql volatile;
172+
173+
SELECT CASE
174+
(CASE vol('bar')
175+
WHEN 'foo' THEN 'it was foo!'
176+
WHEN vol(null) THEN 'null input'
177+
WHEN 'bar' THEN 'it was bar!' END
178+
)
179+
WHEN 'it was foo!' THEN 'foo recognized'
180+
WHEN 'it was bar!' THEN 'bar recognized'
181+
ELSE 'unrecognized' END;
182+
183+
-- In this case, we can't inline the SQL function without confusing things.
184+
CREATE DOMAIN foodomain AS text;
185+
186+
CREATE FUNCTION volfoo(text) returns foodomain as
187+
'begin return $1::foodomain; end' language plpgsql volatile;
188+
189+
CREATE FUNCTION inline_eq(foodomain, foodomain) returns boolean as
190+
'SELECT CASE $2::text WHEN $1::text THEN true ELSE false END' language sql;
191+
192+
CREATE OPERATOR = (procedure = inline_eq,
193+
leftarg = foodomain, rightarg = foodomain);
194+
195+
SELECT CASE volfoo('bar') WHEN 'foo'::foodomain THEN 'is foo' ELSE 'is not foo' END;
196+
159197
--
160198
-- Clean up
161199
--
162200

163201
DROP TABLE CASE_TBL;
164202
DROP TABLE CASE2_TBL;
203+
DROP OPERATOR = (foodomain, foodomain);
204+
DROP FUNCTION inline_eq(foodomain, foodomain);
205+
DROP FUNCTION volfoo(text);
206+
DROP DOMAIN foodomain;
207+
DROP FUNCTION vol(text);

0 commit comments

Comments
 (0)