Summary
A SQL injection vulnerability in pg_partman's undo_partition() function allows a partman_user role to place SQL text where a function name is expected. When a caller invokes the function, the injected SQL may execute with that caller's database privileges.
Affected context
The issue occurs in pg_partman deployments prior to 5.5.0 when undo_partition() uses the part_config.time_encoder value in a dynamically constructed SELECT statement without identifier quoting. Exploitation generally depends on a role with partman_user access being able to influence that configuration value and a privileged caller subsequently invoking the affected function. The function is not normally part of the default background-worker execution path.
Exploit preconditions
- Attack surface: PostgreSQL extension function
undo_partition() - Required access: A role with
partman_useraccess that can store or alter the relevant configuration value - Trigger point: Invocation of
undo_partition()by a database role - Privilege condition: The invoking role must have permissions that make the resulting SQL operations consequential
- Why exploitation is possible: Unrestricted configuration text is interpolated into dynamic SQL without safe identifier handling
Why this happens
The underlying bug is the construction of a dynamic SELECT statement from unrestricted text that is treated as a function name. Because the value is interpolated without identifier quoting or a constrained allowlist, attacker- controlled content can alter the meaning of the SQL statement. This maps to CWE-89, SQL Injection, where input crosses a query-construction boundary without sufficient separation between data and executable SQL.
Potential impact
- A caller's database privileges may be abused to read or modify data
- Database integrity could be affected through unauthorized SQL operations
- Availability may be reduced through destructive or resource-intensive queries
- Privileged callers could expose broader confidentiality, integrity, or availability permissions
- In some deployments, database compromise could support follow-on access to connected systems
General mitigation guidance
Mitigation generally focuses on removing SQL construction from untrusted
configuration values and reducing the authority available to extension
functions.
Common defensive patterns include:
- Restricting configuration fields to validated function identifiers or a narrowly defined allowlist
- Using parameterized SQL for values and safe identifier-quoting mechanisms where dynamic identifiers are unavoidable
- Separating configuration data from executable SQL fragments
- Limiting which roles can modify partman configuration and invoke maintenance functions
- Applying least privilege to database roles and connected application accounts
- Reviewing existing configuration values for unexpected SQL syntax
- Keeping extension components maintained through the organization's normal security-update process
How teams detect issues like this
Detection approaches can include:
- Static analysis rules for string interpolation or concatenation in dynamic SQL construction
- Code review checks for unquoted identifiers derived from table, column, function, or configuration fields
- Database audits of roles that can modify partman configuration or execute
undo_partition() - Monitoring configuration changes for values containing SQL operators, comments, statement separators, or unexpected keywords
- Query logging and alerting for unusual statements executed during partition maintenance
- Policy enforcement requiring parameterization, identifier allowlists, and privilege review for database extensions
- Neutral automated code-review tooling to flag dynamic query construction patterns

