postgres.git / summary / log / commit / refs

commit    d9c7185ee4613e245492ccda6942fcde263e0a11
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 8c7f62f48bc..7f087468648 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -1946,9 +1946,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)); @@ -1978,8 +1978,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 @@ -2111,8 +2112,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 ec8b3f7e22a..73b759309f9 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -557,8 +557,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: de2d7b602b43]