Re: [PATCH] Fix off-by-one in -T tablespace mapping length check

From: Daniel Gustafsson <daniel(at)yesql(dot)se>
To: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Fix off-by-one in -T tablespace mapping length check
Date: 2026-12-04 12:07:09
Message-ID: FB274A3B-6BC6-4787-B829-9C727D912D77@yesql.se
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 25 Nov 2026, at 13:32, Raja Sai pranav <rajasaipranav0(at)gmail(dot)com> wrote:

> 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".

Thanks for the report and patch, I have reviewed and pushed it.

--
Daniel Gustafsson

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2026-12-04 12:42:40 Speed up NUMERICs by skipping varlena unpacking
Previous Message Guillaume Lelarge 2026-12-04 12:00:31 Re: Proposal: per-table row counts in pg_dump/pg_restore, with restore-time verification