pgjdbc catalog queries can run objects of other database roles
Description
Some pgjdbc catalog queries used unqualified operators, functions and type names. Another database role could supply its own and have them run in the application's session.
Details
The queries of DatabaseMetaData, ResultSetMetaData and TypeInfoCache used unqualified operators and functions. Some compare types such as oid and int4 that have no exact match in pg_catalog. A role with CREATE on a schema of the search_path could add an exact match. The server then ran it as the caller.
TypeInfoCache resolved an unqualified type name, such as interval for a PGInterval parameter, by its own ranking. A type or domain with the same name in another schema could win, even one in the temporary schema of another session. The server then ran the CHECK of that domain in the application's session.
Proof of concept
-- An attacker with CREATE on schema s, which is on the application's search_path.
CREATE FUNCTION s.f(oid, int4) RETURNS bool LANGUAGE plpgsql AS 'BEGIN RETURN true; END';
CREATE OPERATOR s.= (LEFTARG = oid, RIGHTARG = int4, PROCEDURE = s.f);rs.getMetaData().getTableName(1); // Runs s.f as the application's role.Impact
A database role that can create objects in a schema of the application's search_path can run SQL with the privileges of the application's role. The type name case needs only the TEMP privilege, which every role has by default. The application must call metadata methods or use types such as PGInterval.