CodeQL / Unpinned tag for a non-immutable Action or reusable workflow
Unpinned 3rd party Action 'C kernel (CK) gates' step Uses Step uses 'dtolnay/rust-toolchain' with ref 'stable', not a pinned commit hash
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
Unpinned 3rd party Action 'C kernel (CK) gates' step Uses Step uses 'dtolnay/rust-toolchain' with ref 'stable', not a pinned commit hash
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/16892597a51b73bd.bundle -o 16892597a51b73bd.bundle git fetch 16892597a51b73bd.bundle refs/pull/392/head
github-import 1
aien-dev/aienux
392
f9899249ff727a0d264a8d346462c0b69eedbdee
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":392,"url":"https://github.com/aien-dev/aienux/pull/392","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:20Z","metadata":"unavailable","state_derived":true}
diff --git a/.crumb b/.crumb index fced4e5e..1478c024 100644 --- a/.crumb +++ b/.crumb @@ -22,38 +22,38 @@ "children": [ { "digest": "b5ced6fea583b485206b73ec08b91de68a5cfd2672ac448191831754c9e0907d", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "2f740ef54106c2b6419bfd2df1266762cd444f439c7e12ae2a7c5c3877de926c", - "last_commit": "4bd49ca", + "digest": "70db91df5507a6c7b251f88d6f5abf42d8ad0532d3a17bc0eeb99276c3ba7be8", + "last_commit": "be246b3", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "64cc87f8507bb580e1788d335c5a76039f3e3a016552595526718448ab5aa098", - "last_commit": "3037967", + "digest": "74e9389336c778d22cbcee7ddbe725beea3afb71a1fe45b6b37d9ca59375d0d8", + "last_commit": "be246b3", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "9adb69cc749bc97fa48e268f0397b900da83c84191ab6a9373358e8b9ab57922", - "last_commit": "44cf2dd", + "digest": "dc53d5532581d1c4e53f3ea7426c322e8c06e4b65ca4c5a624a8447c1dedc97b", + "last_commit": "be246b3", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:19cc8f21ac13e49018f60836bdaf7c4c23477cf558624048754f1e83c6d88cac", - "compiled_at": "2026-10-17T01:49:45Z", + "children_root": "sha256:a96709672dac7686de2a9da41916737adb0be792e995aee7e8b50d45cce7054a", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:1b7f96caef28425fc1a0751dd671452aa26ef3561f79f663480e083341950199", + "digest": "sha256:189bc10894bee61eabb118fba61d772a51be6f9a7ae6498692abf852a1b42cf8", "evidence_files": 211, "evidence_root": "sha256:3a8f56415109a697e4beb6048c85805c50c9c6c4089629057f4e8b3e76863981", "files": 3501, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:4fa99ea1d4144da8067bb5d01898dc38984898b2c5d5fb06b0e19033c824b265" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:2dc640be8627341aa0246b794cb35217f2dcc8bec59c6cf5ac80a44b2010d1b4" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/ck-kernel.yml b/.github/workflows/ck-kernel.yml index b116f380..258db85e 100644 --- a/.github/workflows/ck-kernel.yml +++ b/.github/workflows/ck-kernel.yml @@ -46,6 +46,8 @@ on: - 'scripts/lib_gate_hold.sh' - 'scripts/gate_hold_selftest.sh' - 'scripts/ck_shards_verify.sh' + - 'scripts/native_package.sh' + - 'scripts/native_package_test.sh' - '.github/workflows/ck-kernel.yml' pull_request: branches: [main] @@ -60,6 +62,8 @@ on: - 'scripts/lib_gate_hold.sh' - 'scripts/gate_hold_selftest.sh' - 'scripts/ck_shards_verify.sh' + - 'scripts/native_package.sh' + - 'scripts/native_package_test.sh' - '.github/workflows/ck-kernel.yml' permissions: @@ -84,7 +88,7 @@ jobs: fail-fast: false matrix: include: - - { name: plain, targets: "test" } + - { name: plain, targets: "test", rust: true } - { name: ASan + UBSan, targets: "sanitize" } - { name: stage plain, targets: "stage-test" } - { name: stage ASan + UBSan, targets: "stage-sanitize" } @@ -94,6 +98,11 @@ jobs: run: | sudo apt-get update sudo apt-get install -y build-essential + - name: Install Rust toolchain (make test runs package-test, which builds the aarch64-unknown-none inference crate for the service package) + if: matrix.rust + uses: dtolnay/rust-toolchain@stable + with: + targets: aarch64-unknown-none - name: Build and run (make -j) run: make -j"$(nproc)" -C native/kernel ${{ matrix.targets }} - name: Check the host tests left the tree clean (the gate runner refuses a dirty tree) diff --git a/docs/.crumb b/docs/.crumb index 51b2754a..52f646b2 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:491e93c09b6185b662afda8b29d24bb3ee81bc2aed9009757fdf4674d197be31", - "compiled_at": "2026-10-16T10:58:02Z", + "compiled_at": "2026-10-16T20:34:49Z", "compiler": "crumb-compile/1", - "digest": "sha256:2f740ef54106c2b6419bfd2df1266762cd444f439c7e12ae2a7c5c3877de926c", + "digest": "sha256:70db91df5507a6c7b251f88d6f5abf42d8ad0532d3a17bc0eeb99276c3ba7be8", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 216, - "generated_at_commit": "4637b04d06f4e3cec4c79920bbed0efb46b93995", - "source_tree": "sha256:1abaa75a2a66a6ce77842c9439ad7a5cbb7dee680ef47a6c33d9b865e8f70417" + "generated_at_commit": "d8ab339757a1e3da45b7d40451fdd4be1f9f6163", + "source_tree": "sha256:a6d259728954d2e6510e973029c8c09ee0c94a21b3839f33dd6a29447bbd9738" }, "owner_lane": "", "provenance": { diff --git a/docs/NATIVE_IMAGE_SLOTS.md b/docs/NATIVE_IMAGE_SLOTS.md index f3ee8cdf..b40e07d5 100644 --- a/docs/NATIVE_IMAGE_SLOTS.md +++ b/docs/NATIVE_IMAGE_SLOTS.md @@ -1,9 +1,10 @@ -# Native image slots: install, update and recovery without Linux (P11 cuts 1, 2a, 2b and 3) +# Native image slots: install, update and recovery without Linux (P11 cuts 1, 2a, 2b, 3 and 4) 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) and cut 3 (the inactive-slot -updater and the loader in the package, section 9) done; the owner +updater and the loader in the package, section 9) and cut 4 (the +known-good native recovery image, section 10) done in QEMU; 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 @@ -217,6 +218,8 @@ nothing and to re-release anything. 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. + **Cut 4 (done, QEMU; section 10).** The recovery image lives on the + loader's own ESP and runs only when neither slot may boot. **Cut 2b (done, QEMU; section 8): kernel-side promote.** After its health milestone the kernel writes the control record over its own NVMe path (promote the @@ -432,5 +435,114 @@ stay refused by the Makefile (gate row 15) and by `keys-c`. - 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. +The known-good recovery image for the case where both slots are invalid +is cut 4 (section 10). Hardware NOT_RUN: nothing was written to a physical disk. + +## 10. The known-good native recovery image (cut 4) + +Cuts 2a to 3 cover one bad slot: the loader falls back to the other one. When +both slots are unusable the loader used to stop with `recovery_required` and +hand the machine back to the firmware. Cut 4 adds a small, signed AIENOS image +that SLOTLDR.EFI starts in that case, and that can rewrite a slot without +Linux. + +**What it is.** A service image built with `CK_RECOVERY=1` +(`make -C native/kernel CK_SERVICE=1 CK_RECOVERY=1 full`; output directory +suffix `-recovery`). It is the service kernel without a model and without an +inference unit (make refuses `CK_RECOVERY` with `CK_INFER_LIB`), and it says so +on its first lifecycle line: `recovery: AIENOS native recovery image (P11 cut +4): no model, ...`. Its manifest has the slot format (section 4), is signed by +the same trusted key as a slot image (today the Owner Root, see +`docs/OWNER_KEY_CEREMONY.md`) and binds the image by SHA-512. + +**Where it lives and how it is selected** (`native/boot/slot_loader.c`, +`slot_format.h`): + +- On the loader's own boot volume (the ESP): `\EFI\AIENOS\RECOVERY.MAN` + (exactly one 4096-byte manifest unit) and `\EFI\AIENOS\RECOVERY.EFI` + (at most 64 MiB, `CK_SLOT_RECOVERY_MAX_BYTES`). +- Only after the loader has found the slots partition, read the control + record and both slots, and `ck_slot_select` returned `recovery_required` + (both slots invalid, below the floor, or the active one unusable while the + candidate is out of tries). Whenever a slot may boot, the recovery image is + never used, even if it is on the ESP. +- Not for the earlier refusals: no trusted key built in, no slots partition + or two of them, a damaged primary GPT, a slots partition that is too small. + Then the loader still returns `EFI_LOAD_ERROR` to the firmware; this cut + does not repair the partition table. +- Verified like a slot: manifest structure, a trusted signer, the Ed25519 + signature over the manifest, and the image's SHA-512. A refused or missing + recovery image prints `slot_loader: recovery image refused (<why>): not + booted` and nothing starts. On success the loader prints `slot_loader: + boot=R reason=recovery_required gen=<n> ...`, hands the kernel a boot record + with slot 2 (`CK_SLOT_BOOT_RECOVERY`, shown as `R`), reason + `recovery_required` and tries 0, and starts the image with `LoadImage` + + `StartImage` from the verified bytes. The loader writes nothing on this + path. +- The same image may also be staged into a slot like any other signed image. + Booted from a slot it is an ordinary slot image: the normal rules apply + (it is promoted after health, and its updater refuses unless it is the + active slot). + +**What it can do.** + +- Unlock the Store (the service image's `unlock` verb; in QEMU the TEST keys + open it at boot), show `status` and `evidence`, and `shutdown`. +- Rewrite a slot from a package: the operator verb `update` runs the cut 3 + updater (section 9) unchanged, with one difference: booted as the recovery + image it does not require to be the active slot or that no candidate is + staged. It writes the slot that is not active, after the same checks + (signature and digest before any write, generation above the floor and + above the last good generation, read back after the write), and stages it + as the candidate with fresh tries, replacing a stale (failed or exhausted) + candidate. The active slot and the loader are never written. +- After `shutdown` and a reboot, the loader tries the rewritten slot + (`candidate_try`) and the kernel promotes it after its health milestone + (section 8). From then on the machine boots normally. + +**What it cannot do.** + +- Promote or abandon anything: its boot record names no slot, and the kernel + says `slot: booted the recovery image (both slots unusable) ...: nothing + promoted or abandoned`. +- Write without a control record: a disk whose control region is empty or + corrupt is refused (`no control record on disk`, nothing written). The + control record is created at install time. +- Install an image below the generation floor or not newer than the last + good generation (no rollback through recovery). +- Repair the GPT, the ESP, the loader or itself; rotate keys; use the network + or a TPM; run a model. +- Production unlock still follows the Store's own key rules: an image built + for hardware staging without its keys stays LOCKED and refuses `update` + (gate row 15). + +**Limits recorded here.** No generation floor applies to the recovery image +itself: an older, owner-signed recovery image on the ESP would still start. +It can only write a slot through the checks above, so it cannot install an +image below the floor. The package (`docs/NATIVE_PACKAGE.md`) does not yet +build or carry the recovery image, and putting it on the ESP is an attended +desk step. Hardware NOT_RUN: nothing here has run on the Spark. + +**Linux is not recovery.** A development rescue through Linux (the BootNext +return to Linux of aienos#273, a Linux USB stick, or a Linux shell writing the +disk) does not satisfy the final N7 gate. N7 needs this path (both slots +invalid, the recovery image, a slot rewritten, a reboot into it) on the +physical candidate inside an attended window. That run is NOT_RUN. + +**Tests.** + +- `make -C native/kernel slot-test` (`ck_slot_tool selftest`, 172 checks): the + recovery boot record (valid; tries, a slot reason, generation 0, a reserved + byte and a sequence without a control record each refused), the kernel + action for a recovery boot (nothing, also when healthy), and a recovery + update over a disk with both slots damaged and an exhausted candidate + (an ordinary update still refused, a wrong key refused with nothing + written, B rewritten and staged with fresh tries, A's bytes untouched, + the loader's next decision a candidate try of B). +- `scripts/qemu_ck_slot_loader_test.sh` row 7 (no recovery image on the ESP: + nothing starts), row 16 (both slots invalid, the recovery image starts, + `update` rewrites B, the next boot tries B and the kernel promotes it; the + recovery image is not used while B may boot) and row 17 (a recovery image + signed by the wrong key or with a flipped byte is refused, no kernel runs, + the slots partition is byte-identical; make refuses a recovery image with a + model). `native/kernel/GATES.md` rows 192, 215 and 216. diff --git a/native/.crumb b/native/.crumb index dbbce44e..db5cb2f0 100644 --- a/native/.crumb +++ b/native/.crumb @@ -50,74 +50,74 @@ "children": [ { "digest": "50aa2fbd6ca356b7703517bc7ecf439f7b6e0a6b935d0ca6ca4c6f7caf23ce34", - "last_commit": "4bd49ca", + "last_commit": "09e59a8", "name": "argus", "purpose": "Directory native/argus. Purpose not yet described by a human." }, { - "digest": "7bd231b92b38492fbb936e28caa803047edd9250800eac92fe56e09d38bf98cd", - "last_commit": "bfea771", + "digest": "0c3d85e774b83c7c8e97b2329e7b02d52b3307a1feab7134a06dd777649c6fd5", + "last_commit": "be246b3", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, { "digest": "750c519c466783ed6330a6a2c8fcb2153efdaadb9d65b38a6a85e8463d78b68d", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, { "digest": "0a3e98a5f36f528eb7ae6e1611d466343068780b9029621286b292ec1c796950", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "crypto", "purpose": "Directory native/crypto. Purpose not yet described by a human." }, { "digest": "99bc55e79695290b65ae08a38e5731e3ed976d79cdb3b6c9bc21c706b1ddab22", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "disk", "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "7a81b1b4b47154e15b9682b72d7fa49f309454f093aeb019061b8148550920ae", - "last_commit": "3037967", + "digest": "9b67f817c478155eaf81a0c8ace083fe233c3fa9dd3c33108fa029571fc58693", + "last_commit": "be246b3", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, { "digest": "a805df7e3af4b8a2ef345223523b54e7e47565204ec8f8e598f6dcaf53b07419", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "m5", "purpose": "Directory native/m5. Purpose not yet described by a human." }, { "digest": "ff5c88f5e9bf86e7ff836d24c048dd2d4efc94f6f19a21a0caf72ae9c402deaf", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "net", "purpose": "Directory native/net. Purpose not yet described by a human." }, { "digest": "135fd1edbe2388764de7dc755611b483b0c61adab858718c390b7745d141635c", - "last_commit": "4bd49ca", + "last_commit": "7db773a", "name": "sig", "purpose": "Directory native/sig. Purpose not yet described by a human." }, { "digest": "f75197d76b7daaeee8223cf25353251824cd4497400a439724a3a424e014a229", - "last_commit": "4bd49ca", + "last_commit": "e5e8f08", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:cec690f1b64aaba9f5f7ea41048a25c5b3cdf4834f672e977dc7eb47d34e4be2", - "compiled_at": "2026-10-17T01:49:45Z", + "children_root": "sha256:f3ea2865e4bd37ab031d684c55a43455d8e3a7663cb1b991ff5a15cb75881a7e", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:64cc87f8507bb580e1788d335c5a76039f3e3a016552595526718448ab5aa098", + "digest": "sha256:74e9389336c778d22cbcee7ddbe725beea3afb71a1fe45b6b37d9ca59375d0d8", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 606, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:1d9fe182bcaa94de3564ee02f9e8c63bddcea00fcea22feed112ffe6e01a1b78" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:bf24f3163f12ab6fe7bf8079aeb27bb8d6ed45875186f61cae61abe0bae65af3" }, "owner_lane": "", "provenance": { diff --git a/native/boot/.crumb b/native/boot/.crumb index cc53e7fd..7fd53473 100644 --- a/native/boot/.crumb +++ b/native/boot/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-16T23:33:50Z", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:7bd231b92b38492fbb936e28caa803047edd9250800eac92fe56e09d38bf98cd", + "digest": "sha256:0c3d85e774b83c7c8e97b2329e7b02d52b3307a1feab7134a06dd777649c6fd5", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 26, - "generated_at_commit": "4bd49ca164761fe66c6eb339acabdbcc84e8a33c", - "source_tree": "sha256:57b9a2e0d628acd6b2c2d1dab4fc1a4eafec507bc2601b54d92f96fc46393666" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:1d367b610a8b0bfdad3d8aa50219d144c627da692e723e96c35047cf1b9f1bb1" }, "owner_lane": "", "provenance": { diff --git a/native/boot/README.md b/native/boot/README.md index 0f727735..f092637f 100644 --- a/native/boot/README.md +++ b/native/boot/README.md @@ -24,7 +24,10 @@ baked in at build time, applies `slot_select.c` (bounded candidate tries, generation floor, never a boot loop), commits a candidate try before the jump, and chain-loads the chosen image (this stub + kernel, unchanged and byte-identical) with `LoadImage` + `StartImage`. Both slots invalid: it prints -`slot_loader: recovery_required (...)` and returns to the firmware. It never +`slot_loader: recovery_required (...)`, then starts the known-good recovery +image from its own volume (`\EFI\AIENOS\RECOVERY.EFI` + `RECOVERY.MAN`, +verified with the same keys; P11 cut 4, `docs/NATIVE_IMAGE_SLOTS.md` section +10) or, when there is none or it is refused, returns to the firmware. It never touches UEFI variables, runtime services or `ExitBootServices`. Build: `make -C native/kernel slot-loader CK_SLOT_KEYS=<file>` (or the TEST-ONLY `CK_TEST_SLOT_KEY=1`, refused with `CK_HARDWARE_STAGING`); gate diff --git a/native/boot/slot_format.h b/native/boot/slot_format.h index ffdc81be..11d25a68 100644 --- a/native/boot/slot_format.h +++ b/native/boot/slot_format.h @@ -222,11 +222,20 @@ int ck_slot_abandon_candidate(struct ck_slot_control *c, const char **why); #define CK_SLOT_BOOT_VERSION 1u #define CK_SLOT_BOOT_BYTES 256u #define CK_SLOT_BOOT_SIGNED 224u /* digest = SHA-256 over bytes [0, 224) */ +/* P11 cut 4: boot_slot of the known-good recovery image (no slot may boot; + * reason CK_SLOT_R_RECOVERY_REQUIRED, tries 0). SLOTLDR.EFI loads it from its + * own boot volume, CK_SLOT_RECOVERY_IMAGE + CK_SLOT_RECOVERY_MANIFEST (one + * signed manifest unit, the same format and keys as a slot), and starts it only + * when both slots are unusable. docs/NATIVE_IMAGE_SLOTS.md section 10. */ +#define CK_SLOT_BOOT_RECOVERY 2u +#define CK_SLOT_RECOVERY_IMAGE "\\EFI\\AIENOS\\RECOVERY.EFI" +#define CK_SLOT_RECOVERY_MANIFEST "\\EFI\\AIENOS\\RECOVERY.MAN" +#define CK_SLOT_RECOVERY_MAX_BYTES (64ull << 20) struct ck_slot_boot_record { uint8_t magic[8]; /* 0 "AIENSLB1" */ uint32_t version; /* 8 1 */ - uint8_t boot_slot; /* 12 0 or 1: the slot whose image the loader started */ + uint8_t boot_slot; /* 12 0 or 1: the slot whose image the loader started; 2: recovery image */ uint8_t reason; /* 13 enum ck_slot_reason of the loader's decision */ uint8_t tries; /* 14 tries_remaining on disk when the image started */ uint8_t control_present; /* 15 1 = a valid control record was read (and committed when asked) */ diff --git a/native/boot/slot_loader.c b/native/boot/slot_loader.c index af13b5f4..6d8436ee 100644 --- a/native/boot/slot_loader.c +++ b/native/boot/slot_loader.c @@ -39,8 +39,13 @@ * DevicePath = this loader's own LoadedImageDevicePath, so the chained * stub's LoadedImage->DeviceHandle is still the boot volume and its * MODEL.MAP still loads) and StartImage. - * Recovery required: print why for both slots and return EFI_LOAD_ERROR to - * the firmware, which goes on to its next boot option. + * Recovery required: print why for both slots. (cut 4) When the slots were + * read but neither may boot, load the known-good recovery image from this + * loader's own volume (CK_SLOT_RECOVERY_MANIFEST + CK_SLOT_RECOVERY_IMAGE), + * verify it with the same keys, hand over a boot record with slot + * CK_SLOT_BOOT_RECOVERY and start it. No recovery image, a refused one, or an + * earlier refusal (no keys, no or two slots partitions, too small): return + * EFI_LOAD_ERROR to the firmware, which goes on to its next boot option. * * Never: UEFI variables, runtime services, ExitBootServices (the chained * stub leaves boot services itself), any disk write other than the control @@ -343,13 +348,18 @@ static const char *unusable_why(const struct ck_slot_eval *e, const struct ck_sl return "not selectable"; } +static char slot_label(unsigned b) +{ + return b < CK_SLOT_COUNT ? (char)('A' + b) : 'R'; +} + /* ---- boot record for the kernel (P11 cut 2b, slot_format.h) ------------------ */ /* One page of the OS-loader memory type CK_SLOT_BOOT_MEMTYPE: it survives * ExitBootServices and shows up in the final memory map the unchanged stub * copies into the handoff. If the allocation fails the image still boots; * its kernel then finds no record and writes nothing (no promotion, no * abandon): fail closed, the tries counter still bounds a candidate. */ -static void hand_over(unsigned b, const struct ck_slot_decision *d, const struct ck_slot_control *ctl, +static void hand_over(unsigned b, enum ck_slot_reason reason, unsigned tries, const struct ck_slot_control *ctl, uint64_t seq, const struct ck_slot_manifest *m) { uint64_t pa = 0; @@ -366,8 +376,8 @@ static void hand_over(unsigned b, const struct ck_slot_decision *d, const struct memcpy(br->magic, CK_SLOT_BOOT_MAGIC, 8); br->version = CK_SLOT_BOOT_VERSION; br->boot_slot = (uint8_t)b; - br->reason = (uint8_t)d->reason; - br->tries = d->loader_write ? d->ctl_out.tries_remaining : (ctl ? ctl->tries_remaining : 0); + br->reason = (uint8_t)reason; + br->tries = (uint8_t)tries; br->control_present = ctl ? 1 : 0; br->control_seq = ctl ? seq : 0; br->generation = m->generation; @@ -376,13 +386,125 @@ static void hand_over(unsigned b, const struct ck_slot_decision *d, const struct for (unsigned i = 0; i < sizeof br->loader_commit && c[i]; i++) br->loader_commit[i] = (uint8_t)c[i]; ck_slot_boot_seal(br); - say("slot_loader: boot record handed over type=0x%x slot=%c reason=%s seq=%llu\n", CK_SLOT_BOOT_MEMTYPE, 'A' + b, - ck_slot_reason_text(d->reason), (unsigned long long)br->control_seq); + say("slot_loader: boot record handed over type=0x%x slot=%c reason=%s seq=%llu\n", CK_SLOT_BOOT_MEMTYPE, slot_label(b), + ck_slot_reason_text(reason), (unsigned long long)br->control_seq); } -EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) +/* ---- the known-good recovery image (P11 cut 4, slot_format.h) ---------------- */ +/* Only when ck_slot_select finds no bootable slot. From this loader's own boot + * volume: CK_SLOT_RECOVERY_MANIFEST (one manifest unit, the slot format) and + * CK_SLOT_RECOVERY_IMAGE, verified with the same keys as a slot (signature + * over the manifest, SHA-512 of the image). No generation floor applies to it: + * it never becomes a slot's image and promotes nothing (docs/NATIVE_IMAGE_SLOTS.md + * section 10). 0 = verified bytes in *img_out (pages in *pages_out). */ +static struct ck_slot_manifest rec_man __attribute__((aligned(4096))); + +static EFI_STATUS read_file(EFI_FILE_PROTOCOL *root, CHAR16 *path, void *buf, uint64_t want, uint64_t *got) +{ + EFI_FILE_PROTOCOL *f = 0; + EFI_STATUS s = root->Open(root, &f, path, EFI_FILE_MODE_READ, 0); + *got = 0; + if (s != EFI_SUCCESS || !f) + return s != EFI_SUCCESS ? s : EFI_LOAD_ERROR; + while (*got < want) { + uint64_t n = want - *got; + s = f->Read(f, &n, (uint8_t *)buf + *got); + if (s != EFI_SUCCESS || n == 0) + break; + *got += n; + } + f->Close(f); + return s; +} + +static int load_recovery(EFI_HANDLE image, uint8_t **img_out, uint64_t *pages_out, const char **why) +{ + static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT; + static const EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT; + static CHAR16 man_path[sizeof CK_SLOT_RECOVERY_MANIFEST], img_path[sizeof CK_SLOT_RECOVERY_IMAGE]; + EFI_LOADED_IMAGE_PROTOCOL *li = 0; + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0; + EFI_FILE_PROTOCOL *root = 0; + struct ck_slot_eval ev; + uint64_t got = 0, pages, base = 0; + for (unsigned i = 0; i < sizeof man_path / sizeof man_path[0]; i++) + man_path[i] = (CHAR16)CK_SLOT_RECOVERY_MANIFEST[i]; + for (unsigned i = 0; i < sizeof img_path / sizeof img_path[0]; i++) + img_path[i] = (CHAR16)CK_SLOT_RECOVERY_IMAGE[i]; + *img_out = 0; + *pages_out = 0; + if (bs->HandleProtocol(image, &li_guid, (void **)&li) != EFI_SUCCESS || !li || !li->DeviceHandle || + bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs) != EFI_SUCCESS || !fs || + fs->OpenVolume(fs, &root) != EFI_SUCCESS || !root) { + *why = "no recovery image: the loader's boot volume has no file system"; + return -1; + } + memset(&rec_man, 0, sizeof rec_man); + if (read_file(root, man_path, &rec_man, sizeof rec_man, &got) != EFI_SUCCESS || got == 0) { + root->Close(root); + *why = "no recovery image: " CK_SLOT_RECOVERY_MANIFEST " absent"; + return -1; + } + int bad = 1; + if (got != sizeof rec_man) + *why = "recovery manifest is not one unit"; + else if (ck_slot_manifest_check(&rec_man, why) != 0) { + } else if (rec_man.image_len > CK_SLOT_RECOVERY_MAX_BYTES) + *why = "recovery image above the size bound"; + else + bad = 0; + if (bad) { + root->Close(root); + return -1; + } + pages = (rec_man.image_len + PAGE - 1) / PAGE; + if (bs->AllocatePages(AllocateAnyPages, EfiLoaderData, pages, &base) != EFI_SUCCESS || !base) { + root->Close(root); + *why = "AllocatePages failed"; + return -1; + } + if (read_file(root, img_path, (void *)(uintptr_t)base, rec_man.image_len, &got) != EFI_SUCCESS || + got != rec_man.image_len) { + root->Close(root); + bs->FreePages(base, pages); + *why = got ? "recovery image shorter than its manifest" : "recovery image " CK_SLOT_RECOVERY_IMAGE " absent"; + return -1; + } + root->Close(root); + ck_slot_verify(&rec_man, (const uint8_t *)(uintptr_t)base, CK_SLOT_RECOVERY_MAX_BYTES, ck_slot_loader_keys, + ck_slot_loader_nkeys, &ev); + if (!ev.valid) { + bs->FreePages(base, pages); + *why = ev.why ? ev.why : "invalid"; + return -1; + } + *img_out = (uint8_t *)(uintptr_t)base; + *pages_out = pages; + return 0; +} + +/* LoadImage from the verified bytes and StartImage (step 5). */ +static EFI_STATUS start_verified(EFI_HANDLE image, unsigned b, uint8_t *img, uint64_t len, uint64_t pages) { static const EFI_GUID lidp_guid = LOADED_IMAGE_DEVICE_PATH_GUID_INIT; + void *dp = 0; + if (bs->HandleProtocol(image, &lidp_guid, &dp) != EFI_SUCCESS) + dp = 0; + EFI_HANDLE child = 0; + EFI_STATUS s = ((load_image_fn)bs->LoadImage)(0, image, dp, img, len, &child); + bs->FreePages((uint64_t)(uintptr_t)img, pages); + if (s != EFI_SUCCESS || !child) { + say("slot_loader: LoadImage of slot %c status=0x%llx: not booted\n", slot_label(b), (unsigned long long)s); + return EFI_LOAD_ERROR; + } + say("slot_loader: starting slot %c\n", slot_label(b)); + s = ((start_image_fn)bs->StartImage)(child, 0, 0); + say("slot_loader: slot %c returned status=0x%llx\n", slot_label(b), (unsigned long long)s); + return s; +} + +EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) +{ struct slots_part sp; struct ck_slot_eval ev[CK_SLOT_COUNT]; struct ck_slot_decision d; @@ -489,12 +611,23 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) } } if (d.boot_slot < 0) { + uint8_t *rimg = 0; + uint64_t rpages = 0; say("slot_loader: recovery_required (A: %s; B: %s)\n", unusable_why(&ev[0], ctl, 0), unusable_why(&ev[1], ctl, 1)); for (unsigned s = 0; s < CK_SLOT_COUNT; s++) if (img[s]) bs->FreePages((uint64_t)(uintptr_t)img[s], pages[s]); - return EFI_LOAD_ERROR; + /* P11 cut 4: the known-good recovery image on this loader's volume. */ + if (load_recovery(image, &rimg, &rpages, &why) != 0) { + say("slot_loader: recovery image refused (%s): not booted\n", why); + return EFI_LOAD_ERROR; + } + say("slot_loader: boot=R reason=%s gen=%llu len=%llu (recovery image, both slots unusable)\n", + ck_slot_reason_text(d.reason), (unsigned long long)rec_man.generation, + (unsigned long long)rec_man.image_len); + hand_over(CK_SLOT_BOOT_RECOVERY, CK_SLOT_R_RECOVERY_REQUIRED, 0, ctl, seq, &rec_man); + return start_verified(image, CK_SLOT_BOOT_RECOVERY, rimg, rec_man.image_len, rpages); } unsigned b = (unsigned)d.boot_slot; @@ -504,19 +637,6 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) if (img[b ^ 1u]) bs->FreePages((uint64_t)(uintptr_t)img[b ^ 1u], pages[b ^ 1u]); - hand_over(b, &d, ctl, seq, &man[b]); - void *dp = 0; - if (bs->HandleProtocol(image, &lidp_guid, &dp) != EFI_SUCCESS) - dp = 0; - EFI_HANDLE child = 0; - EFI_STATUS s = ((load_image_fn)bs->LoadImage)(0, image, dp, img[b], man[b].image_len, &child); - bs->FreePages((uint64_t)(uintptr_t)img[b], pages[b]); - if (s != EFI_SUCCESS || !child) { - say("slot_loader: LoadImage of slot %c status=0x%llx: not booted\n", 'A' + b, (unsigned long long)s); - return EFI_LOAD_ERROR; - } - say("slot_loader: starting slot %c\n", 'A' + b); - s = ((start_image_fn)bs->StartImage)(child, 0, 0); - say("slot_loader: slot %c returned status=0x%llx\n", 'A' + b, (unsigned long long)s); - return s; + hand_over(b, d.reason, tries, ctl, seq, &man[b]); + return start_verified(image, b, img[b], man[b].image_len, pages[b]); } diff --git a/native/boot/slot_select.c b/native/boot/slot_select.c index 978ec9c1..c8a28297 100644 --- a/native/boot/slot_select.c +++ b/native/boot/slot_select.c @@ -380,7 +380,21 @@ int ck_slot_boot_check(const struct ck_slot_boot_record *b, const char **why) sha256_hash((const uint8_t *)b, CK_SLOT_BOOT_SIGNED, d); if (!eq_bytes(d, b->digest, sizeof d)) w = "boot record digest mismatch"; - else if (b->boot_slot >= CK_SLOT_COUNT) + else if (b->boot_slot == CK_SLOT_BOOT_RECOVERY) { + /* P11 cut 4: the recovery image, started because no slot may boot. */ + if (b->reason != CK_SLOT_R_RECOVERY_REQUIRED) + w = "recovery boot record with a slot reason"; + else if (b->control_present > 1) + w = "boot record control flag out of range"; + else if (b->tries != 0) + w = "recovery boot record with tries"; + else if (b->generation == 0 || b->policy_generation > b->generation) + w = "boot record generation fields"; + else if (!all_zero(b->reserved, sizeof b->reserved)) + w = "boot record reserved bytes nonzero"; + else if (!b->control_present && b->control_seq != 0) + w = "boot record claims a sequence without a control record"; + } else if (b->boot_slot >= CK_SLOT_COUNT) w = "boot record slot out of range"; else if (b->reason <= CK_SLOT_R_NONE || b->reason >= CK_SLOT_R_RECOVERY_REQUIRED) w = "boot record reason out of range"; @@ -413,6 +427,11 @@ enum ck_slot_action ck_slot_kernel_action(const struct ck_slot_boot_record *b, c *why = w; return CK_SLOT_A_REFUSE; } + if (b->boot_slot == CK_SLOT_BOOT_RECOVERY) { + if (why) + *why = "recovery boot: no slot to promote or abandon"; + return CK_SLOT_A_NONE; + } s = b->boot_slot; if (!ctl || ck_slot_control_check(ctl, &w) != 0) { if (why) diff --git a/native/boot/slot_update.c b/native/boot/slot_update.c index 04752d78..66483037 100644 --- a/native/boot/slot_update.c +++ b/native/boot/slot_update.c @@ -56,9 +56,13 @@ int ck_slot_update(const struct ck_slot_update_io *io, struct ck_slot_update_res 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) + /* The recovery image (P11 cut 4) runs because no slot may boot, so it is + * not the active slot, and a stale candidate (always the non-active slot) + * is simply restaged with fresh tries by ck_slot_stage_candidate. */ + int recovery = io->booted_slot == CK_SLOT_BOOT_RECOVERY; + if (!recovery && 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) + if (!recovery && (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)) diff --git a/native/boot/slot_update.h b/native/boot/slot_update.h index 7077798d..5ecb1588 100644 --- a/native/boot/slot_update.h +++ b/native/boot/slot_update.h @@ -9,6 +9,9 @@ * 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. + * The recovery image (booted_slot CK_SLOT_BOOT_RECOVERY, P11 cut 4) is + * exempt from the last two: staging replaces a stale (failed or exhausted) + * candidate and it writes the slot that is not active. * 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 diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 57d1b019..3d431c41 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -46,68 +46,68 @@ "children": [ { "digest": "8f63182b423b274a3fa6dcf0a17811343f93bd1539e33479102ffa87935da1ca", - "last_commit": "4bd49ca", + "last_commit": "2529bc0", "name": "arch", "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, { "digest": "cd3be26829bc7f947521b1e5df144df2504ef25a3763ec0356861b7d17d5d901", - "last_commit": "4bd49ca", + "last_commit": "6ad9190", "name": "artifact", "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "c9035b8f32a9539fe8d1f93ed968b3b56dd3ba4c2630b32b6c286b1dbd19d908", - "last_commit": "4bd49ca", + "digest": "4fdea0b53dc8df033f72cd71b308e330a0ed680641e53a40788e303a143fb8ff", + "last_commit": "be246b3", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "ebdd66c0966fba664f36dc167836b98d26ffae97f9502b4409e9c22da687b473", - "last_commit": "4bd49ca", + "last_commit": "9404c9e", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "8198bb9a0fbd634281a251b012eca8874c97e469a45eec8b5a6bf06a7e828f9a", - "last_commit": "4bd49ca", + "last_commit": "0735543", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, { "digest": "8252fab960e4a3bb686f2e864b0d9a0571f43381d5006de2ff6c134c132ef092", - "last_commit": "4bd49ca", + "last_commit": "2bc7146", "name": "mm", "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "2a98333d826d94a7131119aaeb5a15d8bc543baa5531fa930e6f0afc1fccb83d", - "last_commit": "4bd49ca", + "digest": "951588129b3797951b2c138ed8568bc82b733b159aae85c265b4651a2450540e", + "last_commit": "be246b3", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { "digest": "0f3d28a645346382567a7a279a3e02a095c8e7f298967db175944c0a58ca9c5d", - "last_commit": "3037967", + "last_commit": "6ad9190", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { - "digest": "412f0a290f187935193ec94f1817c46dbe0edc07a5eb427e39977b65c1d00f9f", - "last_commit": "3037967", + "digest": "125036cd39b66a0135542aeceab95982eb1bc052f01e455c2b71746ad608ee48", + "last_commit": "be246b3", "name": "tools", "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:658a040f493b6ff9dc4a775b2b73a0c89ded592a235231016dd8594094530e0f", - "compiled_at": "2026-10-17T01:49:45Z", + "children_root": "sha256:8306e2ae45d0ad1cad4ba44228cdb4a59845d3eca745d1a3a137985a2872f2ce", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:7a81b1b4b47154e15b9682b72d7fa49f309454f093aeb019061b8148550920ae", + "digest": "sha256:9b67f817c478155eaf81a0c8ace083fe233c3fa9dd3c33108fa029571fc58693", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 421, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:cfa9d56bd0bae125be856e13a57758e007e620d1fc9501643728e676f53eaabe" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:da408bf54dbcce3f52ea520ae759f18445aecd66390adb7673888ed7ff42d5a6" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index b7e37370..c6ca6c37 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -910,7 +910,7 @@ Owner decision 2026-10-12 (Drake): slot loader approved, narrowly scoped to sign | 189 | wrong key: B signed by RFC 8032 TEST 2, not a trusted key: `signer_id names no trusted key`, boots A, no try consumed | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 190 | tampered image: one byte of B's image flipped, manifest intact: `image SHA-512 mismatch`, boots A, no try consumed | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 191 | interrupted update: the staging control record torn (2048 bytes of its body written, no commit header): the host tool and the loader both recover the previous record (`control seq=1 active=A candidate=- tries=0`), `boot=A reason=active`, B never tried; control: the whole record would have tried B | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | -| 192 | both slots invalid: `slot_loader: recovery_required (A: image SHA-512 mismatch; B: signer_id names no trusted key)`, the loader returns `EFI_LOAD_ERROR` to the firmware, no slot started, no kernel ran, the control record untouched; QEMU is stopped by the row timeout (60 s) because the firmware falls through to its next boot option | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 192 | both slots invalid: `slot_loader: recovery_required (A: image SHA-512 mismatch; B: signer_id names no trusted key)`; with no recovery image on its ESP (P11 cut 4) `slot_loader: recovery image refused (no recovery image: \EFI\AIENOS\RECOVERY.MAN absent): not booted`, the loader returns `EFI_LOAD_ERROR` to the firmware, no slot started, no kernel ran, the control record untouched; QEMU is stopped by the row timeout (60 s) because the firmware falls through to its next boot option | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 193 | generation floor: B validly signed at gen 1 while the floor is 2: `slot B valid gen=1`, `(below the generation floor)`, `boot=A reason=candidate_invalid_active gen=2 tries=3 floor=2`, no try consumed | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | | 194 | build (`make -C native/kernel slot-loader-check`, part of `make test`): SLOTLDR.EFI links from its own object list (checked: none of the kernel symbols `ck_kernel_entry`, `kmain`, `ck_boot_model_load`, `ck_printf`), its source calls no runtime services, UEFI variables, `ExitBootServices` or `SetWatchdogTimer`; the TEST-key build prints the TEST-ONLY note and an owner-key build (`CK_SLOT_KEYS`, fixture keys) does not; refused (each with its message): no key source, both key options, an unreadable keys file, and `CK_HARDWARE_STAGING=1 CK_TEST_SLOT_KEY=1` | SLOT_LOADER (host) | PASS (`SLOT_LOADER_BUILD: PASS`) | | 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) | @@ -920,6 +920,8 @@ Owner decision 2026-10-12 (Drake): slot loader approved, narrowly scoped to sign | 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) | +| 215 | P11 cut 4, recovery path: disk `aienos-slots-inbox`, control `active=0 candidate=none`, slot A's image flipped, B empty (both invalid), the inbox holds B's TEST-signed image (gen 2); the ESP holds the recovery image (`CK_RECOVERY=1` TEST-key service image, no model) as `\EFI\AIENOS\RECOVERY.EFI` + `RECOVERY.MAN` (gen 1). Boot 1: `slot_loader: recovery_required (A: image SHA-512 mismatch; B: ...)`, `slot_loader: boot=R reason=recovery_required gen=1`, boot record `slot=R`, `recovery: AIENOS native recovery image (P11 cut 4): no model`, `slot: booted the recovery image (both slots unusable)`, never `slot: promoted`; operator `update`: `(booted R, keys=1)`, `slot: update staged slot B as candidate generation=2`, control `seq=2 active=0 candidate=1 tries=3`, the damaged slot A's bytes unchanged. Boot 2 (recovery image still on the ESP): `boot=B reason=candidate_try gen=2`, no `boot=R`, `slot: promoted slot B to active generation=2 floor=2`, control `active=1 candidate=none`. `make slot-test` adds the recovery boot record checks and a recovery-mode update over an exhausted candidate | SLOT_LOADER | QEMU PASS (hardware NOT_RUN) | +| 216 | P11 cut 4, negative controls: both slots invalid and a recovery image signed by RFC 8032 TEST 2 (wrong key) or with a flipped image byte (wrong digest): `slot_loader: recovery image refused (signer_id names no trusted key): not booted` / `(image SHA-512 mismatch)`, no slot started, no kernel ran, the slots partition byte-identical; `make CK_SERVICE=1 CK_RECOVERY=1 CK_INFER_LIB=... full` is refused (the recovery image carries no model) | 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 20a92ad3..1b1b7285 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -659,6 +659,24 @@ ifneq ($(strip $(CK_HARDWARE_STAGING)),) $(error CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING) endif endif +# CK_RECOVERY=1 (native v1 plan P11 cut 4, gate N7): the known-good recovery +# image, a service image (CK_SERVICE=1) with no model and no inference unit +# that keeps Store unlock, status, evidence and the slot verb update. SLOTLDR.EFI +# starts it from \EFI\AIENOS\RECOVERY.EFI + RECOVERY.MAN on its own volume when +# neither slot may boot (docs/NATIVE_IMAGE_SLOTS.md section 10). Own OUT +# subdirectory (FULL_NAME suffix -recovery). Refused with CK_INFER_LIB. +CK_RECOVERY ?= +ifneq ($(strip $(CK_RECOVERY)),) +ifneq ($(strip $(CK_RECOVERY)),1) +$(error CK_RECOVERY must be empty or 1) +endif +ifneq ($(strip $(CK_SERVICE)),1) +$(error CK_RECOVERY needs CK_SERVICE=1 (the recovery image is a service image)) +endif +ifneq ($(strip $(CK_INFER_LIB)),) +$(error CK_RECOVERY (the recovery image carries no model) cannot be combined with CK_INFER_LIB) +endif +endif # TEST-ONLY Store crash hook (svc/store_crash.h; only # scripts/qemu_ck_store_crash_test.sh sets it): the Store stage reads a crash @@ -977,15 +995,18 @@ ifneq ($(strip $(CK_SERVICE)$(CK_PI5_LIFECYCLE)),) SVC_STAGE_BASE := $(STAGE_SRCS) # Own OUT subdirectory: service objects never mix with a diagnostic image. ifeq ($(strip $(CK_SERVICE_TEST_KEYS)),1) -FULL_NAME := full-service-testkeys +FULL_NAME := full-service-testkeys$(if $(strip $(CK_RECOVERY)),-recovery,) STAGE_DMA_FLAGS += -DCK_SERVICE=1 -DCK_SERVICE_TEST_KEYS=1 else -FULL_NAME := full-service$(if $(strip $(CK_HARDWARE_STAGING)),-hardware-staging,) +FULL_NAME := full-service$(if $(strip $(CK_HARDWARE_STAGING)),-hardware-staging,)$(if $(strip $(CK_RECOVERY)),-recovery,) STAGE_DMA_FLAGS += -DCK_SERVICE=1 endif STAGE_SRCS += $(KDIR)/core/lifecycle.c $(KDIR)/core/lifecycle_fsm.c $(KDIR)/core/line_rx.c # N3 (aienos#329): context assembly and the exact approval binding (pure C, host-tested by test_context). STAGE_SRCS += $(KDIR)/core/context.c $(KDIR)/core/context_proposal.c +ifeq ($(strip $(CK_RECOVERY)),1) +STAGE_DMA_FLAGS += -DCK_RECOVERY_IMAGE=1 +endif # P06 cut 3: the canonical Omega Cortex store over the sealed Store (svc/cortex_journal.c). STAGE_SRCS += $(STAGE_CORTEX_SRCS) # P05 cut 1: production continuity on the service lifecycle (svc/continuity_service.c): diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 11410454..376bd33a 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-15T02:48:19Z", + "compiled_at": "2026-10-16T02:16:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:c9035b8f32a9539fe8d1f93ed968b3b56dd3ba4c2630b32b6c286b1dbd19d908", + "digest": "sha256:4fdea0b53dc8df033f72cd71b308e330a0ed680641e53a40788e303a143fb8ff", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 69, - "generated_at_commit": "edd175183f30a31f4c7683e194dd6479dc8c7a6e", - "source_tree": "sha256:928d92ef1abd9076c57a25fb07becf22b43f0a46a7edd1998383f3dd6296e083" + "generated_at_commit": "02186cf8b8306383ee6eef40d0447c21d3ca20ab", + "source_tree": "sha256:4bf631efd7e490cdef6ccce1ae212bf4c3b771a22a3a092055d6178969373fc6" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c index 97269cde..a807c814 100644 --- a/native/kernel/core/lifecycle.c +++ b/native/kernel/core/lifecycle.c @@ -923,6 +923,10 @@ int ck_lifecycle_run(void) ck_line_rx_init(&g_gen_rx); #ifdef CK_SERVICE_TEST_KEYS ck_puts("lifecycle: TEST-ONLY service image (CK_SERVICE_TEST_KEYS=1: TEST Store keys; QEMU only, never a production boot)\n"); +#endif +#ifdef CK_RECOVERY_IMAGE + ck_puts("recovery: AIENOS native recovery image (P11 cut 4): no model, no inference; Store unlock, status, " + "evidence and the slot verb update only (docs/NATIVE_IMAGE_SLOTS.md section 10)\n"); #endif ck_lc_start(&g_lc, ck_store_service_ready()); ck_slot_service_start(slot_healthy()); diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index dd0aab6a..63c44cbd 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:d835471b1405ca482a41beb34ed339d21e11064a59138599ee5754d198926878", - "compiled_at": "2026-10-13T23:07:22Z", + "compiled_at": "2026-10-16T02:16:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:2a98333d826d94a7131119aaeb5a15d8bc543baa5531fa930e6f0afc1fccb83d", + "digest": "sha256:951588129b3797951b2c138ed8568bc82b733b159aae85c265b4651a2450540e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 48, - "generated_at_commit": "53de55c5dd3485948cd34734caf0f646e28a68c4", - "source_tree": "sha256:8ea0c11b9dedede3565f8e3d47b8192651e0790f8e3df92b172ce4a8dc936af1" + "generated_at_commit": "02186cf8b8306383ee6eef40d0447c21d3ca20ab", + "source_tree": "sha256:d0a4473ca0ead824b61d817158a483f541f7e3a74ac756c6b6d413b5b0c77db0" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/slot_service.c b/native/kernel/svc/slot_service.c index cd6916c3..7195fd95 100644 --- a/native/kernel/svc/slot_service.c +++ b/native/kernel/svc/slot_service.c @@ -62,6 +62,12 @@ void ck_slot_boot_capture(const struct ck_handoff *h) g_rec_state = ck_slot_boot_check(&g_rec, &g_rec_why) == 0 ? 1 : -1; } +/* A, B, or R for the recovery image (P11 cut 4). */ +static char slot_name(unsigned s) +{ + return s < CK_SLOT_COUNT ? (char)('A' + s) : 'R'; +} + static void say_refuse(const char *why) { ck_printf("slot: REFUSED %s: control record not written\n", why); @@ -158,7 +164,14 @@ void ck_slot_service_start(int healthy) say_refuse(g_rec_why ? g_rec_why : "boot record invalid"); return; } - ck_printf("slot: booted slot %c reason=%s generation=%llu tries=%u control_seq=%llu\n", 'A' + g_rec.boot_slot, + if (g_rec.boot_slot == CK_SLOT_BOOT_RECOVERY) { + g_done = 1; + ck_printf("slot: booted the recovery image (both slots unusable) generation=%llu control_seq=%llu: nothing " + "promoted or abandoned; the operator verb update rewrites the inactive slot\n", + (unsigned long long)g_rec.generation, (unsigned long long)g_rec.control_seq); + return; + } + ck_printf("slot: booted slot %c reason=%s generation=%llu tries=%u control_seq=%llu\n", slot_name(g_rec.boot_slot), ck_slot_reason_text((enum ck_slot_reason)g_rec.reason), (unsigned long long)g_rec.generation, (unsigned)g_rec.tries, (unsigned long long)g_rec.control_seq); evaluate(healthy); @@ -303,7 +316,7 @@ void ck_slot_service_update(const char *arg, size_t len) }; 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, + ck_printf("slot: update from the inbox into the inactive slot (booted %c, keys=%u)\n", slot_name(g_rec.boot_slot), UPD_NKEYS); int rc = ck_slot_update(&io, &res, &why); (void)ck_mm_frames_free(pa, pages); diff --git a/native/kernel/tools/.crumb b/native/kernel/tools/.crumb index 434135ec..f96610e7 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-17T01:49:45Z", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:412f0a290f187935193ec94f1817c46dbe0edc07a5eb427e39977b65c1d00f9f", + "digest": "sha256:125036cd39b66a0135542aeceab95982eb1bc052f01e455c2b71746ad608ee48", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 24, - "generated_at_commit": "30379678add587e388db4e11713d6bca8d159b08", - "source_tree": "sha256:811d4174e2479ef3a1cf5ed3f0ae69346da056fc12745e9f1b33afbe0b6f6dc5" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:678ace6040dfc234d361320ccf80cfa5f23c531a6746c3527adfcd937fbd13ec" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tools/ck_slot_tool.c b/native/kernel/tools/ck_slot_tool.c index 1d821f96..7445b3d4 100644 --- a/native/kernel/tools/ck_slot_tool.c +++ b/native/kernel/tools/ck_slot_tool.c @@ -752,6 +752,57 @@ static void selftest_update(void) neg[i].what); } } + + /* P11 cut 4: the recovery image updates when both slots are unusable. + * Fixture: B staged by an update, then its tries exhausted and both slot + * manifests damaged (dual-invalid). */ + { + struct memdisk md = {upd_slots, sizeof upd_slots, -1, 0}; + disk_dev dd; + ts_device td; + ts_region r; + struct ck_slot_control ex; + uint64_t seq; + struct ck_slot_eval ev[2]; + struct ck_slot_decision d; + upd_media(&m, upd_img, UPD_IMG_BYTES, inu); + rc = upd_run(&g, inu, -1, 0, &TEST1_PK, 1, &res, &why); + ex = *upd_control(); + ex.tries_remaining = 0; + md_bind(&dd, &td, &md); + if (rc != 0 || ts_open(&r, &td, 0) != TS_OK || ts_recover(&r, (uint8_t *)&bad, &seq, 0) != TS_OK || + ck_slot_control_commit(&r, &ex, &seq) != TS_OK) + check(0, "recovery update: rig"); + upd_slots[au * CK_SLOT_UNIT + 300] ^= 1; + upd_slots[bu * CK_SLOT_UNIT + 300] ^= 1; + check(!upd_slot_valid(0, 0) && !upd_slot_valid(1, 0), "recovery update: fixture has both slots invalid"); + ev[0] = (struct ck_slot_eval){0, 0, 0, "x"}; + ev[1] = (struct ck_slot_eval){0, 0, 0, "x"}; + ck_slot_select(upd_control(), ev, &d); + check(d.boot_slot < 0 && d.reason == CK_SLOT_R_RECOVERY_REQUIRED && !d.loader_write, + "recovery update: the loader finds no bootable slot (recovery required, no write)"); + memcpy(upd_ref, upd_slots, sizeof upd_slots); + check(upd_run(&g, inu, -1, 0, &TEST1_PK, 1, &res, &why) != 0 && !res.wrote && g.ms.writes == 0 && + strstr(why, "already staged"), + "recovery update: an ordinary (slot-booted) update is still refused, nothing written"); + bad = m; + sign_manifest(&bad, TEST2_SEED); + memcpy(upd_inbox, &bad, sizeof bad); + check(upd_run(&g, inu, -1, CK_SLOT_BOOT_RECOVERY, &TEST1_PK, 1, &res, &why) != 0 && !res.wrote && + g.ms.writes == 0 && memcmp(upd_slots, upd_ref, sizeof upd_slots) == 0, + "recovery update: wrong key refused, nothing written"); + memcpy(upd_inbox, &m, sizeof m); + rc = upd_run(&g, inu, -1, CK_SLOT_BOOT_RECOVERY, &TEST1_PK, 1, &res, &why); + c = upd_control(); + check(rc == 0 && res.target == 1 && c && c->active_slot == 0 && c->candidate_slot == 1 && + c->tries_remaining == CK_SLOT_MAX_TRIES && !(c->flags & CK_SLOT_CTL_FLAG_CANDIDATE_FAILED), + "recovery update: stale exhausted candidate replaced, B staged with fresh tries"); + check(upd_slot_valid(1, 0) && upd_same(au, cap), "recovery update: B verifies, slot A bytes untouched"); + 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, + "recovery update: the loader's next decision is a candidate try of B"); + } } static int selftest(void) @@ -1023,7 +1074,9 @@ static int selftest(void) "boot record: one changed byte without a reseal refused (digest)"); BM(bm.magic[0] ^= 1, "bad boot record magic", "bad magic refused"); BM(bm.version = 2, "unsupported boot record version", "version 2 refused"); - BM(bm.boot_slot = 2, "boot record slot out of range", "slot 2 refused"); + BM(bm.boot_slot = 3, "boot record slot out of range", "slot 3 refused"); + BM(bm.boot_slot = CK_SLOT_BOOT_RECOVERY, "recovery boot record with a slot reason", + "recovery slot with a slot reason refused"); BM(bm.reason = CK_SLOT_R_NONE, "boot record reason out of range", "reason none refused"); BM(bm.reason = CK_SLOT_R_RECOVERY_REQUIRED, "boot record reason out of range", "reason recovery_required refused"); BM(bm.control_present = 2, "boot record control flag out of range", "control flag 2 refused"); @@ -1037,6 +1090,42 @@ static int selftest(void) "no_control with a control record refused"); #undef BM + /* P11 cut 4: the recovery image's boot record (both slots unusable). */ + { + struct ck_slot_boot_record r = b, rm; + r.boot_slot = CK_SLOT_BOOT_RECOVERY; + r.reason = CK_SLOT_R_RECOVERY_REQUIRED; + r.tries = 0; + ck_slot_boot_seal(&r); + check(ck_slot_boot_check(&r, &why) == 0, "recovery boot record: sealed record is valid"); +#define RM(stmt, want, what) \ + do { \ + rm = r; \ + stmt; \ + ck_slot_boot_seal(&rm); \ + check(ck_slot_boot_check(&rm, &why) != 0 && strcmp(why, want) == 0, \ + "recovery boot record: " what); \ + } while (0) + RM(rm.tries = 1, "recovery boot record with tries", "tries refused"); + RM(rm.reason = CK_SLOT_R_ACTIVE, "recovery boot record with a slot reason", "reason active refused"); + RM(rm.generation = 0, "boot record generation fields", "generation 0 refused"); + RM(rm.reserved[0] = 1, "boot record reserved bytes nonzero", "reserved byte refused"); + RM((rm.control_present = 0, rm.control_seq = 7), "boot record claims a sequence without a control record", + "sequence without control refused"); +#undef RM + rm = r; + rm.control_present = 0; + rm.control_seq = 0; + ck_slot_boot_seal(&rm); + check(ck_slot_boot_check(&rm, &why) == 0, "recovery boot record: without a control record is valid"); + ck_slot_control_init(&c, 0); + a = ck_slot_kernel_action(&r, &c, &man, 1, &out, &why); + check(a == CK_SLOT_A_NONE && why && strcmp(why, "recovery boot: no slot to promote or abandon") == 0, + "kernel action: recovery boot promotes nothing even when healthy"); + a = ck_slot_kernel_action(&r, 0, 0, 0, &out, &why); + check(a == CK_SLOT_A_NONE, "kernel action: recovery boot without a control record does nothing"); + } + /* Candidate B on its try, control still names it. */ ck_slot_control_init(&c, 0); c.generation_floor = 1; diff --git a/scripts/.crumb b/scripts/.crumb index 2bac4872..1e87b9d9 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-17T01:30:44Z", + "compiled_at": "2026-10-18T12:30:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:9adb69cc749bc97fa48e268f0397b900da83c84191ab6a9373358e8b9ab57922", + "digest": "sha256:dc53d5532581d1c4e53f3ea7426c322e8c06e4b65ca4c5a624a8447c1dedc97b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 119, - "generated_at_commit": "44cf2dd5925747aebf105136d31c8db58d8851a4", - "source_tree": "sha256:8ae58db519604c153d7140c6898139a24d1664716d187229aa125e147d370af1" + "generated_at_commit": "32b8edb82f70b8ee8848d7e8a8040572d8d6ef59", + "source_tree": "sha256:ef765e852a9c61a6f3aedd106065920ad42efabd08aada13eb05cf884476b48e" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_slot_loader_test.sh b/scripts/qemu_ck_slot_loader_test.sh index d7669eab..271fd5fc 100755 --- a/scripts/qemu_ck_slot_loader_test.sh +++ b/scripts/qemu_ck_slot_loader_test.sh @@ -25,8 +25,9 @@ # 5 B image flipped, manifest intact: "image SHA-512 mismatch", boots A. # 6 torn control write (the staging record's body half written, no commit # header): the previous record is recovered, boots A (active), B not tried. -# 7 both slots invalid: "recovery_required (A: ...; B: ...)", the loader returns -# to the firmware, no kernel runs (QEMU is stopped by the row timeout). +# 7 both slots invalid: "recovery_required (A: ...; B: ...)", no recovery image on +# this ESP (cut 4), the loader returns to the firmware, no kernel runs (QEMU is +# stopped by the row timeout). # 8 B validly signed but below the generation floor: refused, boots A. # P11 cut 2b (the service kernel promotes / abandons; svc/slot_service.c), one NVMe # disk with the Store and slots partitions, "shutdown" sent once the lifecycle is ready: @@ -47,6 +48,12 @@ # partition byte-identical. # 15 update refused while the Store is LOCKED; a TEST-key updater image is refused # by make under CK_HARDWARE_STAGING. +# P11 cut 4 (the known-good recovery image, CK_RECOVERY=1, \EFI\AIENOS\RECOVERY.EFI + .MAN): +# 16 both slots invalid, recovery image on the ESP: the loader verifies and starts it +# (boot record slot R), it promotes nothing, its `update` rewrites B from the inbox +# and stages it; next boot tries B (recovery image not used) and the kernel promotes B. +# 17 recovery image signed by the wrong key / with a flipped image byte: refused, no +# kernel runs, slots partition byte-identical; make refuses CK_RECOVERY with a model. # # Output: one PASS/FAIL line per check, then # AIENOS_CK_SLOT_LOADER: PASS|FAIL|NOT_RUN [(...)] @@ -74,6 +81,8 @@ mk OUT="${out}/payload-b" AIENOS_COMMIT="slotB-${commit}" mk OUT="${out}/svc-a" AIENOS_COMMIT="slotA-${commit}" CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 full mk OUT="${out}/svc-b" AIENOS_COMMIT="slotB-${commit}" CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 full mk OUT="${out}/svc-l" AIENOS_COMMIT="slotL-${commit}" CK_SERVICE=1 full +# P11 cut 4 (rows 16-17): the known-good recovery image (no model; TEST Store keys in QEMU). +mk OUT="${out}/svc-r" AIENOS_COMMIT="slotR-${commit}" CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 CK_RECOVERY=1 full tool="${out}/host/ck_slot_tool" gpt="${out}/host/ck_gpt_image" img_a="${out}/payload-a/BOOTAA64.EFI" @@ -81,6 +90,7 @@ img_b="${out}/payload-b/BOOTAA64.EFI" svc_a="${out}/svc-a/full-service-testkeys/BOOTAA64.EFI" svc_b="${out}/svc-b/full-service-testkeys/BOOTAA64.EFI" svc_l="${out}/svc-l/full-service/BOOTAA64.EFI" +svc_r="${out}/svc-r/full-service-testkeys-recovery/BOOTAA64.EFI" . "${repo_root}/scripts/lib_gate_hold.sh" if ! gh_quiet_check || ! gh_lock_take qemu_ck_slot_loader_test "${AIENOS_GATE_MINUTES:-60}"; then @@ -303,6 +313,7 @@ has 7 "recovery required, both reasons" "slot_loader: recovery_required (A: imag lacks 7 "no slot started" "slot_loader: starting slot" lacks 7 "no kernel ran" "kernel: alive" lacks 7 "no payload ran" "aienos_commit: slot" +has 7 "no recovery image on this ESP: not booted" "slot_loader: recovery image refused (no recovery image: \\EFI\\AIENOS\\RECOVERY.MAN absent): not booted" ctl_is 7 "control untouched" "seq=1 control: active=0 candidate=none tries=0" row_end 7 @@ -352,6 +363,7 @@ swait() { # raw-file regex count pid: until count answers are on the serial (or } skilled=0 # 1 when sboot itself killed QEMU (the power cut of row 13) +srec="" # manifest,image of a recovery image sboot puts on the ESP (P11 cut 4) 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 @@ -360,6 +372,10 @@ sboot() { # name timeout-seconds [efi]: ESP with SLOTLDR (or efi), NVMe ${disk}; mkdir -p "${w}/esp/EFI/BOOT" "${w}/esp/EFI/AIENOS" touch "${w}/esp/EFI/AIENOS/BOOTREPORT.TXT" cp "${efi}" "${w}/esp/EFI/BOOT/BOOTAA64.EFI" + if [[ -n "${srec}" ]]; then # P11 cut 4: recovery image on the ESP (manifest,image) + cp "${srec%%,*}" "${w}/esp/EFI/AIENOS/RECOVERY.MAN" + cp "${srec#*,}" "${w}/esp/EFI/AIENOS/RECOVERY.EFI" + fi cp "${vars_fd}" "${w}/vars.fd" mkfifo "${w}/ser.in" "${w}/ser.out" : >"${w}/serial.raw" @@ -379,7 +395,7 @@ sboot() { # name timeout-seconds [efi]: ESP with SLOTLDR (or efi), NVMe ${disk}; dpid=$! exec 4>"${w}/ser.in" while kill -0 "${qpid}" 2>/dev/null; do - if tr -d '\r' <"${w}/serial.raw" | grep -qE '^lifecycle: ready state=|^report_kind: (panic|fault|final)'; then break; fi + if tr -d '\r' <"${w}/serial.raw" | grep -qE '^lifecycle: ready state=|^report_kind: (panic|fault|final)|^slot_loader: recovery image refused'; then break; fi sleep 1 done if tr -d '\r' <"${w}/serial.raw" | grep -q '^lifecycle: ready state='; then @@ -398,6 +414,9 @@ sboot() { # name timeout-seconds [efi]: ESP with SLOTLDR (or efi), NVMe ${disk}; else printf 'shutdown\r' >&4 || true fi + elif tr -d '\r' <"${w}/serial.raw" | grep -q '^slot_loader: recovery image refused'; then + sleep 3 # the loader returned to the firmware: no image of ours runs after it + kill "${qpid}" 2>/dev/null || true fi set +e wait "${qpid}" @@ -585,10 +604,69 @@ else fi row_end 15 +# ==== P11 cut 4: the known-good native recovery image =============================== +# SLOTLDR.EFI starts \EFI\AIENOS\RECOVERY.EFI (manifest RECOVERY.MAN, same format and +# keys as a slot) from its own ESP only when neither slot may boot. The recovery +# image (CK_RECOVERY=1 service image: no model) rewrites the inactive slot from the +# inbox with the same updater; the next boot tries the rewritten slot and promotes it. +mm SR1 "${svc_r}" 1 +mm SR1k2 "${svc_r}" 1 0 --test2 +cp "${svc_r}" "${work}/svc_r_bad" +flip "${work}/svc_r_bad" 4242 + +# ---- row 16: both slots invalid -> recovery image -> update B -> reboot -> B promoted ---- +slots --a "${SA1}" --control "${work}/c_init_a" +flip "${disk}" $(( $(geo_val a_off) + 4096 + 1000 )) +inbox "${SB2}" +a_sum="$(slot_sum A)" +srec="${work}/SR1,${svc_r}" +sops=("update") +sboot r16-recovery "${svc_to}" +sops=() +has 16 "both slots unusable" "slot_loader: recovery_required (A: image SHA-512 mismatch; B: " +has 16 "loader starts the verified recovery image" "slot_loader: boot=R reason=recovery_required gen=1" +has 16 "boot record names the recovery image" "slot_loader: boot record handed over type=0x80000a1e slot=R reason=recovery_required" +has 16 "recovery image banner (no model)" "recovery: AIENOS native recovery image (P11 cut 4): no model" +has 16 "kernel knows it is the recovery boot" "slot: booted the recovery image (both slots unusable)" +lacks 16 "recovery promotes nothing" "slot: promoted" +has 16 "update from the recovery image" "slot: update from the inbox into the inactive slot (booted R, keys=1)" +has 16 "B rewritten and staged" "slot: update staged slot B as candidate generation=2" +ran 16 R +ctl_is 16 "control: B candidate with 3 tries, A still named active" "seq=2 control: active=0 candidate=1 tries=3" +if [[ "$(slot_sum A)" == "${a_sum}" ]]; then ok "row 16: damaged slot A bytes untouched by the recovery update"; else bad "row 16: slot A bytes changed"; fi +sboot r16-try "${svc_to}" +has 16 "loader tries the rewritten B" "slot_loader: boot=B reason=candidate_try gen=2" +lacks 16 "recovery image not used while a slot may boot" "slot_loader: boot=R" +has 16 "kernel promoted the rewritten B" "slot: promoted slot B to active generation=2 floor=2" +ran 16 B +ctl_is 16 "control: B active" "control: active=1 candidate=none tries=0 flags=0x00 floor=2 last_good=2" +row_end 16 + +# ---- row 17: negative controls: recovery image wrong key / wrong digest -> not booted ---- +for k in key digest; do + slots --a "${SA1}" --control "${work}/c_init_a" + flip "${disk}" $(( $(geo_val a_off) + 4096 + 1000 )) + p_sum="$(part_sum)" + if [[ "${k}" == key ]]; then srec="${work}/SR1k2,${svc_r}"; want="signer_id names no trusted key"; else srec="${work}/SR1,${work}/svc_r_bad"; want="image SHA-512 mismatch"; fi + sboot "r17-${k}" "${AIENOS_SLOT_RECOVERY_TIMEOUT:-60}" + has 17 "wrong ${k}: recovery image refused" "slot_loader: recovery image refused (${want}): not booted" + lacks 17 "wrong ${k}: nothing started" "slot_loader: starting slot" + lacks 17 "wrong ${k}: no kernel ran" "kernel: alive" + if [[ "$(part_sum)" == "${p_sum}" ]]; then ok "row 17: wrong ${k}: slots partition byte-identical"; else bad "row 17: wrong ${k}: slots partition changed"; fi +done +srec="" +mk_out="$(make -n -C native/kernel CROSS="${cross}" OUT="${work}/rc" CK_SERVICE=1 CK_RECOVERY=1 CK_INFER_LIB=/nonexistent full 2>&1 || true)" +if grep -qF -- "CK_RECOVERY (the recovery image carries no model) cannot be combined with CK_INFER_LIB" <<<"${mk_out}"; then + ok "row 17: a recovery image with a model is refused (make)" +else + bad "row 17: make accepted CK_RECOVERY with CK_INFER_LIB" +fi +row_end 17 + 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 (15 rows, ${boots} boots, ${checks} checks; QEMU, not hardware)" + echo "${tag}: PASS (17 rows, ${boots} boots, ${checks} checks; QEMU, not hardware)" else echo "${tag}: FAIL (${fails} of ${checks} checks)" exit 1