Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ca73753

Browse files
committed
Handle RLS dependencies in inlined set-returning functions properly.
If an SRF in the FROM clause references a table having row-level security policies, and we inline that SRF into the calling query, we neglected to mark the plan as potentially dependent on which role is executing it. This could lead to later executions in the same session returning or hiding rows that should have been hidden or returned instead. Our thanks to Wolfgang Walther for reporting this problem. Stephen Frost and Tom Lane Security: CVE-2023-2455
1 parent 681d9e4 commit ca73753

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5205,6 +5205,13 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
52055205
*/
52065206
record_plan_function_dependency(root, func_oid);
52075207

5208+
/*
5209+
* We must also notice if the inserted query adds a dependency on the
5210+
* calling role due to RLS quals.
5211+
*/
5212+
if (querytree->hasRowSecurity)
5213+
root->glob->dependsOnRole = true;
5214+
52085215
return querytree;
52095216

52105217
/* Here if func is not inlinable: release temp memory and return NULL */

src/test/regress/expected/rowsecurity.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,6 +4427,33 @@ SELECT * FROM rls_tbl;
44274427

44284428
DROP TABLE rls_tbl;
44294429
RESET SESSION AUTHORIZATION;
4430+
-- CVE-2023-2455: inlining an SRF may introduce an RLS dependency
4431+
create table rls_t (c text);
4432+
insert into rls_t values ('invisible to bob');
4433+
alter table rls_t enable row level security;
4434+
grant select on rls_t to regress_rls_alice, regress_rls_bob;
4435+
create policy p1 on rls_t for select to regress_rls_alice using (true);
4436+
create policy p2 on rls_t for select to regress_rls_bob using (false);
4437+
create function rls_f () returns setof rls_t
4438+
stable language sql
4439+
as $$ select * from rls_t $$;
4440+
prepare q as select current_user, * from rls_f();
4441+
set role regress_rls_alice;
4442+
execute q;
4443+
current_user | c
4444+
-------------------+------------------
4445+
regress_rls_alice | invisible to bob
4446+
(1 row)
4447+
4448+
set role regress_rls_bob;
4449+
execute q;
4450+
current_user | c
4451+
--------------+---
4452+
(0 rows)
4453+
4454+
RESET ROLE;
4455+
DROP FUNCTION rls_f();
4456+
DROP TABLE rls_t;
44304457
--
44314458
-- Clean up objects
44324459
--

src/test/regress/sql/rowsecurity.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,26 @@ SELECT * FROM rls_tbl;
21272127
DROP TABLE rls_tbl;
21282128
RESET SESSION AUTHORIZATION;
21292129

2130+
-- CVE-2023-2455: inlining an SRF may introduce an RLS dependency
2131+
create table rls_t (c text);
2132+
insert into rls_t values ('invisible to bob');
2133+
alter table rls_t enable row level security;
2134+
grant select on rls_t to regress_rls_alice, regress_rls_bob;
2135+
create policy p1 on rls_t for select to regress_rls_alice using (true);
2136+
create policy p2 on rls_t for select to regress_rls_bob using (false);
2137+
create function rls_f () returns setof rls_t
2138+
stable language sql
2139+
as $$ select * from rls_t $$;
2140+
prepare q as select current_user, * from rls_f();
2141+
set role regress_rls_alice;
2142+
execute q;
2143+
set role regress_rls_bob;
2144+
execute q;
2145+
2146+
RESET ROLE;
2147+
DROP FUNCTION rls_f();
2148+
DROP TABLE rls_t;
2149+
21302150
--
21312151
-- Clean up objects
21322152
--

0 commit comments

Comments
 (0)