Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dJRpU-0004sG-7z for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jun 2017 21:52:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dJRpT-0004Kj-LS for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jun 2017 21:52:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dJRpS-0004FV-6h; Fri, 09 Jun 2017 21:52:26 +0000 Received: from 104-190-1-44.lightspeed.sndgca.sbcglobal.net ([104.190.1.44] helo=joeconway.com) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dJRpO-0006TU-CC; Fri, 09 Jun 2017 21:52:24 +0000 Received: from [72.214.29.243] (account jconway@joeconway.com HELO [192.168.4.41]) by joeconway.com (CommuniGate Pro SMTP 6.1.4) with ESMTPSA id 18715390; Fri, 09 Jun 2017 14:52:19 -0700 Subject: Re: [BUGS] BUG #14682: row level security not work with partitioned table From: Joe Conway To: Noah Misch Cc: Mike Palmiotto , Michael Paquier , Robert Haas , fte@nct.ru, PostgreSQL mailing lists , Amit Langote , "pgsql-hackers@postgresql.org" References: <20170604223314.GB1547727@rfd.leadboat.com> <665f3a45-46e0-e49e-b4c0-5159785b638a@joeconway.com> <48656ca0-bdad-cd25-b34a-1995367263f3@joeconway.com> <71a9b042-f255-7a45-2872-b6987fe966a2@joeconway.com> <14a1841e-f5d2-1911-36f4-7ebbd388daa6@joeconway.com> <20170609060951.GB1611116@rfd.leadboat.com> <1768fc5f-b186-7c8e-75db-9e113d0ebe3a@joeconway.com> Openpgp: id=4EA5B4659ED59CC9538139A5332B7E68B699434A Message-ID: <9a479aaa-202f-5d8f-fe89-442d80675d97@joeconway.com> Date: Fri, 9 Jun 2017 14:52:17 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <1768fc5f-b186-7c8e-75db-9e113d0ebe3a@joeconway.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="8bCItTIcdDLN61f0kNmj95U1xUH8RQOSv" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --8bCItTIcdDLN61f0kNmj95U1xUH8RQOSv Content-Type: multipart/mixed; boundary="1ofXSkW6gx45doWvKUld4L24nvhsF5OKb"; protected-headers="v1" From: Joe Conway To: Noah Misch Cc: Mike Palmiotto , Michael Paquier , Robert Haas , fte@nct.ru, PostgreSQL mailing lists , Amit Langote , "pgsql-hackers@postgresql.org" Message-ID: <9a479aaa-202f-5d8f-fe89-442d80675d97@joeconway.com> Subject: Re: [BUGS] BUG #14682: row level security not work with partitioned table References: <20170604223314.GB1547727@rfd.leadboat.com> <665f3a45-46e0-e49e-b4c0-5159785b638a@joeconway.com> <48656ca0-bdad-cd25-b34a-1995367263f3@joeconway.com> <71a9b042-f255-7a45-2872-b6987fe966a2@joeconway.com> <14a1841e-f5d2-1911-36f4-7ebbd388daa6@joeconway.com> <20170609060951.GB1611116@rfd.leadboat.com> <1768fc5f-b186-7c8e-75db-9e113d0ebe3a@joeconway.com> In-Reply-To: <1768fc5f-b186-7c8e-75db-9e113d0ebe3a@joeconway.com> --1ofXSkW6gx45doWvKUld4L24nvhsF5OKb Content-Type: multipart/mixed; boundary="------------DC221EFE87921A82CB0DCC01" Content-Language: en-US This is a multi-part message in MIME format. --------------DC221EFE87921A82CB0DCC01 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 06/09/2017 06:16 AM, Joe Conway wrote: > On 06/08/2017 11:09 PM, Noah Misch wrote: >> On Wed, Jun 07, 2017 at 08:45:20AM -0700, Joe Conway wrote: >>> On 06/07/2017 06:49 AM, Mike Palmiotto wrote: >>> > I ended up narrowing it down to 4 tables (one parent and 3 partitio= ns) >>> > in order to demonstrate policy sorting and order of RLS/partition >>> > constraint checking. It should be much more straight-forward now, b= ut >>> > let me know if there are any further recommended changes. >>>=20 >>> Thanks, will take a look towards the end of the day. >>=20 >> This PostgreSQL 10 open item is past due for your status update. Kind= ly send >> a status update within 24 hours, and include a date for your subsequen= t status >> update. Refer to the policy on open item ownership: >> https://www.postgresql.org/message-id/20170404140717.GA2675809%40torna= do.leadboat.com >=20 > I started reviewing the latest patch last night and will try to finish > up this afternoon (west coast USA time). I left the actual (2 line) code change untouched, but I tweaked the regression test changes a bit. If there are no complaints I will push tomorrow (Saturday). Joe --=20 Crunchy Data - http://crunchydata.com PostgreSQL Support for Secure Enterprises Consulting, Training, & Open Source Development --------------DC221EFE87921A82CB0DCC01 Content-Type: text/x-patch; name="add-rls-support-to-partitioned-tables-01.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="add-rls-support-to-partitioned-tables-01.patch" diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/r= ewriteHandler.c index 35ff8bb..6cd73c1 100644 *** a/src/backend/rewrite/rewriteHandler.c --- b/src/backend/rewrite/rewriteHandler.c *************** fireRIRrules(Query *parsetree, List *act *** 1835,1841 **** =20 /* Only normal relations can have RLS policies */ if (rte->rtekind !=3D RTE_RELATION || ! rte->relkind !=3D RELKIND_RELATION) continue; =20 rel =3D heap_open(rte->relid, NoLock); --- 1835,1842 ---- =20 /* Only normal relations can have RLS policies */ if (rte->rtekind !=3D RTE_RELATION || ! (rte->relkind !=3D RELKIND_RELATION && ! rte->relkind !=3D RELKIND_PARTITIONED_TABLE)) continue; =20 rel =3D heap_open(rte->relid, NoLock); diff --git a/src/test/regress/expected/rowsecurity.out b/src/test/regress= /expected/rowsecurity.out index 7bf2936..d382a9f 100644 *** a/src/test/regress/expected/rowsecurity.out --- b/src/test/regress/expected/rowsecurity.out *************** EXPLAIN (COSTS OFF) SELECT * FROM t1 WHE *** 899,904 **** --- 899,1332 ---- Filter: f_leak(b) (7 rows) =20 + -- + -- Partitioned Tables + -- + SET SESSION AUTHORIZATION regress_rls_alice; + CREATE TABLE part_document ( + did int, + cid int, + dlevel int not null, + dauthor name, + dtitle text + ) PARTITION BY RANGE (cid); + GRANT ALL ON part_document TO public; + -- Create partitions for document categories + CREATE TABLE part_document_fiction PARTITION OF part_document FOR VALUE= S FROM (11) to (12); + CREATE TABLE part_document_satire PARTITION OF part_document FOR VALUES= FROM (55) to (56); + CREATE TABLE part_document_nonfiction PARTITION OF part_document FOR VA= LUES FROM (99) to (100); + GRANT ALL ON part_document_fiction TO public; + GRANT ALL ON part_document_satire TO public; + GRANT ALL ON part_document_nonfiction TO public; + INSERT INTO part_document VALUES + ( 1, 11, 1, 'regress_rls_bob', 'my first novel'), + ( 2, 11, 2, 'regress_rls_bob', 'my second novel'), + ( 3, 99, 2, 'regress_rls_bob', 'my science textbook'), + ( 4, 55, 1, 'regress_rls_bob', 'my first satire'), + ( 5, 99, 2, 'regress_rls_bob', 'my history book'), + ( 6, 11, 1, 'regress_rls_carol', 'great science fiction'), + ( 7, 99, 2, 'regress_rls_carol', 'great technology book'), + ( 8, 55, 2, 'regress_rls_carol', 'great satire'), + ( 9, 11, 1, 'regress_rls_dave', 'awesome science fiction'), + (10, 99, 2, 'regress_rls_dave', 'awesome technology book'); + ALTER TABLE part_document ENABLE ROW LEVEL SECURITY; + -- Create policy on parent + -- user's security level must be higher than or equal to document's + CREATE POLICY pp1 ON part_document AS PERMISSIVE + USING (dlevel <=3D (SELECT seclv FROM uaccount WHERE pguser =3D cur= rent_user)); + -- Dave is only allowed to see cid < 55 + CREATE POLICY pp1r ON part_document AS RESTRICTIVE TO regress_rls_dave + USING (cid < 55); + \d+ part_document + Table "regress_rls_schema.part_document" + Column | Type | Collation | Nullable | Default | Storage | Stats = target | Description=20 + ---------+---------+-----------+----------+---------+----------+-------= -------+------------- + did | integer | | | | plain | = |=20 + cid | integer | | | | plain | = |=20 + dlevel | integer | | not null | | plain | = |=20 + dauthor | name | | | | plain | = |=20 + dtitle | text | | | | extended | = |=20 + Partition key: RANGE (cid) + Policies: + POLICY "pp1" + USING ((dlevel <=3D ( SELECT uaccount.seclv + FROM uaccount + WHERE (uaccount.pguser =3D CURRENT_USER)))) + POLICY "pp1r" AS RESTRICTIVE + TO regress_rls_dave + USING ((cid < 55)) + Partitions: part_document_fiction FOR VALUES FROM (11) TO (12), + part_document_nonfiction FOR VALUES FROM (99) TO (100), + part_document_satire FOR VALUES FROM (55) TO (56) +=20 + SELECT * FROM pg_policies WHERE schemaname =3D 'regress_rls_schema' AND= tablename like '%part_document%' ORDER BY policyname; + schemaname | tablename | policyname | permissive | = roles | cmd | qual | with_ch= eck=20 + --------------------+---------------+------------+-------------+-------= -------------+-----+--------------------------------------------+--------= ---- + regress_rls_schema | part_document | pp1 | PERMISSIVE | {publi= c} | ALL | (dlevel <=3D ( SELECT uaccount.seclv +|=20 + | | | | = | | FROM uaccount +|=20 + | | | | = | | WHERE (uaccount.pguser =3D CURRENT_USER))) |=20 + regress_rls_schema | part_document | pp1r | RESTRICTIVE | {regre= ss_rls_dave} | ALL | (cid < 55) |=20 + (2 rows) +=20 + -- viewpoint from regress_rls_bob + SET SESSION AUTHORIZATION regress_rls_bob; + SET row_security TO ON; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + NOTICE: f_leak =3D> my first satire + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 4 | 55 | 1 | regress_rls_bob | my first satire + 6 | 11 | 1 | regress_rls_carol | great science fiction + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + (4 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + ----------------------------------------------------- + Append + InitPlan 1 (returns $0) + -> Index Scan using uaccount_pkey on uaccount + Index Cond: (pguser =3D CURRENT_USER) + -> Seq Scan on part_document_fiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_satire + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_nonfiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + (10 rows) +=20 + -- viewpoint from regress_rls_carol + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + NOTICE: f_leak =3D> my first satire + NOTICE: f_leak =3D> great satire + NOTICE: f_leak =3D> my science textbook + NOTICE: f_leak =3D> my history book + NOTICE: f_leak =3D> great technology book + NOTICE: f_leak =3D> awesome technology book + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + 10 | 99 | 2 | regress_rls_dave | awesome technology book + (10 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + ----------------------------------------------------- + Append + InitPlan 1 (returns $0) + -> Index Scan using uaccount_pkey on uaccount + Index Cond: (pguser =3D CURRENT_USER) + -> Seq Scan on part_document_fiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_satire + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_nonfiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + (10 rows) +=20 + -- viewpoint from regress_rls_dave + SET SESSION AUTHORIZATION regress_rls_dave; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 6 | 11 | 1 | regress_rls_carol | great science fiction + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + (4 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + -------------------------------------------------------------------- + Append + InitPlan 1 (returns $0) + -> Index Scan using uaccount_pkey on uaccount + Index Cond: (pguser =3D CURRENT_USER) + -> Seq Scan on part_document_fiction + Filter: ((cid < 55) AND (dlevel <=3D $0) AND f_leak(dtitle)) + (6 rows) +=20 + -- pp1 ERROR + INSERT INTO part_document VALUES (100, 11, 5, 'regress_rls_dave', 'test= ing pp1'); -- fail + ERROR: new row violates row-level security policy for table "part_docu= ment" + -- pp1r ERROR + INSERT INTO part_document VALUES (100, 99, 1, 'regress_rls_dave', 'test= ing pp1r'); -- fail + ERROR: new row violates row-level security policy "pp1r" for table "pa= rt_document" + -- Show that RLS policy does not apply for direct inserts to children + -- This should fail with RLS POLICY pp1r violation. + INSERT INTO part_document VALUES (100, 55, 1, 'regress_rls_dave', 'test= ing RLS with partitions'); -- fail + ERROR: new row violates row-level security policy "pp1r" for table "pa= rt_document" + -- But this should succeed. + INSERT INTO part_document_satire VALUES (100, 55, 1, 'regress_rls_dave'= , 'testing RLS with partitions'); -- success + -- We still cannot see the row using the parent + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 6 | 11 | 1 | regress_rls_carol | great science fiction + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + (4 rows) +=20 + -- But we can if we look directly + SELECT * FROM part_document_satire WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first satire + NOTICE: f_leak =3D> great satire + NOTICE: f_leak =3D> testing RLS with partitions + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 4 | 55 | 1 | regress_rls_bob | my first satire + 8 | 55 | 2 | regress_rls_carol | great satire + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (3 rows) +=20 + -- Turn on RLS and create policy on child to show RLS is checked before= constraints + SET SESSION AUTHORIZATION regress_rls_alice; + ALTER TABLE part_document_satire ENABLE ROW LEVEL SECURITY; + CREATE POLICY pp3 ON part_document_satire AS RESTRICTIVE + USING (cid < 55); + -- This should fail with RLS violation now. + SET SESSION AUTHORIZATION regress_rls_dave; + INSERT INTO part_document_satire VALUES (101, 55, 1, 'regress_rls_dave'= , 'testing RLS with partitions'); -- fail + ERROR: new row violates row-level security policy for table "part_docu= ment_satire" + -- And now we cannot see directly into the partition either, due to RLS= + SELECT * FROM part_document_satire WHERE f_leak(dtitle) ORDER BY did; + did | cid | dlevel | dauthor | dtitle=20 + -----+-----+--------+---------+-------- + (0 rows) +=20 + -- The parent looks same as before + -- viewpoint from regress_rls_dave + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 6 | 11 | 1 | regress_rls_carol | great science fiction + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + (4 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + -------------------------------------------------------------------- + Append + InitPlan 1 (returns $0) + -> Index Scan using uaccount_pkey on uaccount + Index Cond: (pguser =3D CURRENT_USER) + -> Seq Scan on part_document_fiction + Filter: ((cid < 55) AND (dlevel <=3D $0) AND f_leak(dtitle)) + (6 rows) +=20 + -- viewpoint from regress_rls_carol + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> awesome science fiction + NOTICE: f_leak =3D> my first satire + NOTICE: f_leak =3D> great satire + NOTICE: f_leak =3D> testing RLS with partitions + NOTICE: f_leak =3D> my science textbook + NOTICE: f_leak =3D> my history book + NOTICE: f_leak =3D> great technology book + NOTICE: f_leak =3D> awesome technology book + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + 10 | 99 | 2 | regress_rls_dave | awesome technology book + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (11 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + ----------------------------------------------------- + Append + InitPlan 1 (returns $0) + -> Index Scan using uaccount_pkey on uaccount + Index Cond: (pguser =3D CURRENT_USER) + -> Seq Scan on part_document_fiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_satire + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + -> Seq Scan on part_document_nonfiction + Filter: ((dlevel <=3D $0) AND f_leak(dtitle)) + (10 rows) +=20 + -- only owner can change policies + ALTER POLICY pp1 ON part_document USING (true); --fail + ERROR: must be owner of relation part_document + DROP POLICY pp1 ON part_document; --fail + ERROR: must be owner of relation part_document + SET SESSION AUTHORIZATION regress_rls_alice; + ALTER POLICY pp1 ON part_document USING (dauthor =3D current_user); + -- viewpoint from regress_rls_bob again + SET SESSION AUTHORIZATION regress_rls_bob; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> my first novel + NOTICE: f_leak =3D> my second novel + NOTICE: f_leak =3D> my first satire + NOTICE: f_leak =3D> my science textbook + NOTICE: f_leak =3D> my history book + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-----------------+--------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + (5 rows) +=20 + -- viewpoint from rls_regres_carol again + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + NOTICE: f_leak =3D> great science fiction + NOTICE: f_leak =3D> great satire + NOTICE: f_leak =3D> great technology book + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------- + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + (3 rows) +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); + QUERY PLAN =20 + --------------------------------------------------------------- + Append + -> Seq Scan on part_document_fiction + Filter: ((dauthor =3D CURRENT_USER) AND f_leak(dtitle)) + -> Seq Scan on part_document_satire + Filter: ((dauthor =3D CURRENT_USER) AND f_leak(dtitle)) + -> Seq Scan on part_document_nonfiction + Filter: ((dauthor =3D CURRENT_USER) AND f_leak(dtitle)) + (7 rows) +=20 + -- database superuser does bypass RLS policy when enabled + RESET SESSION AUTHORIZATION; + SET row_security TO ON; + SELECT * FROM part_document ORDER BY did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + 10 | 99 | 2 | regress_rls_dave | awesome technology book + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (11 rows) +=20 + SELECT * FROM part_document_satire ORDER by did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 4 | 55 | 1 | regress_rls_bob | my first satire + 8 | 55 | 2 | regress_rls_carol | great satire + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (3 rows) +=20 + -- database non-superuser with bypass privilege can bypass RLS policy w= hen disabled + SET SESSION AUTHORIZATION regress_rls_exempt_user; + SET row_security TO OFF; + SELECT * FROM part_document ORDER BY did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + 10 | 99 | 2 | regress_rls_dave | awesome technology book + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (11 rows) +=20 + SELECT * FROM part_document_satire ORDER by did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 4 | 55 | 1 | regress_rls_bob | my first satire + 8 | 55 | 2 | regress_rls_carol | great satire + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (3 rows) +=20 + -- RLS policy does not apply to table owner when RLS enabled. + SET SESSION AUTHORIZATION regress_rls_alice; + SET row_security TO ON; + SELECT * FROM part_document ORDER by did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 1 | 11 | 1 | regress_rls_bob | my first novel + 2 | 11 | 2 | regress_rls_bob | my second novel + 3 | 99 | 2 | regress_rls_bob | my science textbook + 4 | 55 | 1 | regress_rls_bob | my first satire + 5 | 99 | 2 | regress_rls_bob | my history book + 6 | 11 | 1 | regress_rls_carol | great science fiction + 7 | 99 | 2 | regress_rls_carol | great technology book + 8 | 55 | 2 | regress_rls_carol | great satire + 9 | 11 | 1 | regress_rls_dave | awesome science fiction + 10 | 99 | 2 | regress_rls_dave | awesome technology book + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (11 rows) +=20 + SELECT * FROM part_document_satire ORDER by did; + did | cid | dlevel | dauthor | dtitle =20 + -----+-----+--------+-------------------+----------------------------- + 4 | 55 | 1 | regress_rls_bob | my first satire + 8 | 55 | 2 | regress_rls_carol | great satire + 100 | 55 | 1 | regress_rls_dave | testing RLS with partitions + (3 rows) +=20 + -- When RLS disabled, other users get ERROR. + SET SESSION AUTHORIZATION regress_rls_dave; + SET row_security TO OFF; + SELECT * FROM part_document ORDER by did; + ERROR: query would be affected by row-level security policy for table = "part_document" + SELECT * FROM part_document_satire ORDER by did; + ERROR: query would be affected by row-level security policy for table = "part_document_satire" ----- Dependencies ----- SET SESSION AUTHORIZATION regress_rls_alice; SET row_security TO ON; diff --git a/src/test/regress/sql/rowsecurity.sql b/src/test/regress/sql/= rowsecurity.sql index 1b6896e..80537ff 100644 *** a/src/test/regress/sql/rowsecurity.sql --- b/src/test/regress/sql/rowsecurity.sql *************** SET row_security TO OFF; *** 308,313 **** --- 308,455 ---- SELECT * FROM t1 WHERE f_leak(b); EXPLAIN (COSTS OFF) SELECT * FROM t1 WHERE f_leak(b); =20 + -- + -- Partitioned Tables + -- +=20 + SET SESSION AUTHORIZATION regress_rls_alice; +=20 + CREATE TABLE part_document ( + did int, + cid int, + dlevel int not null, + dauthor name, + dtitle text + ) PARTITION BY RANGE (cid); + GRANT ALL ON part_document TO public; +=20 + -- Create partitions for document categories + CREATE TABLE part_document_fiction PARTITION OF part_document FOR VALUE= S FROM (11) to (12); + CREATE TABLE part_document_satire PARTITION OF part_document FOR VALUES= FROM (55) to (56); + CREATE TABLE part_document_nonfiction PARTITION OF part_document FOR VA= LUES FROM (99) to (100); +=20 + GRANT ALL ON part_document_fiction TO public; + GRANT ALL ON part_document_satire TO public; + GRANT ALL ON part_document_nonfiction TO public; +=20 + INSERT INTO part_document VALUES + ( 1, 11, 1, 'regress_rls_bob', 'my first novel'), + ( 2, 11, 2, 'regress_rls_bob', 'my second novel'), + ( 3, 99, 2, 'regress_rls_bob', 'my science textbook'), + ( 4, 55, 1, 'regress_rls_bob', 'my first satire'), + ( 5, 99, 2, 'regress_rls_bob', 'my history book'), + ( 6, 11, 1, 'regress_rls_carol', 'great science fiction'), + ( 7, 99, 2, 'regress_rls_carol', 'great technology book'), + ( 8, 55, 2, 'regress_rls_carol', 'great satire'), + ( 9, 11, 1, 'regress_rls_dave', 'awesome science fiction'), + (10, 99, 2, 'regress_rls_dave', 'awesome technology book'); +=20 + ALTER TABLE part_document ENABLE ROW LEVEL SECURITY; +=20 + -- Create policy on parent + -- user's security level must be higher than or equal to document's + CREATE POLICY pp1 ON part_document AS PERMISSIVE + USING (dlevel <=3D (SELECT seclv FROM uaccount WHERE pguser =3D cur= rent_user)); +=20 + -- Dave is only allowed to see cid < 55 + CREATE POLICY pp1r ON part_document AS RESTRICTIVE TO regress_rls_dave + USING (cid < 55); +=20 + \d+ part_document + SELECT * FROM pg_policies WHERE schemaname =3D 'regress_rls_schema' AND= tablename like '%part_document%' ORDER BY policyname; +=20 + -- viewpoint from regress_rls_bob + SET SESSION AUTHORIZATION regress_rls_bob; + SET row_security TO ON; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- viewpoint from regress_rls_carol + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- viewpoint from regress_rls_dave + SET SESSION AUTHORIZATION regress_rls_dave; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- pp1 ERROR + INSERT INTO part_document VALUES (100, 11, 5, 'regress_rls_dave', 'test= ing pp1'); -- fail + -- pp1r ERROR + INSERT INTO part_document VALUES (100, 99, 1, 'regress_rls_dave', 'test= ing pp1r'); -- fail +=20 + -- Show that RLS policy does not apply for direct inserts to children + -- This should fail with RLS POLICY pp1r violation. + INSERT INTO part_document VALUES (100, 55, 1, 'regress_rls_dave', 'test= ing RLS with partitions'); -- fail + -- But this should succeed. + INSERT INTO part_document_satire VALUES (100, 55, 1, 'regress_rls_dave'= , 'testing RLS with partitions'); -- success + -- We still cannot see the row using the parent + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + -- But we can if we look directly + SELECT * FROM part_document_satire WHERE f_leak(dtitle) ORDER BY did; +=20 + -- Turn on RLS and create policy on child to show RLS is checked before= constraints + SET SESSION AUTHORIZATION regress_rls_alice; + ALTER TABLE part_document_satire ENABLE ROW LEVEL SECURITY; + CREATE POLICY pp3 ON part_document_satire AS RESTRICTIVE + USING (cid < 55); + -- This should fail with RLS violation now. + SET SESSION AUTHORIZATION regress_rls_dave; + INSERT INTO part_document_satire VALUES (101, 55, 1, 'regress_rls_dave'= , 'testing RLS with partitions'); -- fail + -- And now we cannot see directly into the partition either, due to RLS= + SELECT * FROM part_document_satire WHERE f_leak(dtitle) ORDER BY did; + -- The parent looks same as before + -- viewpoint from regress_rls_dave + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- viewpoint from regress_rls_carol + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- only owner can change policies + ALTER POLICY pp1 ON part_document USING (true); --fail + DROP POLICY pp1 ON part_document; --fail +=20 + SET SESSION AUTHORIZATION regress_rls_alice; + ALTER POLICY pp1 ON part_document USING (dauthor =3D current_user); +=20 + -- viewpoint from regress_rls_bob again + SET SESSION AUTHORIZATION regress_rls_bob; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; +=20 + -- viewpoint from rls_regres_carol again + SET SESSION AUTHORIZATION regress_rls_carol; + SELECT * FROM part_document WHERE f_leak(dtitle) ORDER BY did; +=20 + EXPLAIN (COSTS OFF) SELECT * FROM part_document WHERE f_leak(dtitle); +=20 + -- database superuser does bypass RLS policy when enabled + RESET SESSION AUTHORIZATION; + SET row_security TO ON; + SELECT * FROM part_document ORDER BY did; + SELECT * FROM part_document_satire ORDER by did; +=20 + -- database non-superuser with bypass privilege can bypass RLS policy w= hen disabled + SET SESSION AUTHORIZATION regress_rls_exempt_user; + SET row_security TO OFF; + SELECT * FROM part_document ORDER BY did; + SELECT * FROM part_document_satire ORDER by did; +=20 + -- RLS policy does not apply to table owner when RLS enabled. + SET SESSION AUTHORIZATION regress_rls_alice; + SET row_security TO ON; + SELECT * FROM part_document ORDER by did; + SELECT * FROM part_document_satire ORDER by did; +=20 + -- When RLS disabled, other users get ERROR. + SET SESSION AUTHORIZATION regress_rls_dave; + SET row_security TO OFF; + SELECT * FROM part_document ORDER by did; + SELECT * FROM part_document_satire ORDER by did; +=20 ----- Dependencies ----- SET SESSION AUTHORIZATION regress_rls_alice; SET row_security TO ON; --------------DC221EFE87921A82CB0DCC01-- --1ofXSkW6gx45doWvKUld4L24nvhsF5OKb-- --8bCItTIcdDLN61f0kNmj95U1xUH8RQOSv Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJZOxiSAAoJEDfy90M199hly8UP/jiyXgoRxloDPUIKFcc0P6wU j/m+LvBisCulN9s2rFkR+iHsxhWf0fQ+vQUOf9HnnrJZ1L6lPxJSz7GViI5PdRsV ovlMG4wsq3LOftKTQnAXrdD+JBMIwMiSz267g5/yyLYOJYb3+OSG0IF3pImutpis H51GOB5ClOzjwWVfyjc6N1Rhxv8N5PoPTXqfIWHqmHeWesRve+z6yzW5cjEeoqd8 ZyJKZUHTebTIkRvZPA/hFJ+RqmPtAd5HPjvaiZ+kfjdSB1bfeltlBYE5GczObgu5 uAHQNC5xDFVv4Ab93JgHD2UrQ859q+vvMVssjr8J9N6K6CPiGHtlG33DQooMFdh+ w2WGsvfhbU7QUStP8eQ8Ew9Zw4eO88cCPKQjEpRGCBhW2Mxe41OKm9nqScSZ8y1Y saosQiaAg1PeQqK3dfOv/Qwl0XnxDRkFWwD+UM1zx0l9c1fnvXKLWfKQE7FIv+BK Sa9C+4DxQcTs1oz4exFDKUj8ZHNoHSGYsQLwCZ1feCIip5R4QPI0JvsQev/eK1NI ivbBXjvs/TZ1QbFsPY3wJ/Ls+YHl6rzcQ77am4JJELd7IRoWzvn190BOT7DzwkCK CCn5MZeBu2QtRMXpdDlSle+TXKGLKHhY6nrowKcrQMt2+lgBpFd0KmD8iCxIS3jM JkkRimR1TWhSPf1X93F8 =Ek2W -----END PGP SIGNATURE----- --8bCItTIcdDLN61f0kNmj95U1xUH8RQOSv--