Skip to content

Commit 866fd00

Browse files
committed
Ensure COPY TO on an RLS-enabled table copies no more than it should.
The COPY documentation is quite clear that "COPY relation TO" copies rows from only the named table, not any inheritance children it may have. However, if you enabled row-level security on the table then this stopped being true, because the code forgot to apply the ONLY modifier in the "SELECT ... FROM relation" query that it constructs in order to allow RLS predicates to be attached. Fix that. Report and patch by Antonin Houska (comment adjustments and test case by me). Back-patch to all supported branches. Discussion: https://postgr.es/m/3472.1675251957@antos
1 parent ae632f7 commit 866fd00

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

src/backend/commands/copy.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,11 +1019,14 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
10191019

10201020
/*
10211021
* Build RangeVar for from clause, fully qualified based on the
1022-
* relation which we have opened and locked.
1022+
* relation which we have opened and locked. Use "ONLY" so that
1023+
* COPY retrieves rows from only the target table not any
1024+
* inheritance children, the same as when RLS doesn't apply.
10231025
*/
10241026
from = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
10251027
pstrdup(RelationGetRelationName(rel)),
10261028
-1);
1029+
from->inh = false; /* apply ONLY */
10271030

10281031
/* Build query */
10291032
select = makeNode(SelectStmt);
@@ -1581,8 +1584,8 @@ BeginCopy(ParseState *pstate,
15811584
/*
15821585
* With row level security and a user using "COPY relation TO", we
15831586
* have to convert the "COPY relation TO" to a query-based COPY (eg:
1584-
* "COPY (SELECT * FROM relation) TO"), to allow the rewriter to add
1585-
* in any RLS clauses.
1587+
* "COPY (SELECT * FROM ONLY relation) TO"), to allow the rewriter to
1588+
* add in any RLS clauses.
15861589
*
15871590
* When this happens, we are passed in the relid of the originally
15881591
* found relation (which we have locked). As the planner will look up

src/test/regress/expected/rowsecurity.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,6 +3274,42 @@ ERROR: permission denied for table copy_rel_to
32743274
SET row_security TO ON;
32753275
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
32763276
ERROR: permission denied for table copy_rel_to
3277+
-- Check behavior with a child table.
3278+
RESET SESSION AUTHORIZATION;
3279+
SET row_security TO ON;
3280+
CREATE TABLE copy_rel_to_child () INHERITS (copy_rel_to);
3281+
INSERT INTO copy_rel_to_child VALUES (1, 'one'), (2, 'two');
3282+
-- Check COPY TO as Superuser/owner.
3283+
RESET SESSION AUTHORIZATION;
3284+
SET row_security TO OFF;
3285+
COPY copy_rel_to TO STDOUT WITH DELIMITER ',';
3286+
1,c4ca4238a0b923820dcc509a6f75849b
3287+
SET row_security TO ON;
3288+
COPY copy_rel_to TO STDOUT WITH DELIMITER ',';
3289+
1,c4ca4238a0b923820dcc509a6f75849b
3290+
-- Check COPY TO as user with permissions.
3291+
SET SESSION AUTHORIZATION regress_rls_bob;
3292+
SET row_security TO OFF;
3293+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - would be affected by RLS
3294+
ERROR: query would be affected by row-level security policy for table "copy_rel_to"
3295+
SET row_security TO ON;
3296+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
3297+
-- Check COPY TO as user with permissions and BYPASSRLS
3298+
SET SESSION AUTHORIZATION regress_rls_exempt_user;
3299+
SET row_security TO OFF;
3300+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
3301+
1,c4ca4238a0b923820dcc509a6f75849b
3302+
SET row_security TO ON;
3303+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
3304+
1,c4ca4238a0b923820dcc509a6f75849b
3305+
-- Check COPY TO as user without permissions. SET row_security TO OFF;
3306+
SET SESSION AUTHORIZATION regress_rls_carol;
3307+
SET row_security TO OFF;
3308+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
3309+
ERROR: permission denied for table copy_rel_to
3310+
SET row_security TO ON;
3311+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
3312+
ERROR: permission denied for table copy_rel_to
32773313
-- Check COPY FROM as Superuser/owner.
32783314
RESET SESSION AUTHORIZATION;
32793315
SET row_security TO OFF;
@@ -3304,6 +3340,7 @@ ERROR: permission denied for table copy_t
33043340
RESET SESSION AUTHORIZATION;
33053341
DROP TABLE copy_t;
33063342
DROP TABLE copy_rel_to CASCADE;
3343+
NOTICE: drop cascades to table copy_rel_to_child
33073344
-- Check WHERE CURRENT OF
33083345
SET SESSION AUTHORIZATION regress_rls_alice;
33093346
CREATE TABLE current_check (currentid int, payload text, rlsuser text);

src/test/regress/sql/rowsecurity.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,40 @@ COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
12651265
SET row_security TO ON;
12661266
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
12671267

1268+
-- Check behavior with a child table.
1269+
RESET SESSION AUTHORIZATION;
1270+
SET row_security TO ON;
1271+
CREATE TABLE copy_rel_to_child () INHERITS (copy_rel_to);
1272+
INSERT INTO copy_rel_to_child VALUES (1, 'one'), (2, 'two');
1273+
1274+
-- Check COPY TO as Superuser/owner.
1275+
RESET SESSION AUTHORIZATION;
1276+
SET row_security TO OFF;
1277+
COPY copy_rel_to TO STDOUT WITH DELIMITER ',';
1278+
SET row_security TO ON;
1279+
COPY copy_rel_to TO STDOUT WITH DELIMITER ',';
1280+
1281+
-- Check COPY TO as user with permissions.
1282+
SET SESSION AUTHORIZATION regress_rls_bob;
1283+
SET row_security TO OFF;
1284+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - would be affected by RLS
1285+
SET row_security TO ON;
1286+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
1287+
1288+
-- Check COPY TO as user with permissions and BYPASSRLS
1289+
SET SESSION AUTHORIZATION regress_rls_exempt_user;
1290+
SET row_security TO OFF;
1291+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
1292+
SET row_security TO ON;
1293+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --ok
1294+
1295+
-- Check COPY TO as user without permissions. SET row_security TO OFF;
1296+
SET SESSION AUTHORIZATION regress_rls_carol;
1297+
SET row_security TO OFF;
1298+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
1299+
SET row_security TO ON;
1300+
COPY copy_rel_to TO STDOUT WITH DELIMITER ','; --fail - permission denied
1301+
12681302
-- Check COPY FROM as Superuser/owner.
12691303
RESET SESSION AUTHORIZATION;
12701304
SET row_security TO OFF;

0 commit comments

Comments
 (0)