Use a safe extraction filter for dataset tar archives
Original report by @0xSaud (verbatim)AI-assisted issue conversion (Codex). Original report and reproduction by @0xSaud; all credit belongs to the original reporter.
Source: https://github.com/pytorch/pytorch/security/advisories/GHSA-m4rq-h4j7-8887
The reporter identifies
_extract_tar()callingtar.extractall(to_path)without an explicit extraction filter. On Python environments whose default permits fully trusted extraction, crafted member paths can write outside the destination. Suggested hardening: explicitly usefilter="data", accounting for supported Python versions. The original report below contains truncated text and an incomplete reproduction; it is preserved as submitted.This conversion has not independently reproduced or verified the report.
Summary
torchvision.datasets.utils.extract_archive()(and its callerdownload_and_extract_archive()) extract.tar/.tgz/.tbzarchives by callingtarfile.extractall()without a member filter. On Python 3.10–3.13 thetarfiledefault isfilter="fully_trusted", so a tar member whose name contains..(or an absolute path) is written outside the caller-specified destination directory. An application that extracts an attacker-influenced archive through this public API can have arbitrary files created/overwritten outside the target directory (e.g.~/.ssh/authorized_keys, a.pyonsys.path, cron/config files), which can escalate to remote code execution.Details
Vulnerable sink —
torchvision/datasets/utils.py,_extract_tar():def _extract_tar(from_path, to_path, compression): with tarfile.open(from_path, f"r:{compression[1:]}" if compression else "r") as tar: tar.extractall(to_path) # no filter= -> unsafe on Python <= 3.13Reached from the documented public API:
extract_archive(from_path, to_path=None, ...)→_ARCHIVE_EXTRACTORS[".tar"] == _extract_tar→tar.extractall(to_path)download_and_extract_archive(url, download_root, extract_root=None, ...)→ downloadsurl, thenextract_archive(archive, extract_root)Because no member filter is applied,
tarfileon Python 3.10–3.13 follows..components and absolute paths in member names and writes them relative to the process, not confined toto_path. (The sibling_extract_zippath relies onzipfile.extractall, which CPython path-sanitizes; the tar path is the issue.)This is the classic unsafe-
tarfile.extractallweakness (CVE-2007-4559 family).PoC
_extract_tarcopied verbatim and driven with a malicious tar (one member named../pwned_OUTSIDE_target.txt):import os, io, tarfile, tempfile def _extract_tar(from_path, to_path, compression): # verbatim from torchvision with tarfile.open(from_path, f"r:{compression[1:]}" if compression else "r") as tar: tar.extractall(to_path) work = tempfile.mkdtemp(); target = os.path.join(work, "extract_here"); os.makedirs(target) tarp = os.path.join(work, "malicious.tar") with tarfile.open(tarp, "w") as t: data = b"owned\n"; ti = tarfile.TarInfo("../pwned_OUTSIDE_target.txt"); ti.size = len t.addfile(ti, io.BytesIO(data)) _extract_tar(tarp, target, None) print("written outside target:", os.path.exists(os.path.join(work, "pwned_OUTSIDE_target.
- On Python 3.10–3.13: prints
written outside target: True— the file escapestarget.- On Python 3.14+: PEP 706 makes
filter="data"the default, so extraction is refused (`untime-mitigated.A real attacker uses a member name such as
../../../../home/<user>/.ssh/authorized_keysImpact
Directory traversal / arbitrary file write (CWE-22). Affects any application that passes e or URL to
extract_archive()/download_and_extract_archive()while running on Python 3.10–3.13 (e.g. services that accept user-supplied dataset bundles or community model/chen's own dataset flows use hardcoded, integrity-checked mirrors and are notattacker-controlled by default. Arbitrary file write can lead to remote code execution.Suggested fix
Pass the hardening filter explicitly (available on Python 3.10+, also silences the 3.12/3
def _extract_tar(from_path, to_path, compression): with tarfile.open(from_path, f"r:{compression[1:]}" if compression else "r") as tar: tar.extractall(to_path, filter="data")
Source: pytorch/vision