notes

Per-process resource quotas

The security audit's one remaining userspace-reachable attack was resource exhaustion. A process that spawns children without limit, or spawns children that block forever, makes the kernel allocate a Thread, a 16 KiB kernel stack, and an address space per child, with no bound. untyped memory bounds a process's pages as of milestone 11 (untyped memory, and the number that proves the kernel stops allocating), but not the kernel objects behind a spawn. This is the bound for those.

The idea, and why it needs no bookkeeping

A spawner is given a quota: at most N children alive at once. The clever part is when a slot comes back. It is not a timer, not a scan, not a reference count the kernel has to maintain. The slot is a value that lives inside the child's Thread, and Rust's ownership returns it at exactly the right moment:

pub struct QuotaToken(&'static AtomicU32);
impl Drop for QuotaToken {
    fn drop(&mut self) { self.0.fetch_add(1, Ordering::Relaxed); }
}

Reserving a slot is one atomic decrement (a compare-exchange loop that never dips below zero). The QuotaToken holding that reservation is a field of the spawned Thread. When the reaper drops a finished thread, the token drops with it and the slot returns. So:

This is the same ownership-does-the-work pattern the KernelStack and AddressSpace already use (the reaper's drop frees them). The quota just adds one more thing that rides the thread's lifetime.

What it bounds

sched::spawn_with_quota(&BUDGET, closure) returns None when the budget is spent or the kernel is out of memory: the caller cannot tell the two apart and does not need to. The shell's process service used a budget of eight (shell_service::SPAWN_QUOTA) and, on None, reported "could not spawn a process" to the shell rather than panicking (the audit's other spawn finding). So a spawn flood, or a pile of workers that block and never exit, was capped at eight live children: eight threads, eight stacks, eight address spaces, and no more. Kernel memory from spawning was bounded, per spawner, for the first time.

Where this stands today

The mechanism is in the kernel and nothing calls it. Its one caller was the kernel-wired shell_service's spawn service, which DECISIONS §28 (SMP placement) retired; Thread.quota is None on every thread that exists. That is not a regression, because the bound is elsewhere: a userspace process spawns out of its own untyped budget (§10, §16), so the budget is the quota and retyping enforces it instead of a counter. What remains uncovered is a kernel thread spawned in a loop by something untrusted, and nothing does that.

spawn_with_quota is kept rather than deleted, with the disposition written on the function (DECISIONS §38), because removing a documented safety mechanism is a design decision and not dead-code cleanup. The rest of this note describes the mechanism as designed; read it that way.

What it is not

It is a per-spawner quota, and when it had a caller there was exactly one spawner (the shell's service), so it was a per-process quota for the process that mattered. Generalizing to many spawners is a table of counters instead of one static, not a new idea. And it bounds spawns; it does not bound every kind of kernel object a future system call might create. The complete answer is still the untyped-kernel-objects model (a process's entire kernel footprint drawn from its own untyped, as seL4 does), where a Thread and its stack are retyped from untyped exactly as pages are in milestone 11. This quota is the pragmatic bound that closes the reachable exhaustion vector now; untyped kernel objects are the principled version, still ahead.