diff --git a/.crumb b/.crumb index e67ef952..f8510256 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "8297c9aa81a1feb24ac4beb4ecca58003f3bec5b7f475e40ad237da2868caf01", - "last_commit": "3647654", + "digest": "ff4b451c83c055a494d6598f83241ead8cc7bd43626e37cbadf4bf65ace5b74c", + "last_commit": "fe21660", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "c8bdee2362c57e35e83ede896d04990a8bf52975aa22d4fb0d57cc4855b7391e", - "last_commit": "50546b6", + "digest": "51c89e915314b1fea02b9fcb80f143c3146a4b52d71e090d0575ba89c996b0c1", + "last_commit": "04e39e7", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "e35f107caf8f963b07c4c8885278702812e3af2af5451b6b47793c58890d18a0", - "last_commit": "cf5b432", + "digest": "8911239203abaca6f5315fecaa93bea68b0b425436b711cce44efeccdf221bdb", + "last_commit": "04e39e7", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:040ce2ca531f850fab8748870c4e4d7aa5274e2ceb91e9c029c91358401dbc05", - "compiled_at": "2026-10-12T20:24:30Z", + "children_root": "sha256:a6bcb849b01c4c333bfdd51007f1162ee7111bbe6748ee32b20f4c94e56a8d5d", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:119ee1e24071dfb90ade0dad3d84aadac6a70ea401811096425b14c23f3bc443", + "digest": "sha256:23d2ddcc47b08a59140989b78ab32c86597436b8c497d233ac08c1ecf10ae23d", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3115, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:b4ec1b4f4fbe1755f9848dc4c603a70b8d92e16290ed6e09bf97fb657c492603" + "files": 3117, + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:55fabf3b3fb0b386f168198e9fa7d26c1b5d9d17ad41856e76dec00ccc49778f" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 79272995..4ca41f86 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:baea130f2c52779b57d90139beb66ac01b21ab499d113471a334be5500f3e369", - "compiled_at": "2026-10-12T19:04:55Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:8297c9aa81a1feb24ac4beb4ecca58003f3bec5b7f475e40ad237da2868caf01", + "digest": "sha256:ff4b451c83c055a494d6598f83241ead8cc7bd43626e37cbadf4bf65ace5b74c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 74, - "generated_at_commit": "44dc7ee624d1b72eb3930cd35f55629c2c12a008", - "source_tree": "sha256:dc7a15a49ce75b0c9c96fbafad653d3e6c0363fd2c89085379f4de89a13d8693" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:87b7ec7512cc0767c9b71c8c869251782ac516418bba44b9efb93c764d3cb912" }, "owner_lane": "", "provenance": { diff --git a/docs/NATIVE_IMAGE_SLOTS.md b/docs/NATIVE_IMAGE_SLOTS.md index 4d7568a0..f3ee8cdf 100644 --- a/docs/NATIVE_IMAGE_SLOTS.md +++ b/docs/NATIVE_IMAGE_SLOTS.md @@ -1,8 +1,9 @@ -# Native image slots: install, update and recovery without Linux (P11 cuts 1, 2a and 2b) +# Native image slots: install, update and recovery without Linux (P11 cuts 1, 2a, 2b and 3) STATUS: cut 1 (format, rules, host tool), cut 2a (SLOTLDR.EFI, a separate slot loader in front of the unchanged image, QEMU gate PASS) and cut 2b -(kernel-side promote / abandon, section 8) done; the owner +(kernel-side promote / abandon, section 8) and cut 3 (the inactive-slot +updater and the loader in the package, section 9) done; the owner approved the loader on 2026-10-12 (section 2). Nothing here has run on hardware (hardware NOT_RUN). The kernel image and its stub are unchanged. SECURITY_IMPACT: PERSISTENT_FORMAT + RECOVERY (a new on-disk format and a new @@ -212,6 +213,7 @@ nothing and to re-release anything. path, verifies by reading back, stages through the torn-slot region, and promotes after its health milestone. Qualifies interrupted updates, tampered images, failed candidates and authorized rollback (the P11 list). + **Cut 3 (done, QEMU; section 9).** 4. **Known-good recovery image.** The same loader plus a slot image on recovery media built by our tools, for the dual-invalid case. Then N7 is run on the physical candidate inside an attended window. @@ -353,5 +355,82 @@ record mutations (each refused with its reason), the kernel-action scenarios and the shared commit routine. Not here: the updater that writes a new candidate into the inactive slot -(cut 3) and the recovery image for the dual-invalid case (cut 4). Hardware -NOT_RUN. +(cut 3, section 9) and the recovery image for the dual-invalid case (cut 4). +Hardware NOT_RUN. + +## 9. The inactive-slot updater (cut 3) + +The running service kernel writes a new signed image into the slot it is +NOT running from, then stages it as the candidate; the loader tries it on +the next boot and the kernel promotes it after its health milestone +(section 8). One routine does the work, `ck_slot_update` +(`native/boot/slot_update.c`), shared by the kernel and the host tool's +self-test, over the same control-commit routine as the loader. + +**Source: the update inbox partition.** GPT type +`58D9C90E-497A-4848-95DD-EB7E22B0CE96` (`CK_SLOT_INBOX_TYPE`), on the same +NVMe disk, with one slot's shape: the signed manifest in unit 0, the image +from unit 1. The desk tool fills it from a package +(`ck_slot_tool inbox-image --m slot/MANIFEST.signed.bin,image/BOOTAA64.EFI`); +the kernel only reads it. How the inbox is filled on the physical machine +(an owner-attended step) is not part of this cut. + +**Operator verb `update`** (lifecycle READY only: a LOCKED Store, or a +pending task, refuses it; `core/lifecycle_fsm.c`). The kernel: + +1. refuses unless it was started by SLOTLDR.EFI (boot record), a control + record exists, no candidate is staged, and the booted slot is the active + one; the target is the other slot; +2. reads the inbox manifest and image and runs the loader's own + `ck_slot_verify` (Ed25519 signature by a trusted key, SHA-512 of the + image, size within the slot) and a dry run of + `ck_slot_stage_candidate` (generation above the floor). Any refusal: + **nothing is written**; +3. zeroes the target's manifest unit and flushes: from here the target is + invalid and the loader keeps booting the active slot; +4. writes the image units, flushes; then the manifest last, flushes; +5. reads the whole target back and verifies it again from the disk bytes + (a mismatch zeroes the manifest unit again and refuses); +6. stages: one control record commit (candidate = target, three tries). + +Every write is bounded to the target slot's unit range; the active slot and +the control region (except the one commit of step 6) are never written. An +interruption before step 6 leaves the control record untouched and the +target invalid (or, only between steps 5 and 6, valid but not staged, which +the loader ignores until a later `update` stages it). + +**Trusted keys.** The service image is built with the same key table as +SLOTLDR.EFI (`ck_slot_tool keys-c`): RFC 8032 TEST 1 in the TEST-keys +service image (`CK_SERVICE_TEST_KEYS=1`, QEMU only), the Owner Root from +`CK_OWNER_PUBKEYS` under `CK_HARDWARE_STAGING`; the plain service image +has no table and refuses every update. TEST keys under hardware staging +stay refused by the Makefile (gate row 15) and by `keys-c`. + +**Evidence.** +- `make -C native/kernel slot-test`: 155 checks. The 22 added for cut 3: a + positive update (B written, verified from disk, staged, A unchanged, the + loader's next decision is a candidate try; exhausted tries and an invalid + candidate fall back to A), refusal while a candidate is staged, an + injected I/O error at each of the 16 writes of an update (every one + refused with A and the control record untouched and B invalid or + unstaged), a stop at each step, and ten refusals before any write (bad + signature, wrong key, wrong digest, truncated inbox, generation at the + floor, booted slot not active, no trusted key, no control record, image + larger than the slot, corrupt manifest). Three mutants of + `slot_update.c` (writing the active slot, staging before the image, + skipping signature verification) each fail these checks. +- `scripts/qemu_ck_slot_loader_test.sh` rows 12-15 (`docs/GATES.md`): + row 12 update from slot A writes and stages B, a second `update` is + refused, A's bytes are unchanged, the next boot tries B and the kernel + promotes it; row 13 QEMU is killed in the middle of the image write + (TEST-only `update halt-after=1`), the control record and A are + untouched, the loader refuses the half-written B and boots A, and a new + `update` then succeeds; row 14 an inbox signed by the wrong key and one + with a flipped image byte are refused with the slots partition + byte-identical; row 15 the update is refused while the Store is locked, + and a TEST-key updater image is refused under hardware staging. +- The package carries SLOTLDR.EFI (`docs/NATIVE_PACKAGE.md`, "Slot loader + in the package"). + +Not here: the known-good recovery image for the case where both slots are +invalid (cut 4). Hardware NOT_RUN: nothing was written to a physical disk. diff --git a/docs/NATIVE_PACKAGE.md b/docs/NATIVE_PACKAGE.md index 0e613eec..6ba15247 100644 --- a/docs/NATIVE_PACKAGE.md +++ b/docs/NATIVE_PACKAGE.md @@ -28,11 +28,11 @@ with the release candidate." | `slot/MANIFEST.tbs` | the 448 bytes the owner signs, written by `tbs --package DIR --pubkey HEX64` (or `build --pubkey`). The manifest's `signer_id` (SHA-256 of the owner public key) lies inside the signed bytes, so the bytes to sign depend on which key will sign | | `slot/MANIFEST.signed.bin` | after `sign`: the manifest with signer id and signature, verified before it is written | -`PACKAGE.txt` lines (format 2), grouped as the owner's correction names them: +`PACKAGE.txt` lines (format 3; the loader lines are in "Slot loader in the package"), grouped as the owner's correction names them: | Group | Lines | |---|---| -| Source revision | `format 2`, `repo aien-dev/aienos`, `commit <40 hex>`, `commit_time `, `tree_clean yes` (`no` only for test builds with `--allow-dirty`) | +| Source revision | `format 3`, `repo aien-dev/aienos`, `commit <40 hex>`, `commit_time `, `tree_clean yes` (`no` only for test builds with `--allow-dirty`) | | What was asked for | `variant`, `generation`, `policy_generation`, `min_rollback_generation` | | Toolchain | `toolchain_cross ` (the `CROSS` value the kernel Makefile resolves, asked from make itself), `toolchain_cc`, `toolchain_ld`, `toolchain_objcopy` (first line of `gcc/ld/objcopy --version`), `toolchain_make` (first line of `make --version`), `build_host_arch` (`uname -m`) | | Inputs | `input_make_goal` (`all` for core, `full` otherwise), `input_make_vars` (`none`, or `CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=sha256: CK_MACHINE_ID=sha256:`), `input_env_ck none` (any `CK_*` in the environment or in an outer make's `MAKEFLAGS` is refused, so nothing unrecorded selects the image), `input_makefile_sha256` (`native/kernel/Makefile`), `input_image_ld_sha256` (`native/boot/image.ld`), `pin ` per `--pins` line, `model_bytes`/`model_sha256` from `--model`, `firmware ` per `--firmware`, and for the staging variant `owner_pubkeys_sha256` and `machine_id_sha256` | @@ -45,10 +45,8 @@ provenance; the provenance is `package_sha256`; `SHA256SUMS` covers `PACKAGE.txt` and the image. Change any record line and the provenance no longer matches; the self-test proves each of these refusals. -TODO (slot loader, `docs/NATIVE_IMAGE_SLOTS.md` section 6 cut 2, in progress -on another branch): when `SLOTLDR.EFI` exists, the package must carry it (or -name the loader package it was tested with) and record its digest. This cut -does not touch the loader or the Makefile. +The slot loader `SLOTLDR.EFI` is part of the package since P11 cut 3 (format +3): see "Slot loader in the package" at the end of this document. ## What the record proves and what it does not @@ -136,11 +134,37 @@ About 40 s, nothing booted. ## What is still missing for N7 -The package is the input to the updater (slots cut 3) and to the known-good -recovery image (slots cut 4). Neither exists yet. The owner approved the -slot loader on 2026-10-12 (`docs/NATIVE_IMAGE_SLOTS.md` section 2); its first -cut, `SLOTLDR.EFI` (slots cut 2a, QEMU only), exists but is not in the package -yet: a follow-up adds `SLOTLDR.EFI` and its SHA-256 to `PACKAGE.txt` and -`SHA256SUMS`, built with the owner's `CK_SLOT_KEYS`. Install today is still +The package is the input to the updater (slots cut 3, QEMU only: +`docs/NATIVE_IMAGE_SLOTS.md` section 9) and to the known-good recovery image +(slots cut 4). The owner approved the slot loader on 2026-10-12 +(`docs/NATIVE_IMAGE_SLOTS.md` section 2); the package carries it since cut 3 +(section below). Install today is still the one-time BootNext procedure of `docs/NATIVE_BOOT_ONE_TIME.md`, which N7 rejects as the supported path. + +## Slot loader in the package (P11 cut 3) + +Format 3 adds the A/B slot loader (`native/boot/slot_loader.c`, +`docs/NATIVE_IMAGE_SLOTS.md`) to every package: + +| File / line | Meaning | +| --- | --- | +| `loader/SLOTLDR.EFI` | `make slot-loader`, built twice from the same tree like the image; the two builds must be byte-identical or `build` refuses | +| `loader loader/SLOTLDR.EFI` | path inside the package | +| `loader_keys owner` or `test` | `owner` for `--variant hardware-staging`: the `--owner-pubkeys` file is the loader's `CK_SLOT_KEYS` (Owner Root keys; `ck_slot_tool keys-c` refuses an RFC 8032 TEST key in it). `test` for `core` and `full`: the labelled TEST 1 key loader (`CK_TEST_SLOT_KEY=1`), never for hardware | +| `loader_bytes`, `loader_sha256` | size and SHA-256 of the loader | +| `loader_reproducible two_clean_builds_identical` | the two builds matched | + +`SHA256SUMS` covers `loader/SLOTLDR.EFI`, so the owner's one signature over +the slot manifest (whose provenance is `package_sha256`) binds the loader too. +The loader itself is not a slot payload: it is installed on the ESP once +(owner-attended, not part of this cut) and verifies slot manifests with the +keys it was built with. `verify` refuses a package whose loader bytes or +digest differ from the record, whose `loader_keys` does not match the +variant, a `test` loader without its "TEST-ONLY key" note, or an `owner` +loader carrying any TEST-ONLY note. Self-test rows (`make package-test`): +record lines, coverage by `SHA256SUMS`, a tampered loader byte, a missing +loader, tampered `loader_sha256` / `loader_keys` / missing `loader_bytes` +lines, and the hardware-staging package's owner-key loader without a +TEST-ONLY note. Format 2 packages (no loader) are refused by format 3 +`verify`; rebuild them. diff --git a/native/.crumb b/native/.crumb index 82f82b67..7eac8fb8 100644 --- a/native/.crumb +++ b/native/.crumb @@ -55,8 +55,8 @@ "purpose": "Directory native/argus. Purpose not yet described by a human." }, { - "digest": "0b5769362a82f2b2d66ef490df93fc79eff8829ed6ee933bdedae2adecb8b240", - "last_commit": "3647654", + "digest": "43c12487524be9f610211b885b936ade7cb7fc907c7afbd37c57cbd517dfea81", + "last_commit": "fe21660", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "85bc49d4e8d7d16a24ba3b095076c1187da66f23de8416b68f7d42cd0776095a", - "last_commit": "50546b6", + "digest": "9ac3378c865ea4bfbf2380a6e9cb5dea03264870381bf314e96ce62c99e20a38", + "last_commit": "04e39e7", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:574b81970e341fc76d5af24f7e2690bc431521c7a447a45e2af87ef81c372f78", - "compiled_at": "2026-10-12T20:24:30Z", + "children_root": "sha256:7231f6ca9214a6886e126014dbd82c37be36c7e5ddf884ab0321cdc04c8650bd", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:c8bdee2362c57e35e83ede896d04990a8bf52975aa22d4fb0d57cc4855b7391e", + "digest": "sha256:51c89e915314b1fea02b9fcb80f143c3146a4b52d71e090d0575ba89c996b0c1", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 486, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:68b0e08270c02d99a700008302efdec6dd5c1504e78ce28e8f18b853ec72c98b" + "files": 488, + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:198cec669d0d505617ff5b4c423c4296ded1081ee2e1e2e0abdc88fa5c0f154d" }, "owner_lane": "", "provenance": { diff --git a/native/boot/.crumb b/native/boot/.crumb index 08e4dce4..fcebea11 100644 --- a/native/boot/.crumb +++ b/native/boot/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T19:04:55Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:0b5769362a82f2b2d66ef490df93fc79eff8829ed6ee933bdedae2adecb8b240", + "digest": "sha256:43c12487524be9f610211b885b936ade7cb7fc907c7afbd37c57cbd517dfea81", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 14, - "generated_at_commit": "44dc7ee624d1b72eb3930cd35f55629c2c12a008", - "source_tree": "sha256:eb2981689c1be6f556d1f70211b1d6fbba57daba2530b49257ea1fc72163ac1c" + "files": 16, + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:e871c0a19ada2f9543ed55c0455a103d92dc66cc32564dd59abd6bc7341dc617" }, "owner_lane": "", "provenance": { diff --git a/native/boot/slot_format.h b/native/boot/slot_format.h index eb8a04c1..ffdc81be 100644 --- a/native/boot/slot_format.h +++ b/native/boot/slot_format.h @@ -9,7 +9,8 @@ * record only to consume a candidate try. The service kernel * (native/kernel/svc/slot_service.c, cut 2b) promotes a healthy candidate and * abandons an exhausted or invalid one over its own NVMe path, told which slot - * it is by the loader's boot record (below). Staging is cut 3. This header, + * it is by the loader's boot record (below). Staging is the cut 3 updater + * (slot_update.h, inbox partition type below). This header, * slot_select.c and the host tool native/kernel/tools/ck_slot_tool.c pin * the format and the decision rules (mutation-tested in make slot-test). * @@ -85,6 +86,14 @@ static inline uint64_t ck_slot_manifest_unit(unsigned slot, uint64_t cap_units) #define CK_SLOT_PART_TYPE_BYTES \ {0xcd, 0x96, 0x10, 0x8b, 0x28, 0x79, 0x08, 0x4e, 0xb5, 0xe4, 0xc1, 0x9b, 0x6b, 0x53, 0x27, 0x60} +/* 58D9C90E-497A-4848-95DD-EB7E22B0CE96: AIENOS update inbox partition type (P11 + * cut 3, native/boot/slot_update.h). Same shape as ONE slot: unit 0 is the + * signed manifest, the image starts at unit 1. The desk tool writes it + * (ck_slot_tool inbox-image); the kernel only reads it. */ +#define CK_SLOT_INBOX_TYPE_TEXT "58D9C90E-497A-4848-95DD-EB7E22B0CE96" +#define CK_SLOT_INBOX_TYPE_BYTES \ + {0x0e, 0xc9, 0xd9, 0x58, 0x7a, 0x49, 0x48, 0x48, 0x95, 0xdd, 0xeb, 0x7e, 0x22, 0xb0, 0xce, 0x96} + /* ---- manifest ------------------------------------------------------------ */ #define CK_SLOT_MANIFEST_MAGIC "AIENSLT1" #define CK_SLOT_MANIFEST_VERSION 1u diff --git a/native/boot/slot_update.c b/native/boot/slot_update.c new file mode 100644 index 00000000..04752d78 --- /dev/null +++ b/native/boot/slot_update.c @@ -0,0 +1,148 @@ +/* slot_update.c -- the inactive-slot updater (contract in slot_update.h). */ +#include "slot_update.h" + +static void zero(uint8_t *p, uint64_t n) +{ + for (uint64_t i = 0; i < n; i++) + p[i] = 0; +} + +static int refuse(const char **why, const char *w) +{ + if (why) + *why = w; + return -1; +} + +/* Write count units starting at unit u of the slots partition, refusing any + * unit outside the target slot [lo, hi). */ +static int put_units(const struct ck_slot_update_io *io, uint64_t lo, uint64_t hi, uint64_t u, uint64_t count, + const uint8_t *src) +{ + uint32_t bpu = CK_SLOT_UNIT / io->slots->block_size; + if (u < lo || count > hi - u || count == 0) + return -2; + return disk_write(io->slots, u * bpu, (uint32_t)(count * bpu), src) == DISK_OK ? 0 : -1; +} + +static int get_units(const disk_dev *d, uint64_t u, uint64_t count, uint8_t *dst) +{ + uint32_t bpu = CK_SLOT_UNIT / d->block_size; + return disk_read(d, u * bpu, (uint32_t)(count * bpu), dst) == DISK_OK ? 0 : -1; +} + +static int step(const struct ck_slot_update_io *io, const char *name, uint64_t done, uint64_t total) +{ + return io->progress ? io->progress(io->ctx, name, done, total) : 0; +} + +/* Units per write call: bounded so one call never asks the backend for more + * than 64 units (256 KiB); disk_write splits further by max_blocks_per_io. */ +#define CHUNK_UNITS 64u + +int ck_slot_update(const struct ck_slot_update_io *io, struct ck_slot_update_result *res, const char **why) +{ + static struct ck_slot_manifest man, back; + static struct ck_slot_control staged; + struct ck_slot_eval ev; + const char *w = 0; + if (!res) + return refuse(why, "no result"); + zero((uint8_t *)res, sizeof *res); + if (!io || !io->slots || !io->inbox || !io->control || !io->buf || !io->keys) + return refuse(why, "update arguments"); + if (io->nkeys == 0) + return refuse(why, "this image trusts no slot signing key"); + const struct ck_slot_control *ctl = io->ctl; + if (!ctl || ck_slot_control_check(ctl, &w) != 0) + return refuse(why, ctl ? w : "no control record on disk"); + if (ctl->candidate_slot != CK_SLOT_NONE) + return refuse(why, "a candidate is already staged: boot it (promote or abandon) first"); + if (io->booted_slot >= CK_SLOT_COUNT || io->booted_slot != ctl->active_slot) + return refuse(why, "the booted slot is not the active slot"); + for (const disk_dev *d = io->slots;; d = io->inbox) { + if (disk_check(d) != DISK_OK || (d->block_size != 512 && d->block_size != 4096)) + return refuse(why, "partition geometry"); + if (d == io->inbox) + break; + } + unsigned t = 1u - ctl->active_slot; + uint64_t units = io->slots->block_count / (CK_SLOT_UNIT / io->slots->block_size); + uint64_t cap = ck_slot_cap_units(units); + uint64_t in_units = io->inbox->block_count / (CK_SLOT_UNIT / io->inbox->block_size); + if (cap < CK_SLOT_MIN_CAP_UNITS || in_units < 2) + return refuse(why, "slots or inbox partition too small"); + uint64_t lo = ck_slot_manifest_unit(t, cap), hi = lo + cap; + res->target = t; + + /* 2. Read and verify; nothing is written before this passes. */ + if (get_units(io->inbox, 0, 1, (uint8_t *)&man) != 0) + return refuse(why, "inbox manifest read failed"); + if (ck_slot_manifest_check(&man, &w) != 0) + return refuse(why, w); + uint64_t len = man.image_len, n = (len + CK_SLOT_UNIT - 1) / CK_SLOT_UNIT; + if (n > cap - 1) + return refuse(why, "image larger than the slot"); + if (n > in_units - 1) + return refuse(why, "image longer than the inbox (truncated package)"); + if (n * CK_SLOT_UNIT > io->buf_cap) + return refuse(why, "image larger than the update buffer"); + for (uint64_t u = 0; u < n; u += CHUNK_UNITS) { + uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; + if (get_units(io->inbox, 1 + u, c, io->buf + u * CK_SLOT_UNIT) != 0) + return refuse(why, "inbox image read failed"); + } + if (!ck_slot_verify(&man, io->buf, (cap - 1) * CK_SLOT_UNIT, io->keys, io->nkeys, &ev)) + return refuse(why, ev.why ? ev.why : "inbox image refused"); + for (uint64_t i = 0; i < sizeof staged; i++) + ((uint8_t *)&staged)[i] = ((const uint8_t *)ctl)[i]; + if (ck_slot_stage_candidate(&staged, t, man.generation, &w) != 0) + return refuse(why, w); + if (step(io, "verified", 0, n)) + return refuse(why, "stopped before writing"); + + /* 3. Invalidate the target first. */ + static uint8_t zu[CK_SLOT_UNIT]; + res->wrote = 1; + if (put_units(io, lo, hi, lo, 1, zu) != 0 || disk_flush(io->slots) != DISK_OK) + return refuse(why, "invalidating the target slot failed"); + if (step(io, "invalidated", 0, n)) + return refuse(why, "stopped after invalidating the target slot"); + + /* 4. Image units. */ + for (uint64_t u = 0; u < n; u += CHUNK_UNITS) { + uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; + if (put_units(io, lo, hi, lo + 1 + u, c, io->buf + u * CK_SLOT_UNIT) != 0) + return refuse(why, "image write failed (target slot left invalid)"); + res->units_written += c; + if (step(io, "image", u + c, n)) + return refuse(why, "stopped while writing the image (target slot left invalid)"); + } + if (disk_flush(io->slots) != DISK_OK) + return refuse(why, "image flush failed (target slot left invalid)"); + + /* 5. Manifest last, then read everything back and verify again. */ + if (put_units(io, lo, hi, lo, 1, (const uint8_t *)&man) != 0 || disk_flush(io->slots) != DISK_OK) + return refuse(why, "manifest write failed (target slot left invalid)"); + res->units_written++; + if (step(io, "manifest", n, n)) + return refuse(why, "stopped after the manifest (slot valid, not staged)"); + int ok = get_units(io->slots, lo, 1, (uint8_t *)&back) == 0; + for (uint64_t u = 0; ok && u < n; u += CHUNK_UNITS) { + uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; + ok = get_units(io->slots, lo + 1 + u, c, io->buf + u * CK_SLOT_UNIT) == 0; + } + if (!ok || !ck_slot_verify(&back, io->buf, (cap - 1) * CK_SLOT_UNIT, io->keys, io->nkeys, &ev)) { + (void)put_units(io, lo, hi, lo, 1, zu); + (void)disk_flush(io->slots); + return refuse(why, "read-back verification of the written slot failed (slot invalidated)"); + } + + /* 6. Stage. */ + int crc = ck_slot_control_commit(io->control, &staged, &res->seq); + if (crc != TS_OK) + return refuse(why, "staging commit failed (slot written and valid, not staged)"); + res->generation = man.generation; + res->image_len = len; + return 0; +} diff --git a/native/boot/slot_update.h b/native/boot/slot_update.h new file mode 100644 index 00000000..7077798d --- /dev/null +++ b/native/boot/slot_update.h @@ -0,0 +1,63 @@ +/* slot_update.h -- write a signed candidate image into the INACTIVE slot + * (native v1 plan P11 cut 3, gate N7; docs/NATIVE_IMAGE_SLOTS.md section 9). + * + * One routine, shared by the service kernel (svc/slot_service.c, operator + * verb `update`) and the host tool (ck_slot_tool, its self-test injects an + * I/O error at every write). Source: the update inbox partition + * (CK_SLOT_INBOX_TYPE, one slot's shape: manifest unit, then the image). + * + * Order, and what an interruption at each point leaves behind: + * 1. refuse unless: control record present, no candidate staged, the + * booted slot is the active one; target = the other slot. Nothing read. + * 2. read the inbox manifest and image, ck_slot_verify against the image's + * trusted keys (signature over the manifest, SHA-512 of the image), and + * dry-run ck_slot_stage_candidate (generation above the floor) on a copy + * of the control record. Any refusal: NOTHING written. + * 3. overwrite the target's manifest unit with zeros, flush. From here the + * target slot is INVALID until step 5 completes; the control record is + * untouched, so the loader keeps booting the active slot. + * 4. write the image units, flush. + * 5. write the manifest unit, flush; read the whole slot back and verify it + * again (signature + digest from the disk bytes). A failed read-back + * zeroes the manifest unit again (best effort) and refuses. + * 6. stage: one control record commit (ck_slot_control_commit), candidate = + * target, tries = CK_SLOT_MAX_TRIES. Only now does the loader try it. + * Every write lies inside the target slot's unit range [M, M + CAP) (checked + * before each write); the active slot and the control region (except the one + * commit of step 6) are never written. Freestanding, no allocation: the + * caller provides the image buffer. */ +#ifndef AIENOS_CK_SLOT_UPDATE_H +#define AIENOS_CK_SLOT_UPDATE_H +#include "slot_control.h" +#include "../disk/disk.h" + +struct ck_slot_update_io { + const disk_dev *slots; /* the slots partition (bounded view) */ + const disk_dev *inbox; /* the update inbox partition (read only) */ + ts_region *control; /* opened and recovered over the slots partition */ + const struct ck_slot_control *ctl; /* the recovered control record (NULL: refused) */ + unsigned booted_slot; /* the slot this kernel was started from */ + const uint8_t (*keys)[32]; /* trusted manifest signers */ + unsigned nkeys; + uint8_t *buf; /* image buffer, buf_cap bytes, 4096-aligned length */ + uint64_t buf_cap; + /* Optional progress hook: step name, units done, units total. A nonzero + * return stops the update there (the hosts's interruption point). */ + int (*progress)(void *ctx, const char *step, uint64_t done, uint64_t total); + void *ctx; +}; + +struct ck_slot_update_result { + unsigned target; /* the slot written (0 or 1) */ + uint64_t generation; /* of the staged image */ + uint64_t image_len; + uint64_t units_written; /* inside the target slot */ + uint64_t seq; /* control sequence after staging */ + int wrote; /* 1 once step 3 wrote anything */ +}; + +/* 0 = staged (res filled). -1 = refused with *why (res->wrote says whether + * the target slot was touched; the control record never was). */ +int ck_slot_update(const struct ck_slot_update_io *io, struct ck_slot_update_result *res, const char **why); + +#endif diff --git a/native/kernel/.crumb b/native/kernel/.crumb index e58e08fc..2d675871 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,14 +57,14 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "8f639eb89b1c234e186ed3ec0d6cf3bf22cc63af2264dd287aca9eb62c5c27ff", - "last_commit": "66bd299", + "digest": "5f5def88e2ef4bbd425e833cecb75d7cc805e7516a5128a88a7dea4e9b87cdd9", + "last_commit": "fe21660", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "04dc7faae4e24952ed8c1b46c83a346f0be5a84828ea9e53c76611eec228bf45", - "last_commit": "3647654", + "digest": "d7b81a8f888e7b4ca5337a5c9279671c33e1f4a70a7318ed20e1c5367c8a289b", + "last_commit": "fe21660", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, @@ -81,33 +81,33 @@ "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "1bac580f6739048716c499c005fba3b85be0092bbdef5ee37b3e32c72a878024", - "last_commit": "3647654", + "digest": "f2ef5ffe9eae8f9119dd302b55989b8b50f68def6ad03a2db227e28dbd270ddd", + "last_commit": "fe21660", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "9aa43b7cb6119b9cb8fe1fc72e35e2983048acb1c839695a12de88c230c5f580", - "last_commit": "3c07242", + "digest": "515f5d05394be70d66a845c3d5f2aecdd11e5402c189aefeb817577857ad0118", + "last_commit": "fe21660", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { - "digest": "55e46a8f9efc987be206a64cc6c1085ac3ff9092d2f3ce4e490ce404785b19a4", - "last_commit": "66bd299", + "digest": "e17d2e0716b7121a785c5db6d437ecce7061b40741c97b4859cdc3021878646e", + "last_commit": "fe21660", "name": "tools", "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:776233e69bb669107a9239b75a3130af7554dbcfe070cc499d5e0aebe4724791", - "compiled_at": "2026-10-12T20:24:30Z", + "children_root": "sha256:6e029b6ca56fd468865463b2676ec83380dc39ac2541d45078cfb2e1e35f0669", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:85bc49d4e8d7d16a24ba3b095076c1187da66f23de8416b68f7d42cd0776095a", + "digest": "sha256:9ac3378c865ea4bfbf2380a6e9cb5dea03264870381bf314e96ce62c99e20a38", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 317, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:8fc16b028f631ce40b2fd680b273c9005413f4490b28292f3b2fec518558fe45" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:b484dc98127db90feb47194f1193e42c42ee4030295558feab5d995f5fe3645f" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index d43e963c..b6bbbf33 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -889,6 +889,10 @@ Owner decision 2026-10-12 (Drake): slot loader approved, narrowly scoped to sign | 195 | P11 cut 2b, promote on health: SLOTLDR.EFI and the TEST-key service image on one NVMe disk (ck_gpt_image `aienos-slots`: Store partition + slots partition), B staged (gen 2, policy 2, tries 3). The loader commits the try (`control committed seq=2 tries=2`) and hands over its boot record (`slot_loader: boot record handed over type=0x80000a1e slot=B reason=candidate_try seq=2`); the kernel reads it (`slot: booted slot B reason=candidate_try generation=2 tries=2 control_seq=2`), reaches health (Store open, `continuity: PROVISIONED` before the promotion, `lifecycle: ready state=ready store=open`) and commits the promotion through the shared torn-slot routine (`slot: promoted slot B to active generation=2 floor=2 seq=3`); the host tool reads `active=1 candidate=none tries=0 flags=0x00 floor=2 last_good=2` from the disk. Next boot: `boot=B reason=active gen=2 tries=0 floor=2`, no loader write, `slot: nothing to write`, control unchanged | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 196 | P11 cut 2b, health fail, exhausted tries, abandon and rollback: B is the plain service image (no production keys in QEMU: the Store stays LOCKED, never healthy), one try left. Boot 1: the last try is committed (`seq=2 tries=0`), `lifecycle: ready state=locked`, `slot: candidate B waiting for health (tries left 0)`, at the stop order `slot: candidate B NOT promoted this boot`, no `slot: promoted`; the disk keeps `candidate=1 tries=0`. Boot 2: `boot=A reason=candidate_exhausted_active`, the kernel abandons B (`slot: candidate abandoned (marked failed) active=A stays seq=3`), disk `active=0 candidate=none tries=0 flags=0x01`, and the host tool's next decision is a steady boot of A (`boot_slot=A reason=active loader_write=0`) | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 197 | P11 cut 2b, negative control: the TEST-key service image started directly (no SLOTLDR.EFI) over a disk with B staged: `slot: no boot record (not started by SLOTLDR.EFI): slot service idle, nothing written`, healthy (`lifecycle: ready state=ready store=open`) yet no `slot: promoted`, control untouched (`seq=1 ... candidate=1 tries=3`). Both slots invalid stays row 192 (no image started, nothing written). Host side (`make slot-test`, in `make test`): boot record mutations each refused with its reason, kernel-action scenarios (promote, wait, abandon, refuse on no control, corrupt control, unreadable manifest, manifest digest or generation differing from the record, damaged record; none on steady boots and on the non-active slot) and the shared control-commit routine | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 211 | P11 cut 3, update and promote: disk layout `aienos-slots-inbox` (Store + slots + update inbox partition, type `58D9C90E-497A-4848-95DD-EB7E22B0CE96`), A active (gen 1), B empty, the inbox holds B's TEST-signed image (gen 2, policy 2; `ck_slot_tool inbox-image`). Operator `update` on the service running from A: `slot: update staged slot B as candidate generation=2`; a second `update`: `slot: update REFUSED a candidate is already staged`; control on disk `active=0 candidate=1 tries=3`; slot A's bytes unchanged (SHA-256 of its range); the host tool agrees the next decision is `candidate_try` of B. Next boot: `slot_loader: boot=B reason=candidate_try gen=2`, `slot: promoted slot B to active generation=2 floor=2`, control `active=1` | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 212 | P11 cut 3, interrupted update: `update halt-after=1` (TEST-only, TEST-key image only) parks the CPU after 64 image units and the gate kills QEMU (power cut model): `slot: TEST-ONLY update halted after 64 of`, never `staged`; control record untouched (`seq=1 active=0 candidate=none`), slot A's bytes unchanged. Next boot: `slot_loader: slot B refused`, `boot=A reason=active gen=1`, and a new `update` then stages B (`seq=2 candidate=1 tries=3`) | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 213 | P11 cut 3, negative controls: an inbox signed by RFC 8032 TEST 2 (wrong key) and one whose image has a flipped byte (wrong digest): each `slot: update REFUSED ...: nothing written`, never staged, control untouched, the whole slots partition byte-identical (SHA-256 before and after). `make slot-test` adds bad signature, truncated inbox, generation at the floor, booted slot not active, no trusted key, no control record, image larger than the slot, corrupt manifest, an injected I/O error at each write and a stop at each step | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 214 | P11 cut 3, authority: the plain service image (no production keys: Store LOCKED) refuses `update` (`lifecycle: REFUSED verb=update state=locked reason="locked: unlock before updating a slot"`), slots partition byte-identical; `make CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 CK_HARDWARE_STAGING=1 full` is refused (TEST-key updater never under hardware staging) | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | Run (2026-10-12, code commit bff20a1 on main ff5da2d, QEMU 8.2 aarch64 virt + AAVMF, TCG): `AIENOS_CK_SLOT_LOADER: PASS (8 rows, 11 boots, 76 checks; QEMU, not hardware)`, 126 s; each slot boot about 6 s, row 7 stopped at 60 s. SLOTLDR.EFI is 36864 bytes. The core `BOOTAA64.EFI` built from this tree is byte-identical to the one built from main ff5da2d (same `AIENOS_COMMIT`, sha256 643fdb5991cfe261). Also PASS in CI (aienos#326, ARM64 job of run 38274680333, `required gate SLOT_LOADER: PASS`). After the rebase onto main 22157e1 (aienos#324 and #325) neither the loader nor the gate script changed, so the gate was not rerun locally (the lock was held by another gate); CI reran it on the final tree. Hardware NOT_RUN. diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 3ee8b9c4..7f180e17 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -726,6 +726,17 @@ STAGE_SRCS += $(KDIR)/svc/continuity_codec.c $(KDIR)/svc/continuity_resolve.c \ # P11 cut 2b: kernel-side slot promotion / abandon (svc/slot_service.c) over the shared # slot decision rules and control-commit routine of SLOTLDR.EFI (native/boot). STAGE_SRCS += $(KDIR)/svc/slot_service.c $(BOOT)/slot_select.c $(BOOT)/slot_control.c +# P11 cut 3: the inactive-slot updater (svc/slot_service.c verb `update`, native/boot/slot_update.c) +# verifies the inbox image against the key table SLOTLDR.EFI is built with (ck_slot_tool keys-c): +# RFC 8032 TEST 1 in the TEST-keys image, the Owner Root (CK_OWNER_PUBKEYS) under CK_HARDWARE_STAGING. +# The plain service image gets no table and refuses every update. +STAGE_SRCS += $(BOOT)/slot_update.c +SVC_SLOT_KEY_SRC := $(if $(strip $(CK_SERVICE_TEST_KEYS)),--test-key,$(if $(strip $(CK_HARDWARE_STAGING)),--keys $(OWNER_PUBKEYS_FILE),)) +ifneq ($(SVC_SLOT_KEY_SRC),) +SVC_SLOT_KEYS_C := $(OUT)/gen-$(FULL_NAME)/svc_slot_keys.c +STAGE_SRCS += $(SVC_SLOT_KEYS_C) +STAGE_DMA_FLAGS += -DCK_SLOT_UPDATE_KEYS=1 +endif endif ifeq ($(strip $(CK_TEST_DISK_XLATE_BYPASS)),1) FULL_NAME := $(FULL_NAME)-test-xlate-bypass @@ -1614,12 +1625,18 @@ gsp-rpc-test: $(GSP_RPC_TOOL) # freestanding flags and needs only the image's own digest/signature primitives. # No image code changes in this cut; the loader does not read slots yet. SLOT_TOOL := $(OUT)/host/ck_slot_tool -SLOT_TOOL_SRCS := $(KDIR)/tools/ck_slot_tool.c $(BOOT)/slot_select.c $(BOOT)/slot_control.c $(NATIVE)/sig/sha512.c \ +SLOT_TOOL_SRCS := $(KDIR)/tools/ck_slot_tool.c $(BOOT)/slot_select.c $(BOOT)/slot_control.c $(BOOT)/slot_update.c $(NATIVE)/disk/disk.c $(NATIVE)/sig/sha512.c \ $(NATIVE)/sig/ed25519.c $(NATIVE)/sig/ct.c $(NATIVE)/argus/sha256.c $(NATIVE)/store/torn_slot.c -$(SLOT_TOOL): $(SLOT_TOOL_SRCS) $(BOOT)/slot_format.h $(BOOT)/slot_control.h +$(SLOT_TOOL): $(SLOT_TOOL_SRCS) $(BOOT)/slot_format.h $(BOOT)/slot_control.h $(BOOT)/slot_update.h @mkdir -p $(dir $@) $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -o $@ $(SLOT_TOOL_SRCS) slot-tool: $(SLOT_TOOL) +# P11 cut 3: the service image's slot key table (see SVC_SLOT_KEY_SRC above). +ifneq ($(SVC_SLOT_KEYS_C),) +$(SVC_SLOT_KEYS_C): $(SLOT_TOOL) FORCE + @mkdir -p $(dir $@) + @$(SLOT_TOOL) keys-c $(SVC_SLOT_KEY_SRC) --out $@ +endif slot-test: $(SLOT_TOOL) $(SLOT_TOOL) selftest | grep -E '^CK_SLOT_TOOL_SELFTEST: (PASS|FAIL)' | grep -q PASS || { $(SLOT_TOOL) selftest | grep -E 'FAIL|SELFTEST'; exit 1; } @$(SLOT_TOOL) selftest | tail -1 diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 12561dce..b6e9ce01 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T20:24:30Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:8f639eb89b1c234e186ed3ec0d6cf3bf22cc63af2264dd287aca9eb62c5c27ff", + "digest": "sha256:5f5def88e2ef4bbd425e833cecb75d7cc805e7516a5128a88a7dea4e9b87cdd9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 45, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:f5d9c30ab3c57068be624be5333ac50772f0093a0f9c9b598d5a80c197460520" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:c20518c21d9654bc4cf8e3743341819b51a61470925b09a70790849c2596e0af" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c index 17c904c8..a4b0832e 100644 --- a/native/kernel/core/lifecycle.c +++ b/native/kernel/core/lifecycle.c @@ -102,7 +102,7 @@ static uint32_t g_cx_refused; /* remember/facts refused by the Store or Corte static void help(void) { - ck_puts("lifecycle: verbs: status unlock [<64-hex recovery secret>] rotate task prompt approve deny cancel recall remember facts evidence help shutdown\n"); + ck_puts("lifecycle: verbs: status unlock [<64-hex recovery secret>] rotate task prompt approve deny cancel recall remember facts update evidence help shutdown\n"); } static void status(void) @@ -478,6 +478,7 @@ static void handle_line(const char *line, size_t len) case CK_LC_R_RECALL: recall(); return; case CK_LC_R_REMEMBER: remember(); return; case CK_LC_R_FACTS: facts(); return; + case CK_LC_R_UPDATE: ck_slot_service_update(arg, arg_len); return; /* P11 cut 3, svc/slot_service.c */ case CK_LC_R_OK: break; } switch (v) { diff --git a/native/kernel/core/lifecycle_fsm.c b/native/kernel/core/lifecycle_fsm.c index 18502bd4..a9efd883 100644 --- a/native/kernel/core/lifecycle_fsm.c +++ b/native/kernel/core/lifecycle_fsm.c @@ -52,6 +52,8 @@ enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, siz if (word_is(w, n, "unlock")) return CK_LC_V_UNLOCK; /* rotate carries " " (the binding checks it) */ if (word_is(w, n, "rotate")) return CK_LC_V_ROTATE; + /* update carries an optional argument the binding checks (slot_service.c) */ + if (word_is(w, n, "update")) return CK_LC_V_UPDATE; if (*arg_len != 0) return CK_LC_V_INVALID; if (word_is(w, n, "help")) return CK_LC_V_HELP; if (word_is(w, n, "status")) return CK_LC_V_STATUS; @@ -85,6 +87,7 @@ const char *ck_lc_verb_name(enum ck_lc_verb v) case CK_LC_V_REMEMBER: return "remember"; case CK_LC_V_FACTS: return "facts"; case CK_LC_V_ROTATE: return "rotate"; + case CK_LC_V_UPDATE: return "update"; } return "?"; } @@ -165,6 +168,11 @@ enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, siz if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); if (arg_len == 0) return refuse(lc, "rotate needs "); return CK_LC_R_ROTATE; + case CK_LC_V_UPDATE: + if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: unlock before updating a slot"); + if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); + lc->updates++; + return CK_LC_R_UPDATE; case CK_LC_V_TASK: if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); diff --git a/native/kernel/core/lifecycle_fsm.h b/native/kernel/core/lifecycle_fsm.h index 9179e73c..2d09eeab 100644 --- a/native/kernel/core/lifecycle_fsm.h +++ b/native/kernel/core/lifecycle_fsm.h @@ -41,6 +41,7 @@ enum ck_lc_verb { CK_LC_V_REMEMBER, /* P06 cut 4: record one owner fact in the Cortex journal (sealed Store) */ CK_LC_V_FACTS, /* list the remembered facts read back from the journal */ CK_LC_V_ROTATE, /* P04 cut 2b: rotate the recovery secret (current + new, 64 hex each) */ + CK_LC_V_UPDATE, /* P11 cut 3: write the inbox image into the inactive slot and stage it */ }; /* Bounds: one request line, and the task text that a line may carry. */ @@ -63,6 +64,7 @@ enum ck_lc_result { CK_LC_R_REMEMBER, /* remember accepted: the binding must append the fact (task holds the text) */ CK_LC_R_FACTS, /* facts accepted: the binding must list the journal */ CK_LC_R_ROTATE, /* rotate accepted: the binding must re-wrap K_vol under the new secret */ + CK_LC_R_UPDATE, /* update accepted: the binding must run the inactive-slot updater */ }; typedef struct { @@ -76,6 +78,7 @@ typedef struct { uint32_t requests, refused, invalid, tasks, approved, denied, cancelled; uint32_t prompts, replies, gen_cancelled, gen_failed, recalls; uint32_t remembers, facts; /* P06 cut 4: accepted remember / facts requests */ + uint32_t updates; /* P11 cut 3: accepted update requests */ char fact[CK_LC_TASK_MAX + 1]; /* text of the remember request being served, else "" */ size_t fact_len; const char *reason; /* last refusal reason, static string */ diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 988ee6dd..2b928683 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T19:04:55Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:04dc7faae4e24952ed8c1b46c83a346f0be5a84828ea9e53c76611eec228bf45", + "digest": "sha256:d7b81a8f888e7b4ca5337a5c9279671c33e1f4a70a7318ed20e1c5367c8a289b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 27, - "generated_at_commit": "44dc7ee624d1b72eb3930cd35f55629c2c12a008", - "source_tree": "sha256:01f9f3ea8287a9b73b338c07bd3517694a1d03fa55f43c8a5f9ee934d8972a7a" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:0805a014ffb340ed6b5f1cba3e6368c77f7d280bc1a962302156f95a6852f936" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c index 526f1c10..9a9fdf39 100644 --- a/native/kernel/dev/devices.c +++ b/native/kernel/dev/devices.c @@ -18,28 +18,41 @@ static ck_nvme g_nvme; * whole namespace: every access they make goes through ck_part_xlate. */ const disk_dev *ck_dev_boot_disk(void) { return g_nvme.bound && g_nvme.part.valid ? &g_nvme.part.dev : 0; } -/* The AIENOS image slots partition (native/boot/slot_format.h, P11 cut 2b) on - * the same bound NVMe disk: a second bounded partition view, found with the - * same strict GPT validation as the AIENOS partition; never the whole - * namespace. Looked up once, on first use. *rc_out: 0, a CK_GPT_* code, or - * CK_DEV_NO_BOOT_DISK when no disk is bound (or it was released). */ -static ck_part g_slots; -static int g_slots_rc = 1; /* 1: not looked up yet */ -const disk_dev *ck_dev_slots_disk(int *rc_out) +/* The AIENOS image slots partition (native/boot/slot_format.h, P11 cut 2b) and + * the update inbox partition (P11 cut 3) on the same bound NVMe disk: further + * bounded partition views, found with the same strict GPT validation as the + * AIENOS partition; never the whole namespace. Each is looked up once, on + * first use. *rc_out: 0, a CK_GPT_* code, or CK_DEV_NO_BOOT_DISK when no disk + * is bound (or it was released). */ +struct typed_part { + ck_part part; + int rc; /* 1: not looked up yet */ +}; +static struct typed_part g_slots = {.rc = 1}, g_inbox = {.rc = 1}; +static const disk_dev *typed_disk(struct typed_part *tp, const uint8_t type[16], int *rc_out) { - static const uint8_t type[16] = CK_SLOT_PART_TYPE_BYTES; int rc; if (!g_nvme.bound || !g_nvme.part.valid) { rc = CK_DEV_NO_BOOT_DISK; } else { - if (g_slots_rc == 1) { + if (tp->rc == 1) { ck_gpt_info gi; - g_slots_rc = ck_gpt_find_type(&g_nvme.disk, type, &g_slots, &gi); + tp->rc = ck_gpt_find_type(&g_nvme.disk, type, &tp->part, &gi); } - rc = g_slots_rc; + rc = tp->rc; } if (rc_out) *rc_out = rc; - return rc == 0 && g_slots.valid ? &g_slots.dev : 0; + return rc == 0 && tp->part.valid ? &tp->part.dev : 0; +} +const disk_dev *ck_dev_slots_disk(int *rc_out) +{ + static const uint8_t type[16] = CK_SLOT_PART_TYPE_BYTES; + return typed_disk(&g_slots, type, rc_out); +} +const disk_dev *ck_dev_inbox_disk(int *rc_out) +{ + static const uint8_t type[16] = CK_SLOT_INBOX_TYPE_BYTES; + return typed_disk(&g_inbox, type, rc_out); } void ck_dev_nvme_release(void) diff --git a/native/kernel/dev/devices.h b/native/kernel/dev/devices.h index b9b14a0b..a2ec7903 100644 --- a/native/kernel/dev/devices.h +++ b/native/kernel/dev/devices.h @@ -12,6 +12,8 @@ const disk_dev *ck_dev_boot_disk(void); * 0, the CK_GPT_* refusal, or CK_DEV_NO_BOOT_DISK. */ #define CK_DEV_NO_BOOT_DISK (-1) const disk_dev *ck_dev_slots_disk(int *rc_out); +/* The update inbox partition (CK_SLOT_INBOX_TYPE, P11 cut 3), same rules. */ +const disk_dev *ck_dev_inbox_disk(int *rc_out); /* Revoke the NVMe function's bus mastering once the last disk user (the * Store stage) is done, or after a failed bind; prints the dma_gate line. * The boot disk is unavailable afterwards. */ diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index f938a303..cab092a8 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:d835471b1405ca482a41beb34ed339d21e11064a59138599ee5754d198926878", - "compiled_at": "2026-10-12T19:04:55Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:1bac580f6739048716c499c005fba3b85be0092bbdef5ee37b3e32c72a878024", + "digest": "sha256:f2ef5ffe9eae8f9119dd302b55989b8b50f68def6ad03a2db227e28dbd270ddd", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 43, - "generated_at_commit": "44dc7ee624d1b72eb3930cd35f55629c2c12a008", - "source_tree": "sha256:0abe2554113bf606fd07efe3f958e50c3d741a51f1bababe5bca79f9b8d22ab9" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:8d9bbcbf03ff3204145e5708e278082fbaea58c8b02b12f2c4fbd1adee241633" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/slot_service.c b/native/kernel/svc/slot_service.c index 4de9f00b..cd6916c3 100644 --- a/native/kernel/svc/slot_service.c +++ b/native/kernel/svc/slot_service.c @@ -8,6 +8,8 @@ #include "store_sealed.h" #include "slot_service.h" #include "../../boot/slot_control.h" +#include "../../boot/slot_update.h" +#include "ck_internal.h" #ifndef CK_SERVICE #error "slot_service.c is only part of a CK_SERVICE=1 image" @@ -175,3 +177,142 @@ void ck_slot_service_stop(void) "its tries down\n", 'A' + g_rec.boot_slot); } + +/* ---- P11 cut 3: the inactive-slot updater (operator verb `update`) ------ */ +/* Trusted manifest signers: the same key table SLOTLDR.EFI is built with, + * generated by ck_slot_tool keys-c (native/kernel/Makefile): RFC 8032 TEST 1 + * in the TEST-keys service image, the Owner Root (CK_OWNER_PUBKEYS) under + * CK_HARDWARE_STAGING. The plain service image trusts no key and refuses. */ +#ifdef CK_SLOT_UPDATE_KEYS +extern const uint8_t ck_slot_loader_keys[][32]; +extern const unsigned ck_slot_loader_nkeys; +#define UPD_KEYS ck_slot_loader_keys +#define UPD_NKEYS ck_slot_loader_nkeys +#else +static const uint8_t g_no_keys[1][32]; +#define UPD_KEYS g_no_keys +#define UPD_NKEYS 0u +#endif +#define UPDATE_MAX_UNITS ((64u << 20) / CK_SLOT_UNIT) /* image buffer bound: 64 MiB */ + +#ifdef CK_SERVICE_TEST_KEYS +/* TEST-ONLY interruption point (scripts/qemu_ck_slot_loader_test.sh): with + * "update halt-after=K" the updater parks the CPU once K or more image units + * are written, so the gate can kill QEMU mid-write. Never in another image. */ +static uint64_t g_halt_after; +static int update_progress(void *ctx, const char *step, uint64_t done, uint64_t total) +{ + (void)ctx; + if (g_halt_after && step[0] == 'i' && done >= g_halt_after) { + ck_printf("slot: TEST-ONLY update halted after %llu of %llu image units (interrupted on purpose)\n", + (unsigned long long)done, (unsigned long long)total); + for (;;) __asm__ volatile("wfi"); + } + return 0; +} +#endif + +static void update_refuse(const char *why, int wrote, unsigned target) +{ + if (wrote) + ck_printf("slot: update REFUSED %s: slot %c left invalid, active slot and control record untouched\n", why, + 'A' + target); + else + ck_printf("slot: update REFUSED %s: nothing written\n", why); +} + +void ck_slot_service_update(const char *arg, size_t len) +{ +#ifdef CK_SERVICE_TEST_KEYS + g_halt_after = 0; + if (len) { + static const char k[] = "halt-after="; + size_t i = 0; + uint64_t v = 0; + for (; i < sizeof k - 1; i++) + if (i >= len || arg[i] != k[i]) break; + int ok = i == sizeof k - 1 && len > i && len - i <= 6; + for (; ok && i < len; i++) { + if (arg[i] < '0' || arg[i] > '9') ok = 0; + else v = v * 10 + (uint64_t)(arg[i] - '0'); + } + if (!ok || v == 0) { + update_refuse("argument (TEST image: none, or halt-after=)", 0, 0); + return; + } + g_halt_after = v; + } +#else + (void)arg; + if (len) { + update_refuse("update takes no argument", 0, 0); + return; + } +#endif + if (g_rec_state != 1) { + update_refuse(g_rec_state == 0 ? "not started by SLOTLDR.EFI: the booted slot is unknown" + : "the boot record was refused", + 0, 0); + return; + } + int prc = 0, irc = 0; + const disk_dev *d = ck_dev_slots_disk(&prc); + const disk_dev *in = ck_dev_inbox_disk(&irc); + if (!d || !in) { + int rc = d ? irc : prc; + update_refuse(rc == CK_DEV_NO_BOOT_DISK ? "no bound boot disk" + : !d ? "no slots partition" + : "no update inbox partition", + 0, 0); + return; + } + if (d->block_size != 512 && d->block_size != 4096) { + update_refuse("slots partition block size", 0, 0); + return; + } + ts_device tdev; + ts_region r; + ts_slot_state tss[2]; + uint64_t seq = 0; + ss_ts_device(d, &tdev); + int trc = ts_open(&r, &tdev, 0); + if (trc == TS_OK) trc = ts_recover(&r, (uint8_t *)&ctl, &seq, tss); + uint64_t cap = ck_slot_cap_units(d->block_count / (CK_SLOT_UNIT / d->block_size)); + uint64_t inu = in->block_size ? in->block_count / (CK_SLOT_UNIT / in->block_size) : 0; + uint64_t pages = cap > 1 ? cap - 1 : 1; + if (inu > 1 && inu - 1 < pages) pages = inu - 1; + if (pages > UPDATE_MAX_UNITS) pages = UPDATE_MAX_UNITS; + uint64_t pa = 0; + if (ck_mm_frames_alloc(pages, &pa) != 0 || !ck_mm_mapped(pa, pages * CK_SLOT_UNIT)) { + update_refuse("no memory for the image buffer", 0, 0); + return; + } + struct ck_slot_update_io io = { + .slots = d, + .inbox = in, + .control = &r, + .ctl = trc == TS_OK ? &ctl : 0, + .booted_slot = g_rec.boot_slot, + .keys = UPD_KEYS, + .nkeys = UPD_NKEYS, + .buf = (uint8_t *)(uintptr_t)pa, + .buf_cap = pages * CK_SLOT_UNIT, +#ifdef CK_SERVICE_TEST_KEYS + .progress = update_progress, +#endif + }; + struct ck_slot_update_result res; + const char *why = 0; + ck_printf("slot: update from the inbox into the inactive slot (booted %c, keys=%u)\n", 'A' + g_rec.boot_slot, + UPD_NKEYS); + int rc = ck_slot_update(&io, &res, &why); + (void)ck_mm_frames_free(pa, pages); + if (rc != 0) { + update_refuse(why ? why : "refused", res.wrote, res.target); + return; + } + ck_printf("slot: update staged slot %c as candidate generation=%llu bytes=%llu units=%llu seq=%llu tries=%u: " + "reboot to try it\n", + 'A' + res.target, (unsigned long long)res.generation, (unsigned long long)res.image_len, + (unsigned long long)res.units_written, (unsigned long long)res.seq, CK_SLOT_MAX_TRIES); +} diff --git a/native/kernel/svc/slot_service.h b/native/kernel/svc/slot_service.h index 1cf80367..d93dd1e6 100644 --- a/native/kernel/svc/slot_service.h +++ b/native/kernel/svc/slot_service.h @@ -29,4 +29,10 @@ void ck_slot_service_start(int healthy); void ck_slot_service_health(int healthy); void ck_slot_service_stop(void); +/* P11 cut 3: operator verb `update` (lifecycle READY only): write the signed + * image in the update inbox partition into the inactive slot and stage it as + * the candidate (native/boot/slot_update.h). Prints one "slot: update ..." + * outcome line. */ +#include +void ck_slot_service_update(const char *arg, size_t len); #endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index b93b2bb3..1e601908 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T16:18:21Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:9aa43b7cb6119b9cb8fe1fc72e35e2983048acb1c839695a12de88c230c5f580", + "digest": "sha256:515f5d05394be70d66a845c3d5f2aecdd11e5402c189aefeb817577857ad0118", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 151, - "generated_at_commit": "d60d904d6960d80246ce628332c1ed63971cb96f", - "source_tree": "sha256:c9b5a15196836b17ef25f4416dad20b6d783448f7f9008ee43a7044dd25705de" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:b4a14f3b8d7e17e89a2842c17a0dd6b5ae21123cd00c3c0032729582ac9743a5" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_lifecycle.c b/native/kernel/tests/test_lifecycle.c index 4959867e..92e0fa9b 100644 --- a/native/kernel/tests/test_lifecycle.c +++ b/native/kernel/tests/test_lifecycle.c @@ -30,6 +30,9 @@ int main(void) CHECK(CK_LC_LINE_MAX >= 7 + 64 + 1 + 64); /* "rotate " + current + " " + new */ CHECK(dec("rotate aa bb", &a, &al) == CK_LC_V_ROTATE && al == 5 && memcmp(a, "aa bb", 5) == 0); CHECK(dec("rotate", &a, &al) == CK_LC_V_ROTATE && al == 0); + CHECK(dec("update", &a, &al) == CK_LC_V_UPDATE && al == 0); /* P11 cut 3 */ + CHECK(dec("update halt-after=2", &a, &al) == CK_LC_V_UPDATE && al == 12 && memcmp(a, "halt-after=2", 12) == 0); + CHECK(strcmp(ck_lc_verb_name(CK_LC_V_UPDATE), "update") == 0); CHECK(dec("frobnicate", &a, &al) == CK_LC_V_UNKNOWN); CHECK(dec("prompt hi", &a, &al) == CK_LC_V_PROMPT && al == 2 && memcmp(a, "hi", 2) == 0); CHECK(dec("prompt", &a, &al) == CK_LC_V_INVALID); /* prompt needs text */ @@ -60,6 +63,7 @@ int main(void) CHECK(req(&lc, "status") == CK_LC_R_OK && req(&lc, "help") == CK_LC_R_OK && req(&lc, "evidence") == CK_LC_R_OK); CHECK(req(&lc, "task hello") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.tasks == 0); CHECK(req(&lc, "rotate aa bb") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED); /* unlock first */ + CHECK(req(&lc, "update") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.updates == 0 && strstr(lc.reason, "unlock before updating")); CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "deny") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); ck_lc_unlock_done(&lc, 0); @@ -75,6 +79,7 @@ int main(void) CHECK(req(&lc, "rotate aa bb") == CK_LC_R_ROTATE); ck_lc_rotate_done(&lc, 1); CHECK(lc.state == CK_LC_READY); + CHECK(req(&lc, "update") == CK_LC_R_UPDATE && lc.state == CK_LC_READY && lc.updates == 1); CHECK(req(&lc, "shutdown") == CK_LC_R_STOP && lc.state == CK_LC_QUIESCE); CHECK(req(&lc, "status") == CK_LC_R_REFUSED && strcmp(lc.reason, "stopping") == 0); ck_lc_stopped(&lc); @@ -86,6 +91,7 @@ int main(void) CHECK(lc.state == CK_LC_READY); CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); CHECK(req(&lc, "task draft the plan") == CK_LC_R_OK && lc.state == CK_LC_SERVE && lc.tasks == 1); + CHECK(req(&lc, "update") == CK_LC_R_REFUSED && lc.state == CK_LC_SERVE && strstr(lc.reason, "pending")); /* P11 cut 3 */ CHECK(strcmp(lc.task, "draft the plan") == 0 && lc.task_len == 14); CHECK(req(&lc, "task another") == CK_LC_R_REFUSED && strcmp(lc.task, "draft the plan") == 0); /* one at a time */ CHECK(req(&lc, "approve") == CK_LC_R_COMMIT && lc.state == CK_LC_SERVE); @@ -102,7 +108,7 @@ int main(void) CHECK(lc.state == CK_LC_QUIESCE && lc.task_len == 0 && lc.approved == 1); ck_lc_commit_done(&lc, 0); /* ignored outside SERVE */ CHECK(lc.approved == 1); - CHECK(lc.requests == 14); + CHECK(lc.requests == 15); /* 14 + the P11 cut 3 update refused in SERVE */ /* P06: prompt and recall. Locked: both refused, nothing counted. */ ck_lc_init(&lc); diff --git a/native/kernel/tools/.crumb b/native/kernel/tools/.crumb index 080f2056..37817ce5 100644 --- a/native/kernel/tools/.crumb +++ b/native/kernel/tools/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T20:24:30Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:55e46a8f9efc987be206a64cc6c1085ac3ff9092d2f3ce4e490ce404785b19a4", + "digest": "sha256:e17d2e0716b7121a785c5db6d437ecce7061b40741c97b4859cdc3021878646e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 18, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:caeecf0f379baac93db02234e7f87577b3cd5ca7a4132fef9747e1ea90604f1f" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:23b1f030b6124408a90f748cff85075c2fe69c26d808601869b3f83f6d74847d" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tools/ck_gpt_image.c b/native/kernel/tools/ck_gpt_image.c index 4c0d3de5..795b654d 100644 --- a/native/kernel/tools/ck_gpt_image.c +++ b/native/kernel/tools/ck_gpt_image.c @@ -23,6 +23,8 @@ * esp-model-aienos ESP_MIB MODEL_FILE STORE_MIB esp-model plus an * all-zero AIENOS partition of STORE_MIB at the end: * one disk with ESP, model and Store (service gate) + * aienos-slots-inbox AIENOS [1,41) + slots [41,M-17) + update inbox + * [M-17,M-1) (MIB >= 64; P11 cut 3); prints all three ranges * CORRUPTION: bad-primary-crc bad-backup-crc bad-primary-entries * bad-backup-entries no-pmbr * Prints one line: "gpt_image bs=B blocks=N aienos_first_lba=F @@ -155,6 +157,21 @@ static int cmd_create(int argc, char **argv) (unsigned long long)sp[1].first, (unsigned long long)sp[1].last); return 0; } + if (!strcmp(lay, "aienos-slots-inbox")) { + /* P11 cut 3: the same disk plus the update inbox partition (slot_update.h). */ + static const uint8_t slots_type[16] = CK_SLOT_PART_TYPE_BYTES, inbox_type[16] = CK_SLOT_INBOX_TYPE_BYTES; + gw_part sp[3] = {{ck_aienos_part_type, 1 * per, 41 * per - 1u, "AIENOS", 0}, + {slots_type, 41 * per, (mib - 17u) * per - 1u, "AIENOS-SLOTS", 0}, + {inbox_type, (mib - 17u) * per, (mib - 1u) * per - 1u, "AIENOS-INBOX", 0}}; + if (argc != 6 || mib < 64) return usage(); + if (gw_write(argv[2], bs, blocks, sp, 3, flags)) return 1; + printf("gpt_image bs=%u blocks=%llu aienos_first_lba=%llu aienos_last_lba=%llu slots_first_lba=%llu " + "slots_last_lba=%llu inbox_first_lba=%llu inbox_last_lba=%llu\n", + bs, (unsigned long long)blocks, (unsigned long long)sp[0].first, (unsigned long long)sp[0].last, + (unsigned long long)sp[1].first, (unsigned long long)sp[1].last, (unsigned long long)sp[2].first, + (unsigned long long)sp[2].last); + return 0; + } for (int i = 6; i < argc; i++) { if (!strcmp(argv[i], "bad-primary-crc")) flags |= GW_BAD_PRIMARY_CRC; else if (!strcmp(argv[i], "bad-backup-crc")) flags |= GW_BAD_BACKUP_CRC; diff --git a/native/kernel/tools/ck_slot_tool.c b/native/kernel/tools/ck_slot_tool.c index 3f2575a0..1d821f96 100644 --- a/native/kernel/tools/ck_slot_tool.c +++ b/native/kernel/tools/ck_slot_tool.c @@ -33,6 +33,8 @@ * ck_slot_tool slots-image --disk IMG --bs 512|4096 --first LBA --last LBA * [--a M,IMG] [--b M,IMG] [--control C] write a slots partition (control * region, manifests, images) into a disk image where SLOTLDR.EFI reads it + * ck_slot_tool inbox-image --disk IMG --bs 512|4096 --first LBA --last LBA --m M,IMG + * write a signed candidate into the update inbox partition (P11 cut 3) * Every refusal prints "refused: " and exits 1. */ #include #include @@ -45,6 +47,7 @@ #include "../../argus/sha256.h" #include "../../store/torn_slot.h" #include "../../boot/slot_control.h" +#include "../../boot/slot_update.h" /* RFC 8032 section 7.1 TEST 1: seed and public key (the key ck_owner_gen refuses). */ static const uint8_t TEST1_SEED[32] = { @@ -454,6 +457,303 @@ static void expect_refused(const struct ck_slot_manifest *m, const uint8_t *img, } } +/* ---- P11 cut 3: the inactive-slot updater (native/boot/slot_update.c) ------- */ +/* In-memory disk (512-byte blocks) with a write counter and an injected I/O + * error at one backend write index. */ +struct memdisk { + uint8_t *bytes; + uint64_t len; + long fail_at; /* backend write index that fails, -1 = none */ + long writes; +}; +static int md_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf) +{ + struct memdisk *d = ctx; + if ((lba + count) * 512 > d->len) + return DISK_ERANGE; + memcpy(buf, d->bytes + lba * 512, (size_t)count * 512); + return DISK_OK; +} +static int md_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf) +{ + struct memdisk *d = ctx; + if (d->fail_at >= 0 && d->writes++ == d->fail_at) + return DISK_EIO; + if (d->fail_at < 0) + d->writes++; + if ((lba + count) * 512 > d->len) + return DISK_ERANGE; + memcpy(d->bytes + lba * 512, buf, (size_t)count * 512); + return DISK_OK; +} +static int md_flush(void *ctx) +{ + (void)ctx; + return DISK_OK; +} +static void md_bind(disk_dev *dd, ts_device *td, struct memdisk *m) +{ + dd->ctx = m; + dd->block_size = 512; + dd->block_count = m->len / 512; + dd->read = md_read; + dd->write = md_write; + dd->flush = md_flush; + dd->max_blocks_per_io = 64; /* 32 KiB: disk_write splits the updater's chunks */ + if (td) { + td->ctx = m; + td->block_size = 512; + td->block_count = m->len / 512; + td->read = md_read; + td->write = md_write; + td->flush = md_flush; + } +} + +#define UPD_SLOT_UNITS (4u + 2u * 80u) /* cap 80 units per slot */ +#define UPD_IMG_BYTES 300000u /* 74 units: two updater chunks */ +static uint8_t upd_slots[UPD_SLOT_UNITS * CK_SLOT_UNIT], upd_ref[sizeof upd_slots]; +static uint8_t upd_inbox[(1u + 90u) * CK_SLOT_UNIT]; +static uint8_t upd_buf[80u * CK_SLOT_UNIT]; +static uint8_t upd_img[UPD_IMG_BYTES]; + +struct upd_rig { + struct memdisk ms, mi; + disk_dev slots, inbox; + ts_device tdev; + ts_region r; + struct ck_slot_control ctl; + struct ck_slot_update_io io; +}; + +/* Fresh media: slot A holds a valid generation-1 image and is active; slot B + * holds old bytes; the inbox holds manifest m and image img (inbox_units units). */ +static void upd_media(const struct ck_slot_manifest *m, const uint8_t *img, uint64_t len, uint64_t inbox_units) +{ + static struct ck_slot_manifest a; + struct memdisk md = {upd_slots, sizeof upd_slots, -1, 0}; + disk_dev dd; + ts_device td; + ts_region r; + struct ck_slot_control c; + uint64_t seq; + uint64_t cap = ck_slot_cap_units(UPD_SLOT_UNITS); + memset(upd_slots, 0xEE, sizeof upd_slots); /* old bytes everywhere */ + memset(upd_slots, 0, CK_SLOT_CONTROL_UNITS * CK_SLOT_UNIT); + fill_manifest(&a, upd_img, 5000, 1, 1, 1, "0123456789abcdef0123456789abcdef01234567", "slot A", 1700000000); + sign_manifest(&a, TEST1_SEED); + memcpy(upd_slots + ck_slot_manifest_unit(0, cap) * CK_SLOT_UNIT, &a, sizeof a); + memcpy(upd_slots + (ck_slot_manifest_unit(0, cap) + 1) * CK_SLOT_UNIT, upd_img, 5000); + md_bind(&dd, &td, &md); + ck_slot_control_init(&c, 0); + c.generation_floor = 1; + c.last_good_generation = 1; + if (ts_open(&r, &td, 0) != TS_OK || ts_recover(&r, (uint8_t *)&a, &seq, 0) != TS_EMPTY || + ck_slot_control_commit(&r, &c, &seq) != TS_OK) + check(0, "update: rig control commit"); + memcpy(upd_ref, upd_slots, sizeof upd_slots); + memset(upd_inbox, 0, sizeof upd_inbox); + memcpy(upd_inbox, m, sizeof *m); + memcpy(upd_inbox + CK_SLOT_UNIT, img, (size_t)len); + (void)inbox_units; +} + +static int upd_stop_at(void *ctx, const char *step, uint64_t done, uint64_t total) +{ + (void)done; + (void)total; + return ctx && strcmp(step, (const char *)ctx) == 0; +} + +static int upd_run_hook(struct upd_rig *g, uint64_t inbox_units, long fail_at, unsigned booted, const uint8_t (*keys)[32], + unsigned nkeys, const char *stop, struct ck_slot_update_result *res, const char **why) +{ + uint64_t seq; + memset(g, 0, sizeof *g); + g->ms = (struct memdisk){upd_slots, sizeof upd_slots, -1, 0}; + g->mi = (struct memdisk){upd_inbox, inbox_units * CK_SLOT_UNIT, -1, 0}; + md_bind(&g->slots, &g->tdev, &g->ms); + md_bind(&g->inbox, 0, &g->mi); + int have = ts_open(&g->r, &g->tdev, 0) == TS_OK && ts_recover(&g->r, (uint8_t *)&g->ctl, &seq, 0) == TS_OK; + g->ms.fail_at = fail_at; + g->ms.writes = 0; + g->io = (struct ck_slot_update_io){&g->slots, &g->inbox, &g->r, have ? &g->ctl : 0, booted, keys, nkeys, + upd_buf, sizeof upd_buf, upd_stop_at, (void *)stop}; + return ck_slot_update(&g->io, res, why); +} +static int upd_run(struct upd_rig *g, uint64_t inbox_units, long fail_at, unsigned booted, const uint8_t (*keys)[32], + unsigned nkeys, struct ck_slot_update_result *res, const char **why) +{ + return upd_run_hook(g, inbox_units, fail_at, booted, keys, nkeys, 0, res, why); +} + +/* The control record now on the slots media (NULL when none). */ +static const struct ck_slot_control *upd_control(void) +{ + static struct ck_slot_control c; + struct memdisk md = {upd_slots, sizeof upd_slots, -1, 0}; + disk_dev dd; + ts_device td; + ts_region r; + uint64_t seq; + md_bind(&dd, &td, &md); + return ts_open(&r, &td, 0) == TS_OK && ts_recover(&r, (uint8_t *)&c, &seq, 0) == TS_OK ? &c : 0; +} + +static int upd_same(uint64_t from_unit, uint64_t units) +{ + return memcmp(upd_slots + from_unit * CK_SLOT_UNIT, upd_ref + from_unit * CK_SLOT_UNIT, units * CK_SLOT_UNIT) == 0; +} + +/* Slot s evaluated from the media, as the loader would. */ +static int upd_slot_valid(unsigned s, uint64_t *gen) +{ + uint64_t cap = ck_slot_cap_units(UPD_SLOT_UNITS), mu = ck_slot_manifest_unit(s, cap); + struct ck_slot_eval ev; + int ok = ck_slot_verify((const struct ck_slot_manifest *)(upd_slots + mu * CK_SLOT_UNIT), + upd_slots + (mu + 1) * CK_SLOT_UNIT, (cap - 1) * CK_SLOT_UNIT, &TEST1_PK, 1, &ev); + if (gen) + *gen = ev.generation; + return ok; +} + +static void selftest_update(void) +{ + static struct upd_rig g; + static struct ck_slot_manifest m, bad; + struct ck_slot_update_result res; + const char *why; + const uint64_t cap = ck_slot_cap_units(UPD_SLOT_UNITS), au = ck_slot_manifest_unit(0, cap), + bu = ck_slot_manifest_unit(1, cap), inu = 1 + (UPD_IMG_BYTES + CK_SLOT_UNIT - 1) / CK_SLOT_UNIT; + const struct ck_slot_control *c; + uint64_t gen = 0; + uint32_t x = 0xC0FFEEu; + for (size_t i = 0; i < sizeof upd_img; i++) { + x = x * 1664525u + 1013904223u; + upd_img[i] = (uint8_t)(x >> 24); + } + fill_manifest(&m, upd_img, UPD_IMG_BYTES, 2, 2, 1, "89abcdef0123456789abcdef0123456789abcdef", "slot B update", + 1700000100); + sign_manifest(&m, TEST1_SEED); + + /* Positive: B written, verified, staged; A and the control region otherwise untouched. */ + upd_media(&m, upd_img, UPD_IMG_BYTES, inu); + int rc = upd_run(&g, inu, -1, 0, &TEST1_PK, 1, &res, &why); + long total = g.ms.writes; + c = upd_control(); + check(rc == 0 && res.target == 1 && res.generation == 2 && res.image_len == UPD_IMG_BYTES && + res.units_written == inu, + "update: inbox image written into inactive slot B and staged"); + check(c && c->active_slot == 0 && c->candidate_slot == 1 && c->tries_remaining == CK_SLOT_MAX_TRIES && + c->generation_floor == 1, + "update: control record names B candidate, A stays active, floor unchanged"); + check(upd_slot_valid(1, &gen) && gen == 2 && upd_slot_valid(0, 0) && upd_same(au, cap), + "update: slot B verifies from the disk bytes; slot A bytes unchanged"); + { + struct ck_slot_eval ev[2]; + struct ck_slot_decision d; + upd_slot_valid(0, 0); + ev[0] = (struct ck_slot_eval){1, 1, 1, 0}; + ev[1] = (struct ck_slot_eval){1, 2, 2, 0}; + ck_slot_select(c, ev, &d); + check(d.boot_slot == 1 && d.reason == CK_SLOT_R_CANDIDATE_TRY && d.loader_write, + "update: the loader's next decision is a candidate try of B"); + struct ck_slot_control ex = *c; + ex.tries_remaining = 0; + ck_slot_select(&ex, ev, &d); + check(d.boot_slot == 0 && d.reason == CK_SLOT_R_CANDIDATE_EXHAUSTED, + "update: exhausted tries -> the loader boots A (rollback to the previous active)"); + ev[1] = (struct ck_slot_eval){0, 0, 0, "x"}; + ck_slot_select(c, ev, &d); + check(d.boot_slot == 0 && d.reason == CK_SLOT_R_CANDIDATE_INVALID, + "update: a failed (invalid) candidate -> the loader boots A"); + } + check(upd_run(&g, inu, -1, 0, &TEST1_PK, 1, &res, &why) != 0 && !res.wrote && strstr(why, "already staged"), + "update: refused while a candidate is staged, nothing written"); + + /* An I/O error at every backend write: control record untouched (or the + * staging commit failed), slot A untouched, B invalid unless only the commit failed. */ + { + int all = 1; + long n = 0; + for (long k = 0; k < total; k++, n++) { + upd_media(&m, upd_img, UPD_IMG_BYTES, inu); + rc = upd_run(&g, inu, k, 0, &TEST1_PK, 1, &res, &why); + c = upd_control(); + int ctl_same = c && c->candidate_slot == CK_SLOT_NONE && c->active_slot == 0; + int b_ok = !upd_slot_valid(1, 0) || strstr(why, "staging commit") != 0; + if (rc == 0 || !ctl_same || !upd_same(au, cap) || !b_ok || !res.wrote) + all = 0; + } + char what[160]; + snprintf(what, sizeof what, + "update: I/O error at each of %ld writes -> refused, A + control untouched, B invalid or unstaged", n); + check(all && total > 4, what); + } + /* Interrupted at each step (power cut model: the hook stops the run there). */ + { + static const char *const steps[] = {"verified", "invalidated", "image", "manifest"}; + for (unsigned s = 0; s < 4; s++) { + upd_media(&m, upd_img, UPD_IMG_BYTES, inu); + rc = upd_run_hook(&g, inu, -1, 0, &TEST1_PK, 1, steps[s], &res, &why); + c = upd_control(); + char what[128]; + snprintf(what, sizeof what, "update: stopped at \"%s\" -> refused, A + control untouched, B %s", steps[s], + s == 0 ? "untouched" : s == 3 ? "valid but not staged" : "invalid"); + check(rc != 0 && c && c->candidate_slot == CK_SLOT_NONE && upd_same(au, cap) && + (s == 0 ? upd_same(bu, cap) : s == 3 ? upd_slot_valid(1, 0) : !upd_slot_valid(1, 0)), + what); + } + } + + /* Negative controls: refused before any write, the media byte-identical. */ + { + struct { + const char *what; + int kind; + } neg[] = { + {"update: bad signature refused, nothing written", 1}, + {"update: wrong key (TEST 2 signer) refused, nothing written", 2}, + {"update: wrong digest (image byte flipped) refused, nothing written", 3}, + {"update: truncated inbox refused, nothing written", 4}, + {"update: generation at the floor refused (no rollback), nothing written", 5}, + {"update: booted slot != active slot refused, nothing written", 6}, + {"update: image with no trusted key refused, nothing written", 7}, + {"update: no control record refused, nothing written", 8}, + {"update: image larger than the slot refused, nothing written", 9}, + {"update: corrupt inbox manifest refused, nothing written", 10}, + }; + for (unsigned i = 0; i < sizeof neg / sizeof neg[0]; i++) { + bad = m; + uint64_t in_units = inu; + unsigned booted = 0, nk = 1; + switch (neg[i].kind) { + case 1: bad.signature[7] ^= 1; break; + case 2: sign_manifest(&bad, TEST2_SEED); break; + case 4: in_units = inu - 3; break; + case 5: + fill_manifest(&bad, upd_img, UPD_IMG_BYTES, 1, 1, 1, "89abcdef0123456789abcdef0123456789abcdef", + "slot B old", 1700000100); + sign_manifest(&bad, TEST1_SEED); + break; + case 6: booted = 1; break; + case 7: nk = 0; break; + case 9: bad.image_len = (cap + 2) * CK_SLOT_UNIT; break; + case 10: bad.magic[0] ^= 1; break; + default: break; + } + upd_media(&bad, upd_img, UPD_IMG_BYTES, in_units); + if (neg[i].kind == 3) + upd_inbox[CK_SLOT_UNIT + 4242] ^= 0x40; + if (neg[i].kind == 8) + memset(upd_slots, 0, CK_SLOT_CONTROL_UNITS * CK_SLOT_UNIT), memcpy(upd_ref, upd_slots, sizeof upd_slots); + rc = upd_run(&g, in_units, -1, booted, &TEST1_PK, nk, &res, &why); + check(rc != 0 && !res.wrote && g.ms.writes == 0 && memcmp(upd_slots, upd_ref, sizeof upd_slots) == 0, + neg[i].what); + } + } +} + static int selftest(void) { uint8_t keys[MAX_KEYS][32]; @@ -851,6 +1151,8 @@ static int selftest(void) "keys-c: eight keys into 512 bytes refused, no write past the buffer"); } + selftest_update(); + printf("CK_SLOT_TOOL_SELFTEST: %s (%u checks, %u failures)\n", g_fails ? "FAIL" : "PASS", g_checks, g_fails); return g_fails ? 1 : 0; } @@ -1417,6 +1719,42 @@ static int cmd_slots_image(int argc, char **argv) return 0; } +/* inbox-image: write a signed candidate (manifest + image) into the update + * inbox partition of a disk image (CK_SLOT_INBOX_TYPE, one slot's shape: + * manifest at unit 0, image from unit 1), where the kernel verb `update` + * reads it (native/boot/slot_update.h). The rest of the partition is zeroed. + * inbox-image --disk IMG --bs 512|4096 --first LBA --last LBA --m M,IMG */ +static int cmd_inbox_image(int argc, char **argv) +{ + const char *disk = opt(argc, argv, "--disk"), *spec = opt(argc, argv, "--m"); + uint64_t bsz, first, last; + static uint8_t zero[CK_SLOT_UNIT]; + if (!disk || !spec || parse_u64(opt(argc, argv, "--bs"), &bsz) != 0 || (bsz != 512 && bsz != 4096) || + parse_u64(opt(argc, argv, "--first"), &first) != 0 || parse_u64(opt(argc, argv, "--last"), &last) != 0 || + last < first) + return refuse("inbox-image --disk IMG --bs 512|4096 --first LBA --last LBA --m M,IMG"); + uint64_t units = (last - first + 1) * bsz / CK_SLOT_UNIT; + if (units < 2) + return refuse("inbox partition too small"); + FILE *f = fopen(disk, "r+b"); + if (!f) + return refuse("cannot open the disk image"); + for (uint64_t u = 0; u < units; u++) + if (fseeko(f, (off_t)(first * bsz + u * CK_SLOT_UNIT), SEEK_SET) != 0 || fwrite(zero, 1, sizeof zero, f) != sizeof zero) { + fclose(f); + return refuse("inbox clear failed"); + } + if (put_slot(f, spec, first * bsz, (units - 1) * CK_SLOT_UNIT, "inbox") != 0) { + fclose(f); + return 1; + } + if (fclose(f) != 0) + return refuse("disk image close failed"); + printf("inbox: units=%llu image_cap=%llu off=%llu\n", (unsigned long long)units, + (unsigned long long)((units - 1) * CK_SLOT_UNIT), (unsigned long long)(first * bsz)); + return 0; +} + int main(int argc, char **argv) { if (argc < 2) @@ -1449,6 +1787,8 @@ int main(int argc, char **argv) return cmd_keys_c(argc, argv); if (strcmp(argv[1], "slots-image") == 0) return cmd_slots_image(argc, argv); + if (strcmp(argv[1], "inbox-image") == 0) + return cmd_inbox_image(argc, argv); if (strcmp(argv[1], "control-recover") == 0) return cmd_control_recover(argc, argv); return refuse("unknown mode"); diff --git a/scripts/.crumb b/scripts/.crumb index dabea57b..2baf8eb4 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T20:24:30Z", + "compiled_at": "2026-10-12T20:56:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:e35f107caf8f963b07c4c8885278702812e3af2af5451b6b47793c58890d18a0", + "digest": "sha256:8911239203abaca6f5315fecaa93bea68b0b425436b711cce44efeccdf221bdb", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 89, - "generated_at_commit": "cf5b432a7ffe41864750ff433acf7aa89cf67f0f", - "source_tree": "sha256:9c05c1257ecb21e4bc140cc337509ad262a199035d8b15f42f53b119f541d454" + "generated_at_commit": "04e39e7223149deef6c14e4175d40e80c63eee0a", + "source_tree": "sha256:d5c133e26d231b6fe0ac6e1021cec05c81407d981eefcafe916e9d067ce16918" }, "owner_lane": "", "provenance": { diff --git a/scripts/native_package.sh b/scripts/native_package.sh index 96d2220e..59eebcf4 100755 --- a/scripts/native_package.sh +++ b/scripts/native_package.sh @@ -6,7 +6,7 @@ # image/BOOTAA64.EFI the image, built twice from a clean tree; the two # builds must be byte-identical or the package is # refused as not signable (TRUST-1 Q24) -# PACKAGE.txt the provenance record, "key value" lines (format 2): +# PACKAGE.txt the provenance record, "key value" lines (format 3): # source revision (commit, commit time, tree_clean), # toolchain (cross prefix, gcc/ld/objcopy/make version # lines, build host arch), inputs (make goal and @@ -14,7 +14,11 @@ # pins, model and firmware digests, owner file digests) # and the resulting image (bytes, SHA-256, SHA-512). # See write_package_txt and docs/NATIVE_PACKAGE.md. -# SHA256SUMS sha256sum lines over image/BOOTAA64.EFI and PACKAGE.txt +# loader/SLOTLDR.EFI the signed A/B slot loader (P11 cut 3), built twice like +# the image: owner keys (CK_SLOT_KEYS = --owner-pubkeys) +# for hardware-staging, the labelled TEST key otherwise +# SHA256SUMS sha256sum lines over image/BOOTAA64.EFI, loader/SLOTLDR.EFI +# and PACKAGE.txt # PROVENANCE.txt "package_sha256 ": the resulting # package digest, which is also the slot manifest's # provenance field (it cannot live in PACKAGE.txt, @@ -58,7 +62,7 @@ set -euo pipefail here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) repo=$(cd "$here/.." && pwd) kdir="$repo/native/kernel" -FORMAT=2 +FORMAT=3 # 3: P11 cut 3 adds loader/SLOTLDR.EFI and its loader_* lines MAKEFILE_REL=native/kernel/Makefile IMAGE_LD_REL=native/boot/image.ld @@ -122,6 +126,25 @@ build_once() { esac } +# P11 cut 3: the slot loader that boots the package's slots (docs/NATIVE_PACKAGE.md, +# "Slot loader in the package"). hardware-staging: the owner public keys file is the +# loader's trusted-key table (ck_slot_tool keys-c refuses TEST keys in it); core and +# full: the labelled RFC 8032 TEST key loader (never for hardware). +loader_keys_of() { if [ "$1" = hardware-staging ]; then echo owner; else echo test; fi; } +build_loader_once() { + # $1 = OUT dir, $2 = variant, $3 = owner pubkeys, $4 = machine id + local out="$1" variant="$2" own="$3" mid="$4" + if [ "$variant" = hardware-staging ]; then + make -s -C "$kdir" OUT="$out" slot-loader CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="$own" CK_MACHINE_ID="$mid" \ + CK_SLOT_KEYS="$own" >"$out.loader.log" 2>&1 || refuse "slot loader build failed (see $out.loader.log)" + else + make -s -C "$kdir" OUT="$out" slot-loader CK_TEST_SLOT_KEY=1 >"$out.loader.log" 2>&1 \ + || refuse "slot loader build failed (see $out.loader.log)" + fi + echo "$out/SLOTLDR.EFI" +} + + write_package_txt() { # $1 = package dir, then named globals of cmd_build local dir="$1" own_sha="" mid_sha="" @@ -170,6 +193,12 @@ write_package_txt() { echo "image_sha256 $(sha256_of "$dir/image/BOOTAA64.EFI")" echo "image_sha512 $(sha512_of "$dir/image/BOOTAA64.EFI")" echo "reproducible two_clean_builds_identical" + # the slot loader (P11 cut 3) + echo "loader loader/SLOTLDR.EFI" + echo "loader_keys $(loader_keys_of "$variant")" + echo "loader_bytes $(size_of "$dir/loader/SLOTLDR.EFI")" + echo "loader_sha256 $(sha256_of "$dir/loader/SLOTLDR.EFI")" + echo "loader_reproducible two_clean_builds_identical" } >"$dir/PACKAGE.txt" } @@ -256,12 +285,22 @@ cmd_build() { refuse "two clean builds differ ($da vs $db): not reproducible, not signable" fi - mkdir -p "$out/image" "$out/slot" + local ldr_a ldr_b + ldr_a=$(build_loader_once "$scratch/a" "$variant" "$own" "$mid") + ldr_b=$(build_loader_once "$scratch/b" "$variant" "$own" "$mid") + if [ "$(sha256_of "$ldr_a")" != "$(sha256_of "$ldr_b")" ]; then + rm -rf "$scratch" + refuse "two clean slot loader builds differ: not reproducible, not signable" + fi + + mkdir -p "$out/image" "$out/slot" "$out/loader" cp "$img_a" "$out/image/BOOTAA64.EFI" chmod 444 "$out/image/BOOTAA64.EFI" + cp "$ldr_a" "$out/loader/SLOTLDR.EFI" + chmod 444 "$out/loader/SLOTLDR.EFI" rm -rf "$scratch" write_package_txt "$out" - (cd "$out" && sha256sum image/BOOTAA64.EFI PACKAGE.txt >SHA256SUMS) + (cd "$out" && sha256sum image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt >SHA256SUMS) local prov tool prov=$(sha256_of "$out/SHA256SUMS") echo "package_sha256 $prov" >"$out/PROVENANCE.txt" @@ -288,10 +327,13 @@ rec() { awk -v k="$2" '$1==k{sub(/^[^ ]+ /,""); print}' "$1/PACKAGE.txt"; } check_package_files() { local dir="$1" [ -d "$dir" ] || refuse "package directory missing" - for f in image/BOOTAA64.EFI PACKAGE.txt SHA256SUMS PROVENANCE.txt slot/MANIFEST.bin; do + for f in image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt SHA256SUMS PROVENANCE.txt slot/MANIFEST.bin; do [ -f "$dir/$f" ] || refuse "package is missing $f" done (cd "$dir" && sha256sum -c --quiet --strict SHA256SUMS >/dev/null 2>&1) || refuse "SHA256SUMS does not match the package files" + for f in image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt; do + [ -n "$(awk -v f="$f" '$2==f' "$dir/SHA256SUMS")" ] || refuse "SHA256SUMS does not cover $f" + done local fmt fmt=$(rec "$dir" format) [ "$fmt" = "$FORMAT" ] || refuse "PACKAGE.txt format $fmt is not $FORMAT" @@ -308,7 +350,8 @@ check_record() { local -a required=(format repo commit commit_time tree_clean variant generation policy_generation min_rollback_generation toolchain_cross toolchain_cc toolchain_ld toolchain_objcopy toolchain_make build_host_arch input_make_goal input_make_vars input_env_ck input_makefile_sha256 - input_image_ld_sha256 image image_bytes image_sha256 image_sha512 reproducible) + input_image_ld_sha256 image image_bytes image_sha256 image_sha512 reproducible + loader loader_keys loader_bytes loader_sha256 loader_reproducible) for k in "${required[@]}"; do [ "$(awk -v k="$k" '$1==k' "$dir/PACKAGE.txt" | wc -l)" = 1 ] || refuse "PACKAGE.txt must hold exactly one $k line" done @@ -318,6 +361,19 @@ check_record() { [ "$(rec "$dir" image_sha256)" = "$(sha256_of "$img")" ] || refuse "PACKAGE.txt image_sha256 does not match the image" [ "$(rec "$dir" image_sha512)" = "$(sha512_of "$img")" ] || refuse "PACKAGE.txt image_sha512 does not match the image" [ "$(rec "$dir" reproducible)" = two_clean_builds_identical ] || refuse "PACKAGE.txt does not record a reproducible build" + # The slot loader (P11 cut 3): bytes, digest, reproducibility, and a key source that + # matches the variant (TEST-key loaders carry their "TEST-ONLY key" note, owner ones none). + local ldr="$dir/loader/SLOTLDR.EFI" + [ "$(rec "$dir" loader)" = loader/SLOTLDR.EFI ] || refuse "PACKAGE.txt loader is not loader/SLOTLDR.EFI" + [ "$(rec "$dir" loader_bytes)" = "$(size_of "$ldr")" ] || refuse "PACKAGE.txt loader_bytes does not match the loader" + [ "$(rec "$dir" loader_sha256)" = "$(sha256_of "$ldr")" ] || refuse "PACKAGE.txt loader_sha256 does not match the loader" + [ "$(rec "$dir" loader_reproducible)" = two_clean_builds_identical ] || refuse "PACKAGE.txt does not record a reproducible loader build" + [ "$(rec "$dir" loader_keys)" = "$(loader_keys_of "$(rec "$dir" variant)")" ] || refuse "PACKAGE.txt loader_keys does not match the variant" + if [ "$(rec "$dir" loader_keys)" = test ]; then + LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY key" "$ldr" || refuse "TEST-key loader lacks its TEST-ONLY note" + elif LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY" "$ldr"; then + refuse "owner-key loader carries a TEST-ONLY note" + fi local tc; tc=$(rec "$dir" tree_clean) case "$tc" in yes) ;; @@ -337,7 +393,7 @@ check_record() { [ "$(rec "$dir" input_make_goal)" = "$(make_goal_of "$variant")" ] || refuse "PACKAGE.txt input_make_goal does not match variant $variant" [ "$(rec "$dir" input_make_vars)" = "$(make_vars_of "$variant" "$own_sha" "$mid_sha")" ] || refuse "PACKAGE.txt input_make_vars does not match variant $variant" echo "record:" - for k in image image_bytes image_sha256 image_sha512 reproducible tree_clean input_env_ck variant input_make_goal input_make_vars; do + for k in image image_bytes image_sha256 image_sha512 reproducible loader loader_keys loader_bytes loader_sha256 loader_reproducible tree_clean input_env_ck variant input_make_goal input_make_vars; do echo " checked $k $(rec "$dir" "$k")" done diff --git a/scripts/native_package_test.sh b/scripts/native_package_test.sh index f75fc8af..f3bbe94f 100755 --- a/scripts/native_package_test.sh +++ b/scripts/native_package_test.sh @@ -28,7 +28,7 @@ expect_refused() { # what, needle, cmd... has_line() { # what, file, regex if /usr/bin/grep -qE -- "$3" "$2"; then pass "$1"; else fail "$1"; fi } -remake_sums() { (cd "$1" && sha256sum image/BOOTAA64.EFI PACKAGE.txt >SHA256SUMS); } +remake_sums() { (cd "$1" && sha256sum image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt >SHA256SUMS); } remake_all() { remake_sums "$1"; echo "package_sha256 $(sha256sum "$1/SHA256SUMS" | cut -c1-64)" >"$1/PROVENANCE.txt"; } dirty=() tree_clean=yes @@ -49,7 +49,7 @@ for k in "commit " "image_sha256 " "reproducible two_clean_builds_identical" "pi done [ "$(stat -c %s "$T/p1/slot/MANIFEST.bin")" = 4096 ] && pass "manifest is 4096 bytes" || fail "manifest size" # 1b. the provenance record: source revision, toolchain, inputs, resulting digest -has_line "record: format 2" "$P" "^format 2$" +has_line "record: format 3" "$P" "^format 3$" has_line "record: commit is HEAD" "$P" "^commit $(git -C "$repo" rev-parse HEAD)$" has_line "record: commit_time is HEAD's" "$P" "^commit_time $(git -C "$repo" log -1 --format=%ct)$" has_line "record: tree_clean $tree_clean" "$P" "^tree_clean $tree_clean$" @@ -69,6 +69,13 @@ has_line "record: image_sha512" "$P" "^image_sha512 $(sha512sum "$T/p1/image/BOO has_line "PROVENANCE.txt package_sha256 = sha256(SHA256SUMS)" "$T/p1/PROVENANCE.txt" "^package_sha256 $(sha256sum "$T/p1/SHA256SUMS" | cut -c1-64)$" "$NATIVE_PACKAGE_TOOL_OUT/host/ck_slot_tool" show "$T/p1/slot/MANIFEST.bin" | /usr/bin/grep -q "provenance=$(sha256sum "$T/p1/SHA256SUMS" | cut -c1-64)" \ && pass "manifest provenance = package_sha256" || fail "manifest provenance" +# 1c. the slot loader in the package (P11 cut 3): TEST-key loader for a full package +has_line "record: loader path" "$P" "^loader loader/SLOTLDR.EFI$" +has_line "record: loader_keys test (full package)" "$P" "^loader_keys test$" +has_line "record: loader_sha256" "$P" "^loader_sha256 $(sha256sum "$T/p1/loader/SLOTLDR.EFI" | cut -c1-64)$" +has_line "record: loader_reproducible" "$P" "^loader_reproducible two_clean_builds_identical$" +has_line "SHA256SUMS covers the loader" "$T/p1/SHA256SUMS" " loader/SLOTLDR.EFI$" +LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY key" "$T/p1/loader/SLOTLDR.EFI" && pass "full package loader carries its TEST-ONLY key note" || fail "full package loader lacks the TEST-ONLY note" cp -r "$T/p1" "$T/p1o" # unsigned copy for the offline-signing round trip (section 8) expect_refused "build refuses an existing --out" "already exists" "$np" build --out "$T/p1" --generation 7 "${dirty[@]}" echo "aien-dev/omega notahash" >"$T/badpins.txt" @@ -106,6 +113,10 @@ echo "$out" | /usr/bin/grep -q "^ checked toolchain_cc " && pass "verify --sam # 4. tampering (each on a fresh copy) cp -r "$T/p1" "$T/t1"; chmod u+w "$T/t1/image/BOOTAA64.EFI"; printf '\xff' | dd of="$T/t1/image/BOOTAA64.EFI" bs=1 seek=100 conv=notrunc status=none expect_refused "tampered image byte" "SHA256SUMS does not match" "$np" verify --package "$T/t1" --test-key "${dirty[@]}" +cp -r "$T/p1" "$T/t1l"; chmod u+w "$T/t1l/loader/SLOTLDR.EFI"; printf '\xff' | dd of="$T/t1l/loader/SLOTLDR.EFI" bs=1 seek=100 conv=notrunc status=none +expect_refused "tampered loader byte" "SHA256SUMS does not match" "$np" verify --package "$T/t1l" --test-key "${dirty[@]}" +cp -r "$T/p1" "$T/t1m"; rm -f "$T/t1m/loader/SLOTLDR.EFI" +expect_refused "missing loader" "missing loader/SLOTLDR.EFI" "$np" verify --package "$T/t1m" --test-key "${dirty[@]}" cp -r "$T/p1" "$T/t2"; sed -i 's/^generation 7$/generation 9/' "$T/t2/PACKAGE.txt" expect_refused "tampered PACKAGE.txt" "SHA256SUMS does not match" "$np" verify --package "$T/t2" --test-key "${dirty[@]}" cp -r "$T/p1" "$T/t3"; sed -i 's/^generation 7$/generation 9/' "$T/t3/PACKAGE.txt"; remake_sums "$T/t3" @@ -139,6 +150,9 @@ tamper env-ck 's/^input_env_ck .*/input_env_ck CK_EXTRA_CFLAGS/' "input_env_ck i tamper missing-toolchain '/^toolchain_objcopy /d' "exactly one toolchain_objcopy" "${dirty[@]}" tamper duplicate-commit '/^commit /p' "exactly one commit" "${dirty[@]}" tamper toolchain-same-host 's/^toolchain_cc .*/toolchain_cc gcc 0.0/' "toolchain_cc differs from this host" --same-host "${dirty[@]}" +tamper loader-sha256 's/^loader_sha256 f/loader_sha256 0/;t;s/^loader_sha256 ./loader_sha256 f/' "loader_sha256 does not match" "${dirty[@]}" +tamper loader-keys 's/^loader_keys .*/loader_keys owner/' "loader_keys does not match the variant" "${dirty[@]}" +tamper missing-loader-line '/^loader_bytes /d' "exactly one loader_bytes" "${dirty[@]}" # 5. a signed manifest from another package does not verify here expect_ok "build core package generation 8" "$np" build --out "$T/p2" --generation 8 --variant core "${dirty[@]}" @@ -155,6 +169,8 @@ expect_refused "non-reproducible build refused (test hook)" "not reproducible, n expect_ok "build hardware-staging package (fixture owner files)" "$np" build --out "$T/p4" --generation 7 --variant hardware-staging --owner-pubkeys "$fix/TEST-FIXTURE-owner-pubkeys.txt" --machine-id "$fix/TEST-FIXTURE-machine-id.txt" "${dirty[@]}" /usr/bin/grep -q "^owner_pubkeys_sha256 " "$T/p4/PACKAGE.txt" && pass "staging PACKAGE.txt records owner_pubkeys_sha256" || fail "staging owner digest missing" /usr/bin/grep -q "^variant hardware-staging$" "$T/p4/PACKAGE.txt" && pass "staging variant recorded" || fail "staging variant" +has_line "staging record: loader_keys owner" "$T/p4/PACKAGE.txt" "^loader_keys owner$" +LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY" "$T/p4/loader/SLOTLDR.EFI" && fail "staging loader carries a TEST-ONLY note" || pass "staging loader carries no TEST-ONLY note (owner keys only)" has_line "staging record: input_make_vars names the owner files by digest" "$T/p4/PACKAGE.txt" \ "^input_make_vars CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=sha256:$(sha256sum "$fix/TEST-FIXTURE-owner-pubkeys.txt" | cut -c1-64) CK_MACHINE_ID=sha256:$(sha256sum "$fix/TEST-FIXTURE-machine-id.txt" | cut -c1-64)$" expect_refused "staging variant needs both owner files" "needs --owner-pubkeys" "$np" build --out "$T/p5" --generation 7 --variant hardware-staging "${dirty[@]}" diff --git a/scripts/qemu_ck_lifecycle_test.sh b/scripts/qemu_ck_lifecycle_test.sh index 5318523c..889e1efa 100755 --- a/scripts/qemu_ck_lifecycle_test.sh +++ b/scripts/qemu_ck_lifecycle_test.sh @@ -329,7 +329,7 @@ rcheck 161 "task refused while locked" '^lifecycle: REFUSED verb=task state=lock rcheck 161 "unlock refused, still locked" '^lifecycle: REFUSED verb=unlock state=locked reason="production key source refused: still locked"' rcheck 161 "unlock asked the production key source again" '^store: unlock REFUSED proof=keyed step="production Store key source" rc=-2002' rcheck 161 "status answered" '^lifecycle: state=locked store=locked pending=- requests=' -rcheck 161 "help answered" '^lifecycle: verbs: status unlock \[<64-hex recovery secret>\] rotate task prompt approve deny cancel recall remember facts evidence help shutdown$' +rcheck 161 "help answered" '^lifecycle: verbs: status unlock \[<64-hex recovery secret>\] rotate task prompt approve deny cancel recall remember facts update evidence help shutdown$' rcheck 161 "evidence answered" '^lifecycle: evidence requests=5 refused=2 invalid=0 tasks=0 approved=0' rcheck 162 "stop order with no Store" '^lifecycle: stop order store=was-locked nvme=released state=stop requests=6$' before 162 "locked lifecycle ready < NVMe revoke (revoke only at the stop order)" '^lifecycle: ready state=locked' '^dma_gate: nvme bus master revoked' diff --git a/scripts/qemu_ck_slot_loader_test.sh b/scripts/qemu_ck_slot_loader_test.sh index 110101d1..d7669eab 100755 --- a/scripts/qemu_ck_slot_loader_test.sh +++ b/scripts/qemu_ck_slot_loader_test.sh @@ -36,6 +36,17 @@ # promoted; next boot falls back to A and the kernel abandons B (flags 0x01). # 11 negative control: the service image started without SLOTLDR finds no boot # record and writes nothing. +# P11 cut 3 (the inactive-slot updater, operator verb `update`; native/boot/slot_update.c), +# disk layout aienos-slots-inbox (Store + slots + update inbox): +# 12 update from active A writes the inbox image into B and stages it; a second +# update is refused (candidate staged); A's bytes unchanged; next boot tries B +# and the kernel promotes it. +# 13 QEMU killed mid-image-write (TEST-only "update halt-after=1"): control and A +# untouched, the loader refuses the half-written B and boots A, a new update works. +# 14 inbox signed by the wrong key / with a flipped image byte: refused, slots +# partition byte-identical. +# 15 update refused while the Store is LOCKED; a TEST-key updater image is refused +# by make under CK_HARDWARE_STAGING. # # Output: one PASS/FAIL line per check, then # AIENOS_CK_SLOT_LOADER: PASS|FAIL|NOT_RUN [(...)] @@ -332,9 +343,19 @@ cap_s="$(geo_val image_cap)" "${tool}" select --control "${work}/c_stage_b" --a "${SA1}" --b "${SL2}" --test-key --cap "${cap_s}" --out "${work}/c_t2" >/dev/null || true "${tool}" select --control "${work}/c_t2" --a "${SA1}" --b "${SL2}" --test-key --cap "${cap_s}" --out "${work}/c_t1" >/dev/null || true +# Operator lines sboot sends once the lifecycle is ready (P11 cut 3), and the +# answer each one waits for. +sops=() +sans='^slot: update (staged|REFUSED)|^slot: TEST-ONLY update halted|^lifecycle: REFUSED verb=update' +swait() { # raw-file regex count pid: until count answers are on the serial (or QEMU exits) + while kill -0 "$4" 2>/dev/null && [[ "$(tr -d '\r' <"$1" | grep -cE "$2" || true)" -lt "$3" ]]; do sleep 1; done +} + +skilled=0 # 1 when sboot itself killed QEMU (the power cut of row 13) qpid="" sboot() { # name timeout-seconds [efi]: ESP with SLOTLDR (or efi), NVMe ${disk}; "shutdown" once ready local name="$1" to="$2" efi="${3:-${out}/SLOTLDR.EFI}" w t0 rc dpid + skilled=0 w="${work}/s-${name}" mkdir -p "${w}/esp/EFI/BOOT" "${w}/esp/EFI/AIENOS" touch "${w}/esp/EFI/AIENOS/BOOTREPORT.TXT" @@ -363,7 +384,20 @@ sboot() { # name timeout-seconds [efi]: ESP with SLOTLDR (or efi), NVMe ${disk}; done if tr -d '\r' <"${w}/serial.raw" | grep -q '^lifecycle: ready state='; then sleep 1 - printf 'shutdown\r' >&4 || true + # P11 cut 3: operator lines (sops) first, each answered before the next. + local op n=0 halted=0 + for op in "${sops[@]}"; do + n=$((n + 1)) + printf '%s\r' "${op}" >&4 || true + swait "${w}/serial.raw" "${sans}" "${n}" "${qpid}" + if tr -d '\r' <"${w}/serial.raw" | grep -q '^slot: TEST-ONLY update halted'; then halted=1; break; fi + done + if [[ "${halted}" == 1 ]]; then + kill "${qpid}" 2>/dev/null || true # the power cut: QEMU dies mid-update + skilled=1 + else + printf 'shutdown\r' >&4 || true + fi fi set +e wait "${qpid}" @@ -438,10 +472,123 @@ lacks 11 "no promotion without the loader's record" "slot: promoted" ctl_is 11 "control untouched" "seq=1 control: active=0 candidate=1 tries=3" row_end 11 +# ==== P11 cut 3: the inactive-slot updater (operator verb `update`) ================ +# Disk layout aienos-slots-inbox: AIENOS Store + slots + the update inbox partition +# (ck_slot_tool inbox-image writes a signed candidate there; the kernel reads it). +# The running service (slot A, TEST keys) verifies the inbox image (signature + digest) +# before writing anything, writes the INACTIVE slot only, reads it back, then stages it. +gline="$("${gpt}" create "${work}/upd.img" 512 80 aienos-slots-inbox)" +first="$(sed -n 's/.*slots_first_lba=\([0-9]*\).*/\1/p' <<<"${gline}")" +last="$(sed -n 's/.*slots_last_lba=\([0-9]*\).*/\1/p' <<<"${gline}")" +ifirst="$(sed -n 's/.*inbox_first_lba=\([0-9]*\).*/\1/p' <<<"${gline}")" +ilast="$(sed -n 's/.*inbox_last_lba=\([0-9]*\).*/\1/p' <<<"${gline}")" +[[ -n "${first}" && -n "${last}" && -n "${ifirst}" && -n "${ilast}" ]] || { bad "ck_gpt_image aienos-slots-inbox gave no geometry: ${gline}"; first=0; last=0; ifirst=0; ilast=0; } +disk="${work}/upd.img" +cp "${disk}" "${work}/blank.img" +inbox() { # manifest,image: write the inbox partition + "${tool}" inbox-image --disk "${disk}" --bs 512 --first "${ifirst}" --last "${ilast}" --m "$1" | grep -q '^inbox:' || bad "inbox-image refused: $1" +} +slot_sum() { # A|B: SHA-256 of that slot's whole byte range on the disk + local off + off="$(geo_val "$(tr 'AB' 'ab' <<<"$1")_off")" + dd if="${disk}" bs=4096 skip="$((off / 4096))" count="$(( $(geo_val image_cap) / 4096 + 1 ))" status=none | sha256sum | cut -c1-64 +} +part_sum() { # SHA-256 of the whole slots partition (control region + both slots) + dd if="${disk}" bs=512 skip="${first}" count="$((last - first + 1))" status=none | sha256sum | cut -c1-64 +} +cp "${svc_b}" "${work}/svc_b_bad" +flip "${work}/svc_b_bad" 4242 +mm SB2k2 "${svc_b}" 2 2 --test2 +SB2k2="${work}/SB2k2,${svc_b}" +SB2bad="${work}/SB2,${work}/svc_b_bad" +mm SL1 "${svc_l}" 1 +SL1="${work}/SL1,${svc_l}" + +# ---- row 12: update writes B from the inbox, stages it; next boot tries + promotes ---- +slots --a "${SA1}" --control "${work}/c_init_a" +inbox "${SB2}" +a_sum="$(slot_sum A)" +sops=("update" "update") +sboot r12-update "${svc_to}" +sops=() +has 12 "service booted from active A" "slot: booted slot A reason=active" +has 12 "update verified, wrote and staged B" "slot: update staged slot B as candidate generation=2" +has 12 "second update refused: candidate already staged" "slot: update REFUSED a candidate is already staged" +ran 12 A +ctl_is 12 "control: B candidate with 3 tries, A active" "seq=2 control: active=0 candidate=1 tries=3" +if [[ "$(slot_sum A)" == "${a_sum}" ]]; then ok "row 12: slot A bytes unchanged by the update"; else bad "row 12: slot A bytes changed"; fi +region "${work}/c12.region" +"${tool}" control-recover --region "${work}/c12.region" --out "${work}/c12" >/dev/null || true +tool_agrees 12 "${work}/c12" "${SA1}" "${SB2}" "boot_slot=B reason=candidate_try loader_write=1" +sboot r12-try "${svc_to}" +has 12 "loader tries the written B" "slot_loader: boot=B reason=candidate_try gen=2" +has 12 "kernel promoted the updated B" "slot: promoted slot B to active generation=2 floor=2" +ran 12 B +ctl_is 12 "control: B active" "control: active=1 candidate=none tries=0 flags=0x00 floor=2 last_good=2" +row_end 12 + +# ---- row 13: interrupted update (QEMU killed mid-write) -> B invalid, control untouched +slots --a "${SA1}" --control "${work}/c_init_a" +inbox "${SB2}" +a_sum="$(slot_sum A)" +sops=("update halt-after=1") +sboot r13-halt "${svc_to}" +sops=() +has 13 "update started" "slot: update from the inbox into the inactive slot (booted A, keys=1)" +has 13 "halted mid-image (power cut model)" "slot: TEST-ONLY update halted after 64 of" +lacks 13 "never staged" "slot: update staged" +if [[ "${skilled}" == 1 ]]; then ok "row 13: QEMU killed mid-update by the gate (power cut model)"; else bad "row 13: QEMU was not killed mid-update"; fi +ctl_is 13 "control record untouched" "seq=1 control: active=0 candidate=none tries=0" +if [[ "$(slot_sum A)" == "${a_sum}" ]]; then ok "row 13: slot A bytes unchanged"; else bad "row 13: slot A bytes changed"; fi +sops=("update") +sboot r13-after "${svc_to}" +sops=() +has 13 "loader refuses the half-written B" "slot_loader: slot B refused" +has 13 "loader boots A" "slot_loader: boot=A reason=active gen=1" +ran 13 A +has 13 "the update is repeatable after the interruption" "slot: update staged slot B as candidate generation=2" +ctl_is 13 "control: B staged by the repeated update" "seq=2 control: active=0 candidate=1 tries=3" +row_end 13 + +# ---- row 14: negative controls: wrong key, wrong digest -> refused, nothing written ---- +for k in key digest; do + slots --a "${SA1}" --control "${work}/c_init_a" + if [[ "${k}" == key ]]; then inbox "${SB2k2}"; else inbox "${SB2bad}"; fi + p_sum="$(part_sum)" + sops=("update") + sboot "r14-${k}" "${svc_to}" + sops=() + has 14 "wrong ${k}: refused before any write" "slot: update REFUSED" + has 14 "wrong ${k}: nothing written" ": nothing written" + lacks 14 "wrong ${k}: never staged" "slot: update staged" + ran 14 A + ctl_is 14 "wrong ${k}: control untouched" "seq=1 control: active=0 candidate=none tries=0" + if [[ "$(part_sum)" == "${p_sum}" ]]; then ok "row 14: wrong ${k}: slots partition byte-identical"; else bad "row 14: wrong ${k}: slots partition changed"; fi +done +row_end 14 + +# ---- row 15: no update without an unlocked Store; TEST keys refused under staging ---- +slots --a "${SL1}" --control "${work}/c_init_a" +inbox "${SB2}" +p_sum="$(part_sum)" +sops=("update") +sboot r15-locked "${svc_to}" +sops=() +has 15 "plain service image stays LOCKED" "lifecycle: ready state=locked" +has 15 "update refused while locked" "lifecycle: REFUSED verb=update state=locked reason=\"locked: unlock before updating a slot\"" +if [[ "$(part_sum)" == "${p_sum}" ]]; then ok "row 15: slots partition byte-identical"; else bad "row 15: slots partition changed"; fi +mk_out="$(make -n -C native/kernel CROSS="${cross}" OUT="${work}/hs" CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 CK_HARDWARE_STAGING=1 full 2>&1 || true)" +if grep -qF -- "CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING" <<<"${mk_out}"; then + ok "row 15: TEST-key updater image refused under CK_HARDWARE_STAGING (make)" +else + bad "row 15: make accepted a TEST-key service image under CK_HARDWARE_STAGING" +fi +row_end 15 + gh_lock_release echo "slot loader gate: ${boots} boots, ${checks} checks, ${fails} failures, QEMU ${qemu_secs} s (commit ${commit:0:12}; QEMU only, hardware NOT_RUN)" if [[ "${fails}" == 0 ]]; then - echo "${tag}: PASS (11 rows, ${boots} boots, ${checks} checks; QEMU, not hardware)" + echo "${tag}: PASS (15 rows, ${boots} boots, ${checks} checks; QEMU, not hardware)" else echo "${tag}: FAIL (${fails} of ${checks} checks)" exit 1