From 38891f5a77798394f3e287c14e74e91a72ad5602 Mon Sep 17 00:00:00 2001 From: Eliot Robson Date: Wed, 18 Nov 2026 17:12:27 -0500 Subject: [PATCH] Drop root's supplementary groups when sandboxing student code drop_privileges set the sandbox uid and gid but left the supplementary groups inherited from root in place, so the sandbox user still belonged to group 0 and others. Group permissions therefore didn't restrict it. Clear the supplementary groups with setgroups([]) while still root, before setuid. Co-Authored-By: Claude Opus 4.8 --- src/pytest_prairielearn_grader/utils.py | 9 +++++-- .../test_privilege_drop/expected_outcome.json | 9 +++++++ .../test_privilege_drop/scenario.py | 10 +++++++ .../test_privilege_drop/student_code.py | 1 + tests/test_drop_privileges.py | 27 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/test_drop_privileges.py diff --git a/src/pytest_prairielearn_grader/utils.py b/src/pytest_prairielearn_grader/utils.py index 04da00d..b096d49 100644 --- a/src/pytest_prairielearn_grader/utils.py +++ b/src/pytest_prairielearn_grader/utils.py @@ -380,12 +380,17 @@ def drop_privileges(user_name: str) -> None: except KeyError: raise ValueError(f"User '{user_name}' not found.") from None - # 1. Set the real and effective GID + # 1. Drop the supplementary groups inherited from root. Without this the sandbox keeps root's + # groups (including group 0), so group permissions wouldn't restrict it. This must happen while + # still root, before setuid. + os.setgroups([]) + + # 2. Set the real and effective GID # NOTE: Set GID before UID, as changing UID may restrict GID changes os.setgid(target_gid) os.setegid(target_gid) - # 2. Set the real and effective UID + # 3. Set the real and effective UID os.setuid(target_uid) os.seteuid(target_uid) diff --git a/tests/scenario_root/test_privilege_drop/expected_outcome.json b/tests/scenario_root/test_privilege_drop/expected_outcome.json index cc083eb..84b747e 100644 --- a/tests/scenario_root/test_privilege_drop/expected_outcome.json +++ b/tests/scenario_root/test_privilege_drop/expected_outcome.json @@ -28,6 +28,15 @@ "points": 1.0, "test_id": "test_privilege_drop.py::test_subprocess_group_ids_match[student_code]", "outcome": "passed" + }, + { + "max_points": 1, + "message": "", + "name": "test_supplementary_groups_dropped", + "points_frac": 1.0, + "points": 1.0, + "test_id": "test_privilege_drop.py::test_subprocess_supplementary_groups_dropped[student_code]", + "outcome": "passed" } ] } diff --git a/tests/scenario_root/test_privilege_drop/scenario.py b/tests/scenario_root/test_privilege_drop/scenario.py index 7d212d4..dbb906d 100644 --- a/tests/scenario_root/test_privilege_drop/scenario.py +++ b/tests/scenario_root/test_privilege_drop/scenario.py @@ -66,3 +66,13 @@ def test_subprocess_group_ids_match(sandbox: StudentFixture) -> None: assert real_gid == effective_gid, f"Real GID ({real_gid}) should match effective GID ({effective_gid})" assert isinstance(real_gid, int) assert real_gid >= 0 + + +@pytest.mark.skipif(sys.platform == "win32", reason="Privilege dropping not supported on Windows") +@pytest.mark.grading_data(name="test_supplementary_groups_dropped", points=1) +def test_subprocess_supplementary_groups_dropped(sandbox: StudentFixture) -> None: + """When privileges are dropped, root's supplementary groups must not remain.""" + groups = sandbox.query("current_groups") + assert isinstance(groups, list) + if sandbox.worker_username is not None: + assert 0 not in groups, f"Root's group 0 should have been dropped, but groups are {groups}" diff --git a/tests/scenario_root/test_privilege_drop/student_code.py b/tests/scenario_root/test_privilege_drop/student_code.py index 6f8890b..53c5f6a 100644 --- a/tests/scenario_root/test_privilege_drop/student_code.py +++ b/tests/scenario_root/test_privilege_drop/student_code.py @@ -7,3 +7,4 @@ current_euid = os.geteuid() current_gid = os.getgid() current_egid = os.getegid() +current_groups = os.getgroups() diff --git a/tests/test_drop_privileges.py b/tests/test_drop_privileges.py new file mode 100644 index 0000000..af5fbdf --- /dev/null +++ b/tests/test_drop_privileges.py @@ -0,0 +1,27 @@ +import sys + +import pytest + +if sys.platform == "win32": + pytest.skip("Privilege dropping is not supported on Windows", allow_module_level=True) + +import os +import pwd +from types import SimpleNamespace + +from pytest_prairielearn_grader.utils import drop_privileges + + +def test_drops_supplementary_groups_before_setuid(monkeypatch: pytest.MonkeyPatch) -> None: + calls: list[tuple[str, object]] = [] + + monkeypatch.setattr(pwd, "getpwnam", lambda name: SimpleNamespace(pw_uid=1000, pw_gid=1000)) + for name in ("setgroups", "setgid", "setegid", "setuid", "seteuid"): + monkeypatch.setattr(os, name, lambda value, name=name: calls.append((name, value))) + + drop_privileges("ag") + + assert ("setgroups", []) in calls + order = [name for name, _ in calls] + assert order.index("setgroups") < order.index("setuid") + assert order.index("setgroups") < order.index("setgid")