#9687·vision

Use a safe extraction filter for dataset tar archives

Author: malfetCreated Sep 29, 2026Updated Sep 29, 2026

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() calling tar.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 use filter="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.

Original report by @0xSaud (verbatim)

Summary

torchvision.datasets.utils.extract_archive() (and its caller download_and_extract_archive()) extract .tar/.tgz/.tbz archives by calling tarfile.extractall() without a member filter. On Python 3.10–3.13 the tarfile default is filter="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 .py on sys.path, cron/config files), which can escalate to remote code execution.

Details

Vulnerable sink — torchvision/datasets/utils.py, _extract_tar():

python
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.13

Reached 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, ...) → downloads url, then extract_archive(archive, extract_root)

Because no member filter is applied, tarfile on Python 3.10–3.13 follows .. components and absolute paths in member names and writes them relative to the process, not confined to to_path. (The sibling _extract_zip path relies on zipfile.extractall, which CPython path-sanitizes; the tar path is the issue.)

This is the classic unsafe-tarfile.extractall weakness (CVE-2007-4559 family).

PoC

_extract_tar copied verbatim and driven with a malicious tar (one member named ../pwned_OUTSIDE_target.txt):

python
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 escapes target.
  • 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_keys

Impact

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

python
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")