README.md

nit #

25% smaller than init.

nit is a boot-only init: it mounts the default filesystems, (optionally unpacks an erofs system image and pivots the root onto it), then execves a user-defined binary as the PID-1 hand-off. After the exec nit is gone from memory — the target (e.g. execd, or anything) owns PID 1 from there. nit does no supervision, no inittab, no getty, and no shutdown/umount: those are the target's job, not nit's.

Building #

make builds and tests nit as a static unikernel init: one Containerfile with three build targets, make build (the shipped image, exported to out/usr/bin/init), make test (the superset test image, which runs the whole pipeline in-container and then runs the boot gate in-container, as its final step), and make verify (the formal-verification target: Kani 0.68.0 proves nit's pure logic in a hash-locked container; the gate is the in-container build). The build needs docker, so on a fresh host first:

sudo dnf install make git
sudo systemctl enable --now docker.service
# Add the current user to the Docker group
sudo usermod -aG docker $USER

You may need to reconnect to change the signed-in users. Use groups to see if you are in the docker group. Then build:

make

which produces out/usr/bin/init -- nit installed as the rootfs init. Acceptance is the QEMU boot gate (make boot-test), which boots the real nit as PID 1 against the published hash-locked kernel and verifies the execve hand-off.

Command-line options #

nit reads exactly one line of config — /proc/cmdline — and understands a closed set of init.* options; any other token (kernel flags like ro, console=, init=/, or an unrecognized init.* typo) is ignored, so a typo such as nit.taret=... is silently dropped. A key with an empty value (init.target=) is treated as absent.

option meaning
init.target=<path> the binary to execve as the PID-1 hand-off. Optional: when absent nit still boots (mount/unpack/pivot) but reports the missing target to the console and blocks, since there is nothing to exec.
init.unpack=<path> the erofs image (e.g. /system) to unpack and pivot the root onto, when nit is the initramfs init of a unikernel layout.
init.uid=<n> real+effective UID to drop to before the exec.
init.gid=<n> real+effective GID to drop to before the exec.
init.caps=<list> capabilities to hand the exec target, a comma-separated list of lowercase names (no cap_ prefix), e.g. net_admin,net_raw. Requires both init.uid and init.gid: granting caps to a root target is a no-op, so a caps request without a drop is refused. A name nit's closed table does not know (a typo) refuses the boot rather than silently dropping the capability. Process-level, not a file stamp: the grant is staged into the process's capability sets after the drop (a capset reduce + prctl(PR_CAP_AMBIENT, RAISE)), so the target file is never written — the erofs root stays read-only and nosuid, and init.caps composes cleanly with init.unpack. The grant is inherited by the target's plain children (it sits in the target's pP/pI/pA): the target must LOWER the ambient set at start, before spawning anything, or every child it forks runs with the grant — the execd contract, not a kernel guarantee (see Security).

When init.uid/init.gid are set, nit clears its supplementary groups, then sets the gid, then the uid (the order that lets a root drop cleanly), immediately before execve — and after unpack/pivot, which need CAP_SYS_ADMIN.

When init.caps is set, nit stages the grant as a process-level ambient hand-off, not a file stamp, entirely around the drop:

  1. prctl(PR_SET_KEEPCAPS) — the full permitted set survives the uid/gid drop (without it the drop zeroes the sets, and the reduce below is rejected, new ⊄ old).
  2. setgroups(0), setgid, setuid — the drop.
  3. prctl(PR_SET_NO_NEW_PRIVS) — the target can never re-elevate via a setuid-root or file-capability binary. (It does not set SECURE_NO_CAP_AMBIENT_RAISE, so it does not block step 4.)
  4. capset reduce — pP = pI = pE = {grant}, exactly the requested set. This is the load-bearing step: a capset that sets a non-empty inheritable set requires CAP_SETPCAP, and a setuid to non-root would otherwise clear the permitted/effective sets (cap_emulate_setxuid) — step 1's KEEPCAPS is what keeps the reduce legal after the drop. Pinned live by tests/syscall.rs::ambient_handoff_to_unstamped_target.
  5. prctl(PR_CAP_AMBIENT, RAISE, cap) per granted cap — legal now, since the grant is in both pP and pI.

At execve the kernel rebuilds the target's sets as pP' = (X & fP) | (pI & fI) | pA', pE' = (fE ? pP' : pA'); for an unstamped target with an unchanged uid that is pP = pE = pA = grant — delivered with no file involved, so the erofs root stays read-only and nosuid.

The ambient set is inherited by the target's plain children: execve copies the parent's full credential block, and the kernel clears the ambient set only when the new file carries file capabilities of its own or the uid/gid changes (has_fcap || id_changed in cap_bprm_creds_from_file) — an unstamped child exec with an unchanged uid keeps the ambient set, with pE' = pA' (verified live). So "the grant does not propagate by default" is a policy the target implements, not a kernel guarantee: the target (e.g. execd) starts by stripping the inherited ambient set (prctl(PR_CAP_AMBIENT, LOWER, cap), which carries no permission gate) and RAISEs it only for the children it chooses to grant. A granted child can re-RAISE for its own children while the cap stays in its pP/pI; and because PR_SET_NO_NEW_PRIVS is set, no child can widen the grant beyond what the target itself holds.

The grant is narrow by construction and the drop is fail-closed: whoever controls /proc/cmdline can grant the PID-1 target any capability in the closed table, so keep init.caps to the least set that does the job. Treat it alongside the trust-chain caveat below.

Example:

ro init=/init init.unpack=/system init.target=/usr/bin/execd console=ttyS0,115200n8

Verification #

nit is audited by verifying it, not by inspecting it. The in-container pipeline (make test) is the gate: style+lint, ground truth against the hash-locked kernel source, the nolibc build, the self-containment check on the linked binary, and the boot — all in one container, so nothing that passes on a host can drift from what ships.

make                # build + test (the default)

# Ground truth: nit's first-party syscall numbers and uapi constants must
# match the kernel's own source; a mismatch fails the build. Runs in the
# container against the hash-locked tree (NIT_LINUX_SRC). On a host, the
# source-free structural checks still run via `cargo test -p nit --tests`;
# the live checks run when NIT_KERNEL_SRC=/path/to/linux-7.2.4 is set.
make test

# Acceptance: boot the real nit as PID 1 under QEMU and assert the
# boot-to-execve hand-off on the serial console -- the kernel hands off to
# /init, nit execve's the target, and the target runs. The x86_64 gate runs
# fully in the container (see "Boot acceptance in the container"): the test
# image carries the published hash-locked kernel, the hash-locked OVMF
# firmware, and the gate, which runs at build time.
make boot-test            # x86_64. Re-runs the in-container gate standalone
                          # against the last built local/nit-test.

Each boot-test* target exits non-zero unless every marker appears in order. Privilege is dropped via the kernel command line (init.uid/init.gid, as documented under Command-line options), and the gate always boots with a drop and asserts the dropped credentials stuck through the exec.

Boot acceptance in the container #

The superset test image (make test, the test target of the single Containerfile) runs the whole pipeline -- live ABI gate, nolibc build, self-containment gate -- and then runs the boot gate in-container as its final step: scripts/boot-gate.sh boots the exact shipped init (/rootfs/usr/bin/init) as PID 1 under the published hash-locked qemu-system-x86_64 and asserts the boot-to-execve hand-off. The test image cannot exist without a verified boot; make boot-test re-runs the same gate standalone against the last built image.

The gate #

The test image carries every input the gate needs, each hash-locked and re-verified by sha256 on every run (fail-closed):

  • Kernel: the published stagex/user-linux-server bzImage (digest pinned in the Containerfile; the gate re-checks the byte sha).
  • Firmware: OVMF from the Debian pool, ADDed by URL and verified twice -- the sha256 of the .deb and the sha256 of the extracted OVMF.fd (see provenance below).
  • QEMU: the published stagex/user-qemu 11.0.0 (digest pinned).
  • The binary booted: /rootfs/usr/bin/init, verified byte-identical to the nolibc build output, so the gate can never boot a different init than the one the pipeline built.

The guest is an initramfs built in-container (busybox cpio --null -H newc

  • gzip): the init at /init and a static marker at /marker (compiled in the container with cc -static). Boot is -machine q35 -bios OVMF.fd -kernel /bzImage -initrd ... -nographic < /dev/null; the assertion is three ordered serial markers: Run /init as init process -> nit: execve /marker -> NIT-EXEC-TARGET-RAN. No /dev/kvm is needed: TCG suffices, and the gate must also run inside a buildkit RUN (no privileged devices there).

Negatives, all verified to fail closed with distinct messages: corrupt /bzImage -> "sha mismatch"; corrupt OVMF.fd -> "firmware sha mismatch"; corrupt init -> "differs from the nolibc build output".

Why OVMF, not SeaBIOS #

The container is not the problem: the host's glibc qemu 10.0.11, extracted into a scratch container, boots the same published kernel in-container via SeaBIOS (all markers). The fault is firmware-path specific: the published musl qemu builds (10.1.1 and 11.0.0) hang at "Booting from ROM.." on the SeaBIOS/legacy boot path in a container -- under both TCG and KVM, on both i440fx and q35, with both the stagex SeaBIOS ROM and the host's SeaBIOS 1.16.3 ROM -- while the OVMF/UEFI path (-machine q35 -bios OVMF.fd) boots deterministically.

Matrix (same kernel, same initramfs, in-container):

QEMU (in-container) TCG KVM
11.0.0 (published musl), SeaBIOS hang hang
11.0.0 (published musl), OVMF boots boots
10.1.1 (published musl), SeaBIOS hang hang
10.1.1 (published musl), OVMF boots boots
10.0.11 (host glibc), SeaBIOS boots boots

"Boots" = all three ordered markers on the serial console, deterministic across repeated runs. The SeaBIOS hangs show no guest errors or triple fault (-d guest_errors stays empty): the guest never reaches the kernel; it hangs inside the firmware-to-kernel hand-off, which is what a SeaBIOS build does that OVMF does not.

OVMF provenance #

No published stagex image ships OVMF: all seven published stagex/user-edk2 digests are the 1 MB BaseTools build toolchain (no OVMF.fd), and there is no user-ovmf or equivalent. Until stagex publishes firmware, the gate fetches it the same way the pipeline fetches the kernel source tarball -- by URL, verified by sha256:

http://deb.debian.org/debian/pool/main/e/edk2/ovmf_2025.02-8+deb13u1_all.deb
sha256 78e0d54df11fc77406cb7a0bc9a39e5bca6d1cbe06556b91d9a73491c52decdf

Extracted OVMF.fd (via ar x + tar xf data.tar.xz, both available in the base image) is sha256 14954040cc754ade0c625ac3470ea46d4c14318b5b1396de4ef77090203d76db -- byte-identical to the host's OVMF (Debian trixie ovmf_2025.02-8+deb13u1), which was used in the matrix above. The OVMF pattern matches packages/service/qemu in the stagex repo, which boots guest kernels with -machine q35 -bios OVMF.fd for exactly this reason.

Security #

The TLDR of the decisions that make this a small, auditable init. Each bullet is a design choice that buys security by construction; most are then pinned by a green gate (see Audit for the invariant table and the commands that prove them).

  • Zero dependencies to audit. No external crates, no alloc, no libc: the shipped binary is no_std, no_alloc, nolibc, and fully statically linked (nm -u is empty, there is no .dynamic section). The supply chain is the compiler plus 19 hand-written syscalls — nothing else is in scope.
  • A closed, provably network-free syscall surface. Exactly the 19 syscalls a boot-only init names, on both arches, nothing else; a socket/connect/ send/recv name fails the build. nit has no exfiltration channel.
  • Single-threaded by construction, so there is no concurrency to audit. The closed syscall set admits no clone/fork/unshare/setns (audit::syscall_surface_is_single_threaded fails the build if one ever enters it), which is why nit's codebase contains not a single atomic, lock, or synchronization primitive: the one-time /proc/cmdline publish is a plain write. The gate keeps that argument true as the surface changes.
  • A 19-syscall surface with no "removable" extras. Two syscalls that look removable are load-bearing: fcntl (the dup2(n,n) check that validates a target fd before closing anything) and mknod (the /dev fallback when no devtmpfs is mounted). The closed-set gate pins the exact 19 on both arches, so neither a removal nor an addition can slip through silently.
  • Every number pinned to the kernel's own source. Syscall numbers and uapi constants are cross-checked against a hash-locked kernel tree at build time; a drift fails the container, so the ABI can't silently go stale.
  • No allocation, almost no parsing. nit allocates nothing and parses a single line of /proc/cmdline; unsafe is confined to the syscall dispatcher (syscall.rs/raw.rs), each block carrying a SAFETY comment naming the invariant and the test that pins it.
  • The root hand-off is descriptor-hygienic. Every boot-path open is O_CLOEXEC, so no root-opened descriptor crosses the execve into the (possibly unprivileged) target.
  • Privilege drop is fail-closed. A partial, zero, or unparseable init.uid/init.gid refuses the boot rather than silently keeping a root id, and prctl(PR_SET_NO_NEW_PRIVS) before exec blocks setuid/file-cap re-elevation. A init.caps request that would not be paired with a full drop, or that names a capability outside the closed table, is refused the same way.
  • Mount hygiene. nosuid/nodev/noexec on the world-writable and monitoring mounts, hidepid=2 on /proc, and an empty environment + minimal argv at exec. On the init.unpack path nit additionally mounts a 0700 tmpfs over /root and /etc/keys; running directly as a rootfs init (no init.unpack) it trusts the image's existing /root//etc/keys rather than clobbering a real persistent one.
  • The trust chain is stated, not hidden. init.unpack and init.target are unauthenticated /proc/cmdline paths; integrity rests on the bootloader/UKI stage above nit and is called out as such at the top of Audit.
  • The whole repo fits in one context window. All source, tests, build files, and this README together are ~42k tokens — a fraction of a 256k-class LLM window, and small enough that even a 64k window holds it with room to spare. An auditor, human or model, can load the entire codebase and never work from an approximation of it, which is deliberate: the audit target is the whole repo, not a subset.

