Startup parameter injection via embedded NUL characters
Description
pgjdbc allows embedded NUL (i.e. byte value of zero) characters in PostgreSQL startup parameters, potentially allowing an attacker to override the database user or other connection settings.
Details
PostgreSQL startup messages contain NUL-terminated name/value pairs. pgjdbc does not validate values for embedded NUL characters before serialization.
An attacker who controls a startup parameter such as the database name, currentSchema, or options can inject additional parameters. Since PostgreSQL uses the last occurrence of duplicate parameters, injected values can override the intended user, database, or replication settings.
URL-encoded %00 characters can also introduce NUL characters through JDBC URLs.
Proof of concept
An application connects with user appuser and accepts an attacker-controlled currentSchema value:
currentSchema=public\0user\0postgresThe resulting startup message contains:
user=appuser
database=mydb
search_path=public
user=postgresPostgreSQL processes the final user parameter, potentially authenticating the connection as postgres instead of appuser.
Successful authentication depends on the server's authentication configuration.
Impact
Applications that accept untrusted connection parameters may connect as an unintended database role, potentially allowing unauthorized database access or privilege escalation.
This does not bypass PostgreSQL authentication. The impact depends on the authentication method:
- trust: Authentication succeeds without a password, potentially allowing access as another database user.
- md5: Authentication fails when the injected username differs from the configured username, even if both roles share the same password. The MD5 response incorporates the username, and pgjdbc computes it using the originally configured username.
- scram-sha-256: Authentication succeeds if the injected role has the same password as the configured role. Unlike MD5, SCRAM does not incorporate the username into the password-derived key.
The vulnerability may also allow attackers to override other startup parameters, potentially changing session behavior or causing denial of service.