>What is the recommended lock-ordering strategy when authorization depends
on session, membership, role, and tenant records?

Not sure what is being asked here?  I give it a try

Unless there is a specific need for locking records for business needs such
as updating inventory levels, auditing, or accounting records where the
order of operations is critical, avoid manual locking.

There really is no reason to be locking records or objects based on
authorization. Checking permissions should be read-only events. Once access
has been granted via role membership or RLS, then we can decide if a row or
object needs to be locked. Locking rows and objects should not be a factor
when checking permissions.

>Is READ COMMITTED with explicit row locks and post-lock authority
revalidation an appropriate approach?

PG by default operates in read committed. PG cannot operate in a lower
transaction isolation level (read uncommitted).

Use of explicit locking is highly discouraged. This approach will introduce
a ton of problems for you as the number of concurrent users increases. It
looks like you are trying to implement pessimistic locking instead of using
optimistic locking. PG’s concurrency model is optimistic locking, not
pessimistic. You can use pessimistic locking in PG, but it is going to
create future problems with deadlocks, blocking sessions via conflicting
locks, and race conditions. Just let PG handle the locking except for
corner-case operations where the order of operations will affect the
results.

Trying to re-validate credentials post-locking mid-transaction is going to
add a lot of overhead and complexity. If the application is written
correctly, the exclusive locks are only held for tiny fractions of seconds.
Holding exclusive locks for a long period of time where the need to
revalidate credentials is necessary is asking for problems.

Consider a user logs in on their computer. The computer automatically locks
the computer at 15 minutes of inactivity and logs them out after 8 hours.
The user starts working and acquires a lock on a record 20 minutes before
they go home. The user goes home and leaves the computer on. The computer
locks and the system logs them out after 8 hours. What about the session on
PG? We have a transaction that has been active for 20+ minutes. This is
going to create one big mess, block automatic maintenance (vacuum), and
block readers and writers, etc.

>What is the recommended least-privilege ownership and permission model for
SECURITY DEFINER functions that require row-level locking?

Typically the security definer, tables, and functions are owned by the same
ROLE: a non-superuser that cannot log in.

>How should concurrent authorization changes, deadlocks, and transaction
retries be handled without weakening security?

Not completely sure what you are talking about here.

If you are talking about permission changes mid-transaction, this is going
to be a problem, as parts of it are going to be checked while other parts
will not be checked.

Example RLS check:

1. Start a transaction
2. SELECT * FROM table A WHERE col1 = 'something';
3. RLS grants
4. Update rows in table B based on table A results
5. RLS grants
6. Security access revoked for session
7. Update rows in table C based on table B and A results
8. RLS denies access
9. Transaction is rolled back due to access deny error.

However, if RLS is only checked on table A and not on table B and C, we
assume table A access grant gave access to table B and C, and the
transaction would be committed.

There are other additional gotchas here to consider when dealing with
in-flight transactions being able to see recent permission changes that
would revoke or grant access to tables, rows, columns, and/or functions in
the database.  This gets even more complicated if explicit locking is being
used because  security changes could be blocked by readers.

Transaction retries are handled on a case-by-case basis based on the
business logic requirements. Do we update the inventory count when a serial
isolation error has been thrown? From a security standpoint:

If the users had authority to start the transaction and authorization was
removed mid-transaction, PG will allow the transaction to progress unless
the business logic checks access just prior to the commit. The typical
security logic is that permissions are checked prior to executing a
statement. Once permission is granted, it is assumed to be valid until the
function is completed.

Changing permissions mid-transaction and expecting the change to be applied
and take effect is going to have issues.

> Overall goal (PostgreSQL-supported security practices without excessive
privileges)

PostgreSQL does not publish a single comprehensive security standard beyond
the basic principle of minimizing superuser access and using least
privilege. Most organizations apply external standards such as STIG,
FedRAMP, CIS benchmarks, PCI, banking standards, etc.

>

Reply via email to