postgres.git / summary / log / commit / refs
commit 04137cd764c84a6a9b97384ae6566c4a25eaa828
Author: Fujii Masao <fujii@postgresql.org>
Date: Sat Nov 21 14:19:53 2026 +0000
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@riseup.net>
Author: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/f1743511-5fab-482e-94ec-1198ce18723e@riseup.net
Backpatch-through: 15
src/bin/pg_basebackup/pg_basebackup.c | 16 +++++++++-------
src/bin/pg_basebackup/receivelog.c | 6 ++++--
2 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index e78c95bcb78..4423cea16a6 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -2015,9 +2015,9 @@ BaseBackup(char *compression_algorithm, char *compression_detail,
if (PQresultStatus(res) != PGRES_TUPLES_OK)
pg_fatal("could not initiate base backup: %s",
PQerrorMessage(conn));
- if (PQntuples(res) != 1)
- pg_fatal("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields",
- PQntuples(res), PQnfields(res), 1, 2);
+ if (PQntuples(res) != 1 || PQnfields(res) < 1)
+ pg_fatal("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d or more fields",
+ PQntuples(res), PQnfields(res), 1, 1);
strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart));
@@ -2046,8 +2046,9 @@ BaseBackup(char *compression_algorithm, char *compression_detail,
if (PQresultStatus(res) != PGRES_TUPLES_OK)
pg_fatal("could not get backup header: %s",
PQerrorMessage(conn));
- if (PQntuples(res) < 1)
- pg_fatal("no data returned from server");
+ if (PQntuples(res) < 1 || PQnfields(res) < 3)
+ pg_fatal("could not get backup header: got %d rows and %d fields, expected %d or more rows and %d or more fields",
+ PQntuples(res), PQnfields(res), 1, 3);
/*
* Sum up the total size, for progress reporting
@@ -2188,8 +2189,9 @@ BaseBackup(char *compression_algorithm, char *compression_detail,
if (PQresultStatus(res) != PGRES_TUPLES_OK)
pg_fatal("backup failed: %s",
PQerrorMessage(conn));
- if (PQntuples(res) != 1)
- pg_fatal("no write-ahead log end position returned from server");
+ if (PQntuples(res) != 1 || PQnfields(res) < 1)
+ pg_fatal("could not get write-ahead log end position: got %d rows and %d fields, expected %d rows and %d or more fields",
+ PQntuples(res), PQnfields(res), 1, 1);
strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend));
if (verbose && includewal != NO_WAL)
pg_log_info("write-ahead log end point: %s", xlogend);
diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index 061a7ca2e88..117e472ddbc 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -551,8 +551,10 @@ ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
*/
if (PQnfields(res) != 2 || PQntuples(res) != 1)
{
- pg_log_warning("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
- PQntuples(res), PQnfields(res), 1, 2);
+ pg_log_error("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
+ PQntuples(res), PQnfields(res), 1, 2);
+ PQclear(res);
+ return false;
}
/* Write the history file to disk */
[parent: 42fa657db316]