aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#209 (code only)
Apply it
git checkout -b patch-e9719927191b150a 7e15a5754dee5b865550e99de8376c5fa9585d47 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/e9719927191b150a.patch | git am
Provenance
github-import 1
aien-dev/aienux
209
1dc5833c4407396a793908c7ab018ec9a3bdafda
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":209,"url":"https://github.com/aien-dev/aienux/pull/209","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:35:57Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/evidence/ck_gates_1a369b636d4cd846dbc98b5c2c0789a78e789193fcbd23412b92699dfb6cea44.json b/evidence/ck_gates_1a369b636d4cd846dbc98b5c2c0789a78e789193fcbd23412b92699dfb6cea44.json new file mode 100644 index 00000000..b0ad4bd6 --- /dev/null +++ b/evidence/ck_gates_1a369b636d4cd846dbc98b5c2c0789a78e789193fcbd23412b92699dfb6cea44.json @@ -0,0 +1,42 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-01T14:01:49Z", + "finished_utc": "2026-10-01T14:04:06Z", + "commit": "29e9fe46b42fd912a28442c4695eb3a367bed80a", + "commit_subject": "ck: load P2 artifacts from the NVMe boot disk Store; fw_cfg only as a TEST build", + "tree_clean_before": true, + "tree_clean_after": true, + "quiet_flag": "/home/drakestapleton/workspace/.spark-quiet", + "quiet_flag_present_at_start": false, + "host": {"hostname": "spark-b87b", "arch": "aarch64", "kernel": "7.0.0-1019-nvidia", "cpu_model": "Cortex-A725;Cortex-X925"}, + "qemu": {"binary": "/usr/bin/qemu-system-aarch64", "version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)"}, + "firmware": {"aavmf_code": "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd", "aavmf_code_sha256": "4a4cb7f6d8106bb2a7dd8c763fab14b1810152136fc4304e5b728f0043e84f12", "aavmf_vars": "/usr/share/AAVMF/AAVMF_VARS.fd", "aavmf_vars_sha256": "b3b855c5a80310168051164986855692d1bdb06e67619856177965cd87c6774f"}, + "nvme_dma_mode": "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately", + "children": [ + {"name": "boot", "script": "scripts/qemu_ck_boot_test.sh", "present": true, "script_sha256": "64bcb2d1285485b989dc8309ce0b024ea799d3bce13cff22f62924d3ae4b7cce", "exit": "0", "seconds": 7, "log_sha256": "7acd76e89357aa19d8eb01d97ff452b604446f151518b4f6cf25837c98226d44", "log_lines": 22}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "b005ed29619d1113536a0686cedafe84fcea95fa30321307ec1d1335ea231925", "exit": "0", "seconds": 87, "log_sha256": "1d5e2c63c5b6eb2247ebb79a23874285c3acc4d606065c03a10a93d9aaebd609", "log_lines": 533}, + {"name": "net", "script": "scripts/qemu_ck_net_test.sh", "present": true, "script_sha256": "ff5bdf5246950bc9e825c76ecc541f39460b88c181a948225a7a393f25a71b25", "exit": "0", "seconds": 12, "log_sha256": "9545fcebfdafa9cbd0fcdd73ce148623315de66c09d57c9210dc8db818ea6cf8", "log_lines": 59}, + {"name": "artifact", "script": "scripts/qemu_ck_artifact_test.sh", "present": true, "script_sha256": "5e2b6d1cdaef908ac3711b88963435c757b7321876278fe3697bdbd450adbd69", "exit": "0", "seconds": 31, "log_sha256": "640f4fa692368792a5a1ffb3e90abc6774b21b131cac742d5bebb9a3109e6946", "log_lines": 215} + ], + "gates": [ + {"id": "M1", "marker": "AIENOS_CK_M1", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M3", "marker": "AIENOS_CK_M3", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "SMMU", "marker": "AIENOS_CK_SMMU", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "NVME_SHUTDOWN", "marker": "AIENOS_CK_NVME_SHUTDOWN", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "P2_ARTIFACT", "marker": "AIENOS_CK_P2_ARTIFACT", "source": "artifact", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M0_ROLLBACK", "marker": "AIENOS_CK_M0_ROLLBACK", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md)"}, + {"id": "M4_NVME", "marker": "AIENOS_CK_M4_NVME", "source": "store", "verdict": "PASS", "reason": "SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"}, + {"id": "M4_STORE", "marker": "AIENOS_CK_M4_STORE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_STORE_CRASH", "marker": "AIENOS_CK_M4_STORE_CRASH", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no C Store crash/kill campaign (4 KiB or 512 B) and no slot reuse (C Store is append-only)"}, + {"id": "M4_CONTINUITY", "marker": "AIENOS_CK_M4_CONTINUITY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no continuity core (ADR 0016 agent identity and memory) in the C kernel"}, + {"id": "M4_RECOVERY", "marker": "AIENOS_CK_M4_RECOVERY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no Recovery Core (ADR 0006) in the C kernel"}, + {"id": "ARGUS1_REVOKE", "marker": "AIENOS_CK_ARGUS1_REVOKE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "KEYBOARD", "marker": "AIENOS_CK_KEYBOARD", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel"}, + {"id": "NET", "marker": "AIENOS_CK_NET", "source": "net", "verdict": "PASS", "reason": "QEMU user networking (slirp): attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A; QEMU does not route virtio-net DMA through the SMMU (no ACCESS_PLATFORM), so confinement is not proven for it"} + ], + "counts": {"total": 14, "pass": 9, "fail": 0, "not_run": 5}, + "verdict": "NOT_ALL_GATES_PASS", + "parity": "native/kernel/GATES.md", + "physical": "NOT_RUN", + "physical_statement": "QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." +} diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 9fba60b2..995ed96e 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -129,19 +129,41 @@ signature, admission and receipt code in `native/kernel/artifact/` and the host tool `native/kernel/tools/ck_artifact_tool.c` (C ports of aienos-artifact, admission.rs, receipt.rs and aienos-artifact-tool; the tool's pack, sign, negative corpus and expected.txt are byte-identical to the Rust -tool's). Two boots as in the Rust gate: `make CK_SEED0B_TEST_ANCHOR=1` -(trusts the RFC 8032 TEST 1 public key only, labelled TEST ONLY) and the -ordinary build (no anchors). Same QEMU command line plus one `-fw_cfg` file. +tool's). Three boots of the full image (`make full`, with the device, +security and Store stages): `make full CK_SEED0B_TEST_ANCHOR=1` (trusts the +RFC 8032 TEST 1 public key only, labelled TEST ONLY), the ordinary build (no +anchors), and the TEST ONLY build again on a copy of the boot disk with one +byte of a sealed artifact flipped. Same QEMU command line plus +`iommu=smmuv3` and the NVMe boot disk; no `-fw_cfg`. How the C gate differs from the Rust gate (all apply to rows 33-41): -- **Candidate source**: one QEMU fw_cfg file `opt/aienos/artifacts` (a - bundle: `AIENBND\0`, count, then name/length/bytes per candidate) instead of - `\EFI\AIENOS\ARTIFACTS\*.AIEN` read by the UEFI loader. The C boot stub - (native/boot) reads no files. The kernel finds the fw_cfg window from the - ACPI DSDT (`QEMU0002` device, Memory32Fixed), nothing hard-coded. The - 640 KiB per-file limit and the input-frame shortage path (FirmwareRead) - are applied by the kernel at the same stage (`received`). +- **Candidate source**: the sealed C Store on the NVMe boot disk instead of + `\EFI\AIENOS\ARTIFACTS\*.AIEN` read by the UEFI loader (the C boot stub, + native/boot, reads no files). At image build time the host tool + `native/kernel/tools/ck_store_image.c` (`make store-image`) writes each + signed artifact into the Store as ordinary sealed objects (chunks of kind + 0x0A02 plus one index of kind 0x0A01, format in + `native/kernel/svc/artifact_store.h`); no second disk format. At boot the + store stage reads them over the SMMU-confined NVMe driver before the NVMe + DMA revoke and prints `artifact_disk: NAME bytes=N sha256=H` per artifact; + the gate requires each H to equal the host's SHA-256 of the file, the line + `artifact_source: nvme_store generation=G candidates=N`, and the order disk + read < `dma_gate: nvme bus master revoked` < loader. The Store's keys are + TEST keys, so the bytes stay untrusted: format, signature, digest and + admission checks run unchanged on them. The 640 KiB per-file limit and a + missing artifact (FirmwareRead) are applied at the same stage + (`received`). Hostile disk cases: `D01-DISK-MISSING.AIEN` is named in the + disk index with no bytes (refused at `received`, FirmwareRead, receipt + checked); index entries and chunks carry the artifact's SHA-256, so chunks + of different writes are never spliced (host test); boot 3 flips one byte of P26SEED's sealed envelope, and the + Store is refused at open (`store: REFUSED`, `artifact_disk: none (Store + refused: ..)`, `artifact_source: none (..)`, zero candidates, nothing + admitted). The old QEMU fw_cfg side channel survives only as a TEST build + (`make CK_TEST_FWCFG_ARTIFACTS=1`, own output directory, prints + `artifact_source_mode: TEST-ONLY ...` and never counts toward a PASS); the + Makefile and the gate check that no default image contains its strings or + symbols. - **Build and tools**: make + gcc and the in-tree C tool, not cargo and the Rust tool; the probe programs are the same committed fixtures packed by the same `pack.sh`. No Machine 1 captured-log mode (stays with the Rust gate). @@ -163,12 +185,12 @@ How the C gate differs from the Rust gate (all apply to rows 33-41): | # | Rust check (pattern) | CK gate | Status | | --- | --- | --- | --- | | 33 | common: kernel alive + M3 threads/el0/ipc proofs unchanged | P2_ARTIFACT | IDENTICAL | -| 34 | firmware read all candidates (`^artifact_candidates: N$`), report not truncated, final report | P2_ARTIFACT | DIFFERS (the kernel reads the fw_cfg bundle, not firmware files; same line and count; also fails on `artifact_bundle: malformed`) | -| 35 | frames reclaimed (`artifact_frames_free_before` == `_after`) | P2_ARTIFACT | IDENTICAL (absolute counts differ) | +| 34 | firmware read all candidates (`^artifact_candidates: N$`), report not truncated, final report | P2_ARTIFACT | DIFFERS (the store stage reads the candidates from the sealed Store on the NVMe boot disk, not firmware files; same line and count; also requires `artifact_source: nvme_store`, one `artifact_disk:` line per artifact whose SHA-256 equals the host file, disk read before the NVMe DMA revoke, the heap copies released after the loader, no TEST-ONLY fw_cfg source, and fails on `artifact_bundle: malformed`) | +| 35 | frames reclaimed (`artifact_frames_free_before` == `_after`) | P2_ARTIFACT | IDENTICAL (absolute counts differ; verifies execution page frames are reclaimed; candidate heap buffers are also released via ck_stage_disk_artifacts_free) | | 36 | qualification build labelled TEST ONLY, receipt tier line | P2_ARTIFACT | IDENTICAL | | 37 | P26SEED read via grant, write/forged denied, granted subset of requested, exactly one capability, receipt | P2_ARTIFACT | IDENTICAL | | 38 | P25EXEC / P25WX (W^X fault) / P25SPIN (time budget) / P25TAMP (BadSignature) outcomes + receipts | P2_ARTIFACT | IDENTICAL | -| 39 | hostile set: each refused at its stage or admitted-and-contained, receipts checked by the host tool | P2_ARTIFACT | DIFFERS (same expected.txt and checks, receipts checked by the C tool; also requires at least 29 cases) | +| 39 | hostile set: each refused at its stage or admitted-and-contained, receipts checked by the host tool | P2_ARTIFACT | DIFFERS (same expected.txt and checks, receipts checked by the C tool; also requires at least 29 cases, plus two hostile disk cases: an artifact named in the disk index without bytes refused at `received` FirmwareRead with its receipt, and a boot on a disk with one sealed artifact byte flipped where the Store is refused and nothing is read or admitted) | | 40 | H29 READ\|WRITE requested -> READ granted | P2_ARTIFACT | IDENTICAL | | 41 | production build refuses every candidate, zero admitted | P2_ARTIFACT | IDENTICAL | @@ -314,7 +336,7 @@ complete. **C kernel receipt tally (QEMU only, no physical run):** 9 of 14 CK gates PASS, 5 NOT_RUN (M0_ROLLBACK, M4_STORE_CRASH, M4_CONTINUITY, M4_RECOVERY, KEYBOARD), 0 FAIL, matching `evidence/ck_gates_bb4040322b026f5f561733fdcaf900770070f2281800b0e3603c2aa2c670b7ff.json` (run at 4f56a96). The row counts below are per Rust-parity row, not per gate. -Rows 1-106: IDENTICAL 28, DIFFERS 37, NOT_RUN 41, counted from the table (the previous summary, 20/30/56, was miscounted: the table on main had 21/35/50; rows 33-41 then moved from NOT_RUN to 7 IDENTICAL + 2 DIFFERS with the C artifact loader; rows 92-95 have no CK gate at all; rows 96-106 are C-only). Rows 33-41 were checked against scripts/qemu_ck_artifact_test.sh. Rows 102-106 were checked against scripts/qemu_ck_net_test.sh. Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. +Rows 1-106: IDENTICAL 28, DIFFERS 37, NOT_RUN 41, counted from the table; rows 34 and 39 now also cover the NVMe boot-disk source and its hostile disk cases (statuses unchanged) (the previous summary, 20/30/56, was miscounted: the table on main had 21/35/50; rows 33-41 then moved from NOT_RUN to 7 IDENTICAL + 2 DIFFERS with the C artifact loader; rows 92-95 have no CK gate at all; rows 96-106 are C-only). Rows 33-41 were checked against scripts/qemu_ck_artifact_test.sh. Rows 102-106 were checked against scripts/qemu_ck_net_test.sh. Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. `scripts/trust1_m5_qualify.sh --with-qemu` also runs three of these gates as qemu rows: `ck_m1_boot_qemu` (M1), `ck_store_kernel_qemu` (M4_STORE) and diff --git a/native/kernel/Makefile b/native/kernel/Makefile index a0800086..438c30a6 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -11,6 +11,14 @@ # $(OUT)/full-qemu-unsafe-dma/BOOTAA64.EFI (only # scripts/qemu_ck_store_test.sh sets this; refused # together with CK_HARDWARE_STAGING=1) +# make [full] CK_TEST_FWCFG_ARTIFACTS=1 +# TEST-ONLY: loader takes candidates from the QEMU fw_cfg +# side channel instead of the boot disk Store; own OUT +# (target/native-kernel-test-fwcfg); announces itself and +# never counts toward a PASS. Default images are checked +# to contain no fw_cfg artifact code. +# make store-image host tool $(OUT)/host/ck_store_image: signed artifacts +# into the sealed Store on a raw NVMe disk image # make test host unit tests -> CK_CORE_HOST: PASS # make sanitize same under ASan + UBSan -> CK_CORE_HOST: PASS # make stage-test | stage-sanitize | stage-free stage host tests (stage.mk) @@ -45,6 +53,21 @@ KNM := $(CROSS)nm KREADELF := $(CROSS)readelf HOSTCC ?= cc +# TEST-ONLY artifact source: CK_TEST_FWCFG_ARTIFACTS=1 compiles the QEMU fw_cfg +# side channel (opt/aienos/artifacts) into the loader instead of the boot disk +# Store. Every image built without it is checked to contain no fw_cfg artifact +# code. Such an image announces itself on the console and never counts toward +# a PASS. Its default OUT is separate so it never replaces a default image. +CK_TEST_FWCFG_ARTIFACTS ?= +ifneq ($(strip $(CK_TEST_FWCFG_ARTIFACTS)),) +ifneq ($(strip $(CK_TEST_FWCFG_ARTIFACTS)),1) +$(error CK_TEST_FWCFG_ARTIFACTS must be empty or 1) +endif +ifneq ($(strip $(CK_HARDWARE_STAGING)),) +$(error CK_TEST_FWCFG_ARTIFACTS (QEMU fw_cfg test source) cannot be combined with CK_HARDWARE_STAGING) +endif +OUT ?= $(REPO)/target/native-kernel-test-fwcfg +endif OUT ?= $(REPO)/target/native-kernel AIENOS_COMMIT ?= $(shell git -C $(REPO) rev-parse HEAD 2>/dev/null || echo unknown) @@ -77,6 +100,9 @@ ART_TRUST_FLAGS := -DCK_SEED0B_TEST_ANCHOR=1 else ART_TRUST_FLAGS := endif +ifeq ($(strip $(CK_TEST_FWCFG_ARTIFACTS)),1) +ART_TRUST_FLAGS += -DCK_TEST_FWCFG_ARTIFACTS=1 +endif KINC := -I$(KDIR)/include -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/arch -I$(BOOT) MODULE_INC := -I$(NATIVE)/disk -I$(NATIVE)/store -I$(NATIVE)/m5 -I$(NATIVE)/crypto \ @@ -182,6 +208,19 @@ $(STAGE_KOBJ)/$(subst /,_,$(patsubst $(REPO)/%,%,$(abspath $(1)))).o: $(1) $(STA endef $(foreach s,$(STAGE_SRCS),$(eval $(call stage_rule,$(s)))) +# fwcfg_check(elf): a default image (no CK_TEST_FWCFG_ARTIFACTS) must carry no +# fw_cfg artifact code or strings; a test image must carry its announcement. +define fwcfg_check + @if [ "$(strip $(CK_TEST_FWCFG_ARTIFACTS))" = 1 ]; then \ + grep -aqF "TEST-ONLY QEMU fw_cfg side channel" $(1) || { echo "test fw_cfg image lacks its TEST-ONLY announcement"; rm -f $(1); exit 1; }; \ + else \ + s=$$(grep -aoE "opt/aienos/artifacts|fw_cfg|QEMU0002" $(1) | sort -u | tr "\n" " "); \ + y=$$($(KNM) $(1) | grep -E " (fw_select|fw_read|fw_skip|fw_find|fwcfg_from_dsdt|fwcfg_source|fwcfg)$$" | tr "\n" " "); \ + if [ -n "$$s$$y" ]; then echo "default image carries fw_cfg artifact code: $$s$$y"; rm -f $(1); exit 1; fi; \ + echo "no fw_cfg artifact code in $(1)"; \ + fi +endef + # link_image(output elf, objects): refuses undefined symbols (weak stage # references excepted) and any relocation other than R_AARCH64_RELATIVE. define link_image @@ -204,11 +243,13 @@ $(ELF): $(CORE_OBJS) $(BOOT)/image.ld $(call link_image,$@,$(CORE_OBJS)) @s=$$($(KNM) $@ | grep -E ' [TtDdBbWwVv] ck_stage_(devices|security|store|quiesce)$$' || true); \ if [ -n "$$s" ]; then echo "core image defines stage symbols: $$s"; rm -f $@; exit 1; fi + $(call fwcfg_check,$@) $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld $(call link_image,$@,$(CORE_OBJS) $(STAGE_LINK)) - @for s in ck_stage_devices ck_stage_security ck_stage_store ck_stage_quiesce; do \ + @for s in ck_stage_devices ck_stage_security ck_stage_store ck_stage_quiesce ck_stage_disk_artifacts; do \ $(KNM) $@ | grep -qE " [TW] $$s$$" || { echo "full image lacks $$s"; rm -f $@; exit 1; }; done + $(call fwcfg_check,$@) $(EFI): $(ELF) $(call pe_image,$<,$@) @@ -266,8 +307,19 @@ $(ART_TOOL): $(KDIR)/tools/ck_artifact_tool.c $(ART_HOST_DEPS) $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror $(ART_HOST_INC) -o $@ $< $(ART_HOST_SRCS) tool: $(ART_TOOL) +# Boot-disk image tool (scripts/qemu_ck_artifact_test.sh): writes signed artifacts +# into the sealed Store on a raw NVMe image with the kernel's own Store code +# (store_boot.c, artifact_store.c). Hosted; never part of an image. +STORE_IMG_TOOL := $(OUT)/host/ck_store_image +STORE_IMG_SRCS := $(STAGE_OWN_SRCS) $(STAGE_NATIVE_SRCS) $(NATIVE)/disk/disk_file.c \ + $(KDIR)/svc/tests/ck_host.c $(KDIR)/tools/ck_store_image.c +$(STORE_IMG_TOOL): $(STORE_IMG_SRCS) $(STAGE_HDRS) + @mkdir -p $(dir $@) + $(HOSTCC) $(STAGE_HOST_FLAGS) $(STAGE_INC) -o $@ $(STORE_IMG_SRCS) +store-image: $(STORE_IMG_TOOL) + clean: rm -rf $(OUT) FORCE: -.PHONY: all full test sanitize clean print-kcflags udp-echo tool FORCE +.PHONY: all full test sanitize clean print-kcflags udp-echo tool store-image FORCE diff --git a/native/kernel/core/artifact_loader.c b/native/kernel/core/artifact_loader.c index f9a3ac88..f40e773f 100644 --- a/native/kernel/core/artifact_loader.c +++ b/native/kernel/core/artifact_loader.c @@ -1055,7 +1055,103 @@ static void print_receipt(const char *name, const struct report *r) ck_puts("\n"); } -/* ============================ fw_cfg source ========================= */ +/* ============================ candidate sources ===================== */ + +/* Default source: the boot disk. The Store stage (svc/artifact_store.c) read + * the candidates from the sealed Store on NVMe before revoking the disk's DMA; + * here they are untrusted bytes and take the same verification path as any + * candidate. A test-only QEMU fw_cfg side channel exists only in images built + * with CK_TEST_FWCFG_ARTIFACTS=1 and is compiled out of every default image. */ + +static struct report rep; +static unsigned run_admitted, run_rejected, run_seen; + +static void source_preamble(uint32_t count) +{ + char vh[65], ph[65]; + uint8_t pd[32]; + cka_policy_digest(&policy, pd); + hex(vh, verifier_id, 32); + hex(ph, pd, 32); + ck_printf("artifact_candidates: %u\n", count); + ck_printf("artifact_receipt_tier: SEED-0B-QEMU\nartifact_verifier_identity: %s\n" + "artifact_policy_digest: %s\n", + vh, ph); + ck_printf("artifact_frames_free_before: %llu\n", (unsigned long long)free_frames()); +} + +/* Candidate names come from untrusted input: 1..MAX_NAME printable bytes. */ +static int candidate_name_ok(const char *name, unsigned *nlen) +{ + unsigned n = 0; + if (!name) + return 0; + while (n <= MAX_NAME && name[n]) { + if (name[n] <= ' ' || name[n] > '~') + return 0; + n++; + } + *nlen = n; + return n >= 1 && n <= MAX_NAME; +} + +static void finish_candidate(const char *name) +{ + run_seen++; + if (rep.admitted) + run_admitted++; + else + run_rejected++; + print_candidate(name, &rep); + print_grants(name, &rep); + print_receipt(name, &rep); +} + +#ifndef CK_TEST_FWCFG_ARTIFACTS +static uint32_t disk_source(void) +{ + struct ck_disk_artifacts da; + const char *why = 0; + memset(&da, 0, sizeof da); + if (!ck_stage_disk_artifacts) + why = "no Store stage in this image"; + else if (ck_stage_disk_artifacts(&da)) + why = "Store stage did not run"; + else if (!da.available) + why = da.why ? da.why : "no artifacts"; + uint32_t count = why ? 0 : da.count; + if (count > MAX_CANDIDATES) + count = MAX_CANDIDATES; + if (why) + ck_printf("artifact_source: none (%s)\n", why); + else + ck_printf("artifact_source: nvme_store generation=%llu candidates=%u " + "(read by the store stage; bytes untrusted until verified)\n", + (unsigned long long)da.generation, count); + source_preamble(count); + for (uint32_t c = 0; c < count; c++) { + const struct ck_disk_artifact *a = &da.a[c]; + char name[MAX_NAME + 1]; + unsigned nlen = 0; + if (!candidate_name_ok(a->name, &nlen)) + break; + memcpy(name, a->name, nlen); + name[nlen] = 0; + if (a->state == CK_DISK_ART_OK && a->bytes && a->len >= 1 && a->len <= MAX_BATCH * PAGE) + process_candidate(a->bytes, a->len, &rep); + else if (a->state == CK_DISK_ART_TOO_LARGE || a->len > MAX_BATCH * PAGE) + firmware_rejection(&rep, CKL_STAGING_TOO_LARGE); + else + firmware_rejection(&rep, CKL_FIRMWARE_READ); /* missing on disk */ + finish_candidate(name); + } + return count; +} +#else +/* ======== TEST-ONLY fw_cfg source (CK_TEST_FWCFG_ARTIFACTS=1) ======== */ +#define FWCFG_TEST_BANNER \ + "artifact_source_mode: TEST-ONLY QEMU fw_cfg side channel " \ + "(CK_TEST_FWCFG_ARTIFACTS=1); not the boot disk; never counts toward a PASS\n" static volatile uint8_t *fwcfg; @@ -1138,24 +1234,8 @@ static int fw_find(uint16_t *sel, uint32_t *size) return -1; } -static struct report rep; - -void ck_artifact_run(void) +static uint32_t fwcfg_source(void) { - static int ran; - if (ran) - return; - ran = 1; - ck_set_stage("artifacts"); -#ifdef CK_SEED0B_TEST_ANCHOR - ck_puts("artifact_trust: seed0b-test qualification build \xe2\x80\x94 TEST ONLY\n"); -#endif - if (NANCHORS) - cka_boot_policy(&policy, anchors, NANCHORS); - else - cka_boot_policy(&policy, 0, 0); - cka_verifier_identity(AIENOS_COMMIT, FEATURE_TEST_ANCHOR, verifier_id); - const char *why = 0; uint64_t base = fwcfg_from_dsdt(&why); uint16_t sel = 0; @@ -1189,26 +1269,17 @@ void ck_artifact_run(void) if (!base) ck_printf("artifact_source: none (%s)\n", why); else - ck_printf("artifact_source: fw_cfg base=0x%llx file=opt/aienos/artifacts bytes=%u\n", + ck_printf("artifact_source: fw_cfg TEST-ONLY base=0x%llx file=opt/aienos/artifacts bytes=%u\n", (unsigned long long)base, size); if (count > MAX_CANDIDATES) count = MAX_CANDIDATES; - char vh[65], ph[65]; - uint8_t pd[32]; - cka_policy_digest(&policy, pd); - hex(vh, verifier_id, 32); - hex(ph, pd, 32); - ck_printf("artifact_candidates: %u\n", count); - ck_printf("artifact_receipt_tier: SEED-0B-QEMU\nartifact_verifier_identity: %s\n" - "artifact_policy_digest: %s\n", - vh, ph); - ck_printf("artifact_frames_free_before: %llu\n", (unsigned long long)free_frames()); + source_preamble(count); - unsigned admitted = 0, rejected = 0, seen = 0; uint64_t left = size >= 12 ? size - 12 : 0; for (uint32_t c = 0; c < count; c++) { uint8_t nb[2], lb[4]; char name[MAX_NAME + 1]; + unsigned ok_len = 0; if (left < 2) break; fw_read(nb, 2); @@ -1224,13 +1295,8 @@ void ck_artifact_run(void) if (len > left) break; left -= len; - int ok_name = 1; - for (unsigned i = 0; i < nlen; i++) - if (name[i] <= ' ' || name[i] > '~') - ok_name = 0; - if (!ok_name) + if (!candidate_name_ok(name, &ok_len) || ok_len != nlen) break; - seen++; uint64_t pages = (len + PAGE - 1) / PAGE, in = 0; if (len > MAX_BATCH * PAGE) { fw_skip(len); @@ -1247,20 +1313,46 @@ void ck_artifact_run(void) ck_mm_frames_free(in, pages); } } - if (rep.admitted) - admitted++; - else - rejected++; - print_candidate(name, &rep); - print_grants(name, &rep); - print_receipt(name, &rep); + finish_candidate(name); } - if (seen != count) - ck_printf("artifact_bundle: malformed after %u of %u candidates\n", seen, count); + return count; +} +#endif + +void ck_artifact_run(void) +{ + static int ran; + if (ran) + return; + ran = 1; + ck_set_stage("artifacts"); +#ifdef CK_SEED0B_TEST_ANCHOR + ck_puts("artifact_trust: seed0b-test qualification build \xe2\x80\x94 TEST ONLY\n"); +#endif +#ifdef CK_TEST_FWCFG_ARTIFACTS + ck_puts(FWCFG_TEST_BANNER); +#endif + if (NANCHORS) + cka_boot_policy(&policy, anchors, NANCHORS); + else + cka_boot_policy(&policy, 0, 0); + cka_verifier_identity(AIENOS_COMMIT, FEATURE_TEST_ANCHOR, verifier_id); + +#ifdef CK_TEST_FWCFG_ARTIFACTS + uint32_t count = fwcfg_source(); +#else + uint32_t count = disk_source(); +#endif + if (run_seen != count) + ck_printf("artifact_bundle: malformed after %u of %u candidates\n", run_seen, count); ck_printf("artifact_frames_free_after: %llu\n", (unsigned long long)free_frames()); ck_artifact_summary.candidates = count; - ck_artifact_summary.admitted = admitted; - ck_artifact_summary.rejected = rejected; + ck_artifact_summary.admitted = run_admitted; + ck_artifact_summary.rejected = run_rejected; +#ifndef CK_TEST_FWCFG_ARTIFACTS + if (ck_stage_disk_artifacts_free) + ck_stage_disk_artifacts_free(); +#endif } @@ -1268,6 +1360,9 @@ void ck_artifact_final(void) { #ifdef CK_SEED0B_TEST_ANCHOR ck_puts("artifact_trust: seed0b-test qualification build \xe2\x80\x94 TEST ONLY\n"); +#endif +#ifdef CK_TEST_FWCFG_ARTIFACTS + ck_puts(FWCFG_TEST_BANNER); #endif ck_printf("artifacts: candidates=%u admitted=%u rejected=%u\n", ck_artifact_summary.candidates, ck_artifact_summary.admitted, ck_artifact_summary.rejected); diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index c5413587..be510376 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -118,12 +118,14 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) /* M3 isolation checks (core/m3.c), before any stage registers an IRQ. */ ck_m3_run(); - /* P2 sealed-artifact loader (core/artifact_loader.c). */ - ck_artifact_run(); - run_stage("devices", ck_stage_devices); run_stage("security", ck_stage_security); run_stage("store", ck_stage_store); + + /* P2 sealed-artifact loader (core/artifact_loader.c). Runs after the store + * stage: its default candidate source is the boot disk Store, read by that + * stage before the NVMe DMA revoke. No stage registers an IRQ. */ + ck_artifact_run(); struct ck_mm_usage mu; ck_mm_usage(&mu); ck_printf("mm_usage: stack_bytes=%llu stack_used=%llu heap_bytes=%llu heap_free=%llu heap_min_free=%llu\n", diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index f27c342b..ca156939 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -119,4 +119,25 @@ __attribute__((weak)) int ck_stage_store(void); /* sealed Store on NVMe */ * off (final report, panic, fault), so no device keeps DMA across a reset. */ __attribute__((weak)) void ck_stage_quiesce(void); +/* Optional Store-stage hook: signed P2 artifact candidates read from the boot + * disk Store (svc/artifact_store.c) during ck_stage_store, before the NVMe + * release. The bytes are UNTRUSTED: the core loader verifies them exactly as + * any other candidate. Returns 0 and fills *out once the store stage ran. */ +enum { CK_DISK_ART_OK = 0, CK_DISK_ART_MISSING = 1, CK_DISK_ART_TOO_LARGE = 2 }; +struct ck_disk_artifact { + const char *name; /* 1..32 printable bytes */ + const uint8_t *bytes; /* len bytes when state == CK_DISK_ART_OK, else NULL */ + uint64_t len; + int state; /* CK_DISK_ART_* */ +}; +struct ck_disk_artifacts { + int available; /* an artifact index was read from the Store */ + const char *why; /* reason when not available */ + uint64_t generation; /* Store generation of the index */ + unsigned count; + const struct ck_disk_artifact *a; +}; +__attribute__((weak)) int ck_stage_disk_artifacts(struct ck_disk_artifacts *out); +__attribute__((weak)) void ck_stage_disk_artifacts_free(void); + #endif diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index 6e99b9f8..d59b824a 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -15,7 +15,8 @@ STAGE_OUT ?= /tmp/aienos-ck-stage-$(shell id -u) # Own sources (image). STAGE_OWN_SRCS := $(STAGE_DIR)/dev/pci.c $(STAGE_DIR)/dev/nvme_bind.c $(STAGE_DIR)/dev/nvme_shutdown.c $(STAGE_DIR)/dev/virtio_net.c \ $(STAGE_DIR)/dev/net_bind.c $(STAGE_DIR)/dev/net_udp.c \ - $(STAGE_DIR)/dev/devices.c $(STAGE_DIR)/svc/security.c $(STAGE_DIR)/svc/store_boot.c + $(STAGE_DIR)/dev/devices.c $(STAGE_DIR)/svc/security.c $(STAGE_DIR)/svc/store_boot.c \ + $(STAGE_DIR)/svc/artifact_store.c # Native modules, compiled unmodified. STAGE_NATIVE_SRCS := \ $(STAGE_NATIVE)/disk/disk.c $(STAGE_NATIVE)/disk/disk_nvme.c \ @@ -79,7 +80,7 @@ stage-free: $(STAGE_OUT)/stage_all.o # ---- host tests ---------------------------------------------------------- STAGE_HOST_SRCS := $(STAGE_OWN_SRCS) $(STAGE_NATIVE_SRCS) $(STAGE_NATIVE)/disk/disk_file.c \ $(STAGE_DIR)/svc/tests/ck_host.c $(STAGE_DIR)/svc/tests/stage_test.c -STAGE_HOST_FLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -pthread +STAGE_HOST_FLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -pthread -DCK_ART_STORE_WRITER STAGE_SAN_FLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer $(STAGE_OUT)/stage_test: $(STAGE_HOST_SRCS) $(wildcard $(STAGE_DIR)/dev/*.h $(STAGE_DIR)/svc/*.h $(STAGE_DIR)/include/*.h) diff --git a/native/kernel/svc/artifact_store.c b/native/kernel/svc/artifact_store.c new file mode 100644 index 00000000..65c70094 --- /dev/null +++ b/native/kernel/svc/artifact_store.c @@ -0,0 +1,401 @@ +/* artifact_store.c -- see artifact_store.h. */ +#include "artifact_store.h" +#include <stddef.h> +#include "ck.h" +#include "sha256.h" + +static void az(void *p, size_t n) +{ + uint8_t *b = p; + while (n--) *b++ = 0; +} +static void acp(void *d, const void *s, size_t n) +{ + uint8_t *o = d; + const uint8_t *i = s; + while (n--) *o++ = *i++; +} +static int aeq(const void *a, const void *b, size_t n) +{ + const uint8_t *x = a, *y = b; + for (size_t i = 0; i < n; i++) + if (x[i] != y[i]) return 0; + return 1; +} +static uint32_t g32(const uint8_t *p) +{ + return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; +} +static size_t slen(const char *s) +{ + size_t n = 0; + while (s[n]) n++; + return n; +} +static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); } +#ifdef CK_ART_STORE_WRITER +static void p32(uint8_t *p, uint32_t v) +{ + for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); +} +static void p16(uint8_t *p, uint16_t v) +{ + p[0] = (uint8_t)v; + p[1] = (uint8_t)(v >> 8); +} +#endif + +static const uint8_t IX_MAGIC[8] = {'A', 'I', 'E', 'N', 'A', 'I', 'X', '1'}; +static const uint8_t CH_MAGIC[8] = {'A', 'I', 'E', 'N', 'A', 'C', 'H', '1'}; + +static uint32_t chunks_for(uint32_t len) { return (len + CK_ART_CHUNK_DATA - 1u) / CK_ART_CHUNK_DATA; } +static uint32_t chunk_size(uint32_t len, uint32_t i) +{ + uint32_t off = i * CK_ART_CHUNK_DATA, rest = len - off; + return rest < CK_ART_CHUNK_DATA ? rest : CK_ART_CHUNK_DATA; +} + +/* name: 1..32 bytes of printable ASCII without space, zero padded to 32. */ +static int name_ok(const uint8_t *nlen, const uint8_t *name) +{ + if (*nlen == 0 || *nlen > CK_ART_NAME_MAX) return 0; + for (uint32_t i = 0; i < CK_ART_NAME_MAX; i++) { + if (i < *nlen) { + if (name[i] < 0x21 || name[i] > 0x7e) return 0; + } else if (name[i]) { + return 0; + } + } + return 1; +} + +void ck_art_set_free(ck_art_set *set) +{ + if (!set) return; + for (uint32_t i = 0; i < CK_ART_MAX_ENTRIES; i++) + if (set->e[i].bytes) { + ck_free(set->e[i].bytes); + set->e[i].bytes = 0; + } +} + +static int bad(ck_art_set *set, const char **why, const char *w, uint8_t *buf, uint64_t *gens) +{ + ck_art_set_free(set); + az(set, sizeof *set); + if (why) *why = w; + if (buf) ck_free(buf); + if (gens) ck_free(gens); + return -1; +} + +int ck_art_collect(ss_store *s, ck_art_set *set, const char **why) +{ + if (why) *why = "ok"; + if (!set) return -1; + az(set, sizeof *set); + if (!s || !s->ws) { + if (why) *why = "no Store"; + return -1; + } + ss_workspace *ws = s->ws; + + /* Newest index = highest Store generation among index-kind claims. */ + int best = -1; + for (uint32_t k = 0; k < s->nclaims; k++) { + const ss_claim *c = &ws->claims[k]; + if (c->c.obj.object_kind != CK_ART_INDEX_KIND) continue; + if (best < 0 || c->c.store_generation > ws->claims[best].c.store_generation) best = (int)k; + } + if (best < 0) return 0; + + uint8_t *buf = ck_alloc(SS_MAX_PLAINTEXT); + uint64_t *gens = ck_alloc(sizeof(uint64_t) * CK_ART_MAX_ENTRIES * CK_ART_MAX_CHUNKS); + if (!buf || !gens) return bad(set, why, "out of memory", buf, gens); + az(gens, sizeof(uint64_t) * CK_ART_MAX_ENTRIES * CK_ART_MAX_CHUNKS); + + uint8_t sid[32]; + size_t len = 0; + uint16_t kind = 0; + acp(sid, ws->claims[best].sid, 32); + uint64_t igen = ws->claims[best].c.store_generation; + if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) != 0 || kind != CK_ART_INDEX_KIND) + return bad(set, why, "index object read failed", buf, gens); + if (len < CK_ART_INDEX_HDR || !aeq(buf, IX_MAGIC, 8) || g32(buf + 8) != CK_ART_VERSION) + return bad(set, why, "index malformed (header)", buf, gens); + uint32_t n = g32(buf + 12); + if (n > CK_ART_MAX_ENTRIES || len != CK_ART_INDEX_HDR + (size_t)n * CK_ART_INDEX_ENTRY) + return bad(set, why, "index malformed (count/length)", buf, gens); + set->found = 1; + set->index_generation = igen; + sha256_hash(buf, len, set->index_sha256); + set->count = n; + for (uint32_t i = 0; i < n; i++) { + const uint8_t *e = buf + CK_ART_INDEX_HDR + (size_t)i * CK_ART_INDEX_ENTRY; + ck_art_entry *a = &set->e[i]; + if (!name_ok(e, e + 4) || e[1] || e[2] || e[3] || g16(e + 42) || g32(e + 44)) + return bad(set, why, "index malformed (entry)", buf, gens); + uint32_t blen = g32(e + 36); + uint16_t ch = g16(e + 40); + if (blen == 0) return bad(set, why, "index malformed (empty artifact)", buf, gens); + acp(a->name, e + 4, e[0]); + a->name[e[0]] = 0; + for (uint32_t j = 0; j < i; j++) + if (aeq(set->e[j].name, a->name, CK_ART_NAME_MAX + 1)) + return bad(set, why, "index malformed (duplicate name)", buf, gens); + a->len = blen; + a->chunks = ch; + acp(a->want, e + 48, 32); + if (blen > CK_ART_MAX_BYTES) { + a->state = CK_ART_TOO_LARGE; /* never read; the loader refuses it */ + continue; + } + if (ch != chunks_for(blen)) return bad(set, why, "index malformed (chunk count)", buf, gens); + a->state = CK_ART_MISSING; /* until every chunk is found */ + a->bytes = ck_alloc(blen); + if (!a->bytes) return bad(set, why, "out of memory", buf, gens); + az(a->bytes, blen); + } + + /* Chunks: committed no later than the index, matching an entry exactly. + * For one (entry, chunk) the newest generation wins. */ + for (uint32_t k = 0; k < s->nclaims; k++) { + const ss_claim *c = &ws->claims[k]; + if (c->c.obj.object_kind != CK_ART_CHUNK_KIND) continue; + uint64_t g = c->c.store_generation; + if (g > igen) { + set->chunks_ignored++; + continue; + } + acp(sid, c->sid, 32); + if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) != 0 || kind != CK_ART_CHUNK_KIND) + return bad(set, why, "chunk object read failed", buf, gens); + int used = 0; + if (len >= CK_ART_CHUNK_HDR && aeq(buf, CH_MAGIC, 8) && g32(buf + 8) == CK_ART_VERSION && + name_ok(buf + 20, buf + 24) && !buf[21] && !buf[22] && !buf[23]) { + uint32_t tl = g32(buf + 12); + uint16_t ci = g16(buf + 16), cc = g16(buf + 18); + for (uint32_t i = 0; i < n; i++) { + ck_art_entry *a = &set->e[i]; + if (a->state == CK_ART_TOO_LARGE || !a->bytes) continue; + if (buf[20] != (uint8_t)slen(a->name) || !aeq(buf + 24, a->name, buf[20])) continue; + if (!aeq(buf + 56, a->want, 32)) break; /* another write's chunk */ + if (tl != a->len || cc != a->chunks || ci >= cc) break; + uint32_t sz = chunk_size(tl, ci); + if (len != CK_ART_CHUNK_HDR + sz) break; + uint64_t *slot = &gens[i * CK_ART_MAX_CHUNKS + ci]; + if (*slot && *slot >= g) break; /* an equal or newer copy already won */ + if (!*slot) a->present++; + *slot = g; + acp(a->bytes + (size_t)ci * CK_ART_CHUNK_DATA, buf + CK_ART_CHUNK_HDR, sz); + used = 1; + break; + } + } + if (used) set->chunks_used++; + else set->chunks_ignored++; + } + for (uint32_t i = 0; i < n; i++) { + ck_art_entry *a = &set->e[i]; + if (a->state == CK_ART_TOO_LARGE) continue; + if (a->present == a->chunks) sha256_hash(a->bytes, a->len, a->sha256); + if (a->present == a->chunks && aeq(a->sha256, a->want, 32)) { + a->state = CK_ART_OK; + } else { + if (a->present == a->chunks) a->state = CK_ART_MISMATCH; + ck_free(a->bytes); + a->bytes = 0; + } + } + az(buf, SS_MAX_PLAINTEXT); + ck_free(buf); + ck_free(gens); + return 0; +} + +/* ---- kernel stage glue ---------------------------------------------------- */ +static ck_art_set *g_set; +static struct ck_disk_artifact g_out[CK_ART_MAX_ENTRIES]; +static struct ck_disk_artifacts g_res; +static int g_loaded; + +static void hex(char *o, const uint8_t *b, size_t n) +{ + static const char d[] = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { + o[2 * i] = d[b[i] >> 4]; + o[2 * i + 1] = d[b[i] & 15]; + } + o[2 * n] = 0; +} + +void ck_art_stage_load(ss_store *s, const char *refusal) +{ + char h[65]; + g_loaded = 1; + az(&g_res, sizeof g_res); + g_res.a = g_out; + if (!s) { + g_res.why = "boot disk Store refused"; + ck_printf("artifact_disk: none (Store refused: %s); no artifact bytes read\n", refusal ? refusal : "?"); + return; + } + if (!g_set) g_set = ck_alloc(sizeof *g_set); + if (!g_set) { + g_res.why = "out of memory"; + ck_printf("artifact_disk: none (out of memory)\n"); + return; + } + ck_art_set_free(g_set); /* bytes of an earlier load (host tests) */ + az(g_out, sizeof g_out); + const char *why = 0; + int rc = ck_art_collect(s, g_set, &why); + if (rc < 0) { + g_res.why = "artifact index refused"; + ck_printf("artifact_disk: REFUSED (%s); no artifact used\n", why); + return; + } + if (!g_set->found) { + g_res.why = "no artifact index in the boot disk Store"; + ck_printf("artifact_disk: none (no artifact index in the boot disk Store)\n"); + return; + } + hex(h, g_set->index_sha256, 32); + ck_printf("artifact_disk: index generation=%llu entries=%u chunks_used=%u chunks_ignored=%u sha256=%s\n", + (unsigned long long)g_set->index_generation, g_set->count, g_set->chunks_used, + g_set->chunks_ignored, h); + for (uint32_t i = 0; i < g_set->count; i++) { + const ck_art_entry *a = &g_set->e[i]; + g_out[i].name = a->name; + g_out[i].len = a->len; + g_out[i].bytes = a->state == CK_ART_OK ? a->bytes : 0; + g_out[i].state = a->state == CK_ART_OK ? CK_DISK_ART_OK : + a->state == CK_ART_TOO_LARGE ? CK_DISK_ART_TOO_LARGE : CK_DISK_ART_MISSING; + if (a->state == CK_ART_OK) { + hex(h, a->sha256, 32); + ck_printf("artifact_disk: %s bytes=%u sha256=%s\n", a->name, a->len, h); + } else if (a->state == CK_ART_TOO_LARGE) { + ck_printf("artifact_disk: %s bytes=%u too-large (limit %u); not read\n", a->name, a->len, + CK_ART_MAX_BYTES); + } else if (a->state == CK_ART_MISMATCH) { + ck_printf("artifact_disk: %s bytes=%u assembled sha256 differs from the index; not used\n", + a->name, a->len); + } else { + ck_printf("artifact_disk: %s bytes=%u missing chunks=%u/%u; not used\n", a->name, a->len, + a->present, a->chunks); + } + } + g_res.available = 1; + g_res.why = "ok"; + g_res.generation = g_set->index_generation; + g_res.count = g_set->count; +} + +int ck_stage_disk_artifacts(struct ck_disk_artifacts *out) +{ + if (!out || !g_loaded) return -1; + *out = g_res; + return 0; +} + +void ck_stage_disk_artifacts_free(void) +{ + if (g_set) { + uint64_t nb = 0; + uint32_t na = 0; + for (uint32_t i = 0; i < CK_ART_MAX_ENTRIES; i++) + if (g_set->e[i].bytes) { + nb += g_set->e[i].len; + na++; + } + ck_printf("artifact_disk: released heap copies artifacts=%u bytes=%llu\n", na, (unsigned long long)nb); + ck_art_set_free(g_set); + ck_free(g_set); + g_set = 0; + } + az(g_out, sizeof g_out); + az(&g_res, sizeof g_res); +} + +/* ---- host writer ------------------------------------------------------------ */ +#ifdef CK_ART_STORE_WRITER +static int put_name(uint8_t *nlen, uint8_t *name, const char *s) +{ + size_t l = s ? slen(s) : 0; + if (l == 0 || l > CK_ART_NAME_MAX) return -1; + *nlen = (uint8_t)l; + az(name, CK_ART_NAME_MAX); + acp(name, s, l); + return name_ok(nlen, name) ? 0 : -1; +} + +int ck_art_write(ss_store *s, const ck_art_input *in, size_t n) +{ + if (!s || (n && !in) || n > CK_ART_MAX_ENTRIES) return -1; + static uint8_t chunk[SS_MAX_OBJECTS][SS_MAX_PLAINTEXT]; + static uint8_t index[CK_ART_INDEX_HDR + CK_ART_MAX_ENTRIES * CK_ART_INDEX_ENTRY]; + ss_object objs[SS_MAX_OBJECTS]; + size_t no = 0; + az(index, sizeof index); + acp(index, IX_MAGIC, 8); + p32(index + 8, CK_ART_VERSION); + p32(index + 12, (uint32_t)n); + /* validate every entry before the first transaction */ + for (size_t i = 0; i < n; i++) { + uint8_t nl, nm[CK_ART_NAME_MAX]; + if (put_name(&nl, nm, in[i].name)) return -2; + size_t li = slen(in[i].name); + for (size_t j = 0; j < i; j++) { + size_t lj = slen(in[j].name); + if (li == lj && aeq(in[j].name, in[i].name, li)) return -2; + } + if (in[i].len == 0 || in[i].len > CK_ART_MAX_BYTES || (!in[i].missing && !in[i].bytes)) return -3; + } + for (size_t i = 0; i < n; i++) { + uint8_t *e = index + CK_ART_INDEX_HDR + i * CK_ART_INDEX_ENTRY; + if (put_name(e, e + 4, in[i].name)) return -2; + size_t li = slen(in[i].name); + for (size_t j = 0; j < i; j++) { + size_t lj = slen(in[j].name); + if (li == lj && aeq(in[j].name, in[i].name, li)) return -2; + } + if (in[i].len == 0 || in[i].len > CK_ART_MAX_BYTES || (!in[i].missing && !in[i].bytes)) return -3; + uint32_t len = (uint32_t)in[i].len, cc = chunks_for(len); + p32(e + 36, len); + p16(e + 40, (uint16_t)cc); + uint8_t dg[32]; + az(dg, sizeof dg); + if (!in[i].missing) sha256_hash(in[i].bytes, len, dg); + acp(e + 48, dg, 32); + if (in[i].missing) continue; + for (uint32_t c = 0; c < cc; c++) { + uint8_t *b = chunk[no]; + uint32_t sz = chunk_size(len, c); + az(b, CK_ART_CHUNK_HDR); + acp(b, CH_MAGIC, 8); + p32(b + 8, CK_ART_VERSION); + p32(b + 12, len); + p16(b + 16, (uint16_t)c); + p16(b + 18, (uint16_t)cc); + put_name(b + 20, b + 24, in[i].name); + acp(b + 56, dg, 32); + acp(b + CK_ART_CHUNK_HDR, in[i].bytes + (size_t)c * CK_ART_CHUNK_DATA, sz); + objs[no].kind = CK_ART_CHUNK_KIND; + objs[no].version = CK_ART_VERSION; + objs[no].bytes = b; + objs[no].len = CK_ART_CHUNK_HDR + sz; + if (++no == SS_MAX_OBJECTS) { + int rc = ss_transact(s, objs, no, 0, 0, 0); + if (rc) return rc; + no = 0; + } + } + } + if (no) { + int rc = ss_transact(s, objs, no, 0, 0, 0); + if (rc) return rc; + } + ss_object ix = {CK_ART_INDEX_KIND, CK_ART_VERSION, index, CK_ART_INDEX_HDR + n * CK_ART_INDEX_ENTRY}; + return ss_transact(s, &ix, 1, 0, 0, 0); +} +#endif diff --git a/native/kernel/svc/artifact_store.h b/native/kernel/svc/artifact_store.h new file mode 100644 index 00000000..a1effe17 --- /dev/null +++ b/native/kernel/svc/artifact_store.h @@ -0,0 +1,101 @@ +/* artifact_store.h -- signed P2 artifacts on the boot disk, kept as ordinary + * application objects inside the sealed C Store (store_sealed.h). No second + * disk format: an artifact is one or more Store objects of kind + * CK_ART_CHUNK_KIND plus one index object of kind CK_ART_INDEX_KIND, all + * sealed, catalogued and anchored exactly like the boot record. + * + * Lookup: the index with the highest Store generation names the candidates + * (name, byte length, chunk count) in boot order. Each chunk object carries + * its artifact's name, total length, chunk number and chunk count; a chunk + * is used only if it matches an index entry exactly and is not newer than + * that index. Index entries and chunks both carry the SHA-256 of the whole + * artifact; a chunk is used only for the entry with the same digest, and the + * assembled bytes must hash to it, so chunks of different writes are never + * spliced. An entry with any chunk absent is MISSING, one whose assembly does + * not hash to its digest is MISMATCH (the loader refuses both at stage + * "received", reason FirmwareRead), never partly loaded. + * + * Trust: the Store keys are TEST keys from a public label (store_boot.h), so + * the Store's MACs prove nothing about who wrote an artifact. Bytes read here + * are untrusted input: the kernel loader (core/artifact_loader.c) still runs + * the full format, signature, digest and admission checks on them. + * + * index "AIENAIX1" u32 version(1) u32 count, then count entries of 80: + * u8 name_len(1..32) u8[3] 0, char name[32] (zero padded), + * u32 byte_len(1..CK_ART_MAX_BYTES), u16 chunks, u16 0, u32 0, + * u8 sha256[32] of the whole artifact + * chunk "AIENACH1" u32 version(1) u32 total_len u16 chunk u16 chunks + * u8 name_len u8[3] 0, char name[32], u8 sha256[32] of the whole + * artifact, then the chunk's bytes + * (CK_ART_CHUNK_DATA per chunk, the last one the remainder) + * All integers little endian. */ +#ifndef AIENOS_CK_ARTIFACT_STORE_H +#define AIENOS_CK_ARTIFACT_STORE_H +#include <stddef.h> +#include <stdint.h> +#include "store_sealed.h" + +#define CK_ART_INDEX_KIND 0x0A01u +#define CK_ART_CHUNK_KIND 0x0A02u +#define CK_ART_VERSION 1u +#define CK_ART_NAME_MAX 32u +#define CK_ART_MAX_ENTRIES 48u /* = loader MAX_CANDIDATES */ +#define CK_ART_MAX_BYTES (160u * 4096u) /* = loader MAX_BATCH pages */ +#define CK_ART_INDEX_HDR 16u +#define CK_ART_INDEX_ENTRY 80u +#define CK_ART_CHUNK_HDR 88u +#define CK_ART_CHUNK_DATA (SS_MAX_PLAINTEXT - CK_ART_CHUNK_HDR) +#define CK_ART_MAX_CHUNKS ((CK_ART_MAX_BYTES + CK_ART_CHUNK_DATA - 1u) / CK_ART_CHUNK_DATA) + +enum { CK_ART_OK = 0, CK_ART_MISSING = 1, CK_ART_TOO_LARGE = 2, CK_ART_MISMATCH = 3 }; + +typedef struct { + char name[CK_ART_NAME_MAX + 1]; + uint32_t len; /* byte length from the index */ + uint16_t chunks; /* chunk count from the index */ + uint16_t present; /* chunks found */ + int state; /* CK_ART_* */ + uint8_t *bytes; /* len bytes, CK_ART_OK only (ck_alloc) */ + uint8_t want[32]; /* artifact SHA-256 named by the index */ + uint8_t sha256[32]; /* of bytes exactly as read, CK_ART_OK only */ +} ck_art_entry; + +typedef struct { + int found; /* an index object was read */ + uint64_t index_generation; /* Store generation of that index */ + uint8_t index_sha256[32]; /* of the index object's plaintext */ + uint32_t count; + uint32_t chunks_used, chunks_ignored; + ck_art_entry e[CK_ART_MAX_ENTRIES]; +} ck_art_set; + +/* Read the newest index and every chunk it names from an open sealed store. + * 0 with set->found == 0: no index (no artifacts on this disk). 0 with + * set->found == 1: entries filled (each OK, MISSING, MISMATCH or TOO_LARGE). < 0: a + * malformed index or a Store read error; *why names it; nothing usable. */ +int ck_art_collect(ss_store *s, ck_art_set *set, const char **why); +void ck_art_set_free(ck_art_set *set); +void ck_stage_disk_artifacts_free(void); + +/* Store stage hook (svc/store_boot.c): s is the open Store after this boot's + * commit, or NULL when the Store was refused (refusal names why). Reads and + * prints the "artifact_disk:" lines; the core loader picks the result up + * through ck_stage_disk_artifacts (ck.h). Must run before the NVMe release. */ +void ck_art_stage_load(ss_store *s, const char *refusal); + +#ifdef CK_ART_STORE_WRITER +/* Host-side writer (image build tool and host tests only; never compiled + * into the kernel). Writes every chunk (at most SS_MAX_OBJECTS per Store + * transaction), then the index in a final transaction, so an index is only + * ever committed after all of its chunks. missing != 0 puts the entry in the + * index (byte_len = len, which must still be >= 1) without writing chunks: + * a deliberately absent artifact for the hostile disk case. */ +typedef struct { + const char *name; + const uint8_t *bytes; + size_t len; + int missing; +} ck_art_input; +int ck_art_write(ss_store *s, const ck_art_input *in, size_t n); +#endif +#endif diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index 04295064..7441dd61 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -1,5 +1,6 @@ /* store_boot.c -- see store_boot.h. */ #include "store_boot.h" +#include "artifact_store.h" #include <stddef.h> #include "ck.h" #include "disk_layout.h" @@ -120,9 +121,11 @@ int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16 if (!g_ws || !g_ss) return fail(r, "io", "allocate workspace", -1); bz(g_ss, sizeof *g_ss); - st_disk sd; - st_dev sdev; - ts_device tdev; + /* static: the open Store (g_ss) keeps pointers to these devices and is + * used after this returns (store_boot_store, the artifact read). */ + static st_disk sd; + static st_dev sdev; + static ts_device tdev; int rc = st_disk_bind(&sd, d, r->store_base_lba, r->store_units, &sdev); if (rc) return fail(r, "geometry", "st_disk_bind", rc); ss_ts_device(d, &tdev); @@ -232,6 +235,7 @@ int ck_stage_store(void) const disk_dev *d = ck_dev_boot_disk(); if (!d) { ck_printf("store: REFUSED proof=io step=\"no boot disk\" (NVMe not bound; see nvme/dma_gate lines)\n"); + ck_art_stage_load(0, "no boot disk"); return -1; } ss_keys keys; @@ -241,6 +245,14 @@ int ck_stage_store(void) int rc = store_boot_run(d, &keys, ck_store_test_uuid, commit ? commit : "unknown", &r); bz(&keys, sizeof keys); store_boot_print(&r); + /* Signed artifact candidates live in this Store: read them while the disk + * is still bound (the loader verifies them later, as untrusted bytes). */ + ck_art_stage_load(r.verdict == CK_SB_COMMITTED ? g_ss : 0, r.step); ck_dev_nvme_release(); /* last disk user: revoke NVMe bus mastering */ return rc; } + +ss_store *store_boot_store(void) +{ + return g_ss; +} diff --git a/native/kernel/svc/store_boot.h b/native/kernel/svc/store_boot.h index f3832a97..0c181335 100644 --- a/native/kernel/svc/store_boot.h +++ b/native/kernel/svc/store_boot.h @@ -50,4 +50,7 @@ int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16 ck_store_report *r); /* Print the report lines (also used by the host test). */ void store_boot_print(const ck_store_report *r); +/* The open Store of the last store_boot_run (valid only after it returned 0; + * host image tool and tests write further objects through it). */ +ss_store *store_boot_store(void); #endif diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index 19e2b87b..d628ddc0 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -16,6 +16,8 @@ #include "pci.h" #include "security.h" #include "store_boot.h" +#include "artifact_store.h" +#include "sha256.h" #include "net_bind.h" #include "net_udp.h" @@ -592,6 +594,155 @@ static void test_net_udp(void) CHECK(ck_net_bind_selftest(NULL, &caps) == CK_NET_E_ARG); } + +/* ---------------- signed-artifact objects in the boot disk Store ---------------- */ + +#define A_UNITS 2048u +static char aimg[256]; + +static int a_boot(disk_file *f, disk_dev *d, ck_store_report *r) +{ + ss_keys k; + ck_store_test_keys(&k); + if (disk_file_open(f, d, aimg, T_BS, (uint64_t)A_UNITS * T_BPU, 0)) return -999; + return store_boot_run(d, &k, ck_store_test_uuid, "art-test", r); +} + +static void test_artifact_store(void) +{ + snprintf(aimg, sizeof aimg, "/tmp/ck_stage_art_%d.img", (int)getpid()); + unlink(aimg); + disk_file f; + disk_dev d; + ck_store_report r; + CHECK(disk_file_open(&f, &d, aimg, T_BS, (uint64_t)A_UNITS * T_BPU, 1) == 0); + disk_file_close(&f); + + static uint8_t big[40000], big2[40000]; + for (size_t i = 0; i < sizeof big; i++) { + big[i] = (uint8_t)(i * 7 + 3); + big2[i] = (uint8_t)(i * 13 + 1); + } + const uint8_t small[10] = "0123456789"; + ck_art_input in[3] = {{"A.AIEN", big, sizeof big, 0}, {"B.AIEN", small, sizeof small, 0}, + {"C-MISSING.AIEN", 0, 100, 1}}; + ck_art_set *set = calloc(1, sizeof *set); + CHECK(set != NULL); + if (!set) return; + const char *why = 0; + + printf(" [artifact store: write at image build, read back at boot]\n"); + CHECK(a_boot(&f, &d, &r) == 0 && r.formatted == 1); + CHECK(ck_art_collect(store_boot_store(), set, &why) == 0 && set->found == 0); /* no index yet */ + ck_art_input longname = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456", small, sizeof small, 0}; + ck_art_input empty = {"E.AIEN", small, 0, 0}; + ck_art_input dup[2] = {{"B.AIEN", small, sizeof small, 0}, {"B.AIEN", small, sizeof small, 0}}; + CHECK(ck_art_write(store_boot_store(), &longname, 1) != 0); + CHECK(ck_art_write(store_boot_store(), &empty, 1) != 0); + CHECK(ck_art_write(store_boot_store(), dup, 2) != 0); + CHECK(ck_art_write(store_boot_store(), in, 3) == 0); + disk_flush(&d); + disk_file_close(&f); + + CHECK(a_boot(&f, &d, &r) == 0 && r.formatted == 0 && r.boot_count_prev == 1); + CHECK(ck_art_collect(store_boot_store(), set, &why) == 0 && set->found == 1 && set->count == 3); + uint8_t h[32]; + sha256_hash(big, sizeof big, h); + CHECK(set->e[0].state == CK_ART_OK && set->e[0].len == sizeof big && set->e[0].chunks == 3); + CHECK(set->e[0].bytes && memcmp(set->e[0].bytes, big, sizeof big) == 0 && memcmp(set->e[0].sha256, h, 32) == 0); + CHECK(set->e[1].state == CK_ART_OK && set->e[1].len == 10 && memcmp(set->e[1].bytes, small, 10) == 0); + CHECK(set->e[2].state == CK_ART_MISSING && set->e[2].present == 0 && set->e[2].bytes == NULL); + CHECK(strcmp(set->e[0].name, "A.AIEN") == 0 && strcmp(set->e[2].name, "C-MISSING.AIEN") == 0); + ck_art_set_free(set); + + /* the stage hook hands the same bytes to the loader */ + struct ck_disk_artifacts da; + ck_art_stage_load(store_boot_store(), 0); + CHECK(ck_stage_disk_artifacts(&da) == 0 && da.available == 1 && da.count == 3); + CHECK(da.a[0].state == CK_DISK_ART_OK && da.a[0].len == sizeof big && memcmp(da.a[0].bytes, big, sizeof big) == 0); + CHECK(da.a[2].state == CK_DISK_ART_MISSING && da.a[2].bytes == NULL); + + printf(" [artifact store: a newer index and chunks win]\n"); + in[0].bytes = big2; + CHECK(ck_art_write(store_boot_store(), in, 2) == 0); + CHECK(ck_art_collect(store_boot_store(), set, &why) == 0 && set->count == 2); + CHECK(set->e[0].state == CK_ART_OK && memcmp(set->e[0].bytes, big2, sizeof big2) == 0); + ck_art_set_free(set); + + + printf(" [artifact store: chunks of another write are never spliced in]\n"); + ck_art_input again = {"A.AIEN", 0, sizeof big2, 1}; /* same name and length, no bytes */ + CHECK(ck_art_write(store_boot_store(), &again, 1) == 0); + CHECK(ck_art_collect(store_boot_store(), set, &why) == 0 && set->count == 1); + CHECK(set->e[0].state == CK_ART_MISSING && set->e[0].present == 0 && set->e[0].bytes == NULL); + ck_art_set_free(set); + /* a chunk that names B and B's digest but carries other bytes */ + static uint8_t fake[CK_ART_CHUNK_HDR + 10], fix[CK_ART_INDEX_HDR + CK_ART_INDEX_ENTRY]; + uint8_t dg[32]; + sha256_hash(small, sizeof small, dg); + memcpy(fake, "AIENACH1\x01\x00\x00\x00\x0a\x00\x00\x00\x00\x00\x01\x00\x06\x00\x00\x00", 24); + memcpy(fake + 24, "B.AIEN", 6); + memcpy(fake + 56, dg, 32); + memcpy(fake + CK_ART_CHUNK_HDR, "9876543210", 10); + memcpy(fix, "AIENAIX1\x01\x00\x00\x00\x01\x00\x00\x00", 16); + fix[16] = 6; + memcpy(fix + 20, "B.AIEN", 6); + fix[16 + 36] = 10; + fix[16 + 40] = 1; + memcpy(fix + 16 + 48, dg, 32); + ss_object fo[2] = {{CK_ART_CHUNK_KIND, 1, fake, sizeof fake}, {CK_ART_INDEX_KIND, 1, fix, sizeof fix}}; + CHECK(ss_transact(store_boot_store(), fo, 2, 0, 0, 0) == 0); + CHECK(ck_art_collect(store_boot_store(), set, &why) == 0 && set->count == 1); + CHECK(set->e[0].state == CK_ART_MISMATCH && set->e[0].present == 1 && set->e[0].bytes == NULL); + ck_art_set_free(set); + ck_art_stage_load(store_boot_store(), 0); + CHECK(ck_stage_disk_artifacts(&da) == 0 && da.available == 1 && da.a[0].state == CK_DISK_ART_MISSING); + ck_stage_disk_artifacts_free(); + CHECK(ck_stage_disk_artifacts(&da) == 0 && da.available == 0 && da.count == 0); + printf(" [artifact store: malformed index refused]\n"); + uint8_t badix[16] = {'A', 'I', 'E', 'N', 'A', 'I', 'X', '1', 2, 0, 0, 0, 0, 0, 0, 0}; + ss_object ob = {CK_ART_INDEX_KIND, 1, badix, sizeof badix}; + CHECK(ss_transact(store_boot_store(), &ob, 1, 0, 0, 0) == 0); + CHECK(ck_art_collect(store_boot_store(), set, &why) < 0 && set->found == 0); + ck_art_stage_load(store_boot_store(), 0); + CHECK(ck_stage_disk_artifacts(&da) == 0 && da.available == 0); + /* a valid index again, then the disk is corrupted under one chunk */ + CHECK(ck_art_write(store_boot_store(), in, 2) == 0); + ss_store *s = store_boot_store(); + uint64_t off = 0, elen = 0; + for (uint32_t k = 0; k < s->nclaims && !off; k++) { + if (s->ws->claims[k].c.obj.object_kind != CK_ART_CHUNK_KIND) continue; + for (uint32_t i = 0; i < s->st.root.n; i++) { + const sv1_entry *e = &s->st.ws->cat[s->st.root.cat_index][i]; + if (memcmp(e->object_id, s->ws->claims[k].sid, 32)) continue; + off = r.store_base_lba * T_BS + e->first_unit * 4096u; + elen = e->byte_length; + } + } + disk_flush(&d); + disk_file_close(&f); + CHECK(off != 0 && elen > 0); + + printf(" [artifact store: corrupted chunk on disk refuses the Store, no artifact used]\n"); + FILE *fp = fopen(aimg, "r+b"); + CHECK(fp != NULL); + if (fp) { + uint8_t b = 0; + fseek(fp, (long)(off + elen / 2), SEEK_SET); + CHECK(fread(&b, 1, 1, fp) == 1); + b ^= 0xa5; + fseek(fp, (long)(off + elen / 2), SEEK_SET); + CHECK(fwrite(&b, 1, 1, fp) == 1); + fclose(fp); + } + CHECK(a_boot(&f, &d, &r) != 0 && r.verdict == CK_SB_REFUSED); + disk_file_close(&f); + ck_art_stage_load(r.verdict == CK_SB_COMMITTED ? store_boot_store() : 0, r.step); + CHECK(ck_stage_disk_artifacts(&da) == 0 && da.available == 0 && da.count == 0); + free(set); + unlink(aimg); +} + int main(void) { setvbuf(stdout, NULL, _IOLBF, 0); @@ -600,6 +751,7 @@ int main(void) test_pci_bars(); test_pci_enum(); test_store(); + test_artifact_store(); test_security(); test_nvme_shutdown(); test_net_udp(); diff --git a/native/kernel/tools/ck_store_image.c b/native/kernel/tools/ck_store_image.c new file mode 100644 index 00000000..cac30dcb --- /dev/null +++ b/native/kernel/tools/ck_store_image.c @@ -0,0 +1,244 @@ +/* ck_store_image -- host tool: put signed P2 artifacts into the sealed C Store + * on a raw boot disk image, at image build time, so the C kernel's loader + * reads them from NVMe (svc/artifact_store.h). It reuses the kernel's own + * Store code (store_boot.c formats and commits the boot record exactly as a + * first boot would; artifact_store.c writes and reads the artifact objects), + * so there is no second disk format. TEST identity and TEST keys only. + * + * ck_store_image build IMG BLOCK_SIZE ENTRY... IMG must exist and be blank + * ENTRY = path to a file (Store name = its basename, 1..32 bytes) + * | missing:NAME:LEN (index entry, no chunks: hostile case) + * ck_store_image list IMG BLOCK_SIZE read back with the kernel reader + * ck_store_image locate IMG BLOCK_SIZE NAME "offset=B length=L" of the + * sealed envelope holding chunk 0 of NAME (for the corrupt-disk case) + * list and locate only read the image. Hosted C; test tooling, never in an + * image. No Python, no outside libraries. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "artifact_store.h" +#include "disk_file.h" +#include "disk_layout.h" +#include "store_boot.h" + +static int usage(void) +{ + fprintf(stderr, "usage: ck_store_image build IMG BS ENTRY... | list IMG BS | locate IMG BS NAME\n"); + return 2; +} + +static uint8_t *slurp(const char *path, size_t *len) +{ + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + size_t cap = CK_ART_MAX_BYTES + 1, n = 0; + uint8_t *b = malloc(cap); + if (b) n = fread(b, 1, cap, f); + fclose(f); + if (!b || n == 0 || n > CK_ART_MAX_BYTES) { + free(b); + return NULL; + } + *len = n; + return b; +} + +typedef struct { + disk_file f; + disk_dev d; + st_disk sd; + st_dev sdev; + ts_device tdev; + uint64_t base_lba, units; +} image; + +static int img_open(image *im, const char *path, const char *bs_s) +{ + uint32_t bs = (uint32_t)strtoul(bs_s, NULL, 10); + if (bs != 512 && bs != 4096) { + fprintf(stderr, "block size must be 512 or 4096\n"); + return -1; + } + memset(im, 0, sizeof *im); + if (disk_file_open(&im->f, &im->d, path, bs, 0, 0)) { + fprintf(stderr, "cannot open %s\n", path); + return -1; + } + uint32_t bpu = CK_LAYOUT_UNIT / bs; + uint64_t units = im->d.block_count / bpu; + if (units < CK_LAYOUT_ANCHOR_UNITS + CK_LAYOUT_MIN_STORE_UNITS + CK_LAYOUT_PROBE_UNITS) { + fprintf(stderr, "image too small\n"); + disk_file_close(&im->f); + return -1; + } + im->base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu; + im->units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; + return 0; +} + +/* Read-only keyed mount, the same proofs as the kernel's store stage. */ +static int img_mount(image *im, ss_store *s, ss_workspace *ws) +{ + ss_keys keys; + ck_store_test_keys(&keys); + int rc = st_disk_bind(&im->sd, &im->d, im->base_lba, im->units, &im->sdev); + if (!rc) { + ss_ts_device(&im->d, &im->tdev); + memset(s, 0, sizeof *s); + rc = ss_open(s, &im->sdev, &im->tdev, 0, &keys, ws); + } + memset(&keys, 0, sizeof keys); + if (rc) fprintf(stderr, "Store refused: rc=%d\n", rc); + return rc; +} + +static void hex(const uint8_t *b, size_t n) +{ + for (size_t i = 0; i < n; i++) printf("%02x", b[i]); +} + +static int cmd_build(int argc, char **argv) +{ + if (argc < 5) return usage(); + size_t n = (size_t)argc - 4; + if (n > CK_ART_MAX_ENTRIES) { + fprintf(stderr, "at most %u artifacts\n", CK_ART_MAX_ENTRIES); + return 1; + } + ck_art_input *in = calloc(n, sizeof *in); + if (!in) return 1; + for (size_t i = 0; i < n; i++) { + const char *a = argv[4 + i]; + if (!strncmp(a, "missing:", 8)) { + char *name = strdup(a + 8), *c = name ? strrchr(name, ':') : NULL; + if (!c) return usage(); + *c = 0; + in[i].name = name; + in[i].len = strtoul(c + 1, NULL, 10); + in[i].missing = 1; + } else { + const char *base = strrchr(a, '/'); + in[i].name = base ? base + 1 : a; + in[i].bytes = slurp(a, &in[i].len); + if (!in[i].bytes) { + fprintf(stderr, "%s: unreadable, empty or over %u bytes\n", a, CK_ART_MAX_BYTES); + return 1; + } + } + } + image im; + if (img_open(&im, argv[2], argv[3])) return 1; + ss_keys keys; + ck_store_report r; + ck_store_test_keys(&keys); + int rc = store_boot_run(&im.d, &keys, ck_store_test_uuid, "ck_store_image", &r); + memset(&keys, 0, sizeof keys); + if (rc || !r.formatted) { + fprintf(stderr, "%s: %s (the image must be blank: all zero)\n", argv[2], + rc ? "Store format/commit failed" : "not blank"); + disk_file_close(&im.f); + return 1; + } + rc = ck_art_write(store_boot_store(), in, n); + if (!rc) rc = disk_flush(&im.d); + uint64_t gen = ss_generation(store_boot_store()); + disk_file_close(&im.f); + if (rc) { + fprintf(stderr, "artifact write failed: rc=%d\n", rc); + return 1; + } + printf("ck_store_image: %s artifacts=%zu store_generation=%llu (TEST identity, TEST keys)\n", argv[2], n, + (unsigned long long)gen); + return 0; +} + +static int cmd_list(int argc, char **argv) +{ + if (argc != 4) return usage(); + image im; + if (img_open(&im, argv[2], argv[3])) return 1; + ss_workspace *ws = calloc(1, sizeof *ws); + ss_store *s = calloc(1, sizeof *s); + ck_art_set *set = calloc(1, sizeof *set); + int rc = !ws || !s || !set ? -1 : img_mount(&im, s, ws); + const char *why = ""; + if (!rc) rc = ck_art_collect(s, set, &why); + if (!rc && !set->found) printf("no artifact index\n"); + if (!rc && set->found) { + printf("index generation=%llu entries=%u sha256=", (unsigned long long)set->index_generation, set->count); + hex(set->index_sha256, 32); + printf("\n"); + for (uint32_t i = 0; i < set->count; i++) { + const ck_art_entry *a = &set->e[i]; + printf("%s bytes=%u ", a->name, a->len); + if (a->state == CK_ART_OK) { + printf("sha256="); + hex(a->sha256, 32); + printf("\n"); + } else { + printf("%s\n", a->state == CK_ART_TOO_LARGE ? "too-large" : a->state == CK_ART_MISMATCH ? "mismatch" : "missing"); + } + } + } + if (rc) fprintf(stderr, "list failed: rc=%d %s\n", rc, why); + if (set) ck_art_set_free(set); + free(set); + free(s); + free(ws); + disk_file_close(&im.f); + return rc ? 1 : 0; +} + +static int cmd_locate(int argc, char **argv) +{ + if (argc != 5) return usage(); + image im; + if (img_open(&im, argv[2], argv[3])) return 1; + ss_workspace *ws = calloc(1, sizeof *ws); + ss_store *s = calloc(1, sizeof *s); + uint8_t *buf = malloc(SS_MAX_PLAINTEXT); + int rc = !ws || !s || !buf ? -1 : img_mount(&im, s, ws), found = 0; + size_t nl = strlen(argv[4]); + if (nl == 0 || nl > CK_ART_NAME_MAX) { + fprintf(stderr, "name must be 1..%u bytes\n", CK_ART_NAME_MAX); + free(buf); + free(s); + free(ws); + disk_file_close(&im.f); + return 1; + } + for (uint32_t k = 0; !rc && !found && k < s->nclaims; k++) { + if (ws->claims[k].c.obj.object_kind != CK_ART_CHUNK_KIND) continue; + uint8_t sid[32]; + size_t len = 0; + uint16_t kind = 0; + memcpy(sid, ws->claims[k].sid, 32); + if (ss_read(s, sid, buf, SS_MAX_PLAINTEXT, &len, &kind) || len < CK_ART_CHUNK_HDR) continue; + if (buf[16] || buf[17] || buf[20] != nl || memcmp(buf + 24, argv[4], nl)) continue; /* chunk 0 of NAME */ + const st_store *st = &s->st; + for (uint32_t i = 0; i < st->root.n; i++) { + const sv1_entry *e = &st->ws->cat[st->root.cat_index][i]; + if (memcmp(e->object_id, sid, 32)) continue; + printf("offset=%llu length=%llu\n", + (unsigned long long)(im.base_lba * im.d.block_size + e->first_unit * CK_LAYOUT_UNIT), + (unsigned long long)e->byte_length); + found = 1; + break; + } + } + if (!rc && !found) fprintf(stderr, "%s: no chunk 0 on disk\n", argv[4]); + free(buf); + free(s); + free(ws); + disk_file_close(&im.f); + return !rc && found ? 0 : 1; +} + +int main(int argc, char **argv) +{ + if (argc < 2) return usage(); + if (!strcmp(argv[1], "build")) return cmd_build(argc, argv); + if (!strcmp(argv[1], "list")) return cmd_list(argc, argv); + if (!strcmp(argv[1], "locate")) return cmd_locate(argc, argv); + return usage(); +} diff --git a/scripts/qemu_ck_artifact_test.sh b/scripts/qemu_ck_artifact_test.sh index 3561695c..d1ce442b 100755 --- a/scripts/qemu_ck_artifact_test.sh +++ b/scripts/qemu_ck_artifact_test.sh @@ -1,29 +1,44 @@ #!/usr/bin/env bash -# Boot the AIENOS C kernel core with the P2 sealed-artifact loader +# Boot the AIENOS C kernel with the P2 sealed-artifact loader # (native/kernel/core/artifact_loader.c) in QEMU AArch64 + UEFI (AAVMF) and # check the serial console against the same expectations as the Rust gate # scripts/qemu_artifact_test.sh (SEED-0B / P2-5). QEMU is not hardware: a # PASS here qualifies nothing physical. # -# Two boots, as in the Rust gate: -# 1. qualification build (make CK_SEED0B_TEST_ANCHOR=1): trusts the RFC 8032 -# TEST 1 public key only (TEST ONLY); P26SEED, P25EXEC/WX/SPIN/TAMP and -# the hostile set H01..H29 with their expected stage and reason; -# 2. ordinary build: empty production trust, every candidate refused. +# The candidates come from the boot disk: at image build time the host tool +# native/kernel/tools/ck_store_image.c writes the signed artifacts into the +# sealed C Store (TEST identity, TEST keys) on a raw NVMe image; at boot the +# store stage reads them back over the SMMU-confined NVMe driver before the +# NVMe DMA revoke, and the loader verifies them as untrusted bytes. +# +# Three boots of the full image (devices, security, Store stages), each with +# QEMU iommu=smmuv3 and the NVMe image: +# 1. qualification build (make full CK_SEED0B_TEST_ANCHOR=1): trusts the RFC +# 8032 TEST 1 public key only (TEST ONLY); P26SEED, P25EXEC/WX/SPIN/TAMP, +# the hostile set H01..H29 with their expected stage and reason, and +# D01-DISK-MISSING (named in the disk index, no bytes on disk: refused +# at "received", FirmwareRead); +# 2. ordinary build (make full): empty production trust, every candidate refused; +# 3. qualification build on a copy of the disk with one byte of a sealed +# artifact envelope flipped: the Store is refused, no artifact byte is +# used, nothing is admitted. # Every candidate's receipt is decoded, bound to the supplied artifact, -# digest-recomputed, TEST-signed and verified by the in-tree C tool. +# digest-recomputed, TEST-signed and verified by the in-tree C tool. Every +# artifact the kernel read is checked against the host's SHA-256 of the file. +# Static check: no default image (core or full, production or qualification) +# contains fw_cfg artifact code; a CK_TEST_FWCFG_ARTIFACTS=1 image is built only +# to prove that check sees it, and is never booted here. # # Differences from qemu_artifact_test.sh, all deliberate (native/kernel/GATES.md): # - builds with make (gcc), the artifacts with the in-tree C tool # (native/kernel/tools/ck_artifact_tool.c), not cargo / the Rust tool; the # probe programs are the committed fixtures, packed by the same pack.sh; -# - candidates reach the kernel as one QEMU fw_cfg file -# (opt/aienos/artifacts, a bundle) instead of ESP files read by a UEFI -# loader: the C boot stub reads no files; +# - candidates are read from the sealed Store on the NVMe boot disk instead of +# ESP files read by a UEFI loader: the C boot stub reads no files; # - no Machine 1 captured-log mode (SEED-0B Machine 1 stays with the Rust gate); -# - takes the machine quiet flag itself (noclobber) and prints NOT_RUN while -# another run holds it; AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG as in -# qemu_ck_boot_test.sh; +# - takes the machine quiet flag itself (noclobber) before building and prints +# NOT_RUN while another run holds it; AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG as +# in qemu_ck_boot_test.sh; # - stricter: any QEMU exit status other than 0 fails. # Last line: AIENOS_CK_P2_ARTIFACT: PASS|FAIL|NOT_RUN. # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). @@ -38,18 +53,7 @@ vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; } [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } -cross="" -if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi -commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" -out_prod="${repo_root}/target/native-kernel" -out_qual="${repo_root}/target/native-kernel-seed0b-test" -make -s -C native/kernel CROSS="${cross}" OUT="${out_prod}" AIENOS_COMMIT="${commit}" >/dev/null -make -s -C native/kernel CROSS="${cross}" OUT="${out_qual}" AIENOS_COMMIT="${commit}" \ - CK_SEED0B_TEST_ANCHOR=1 >/dev/null -make -s -C native/kernel OUT="${out_prod}" tool >/dev/null -tool="${out_prod}/host/ck_artifact_tool" - -# Machine quiet flag: one heavy run at a time on the Spark. +# Machine quiet flag: one heavy run (builds included) at a time on the Spark. quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_artifact_test $$}" if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then @@ -74,6 +78,38 @@ failed=0 pass() { echo "PASS $1"; } fail() { echo "FAIL $1"; failed=1; } +cross="" +if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" +out_prod="${repo_root}/target/native-kernel" +out_qual="${repo_root}/target/native-kernel-seed0b-test" +out_fwcfg="${repo_root}/target/native-kernel-test-fwcfg" +mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } +mk OUT="${out_prod}" +mk OUT="${out_prod}" full +mk OUT="${out_qual}" CK_SEED0B_TEST_ANCHOR=1 +mk OUT="${out_qual}" CK_SEED0B_TEST_ANCHOR=1 full +mk OUT="${out_fwcfg}" CK_TEST_FWCFG_ARTIFACTS=1 +make -s -C native/kernel OUT="${out_prod}" tool store-image >/dev/null +tool="${out_prod}/host/ck_artifact_tool" +simg="${out_prod}/host/ck_store_image" + +# ---- static: the fw_cfg side channel is compiled out of every default image ---- +fwcfg_pat='opt/aienos/artifacts|fw_cfg|QEMU0002' +fwcfg_syms=' (fw_select|fw_read|fw_skip|fw_find|fwcfg_from_dsdt|fwcfg_source|fwcfg)$' +for d in "${out_prod}" "${out_prod}/full" "${out_qual}" "${out_qual}/full"; do + s=$(grep -aoE "${fwcfg_pat}" "${d}/BOOTAA64.EFI" | sort -u | tr '\n' ' ' || true) + y=$("${cross}nm" "${d}/aienos-ck.elf" | grep -E "${fwcfg_syms}" | tr '\n' ' ' || true) + if [[ -z "${s}${y}" ]]; then pass "default image ${d#"${repo_root}"/}: no fw_cfg artifact strings or symbols" + else fail "default image ${d#"${repo_root}"/} carries fw_cfg artifact code: ${s}${y}"; fi +done +if grep -aqF "opt/aienos/artifacts" "${out_fwcfg}/BOOTAA64.EFI" \ + && grep -aqF "TEST-ONLY QEMU fw_cfg side channel" "${out_fwcfg}/BOOTAA64.EFI"; then + pass "the check sees fw_cfg code in a CK_TEST_FWCFG_ARTIFACTS=1 image, which announces TEST-ONLY (not booted, never counted)" +else + fail "CK_TEST_FWCFG_ARTIFACTS=1 image lacks the fw_cfg source or its TEST-ONLY announcement" +fi + # ---- inputs ----------------------------------------------------------------- fixtures="crates/aienos-artifact-tool/fixtures/p2_5" seed_fixtures="crates/aienos-artifact-tool/fixtures/p2_6" @@ -90,11 +126,32 @@ for f in "${art}"/H*.AIEN; do || echo "${n} $("${tool}" id "${f}" 2>/dev/null || echo none)" >>"${work}/ids.txt" done mapfile -t files < <(find "${art}" -maxdepth 1 -name '*.AIEN' | LC_ALL=C sort) -candidate_count=${#files[@]} -"${tool}" bundle "${work}/bundle.bin" "${files[@]}" >/dev/null +missing=D01-DISK-MISSING.AIEN +missing_len=4096 +candidate_count=$(( ${#files[@]} + 1 )) id_prefix() { awk -v n="$1" '$1 == n { print substr($2, 1, 16) }' "${work}/ids.txt"; } -boot() { # image, serial text output +# ---- boot disk: signed artifacts into the sealed Store (image build time) ---- +disk="${work}/nvme.img" +truncate -s 64M "${disk}" +if "${simg}" build "${disk}" 512 "${files[@]}" "missing:${missing}:${missing_len}" >"${work}/build.txt" 2>&1; then + pass "boot disk built: $(head -1 "${work}/build.txt")" +else + fail "boot disk build: $(head -3 "${work}/build.txt" | tr '\n' ' ')" +fi +# The empty file stands for the absent artifact in the host receipt checks. +: >"${art}/${missing}" +"${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store" +rb_ok=1 +for f in "${files[@]}"; do + n=$(basename "${f}") + grep -qxF "${n} bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" || rb_ok=0 +done +grep -qxF "${missing} bytes=${missing_len} missing" "${work}/list.txt" || rb_ok=0 +if [[ "${rb_ok}" == 1 ]]; then pass "host read-back: all ${#files[@]} artifacts byte-identical in the Store, ${missing} absent" +else fail "host read-back of the boot disk Store differs from the files"; fi + +boot() { # image, serial text output, disk image local esp="${work}/esp" log="${work}/serial.log" started status rm -rf "${esp}" mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS" @@ -105,12 +162,13 @@ boot() { # image, serial text output set +e # Issue #61: single-threaded TCG; MTTCG hung in 1/40 soak boots. nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \ - -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ -drive if=pflash,format=raw,file="${work}/vars.fd" \ -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \ -device virtio-blk-pci,drive=esp \ - -fw_cfg name=opt/aienos/artifacts,file="${work}/bundle.bin" \ + -drive if=none,id=nvme0,format=raw,file="$3" \ + -device nvme,drive=nvme0,serial=aienos-nvme-test \ -device ramfb -display none -nic none \ -serial file:"${log}" -no-reboot status=$? @@ -122,17 +180,20 @@ boot() { # image, serial text output has() { grep -qE -- "$2" "$1"; } check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; } -common_checks() { # serial +check_absent() { if has "$1" "$3"; then fail "$2"; else pass "$2"; fi; } +common_checks() { # serial [candidate count] + local want="${2:-${candidate_count}}" check "$1" "left firmware and entered the kernel" "kernel: alive" check "$1" "M3 cooperative threads unchanged" "threads: ok" check "$1" "M3 EL0 isolation proof unchanged" "el0: ok write=granted forged=denied fault=contained exit=0" check "$1" "M3 typed IPC proof unchanged" "ipc: ok message=delivered cap=delegated rights=attenuated forged=denied revoked=denied" - check "$1" "kernel read all ${candidate_count} artifact candidates" "^artifact_candidates: ${candidate_count}$" + check "$1" "kernel read all ${want} artifact candidates" "^artifact_candidates: ${want}$" if grep -q "^report-truncated:" "$1"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi check "$1" "final report reached the console" "report_kind: final" if has "$1" "report_kind: (panic|fault)"; then fail "panic or fault reported"; fi if has "$1" "^receipt: [^ ]+ seq=[0-9]+ invalid$"; then fail "a candidate produced no valid receipt"; fi - if has "$1" "^artifact_bundle: malformed"; then fail "kernel found the bundle malformed"; fi + if has "$1" "^artifact_bundle: malformed"; then fail "kernel found the candidate list malformed"; fi + check_absent "$1" "no TEST-ONLY fw_cfg source in a counted boot" "TEST-ONLY QEMU fw_cfg|^artifact_source: fw_cfg" local before after before=$(grep -oE "artifact_frames_free_before: [0-9]+" "$1" | awk '{print $2}' | tail -1) after=$(grep -oE "artifact_frames_free_after: [0-9]+" "$1" | awk '{print $2}' | tail -1) @@ -142,7 +203,38 @@ common_checks() { # serial fail "frame accounting across candidates (before=${before:-?} after=${after:-?})" fi } - +disk_checks() { # serial: the candidates came from the boot disk, as read by the kernel + check "$1" "SMMUv3 enabled; NVMe confined" "smmu: enabled" + check "$1" "store stage committed this boot" "^store: committed generation=[0-9]+ boot_count=[0-9]+" + check "$1" "artifact index read from the boot disk Store" \ + "^artifact_disk: index generation=[0-9]+ entries=${candidate_count} chunks_used=[0-9]+ chunks_ignored=0 sha256=[0-9a-f]{64}$" + check "$1" "artifact_source=nvme_store with every candidate" \ + "^artifact_source: nvme_store generation=[0-9]+ candidates=${candidate_count} " + local ok=1 f n + for f in "${files[@]}"; do + n=$(basename "${f}") + has "$1" "^artifact_disk: ${n//./\\.} bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)$" || { ok=0; echo " missing or different on the kernel side: ${n}"; } + done + if [[ "${ok}" == 1 ]]; then pass "kernel read all ${#files[@]} artifacts from NVMe, each SHA-256 equal to the host file" + else fail "kernel-side SHA-256 of the disk artifacts"; fi + check "$1" "${missing} named on disk without bytes: reported missing, not used" \ + "^artifact_disk: ${missing//./\\.} bytes=${missing_len} missing chunks=0/1; not used$" + check "$1" "${missing} refused cleanly at received (FirmwareRead), reclaimed" \ + "^artifact: ${missing//./\\.} rejected stage=received reason=FirmwareRead reclaimed=yes$" + local total=0 + for f in "${files[@]}"; do total=$(( total + $(stat -c %s "${f}") )); done + check "$1" "kernel heap copies of the disk artifacts released after the loader" \ + "^artifact_disk: released heap copies artifacts=${#files[@]} bytes=${total}$" + local ld lr ls + ld=$(grep -n '^artifact_disk: ' "$1" | grep -v ':artifact_disk: released ' | tail -1 | cut -d: -f1) + lr=$(grep -n 'dma_gate: nvme bus master revoked' "$1" | head -1 | cut -d: -f1) + ls=$(grep -n '^artifact_source: ' "$1" | head -1 | cut -d: -f1) + if [[ -n "${ld}" && -n "${lr}" && -n "${ls}" && "${ld}" -lt "${lr}" && "${lr}" -lt "${ls}" ]]; then + pass "disk read finished before the NVMe DMA revoke; verification ran after it" + else + fail "order: disk read (${ld:-?}) < NVMe revoke (${lr:-?}) < loader (${ls:-?})" + fi +} rtag="" receipt_check() { # serial name pattern local serial="$1" name="$2" want="$3" line record kdigest out @@ -175,8 +267,11 @@ admitted_line() { # name, exec regex, caps # ---- boot 1: qualification build ------------------------------------------- qual="${work}/qualification.txt" rtag=qual -boot "${out_qual}/BOOTAA64.EFI" "${qual}" +cp "${disk}" "${work}/disk-qual.img" +boot "${out_qual}/full/BOOTAA64.EFI" "${qual}" "${work}/disk-qual.img" common_checks "${qual}" +disk_checks "${qual}" +receipt_check "${qual}" "${missing}" "decision=Rejected .* stage=1 reason=0x10a status=NotRun exit=0 syscalls=0 reads_ok=0 denials=0 flags=0x04\$" check "${qual}" "qualification build is labelled TEST ONLY" \ "artifact_trust: seed0b-test qualification build — TEST ONLY" check "${qual}" "receipts carry the SEED-0B-QEMU tier" "^artifact_receipt_tier: SEED-0B-QEMU$" @@ -218,18 +313,20 @@ check "${qual}" "H29 requested READ|WRITE and was granted READ only" \ "^grant: H29-READ-WRITE-REQUEST\.AIEN\[0\] kind=object id=1 rights=0x1 " if has "${qual}" "^grant: H28-"; then fail "H28 (WRITE-only request) received a capability"; else pass "H28 requested only WRITE and received no capability"; fi check "${qual}" "final report summarises every admission decision" \ - "^artifacts: candidates=${candidate_count} admitted=$((4 + matrix_admitted)) rejected=$((1 + matrix_total - matrix_admitted))$" + "^artifacts: candidates=${candidate_count} admitted=$((4 + matrix_admitted)) rejected=$((2 + matrix_total - matrix_admitted))$" # ---- boot 2: ordinary build, empty production trust ------------------------- prod="${work}/production.txt" rtag=prod -boot "${out_prod}/BOOTAA64.EFI" "${prod}" -release_flag +cp "${disk}" "${work}/disk-prod.img" +boot "${out_prod}/full/BOOTAA64.EFI" "${prod}" "${work}/disk-prod.img" common_checks "${prod}" +disk_checks "${prod}" if has "${prod}" "seed0b-test qualification build"; then fail "ordinary build must not carry the qualification label"; fi if has "${prod}" "^artifact: [^ ]+ admitted"; then fail "ordinary build admitted an artifact"; else pass "ordinary build admitted nothing"; fi prod_expect() { # name -> "stage reason code" in the ordinary build local line + if [[ "$1" == "${missing}" ]]; then echo "received FirmwareRead 0x10a"; return; fi line=$(sed 's/ #.*//' "${work}/expected.txt" | awk -v n="$1" '$1 == n') if [[ -n "${line}" ]] && read -r _ d s r c <<<"${line}" && [[ "${d}" == rejected && "${s}" == verified \ && "${r}" != BadSignature && "${r}" != UntrustedSigner ]]; then @@ -238,7 +335,7 @@ prod_expect() { # name -> "stage reason code" in the ordinary build echo "verified UntrustedSigner 0x11" fi } -for f in "${files[@]}"; do +for f in "${files[@]}" "${art}/${missing}"; do name=$(basename "${f}") read -r s r c <<<"$(prod_expect "${name}")" check "${prod}" "ordinary build refuses ${name} (${r}), reclaimed" \ @@ -248,9 +345,39 @@ done check "${prod}" "final report summarises zero admitted" \ "^artifacts: candidates=${candidate_count} admitted=0 rejected=${candidate_count}$" + +# ---- boot 3: qualification build, one byte of the boot disk Store flipped ---- +corrupt="${work}/corrupt.txt" +cp "${disk}" "${work}/disk-corrupt.img" +loc=$("${simg}" locate "${work}/disk-corrupt.img" 512 P26SEED.AIEN 2>/dev/null || true) +c_off=$(sed -nE 's/^offset=([0-9]+) length=([0-9]+)$/\1/p' <<<"${loc}") +c_len=$(sed -nE 's/^offset=([0-9]+) length=([0-9]+)$/\2/p' <<<"${loc}") +if [[ -n "${c_off}" && -n "${c_len}" ]]; then + c_at=$(( c_off + c_len / 2 )) + c_old=$(od -An -tu1 -j "${c_at}" -N1 "${work}/disk-corrupt.img" | tr -d ' ') + printf "$(printf '\\%03o' $(( c_old ^ 0xa5 )))" \ + | dd of="${work}/disk-corrupt.img" bs=1 seek="${c_at}" count=1 conv=notrunc status=none + pass "corrupted one byte of P26SEED's sealed envelope on the disk copy (offset ${c_at})" +else + fail "locate P26SEED's envelope on the disk image (${loc})" +fi +if "${simg}" list "${work}/disk-corrupt.img" 512 >/dev/null 2>&1; then + fail "host Store open accepted the corrupted disk" +else + pass "host Store open refuses the corrupted disk" +fi +boot "${out_qual}/full/BOOTAA64.EFI" "${corrupt}" "${work}/disk-corrupt.img" +release_flag +common_checks "${corrupt}" 0 +check "${corrupt}" "kernel refuses the corrupted boot disk Store" "^store: REFUSED " +check "${corrupt}" "no artifact byte read from a refused Store" "^artifact_disk: none \(Store refused: " +check "${corrupt}" "loader reports no source, clean refusal" "^artifact_source: none \(" +check_absent "${corrupt}" "no artifact named or loaded from the corrupted disk" "^artifact(_disk)?: [A-Z][^ ]*\.AIEN " +check "${corrupt}" "final report: zero candidates, nothing admitted" "^artifacts: candidates=0 admitted=0 rejected=0$" if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then cp "${qual}" "${AIENOS_LOG_DIR}/qemu_ck_artifact_qualification_serial.log" cp "${prod}" "${AIENOS_LOG_DIR}/qemu_ck_artifact_production_serial.log" + cp "${corrupt}" "${AIENOS_LOG_DIR}/qemu_ck_artifact_corrupt_disk_serial.log" fi if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then echo "---- qualification serial (artifact lines) ----"