Summary
The CloudNativePG (CNPG) instance manager opens superuser connections to managed PostgreSQL databases without pinning search_path in the connection startup packet. A role holding DATABASE OWNER on any managed database — a role CNPG
creates by default at cluster bootstrap — can plant attacker-controlled overloads of built-in operators (for example =, >) in the public schema and re-target the database- or role-level search_path so those overloads resolve before pg_catalog.
On the next instance-manager reconcile, routine introspection probes such as:
SELECT COUNT(*) > 0 FROM pg_catalog.pg_extension WHERE extname = $1
execute as the cluster postgres superuser. Schema-qualifying the relation (pg_catalog.pg_extension) does not qualify the operators in the query: > and = still resolve through the tenant-controlled search_path and invoke the planted function bodies with superuser privileges. From there an attacker can obtain:
- PostgreSQL superuser across every database in the cluster, including
postgres.
- OS command execution inside the instance pod via
COPY ... FROM PROGRAM, which is available to superusers.
- The pod ServiceAccount token, after which the blast radius is bounded by the RBAC granted to the instance pod's ServiceAccount and the surrounding cloud workload identity.
This is a privilege-escalation vector in the class of CWE-426 (Untrusted Search Path), the same family as CVE-2018-1058.
Affected and fixed versions
- Affected: all releases prior to the fixed versions below.
- Fixed in: 1.30.0, 1.29.2, and 1.28.4.
- Remediation: see PR #10774.
Clusters that supply a fixed search_path to operator connections, or that do not expose a DATABASE OWNER role to untrusted users, are not exploitable.
Root cause
fillDefaultParameters in pkg/management/postgres/pool/profiles.go set client_encoding and datestyle as pgx RuntimeParams but never set search_path. Because search_path was absent from the startup packet, PostgreSQL fell back to the resolution chain ALTER ROLE → ALTER DATABASE → the built-in default "$user", public. Both ALTER ROLE <db-owner> and ALTER DATABASE <owned-db> are within the privileges of the database owner, so the tenant fully controlled the value seen by operator-issued queries.
Three production callsites additionally bypassed the connection profile by calling sql.Open("pgx", ...) directly (WaitForPrimaryAvailable, and the two WAL-archive helpers in pkg/management/postgres/wal.go). These were not exploitable in practice given their connection target and role, but they broke the invariant that every CNPG-issued connection is enriched by fillDefaultParameters.
Probe surface
Every introspection query the operator runs against a managed database goes through the affected pool and executes as the cluster postgres superuser. The probes contain at least one built-in operator (=, >, join equality on oid, etc.) whose resolution is influenced by search_path, including extension presence, the PgBouncer auth-role and auth-function probes, bootstrap role/database existence checks, managed-database schema and extension introspection, FDW and foreign-server probes, publication and subscription probes, and declarative role reconciliation. Any future probe inherits the same exposure for as long as search_path is unpinned.
Independent SECURITY DEFINER hazard
The PgBouncer integration creates a SECURITY DEFINER function public.user_search owned by postgres without a SET search_path clause. The = operator in its body is resolved at execution time using the caller's search_path, not the definer's, and the function is reached over the PgBouncer auth connection — a path the connection-level pin does not cover. It must be hardened independently.
Exploit chain (on an unpatched cluster)
- Connect as the database owner (default bootstrap role, no extra privileges):
psql "host=<cluster>-rw.<ns>.svc dbname=app user=app sslmode=require".
- Plant a malicious overload matching the exact signature used by a probe, whose body escalates the current role and then returns the expected result.
- Flip resolution so the overload is found first:
ALTER DATABASE app SET search_path = public, pg_catalog.
- Wait for the next instance-manager reconcile. The probe resolves the operator to the planted function, which runs while the session is the
postgres superuser.
- Reconnect with the escalated privileges and pivot via
COPY ... FROM PROGRAM to read the pod ServiceAccount token.
Remediation
The fix (PR #10774) applies defence in depth:
- Pin
search_path = pg_catalog, public, pg_temp as a pgx RuntimeParam on every pooled operator connection, so it ships in the startup packet and takes precedence over tenant-controlled database/role defaults. pg_catalog first defeats any planted shadow; public stays writable for legitimate references; pg_temp is searched last rather than first.
- Route the three direct
sql.Open callsites through pool.NewDBConnection so they inherit the pin.
- Bracket user-authored scripts (
postInit*, logical-import post-import SQL) and CREATE EXTENSION with a dedicated connection that sets "$user", public and resets to the pinned default, preserving standard resolution without leaking it back to the pool.
- Pin
search_path = pg_catalog, pg_temp on the public.user_search SECURITY DEFINER function, and recreate it automatically on upgrade for existing clusters.
- Pin the monitoring transaction's
search_path with SET LOCAL.
Users should upgrade to a fixed release. If you cannot upgrade immediately, avoid granting DATABASE OWNER to untrusted roles and review any tenant-set search_path on managed databases.
Credit
Reported by Mehmet Ince (mehmetince.net), @mdisec.
Summary
The CloudNativePG (CNPG) instance manager opens superuser connections to managed PostgreSQL databases without pinning
search_pathin the connection startup packet. A role holdingDATABASE OWNERon any managed database — a role CNPGcreates by default at cluster bootstrap — can plant attacker-controlled overloads of built-in operators (for example
=,>) in thepublicschema and re-target the database- or role-levelsearch_pathso those overloads resolve beforepg_catalog.On the next instance-manager reconcile, routine introspection probes such as:
execute as the cluster
postgressuperuser. Schema-qualifying the relation (pg_catalog.pg_extension) does not qualify the operators in the query:>and=still resolve through the tenant-controlledsearch_pathand invoke the planted function bodies with superuser privileges. From there an attacker can obtain:postgres.COPY ... FROM PROGRAM, which is available to superusers.This is a privilege-escalation vector in the class of CWE-426 (Untrusted Search Path), the same family as CVE-2018-1058.
Affected and fixed versions
Clusters that supply a fixed
search_pathto operator connections, or that do not expose aDATABASE OWNERrole to untrusted users, are not exploitable.Root cause
fillDefaultParametersinpkg/management/postgres/pool/profiles.gosetclient_encodinganddatestyleas pgxRuntimeParamsbut never setsearch_path. Becausesearch_pathwas absent from the startup packet, PostgreSQL fell back to the resolution chainALTER ROLE→ALTER DATABASE→ the built-in default"$user", public. BothALTER ROLE <db-owner>andALTER DATABASE <owned-db>are within the privileges of the database owner, so the tenant fully controlled the value seen by operator-issued queries.Three production callsites additionally bypassed the connection profile by calling
sql.Open("pgx", ...)directly (WaitForPrimaryAvailable, and the two WAL-archive helpers inpkg/management/postgres/wal.go). These were not exploitable in practice given their connection target and role, but they broke the invariant that every CNPG-issued connection is enriched byfillDefaultParameters.Probe surface
Every introspection query the operator runs against a managed database goes through the affected pool and executes as the cluster
postgressuperuser. The probes contain at least one built-in operator (=,>, join equality onoid, etc.) whose resolution is influenced bysearch_path, including extension presence, the PgBouncer auth-role and auth-function probes, bootstrap role/database existence checks, managed-database schema and extension introspection, FDW and foreign-server probes, publication and subscription probes, and declarative role reconciliation. Any future probe inherits the same exposure for as long assearch_pathis unpinned.Independent
SECURITY DEFINERhazardThe PgBouncer integration creates a
SECURITY DEFINERfunctionpublic.user_searchowned bypostgreswithout aSET search_pathclause. The=operator in its body is resolved at execution time using the caller'ssearch_path, not the definer's, and the function is reached over the PgBouncer auth connection — a path the connection-level pin does not cover. It must be hardened independently.Exploit chain (on an unpatched cluster)
psql "host=<cluster>-rw.<ns>.svc dbname=app user=app sslmode=require".ALTER DATABASE app SET search_path = public, pg_catalog.postgressuperuser.COPY ... FROM PROGRAMto read the pod ServiceAccount token.Remediation
The fix (PR #10774) applies defence in depth:
search_path = pg_catalog, public, pg_tempas a pgxRuntimeParamon every pooled operator connection, so it ships in the startup packet and takes precedence over tenant-controlled database/role defaults.pg_catalogfirst defeats any planted shadow;publicstays writable for legitimate references;pg_tempis searched last rather than first.sql.Opencallsites throughpool.NewDBConnectionso they inherit the pin.postInit*, logical-import post-import SQL) andCREATE EXTENSIONwith a dedicated connection that sets"$user", publicand resets to the pinned default, preserving standard resolution without leaking it back to the pool.search_path = pg_catalog, pg_tempon thepublic.user_searchSECURITY DEFINERfunction, and recreate it automatically on upgrade for existing clusters.search_pathwithSET LOCAL.Users should upgrade to a fixed release. If you cannot upgrade immediately, avoid granting
DATABASE OWNERto untrusted roles and review any tenant-setsearch_pathon managed databases.Credit
Reported by Mehmet Ince (mehmetince.net), @mdisec.