Skip to content
Access control Write-up Advanced

CVE-2020-25695 Privilege Escalation in Postgresql

A privilege escalation vulnerability in PostgreSQL allowed an unprivileged user to execute arbitrary code as a superuser by exploiting a deferred constraint trigger that runs after the security context is restored but before the transaction commits. An attacker could elevate their privileges to superuser when a privileged user or autovacuum runs ANALYZE or VACUUM.

staaldraad.github.io

Published on staaldraad.github.io

It has been quite a year, I hope everyone is well and staying safe. This is my first and probably only post for the year, and covers a fun privilege escalation vulnerability I found in Postgresql.

Etienne Stalmans, on staaldraad.github.io

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The security context (user ID) was restored before the transaction was committed, allowing deferred triggers to execute with the original user's privileges instead of the restricted context.

Impact

An unprivileged user could elevate their privileges to superuser when a privileged user or autovacuum runs ANALYZE or VACUUM, potentially leading to full database compromise.

What the attacker needed

Unprivileged database user; ability to create functions and indexes; privileged user runs ANALYZE or VACUUM; or autovacuum enabled

The fix

The vulnerability was fixed by patches released for all supported versions of PostgreSQL.

How it was found

  1. Create a table and an index using a user-defined function marked IMMUTABLE.
  2. Replace the function with one that performs privileged actions (e.g., inserts current_user into another table) using CREATE OR REPLACE FUNCTION.
  3. Create a constraint trigger that is INITIALLY DEFERRED and fires after insert on the table, executing a trigger function that calls the replaced function.
  4. As a privileged user, run ANALYZE or VACUUM, which causes the index function to run and insert into the table, firing the deferred trigger after the security context is restored.
  5. Observe that the trigger function executes as the privileged user, allowing privilege escalation.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

In PostgreSQL, some maintenance commands like ANALYZE and VACUUM run with higher privileges to do their job. The bug is that the system switched back to the normal user too early, before the command fully finished. This let a low-privileged user sneak in extra actions that then ran with the higher privileges, potentially taking over the whole database.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.