crates

capability_table

check proofs license minimum Rust version

A capability table for a kernel: a file descriptor table that can point at anything, where each entry carries the rights its holder has over the object. The table lives in the kernel's memory and a program sees only a slot number, so no program can forge a capability. A holder can only pass one on, and a capability passed on never carries more than its source held. The crate is no_std, allocates nothing, has no dependency and contains no unsafe code. The owner supplies the object type and the size, which is part of the type.

What it does

EXAMPLES

Rights narrow by intersection, so a capability passed on never carries more than its holder had.

use capability_table::Rights;

let held = Rights::READ.union(Rights::WRITE);
assert!(held.allows(Rights::READ));
assert!(!held.allows(Rights::GRANT));

// Handing it on with a mask can only ever narrow it.
let passed_on = held.intersect(Rights::READ.union(Rights::GRANT));
assert_eq!(passed_on, Rights::READ);
assert!(passed_on.is_subset_of(held));

No operation widens:

use capability_table::Rights;

let read_only = Rights::READ;
// The best you can do with ALL on the other side is get back what you already had.
assert_eq!(read_only.intersect(Rights::ALL), read_only);
assert!(!read_only.intersect(Rights::ALL).allows(Rights::WRITE));

What is proven

Twelve Kani harnesses, in mod verification at the end of src/lib.rs. The rights proofs hold for every one of the 2^31 words a Rights can hold (its top bit is a marker, not a right); the table proofs hold for every state of a three-slot table and every slot index.

Each harness has a patch in falsifications/ that breaks the property it proves, and the proof fails on the patched code. Two things lie outside the proofs: tables larger than three slots, which the unit tests cover, and highest_seen, which is one value shared by every table.

Building and testing

cargo build
cargo test
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo build --lib --target x86_64-unknown-none   # also aarch64-unknown-none-softfloat, riscv64imac-unknown-none-elf

Run rustup target add once, before the first bare-metal build. Without it, that build stops with can't find crate for core. The tests run on the host in a fraction of a second.

Running the proofs

The proofs need Kani, which rustup does not install. Install it once, with network access. Run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup, which downloads a model checker. CI uses version 0.68.0, and another version may verify differently.

Then cargo kani, from the crate's directory, runs all twelve harnesses in under half a minute on one development machine, most of it compiling. CI runs plain cargo kani, with no extra flags. To run one harness, give its full path and --exact:

cargo kani --harness verification::derive_never_widens_rights --exact

A passing run prints several lines that look alarming. All of them are expected:

Trust the last line of the run: Complete - 12 successfully verified harnesses, 0 failures, 12 total.

Running one falsification patch

patch -p1 --no-backup-if-mismatch < falsifications/verification.derive_never_widens_rights.patch
cargo kani --harness verification::derive_never_widens_rights --exact   # ends VERIFICATION:- FAILED
git checkout -- src

The harness name is the part of the patch's file name after verification.. The run ends with Complete - 0 successfully verified harnesses, 1 failures, 1 total., and the failed check names the assertion that the patch breaks. git checkout -- src reverts the patch.

BUGS

Minimum Rust version

1.85. To check it, install that toolchain and its bare-metal target, then test and build with it:

rustup toolchain install 1.85 --profile minimal --target x86_64-unknown-none
cargo +1.85 test
cargo +1.85 build --lib --target x86_64-unknown-none

License

Licensed under either of the MIT license (LICENSE-MIT) or the Apache License, Version 2.0 (LICENSE-APACHE), at your option.