|
| 1 | +"""Sandbox an untrusted child tree with kernel-enforced resource limits. |
| 2 | +
|
| 3 | +The differentiator versus a plain subprocess wrapper: a ``ProcessGroup`` can cap |
| 4 | +a whole tree's memory, process count, and CPU — enforced by the Windows Job |
| 5 | +Object or a Linux cgroup v2. We also lock the command itself down (empty |
| 6 | +environment, bounded captured output, die-with-parent) so a misbehaving tool |
| 7 | +cannot run away with the machine. |
| 8 | +
|
| 9 | +Kernel resource limits need privileges the environment may not grant: inside a |
| 10 | +container, a systemd user session, or a non-root cgroup the kernel forbids them, |
| 11 | +and macOS (a POSIX process group) has no equivalent. processkit is honest about |
| 12 | +this — it raises rather than silently ignoring the cap — so this example catches |
| 13 | +that and degrades to "contained, but uncapped", staying runnable anywhere. |
| 14 | +
|
| 15 | +Run it: python examples/04_sandbox_resource_limits.py |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import sys |
| 21 | + |
| 22 | +from processkit import Command, ProcessGroup, ResourceLimit, Unsupported |
| 23 | + |
| 24 | +_MiB = 1024 * 1024 |
| 25 | + |
| 26 | +# A short-lived stand-in for an untrusted tool doing a little work. |
| 27 | +_TOOL = "import time; time.sleep(0.1)" |
| 28 | + |
| 29 | + |
| 30 | +def _locked_down_tool() -> Command: |
| 31 | + """An untrusted command tied down independently of the group's limits: an |
| 32 | + empty environment (only PATH allow-listed), a bounded captured output, and |
| 33 | + kill-on-parent-death so it cannot outlive us even without explicit teardown.""" |
| 34 | + return ( |
| 35 | + Command(sys.executable, ["-c", _TOOL]) |
| 36 | + .env_clear() |
| 37 | + .inherit_env(["PATH"]) |
| 38 | + .kill_on_parent_death() |
| 39 | + .output_limit(max_bytes=8 * _MiB) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _run( |
| 44 | + *, |
| 45 | + max_memory: int | None = None, |
| 46 | + max_processes: int | None = None, |
| 47 | + cpu_quota: float | None = None, |
| 48 | +) -> None: |
| 49 | + with ProcessGroup( |
| 50 | + max_memory=max_memory, |
| 51 | + max_processes=max_processes, |
| 52 | + cpu_quota=cpu_quota, |
| 53 | + ) as group: |
| 54 | + group.start(_locked_down_tool()) |
| 55 | + print(f" mechanism : {group.mechanism}") |
| 56 | + # Live usage stats are a bonus, not the point — some mechanisms (a POSIX |
| 57 | + # process group) can't report them, so don't let that mask the sandbox. |
| 58 | + try: |
| 59 | + stats = group.stats() |
| 60 | + print(f" active processes : {stats.active_process_count}") |
| 61 | + print(f" peak memory (bytes) : {stats.peak_memory_bytes}") |
| 62 | + except Unsupported: |
| 63 | + print(" usage stats : unavailable on this platform") |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + try: |
| 68 | + _run(max_memory=512 * _MiB, max_processes=64, cpu_quota=1.0) |
| 69 | + print("ran the tool under kernel-enforced memory / process / CPU limits.") |
| 70 | + except (ResourceLimit, Unsupported) as exc: |
| 71 | + print(f"kernel resource limits are not permitted here: {exc}") |
| 72 | + print("(typical in containers / non-root cgroups / macOS) - running uncapped.") |
| 73 | + _run() |
| 74 | + print("ran the tool contained, but without resource caps.") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments