Skip to main content
Menu
Advisories

pgjdbc simple query mode quotes parameters for the wrong standard_conforming_strings value

GHSA-6RXC-3R4P-GPFFPublished Updated GitHub

Description

With preferQueryMode=simple, pgjdbc could quote a string parameter for the wrong value of standard_conforming_strings. A parameter value could then run as SQL.

Details

In simple query mode the driver writes parameter values into the query text. It doubled quotes and left backslashes alone when it believed the setting was on.

An earlier statement of the same query or batch, a trigger or a configuration reload can turn the setting off. A value with \' then ended the literal, and the rest of the value ran as SQL. Before 42.7.6 the driver always quoted as if the setting were on, so any session with the setting off was affected.

The statement cache also reused a parse made under the other setting. The placeholders of that parse could then fall inside or outside a literal.

The JDK EUC_JP and Shift_JIS charsets encode the yen sign U+00A5 as 0x5C, which the server reads as a backslash. With allowEncodingChanges=true the yen sign worked like a backslash in the same way. PGConnection.escapeLiteral had the same problem.

Proof of concept

// preferQueryMode=simple, autocommit off
PreparedStatement ps = conn.prepareStatement("SET LOCAL standard_conforming_strings = off; SELECT ?");
ps.setString(1, "\\'), (SELECT $$injected$$) --");
ps.execute(); // The subquery runs as SQL.

Impact

An attacker who controls a string parameter can run SQL as the application's database role. This needs preferQueryMode=simple, which is not the default. It also needs standard_conforming_strings off on the server or during the session. The yen sign case also needs allowEncodingChanges=true and the EUC_JP or SJIS client encoding.

Affected versions

References

GitHub · pgjdbc/pgjdbc