Skip to content

Commit

Permalink
Handle RLS dependencies in inlined set-returning functions properly.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tglsfdc committed May 8, 2023
1 parent 681d9e4 commit ca73753
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/backend/optimizer/util/clauses.c
Expand Up @@ -5205,6 +5205,13 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
*/
record_plan_function_dependency(root, func_oid);

/*
* We must also notice if the inserted query adds a dependency on the
* calling role due to RLS quals.
*/
if (querytree->hasRowSecurity)
root->glob->dependsOnRole = true;

return querytree;

/* Here if func is not inlinable: release temp memory and return NULL */
Expand Down
27 changes: 27 additions & 0 deletions src/test/regress/expected/rowsecurity.out
Expand Up @@ -4427,6 +4427,33 @@ SELECT * FROM rls_tbl;

DROP TABLE rls_tbl;
RESET SESSION AUTHORIZATION;
-- CVE-2023-2455: inlining an SRF may introduce an RLS dependency
create table rls_t (c text);
insert into rls_t values ('invisible to bob');
alter table rls_t enable row level security;
grant select on rls_t to regress_rls_alice, regress_rls_bob;
create policy p1 on rls_t for select to regress_rls_alice using (true);
create policy p2 on rls_t for select to regress_rls_bob using (false);
create function rls_f () returns setof rls_t
stable language sql
as $$ select * from rls_t $$;
prepare q as select current_user, * from rls_f();
set role regress_rls_alice;
execute q;
current_user | c
-------------------+------------------
regress_rls_alice | invisible to bob
(1 row)

set role regress_rls_bob;
execute q;
current_user | c
--------------+---
(0 rows)

RESET ROLE;
DROP FUNCTION rls_f();
DROP TABLE rls_t;
--
-- Clean up objects
--
Expand Down
20 changes: 20 additions & 0 deletions src/test/regress/sql/rowsecurity.sql
Expand Up @@ -2127,6 +2127,26 @@ SELECT * FROM rls_tbl;
DROP TABLE rls_tbl;
RESET SESSION AUTHORIZATION;

-- CVE-2023-2455: inlining an SRF may introduce an RLS dependency
create table rls_t (c text);
insert into rls_t values ('invisible to bob');
alter table rls_t enable row level security;
grant select on rls_t to regress_rls_alice, regress_rls_bob;
create policy p1 on rls_t for select to regress_rls_alice using (true);
create policy p2 on rls_t for select to regress_rls_bob using (false);
create function rls_f () returns setof rls_t
stable language sql
as $$ select * from rls_t $$;
prepare q as select current_user, * from rls_f();
set role regress_rls_alice;
execute q;
set role regress_rls_bob;
execute q;

RESET ROLE;
DROP FUNCTION rls_f();
DROP TABLE rls_t;

--
-- Clean up objects
--
Expand Down

0 comments on commit ca73753

Please sign in to comment.