8000 Fix two errors with nested CASE/WHEN constructs. · s-monk/postgres@5327b76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5327b76

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 a2385ca commit 5327b76

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
@@ -2889,19 +2889,30 @@ ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
28892889

28902890
/*
28912891
* If there's a test expression, we have to evaluate it and save the value
2892-
* where the CaseTestExpr placeholders can find it. We must save and
2892+
* where the CaseTestExpr placeholders can find it. We must save and
28932893
* restore prior setting of econtext's caseValue fields, in case this node
2894-
* is itself within a larger CASE.
2894+
* is itself within a larger CASE. Furthermore, don't assign to the
2895+
* econtext fields until after returning from evaluation of the test
2896+
* expression. We used to pass &econtext->caseValue_isNull to the
2897+
* recursive call, but that leads to aliasing that variable within said
2898+
* call, which can (and did) produce bugs when the test expression itself
2899+
* contains a CASE.
2900+
*
2901+
* If there's no test expression, we don't actually need to save and
2902+
* restore these fields; but it's less code to just do so unconditionally.
28952903
*/
28962904
save_datum = econtext->caseValue_datum;
28972905
save_isNull = econtext->caseValue_isNull;
28982906

28992907
if (caseExpr->arg)
29002908
{
2909+
bool arg_isNull;
2910+
29012911
econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
29022912
econtext,
2903-
&econtext->caseValue_isNull,
2913+
&arg_isNull,
29042914
NULL);
2915+
econtext->caseValue_isNull = arg_isNull;
29052916
}
29062917

29072918
/*
@@ -2913,18 +2924,19 @@ ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
29132924
{
29142925
CaseWhenState *wclause = lfirst(clause);
29152926
Datum clause_value;
2927+
bool clause_isNull;
29162928

29172929
clause_value = ExecEvalExpr(wclause->expr,
29182930
econtext,
2919-
isNull,
2931+
&clause_isNull,
29202932
NULL);
29212933

29222934
/*
29232935
* if we have a true test, then we return the result, since the case
29242936
* statement is satisfied. A NULL result from the test is not
29252937 8000
* considered true.
29262938
*/
2927-
if (DatumGetBool(clause_value) && !*isNull)
2939+
if (DatumGetBool(clause_value) && !clause_isNull)
29282940
{
29292941
econtext->caseValue_datum = save_datum;
29302942
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
@@ -93,6 +93,8 @@ static bool contain_subplans_walker(Node *node, void *context);
9393
static bool contain_mutable_functions_walker(Node *node, void *context);
9494
static bool contain_volatile_functions_walker(Node *node, void *context);
9595
static bool contain_nonstrict_functions_walker(Node *node, void *context);
96+
static bool contain_context_dependent_node(Node *clause);
97+
static bool contain_context_dependent_node_walker(Node *node, int *flags);
9698
static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
9799
static List *find_nonnullable_vars_walker(Node *node, bool top_level);
98100
static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
@@ -1150,6 +1152,76 @@ contain_nonstrict_functions_walker(Node *node, void *context)
11501152
context);
11511153
}
11521154

1155+
/*****************************************************************************
1156+
* Check clauses for context-dependent nodes
1157+
*****************************************************************************/
1158+
1159+
/*
1160+
* contain_context_dependent_node
1161+
* Recursively search for context-dependent nodes within a clause.
1162+
*
1163+
* CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
1164+
* not nested within another one, or they'll see the wrong test value. If one
1165+
* appears "bare" in the arguments of a SQL function, then we can't inline the
1166+
* SQL function for fear of creating such a situation.
1167+
*
1168+
* CoerceToDomainValue would have the same issue if domain CHECK expressions
1169+
* could get inlined into larger expressions, but presently that's impossible.
1170+
* Still, it might be allowed in future, or other node types with similar
1171+
* issues might get invented. So give this function a generic name, and set
1172+
* up the recursion state to allow multiple flag bits.
1173+
*/
1174+
static bool
1175+
contain_context_dependent_node(Node *clause)
1176+
{
1177+
int flags = 0;
1178+
1179+
return contain_context_dependent_node_walker(clause, &flags);
1180+
}
1181+
1182+
#define CCDN_IN_CASEEXPR 0x0001 /* CaseTestExpr okay here? */
1183+
1184+
static bool
1185+
contain_context_dependent_node_walker(Node *node, int *flags)
1186+
{
1187+
if (node == NULL)
1188+
return false;
1189+
if (IsA(node, CaseTestExpr))
1190+
return !(*flags & CCDN_IN_CASEEXPR);
1191+
if (IsA(node, CaseExpr))
1192+
{
1193+
CaseExpr *caseexpr = (CaseExpr *) node;
1194+
1195+
/*
1196+
* If this CASE doesn't have a test expression, then it doesn't create
1197+
* a context in which CaseTestExprs should appear, so just fall
1198+
* through and treat it as a generic expression node.
1199+
*/
1200+
if (caseexpr->arg)
1201+
{
1202+
int save_flags = *flags;
1203+
bool res;
1204+
1205+
/*
1206+
* Note: in principle, we could distinguish the various sub-parts
1207+
* of a CASE construct and set the flag bit only for some of them,
1208+
* since we are only expecting CaseTestExprs to appear in the
1209+
* "expr" subtree of the CaseWhen nodes. But it doesn't really
1210+
* seem worth any extra code. If there are any bare CaseTestExprs
1211+
* elsewhere in the CASE, something's wrong already.
1212+
*/
1213+
*flags |= CCDN_IN_CASEEXPR;
1214+
res = expression_tree_walker(node,
1215+
contain_context_dependent_node_walker,
1216+
(void *) flags);
1217+
*flags = save_flags;
1218+
return res;
1219+
}
1220+
}
1221+
return expression_tree_walker(node, contain_context_dependent_node_walker,
1222+
(void *) flags);
1223+
}
1224+
11531225

11541226
/*
11551227
* find_nonnullable_rels
@@ -3826,6 +3898,8 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
38263898
* doesn't work in the general case because it discards information such
38273899
* as OUT-parameter declarations.
38283900
*
3901+
* Also, context-dependent expression nodes in the argument list are trouble.
3902+
*
38293903
* Returns a simplified expression if successful, or NULL if cannot
38303904
* simplify the function.
38313905
*/
@@ -4018,6 +4092,13 @@ inline_function(Oid funcid, Oid result_type, Oid result_collid,
40184092
contain_nonstrict_functions(newexpr))
40194093
goto fail;
40204094

4095+
/*
4096+
* If any parameter expression contains a context-dependent node, we can't
4097+
* inline, for fear of putting such a node into the wrong context.
4098+
*/
4099+
if (contain_context_dependent_node((Node *) args))
4100+
goto fail;
4101+
40214102
/*
40224103
* We may be able to do it; there are still checks on parameter usage to
40234104
* 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) THE E490 N '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)
0