From 2278f66f56ba1ddd80da2c64aa7593d0bd6ef234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 29 Nov 2026 15:13:33 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(discovery):=20ignore=20forei?= =?UTF-8?q?gn=20stdlib=20landmark?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An interpreter symlink stayed unresolved whenever the directory beside it held an existing lib/pythonX.Y/os.py, whoever owned that file. A venv built from the link records the link's directory as home, and CPython then imports the standard library from there. A file another user created in a shared parent such as /tmp was enough to switch the resolution off. The landmark now counts only when lib, lib/pythonX.Y and os.py are each owned by root, the effective user or the owner of the link's directory. They are checked with lstat, so a symlink owned by another user cannot pass on the owner of its target. Platforms without geteuid keep the previous check. Fixes #283. Reported by oss-scanner as ANT-2026-UFV4PHZZ. --- docs/changelog/283.bugfix.rst | 2 ++ src/python_discovery/_py_info_collect.py | 16 +++++++++++--- tests/test_py_info_collect.py | 27 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 docs/changelog/283.bugfix.rst diff --git a/docs/changelog/283.bugfix.rst b/docs/changelog/283.bugfix.rst new file mode 100644 index 0000000..d8c0673 --- /dev/null +++ b/docs/changelog/283.bugfix.rst @@ -0,0 +1,2 @@ +An interpreter symlink is now resolved to the real interpreter when the standard library beside it is owned by another +user. Environments built from ``system_executable`` no longer load a standard library that another local user controls. diff --git a/src/python_discovery/_py_info_collect.py b/src/python_discovery/_py_info_collect.py index ed415a7..fcd65bd 100644 --- a/src/python_discovery/_py_info_collect.py +++ b/src/python_discovery/_py_info_collect.py @@ -175,9 +175,19 @@ def _resolve_executable_symlink(self, path): @staticmethod def _stdlib_landmark_exists(dir_path): lib_name = os.path.basename(os.path.dirname(os.__file__)) - return any( - os.path.exists(os.path.join(dir_path, os.pardir, lib, lib_name, "os.py")) for lib in ("lib", "lib64") - ) + stdlibs = [os.path.join(dir_path, os.pardir, lib, lib_name) for lib in ("lib", "lib64")] + if not hasattr(os, "geteuid"): # pragma: win32 cover + return any(os.path.exists(os.path.join(stdlib, "os.py")) for stdlib in stdlibs) + # CPython loads the stdlib from a home recorded as dir_path, so a landmark another user planted in a shared + # parent must not count; lstat, as a symlink owned by another user can be repointed later. + trusted = {0, os.geteuid(), os.stat(dir_path).st_uid} + for stdlib in stdlibs: + landmark = os.path.join(stdlib, "os.py") + if not os.path.exists(landmark): + continue + if {os.lstat(part).st_uid for part in (os.path.dirname(stdlib), stdlib, landmark)} <= trusted: + return True + return False def _try_posix_versioned_executable(self, base_executable): major, minor = self.version_info.major, self.version_info.minor diff --git a/tests/test_py_info_collect.py b/tests/test_py_info_collect.py index c9856fd..045be40 100644 --- a/tests/test_py_info_collect.py +++ b/tests/test_py_info_collect.py @@ -3,6 +3,7 @@ import ast import json import os +import stat import sys import sysconfig from itertools import takewhile @@ -207,6 +208,32 @@ def test_system_executable_resolves_executable_symlink_only( assert PythonInfo().system_executable == str(expected) +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only") +@pytest.mark.usefixtures("no_framework", "not_a_venv") +@pytest.mark.parametrize( + "foreign", + [ + pytest.param("lib", id="lib"), + pytest.param("lib/{stdlib}", id="stdlib"), + pytest.param("lib/{stdlib}/os.py", id="landmark"), + ], +) +def test_system_executable_ignores_foreign_stdlib_landmark(tmp_path: Path, mocker: MockerFixture, foreign: str) -> None: + link, _ = _layout_stdlib_landmark(tmp_path) + foreign_path = str(tmp_path / "alias" / foreign.format(stdlib=Path(os.__file__).parent.name)) + real_lstat = os.lstat + + def lstat_with_foreign_owner(path: str) -> os.stat_result: + status = list(real_lstat(path)) + if os.path.normpath(path) == foreign_path: + status[stat.ST_UID] = os.geteuid() + 1 + return os.stat_result(status) + + mocker.patch.object(os, "lstat", side_effect=lstat_with_foreign_owner, autospec=True) + mocker.patch.object(sys, "executable", str(link)) + assert PythonInfo().system_executable == str(tmp_path / "install" / "bin" / "python3.12") + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX only") @pytest.mark.usefixtures("not_a_venv") def test_system_executable_framework_symlink_kept(tmp_path: Path, mocker: MockerFixture) -> None: