pgsql: Add a range check on the sequence index from the publisher. - Mailing list pgsql-committers

From Masahiko Sawada
Subject pgsql: Add a range check on the sequence index from the publisher.
Date
Msg-id E1x9nq8-000000016RN-262j@gemulon.postgresql.org
Whole thread
List pgsql-committers
Add a range check on the sequence index from the publisher.

The sequencesync worker asks the publisher about a batch of sequences,
tagging each one with its position in the worker's own list, and the
publisher returns that position alongside the sequence's data. The
received position is used to subscript the list using list_nth(),
which bounds-checks only on assert builds, so it was possible that an
index we never sent made the worker read a pointer from past the end
of the list and then store the remote last_value through it.

Check the position against the list before using it. No sane publisher
can trigger this, but we should not let a remote server steer a memory
access. An in-range position from another batch still gets through and
would attach one sequence's data to another, but that's a wrong value
rather than a corrupt process.

Also, receive the index as int4 rather than int8. Previously, an
out-of-range int8 value was silently truncated to its lower 32 bits by
DatumGetInt32(), which could turn it into a valid index and defeat the
check.

Backpatch to v19, where sequence synchronization was introduced.

Reported-by: Anthropic OSS program
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/CAD21AoBWDMuMmnevZaZ1xSOi75eUx8X7LbVQzmUZudk6F8Bdgg@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7a7f60616136a599153bfacf87ebe32e7c2b0a6a

Modified Files
--------------
src/backend/replication/logical/sequencesync.c | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)


pgsql-committers by date:

Previous
From: Masahiko Sawada
Date:
Subject: pgsql: Don't fully empty GIN pending list in parallel autovacuum worker
Next
From: Masahiko Sawada
Date:
Subject: pgsql: Add a range check on the sequence index from the publisher.