Hi,
tablespace_list_append() in pg_basebackup and add_tablespace_mapping() in
pg_combinebackup copy the -T OLDDIR=NEWDIR argument into fixed MAXPGPATH
buffers. The length check at the top of the loop is
if (dst_ptr - dst >= MAXPGPATH)
pg_fatal("directory name too long");
so a final field of exactly MAXPGPATH bytes passes the check, fills the
buffer, and leaves no terminating NUL. In pg_basebackup, new_dir is the
last member of TablespaceListCell, so later string operations on it
(strlen, strcmp, the realpath/canonicalize step) can read past the
allocation. An OLDDIR of that length is only caught by accident, when the
following '=' is examined.
The attached patch compares against MAXPGPATH - 1 in both places and adds a
TAP test to
https://www.google.com/url?q=http://010_pg_basebackup.pl&source=gmail&ust=1795696336159000&sa=E:
--tablespace-mapping '/foo=/' . ('a' x 1023) now fails with "directory name
too long".
This is a hardening fix: it needs a 1024-byte mapping argument typed by the
user, so I would not consider it a backpatch candidate unless someone sees
a reason. It applies cleanly to current master (9ebc9a458c).
Regards,
Pranav
From f9dd0cde7ec57ec39df9637504ae79620eb982bf Mon Sep 17 00:00:00 2001
From: Raja Sai Pranav <[email protected]>
Date: Wed, 25 Nov 2026 01:39:24 +0530
Subject: [PATCH] Fix off-by-one in -T tablespace mapping length check
tablespace_list_append() in pg_basebackup and add_tablespace_mapping()
in pg_combinebackup copy the mapping into fixed MAXPGPATH buffers,
checking the length at the top of each iteration with
"dst_ptr - dst >= MAXPGPATH". A final field of exactly MAXPGPATH bytes
passes that check and fills the buffer, leaving no terminating NUL. For
pg_basebackup, new_dir is the last member of the struct, so later
string operations on it can read past the allocation. An old directory
of that length is caught only by accident, when the following '=' is
examined.
Reserve room for the terminator by comparing against MAXPGPATH - 1, and
add a test.
---
src/bin/pg_basebackup/pg_basebackup.c | 2 +-
src/bin/pg_basebackup/t/010_pg_basebackup.pl | 10 ++++++++++
src/bin/pg_combinebackup/pg_combinebackup.c | 2 +-
3 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 4daf985ab8..1ee7089984 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -329,7 +329,7 @@ tablespace_list_append(const char *arg)
dst_ptr = dst = cell->old_dir;
for (arg_ptr = arg; *arg_ptr; arg_ptr++)
{
- if (dst_ptr - dst >= MAXPGPATH)
+ if (dst_ptr - dst >= MAXPGPATH - 1)
pg_fatal("directory name too long");
if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
index 23a757b1c8..d11587acd2 100644
--- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl
+++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
@@ -351,6 +351,16 @@ $node->command_fails_like(
],
qr/multiple "=" signs in tablespace mapping/,
'--tablespace-mapping with multiple = fails');
+# A directory name of MAXPGPATH bytes leaves no room for the terminating NUL.
+$node->command_fails_like(
+ [
+ @pg_basebackup_defs,
+ '--pgdata' => "$tempdir/backup_foo",
+ '--format' => 'plain',
+ '--tablespace-mapping' => '/foo=/' . ('a' x 1023)
+ ],
+ qr/directory name too long/,
+ '--tablespace-mapping with too long new directory fails');
$node->command_fails_like(
[
@pg_basebackup_defs,
diff --git a/src/bin/pg_combinebackup/pg_combinebackup.c b/src/bin/pg_combinebackup/pg_combinebackup.c
index 254a27b125..61ac17d6a1 100644
--- a/src/bin/pg_combinebackup/pg_combinebackup.c
+++ b/src/bin/pg_combinebackup/pg_combinebackup.c
@@ -472,7 +472,7 @@ add_tablespace_mapping(cb_options *opt, char *arg)
dst_ptr = dst = tsmap->old_dir;
for (arg_ptr = arg; *arg_ptr != '\0'; arg_ptr++)
{
- if (dst_ptr - dst >= MAXPGPATH)
+ if (dst_ptr - dst >= MAXPGPATH - 1)
pg_fatal("directory name too long");
if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
--
2.50.1 (Apple Git-155)