pgsql: pg_basebackup: Fix crashes on malformed server responses

From: Fujii Masao <fujii(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: pg_basebackup: Fix crashes on malformed server responses
Date: 2026-11-21 14:22:07
Message-ID: E1xUlyg-00000000e46-1QE6@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

pg_basebackup: Fix crashes on malformed server responses

pg_basebackup could crash when a server returned replication command
results with missing rows or columns.

This happened because pg_basebackup checked the row counts of the
BASE_BACKUP results but not the column counts before reading the backup
start and end positions and tablespace information. Its WAL streaming
code checked the row and column counts of TIMELINE_HISTORY results,
but only logged a warning on a mismatch and continued processing.
These omissions allowed NULL pointers returned by PQgetvalue() for
missing fields to reach string and numeric conversion functions.

Fix this by requiring at least one column in the backup start and end
results and at least three columns in the tablespace header before
accessing their values. Also report an error, release the result, and
stop the current WAL streaming attempt if a TIMELINE_HISTORY response
does not contain exactly one row and two columns. The latter change
also fixes the same crash in pg_receivewal, which shares this WAL
streaming code.

Backpatch to all supported versions.

Author: s1ko <s1ko(at)riseup(dot)net>
Author: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Reviewed-by: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Discussion: https://postgr.es/m/f1743511-5fab-482e-94ec-1198ce18723e@riseup.net
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/d9c7185ee4613e245492ccda6942fcde263e0a11

Modified Files
--------------
src/bin/pg_basebackup/pg_basebackup.c | 16 +++++++++-------
src/bin/pg_basebackup/receivelog.c | 6 ++++--
2 files changed, 13 insertions(+), 9 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2026-11-21 19:08:07 pgsql: Don't pfree a Path that belongs to a different parent relation.
Previous Message Fujii Masao 2026-11-21 14:19:09 pgsql: Fix libpq crashes on malformed target_session_attrs query result