The nit → execd ambient-capability contract #
execd is nit's init.target. nit grants the exec target a capability set G
as a process-level ambient grant (not a file stamp), and execd is the
component that decides which of its supervised units ever see a capability.
The contract has two halves:
- nit's half (in nit): drop to
init.uid/init.gid, thencapsetreducepP = pI = pE = G, thenprctl(PR_CAP_AMBIENT, RAISE, cap)per granted cap, thenexecvethe target. The target file is never written, so the rootfs stays read-only andnosuid. - execd's half (here): strip the ambient set at startup, and grant a
capability to a unit child only when the unit's HCL lists it, by raising
the cap into the ambient set in the
fork→execvewindow.
The whole point: "children get no grant by default" is execd's policy, not a kernel guarantee. A plain (unstamped, same-uid) child exec inherits the ambient set, so if execd did nothing, every unit it ever supervised would run with the full grant.
Why ambient is the only channel #
At execve the kernel rebuilds the new process's capability sets as
(man 7, capabilities, "Transformation of capabilities during execve()"):
P'(ambient) = (file is privileged) ? 0 : P(ambient)
P'(permitted) = (P(inheritable) & F(inheritable)) |
(F(permitted) & P(bounding)) | P'(ambient)
P'(effective) = F(effective) ? P'(permitted) : P'(ambient)
P'(inheritable) = P(inheritable) [unchanged]
P'(bounding) = P(bounding) [unchanged]
For an unstamped, same-uid target, F is empty, so:
P'(permitted) = P'(effective) = P'(ambient) = P(ambient);P'(ambient) = P(ambient)(cleared only by a privileged file, i.e. one with file capabilities or a setuid/setgid bit);P'(inheritable) = P(inheritable)unchanged: the process's inheritable set carries acrossexecveas-is. The file's inheritable set only feeds the new permitted set; it never reduces the new inheritable set.
This second line is the one that is easy to get wrong. It is why, after nit's
hand-off, execd itself holds the grant in all four sets
(CapPrm = CapInh = CapEff = CapAmb = G). The practical consequence:
execd's per-unit prctl(PR_CAP_AMBIENT, RAISE, cap) is legal for every cap
in G, because RAISE requires the cap to be in both the permitted and
the inheritable set, and the inheritable set survives execve untouched.
The validity condition: non-root, same-uid, unstamped #
The grant is only meaningful when all three hold:
- Non-root. A root
execvebypasses ambient entirely: the kernel grants the target the full bounding set regardless of the ambient set. Granting caps to a root target is a no-op, which is why nit refusesinit.capsunless it is paired with a fullinit.uid+init.giddrop. - Same-uid exec. A setuid exec (or an exec where the file's uid does not match) clears the ambient set, so the hand-off would die at the boundary.
- Unstamped. File capabilities on the target file clear the ambient set
the same way. The target must carry no
security.capabilityxattr.
nit also sets PR_SET_NO_NEW_PRIVS before the exec. That does not set
SECURE_NO_CAP_AMBIENT_RAISE, so the ambient hand-off still lands; it blocks
setuid/file-cap re-elevation instead.
How execd enforces it #
- Startup strip (
main.rs, before the fork):caps::clear_all(). Both the reaper and controller branches are born clean. The reaper never goes through the per-unit grant path, so this is what keeps it from carrying the grant forever. Defense in depth: if the prctl fails, execd logs loudly and continues, because the per-unit path re-clears and fails closed on its own, and under PID 1 a live-but-stripping init beats a dead one. - Per-unit grant window (
child_process.rs::spatch): a static mutex is held for the whole span set ambient to the unit's caps →fork→ restore ambient to empty (RAII drop).tokio::Command::spawnis synchronous inside the window (no.await), so one unit's fork can never land inside another unit's grant.caps::grant(mask)isCLEAR_ALLthenRAISEper bit:RAISEis kernel-gated, so a unit that names a cap nit never granted to execd fails the spawn withEPERMand is refused, not silently starved. - Fail-closed config load (
config.rs/caps.rs): thecapsfield resolves against a closed 41-name table that mirrors nit'sabi::cap::CAPSbyte-for-byte. Any name outside the table (a typo, a future cap nit cannot grant) refuses the whole config load. A typo in a security parameter must refuse the config, not silently drop the requested capability.
Declaring caps on a unit #
unit "probe" {
command = "/usr/sbin/tcpdump"
args = ["-i", "any"]
caps = ["net_raw"]
health {
type = "oneshot"
}
}
Default is none: a unit without a caps field runs with CapEff = 0x0.
Notes:
- Pre-commands never get caps.
preargv vectors always run with the empty ambient set; the grant is scoped to the unit's main command only, so transient setup stays least-privilege. scriptunits grant the shell.script = "..."runs as/bin/sh -c, so the granted caps attach to/bin/shand are inherited by everything the script forks until each one execs. If a script needs a cap, the entire shell body runs with it. Prefer acommandunit when the grant should attach to a single binary.- Respawns re-grant. The grant window is re-opened on every start, so restart-policy units get the same caps on each respawn.
What is pinned by tests #
tests/caps.rs (root-gated) drives the real execd binary exactly the way nit
would: the test child performs nit's precise hand-off
(KEEPCAPS → drop to nobody → NO_NEW_PRIVS → capset reduce
pP = pI = pE = NET_RAW → RAISE 13 → execv), then four oneshot units
self-report their CapEff:
| unit | caps declared | granted by hand-off | observed CapEff |
|---|---|---|---|
u0 |
(none) | — | 0x0 |
u1 |
net_admin |
no | unit refused to start (fail closed) |
u2 |
net_raw |
yes | 0x2000 |
u3 |
(none), after the grant | — | 0x0 (no leak) |
The test also pins two kernel constants that silently invalidate the whole
measurement if wrong: PR_SET_KEEPCAPS is 8 (a wrong value makes the
capset reduce an illegal raise, so every non-root reading is garbage), and
PR_CAP_AMBIENT RAISE takes the capability number (13), not the bit
mask (0x2000).