← Index berkoc · research
High awaiting-publish

pdm-project/pdm: entry-point path traversal in InstallDestination.write_to_fs (arbitrary file write)

PDM's InstallDestination.write_to_fs overrides the upstream installer library's WheelDestination and drops the path-containment check. A malicious wheel with a ../.. console-script name writes an executable outside the venv, or clobbers an existing in-prefix entry point such as bin/pip.

Vendor
pdm-project/pdm · pypi
CVE
CVE-2026-47757
GHSA
GHSA-wrjc-q82x-3457
CWE
CWE-22
CVSS
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H (8.8)
Disclosed

Background

pdm-project/pdm is a Python package and dependency manager. Its wheel installer is built on top of the upstream installer library and is the materialisation path behind pdm install, pdm sync, and pdm add. When a wheel is installed, every console_scripts entry point declared in its entry_points.txt is turned into an executable wrapper script under the environment's bin/ directory. The name of that wrapper comes straight from the wheel metadata. It is never validated against path traversal.

The override site

The upstream installer.destinations.SchemeDictionaryDestination enforces an is_relative_to containment check before it writes a file. PDM replaces that method with its own implementation to add PDM-specific behaviour (race-resilient .pth rename, copy/symlink/hardlink dispatch) and, in doing so, drops the safeguard. InstallDestination.write_to_fs at src/pdm/installers/installers.py:134-164, verbatim at the audited commit:

def write_to_fs(self, scheme: Scheme, path: str, stream: BinaryIO, is_executable: bool) -> RecordEntry:
    from installer.records import Hash
    from installer.utils import copyfileobj_with_hashing

    target_path = os.path.join(self.scheme_dict[scheme], path)
    if os.path.exists(target_path):
        os.unlink(target_path)

    os.makedirs(os.path.dirname(target_path), exist_ok=True)
    ...

The path argument comes from installer.Script.generate() via parse_entrypoints() and is derived directly from the wheel's entry_points.txt. There is no rejection of .., /, \, null bytes, a leading ., or an absolute path. os.path.join keeps .. components literal, and os.makedirs(..., exist_ok=True) then walks the traversal chain and creates any intermediate directories along the way. A separate os.path.abspath(self.scheme_dict[file_scheme]) call normalises only the scheme root, not the user-controlled path, so the traversal is unaffected.

Reproducer

The cleanest pivot is clobbering an existing in-prefix entry point. A minimal valid wheel with the following entry_points.txt:

[console_scripts]
../../evil-script = innocuous_pkg:main

installed via pdm add ./evil-0.0.1-py3-none-any.whl, materialises the script at <.venv>/bin/../../evil-script, which the kernel resolves to one directory above bin/. The script body is the rendered wrapper template with attacker-controlled module and import-name substitutions, executable bit set.

Two further variants follow from the same primitive. An out-of-prefix write lands wherever the parent directory already exists or can be created:

[console_scripts]
../../.bashrc.d/99-evil = innocuous_pkg:main

And because os.path.join discards the left side when the right side is absolute, an absolute path writes anywhere the process can:

[console_scripts]
/tmp/PWN = innocuous_pkg:main

The analysis is static. I did not assemble a .whl and run PDM end to end. The chain was traced at the pinned commit 904e1dded8f36fc21373db5b731a5801a6a1f2d9 (HEAD of main, 2026-05-14). The same primitive in the sibling pip + distlib case has a live end-to-end PoC, a .whl that writes /tmp/evil-script at mode 0o775, using the same wheel format and the same installer entry-point handling library that PDM's override exposes.

Impact

Arbitrary executable-file write with attacker-controlled Python content during pdm install, pdm sync, or pdm add of any wheel from any configured index. This is realistic supply-chain RCE. The cleanest pivot is clobbering an existing in-prefix entry point (bin/pip, pytest, ruff, aws, dvc, jupyter) whose next invocation runs the attacker's Python with the user's environment. Out-of-prefix writes succeed wherever the parent directory pre-exists or where os.makedirs(parent, exist_ok=True) can create it without a permission error, which puts ~/.bashrc.d/, ~/.config/, and ~/.local/bin/ within reach on most distributions.

The threat model is any PyPI maintainer, or anyone who compromises a maintainer's PyPI token, of any dependency the victim's pyproject.toml resolves. CI runners that run pdm install in Docker builds often execute as root, which magnifies the blast radius.

CVSS 3.1 AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H = 8.8 High, peer-aligned with the pip submission. An S:C scope-change argument toward ~9.6 Critical is defensible for out-of-prefix writes.

The fix

Validate the resolved path stays within the scheme directory before the write:

scheme_root = os.path.realpath(self.scheme_dict[scheme])
target_path = os.path.realpath(os.path.join(self.scheme_dict[scheme], path))

if os.path.commonpath([scheme_root, target_path]) != scheme_root:
    raise InstallerError(
        f"installer refuses to write outside the {scheme!r} scheme: "
        f"{path!r} -> {target_path!r}"
    )

Defence in depth: also validate the path argument at function entry against .., /, \, null bytes, leading dots, and absolute paths. python-poetry/poetry already defends against this class with a Path.is_relative_to() check in installers/wheel_installer.py, a one-paragraph patch that PDM can apply nearly verbatim.

Class-wide pattern

PDM is the sixth confirmed implementation of the same class: auto-generated console-script wrappers whose name field is derived from a package-metadata string and never validated against path traversal. The siblings, reported under coordinated disclosure across PyPA, Astral, Anaconda, Prefix.dev, and Quansight, are pypa/pip (vendored distlib), astral-sh/uv, conda/conda, conda/rattler, and mamba-org/mamba (libmamba). The prior art in the same class is GHSA-pjjw-68hj-v9mw (uv uninstall-side path traversal, fixed April 2026). python-poetry/poetry is the defended reference implementation; pypa/hatch and prefix-dev/pixi inherit the bug from the installers they delegate to.

Timeline

  • 2026-05-14: PGP-signed advisory submitted to pdm-project/pdm as a private GitHub Security Advisory, part of the six-codebase coordinated disclosure.
  • 2026-05-20: CVE-2026-47757 issued by the GitHub CNA after compliance review. Reporter credit on berkant-koc. Awaiting maintainer publication of GHSA-wrjc-q82x-3457.

References

  • CVE-2026-47757: https://www.cve.org/CVERecord?id=CVE-2026-47757
  • GHSA-wrjc-q82x-3457: https://github.com/pdm-project/pdm/security/advisories/GHSA-wrjc-q82x-3457
  • Pinned audit commit: 904e1dded8f36fc21373db5b731a5801a6a1f2d9
  • Defended reference implementation: python-poetry/poetry, installers/wheel_installer.py Path.is_relative_to() check.
  • Prior art: GHSA-pjjw-68hj-v9mw (uv uninstall-side path traversal).
  • CWE-22: Improper Limitation of a Pathname to a Restricted Directory ("Path Traversal").