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: