News for hackers. Stuff that matters.
Re:Row Level Security with PostgreSQL 9.5 by dsp1234 on Wednesday April 20, 2016 @04:18PM (#11537412)
This is great for systems like ours that have thousands of 'users' and setting up Roles is a pain.

I do see an issue in that if an sql injection is found, then it's trivial for the attacker to use set_foo or set session themselves.

Do you know if it is possible to run to get the system to a point where the initial connection role doesn't have permission to 'set session' itself, but does have permission to run set_foo. Where set_foo can set the session, then set a role that does not have access to execute set_foo again.

Said differently, could this be adapted so that:

1.) at the beginning of a connection, the session is unset

2.) the only way to set the session is via function

3.) once the function has been called once, it cannot be called again on the same connection

Re:Row Level Security with PostgreSQL 9.5 by Jweb_Guru on Wednesday April 20, 2016 @04:51PM (#11537658)
I'm pretty sure it's not possible to prevent a role from setting session parameters.

I believe it is not possible to SET ROLE or SET SESSION AUTHORIZATION with code executed within a SECURITY DEFINER function (which is what you're asking to do), though as Tom Lane points out one shouldn't rely on that:

http://www.postgresql.org/message-id/[email protected]...

Given arbitrary SQLi, it's hard to see how one can do better than setting up an untrusted sandbox like that and executing your untrusted SQL there, but being able to prevent setting a session parameter would still be useful within that context.

Re:Row Level Security with PostgreSQL 9.5 by dsp1234 on Wednesday April 20, 2016 @05:12PM (#11537800)
I was hoping for something like mssql's "EXECUTE AS... NO REVERT" where reverting back to the previous role can be prevented.
Re:Row Level Security with PostgreSQL 9.5 by haberman on Wednesday April 20, 2016 @07:41PM (#11538570)
Or in UNIX terms, this is simply "sudo -u". Seems like something that would be pretty useful.
Re:Row Level Security with PostgreSQL 9.5 by badalex on Wednesday April 20, 2016 @05:15PM (#11537814)
Sure, use a language that has access to some kind of shared global variable. For example %_SHARED in plperl:

Something like:

  CREATE OR REPLACE FUNCTION set_foo(name text) returns void as   $$
        my $name = shift;
        die "set_foo() has already been called" if   ($_SHARED{'set_foo'});
        $_SHARED{'set_foo'} = $name;
  $$
  LANGUAGE plperl;


  CREATE OR REPLACE FUNCTION get_foo() returns text as $$
        my $name = shift;
        return $_SHARED{'set_foo'} || 'nobody';
  $$
It would be fairly trivial to extend that to support calling set_foo() once per transaction by checking against txid_current(). But if users can create plperl functions and the worry is sql injection-- It should be easy to write the same thing in C.