pgsql: Fix out-of-bounds access in autoprewarm worker

From: Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix out-of-bounds access in autoprewarm worker
Date: 2026-06-26 17:54:49
Message-ID: E1wdAlN-000J3w-1O@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix out-of-bounds access in autoprewarm worker

The read stream callback apw_read_stream_next_block() advances p->pos
through the block_info array. When processing the last block, it
increments p->pos to prewarm_stop_idx before returning. The callback
itself is safe because it checks bounds before accessing the array.

However, the caller assigned blk from block_info[i] at the end of the
loop body, before the loop condition was re-evaluated. When i equaled
prewarm_stop_idx, this accessed memory beyond the allocated DSM segment,
causing a segfault.

Restructure the loop to check bounds at the top and assign blk at the
beginning of the loop body, where it is always safe. This avoids the
need for an explicit bounds check at the end.

Backpatch to 18, where the bug was introduced by commit 6acab8bdbcda.

Author: Matheus Alcantara <mths(dot)dev(at)pm(dot)me>
Reported-by: Glauber Batista <glauberrbatista(at)gmail(dot)com>
Reviewed-by: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Reviewed-by: Tomas Vondra <tomas(at)vondra(dot)me>
Backpatch-through: 18
Discussion: https://www.postgresql.org/message-id/CAO%2B_mTQgQyTYwDh%3DU8iTnsDmOGyWsZJjUV31SmEYwmw6_xY6Bw%40mail.gmail.com

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3bf2cb22576eac50d80e3eecd485fcae4fdd7f8b

Modified Files
--------------
contrib/pg_prewarm/autoprewarm.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-06-26 18:05:50 pgsql: Make crosstabview honor boolean/null display settings
Previous Message Tomas Vondra 2026-06-26 17:54:37 pgsql: Fix out-of-bounds access in autoprewarm worker