Check that commutator/negator operators return the same data type.

The commutator or negator of an operator must surely return the same
data type as the operator itself (always boolean for a negator, but
for a commutator it could be any type).  CREATE/ALTER OPERATOR
failed to enforce this, providing a route for evildoers to reach
type-confusion problems.

Another ancient oversight in this feature is that if the first CREATE
OPERATOR names a commutator or negator, but no corresponding clause
appears in the CREATE OPERATOR that fills in the shell operator, we
failed to preserve the shell operator's back-link, resulting in
inconsistent catalog state.  That has to be considered part of the
security problem, since it prevents checking whether the two operators
have matching result types.  However, fixing it means that an operator
created without any COMMUTATOR or NEGATOR clause at all can acquire a
commutator/negator, if it overwrites a shell operator that links to
such a sibling object.  That creates a new security problem: an
attacker could set up a trojan-horse operator that names a
commutator/negator operator that doesn't exist yet, but that the
attacker expects to be created later.  Then the attacker has the
potential to inject his own code into uses of that victim operator.
The object_ownercheck() test in OperatorCreate() doesn't prevent
that if the second operator is created by a superuser.  To fix this,
we change that test to insist on exact equality of creating user.
(This tightening could potentially break innocent use-cases, except
that it's hard to foresee innocent cases where the two operators
wouldn't be created by the same role.  The logic does not prevent
either operator from being reassigned to a new owner later.)

In v16 and earlier, this fix incorporates portions of commit
2b5154bea.  Before that, there were yet other scenarios for creating
inconsistent commutator/negator links, potentially allowing type
confusion.  I also adopted some of the cosmetic refactoring done in
that commit, just to make pg_operator.c look less different across
branches.

In master, this deletes OperatorUpd() in favor of an expanded function
UpdateOperatorLinks().  In the back branches, OperatorUpd() is kept to
preserve ABI compatibility.  I doubt there are any external callers,
but as a one-line wrapper it's cheap enough to not worry about.

Reported-by: Stephan Lipp <[email protected]>
Author: Tom Lane <[email protected]>
Backpatch-through: 14
Security: CVE-2026-116335

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/85420b358863b4851a5b5f79be38b0d2869c6a20
Author: Tom Lane <[email protected]>

Modified Files
--------------
src/backend/catalog/pg_operator.c             | 465 ++++++++++++++++++++------
src/backend/commands/operatorcmds.c           |   3 +-
src/include/catalog/pg_operator.h             |   3 +
src/test/regress/expected/create_operator.out | 166 ++++++++-
src/test/regress/sql/create_operator.sql      | 153 ++++++++-
5 files changed, 680 insertions(+), 110 deletions(-)

Reply via email to