Skip to content

Symlink resolution stops at a standard library owned by another user #283

Done

Description

@gaborbernat

system_executable keeps an interpreter symlink unresolved when <link dir>/../lib/pythonX.Y/os.py exists, whoever owns that file. For a link in a subdirectory of a shared directory such as /tmp, another local user can create that file and stop the link from resolving to the real interpreter. A venv built from system_exe then records the link's directory as its home, and CPython looks for the standard library in the directories above it, including the one the other user controls.

Affected code

_resolve_executable_symlink returns the link as soon as _stdlib_landmark_exists finds the file. That function checks only os.path.exists. The lines are unchanged in 1.9.8 (35b7a39).

Mechanism

The early return came in #87 (fixing #86, released in 1.4.2). It keeps distribution aliases such as Debian's /usr/bin/python3 instead of rewriting them to the versioned binary. The check it uses, <link dir>/../lib(64)/pythonX.Y/os.py, does not look at who owns that path. For a link at /tmp/tmp.XXXX/python3 the path is /tmp/lib/python3.11/os.py, which any local user can create in advance if /tmp/lib does not exist yet.

Once the link is kept:

  1. python -m venv takes home from the directory of sys._base_executable, which for an interpreter started through a link is the link itself, and writes it to pyvenv.cfg.
  2. When the environment starts, CPython's getpath reads home and searches it and every directory above it (search_up) for lib/pythonX.Y/os.py. It takes the first match as the standard library.

When the link resolves, home is /usr/bin and the directories above /tmp/tmp.XXXX play no part.

Running /tmp/tmp.XXXX/python3 -m venv directly, without python-discovery, records the same home. python-discovery 1.0.0 to 1.4.0 did not resolve such links either; #85 added the resolution in 1.4.1. So the layout is exposed without python-discovery, and the resolution protects it only until another user creates one file.

Reproduction

Run as root in a container built from the reported commit, then again with the 1.9.8 source mounted. Each lookup runs in a new process so that the in-process cache does not carry the first result over.

import os
import subprocess
import sys
import tempfile
from pathlib import Path

LOOKUP = "from python_discovery import get_interpreter; print(get_interpreter('3.11', env={'PATH': %r}).system_executable)"

shared = Path(tempfile.mkdtemp())
shared.chmod(0o1777)
link_dir = Path(tempfile.mkdtemp(dir=shared))
(link_dir / "python3").symlink_to("/usr/bin/python3.11")
lookup = [sys.executable, "-c", LOOKUP % str(link_dir)]

print("link:  ", link_dir / "python3", "->", os.readlink(link_dir / "python3"))
print("before:", subprocess.check_output(lookup, text=True).strip())
landmark = shared / "lib/python3.11/os.py"
as_nobody = ["setpriv", "--reuid=65534", "--regid=65534", "--clear-groups"]
subprocess.run([*as_nobody, "sh", "-c", f'mkdir -p "{landmark.parent}" && touch "{landmark}"'], check=True)
print("landmark:", landmark, "| owner uid", landmark.stat().st_uid, "| caller uid", os.getuid())
print("after: ", subprocess.check_output(lookup, text=True).strip())
docker run --rm --network=none -u 0 -w /src -v "$PWD/landmark.py:/tmp/landmark.py:ro" \
  -v "$PWD/src:/src/src:ro" <image with python-discovery installed editable from /src> python /tmp/landmark.py

At 4a843030c8092a6a52b0a29069d6859aeadf8669:

link:   /tmp/tmps9n4jx9p/tmpe_v_gwp4/python3 -> /usr/bin/python3.11
before: /usr/bin/python3.11
landmark: /tmp/tmps9n4jx9p/lib/python3.11/os.py | owner uid 65534 | caller uid 0
after:  /tmp/tmps9n4jx9p/tmpe_v_gwp4/python3

At 35b7a3912b5655af8a31e3bad41b3abd4f099f40 (1.9.8):

link:   /tmp/tmp7zq3fnzl/tmpuigpt5jd/python3 -> /usr/bin/python3.11
before: /usr/bin/python3.11
landmark: /tmp/tmp7zq3fnzl/lib/python3.11/os.py | owner uid 65534 | caller uid 0
after:  /tmp/tmp7zq3fnzl/tmpuigpt5jd/python3

Expected: after stays /usr/bin/python3.11. A landmark should only count when lib, lib/pythonX.Y and os.py belong to root, the caller, or the owner of the link's directory; otherwise the link should resolve as it does with no landmark.

Impact

Callers that build environments from system_exe hit this when the interpreter link sits one directory below a directory other users can write to, and another user creates <that directory>/lib/pythonX.Y/os.py before the first lookup that is not served from the cache. A result cached before then keeps the resolved path.

  • Reproducibility: moderate. It needs that specific layout and the file in place before the first uncached lookup; once both hold, one command shows it.
  • Real-world exposure: unlikely. The interpreters that virtualenv, tox and hatch find through PATH, pyenv or uv sit in directories such as /usr/bin, ~/.pyenv or /opt, not one level below /tmp. The same layout is exposed through plain python -m venv, so the fix narrows an exposure the layout already has.

Credits

Reported by oss-scanner as ANT-2026-UFV4PHZZ.

Activity

  1. added
    bug
    on Nov 29, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions