PoC Watch
PostgreSQL code execution as the database OS user
PoC publicHigh
A researcher published proofs of concept for code execution flaws in several database engines. In PostgreSQL, an account that is allowed to create database objects can run code as the operating system user that runs the database. A fix shipped in a recent PostgreSQL security release. Reporting at the time of publication said none of the exploits had been seen in real attacks.
Mitigation by layer
Patch
- Apply the current PostgreSQL minor release for your major version, on primaries and replicas.
Step by step
- Check the running version on every instance, including replicas.
psql -X -At -c 'SHOW server_version;'Expected output
<major>.<minor> Compare with the fixed minor release for your major version in the PostgreSQL security advisory.
- Install the updated packages from your normal package source. Minor releases do not need a dump and restore.
- Restart the service so the new binaries are running, then re-check the version. The service name varies by distribution.
sudo systemctl restart postgresql && psql -X -At -c 'SHOW server_version;'
- Check the running version on every instance, including replicas.
- For managed databases, confirm with the provider that the fix has been applied and whether a restart is pending.
Reduce exposure
- Keep database ports off the internet and reachable only from the application tier.
Step by step
- Check which addresses the server listens on.
psql -X -At -c 'SHOW listen_addresses;'Expected output
A specific internal address or localhost. A value of * means all interfaces and needs a firewall in front of it.
- Check which addresses the server listens on.
- Review pg_hba.conf and remove broad network ranges and unused entries.
Step by step
- List the active client authentication rules.
SELECT line_number, type, database, user_name, address, netmask, auth_method FROM pg_hba_file_rules ORDER BY line_number; - Look for address 0.0.0.0/0 or ::/0, and for the trust method. Tighten or remove those lines in pg_hba.conf, then reload.
SELECT pg_reload_conf();
- List the active client authentication rules.
Harden access
- Audit which roles hold CREATE on schemas and databases and revoke it where it is not needed. Application accounts rarely need it at runtime.
Step by step
- List non-superuser roles that can create objects in each schema. Run this in every database.
SELECT n.nspname AS schema, r.rolname AS role FROM pg_namespace n CROSS JOIN pg_roles r WHERE has_schema_privilege(r.oid, n.oid, 'CREATE') AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' AND NOT r.rolsuper ORDER BY 1, 2; - Remove the default that lets every role create objects in the public schema (already the default from PostgreSQL 15 on new databases).
REVOKE CREATE ON SCHEMA public FROM PUBLIC; - Revoke from specific application roles that do not need it. Test in a non-production environment first, since migrations may rely on it.
REVOKE CREATE ON SCHEMA <schema> FROM <app_role>; REVOKE CREATE ON DATABASE <database> FROM <app_role>;
- List non-superuser roles that can create objects in each schema. Run this in every database.
- Run the database under a dedicated low-privilege OS account with no sudo rights and no access to other services' credentials.
Detect
- Log DDL statements and review for unexpected object creation by application or service accounts.
Step by step
- Enable DDL logging and reload the configuration.
ALTER SYSTEM SET log_statement = 'ddl'; SELECT pg_reload_conf(); SHOW log_statement;Expected output
ddl
- Forward the PostgreSQL log to your SIEM and alert on CREATE FUNCTION, CREATE EXTENSION and similar statements from application roles.
- Enable DDL logging and reload the configuration.
- Alert on the database OS user spawning child processes or opening outbound network connections.
Contain and recover
- Verify that backups are stored where the database OS user cannot modify or delete them.
- If misuse is suspected, rotate database and replication credentials and review hosts reachable from the database server.