CSE Roadmap

Core tier · about 9 hours · 13 parts

Part 4 of 13 · 3 min read

Limits and supervision

A rule posted on a wall ("no running") stops nobody by itself; a rule a teacher actually enforces does. The kernel's resource limits are the posted rule; some of them need a second mechanism to actually be enforced.

In plain words: a resource limit is only as real as the thing checking it — some limits the kernel enforces for you, and some you have to enforce yourself.

Time. The run is stopped after a wall-clock limit (1.5 seconds in the examples) and the result says timed_out, because "it finished" and "it was stopped" must look different to the reader. The kill reaches the whole tree: the process, its children, and everything in its session.

Processor time, memory, file size, open files. These are kernel resource limits set in the child before it starts. A program that allocates past 256 MB gets a MemoryError in Python (the limit is on address space, and the result sets an oom flag from the error text, which is a heuristic). A program that writes past 10 MB gets an error from the write. A program that opens more than 64 files gets "Too many open files".

A limit that does nothing for the user who matters. The obvious limit on the number of processes (RLIMIT_NPROC) is not enforced for root, and a sandbox often runs as root. It was tried, even inside a user namespace: a program asked to start 30 processes started all 30 with the limit set to 5. So the example enforces the cap with a monitor that polls instead of trusting the kernel:

# from code/mini-sandbox/runner.py
while p.poll() is None:
    now = time.monotonic() - t0
    snap = procs.snapshot()
    n = count_user_procs(p.pid, snap)
    res.peak_procs = max(res.peak_procs, n)
    if now > policy.wall:
        reason = "wall"
    elif policy.max_procs and n > policy.max_procs and "no_proc_monitor" not in bugs:
        reason = "procs"
    if reason:
        _kill(p, snap, bugs)
        break
    time.sleep(0.005)

Read it as: every 5 milliseconds the loop takes a fresh snapshot of /proc, counts the processes under this run, and kills the whole tree the moment the count passes the policy's cap. The kernel's own RLIMIT_NPROC is not in this loop at all, because for root it does nothing.

Resource limit, polling monitor

A resource limit (an rlimit) is a kernel-enforced ceiling set before a process starts. A polling monitor is code that repeatedly checks a condition itself, used where the kernel's own enforcement does not apply. Example: RLIMIT_NPROC set to 5 let a root process start 30 anyway; the polling monitor above caught and killed it at the 16th.

An unexpected quirk. A busy loop under a 2-second CPU limit stopped after 2.0 seconds with no namespace, and after 3.0 seconds inside a process-id namespace. The reason: the first process in a PID namespace ignores signals whose default action is to terminate it, so the soft limit's signal does nothing, and only the hard limit (one second later, which cannot be ignored) stops it. The sandbox sets the hard limit one second above the soft one. If you set only the soft limit, your CPU limit would not work in that configuration.

Output. The harness never holds more than 8 MB of any program's output, and the policy returns at most 8,000 characters of each stream: the first 60 percent and the last 40 percent, with a marker saying how many bytes were left out. The end matters because the end of a Python error is the line that names the problem. A defect that keeps only the beginning is one of the planted ones.

Leftovers. When a run ends normally, a child it started may still be running. The example sweeps the run's session after every run. That is not enough, because a program can start a new session (a detached "daemon" process) and escape the sweep, which is why the next layer exists.