postgres.git / summary / log / commit / refs
commit 76a55702d89ba9f5d0198c2d5d3d2d62357a8880
Author: Fujii Masao <fujii@postgresql.org>
Date: Thu Oct 29 11:45:29 2026 +0000
Use pg_parse_lsn() to parse LSN fields in backup_label
The START WAL LOCATION and CHECKPOINT LOCATION fields in backup_label
accepted overlong high components, which could overflow and produce a
different LSN from the one supplied.
Use pg_parse_lsn() to validate and convert these fields, requiring one
to eight hexadecimal digits per component and no trailing characters,
as for pg_lsn input.
Also detect INCREMENTAL FROM LSN without parsing its value, so that
malformed values such as "junk" do not bypass the existing check
against using an incremental backup directly as a data directory.
Author: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Jan Nidzwetzki <jan@planetscale.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/OS7PR01MB18317EED85B1A72C69B226630F5672@OS7PR01MB18317.jpnprd01.prod.outlook.com
src/backend/access/transam/xlogrecovery.c | 27 ++++++++++++++++++---------
src/backend/utils/adt/pg_lsn.c | 2 --
src/include/common/pg_parse_lsn.h | 2 ++
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 13f15728ce6..52ac6d58423 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -44,6 +44,7 @@
#include "catalog/pg_control.h"
#include "commands/tablespace.h"
#include "common/file_utils.h"
+#include "common/pg_parse_lsn.h"
#include "miscadmin.h"
#include "nodes/miscnodes.h"
#include "pgstat.h"
@@ -1190,8 +1191,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
char backupfrom[20];
char backuplabel[MAXPGPATH];
char backuptime[128];
- uint32 hi,
- lo;
+ char lsn[MAXPG_LSNLEN + 1];
/* suppress possible uninitialized-variable warnings */
*checkPointLoc = InvalidXLogRecPtr;
@@ -1218,19 +1218,23 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
* is pretty crude, but we are not expecting any variability in the file
* format).
*/
- if (fscanf(lfp, "START WAL LOCATION: %X/%08X (file %08X%16s)%c",
- &hi, &lo, &tli_from_walseg, startxlogfilename, &ch) != 5 || ch != '\n')
+ if (fscanf(lfp,
+ "START WAL LOCATION: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/] (file %08X%16s)%c",
+ lsn, &tli_from_walseg, startxlogfilename, &ch) != 4 ||
+ ch != '\n' ||
+ !pg_parse_lsn(lsn, &RedoStartLSN))
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
- RedoStartLSN = ((uint64) hi) << 32 | lo;
RedoStartTLI = tli_from_walseg;
- if (fscanf(lfp, "CHECKPOINT LOCATION: %X/%08X%c",
- &hi, &lo, &ch) != 3 || ch != '\n')
+ if (fscanf(lfp,
+ "CHECKPOINT LOCATION: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/]%c", lsn,
+ &ch) != 2 ||
+ ch != '\n' ||
+ !pg_parse_lsn(lsn, checkPointLoc))
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
- *checkPointLoc = ((uint64) hi) << 32 | lo;
*backupLabelTLI = tli_from_walseg;
/*
@@ -1297,7 +1301,12 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
tli_from_file, BACKUP_LABEL_FILE)));
}
- if (fscanf(lfp, "INCREMENTAL FROM LSN: %X/%08X\n", &hi, &lo) > 0)
+ /*
+ * INCREMENTAL FROM LSN indicates that this is an incremental backup. The
+ * LSN itself need not be parsed, so reading its first character is
+ * sufficient.
+ */
+ if (fscanf(lfp, "INCREMENTAL FROM LSN: %c", &ch) == 1)
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("this is an incremental backup, not a data directory"),
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 7886e9b83e8..3ec2c2b650e 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -19,8 +19,6 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
-#define MAXPG_LSNLEN 17
-
/*----------------------------------------------------------
* Formatting and conversion routines.
*---------------------------------------------------------*/
diff --git a/src/include/common/pg_parse_lsn.h b/src/include/common/pg_parse_lsn.h
index 0a80785a4e9..94d776f89b6 100644
--- a/src/include/common/pg_parse_lsn.h
+++ b/src/include/common/pg_parse_lsn.h
@@ -15,6 +15,8 @@
#include "access/xlogdefs.h"
+#define MAXPG_LSNLEN 17
+
extern bool pg_parse_lsn(const char *str, XLogRecPtr *result);
#endif /* PG_PARSE_LSN_H */
[parent: 27c844034f82]