Re: AW: Reimplementing permission checks for rules - Mailing list pgsql-hackers

From Tom Lane
Subject Re: AW: Reimplementing permission checks for rules
Date
Msg-id 23885.970250106@sss.pgh.pa.us
Whole thread Raw
In response to AW: Reimplementing permission checks for rules  (Zeugswetter Andreas SB <ZeugswetterA@wien.spardat.at>)
List pgsql-hackers
Zeugswetter Andreas SB <ZeugswetterA@wien.spardat.at> writes:
>> What I'm thinking about doing is eliminating the "skipAcl" RTE field
>> and instead adding an Oid field named something like "checkAclAs".
>> 
>> Comments?  Is this a general enough mechanism, and does it fit well
>> with the various setUID tricks that people are thinking about?

> Sounds good, and a step in the right direction.

After looking at the rule rewriter some more, I realized that the only
way to push all permissions checks to execution time is not only to keep
skipAcl, but to generalize it.  The problem is with checks on the view
itself --- if you do INSERT INTO someView, which gets rewritten into
an insert to someRealTable, then what you want the executor to check for
isWrite access on someView by current userWrite access on someRealTable by owner of rule
which is infeasible with the existing code because the executor only
checks for write access on the real target table (someRealTable).

What I have now got, and hope to commit today, is the following fields
in RangeTblEntry, replacing skipAcl:
*      checkForRead, checkForWrite, and checkAsUser control run-time access*      permissions checks.  A rel will be
checkedfor read or write access*      (or both, or neither) per checkForRead and checkForWrite.  If*      checkAsUser
isnot InvalidOid, then do the permissions checks using*      the access rights of that user, not the current effective
userID.*      (This allows rules to act as setuid gateways.)
 
   bool        checkForRead;    /* check rel for read access */   bool        checkForWrite;   /* check rel for write
access*/   Oid         checkAsUser;     /* if not zero, check access as this user */
 

The parser always sets checkAsUser = 0; it sets checkForRead if the rel
is referenced explicitly anywhere in the query; and it sets
checkForWrite on the target table of INSERT/UPDATE/DELETE.  It was a
little tricky to get it to do the right thing when the same table is
both a source and target, eg in INSERT ... SELECT, but not too bad.

When a rule is created, the stored parsetree has the rule creator's OID
assigned into each RTE's checkAsUser field, except for the dummy *NEW*
and *OLD* rtable entries.

The rewriter leaves the check fields on a view's RTE as they are, and
just copies the check fields from the rule for the rtable entries it
adds.  No rewrite-time permission checks anywhere.

The executor just carries out the indicated checks.  execMain's checking
got a *lot* simpler, too, because it doesn't have to carry around a lot
of baggage to determine whether to check a given RTE for read, write,
or both.

Seems like a big win ...

NOTE: there is a subtle change here.  A rule used to be taken as
executing setUID to the owner of the table the rule is attached to.
Now it is executed as if setUID to the person who created the rule,
who could be a different user if the table owner gave away RULE rights.
I think this is a more correct behavior, but I'm open to argument.
It'd be easy to make CREATE RULE store the table owner's OID instead,
if anyone wants to argue that that was the right thing.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Lamar Owen
Date:
Subject: Re: New unified regression test driver
Next
From: Tom Lane
Date:
Subject: Re: Database log