Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/pytest_prairielearn_grader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions tests/scenario_root/test_privilege_drop/expected_outcome.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
10 changes: 10 additions & 0 deletions tests/scenario_root/test_privilege_drop/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
1 change: 1 addition & 0 deletions tests/scenario_root/test_privilege_drop/student_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
current_euid = os.geteuid()
current_gid = os.getgid()
current_egid = os.getegid()
current_groups = os.getgroups()
27 changes: 27 additions & 0 deletions tests/test_drop_privileges.py
Original file line number Diff line number Diff line change
@@ -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")
Loading