pgjdbc leaves a half-written message on the connection after an unchecked exception
Description
An unchecked exception while pgjdbc sent parameter values could leave part of a message in the send buffer. The server could then read the bytes of a later value as a SQL command.
Details
The driver closed the connection after an IOException, but not after an unchecked exception. Part of a Bind message then stayed in the send buffer, and the connection stayed open.
Since 42.6.0 a setBinaryStream() value without a length and over 51200 bytes is spooled and released after it is sent. Executing the statement again threw a NullPointerException in the middle of the Bind. The driver also retries a statement itself after SQLState 0A000 or 26000, which a DDL change can cause.
The server read the bytes that followed as the rest of the value. The bytes of a later uploaded value could then be read as a new Query message.
In all versions, an application InputStream that throws an unchecked exception has the same effect.
Proof of concept
An attacker who controls the content of an uploaded value can run SQL as the application's database role. The connection must first fail in this way. That needs a stream value that is executed again, or a stream that throws an unchecked exception.
Impact
All versions before 42.7.15 are affected. The setBinaryStream() case starts in 42.6.0. 42.7.15 fixes the problem.