Skip to content

Fix invalid free in pgmoneta_symlink_at_file() - #1348

Closed
Pranav-error wants to merge 1 commit into
pgmoneta:mainfrom
Pranav-error:fix-symlink-at-file-free
Closed

Pranav-error wants to merge 1 commit into
pgmoneta:mainfrom
Pranav-error:fix-symlink-at-file-free

Conversation

@Pranav-error

Copy link
Copy Markdown
Collaborator

Fixes #1345.

`dirname(strdup(from))` followed by `free(dir_path)` assumes
`dirname()` returns the exact pointer `strdup()` allocated. POSIX
doesn't guarantee that -- on macOS/BSD libc, `dirname()` can return a
pointer into static/internal storage instead. The fix keeps the
`strdup`'d pointer in its own variable and frees that instead of
whatever `dirname()` happened to return.

Verified by reproducing the crash on macOS/arm64 (restore of a backup
with tablespaces, via `pgmoneta-cli verify`), confirming the exact
invalid-free via a live crash report, applying this fix, and confirming
the same restore/verify now completes without crashing.

This surfaced while getting PR #1316's own `test_cli_verify` test to
run end-to-end for the first time -- it was crashing here, before ever
reaching the page-checksum logic that test is meant to exercise.

Copilot AI balanced review requested due to automatic review settings October 4, 2026 10:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

dirname() is not required to return the pointer it was given -- on
macOS/BSD libc it can return a pointer into static storage instead of
modifying the strdup'd buffer in place. free(dir_path) was freeing
whatever dirname() returned rather than the actual allocation, which
crashes macOS's hardened malloc ("BUG IN CLIENT OF LIBMALLOC: not an
allocated block") and is undefined behavior elsewhere.

Found while getting PR pgmoneta#1316's test_cli_verify to actually run
end-to-end: any restore (or verify, which restores internally) of a
backup containing a tablespace crashed inside
copy_tablespaces_restore() -> pgmoneta_symlink_at_file() before this
fix.

Uses pgmoneta_append() rather than strdup() per project convention.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jesperpedersen

Copy link
Copy Markdown
Member

Rebased and merged.

Thanks for your contribution !

jesperpedersen pushed a commit that referenced this pull request Oct 8, 2026
write_incremental_file() did:

  rel_path = pgmoneta_append(rel_path, relative_filename);
  rel_path = dirname(rel_path);
  file_name = pgmoneta_append(file_name, rel_path + strlen(rel_path) + 1);

This relies on dirname() modifying its buffer in place and returning
the same pointer, so pointer arithmetic past the inserted NUL lands on
the original basename bytes. glibc commonly does this for simple
inputs; POSIX doesn't require it, and BSD/macOS libc's dirname() can
return a pointer into a separate static buffer instead -- verified
empirically on macOS/clang, where this produces an EMPTY file_name
rather than the real basename. Masked on Linux CI because glibc's
dirname() happens to modify common inputs in place there.

Fix: split directory and basename with strrchr() on a buffer we fully
control, instead of depending on dirname()'s implementation-defined
in-place behavior. Verified against a slash path, a no-slash path, and
a single-level path via a standalone reproduction, and that the real
codebase builds cleanly under -Werror with the change applied.

Found by sweeping every dirname() call site in pg-family for the same
portability bug class as #1345 after fixing that one in #1348 -- this
is a different call site and failure mode (wrong data, not an invalid
free), but the same root cause.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
jesperpedersen pushed a commit to pgvictoria/pgvictoria that referenced this pull request Oct 11, 2026
dirname() is not required to return the pointer it was given -- on
macOS/BSD libc it can return a pointer into static storage instead of
modifying the strdup'd buffer in place. free(dir_path) was freeing
whatever dirname() returned rather than the actual allocation, which
crashes macOS's hardened malloc ("BUG IN CLIENT OF LIBMALLOC: not an
allocated block") and is undefined behavior elsewhere.

Found the identical bug in pgmoneta (pgmoneta/pgmoneta#1345 /
pgmoneta/pgmoneta#1348) while getting that project's restore/verify
test suite running end-to-end on macOS/arm64; pgvictoria's utils.c had
the exact same construct.

Uses pgvictoria_append() rather than strdup() per project convention.
Verified by compiling utils.c standalone with the Darwin-specific
flags this project's CMakeLists.txt adds for macOS -- clean build, no
warnings.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pgmoneta_symlink_at_file() frees a pointer dirname() never allocated, crashing restore of tablespace-containing backups

3 participants