]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent path traversal in pg_basebackup and pg_rewind
authorMichael Paquier <michael@paquier.xyz>
Mon, 11 May 2026 12:13:49 +0000 (05:13 -0700)
committerNoah Misch <noah@leadboat.com>
Mon, 11 May 2026 12:13:49 +0000 (05:13 -0700)
pg_rewind and pg_basebackup could be fed paths from rogue endpoints that
could overwrite the contents of the client when received, achieving path
traversal.

There were two areas in the tree that were sensitive to this problem:
- pg_basebackup, through the astreamer code, where no validation was
performed before building an output path when streaming tar data.  This
is an issue in v15 and newer versions.
- pg_rewind file operations for paths received through libpq, for all
the stable branches supported.

In order to address this problem, this commit adds a helper function in
path.c, that reuses path_is_relative_and_below_cwd() after applying
canonicalize_path().  This can be used to validate the paths received
from a connection point.  A path is considered invalid if any of the two
following conditions is satisfied:
- The path is absolute.
- The path includes a direct parent-directory reference.

Reported-by: XlabAI Team of Tencent Xuanwu Lab
Reported-by: Valery Gubanov <valerygubanov95@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 14
Security: CVE-2026-6475

src/bin/pg_basebackup/bbstreamer_file.c
src/bin/pg_basebackup/bbstreamer_tar.c
src/bin/pg_rewind/file_ops.c
src/include/port.h
src/port/path.c

index 2ccd772a40ccd792772c5e72e741d0fb7117167c..86b4bd5c4fa06b48b13c0cfaf220be7521f4c7ed 100644 (file)
@@ -215,6 +215,10 @@ bbstreamer_extractor_content(bbstreamer *streamer, bbstreamer_member *member,
                case BBSTREAMER_MEMBER_HEADER:
                        Assert(mystreamer->file == NULL);
 
+                       if (!path_is_safe_for_extraction(member->pathname))
+                               pg_fatal("tar member has unsafe path name: \"%s\"",
+                                                member->pathname);
+
                        /* Prepend basepath. */
                        snprintf(mystreamer->filename, sizeof(mystreamer->filename),
                                         "%s/%s", mystreamer->basepath, member->pathname);
@@ -233,6 +237,14 @@ bbstreamer_extractor_content(bbstreamer *streamer, bbstreamer_member *member,
 
                                if (mystreamer->link_map)
                                        linktarget = mystreamer->link_map(linktarget);
+
+                               if (!is_absolute_path(linktarget) &&
+                                       !path_is_safe_for_extraction(member->linktarget))
+                               {
+                                       pg_fatal("link target has unsafe path name: \"%s\"",
+                                                        member->linktarget);
+                               }
+
                                extract_link(mystreamer->filename, linktarget);
                        }
                        else
index d37db4d28a2cbe62c3829de20a877441e7ea8f2c..04a1450ebc1ebd12d2aff6ab860118d33d6cb025 100644 (file)
@@ -291,6 +291,9 @@ bbstreamer_tar_header(bbstreamer_tar_parser *mystreamer)
        strlcpy(member->pathname, &buffer[TAR_OFFSET_NAME], MAXPGPATH);
        if (member->pathname[0] == '\0')
                pg_fatal("tar member has empty name");
+       if (!path_is_safe_for_extraction(member->pathname))
+               pg_fatal("tar member has unsafe path name: \"%s\"",
+                                member->pathname);
        member->size = read_tar_number(&buffer[TAR_OFFSET_SIZE], 12);
        member->mode = read_tar_number(&buffer[TAR_OFFSET_MODE], 8);
        member->uid = read_tar_number(&buffer[TAR_OFFSET_UID], 8);
index ec64d054970cc6fad5cf7017046cc91e6fdb746c..771fc47e73d287acdb98b7e5d80f9855b81a167a 100644 (file)
@@ -48,6 +48,9 @@ open_target_file(const char *path, bool trunc)
 {
        int                     mode;
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target file path is unsafe for open: \"%s\"", path);
+
        if (dry_run)
                return;
 
@@ -188,6 +191,9 @@ remove_target_file(const char *path, bool missing_ok)
 {
        char            dstpath[MAXPGPATH];
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target file path is unsafe for removal: \"%s\"", path);
+
        if (dry_run)
                return;
 
@@ -208,6 +214,9 @@ truncate_target_file(const char *path, off_t newsize)
        char            dstpath[MAXPGPATH];
        int                     fd;
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target file path is unsafe for truncation: \"%s\"", path);
+
        if (dry_run)
                return;
 
@@ -230,6 +239,10 @@ create_target_dir(const char *path)
 {
        char            dstpath[MAXPGPATH];
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target directory path is unsafe for directory creation: \"%s\"",
+                                path);
+
        if (dry_run)
                return;
 
@@ -244,6 +257,10 @@ remove_target_dir(const char *path)
 {
        char            dstpath[MAXPGPATH];
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target directory path is unsafe for directory removal: \"%s\"",
+                                path);
+
        if (dry_run)
                return;
 
@@ -258,6 +275,9 @@ create_target_symlink(const char *path, const char *link)
 {
        char            dstpath[MAXPGPATH];
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target symlink path is unsafe for creation: \"%s\"", path);
+
        if (dry_run)
                return;
 
@@ -272,6 +292,9 @@ remove_target_symlink(const char *path)
 {
        char            dstpath[MAXPGPATH];
 
+       if (!path_is_safe_for_extraction(path))
+               pg_fatal("target symlink path is unsafe for removal: \"%s\"", path);
+
        if (dry_run)
                return;
 
index a90d0cf32f3bf83ec8011597f83b587f355c11bc..45640541f852004f1b1a821befb6b0bc59b7b543 100644 (file)
@@ -58,6 +58,7 @@ extern void make_native_path(char *filename);
 extern void cleanup_path(char *path);
 extern bool path_contains_parent_reference(const char *path);
 extern bool path_is_relative_and_below_cwd(const char *path);
+extern bool path_is_safe_for_extraction(const char *path);
 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
 extern char *make_absolute_path(const char *path);
 extern const char *get_progname(const char *argv0);
index d614f1ce0b655d50b77d09367ae0745eaceb46a9..0cd9eccb2fb2f6efd3fb1751a1b7f5622a7b733b 100644 (file)
@@ -626,6 +626,23 @@ path_is_relative_and_below_cwd(const char *path)
                return true;
 }
 
+/*
+ * Detect whether a path is safe for use during archive extraction.
+ *
+ * This applies canonicalize_path(), then it checks that the path does
+ * not contain any parent directory references.
+ */
+bool
+path_is_safe_for_extraction(const char *path)
+{
+       char            buf[MAXPGPATH];
+
+       strlcpy(buf, path, sizeof(buf));
+       canonicalize_path(buf);
+
+       return path_is_relative_and_below_cwd(buf);
+}
+
 /*
  * Detect whether path1 is a prefix of path2 (including equality).
  *