]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Handle RLS dependencies in inlined set-returning functions properly.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 May 2023 14:12:45 +0000 (10:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 May 2023 14:12:45 +0000 (10:12 -0400)
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

src/backend/optimizer/util/clauses.c
src/test/regress/expected/rowsecurity.out
src/test/regress/sql/rowsecurity.sql

index 946e232cca62a6864e31e4d9c7a18e311d75608b..b8e469fff6e49e492293bc851d01230e294831c9 100644 (file)
@@ -5142,6 +5142,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 */
index d72f988cf4099a65e58881a3a2b9c3bf1b95ff66..c37c9b90a68246118446f8146a551b2d2cf14d8e 100644 (file)
@@ -4038,6 +4038,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
 --
index 8845bebe2f2eb593e98885bcc65070c539cbd4a6..6973cc4ed0413c43279a3ea18d53d6f44099f974 100644 (file)
@@ -1873,6 +1873,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
 --