Skip to content

Pip seeder can rewrite a RECORD outside a reused environment #3625

Description

@gaborbernat

With --seeder pip on POSIX, reseeding an existing environment can replace a RECORD file outside that environment. PipInvoke._rewrite_launchers scans metadata left in site-packages and writes to a path named by a row of that metadata, rather than to the RECORD it read.

Affected code

At the reported commit 8e09166ddfbdaed3a5dbccc216d38ff1d3dbb4c2, _rewrite_launchers scans every distribution in the destination and selects seed package names. It then takes the first row ending in .dist-info/RECORD as the output path. write_file replaces an existing file or link. The same path remains at ebe06113f440731ab9c5bd6506a4793140532dc9.

Mechanism

The pip seeder uses --ignore-installed, which does not remove installed metadata before installing. The subsequent importlib.metadata.distributions scan therefore also sees a stale pip-0.dist-info in a reused destination. A launcher row that names a file in the environment's scripts directory sets changed. The output row is resolved through distribution.locate_file without checking that it names the metadata file being read or stays inside the destination.

This write can only target a path whose row ends in .dist-info/RECORD, subject to filesystem links and existing parent directories. In the reported reproduction it replaces another package's installation record; it does not write to an arbitrary filename.

Reproduction

The reporter ran the following files against 8e09166ddfbdaed3a5dbccc216d38ff1d3dbb4c2 with Python 3.14. Save the companion files under repro.d/, then run python3.14 repro from a checkout with that virtualenv version installed. The script uses /tmp/repro-pip-seeder-record as a disposable stand-in home and removes that directory at startup.

repro

#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
from pathlib import Path

home = Path("/tmp/repro-pip-seeder-record/home")
shutil.rmtree(home.parent, ignore_errors=True)
checkout = home / "checkout"

users_record = home / ".local/lib/python3.14/site-packages/requests-2.33.0.dist-info/RECORD"
users_record.parent.mkdir(parents=True)
users_record.write_text("requests/__init__.py,sha256=abc,5072\nrequests-2.33.0.dist-info/RECORD,,\n")

subprocess.run([sys.executable, "/tmp/repro.d/make_checkout.py", checkout], check=True)

before = users_record.read_text()
print(f"~/{users_record.relative_to(home)} before:\n{before}", flush=True)

subprocess.run(
    ["virtualenv", "--no-periodic-update", "--seeder", "pip", ".venv"],
    cwd=checkout,
    env={"HOME": str(home), "PATH": os.environ["PATH"]},
    check=True,
)

after = users_record.read_text()
print(f"\n~/{users_record.relative_to(home)} after:\n{after}")
planted_row = "../../../../Documents/thesis.tex,,"
print("BUG" if planted_row not in before.splitlines() and planted_row in after.splitlines() else "OK")

repro.d/make_checkout.py

#!/usr/bin/env python3
import shutil
import sys
from pathlib import Path

HERE = Path(__file__).parent
venv = Path(sys.argv[1]) / ".venv"

(venv / "bin").mkdir(parents=True)
shutil.copyfile(HERE / "tool", venv / "bin" / "tool")

for minor in range(9, 16):
    dist_info = venv / "lib" / f"python3.{minor}" / "site-packages" / "pip-0.dist-info"
    dist_info.mkdir(parents=True)
    shutil.copyfile(HERE / "pip-0.dist-info.METADATA", dist_info / "METADATA")
    shutil.copyfile(HERE / "pip-0.dist-info.RECORD", dist_info / "RECORD")

repro.d/tool

#!/bin/sh
'''exec' true
' '''

repro.d/pip-0.dist-info.METADATA

Metadata-Version: 2.1
Name: pip
Version: 0

repro.d/pip-0.dist-info.RECORD

../../../bin/tool,,
../../../../../.local/lib/python3.14/site-packages/requests-2.33.0.dist-info/RECORD,,
../../../../Documents/thesis.tex,,

Copy the companion directory to /tmp/repro.d before running:

cp -R repro.d /tmp/repro.d
python3.14 repro

The reporter's run exited 0, printed added seed packages: pip==0, pip==26.3, and ended with BUG. The outside file changed from:

requests/__init__.py,sha256=abc,5072
requests-2.33.0.dist-info/RECORD,,

to:

../../../bin/tool,sha256=…,95
../../../../../.local/lib/python3.14/site-packages/requests-2.33.0.dist-info/RECORD,,
../../../../Documents/thesis.tex,,

The expected behavior is to rewrite only the RECORD files installed by this seed run, leaving preexisting metadata and files outside the destination untouched.

Impact

This needs the non-default pip seeder on POSIX and an existing destination reused without --clear. Reproducibility is moderate because this setup and stale metadata are required. Real-world exposure is unlikely because the default app-data seeder and fresh destinations do not take this path.

The replaced file can corrupt another package's install record. Control of the reused destination already permits a .pth file in the same site-packages; Python runs executable .pth lines at interpreter startup, before this metadata rewrite. This finding adds no distinct access for whoever controls the destination.

Credits

Reported by @oss-scanner: ANT-2026-7ZNZMO8Q.

Activity

  1. added 4 commits that reference this issue on Nov 20, 2026
    ee754e4
    3330b76
    f1a0399
    d696d08
  2. added a commit that references this issue on Nov 20, 2026
    b491f05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions