Verification ============ Problem: kernel could have bugs. panic. wrong syscall results. corrupt file system data. incomplete isolation. Many ways to find bugs: compiler warnings. testing. fuzzing (randomized testing). code review. This paper: prove absence of certain kinds of bugs. Plan: write down a model how a RISC-V machine runs. xv6 kernel is the initial contents of DRAM. fs.img is the initial contents of the disk. implement this model in a proof-checker (Rocq, Lean, etc). write a theorem about this model's execution. prove theorem. What is a model of a RISC-V machine? CPU state: registers, CSRs, privilege mode, PC. memory state. device state: UART, disk, etc. a step function: given a state, what is the next state. fetch the instruction at PC, decode it, do what the RISC-V manual says. QEMU also runs RISC-V code. run of QEMU shows one possible execution. model's goal: describe all possible executions. interleaving between multiple cores, devices, etc. This paper: Sail RISC-V model (plus a machine model: shared memory, devices). Demo: xv6 on the Sail model. % sail_riscv_sim --trace --inst-limit 70 kernel/kernel instruction fetch register read/write CSR reads (mhartid) load/store mret: change privilege levels What does the model's definition look like? ~/refsrc/sail-riscv/model postlude/step.sail: run_hart_active check for pending interrupt, fetch, decode, nextPC = PC + 4, execute. execute() describes how to actually execute an instruction. extensions/I/base_insts.sail: execute ITYPE addi, andi, ... extensions/I/base_insts.sail: execute MRET illegal instruction if not at machine privilege otherwise, update CSRs, set next PC, .. extensions/I/base_insts.sail: execute LOAD. vmem_read calls pt_walk if paging is enabled sys/vmem.sail: pt_walk model of what the hardware does with page tables Plan: write a theorem about the model, and prove it. What is a theorem? example: fetchaddr() in kernel/syscall.c checks a user pointer: if (addr >= p->sz || addr + sizeof(uint64) > p->sz) // both tests needed, in case of overflow return -1; testing: run the code on some inputs, check the outputs. addr=4096, sz=8192: OK. addr=8190, sz=8192: rejected. says nothing about inputs we didn't try. theorem: claim about all inputs. for every 64-bit addr and sz: if check passes, addr..addr+7 are all below sz. Lean, Rocq: programs that let you state theorems, write proofs, check proofs. checks every step of proof; if checker accepts, the theorem holds. Demo: verification/Fetch.lean the check from fetchaddr, re-written in Lean's language, using 64-bit arithmetic. #eval runs it: 0 (OK), -1 (rejected). theorem check_ok: if the check passes, addr + 8 <= sz (mathematically, not 64-bit). Lean: error, can't prove it. Q: why is the theorem not true? the comment says overflow is handled. verification/FetchFixed.lean #eval check 0xFFFFFFFFFFFFFFFC 0xFFFFFFFFFFFFFFFF -> 0: passes! addr + 8 wraps around to 4, which is < sz. theorem in FetchFixed.lean: same, but with hypothesis sz <= MAXVA. hypothesis = precondition: the rest of xv6 must ensure sz <= MAXVA. Q: does xv6 ensure that? growproc/sbrk check sz + n > TRAPFRAME. Same theorem, but about the Sail model running the compiled kernel. kernel/kernel.asm: fetchaddr 800028c0: bgeu s1,a1,800028ec # addr >= sz -> return -1 800028c4: addi a5,s1,8 800028c8: bltu a1,a5,800028f0 # sz < addr+8 -> return -1 800028cc: ... # check passed s1 holds addr; a1 holds sz (just loaded from p->sz). Demo: verification/sail/Fetchaddr.lean step: the model's run_hart_active (interrupt check, fetch, decode, execute). runUntil: step until the PC is one of the given PCs. theorem fetchaddr_check: start: PC = 800028c0, addr in s1, sz in a1, sz <= MAXVA, kernel image in memory, CPU set up as start.c left it. end: at a return -1 (800028ec, 800028f0), or at 800028cc with addr + 8 <= sz. the model fetches and decodes these instructions from the kernel's bytes. theorem is about bytes from compiler's output, not C code. hypotheses about the CPU: Xv6Sail.lean, xv6Ref. S mode; PMP, mie, mideleg, ... as start.c set them. simplified: satp = 0 (paging off), mip = 0 (no interrupt pending). % lake env lean Fetchaddr.lean delete the maxva hypothesis: same counterexample as Fetch.lean. Big idea: Lean checks the proof no need to audit the proof body paper's xv6 proof: ~1.6M lines of Rocq / ~1M lines of Lean suffices to audit the theorem statement what is the xv6 theorem? This paper's theorem: UART console traces. Allowed trace: out: "init: starting sh" out: "$ " in: "echo hello world" out: "hello world" Allowed trace: in: "echo foo > f" in: "cat f" out: "foo" Not allowed: in: "echo foo > f" in: "cat f" out: "bar" [ demo: run these commands against xv6? ] Theorem implies lots of things have to go right. file system reading from disk. process execution. page tables. exec. init, sh, .. UART driver. context switching. spinlocks. kalloc. other syscalls. compiler, linker. There is a syscall spec underneath the theorem, as part of the proof. More complicated statement because it's a theorem about partial execution. If combined with some user-level program, then syscall results will be such. Theorem traces include seccomp: isolation. in: "seccomp xx", any xx binary What kind of bug might not be covered by theorem? kernel goes into an infinite loop. scheduler never runs some process. leaking data between processes. model doesn't match real hardware. commands/code not covered by console trace theorem. Verified kernel can still panic. verification/inodes.c: opens many inodes at the same time demo: change Makefile to build user/inodes.c, redirect printk UART to printk.out % make qemu $ inodes ^A x % cat printk.out why does this satisfy the theorem? caveat: strictly speaking, "inodes" is not an allowed command theorem says all output will be a valid trace did not see any incorrect output (safety) theorem does not guarantee we'll see anything at all Bugs that the paper finds. 10 bugs in the xv6 kernel. 1 bug in the Sail model. 1 bug in grep. xv6 kernel bug: disconnected cwd. $ cd /a/b $ rmdir /a/b $ rmdir /a $ mkdir ../xx cwd is still pointing to former inode of /a/b, still referenced by cwd .. points to former inode of /a, no reference, freed kernel panic'ed on mkdir in ip->type != T_DIR What's the fix? Don't allow namei if directory's link count is 0. Did this bug have to get fixed to make the theorem true? kernel panics might be "ok" if they just spin forever. verified xv6 has two UARTs, printk / panic goes to a different UART. from user's perspective on first UART, machine just hangs. Verified xv6 replaced many panic() calls with unreachable(). unreachable() writes to invalid physical memory. Sail model says that's undefined, unclear what happens. So, cannot prove anything about executing unreachable(). Thus, theorem must imply unreachable() is never called. Replacing the panic() with unreachable() would require fixing the cwd bug. xv6 kernel bug: missing icache fence for user code. RISC-V: non-coherent icache. exec writes new process text into fresh page of memory. icache on some CPU might hold old cached data from that page. Must flush icache to ensure user process runs correct code. What's the fix? flush icache in userret (trampoline.S). Did this bug have to be fixed? Yes because proof has to ensure init, sh, echo all run correct code. Why did testing never find it? QEMU keeps its icache coherent, so the bug can't show up there. model aims to cover all possible executions on different hardware. tested on real RISC-V hardware: can observe incoherent icache behavior. xv6 kernel bug: overflowing nlink. 16-bit nlink counter in inode. Creating 2^15 links causes nlink = 32768, which wraps around to -32768. Creating 2^16 links causes nlink = 65536, which wraps around to 0. What goes wrong? One symptom: unlink() panics if nlink<0. So unlink() after 2^15+1 links causes panic. What's the fix? Return error from link() if nlink is already at max value (32767). Did this bug have to be fixed? panic might be allowed by theorem. More problematic issue: could reuse an inode after 2^16+1 links, then unlink. Would lead to incorrect file system results. xv6 kernel bug: kill(0). pid==0 check was missing from kkill() in kernel/proc.c. Would set the p->killed flag on some UNUSED process with p->pid==0. Then, when a process later called fork, it would get immediately killed. Did this bug have to be fixed? Probably not: theorem's commands never invoke kill(0). Weird spec for kill and fork syscalls: what does kill(0) mean? Some later fork'ed process could be immediately killed. xv6 kernel bug: failing to log partial file write. copyin could fail. writei() didn't call log_write() on error path. Problem: bcache has modified data, but it's not logged. Can read() to observe modified data. When bcache gets reused, read() will observe original data again. Did this bug have to be fixed? Could be a wrong result from a read() syscall. Or perhaps write() syscall should say this is possible for invalid buffer? Most of theorem's commands do not invoke such write syscalls. But seccomp'ed binary might, and its output could be redirected to some file? Sail model bug: PTE A/D writeback. One core unmaps page, clears PTE, frees the page. Another core accesses page, sets A bit, writes back PTE, resuscitates page. Did this bug have to be fixed? xv6 doesn't unmap pages while another core could be accessing that PTE. How large is the theorem statement and proof? Figure 32: 1591 lines to define allowed traces of shell commands. Figure 32: ~25K lines to define Sail model and devices. Figure 31: ~1.5M lines to prove theorem.