program_argument_contract
One read-only 4 KiB page that carries a program's argv as length-prefixed bytes. Whoever spawns the
program writes it with PageBuilder, and the program's runtime reads it with ArgumentPage. no_std,
no dependencies, so the writer and the reader share one definition and cannot drift on the layout.
The bytes carry no authority. The access a program holds decides what it may read or write, never what its arguments say, so nothing checks the page's bytes for meaning.
The contract
- The page is
PAGE_BYTES(4096) bytes: a header ofHEADER_BYTES(16) and then records. The header isMAGIC(eight bytes), a little-endianu32count of arguments and a little-endianu32total of record bytes. Each record is a little-endianu32length and then that many bytes. Any byte may appear in an argument, which is why the layout is length-prefixed and not delimited. - The first argument is
argv[0], because programs such asripgrepskip it andclaptreats it as the program's name. - The records have room for
MAXIMUM_RECORDS_BYTES(4,080) bytes, each argument'srecord_bytes(its four-byte prefix and its bytes) included. When a line does not fit,PageBuilderrefuses all of it withPushRefused::TooLongand truncates nothing. - A page that does not parse reads as no arguments: a wrong magic, a zeroed frame, a count that disagrees with the records, or a length that overruns the page. The reader never returns a partial argv, because a program that sees half its arguments would act on a line nobody typed.
PageBuilder::newtakes a slice and refuses, withWrongPageLength, any whose length is notPAGE_BYTES.ArgumentPage::from_virtual_addressisunsafeand names a mapped page;ArgumentPage::parsetakes any slice, and is the safe reader.- The identity is
VERSION1 andINTERFACE_ID18, with the layout above.
EXAMPLES
use program_argument_contract::{ArgumentPage, PAGE_BYTES, PageBuilder};
let mut page = [0u8; PAGE_BYTES];
let mut builder = PageBuilder::new(&mut page).unwrap();
for argument in [&b"rg"[..], b"ab\xffcd", b"src"] {
builder.push(argument).unwrap();
}
assert_eq!(builder.finish(), 3);
let arguments: Vec<&[u8]> = ArgumentPage::parse(&page).iter().collect();
assert_eq!(arguments, [&b"rg"[..], b"ab\xffcd", b"src"]);
What is proven
Two Kani harnesses cover the parser, over a symbolic 48-byte page that carries the magic.
parse_never_panics_and_iterates_len_items proves that parse never panics and that the iterator
yields exactly len() arguments, with is_empty agreeing.
a_parsed_page_holds_exactly_what_its_header_states proves that a page which parses holds exactly
count arguments whose four-byte prefixes and bytes add up to total. Each has a falsification
patch in falsifications/ that turns it red.
the_builder_and_the_parser_round_trip_two_arguments proves that two arguments of three and two
symbolic bytes, written over a 4,096-byte page of stale bytes, read back exactly as PageBuilder
wrote them. Its falsification patch is in falsifications/ too. Each harness verifies in under a
minute. No proof covers the caller's promise that the address names a mapped page,
because Kani cannot see a mapping.
Building and testing
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo test
cargo build --lib --target x86_64-unknown-none # also aarch64-unknown-none-softfloat, riscv64imac-unknown-none-elf
BUGS
- Damage to two header fields at once can read as a shorter argv. A
countand atotalthat both describe a prefix of the records parse as exactly that prefix, since the layout has no checksum. No single-field damage yields a partial argv. The page has one writer and maps read-only, so this is a property of the format under corruption and not an attack surface. from_virtual_addressreads through ausizepointer, so on a target narrower than 64 bits an address above 4 GiB would read a different page. Its# Safetysection makes a fitting address the caller's promise. No target built here is that narrow.- A secret on a command line is plain bytes on this page.
- This crate is one file,
src/lib.rs, because a standard library port copies it by name. A test checks thatsrc/holds no other file. PageBuilder::newtakes a slice of exactly 4,096 bytes, so a proof over the builder needs the whole page. With symbolic argument lengths the solver ran past 11 GiB and failed, so the round-trip proof fixes the lengths at three and two bytes and leaves the bytes symbolic. The tests cover other lengths, the empty argument included.
Minimum Rust version
Rust 1.88.
License
Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.