environment_contract
The read-only page that carries a process's time zone, locale and terminal type, and the rules for
writing and reading it. no_std, no dependencies, no system calls. The program that starts a
process writes the page, and the process's start-up code reads it. They share this one definition,
so they cannot drift.
A value reaches the page only as a member of a closed list. The writer refuses anything else, and the reader answers only a member, so a page some other writer produced cannot hand a program an arbitrary string such as a password.
The page
The page is 107 bytes (PAGE_CONTENT_BYTES). It starts with the 8-byte magic ENVCONF1. Then each
of TZ, LANG and TERM takes one length byte and a fixed field of 40, 24 and 32 bytes.
- Write with
PageBuilder. Each setter (time_zone,locale,terminal_type) returnsErr(Refused)for a value outside its list.buildreturns the page's bytes. A key never set stays zero. - Read with
ConfigurationView, which borrows the page's bytes and is safe. A reader that holds only the address of a mapped page usesConfigurationPage, whosenewisunsafebecause it takes a raw address, and which reads exactly asConfigurationViewdoes. Each accessor returnsNonefor a zeroed or foreign frame, for a key never set, for a length byte past its field and for a value outside its list. Otherwise it returns the list's own string, never bytes borrowed from the page. No input makes it panic. INTERFACE_IDis 13,VERSIONis 1 andMAGICisENVCONF1. A format change is a new version, never a new number. A fourth key is a new version.
EXAMPLES
use environment_contract::{ConfigurationView, PageBuilder, Refused};
let page = PageBuilder::new()
.time_zone("America/Los_Angeles").unwrap()
.locale("en_US.UTF-8").unwrap()
.terminal_type("xterm-256color").unwrap()
.build();
let read = ConfigurationView::new(&page);
assert_eq!(read.time_zone(), Some("America/Los_Angeles"));
assert_eq!(read.locale(), Some("en_US.UTF-8"));
assert_eq!(read.terminal_type(), Some("xterm-256color"));
assert_eq!(
PageBuilder::new().time_zone("definitely-not-a-timezone"),
Err(Refused::UnknownTimeZone),
);
The lists
The lists are domain::KNOWN_TIME_ZONES, KNOWN_LOCALES and KNOWN_TERMINAL_TYPES. They are
short on purpose. They are not the full Internet Assigned Numbers Authority (IANA), glibc or terminfo databases, and compiling those in
would be a dependency decision. A constant assertion fails the build when a member outgrows its
field.
| To do this | Change |
|---|---|
| Add a zone, locale or terminal type | A pull request that adds the member to its list and names the real IANA, glibc or terminfo entry. It must fit the field's maximum. |
| Add a fourth key | A new VERSION and a new field. |
What is proven
Two Kani harnesses in src/verification.rs prove the reader. Each has a falsification patch in
falsifications/ that turns it red.
| Harness | Property | Falsification patch |
|---|---|---|
verification::field_bytes_never_panics_for_any_length_byte |
a length byte past its field reads as absent and never panics, including a length byte of 255 on the last field | verification.field_bytes_never_panics_for_any_length_byte.patch |
verification::an_accessor_answers_only_the_member_its_field_holds |
for any page at all, an accessor answers None or a member of its list, and a member only when the page carries the magic and the field holds exactly that member |
verification.an_accessor_answers_only_the_member_its_field_holds.patch |
The reader compares a field's bytes with each member over the member's own length, rather than decoding them as UTF-8, so the second harness takes a fully symbolic page. It ran in about six minutes and 2.2 gigabytes of memory on a shared 80-core host.
That a built page reads back as the members it was given is a host test,
every_member_round_trips_through_the_safe_reader. The lists are finite, and the test writes and
reads every member of every list, so it covers every input. A Kani harness over a symbolic member
did not finish in under seven minutes, and adds nothing the exhaustive test lacks.
Building and testing
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
cargo build --lib --target aarch64-unknown-none-softfloat
cargo build --lib --target riscv64imac-unknown-none-elf
Running the proof
Install Kani first: run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup. Setup downloads a model checker and the Rust toolchain that Kani pins, so it needs
network access. Then run the harness:
cargo kani
A passing run prints warnings that mean nothing, among them use of an unstable feature for
register_tool. The answer is the last line, Complete - 2 successfully verified harnesses, 0 failures, 2 total.
Running a falsification patch
Apply a patch, confirm its proof fails, then undo it with the same patch reversed. For the first harness:
patch -p1 --no-backup-if-mismatch < falsifications/verification.field_bytes_never_panics_for_any_length_byte.patch
cargo kani --harness verification::field_bytes_never_panics_for_any_length_byte --exact
patch -R -p1 --no-backup-if-mismatch < falsifications/verification.field_bytes_never_panics_for_any_length_byte.patch
The other follows the same pattern with its own name. The failing run prints
VERIFICATION:- FAILED and cargo kani exits 1.
Using it
The project has not published this crate, and publishing is on hold. No release tag exists yet.
Depend on it by git, pinned to a commit of this repository's main and never to a branch:
[dependencies]
environment_contract = { git = "https://github.com/nifeos/environment_contract", rev = "<full commit hash>" }
Once a pre-release tag exists, use tag = "<that tag>" in place of rev.
Fuzzing
Weekly fuzzing is off for this crate, because the reader has unsafe code in one place:
ConfigurationPage::new takes a raw address.
BUGS
- The lists are short on purpose.
KNOWN_LOCALESspells the codesetUTF-8, as glibc'sLANGdoes, whilelocale -aprintsutf8. Nobody has verified whethersetlocaletreats the two alike.
Minimum Rust version
Rust 1.88. To check it, install the toolchain and run the tests with +1.88:
rustup toolchain install 1.88 --profile minimal
cargo +1.88 test
License
Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.