aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / store / README.md
| 1 | # native/store: torn_slot, Store v1 C twin, sealed store |
| 2 | |
| 3 | All C (gnu11), no outside library, offline. Library objects use no heap, no |
| 4 | clock and no I/O except through `disk_dev` / `st_dev` / `ts_device`; every |
| 5 | buffer is a caller-provided workspace (`st_workspace` about 0.9 MiB, |
| 6 | `ss_workspace` about 2.0 MiB, both meant for static storage). |
| 7 | |
| 8 | | file | what | |
| 9 | |---|---| |
| 10 | | `torn_slot.c/.h` | dual-slot torn-write-safe 4096-byte record (unchanged) | |
| 11 | | `store_v1.c/.h` | Store v1 format (ADR 0015): ObjectId, CRC32C, catalog, commit record, superblock, genesis | |
| 12 | | `store_engine.c/.h` | `Store::open` / `transact` / `read_object` twin of `crates/aienos-kernel/src/store/engine.rs` | |
| 13 | | `store_disk.c` | unit adapter over `native/disk`: 512-byte blocks = 8 per unit, 4096 = 1, base LBA, no read-modify-write | |
| 14 | | `store_sealed.c/.h` | M5 sealed store (envelopes + transaction records + anchor) | |
| 15 | | `store_mem.c` | `mem*` for the freestanding build only | |
| 16 | |
| 17 | ## What the C twin covers |
| 18 | |
| 19 | * Format primitives and genesis, byte for byte. |
| 20 | * Open: Valid and DegradedRecovery; every MountError (Io, Unformatted, |
| 21 | Foreign, UnsupportedVersion, CorruptRecoveryRequired, ConflictingRoots, |
| 22 | InconsistentHistory) and every PeerCondition (Zero, Valid, Malformed, |
| 23 | GraphBadNewer, GraphBadOlder); `select_two` and the append-transition rules. |
| 24 | * Transact with the exact Rust write and flush order and checkpoint hook: |
| 25 | objects, catalog, commit record, flush, inactive superblock, flush. Every |
| 26 | StoreError, including poisoned -> NeedsReopen. |
| 27 | * Adapter refuses other block sizes, unaligned bases and regions past the end. |
| 28 | |
| 29 | ## Byte-identity evidence |
| 30 | |
| 31 | * Golden vectors (`docs/adr/0015-golden-vectors.json`): all 89 JSON rows are |
| 32 | turned into a C table by `tests/gen_golden.sh` (POSIX sh + awk) and every |
| 33 | row is checked in both directions. A C transaction from the genesis vector |
| 34 | reproduces the `adjacent_generations` units byte for byte. |
| 35 | * All Rust negative tests ported: `store_v1_negative.rs` (10), the |
| 36 | `v1.rs` malformed cases and the 14 `engine.rs` tests, plus C-only cases. |
| 37 | * C -> Rust (`tests/rust_crosscheck.sh`, 48 checks, 512 and 4096 geometry): |
| 38 | the Rust `aienos-store-tool` runs `tear-closure`, `corrupt-kind` and all 8 |
| 39 | `inject` cases on images the C engine wrote; the C engine then gives the |
| 40 | same mount or refusal as Rust's own `Store::open`. The Rust tool for that |
| 41 | check was built from 7b023f6; the Rust store and tool sources matched |
| 42 | 913b962. They still match that commit aside from the Apache-2.0 headers |
| 43 | (#448). 913b962 is not current origin/main. |
| 44 | * Rust -> C: no Rust-written image is used. That direction is covered by the |
| 45 | golden vectors only. |
| 46 | * Discrepancy found by reading (not run in Rust): Rust `preflight` |
| 47 | binary-searches the catalog after appending unsorted new entries, so a |
| 48 | request of [new X with a smaller id, existing Y] can miss Y and append it |
| 49 | twice. C searches only the old catalog and deduplicates; tested in C. |
| 50 | |
| 51 | ## Sealed store layout (`store_sealed.h`) |
| 52 | |
| 53 | * Device: anchor region of 4 units (torn_slot) outside the Store region. |
| 54 | * Kind `0x0510` = M5 envelope (AES-256-GCM-SIV, 4096-byte chunks, at most |
| 55 | 16 KiB plaintext, 8 objects per transaction). Kind `0x0511` = sealed |
| 56 | transaction record. Existing kinds are 1, 2 and 16 to 23; no collision. |
| 57 | * Record `AIENSTX1`: version (2; a version-1 record is refused with |
| 58 | `SS_E_FORMAT_VERSION`, reformat), identity class, count, store uuid, Store |
| 59 | generation, counter (= generation), key generation, SHA-256 of the previous |
| 60 | record (zero at generation 2), then per object the Store ObjectId and the |
| 61 | 144-byte M5 commit record (binds uuid, kind, envelope id, generation, |
| 62 | counter, sequence, SHA-256 of the envelope), then |
| 63 | HMAC(k_root_auth, "AIENOS-STORE-SEALED-TX-V1\0" || body). |
| 64 | * Envelope id (16 bytes) and nonce prefix (8 bytes), record version 2 = |
| 65 | the first 24 bytes of HMAC(k_envid, "AIENOS-SEALED-ENVID-V2\0" || uuid || |
| 66 | generation u64 || index u32 || kind u16 || version u16 || key_generation |
| 67 | u64 || identity_class u8 || plaintext_len u64 || SHA-256(plaintext)), |
| 68 | k_envid = HKDF-Expand(k_domain, "AIENOS-SEALED-ENVID-KEY-V2\0"). The |
| 69 | envelope id also feeds the per-object key, so the AES-GCM-SIV (key, nonce) |
| 70 | pair of every chunk is a keyed function of the plaintext. |
| 71 | Why (Lane 26, 2026-10-01): version 1 used SHA-256(uuid || generation || |
| 72 | index), so after a whole-disk rollback (undetected without TPM NV) a |
| 73 | rewrite of the same generation with different bytes reused the (key, |
| 74 | nonce) pair. GCM-SIV keeps confidentiality under that reuse except for |
| 75 | equal messages, but the store promised unique nonces and the margin was |
| 76 | not worth keeping. Now only the identical object at the same place gives |
| 77 | the identical envelope (a retried generation after a crash, or a rewrite |
| 78 | after rollback), which shows nothing the old image did not already show. |
| 79 | No entropy source is needed, so the library stays clock-, heap- and |
| 80 | RNG-free and the kernel call sites are unchanged. `ss_read` recomputes |
| 81 | the rule after decrypting and refuses an envelope whose id or nonce |
| 82 | prefix does not match it (`SS_E_ENVELOPE`). |
| 83 | * Anchor record `AIENSAN1`: class, uuid, then the 120-byte `m5_anchor` |
| 84 | (counter = Store generation, digest = SHA-256 of the newest record). |
| 85 | |
| 86 | Mount: `st_open` -> `ts_recover` -> verify every record (class, uuid, MAC, |
| 87 | exactly one per generation 2..G, linked digests, every envelope claimed once |
| 88 | with a matching digest, no other kinds) -> `m5_evaluate_anti_rollback`. |
| 89 | An empty anchor is accepted only at generation 1. |
| 90 | |
| 91 | Commit order: (1) if the anchor lags, write the anchor for the current |
| 92 | generation; (2) seal envelopes and the record; (3) `st_transact` (Store |
| 93 | generation G+1 durable); (4) `ts_commit` the anchor for G+1. A crash between |
| 94 | (3) and (4) mounts as "prepared advance" (anchor one behind) and step (1) of |
| 95 | the next transaction catches it up. Proven by a power cut at every block |
| 96 | boundary. |
| 97 | |
| 98 | Refused: Store rolled back under the anchor, anchor two behind, a missing |
| 99 | anchor, a forked anchor, two anchor slots with one sequence, PRODUCTION/TEST |
| 100 | mix, another store's anchor, other volume key, swapped / unclaimed / tampered |
| 101 | envelopes, plaintext objects, generation skip, stale commit record, replayed |
| 102 | record, broken chain, two records for one generation, an envelope claimed twice, |
| 103 | a version-1 transaction record (`SS_E_FORMAT_VERSION`) and, at read, an |
| 104 | envelope whose id or nonce prefix does not follow the version-2 rule. |
| 105 | |
| 106 | ## Tests |
| 107 | |
| 108 | `make test` (ends with `AIENOS_STORE_NATIVE: PASS|FAIL`), `make sanitize` |
| 109 | (ASan + UBSan over all tests), `make mutants` (`TORN_SLOT_MUTANTS` and |
| 110 | `AIENOS_STORE_MUTANTS`), `make freestanding` (`nm -u` closes over the sibling |
| 111 | native objects), `make lib-checks`. All runs are host emulation over files. |
| 112 | |
| 113 | [`tests/SEALED-COVERAGE.md`](tests/SEALED-COVERAGE.md) maps the sealed store's |
| 114 | rollback and nonce-safety properties (confidentiality under nonce misuse, |
| 115 | authentication, replay, crash consistency, whole-image freshness) to these |
| 116 | tests and mutants, including the independent envelope-id oracle and the |
| 117 | undetected whole-image rollback (#32). |
| 118 | |
| 119 | Every `/* GUARD:name */` line in the four store sources is a mutant: `make |
| 120 | mutants` deletes it and requires some store test to fail. Three Rust-parity |
| 121 | checks are deliberately untagged because no input can reach them alone: |
| 122 | the engine's root commit-id comparison (the superblock-to-commit match |
| 123 | refuses the same case first), the adjacent-generations check (the |
| 124 | predecessor check requires the same thing) and DuplicateObjectConflict inside one request |
| 125 | (equal ObjectIds with different kind, version or bytes need a SHA-256 |
| 126 | collision). |
| 127 | |
| 128 | ## Not done |
| 129 | |
| 130 | * Kernel binding: done. The C kernel links the sealed Store (`native/kernel/stage.mk`, |
| 131 | `native/kernel/svc/store_boot.c`). It runs in the QEMU Store gates and on the Raspberry Pi 5 |
| 132 | SD card with throwaway secrets (`docs/native/PI5-4-UNLOCK-RUNS.md`, |
| 133 | `docs/native/PI5-6-AGENT-RUNS.md`). |
| 134 | * Physical NVMe qualification: not run; nothing here claims it. |
| 135 | * Whole-disk rollback: the anchor is on the same disk, so rolling back both |
| 136 | regions together is not detected (a test records this). That needs TPM NV |
| 137 | (TRUST-1). |
| 138 | * `K_vol` rotation (ADR 0017 section 2.7 item 2; `active_key_epoch` stays 1), |
| 139 | more than 8 objects or 16 KiB per sealed transaction, and |
| 140 | repair of a DegradedRecovery mount (it stays read-only, as in Rust). |
| 141 | Recovery-secret rotation (section 2.7 item 1) is implemented: the |
| 142 | KeySlotManifest chain is in `store_sealed.c`, and |
| 143 | `ck_store_rotate_recovery` is in `native/kernel/svc/store_boot.c`. |