Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dxXhO-0006wT-Iz for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Sep 2017 12:13:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dxXhN-00016X-CU for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Sep 2017 12:13:49 +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 1dxXge-0008H4-0f for pgsql-bugs@postgresql.org; Thu, 28 Sep 2017 12:13:04 +0000 Received: from static-96-255-250-162.washdc.fios.verizon.net ([96.255.250.162] helo=tamriel.snowman.net) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dxXga-0006WU-9O for pgsql-bugs@postgresql.org; Thu, 28 Sep 2017 12:13:02 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id CC0DE5F795; Thu, 28 Sep 2017 08:12:59 -0400 (EDT) Date: Thu, 28 Sep 2017 08:12:59 -0400 From: Stephen Frost To: Ivo Limmen Cc: pgsql-bugs@postgresql.org Subject: Re: Row security policies using session variable can be circumvented Message-ID: <20170928121259.GV4628@tamriel.snowman.net> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="u+t1FGwpC3x/A/b2" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --u+t1FGwpC3x/A/b2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Ivo Limmen (ivo.limmen@qsd.nl) wrote: > CREATE TABLE accounts (user_id integer, manager text, company text, > contact_email text); >=20 > ALTER TABLE accounts ENABLE ROW LEVEL SECURITY; >=20 > CREATE POLICY account_managers ON accounts > USING (user_id is null or user_id =3D current_setting('x.id')::intege= r); >=20 > insert into accounts (user_id, manager, company, contact_email) values (1, > 'jan', 'QSD', 'info@qsd.nl'); > insert into accounts (user_id, manager, company, contact_email) values (2, > 'piet', 'Google', 'info@google.com'); > insert into accounts (user_id, manager, company, contact_email) values > (null, 'piet', 'Microsoft', 'info@microsoft.com'); >=20 > create view test as select * from accounts; Given that you didn't change roles in the above script, one can presume that the view 'test' is owned by the same role which owns 'accounts'. What I don't believe you're remembering is that when a view accesses a relation, it does so with the privileges of the view's *owner*, not of the user who is querying the view. As RLS isn't applied when the owner of a relation is accessing that relation, what you're seeing here is that queries against the view aren't having RLS applied because the 'accounts' relation, when access through the view, is being done so with the privileges of the role that owns the 'test' view. The same is true of the GRANT system. In other words, even without RLS involved, if an account has access to the 'test' view, then they are able to access the rows in the 'accounts' table, even though that role may not have been GRANT'd access to the accounts table. More specifically- > create role tmp; > grant all on accounts to tmp; > grant all on test to tmp; The above 'grant all on accounts to tmp;' isn't necessary for the 'tmp' role to be able to access 'test' and therefore the records in 'accounts', because that access is done as the owner of the 'test' view. > Is this a bug? Or am I doing something wrong? This isn't a bug but simply how views work (and have always worked with the GRANT system and the RLS system simply operates in the same manner). Thanks! Stephen --u+t1FGwpC3x/A/b2 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJZzOdLAAoJEO1sijiDR2RVpGYQAKMXYC3JsgmSxLw48w308bfv twGSbbT8BHFipg0/qhR3aVJkUPhyTXCOrbvY2vxtQ6bVxqeUXyofUTtDoOZKnF4C aVaC+PnyYqiCHSzLWmtLztyMVyCOhXTrBXZVyZbPL0aJA35SZdsbNy0KGADZK79J nCt7fhYuBJarw5onQju3KKvC82ONN9QntkDnVN96cR2YrrH4CiDpkU7aEVlCWthb UTIN23NQFg0/YeMEtYNmKBhsSRITThWU8LT9tqQdN6HBqzthEorbY3EXZZZEWiUG ShYIzsLRRuufrcQI8UJOrXG2ufyJ9hTfxlCULJ08GhjtbIyV9e8qYXZsGrXqqbFW nTnkx13rEHbchqBQtkeuDOpxjN0Mjm6mYVtdsCCFouENLggSmEn5C0FULZhrq0BJ chJAln4veWCaoGdFlZTvYVovKZdyhkB7oYK7HB0kDwKbcn3MJDLbaFht+zmmFQ2x NGF/kXmwZfMTHHqb2q7HxTJOQHnbH8w/nlr9WvSbPdoWhNn+g06BsXTWZLHy5u/7 wvA5JQmx0IB1dCLOUjBxWwNCoZD6KsOgcDgaj9hMt5jWoB9bdN7bF0gE+/ysUT7W vhX/PothoGUu/cGdtWNNKwWcpcMBtwchjpLfMrLUZR7u29J3Is0/BRdjWTxJTiXB b0oOStjfW1+FpPcTUUk4 =fcQ7 -----END PGP SIGNATURE----- --u+t1FGwpC3x/A/b2--