Audit #

Trust chain. nit irrevocably pivots to and execs content named on an unauthenticated /proc/cmdline: init.unpack and init.target are consumed verbatim, with no signature, hash, or dm-verity check. nit assumes the bootloader/UKI stage has already measured and authenticated the command line and the initramfs; without that, whoever controls either controls PID 1 as root. The /etc/keys 0700 tmpfs is scaffolding for a trust anchor (a signed image or a dm-verity root hash) that does not yet exist. Within that chain, nit's own hand-off is hardened: every boot-path descriptor is O_CLOEXEC (nothing root-opened escapes the execve), a privilege drop is fail-closed (a half-specified, zero, or invalid init.uid/init.gid refuses the boot rather than keeping a root id), and a dropped target runs with PR_SET_NO_NEW_PRIVS so a setuid or file-capability binary in the image cannot re-elevate it.

The question an external researcher has to answer: is this init not malicious, not vulnerable, and does every unsafe block trace back to kernel behavior or a test? The design goal is that each of those questions is answered by a green gate, not by reading code cold. Every invariant below has an enforcing mechanism that fails the build if it is violated; work the list top to bottom, and each "proof" is a command you can run.

# Invariant Enforced by Proof
1 No external crates. The binary is no_std, no_alloc, nolibc and fully self-contained. Cargo.toml has no [dependencies]/[dev-dependencies]; the Containerfile self-containment gate builds the init and fails on any undefined symbol, any .dynamic section, or any unwinder-glue symbol. make build (the gate runs in the build stage). The manual proof it automates: nm -u .../nit is empty (nothing to resolve from a libc) and the image has no .dynamic section. The only C-named (non-Rust-mangled) symbols are _start, entry_main, and memcpy, all defined by nit itself. (The rust_begin_unwind symbol in the image is nit's own #[panic_handler] — the attribute emits the fn under that name — a local definition, not an imported unwinder.)
2 The syscall surface is a closed set — exactly the 19 syscalls a boot-only init names, nothing else. audit::syscall_surface_is_closed_set (both arches, from abi.rs). cargo test -p nit --test audit. Negative-tested: adding a 20th name fails the build.
3 None of it is network-capable. nit has no socket/connect/send/recv; no exfiltration channel. audit::syscall_surface_has_no_network_capability. cargo test -p nit --test audit. Negative-tested: a socket constant (correct number, present in the kernel .tbl) is caught by this test even though kernel_source stays green.
4 Every syscall number and uapi constant matches the kernel's own source. kernel_source — reads the kernel tree (hash-locked in the container) and cross-checks nit's src/abi.rs: the first-party syscall numbers against the kernel's syscall_*.tbl tables (both arches, text-parsed on one host; the compiled constants checked natively per arch) and the UAPI_CONSTS set against the kernel's own #defines. make test (the container always runs it live against the hash-locked tree). A mismatch blocks the build.
5 Every unsafe block is justified. Each carries a SAFETY comment naming the invariant and, for the load-bearing sites, the tests that pin it. audit::every_unsafe_block_has_safety_comment — a source scan that fails if any unsafe { has no SAFETY within 6 lines above, with per-file minimums so the scanner can't silently read nothing. cargo test -p nit --test audit. Negative-tested: with the comments stripped, it failed listing all 24 raw.rs line numbers.
6 The untestable-by-unit unsafe sites are tied to the tests that do pin them. Comments, not assertions: raw_syscall (both arches) and memcpy in raw.rs name the syscall.rs suite + kernel_source + make boot-test; _start in main.rs names make boot-test. Reading raw.rs/main.rs. The three sites a unit test cannot reach are the only ones with a prose proof, and each names the specific test that covers it.
7 The pure logic is formally verified over its input domain. parse_cmdline, parse_caps, cap_bit, and write_digits are proven by Kani (CBMC 6.11.0) to satisfy their contracts for every input in the domain: the 41-entry capability table maps each name to its exact distinct bit and unknown names to None; a single named cap grants exactly its bit and no other (including the 19-char checkpoint_restore -> 1 << 40 upper-word grant); the comma fold ORs exactly the named bits for one, two, three, and four tokens and nothing else (proven both directly and end-to-end through parse_cmdline); a repeated name is idempotent; an unknown name in a value refuses the whole grant, not just that token; empty/bare-comma values are refused, not granted; credential values parse across the full valid u32 range and refuse overflow; the command line extracts keys in any order with empty values treated as absent; the digit encoder emits minimal ASCII decimal with no leading zero and round-trips (proven over the full u8 and u16 domains). The caps path is deliberately str-free (cap_bit compares bytes; a non-UTF-8 token can never equal an ASCII table name, so it is Invalid either way), which is what keeps the concrete-token harnesses from exploding in core's UTF-8 validation — measured: the three/four-token and 19-char cases did not close in 1000-1500 s before the rewrite, and close in a couple of seconds after. Each harness's loop bound is explicit (#[kani::unwind(N)]) and justified in the harness doc: N = 42 = the 41-entry cap table + 1, N = 20 = the u64 worst-case digit count, N = 6 = the u16 worst-case digit count. Under- and over-unwinding both fail the gate (an under-unwind leaves an infeasible "still looping" path that trips the unwind assertion), so the bound cannot be silently weakened. verification/kani/tests/proofs.rs (24 harnesses), run by the verify Containerfile target: Kani 0.68.0, CBMC 6.11.0, and the nightly-2026-08-21 toolchain are all hash-locked (sha256 of each artifact), the default CaDiCaL solver needs no external download, and the final build step asserts VERIFICATION: SUCCESSFUL with exactly 24 successfully verified harnesses, 0 failures, 24 total — a failing, timing-out, or missing harness fails the build. make verify (the proofs run in the build). What it does not prove, and who covers that: symbolic-length input to the parser — the concrete-literal harnesses close because their loop trip counts are compile-time constants, but a symbolic-length command line / caps / credential value drives CBMC's Range::try_fold over the slice into a path explosion that never reaches the SAT solver (measured: 0 SAT instances in 900 s, three shapes) — pinned concretely by tests/config.rs::caps_table (multi grant, dedup, high-bit, unknown, uppercase, bare-comma) and the rest of tests/config.rs, which run the real parse_cmdline; write_digits over the full u32/u64 domain exceeds the prover's budget at the justified bound — the wall is bit-width, not loop count, and not the solver (CaDiCaL times out; the bundled kissat returns UNDETERMINED for every check; an SMT solver is a different interface from Kani's SAT slot) — pinned by tests/fmt.rs (u32::MAX → 10 digits, u64::MAX → 20 digits, zero, non-zero offset).

The unsafe inventory #

26 unsafe { blocks total (24 in raw.rs, 2 in config.rs), all covered by invariant #5. The risk concentrates in five sites; the rest are thin wrappers that forward a valid pointer or integer to a first-party dispatcher.

  • raw_syscall (x86_64 and aarch64) — inline asm; the register map is load-bearing. Pinned by the syscall.rs test module (drives the real kernel), kernel_source (numbers), and make boot-test (end to end).
  • _start (x86_64) — naked asm entry point; no caller, so no unit test can reach it. Pinned by make boot-test (a misaligned or mis-dispatched entry faults on the first aligned store or never reaches the exec hand-off).
  • memcpy — the only libc symbol nit needs. Contract is non-overlapping (safe by construction); exercised on every read into a buffer.
  • Two from_raw_parts in config.rs — a one-time /proc/cmdline read into a static buffer. There is no publish protocol: nit is single-threaded by construction (no clone/clone3 in the closed syscall surface, pinned by audit::syscall_surface_is_single_threaded), so the writer never races a reader; the &'static and &mut views are disjoint in time.

Why the gates are split this way #

kernel_source answers "is the number correct?"; audit answers "should nit name this at all?" They are independent: a syscall can have the right number (passes kernel_source) and still be a network channel (fails audit). Both must be green for the surface to be both correct and closed.

The self-containment gate is the only one that inspects the linked binary rather than the source: the never-unwind property of invariant #1 is asserted on the image itself, so a change that re-introduces an external unwind dependency fails the build instead of surfacing only to a later readelf.

All tests live in tests/ as integration targets that link the no_std lib (black-box, against the public API). The lib itself carries no #[cfg(test)] modules. The reason a plain tests/*.rs used to be impossible — cargo would also build the non-test host binary, and a no_std first-party-_start init cannot link on a std host (duplicate _start, unwinding without std) — is now handled by a feature: src/main.rs is gated behind required-features = ["nolibc"], so a host cargo test never builds it. The one shell script is a harness, not test logic: boot-gate.sh builds the initramfs and captures serial output in-container.