pgsql: read_stream: Fix overflow hazard with large shared buffers

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: read_stream: Fix overflow hazard with large shared buffers
Date: 2025-04-07 13:45:25
Message-ID: E1u1mmz-003818-0G@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

read_stream: Fix overflow hazard with large shared buffers

If the limit returned by GetAdditionalPinLimit() is large, the buffer_limit
variable in read_stream_start_pending_read() can overflow. While the code is
careful to limit buffer_limit PG_INT16_MAX, we subsequently add the number of
forwarded buffers.

The overflow can lead to assertion failures, crashes or wrong query results
when using large shared buffers.

It seems easier to avoid this if we make the buffer_limit variable an int,
instead of an int16. Do so, and clamp buffer_limit after adding the number of
forwarded buffers.

It's possible we might want to address this and related issues more widely by
changing to int instead of int16 more widely, but since the consequences of
this bug can be confusing, it seems better to fix it now.

This bug was introduced in ed0b87caaca.

Discussion: https://postgr.es/m/ewvz3cbtlhrwqk7h6ca6cctiqh7r64ol3pzb3iyjycn2r5nxk5@tnhw3a5zatlr

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8ce79483dc47df11159f506cf51bacec9f874055

Modified Files
--------------
src/backend/storage/aio/read_stream.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Melanie Plageman 2025-04-07 13:50:42 Re: pgsql: Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate
Previous Message Alexander Korotkov 2025-04-07 13:29:15 pgsql: Remove GUC_NOT_IN_SAMPLE from enable_self_join_elimination