Skip to content

Guard dataset tar extraction against path traversal (TarSlip) - #9590

Open
ousamabenyounes wants to merge 1 commit into
pytorch:mainfrom
ousamabenyounes:fix/issue-9517
Open

Guard dataset tar extraction against path traversal (TarSlip)#9590
ousamabenyounes wants to merge 1 commit into
pytorch:mainfrom
ousamabenyounes:fix/issue-9517

Conversation

@ousamabenyounes

Copy link
Copy Markdown

Fixes #9517

Problem

torchvision/datasets/utils.py::_extract_tar called tar.extractall(to_path) with no member validation (TarSlip / CVE-class path traversal). A crafted archive member named ../evil — or a symlink/hardlink pointing outside — writes outside the extraction root. download_and_extract_archive feeds a caller-provided URL straight into this sink, so a malicious dataset mirror can drop files anywhere the process can write.

Fix (root cause, tar-scoped)

  • Extract via CPython PEP 706 data filter (extractall(to_path, filter="data")), which rejects traversal, absolute paths, and escaping links. It is the default on 3.14 and available since 3.12 (backported to 3.10.12+/3.11.4+).
  • For interpreters predating the backport, a manual _reject_escaping_tar_members guard validates every member (and link target) against the realpath'd root before extraction writes anything.
  • _extract_zip is left untouched: zipfile.extractall already strips .. and absolute components (verified).

Test verification (RED → GREEN)

New parametrized test test_extract_tar_rejects_path_traversal (Python 3.12.13, CPU):

# RED (unmodified base):
Failed: DID NOT RAISE any of (TarError, RuntimeError)
FAILED test_extract_tar_rejects_path_traversal[../escaped.txt]
FAILED test_extract_tar_rejects_path_traversal[../../escaped.txt]
# GREEN (with fix): full test_datasets_utils.py
57 passed, 1 warning

(Independently reconfirmed the mechanism on this machine: extractall(filter="data") raises OutsideDestinationError on a ../escaped.txt member while a pre-3.12-default extractall writes it outside the root.)

Disclosure (per AI_POLICY.md): this change was prepared with AI assistance and was reviewed, tested, and is submitted by me. Happy to iterate on any feedback.

@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9590

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the cla signed label Aug 7, 2026
Fixes pytorch#9517

torchvision.datasets.utils._extract_tar called tar.extractall() with no
member validation, so a crafted archive (member name '../evil') could
write files outside the extraction root (TarSlip / CVE-class path
traversal). download_and_extract_archive feeds a caller-provided URL
straight into this sink.

Fix: extract tar archives through CPython's PEP 706 'data' filter, which
rejects path traversal, absolute paths, and links escaping the
destination. A manual containment guard covers Python patches predating
the backported filter (<3.10.12/<3.11.4). Zip extraction is unaffected:
zipfile.extractall already strips '..' and absolute components.

Regression test: a tar member named '../escaped.txt' must not write
outside to_path and must raise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path traversal in TorchVision dataset archive extraction

1 participant