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#277 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/4ff78b0d962f0ad6.bundle -o 4ff78b0d962f0ad6.bundle git fetch 4ff78b0d962f0ad6.bundle refs/pull/277/head
Provenance
github-import 1
aien-dev/aienux
277
d391fd032477d9b86ccf30644caea3cfb1c98ecf
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":277,"url":"https://github.com/aien-dev/aienux/pull/277","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:06Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 1d29b081..ab43d4e8 100644 --- a/.crumb +++ b/.crumb @@ -27,33 +27,33 @@ "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "bd7eb99f15a1f1a63969943ca3c980811ed110709dc9b3861f5aad6fd25fa84c", - "last_commit": "61bd76a", + "digest": "4b1e9c63f6fd3b920b9f3f87060222ce2fba8342db53734ffc1748ba15fb74b5", + "last_commit": "94e8d58", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "71e9cebf25d3fbfb2384b29e439fee7bbffa8cfb5fc59f8e263e560ad1969ce9", - "last_commit": "e387b50", + "digest": "754f7fe4dd0eafbab2cf6a9f33ccbaca08497dfca1459dc30c76214322d7d9cb", + "last_commit": "1ab797d", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "f64c580898dae2fab2c5fcbe9c65f99dfe0ef2bb9b5df76a9d61f3d4d658b4cf", - "last_commit": "e387b50", + "digest": "76d29d214c40f89bd922e34a16760a2652ee259bc0f16d5bf6a847fcad3e5cad", + "last_commit": "bde85f8", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:6856828e62bb9d4cb59c3ef97d1b74dccc9614f0cad0794710a4b91765691c90", - "compiled_at": "2026-10-08T04:37:23Z", + "children_root": "sha256:e2730ee97698209a116fcec3c63b59131770d42e76dc6f266ed86ef0a869e023", + "compiled_at": "2026-10-08T11:03:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:d1db118ed428b38e1155f1fc3dda731c865ceec5f9c063ad6d4de1e7fc8eef05", + "digest": "sha256:ba0c7ee021274b14a59383b6ba55c9ac9f647713bd0af9b80f65cf6240b017f7", "evidence_files": 153, "evidence_root": "sha256:29aa8775feb12d9e0e64b41fc1fc48b97e32dc16bf9176562cbe9fe1ea934f00", - "files": 2883, - "generated_at_commit": "e387b502b58d9d15fd2751ec5ef4c60f93862244", - "source_tree": "sha256:0149676c07cb52dc8e051edfedfb0fb033e481a4cd374b4a4dd3826e5ca2eae1" + "files": 2955, + "generated_at_commit": "1ab797d6c0c42fee6b5fbebebaa81e1e86a55609", + "source_tree": "sha256:d1db949af9f063f43edb8c89550ee3f096b3382d4a87d18d80997c8356c77198" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 786763a5..d22610cc 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -17,27 +17,27 @@ "generated": { "children": [ { - "digest": "c24b943fbbd6b536c29d731066917fc908de275c72f594d2d4a5a1c7d19f879c", - "last_commit": "9d41efc", + "digest": "97b2985e62f1f3cd6977c23c0c924cd7ce02c2aa635a06eb424c6b1fe9ebd3f0", + "last_commit": "94e8d58", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, { "digest": "e9e804e088a0b2316d035b957c09289bf9f6176eece959d43c40c6e66005fe5e", - "last_commit": "7e15a57", + "last_commit": "5849a7a", "name": "crumbs", "purpose": "Directory docs/crumbs. Purpose not yet described by a human." } ], - "children_root": "sha256:25a2af14107c1c2ae96916dd5c4086195ebd4117975ea0ae458855ac21f46099", - "compiled_at": "2026-10-06T19:08:10Z", + "children_root": "sha256:b45190ec82f8e2a2101aed670d7e48b08c2e55d9674785ff0c784dff76ab2468", + "compiled_at": "2026-10-08T10:54:55Z", "compiler": "crumb-compile/1", - "digest": "sha256:bd7eb99f15a1f1a63969943ca3c980811ed110709dc9b3861f5aad6fd25fa84c", + "digest": "sha256:4b1e9c63f6fd3b920b9f3f87060222ce2fba8342db53734ffc1748ba15fb74b5", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 61, - "generated_at_commit": "f23da6872ed8576de162878d89c4eaaa3e065099", - "source_tree": "sha256:6f1215dca863b963793ae16c3c2ed46dadb3867c5706df912324cf09d62898b4" + "generated_at_commit": "94e8d58c6598d71f67ecf7b94ab65cb369a63bf6", + "source_tree": "sha256:8e92139bff44fab9183e57cd8a95fdcd24e0a31c836e805c1a6d77cc20bf9490" }, "owner_lane": "", "provenance": { diff --git a/docs/adr/.crumb b/docs/adr/.crumb index e19e4b8e..9535ae00 100644 --- a/docs/adr/.crumb +++ b/docs/adr/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T02:27:48Z", + "compiled_at": "2026-10-08T10:54:55Z", "compiler": "crumb-compile/1", - "digest": "sha256:c24b943fbbd6b536c29d731066917fc908de275c72f594d2d4a5a1c7d19f879c", + "digest": "sha256:97b2985e62f1f3cd6977c23c0c924cd7ce02c2aa635a06eb424c6b1fe9ebd3f0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 21, - "generated_at_commit": "640522a03b9f939d2d07ef0d0fdc08e35ecefc7e", - "source_tree": "sha256:c92fa7310220ab8a92ea8189797537fba23da7e720846117fe16f8973e559cec" + "generated_at_commit": "94e8d58c6598d71f67ecf7b94ab65cb369a63bf6", + "source_tree": "sha256:0e4bd7a7252bee29ed148d7bfce8c8088df964f49dda98865db53a2e774a9ce2" }, "owner_lane": "", "provenance": { diff --git a/docs/adr/0014-binary-artifact-v0-and-native-admission.md b/docs/adr/0014-binary-artifact-v0-and-native-admission.md index 0b3e6863..3b18764e 100644 --- a/docs/adr/0014-binary-artifact-v0-and-native-admission.md +++ b/docs/adr/0014-binary-artifact-v0-and-native-admission.md @@ -449,3 +449,33 @@ yielding `SEED_0B_MACHINE1`. Phase 2 is complete only after the same qualified artifact is admitted, executed with attenuated authority, denied an unauthorized write, reclaimed, and covered by a valid receipt in QEMU and then on Machine 1. + +## Amendment note, 2026-10-08: second TEST key for OSC unit containers + +This note adds to section 6 and does not change the status of this ADR or any other text in it. + +Section 6 allows a `seed0b-test-anchor` build to add "one known qualification public key". Today that key is the RFC 8032 TEST 1 public key (`d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a`, signer fingerprint `21fe31dfa154a261626bf854046fd2271b7bed4b6abe45aa58877ef47f9721b9`). It verifies Binary Artifact v0. + +The OSC unit container ([aien-protocols](https://github.com/aien-dev/aien-protocols) `specs/osc-unit-artifact`, PR #17, v1 draft) is a different format with its own conformance vectors. Those vectors are signed with a different throwaway key, `keys/test1.pub` of that spec, whose signer fingerprint is `7b136f1c9197f8a99a91057f5deced18d5754669e559ee7cc4f7cef64a626abf`. It is not the RFC 8032 TEST 1 key. + +Amendment: a `seed0b-test-anchor` qualification build may also hold the OSC unit spec's TEST key (`specs/osc-unit-artifact/keys/test1.pub`, fingerprint `7b136f1c9197f8a99a91057f5deced18d5754669e559ee7cc4f7cef64a626abf`), beside the RFC 8032 TEST 1 key. The OSC unit admission path trusts only the spec key and Binary Artifact v0 admission trusts only the RFC 8032 key; neither key is accepted on the other path. + +- This is TEST only. The private seed of the spec key is derivable from a public label in the spec's generator and is not secret. +- A release image holds neither key, and its trust-anchor sets stay empty. +- The qualification gate checks this: the OSC unit gate refuses a TEST-signed unit in an ordinary build, and the boot report names the build as a TEST-only qualification tier. + +Observation, no change made here: the `execution_status` mapping text in section 7.1 lists codes 0 to 5, while the field table above it also lists `6` (canary failed). The mapping text omits 6. + +## Amendment, 2026-10-08: the OSC unit is a second container type + +This amendment does not change the status of this ADR, the Binary Artifact v0 encoding, its limits, its rejection reasons, its receipt, or its trust anchor. It records how the OSC unit container ([aien-protocols](https://github.com/aien-dev/aien-protocols) `specs/osc-unit-artifact`, PR #17, container version 1) relates to this ADR. It settles conflicts C2 and C3 of that spec's section 15.3 and records the AIENOS position on C1. + +Decision record: the operator was offered three choices on aienos#277 (record the OSC unit as its own container type; fit it into Binary Artifact v0; or keep it as an unrecorded QEMU-only path) and on 2026-10-08 delegated the choice ("proceed with however you think best"). The orchestrator chose the first, which this amendment writes down. + +1. **Two container types.** AIENOS recognizes two native container types, told apart by their first eight bytes: Binary Artifact v0 (this ADR, sections 2 to 7) and the OSC unit (magic `OSCUNIT\0`, defined only by the aien-protocols spec). Neither format is a version or extension of the other. The native loader routes on that magic: bytes beginning `OSCUNIT\0` go to the OSC unit path and never reach the Binary Artifact v0 parser, and everything else goes to the Binary Artifact v0 path. Each parser also refuses the other format on its own (`BadMagic` in Binary Artifact v0, `BAD_MAGIC` in the OSC unit reader), so a routing mistake is refused, never misread. +2. **Separate parsers and admission paths.** Each container has its own parser and admission path. Neither path calls the other's parser, and section 5 of this ADR still has no OSC unit responsibilities. Bytes reach both paths the same way (section 1: firmware or the Store is a byte provider only; the kernel copies to protected staging before parsing). +3. **Limits (spec conflict C3).** The limits of section 4 ("Maximum section count: 2", 16 MiB per artifact, 8 MiB payload, 16 capabilities) apply to Binary Artifact v0 only. An OSC unit is held to the limits of its own spec (section 7 there: 4 sections, 2 MiB, 16 capabilities), plus any stricter limit compiled into the kernel, which is part of the kernel's identity and cannot be relaxed by the unit. Today that is the native staging limit of 160 pages (655,360 bytes) per artifact. +4. **Trust anchors (spec conflict C2).** Unchanged from the amendment note above: each path trusts only its own anchors, a qualification build may hold the spec's TEST key for the OSC unit path, and a release image holds no test key for either path. OSC unit OWNER anchors stay empty until TRUST-1 (ADR 0017) provisions them. +5. **Pinned generations (spec conflict C1, AIENOS position).** No AIENOS ADR yet defines a per-resource generation that an OSC unit capability request could pin. Until one does, the OSC unit admission path refuses every request that pins a nonzero `required_generation`, because the kernel has no generation to compare it with. The refusal code follows the spec's fixed check order (its section 8.2): a request on an unsupported domain is refused `CAP_DOMAIN_UNSUPPORTED` (27) first, a domain-1 value wider than 32 bits `CAP_GEN_NOT_REPRESENTABLE` (28) next, and any remaining nonzero pin `CAP_GENERATION_STALE` (29). AIENOS supports domain 1 only. That direction only ever refuses more, never admits more. Defining a per-resource generation is a later AIENOS amendment; it needs no byte change in the container. +6. **Admitted is not Admitted.** Section 5 of this ADR reaches `Admitted` only after a canary run. The OSC unit path defines no canary, so an accepted OSC unit is reported as accepted by its own path, never as `Admitted` in the sense of section 5, and no Admission Receipt v0 (section 7) is issued for it. A canary and a receipt for OSC units are later work. +7. **Evidence boundary.** The OSC unit path in aienos#277 is qualified in QEMU only. A QEMU pass is not a result on the physical Spark. diff --git a/native/.crumb b/native/.crumb index cdc84dd9..cc871e16 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "0dbade83a3790275a2ac39ba503afab9e5f339dcbe33c40d2a8ca272179e5d10", - "last_commit": "e387b50", + "digest": "8db3b02f3a745d27072e98f7aa2954da4399ad7ca309fde516daecb483ee57fd", + "last_commit": "1ab797d", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:6546d0196d635cae4ca479a86164de5401d9e61a3834ab1cfd5e22ba147d1989", - "compiled_at": "2026-10-08T04:37:23Z", + "children_root": "sha256:e1837301912bc5862254dd4fb533126db5cfe18b0a03e5085c054320179b8fe5", + "compiled_at": "2026-10-08T11:03:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:71e9cebf25d3fbfb2384b29e439fee7bbffa8cfb5fc59f8e263e560ad1969ce9", + "digest": "sha256:754f7fe4dd0eafbab2cf6a9f33ccbaca08497dfca1459dc30c76214322d7d9cb", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 318, - "generated_at_commit": "e387b502b58d9d15fd2751ec5ef4c60f93862244", - "source_tree": "sha256:02eb6c89d383fa02ded35ab0b6307121c091869a3018d9d7b093939978e3ff87" + "files": 389, + "generated_at_commit": "1ab797d6c0c42fee6b5fbebebaa81e1e86a55609", + "source_tree": "sha256:d00a64548c8e59e284a47837e329161f0823d4f5c32f9890bb72bd523ea1e2b0" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 77ea02a1..31933e96 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -51,26 +51,26 @@ "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, { - "digest": "efd08e41584e2cbb282cea72e620fa2db42c423c6b3211a91585affb92e34893", - "last_commit": "5849a7a", + "digest": "b7c024fa610faa124f2f26fd002815ea178595962647d544e98ea5a39735f214", + "last_commit": "527a980", "name": "artifact", "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "b45aaa519ba778938f0e30eaa63431594398232c02f1c9f3dfdf970f3a6ed5a8", - "last_commit": "e387b50", + "digest": "fd9456ff602f0eaa5937f04b46762e8dd4c2692707a6b78076b3da4667f4b4ff", + "last_commit": "bde85f8", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "fb8880e2cf5544851b097a3f9977d543ec58a4644a8e43ea615c7242a55de245", - "last_commit": "e387b50", + "last_commit": "8641977", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "4a14f71ed7d1e067b7ead5dacb0c2fda6217626ba5bdf377b3d33701cee109f9", - "last_commit": "e387b50", + "last_commit": "8641977", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -82,13 +82,13 @@ }, { "digest": "25f9bc7ba37e97d49ebc01a48acf93cddb55bda543bed09eb60d25c43caaaaa5", - "last_commit": "e387b50", + "last_commit": "8641977", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "0fd32ca401eef461e59a9ac01b7879ce928c8057e8c2843d934772af8933e1c7", - "last_commit": "3df181c", + "digest": "1b900c32e47ab5317c6fc9ed443f06496eb9c261857f0c7eec3cd65c97eaddcf", + "last_commit": "1ab797d", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:673a07645a7a618d81218876577a7d16979324a64a204069936580b7df9f76d8", - "compiled_at": "2026-10-08T04:37:23Z", + "children_root": "sha256:e378e3a6ed3c6635280cdec9532d1ee6f5535e69cd8b2cd82b887e69fef9684e", + "compiled_at": "2026-10-08T11:03:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:0dbade83a3790275a2ac39ba503afab9e5f339dcbe33c40d2a8ca272179e5d10", + "digest": "sha256:8db3b02f3a745d27072e98f7aa2954da4399ad7ca309fde516daecb483ee57fd", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 154, - "generated_at_commit": "e387b502b58d9d15fd2751ec5ef4c60f93862244", - "source_tree": "sha256:5f1ab2483973c31d1244c3f3cffdb0416d5655240bc133844fb2d8806582baf5" + "files": 225, + "generated_at_commit": "1ab797d6c0c42fee6b5fbebebaa81e1e86a55609", + "source_tree": "sha256:dd3b1d432eace5c18052e4153139a27e7451fd6cc96e80ffd1263ceddb4dc061" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 7ce1aaac..390c0a22 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -677,6 +677,33 @@ NOT_RUN (never a silent pass) when QEMU, AAVMF, socat or the cross compiler is m Evidence: red on main a93e27b (`AIENOS_CK_SCREEN: FAIL`, gate script and branch-built checker copied in), green at 2e44c0b standalone and in the full receipt above, green again at c2232ef (CodeQL widening fix in fbcon.c): `fb_check: PASS 800x600 format=bgrx scale=1 grid=100x60 scrolls=1 pixels=480000 mismatched=0 undecodable=0`. Logs: `~/workspace/evidence-out/L6C/` on the Spark (not committed). +## OSC_UNIT: OSC unit admission (C-only, Campaign 3, arch#158/#162) -> CK `OSC_UNIT` (scripts/qemu_ck_osc_unit_test.sh) + +Contract: aien-dev/aien-protocols PR #17, `specs/osc-unit-artifact/OSC_UNIT_ARTIFACT.md` (container v1, +frozen). Code: `native/kernel/artifact/osc_unit.{h,c}` (pure admission function, section 8.2 check order, +section 8.4 code scan with the decoder round trip), `native/kernel/core/osc_admit.{h,c}` (kernel policy and the +`osc_unit:` report lines). It reuses `argus/sha256.c` and `sig/ed25519.c` (strict verify, `S < L`); no new crypto. +Candidates reach it through the same boot-disk Store path as the P2 artifacts; a candidate that begins with the +magic `OSCUNIT\0` is judged by this module instead of the Binary Artifact v0 path. + +- Host conformance: `tests/test_osc_unit.c` runs every line of the frozen `expected.txt` (52), `lookups.txt` (6) + and `state.txt` (3) from `tests/fixtures/osc_unit` (provenance there) and requires the stated verdict; also + every single-byte flip and every truncation of the valid vector is refused. Part of `make test` and + `make sanitize`. +- QEMU gate: two boots (TEST-anchor qualification build; ordinary release build) of four vectors staged in the + sealed Store. Qualification: valid TEST unit ACCEPT with the frozen UnitDigest; OWNER unit UNTRUSTED_SIGNER; + bad signature; SVC word. Release: TEST unit TEST_SIGNER_IN_RELEASE (also for the bad-signature vector: class is + checked first); OWNER unit UNTRUSTED_SIGNER; SVC word CODE_INSTRUCTION; nothing accepted. The release image is + checked not to carry the TEST public key. +- Labels: QEMU (aarch64 virt), TEST signer, not physical. Admission only: nothing is mapped, launched or + scheduled (launch is a later cut; launch code 41 and `launch.txt` are not covered here). OWNER anchors are empty + until TRUST-1 provisions one. +- Kernel policy: capability domain 1 only (32-bit generation); the kernel holds no resource state yet, so any + pinned generation is refused CAP_GENERATION_STALE. + +Spec status: OSC_UNIT_ARTIFACT is v1 FROZEN (aien-protocols#17, 2026-10-08; bytes and vectors unchanged by the freeze). Passing the host vectors +is not evidence of the native loader; only the QEMU gate row exercises the in-kernel path, and nothing here is physical. + ## C3-1a console session: scripts/qemu_ck_console_test.sh -> CK `CONSOLE` (Campaign 3 cut C3-1a, C-only, no Rust-kernel counterpart) QEMU only. Hardware NOT_RUN: no physical serial port, xHCI controller, keyboard or SMMU run. diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 1941edb9..ac8f5024 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -469,8 +469,8 @@ CORE_C := $(BOOT)/efi_main.c $(BOOT)/efi_model.c $(BOOT)/efi_gop.c \ # P2 sealed-artifact loader: format/admission/receipt (artifact/), SHA-256 # (argus), Ed25519/SHA-512 (sig). Built into the core so every image carries # the loader; the TEST-anchor qualification build is CK_SEED0B_TEST_ANCHOR=1. -ART_C := $(KDIR)/core/artifact_loader.c $(KDIR)/artifact/format.c $(KDIR)/artifact/admission.c \ - $(KDIR)/artifact/receipt.c $(NATIVE)/argus/sha256.c $(NATIVE)/sig/sha512.c \ +ART_C := $(KDIR)/core/artifact_loader.c $(KDIR)/core/osc_admit.c $(KDIR)/artifact/format.c $(KDIR)/artifact/admission.c \ + $(KDIR)/artifact/receipt.c $(KDIR)/artifact/osc_unit.c $(NATIVE)/argus/sha256.c $(NATIVE)/sig/sha512.c \ $(NATIVE)/sig/ed25519.c $(NATIVE)/sig/ct.c ART_INC := -I$(KDIR)/svc/compat -I$(KDIR)/artifact -I$(NATIVE)/sig -I$(NATIVE)/argus CORE_C += $(ART_C) @@ -589,6 +589,10 @@ $(KOBJ)/kernel/core/artifact_loader.o: $(KDIR)/core/artifact_loader.c $(HDRS) $( @mkdir -p $(dir $@) $(KCC) $(KCFLAGS) $(ART_TRUST_FLAGS) -DAIENOS_COMMIT='"$(AIENOS_COMMIT)"' -c -o $@ $< +$(KOBJ)/kernel/core/osc_admit.o: $(KDIR)/core/osc_admit.c $(HDRS) $(ART_HDRS) $(OUT)/trust.stamp + @mkdir -p $(dir $@) + $(KCC) $(KCFLAGS) $(ART_TRUST_FLAGS) -c -o $@ $< + $(KOBJ)/%.o: $(NATIVE)/%.S @mkdir -p $(dir $@) $(KCC) $(KCFLAGS) -c -o $@ $< @@ -762,7 +766,7 @@ $(FULL_EFI): $(FULL_ELF) HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/mm/frames.c \ $(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c \ $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/acpi_dev.c -TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_entropy test_handoff test_fbcon \ +TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_osc_unit test_osc_admit test_entropy test_handoff test_fbcon \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ test_continuity_subject test_continuity_subject_provision HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) @@ -869,7 +873,7 @@ $(OUT)/host/ck_udp_echo: $(KDIR)/tools/ck_udp_echo.c # Artifact library host test and the in-tree C artifact tool (pack, sign with # TEST keys, negative corpus, bundle, receipt check) used by the QEMU gate. -ART_HOST_SRCS := $(KDIR)/artifact/format.c $(KDIR)/artifact/admission.c $(KDIR)/artifact/receipt.c \ +ART_HOST_SRCS := $(KDIR)/artifact/format.c $(KDIR)/artifact/admission.c $(KDIR)/artifact/receipt.c $(KDIR)/artifact/osc_unit.c \ $(NATIVE)/argus/sha256.c $(NATIVE)/sig/sha512.c $(NATIVE)/sig/ed25519.c $(NATIVE)/sig/ct.c ART_HOST_INC := -I$(KDIR)/artifact -I$(NATIVE)/sig -I$(NATIVE)/argus ART_HOST_DEPS := $(ART_HOST_SRCS) $(wildcard $(KDIR)/artifact/*.h) $(ART_HDRS) @@ -878,10 +882,27 @@ $(OUT)/host/test_artifact: $(KDIR)/tests/test_artifact.c $(ART_HOST_DEPS) $(KDIR @mkdir -p $(dir $@) $(HOSTCC) $(HCFLAGS) $(ART_HOST_INC) -o $@ $< $(ART_HOST_SRCS) +$(OUT)/host/test_osc_unit: $(KDIR)/tests/test_osc_unit.c $(ART_HOST_DEPS) $(KDIR)/tests/ck_test.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(ART_HOST_INC) -DOSC_FIXTURE_DIR='"$(abspath $(KDIR)/tests/fixtures/osc_unit)"' -o $@ $< $(ART_HOST_SRCS) + +$(OUT)/host-san/test_osc_unit: $(KDIR)/tests/test_osc_unit.c $(ART_HOST_DEPS) $(KDIR)/tests/ck_test.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) $(ART_HOST_INC) -DOSC_FIXTURE_DIR='"$(abspath $(KDIR)/tests/fixtures/osc_unit)"' -o $@ $< $(ART_HOST_SRCS) + $(OUT)/host-san/test_artifact: $(KDIR)/tests/test_artifact.c $(ART_HOST_DEPS) $(KDIR)/tests/ck_test.h @mkdir -p $(dir $@) $(HOSTCC) $(HCFLAGS) $(SANFLAGS) $(ART_HOST_INC) -o $@ $< $(ART_HOST_SRCS) +# osc_admit.c reporting (oversize Store entry): host test with ck_printf captured. +$(OUT)/host/test_osc_admit: $(KDIR)/tests/test_osc_admit.c $(KDIR)/core/osc_admit.c $(ART_HOST_DEPS) $(KDIR)/tests/ck_test.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(ART_HOST_INC) -I$(KDIR)/include -o $@ $< $(KDIR)/core/osc_admit.c $(ART_HOST_SRCS) + +$(OUT)/host-san/test_osc_admit: $(KDIR)/tests/test_osc_admit.c $(KDIR)/core/osc_admit.c $(ART_HOST_DEPS) $(KDIR)/tests/ck_test.h + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) $(ART_HOST_INC) -I$(KDIR)/include -o $@ $< $(KDIR)/core/osc_admit.c $(ART_HOST_SRCS) + # Continuity codec (C port cut 1, contract CONTINUITY_RECOVERY_CONTRACT.md, # aienos#222): host test plus TEST-only -D mutants, each of which must make # the test print its FAIL verdict. Mutant flags are refused under diff --git a/native/kernel/artifact/.crumb b/native/kernel/artifact/.crumb index 337ec3f2..f2813b2d 100644 --- a/native/kernel/artifact/.crumb +++ b/native/kernel/artifact/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-08T03:06:53Z", "compiler": "crumb-compile/1", - "digest": "sha256:efd08e41584e2cbb282cea72e620fa2db42c423c6b3211a91585affb92e34893", + "digest": "sha256:b7c024fa610faa124f2f26fd002815ea178595962647d544e98ea5a39735f214", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 4, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:75bc1b8597e886eca09d8b1a71d7fbf0428643ba09fd3632792100616fcf6dcd" + "files": 7, + "generated_at_commit": "527a980c361c87b144d368120e79df5f40d47926", + "source_tree": "sha256:2f960d8a13e9bdb120b2e6b930bd32f6eef5987d13ab71defaf704a9cba6fab4" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/artifact/osc_unit.c b/native/kernel/artifact/osc_unit.c new file mode 100644 index 00000000..5aa40a21 --- /dev/null +++ b/native/kernel/artifact/osc_unit.c @@ -0,0 +1,513 @@ +/* osc_unit.c -- OSCUNIT admission. See osc_unit.h and OSC_UNIT_ARTIFACT.md + * (aien-protocols PR #17). The check order below is section 8.2, one comment + * per step, because the first failure decides the result. */ +#include <string.h> +#include "osc_unit.h" +#include "aienos_sig.h" +#include "sha256.h" + +static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } +static uint32_t rd32(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 uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } + +static int all_zero(const uint8_t *p, size_t n) +{ + uint8_t acc = 0; + for (size_t i = 0; i < n; i++) + acc |= p[i]; + return acc == 0; +} + +static const char *const code_names[] = { + [0] = "OK", [1] = "TRUNCATED", [2] = "BAD_MAGIC", [3] = "CONTAINER_VERSION", [4] = "HEADER_FIELD", + [5] = "UNIT_FORMAT", [6] = "ABI_VERSION", [7] = "UNKNOWN_FLAGS", [8] = "TRAILING_BYTES", + [9] = "TOTAL_LENGTH", [10] = "SECTION_TABLE", [11] = "SECTION_BOUNDS", [12] = "SECTION_OVERLAP", + [13] = "SECTION_LAYOUT", [14] = "LIMIT_EXCEEDED", [15] = "IR_HASH_MISMATCH", + [16] = "CODE_HASH_MISMATCH", [17] = "ENTRY_HASH_MISMATCH", [18] = "CAPS_HASH_MISMATCH", + [19] = "IR_MALFORMED", [20] = "ENTRY_TABLE", [21] = "CAPS_TABLE", [22] = "SIGNER_CLASS", + [23] = "SIGNATURE_ALGORITHM", [24] = "TEST_SIGNER_IN_RELEASE", [25] = "UNTRUSTED_SIGNER", + [26] = "BAD_SIGNATURE", [27] = "CAP_DOMAIN_UNSUPPORTED", [28] = "CAP_GEN_NOT_REPRESENTABLE", + [29] = "CAP_GENERATION_STALE", [30] = "RESOURCE_UNAVAILABLE", [31] = "ENTRY_NAME", + [32] = "ENTRY_NAME_HASH", [33] = "ENTRY_NAME_DUPLICATE", [34] = "CODE_INSTRUCTION", +}; + +const char *osc_code_name(unsigned code) +{ + if (code < sizeof code_names / sizeof code_names[0] && code_names[code]) + return code_names[code]; + if (code == OSC_LAUNCH_BAD_ENTRY) + return "LAUNCH_BAD_ENTRY"; + if (code == OSC_LAUNCH_ARG_SHAPE) + return "LAUNCH_ARG_SHAPE"; + return "?"; +} + +/* --------------------------------------------------------------------------- + * Section 8.4: the OSC-emitted A64 subset. A port of the operation table + * (osc_a64.c:28-72), the variable-field masks (:76-93) and the decode + * round trip (:245-274) of omega at d64ccb3. A word passes when for some table + * entry (word & ~varmask(class)) == base AND the fields decoded from the word + * re-encode to the same word. The encoder refuses only the cases tested in + * round_trip_ok; every other field value re-encodes to itself. + * ------------------------------------------------------------------------- */ +enum { CL_RRR_SH, CL_RI, CL_R4, CL_MULH, CL_R3, CL_BFM, CL_CSINC, CL_MOV16, CL_MEM, CL_PAIR, + CL_B26, CL_BCOND, CL_CB, CL_BREG }; +enum { OP_PAIR_STP_OFF, OP_PAIR_STP_PRE, OP_PAIR_STP_POST, OP_PAIR_LDP_OFF, OP_PAIR_LDP_PRE, + OP_PAIR_LDP_POST, OP_OTHER }; + +struct a64_op { uint32_t base; uint8_t cls; uint8_t pairop; }; + +static const struct a64_op a64_ops[] = { + {0x8B000000u, CL_RRR_SH, OP_OTHER}, {0xCB000000u, CL_RRR_SH, OP_OTHER}, + {0xAB000000u, CL_RRR_SH, OP_OTHER}, {0xEB000000u, CL_RRR_SH, OP_OTHER}, + {0x91000000u, CL_RI, OP_OTHER}, {0xD1000000u, CL_RI, OP_OTHER}, + {0xB1000000u, CL_RI, OP_OTHER}, {0xF1000000u, CL_RI, OP_OTHER}, + {0x9B000000u, CL_R4, OP_OTHER}, {0x9B008000u, CL_R4, OP_OTHER}, + {0x9B407C00u, CL_MULH, OP_OTHER}, {0x9BC07C00u, CL_MULH, OP_OTHER}, + {0x9AC00C00u, CL_R3, OP_OTHER}, {0x9AC00800u, CL_R3, OP_OTHER}, + {0x9AC02000u, CL_R3, OP_OTHER}, {0x9AC02400u, CL_R3, OP_OTHER}, {0x9AC02800u, CL_R3, OP_OTHER}, + {0x8A000000u, CL_RRR_SH, OP_OTHER}, {0xAA000000u, CL_RRR_SH, OP_OTHER}, + {0xCA000000u, CL_RRR_SH, OP_OTHER}, {0xAA200000u, CL_RRR_SH, OP_OTHER}, + {0x93400000u, CL_BFM, OP_OTHER}, {0xD3400000u, CL_BFM, OP_OTHER}, + {0x9A800400u, CL_CSINC, OP_OTHER}, + {0xD2800000u, CL_MOV16, OP_OTHER}, {0x92800000u, CL_MOV16, OP_OTHER}, {0xF2800000u, CL_MOV16, OP_OTHER}, + {0xF9400000u, CL_MEM, OP_OTHER}, {0xF9000000u, CL_MEM, OP_OTHER}, + {0xA9000000u, CL_PAIR, OP_PAIR_STP_OFF}, {0xA9800000u, CL_PAIR, OP_PAIR_STP_PRE}, + {0xA8800000u, CL_PAIR, OP_PAIR_STP_POST}, {0xA9400000u, CL_PAIR, OP_PAIR_LDP_OFF}, + {0xA9C00000u, CL_PAIR, OP_PAIR_LDP_PRE}, {0xA8C00000u, CL_PAIR, OP_PAIR_LDP_POST}, + {0x14000000u, CL_B26, OP_OTHER}, {0x94000000u, CL_B26, OP_OTHER}, + {0x54000000u, CL_BCOND, OP_OTHER}, {0xB4000000u, CL_CB, OP_OTHER}, {0xB5000000u, CL_CB, OP_OTHER}, + {0xD63F0000u, CL_BREG, OP_OTHER}, {0xD65F0000u, CL_BREG, OP_OTHER}, + {0x38606800u, CL_R3, OP_OTHER}, /* ldrb (register) */ +}; + +static uint32_t cls_varmask(unsigned cls) +{ + switch (cls) { + case CL_RRR_SH: return 0x00DFFFFFu; + case CL_RI: return 0x007FFFFFu; + case CL_R4: return 0x001F7FFFu; + case CL_MULH: return 0x001F03FFu; + case CL_R3: return 0x001F03FFu; + case CL_BFM: return 0x003FFFFFu; + case CL_CSINC: return 0x001FF3FFu; + case CL_MOV16: return 0x007FFFFFu; + case CL_MEM: return 0x003FFFFFu; + case CL_PAIR: return 0x003FFFFFu; + case CL_B26: return 0x03FFFFFFu; + case CL_BCOND: return 0x00FFFFEFu; + case CL_CB: return 0x00FFFFFFu; + case CL_BREG: return 0x000003E0u; + default: return 0; + } +} + +/* The decoded fields re-encode to w unless the encoder refuses them. */ +static int round_trip_ok(const struct a64_op *op, uint32_t w) +{ + uint32_t rd = w & 31, rn = (w >> 5) & 31, ra = (w >> 10) & 31; + switch (op->cls) { + case CL_RRR_SH: return ((w >> 22) & 3) <= 2; /* LSL LSR ASR; ROR refused */ + case CL_CSINC: return ((w >> 12) & 15) <= 14; /* cond NV refused */ + case CL_BCOND: return (w & 15) <= 13; /* AL and NV refused */ + case CL_BREG: return rn != 31; /* branch register is x0..x30 only */ + case CL_PAIR: { + int wb = op->pairop == OP_PAIR_STP_PRE || op->pairop == OP_PAIR_STP_POST || + op->pairop == OP_PAIR_LDP_PRE || op->pairop == OP_PAIR_LDP_POST; + int load = op->pairop == OP_PAIR_LDP_OFF || op->pairop == OP_PAIR_LDP_PRE || + op->pairop == OP_PAIR_LDP_POST; + /* writeback base equal to a transfer register (rn is SP when 31, never equal) */ + if (wb && rn != 31 && (rn == rd || rn == ra)) + return 0; + if (load && rd == ra) /* Rt == Rt2, including XZR == XZR */ + return 0; + return 1; + } + default: return 1; + } +} + +int osc_a64_word_allowed(uint32_t w) +{ + /* brk: the compiler emits it only as the unreachable guard after a trap call, immediate = trap code. */ + if ((w & 0xFFE0001Fu) == 0xD4200000u) { + uint32_t imm = (w >> 5) & 0xFFFFu; + return imm >= 1 && imm <= 14; + } + for (size_t i = 0; i < sizeof a64_ops / sizeof a64_ops[0]; i++) { + const struct a64_op *op = &a64_ops[i]; + if ((w & ~cls_varmask(op->cls)) != op->base) + continue; + if (round_trip_ok(op, w)) + return 1; + } + return 0; +} + +/* --------------------------------------------------------------------------- + * Admission + * ------------------------------------------------------------------------- */ +struct sec { uint32_t off, len; const uint8_t *hash; }; + +static int name_charset_ok(const uint8_t *n, unsigned len) +{ + for (unsigned i = 0; i < len; i++) { + uint8_t c = n[i]; + int alpha = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'; + int digit = c >= '0' && c <= '9'; + if (!(alpha || (digit && i > 0))) + return 0; + } + return len >= 1; +} + +static void name_hash(const uint8_t *n, unsigned len, uint8_t out16[16]) +{ + static const char tag[] = "AIENOS-OSC-FN-NAME-V1"; /* + the NUL below */ + sha256_ctx c; + uint8_t d[32]; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)tag, sizeof tag); /* sizeof includes the NUL */ + sha256_update(&c, n, len); + sha256_final(&c, d); + memcpy(out16, d, 16); +} + +static int key_in(const uint8_t (*set)[32], unsigned n, const uint8_t fp[32], const uint8_t **key) +{ + for (unsigned i = 0; set && i < n; i++) { + uint8_t h[32]; + sha256_hash(set[i], 32, h); + if (memcmp(h, fp, 32) == 0) { + *key = set[i]; + return 1; + } + } + return 0; +} + +/* Ed25519 group order L, little endian. */ +static const uint8_t ed_L[32] = { + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10}; + +static int scalar_below_L(const uint8_t s[32]) +{ + for (int i = 31; i >= 0; i--) { + if (s[i] != ed_L[i]) + return s[i] < ed_L[i]; + } + return 0; /* equal to L */ +} + +unsigned osc_unit_admit(const uint8_t *f, size_t len, const struct osc_policy *pol, struct osc_accept *out) +{ + struct sec sec[5]; /* 1-based */ + uint32_t total; + unsigned ufv, cls, ncap = 0; + uint16_t nfn; + uint8_t digest[32]; + + if (!f || !pol) + return OSC_TRUNCATED; + if (pol->staging_max && len > pol->staging_max) + return OSC_RESOURCE_UNAVAILABLE; + /* 1 */ + if (len < 128) + return OSC_TRUNCATED; + /* 2: magic, container version, fixed header fields and reserved bytes, unit format, ABI, flags */ + if (memcmp(f, "OSCUNIT\0", 8) != 0) + return OSC_BAD_MAGIC; + if (rd16(f + 8) != 1) + return OSC_CONTAINER_VERSION; + if (rd16(f + 10) != 128 || rd32(f + 24) != 128 || rd16(f + 28) != 4 || rd16(f + 30) != 48 || + !all_zero(f + 34, 2) || !all_zero(f + 50, 2) || !all_zero(f + 52, 4) || !all_zero(f + 60, 4) || + !all_zero(f + 96, 32)) + return OSC_HEADER_FIELD; + ufv = rd16(f + 12); + if (ufv >= 32 || !((pol->unit_formats >> ufv) & 1u)) + return OSC_UNIT_FORMAT; + { + unsigned abi = rd16(f + 14); + if (abi >= 32 || !((pol->abi_versions >> abi) & 1u)) + return OSC_ABI_VERSION; + } + if (rd32(f + 16) != 0) + return OSC_UNKNOWN_FLAGS; + /* 3 */ + total = rd32(f + 20); + if (total < 384 || total > OSC_UNIT_MAX_BYTES) + return OSC_TOTAL_LENGTH; + if (len < total) + return OSC_TRUNCATED; + if (len > total) + return OSC_TRAILING_BYTES; + /* 4: the 320 header bytes are now in the file (total >= 384). */ + for (unsigned k = 1; k <= 4; k++) { + const uint8_t *e = f + 128 + (k - 1) * 48; + if (rd16(e) != k || rd16(e + 2) != 0 || !all_zero(e + 12, 4)) + return OSC_SECTION_TABLE; + sec[k].off = rd32(e + 4); + sec[k].len = rd32(e + 8); + sec[k].hash = e + 16; + } + { + uint64_t sigoff = (uint64_t)total - 64; + for (unsigned k = 1; k <= 4; k++) + if (sec[k].off < 320 || (uint64_t)sec[k].off + sec[k].len > sigoff) + return OSC_SECTION_BOUNDS; + for (unsigned a = 1; a <= 4; a++) + for (unsigned b = a + 1; b <= 4; b++) + if (sec[a].len && sec[b].len && sec[a].off < (uint64_t)sec[b].off + sec[b].len && + sec[b].off < (uint64_t)sec[a].off + sec[a].len) + return OSC_SECTION_OVERLAP; + uint64_t pos = 320; + for (unsigned k = 1; k <= 4; k++) { + uint64_t want = (pos + 15) / 16 * 16; + if (sec[k].off != want) + return OSC_SECTION_LAYOUT; + if (want > pos && !all_zero(f + pos, (size_t)(want - pos))) + return OSC_SECTION_LAYOUT; + pos = want + sec[k].len; + } + if (pos != sigoff) + return OSC_SECTION_LAYOUT; + } + /* 5: limits (every read below lies in a bounds-checked section) */ + nfn = rd16(f + 32); + { + uint32_t stack = rd32(f + 36); + uint64_t ticks = rd64(f + 40); + uint16_t pools = rd16(f + 48); + if (nfn < 1 || nfn > OSC_MAX_FUNCS) + return OSC_LIMIT_EXCEEDED; + if (stack < 4096 || stack > 1048576 || (stack % 16)) + return OSC_LIMIT_EXCEEDED; + if (ticks < 1 || ticks > 1000000000ull) + return OSC_LIMIT_EXCEEDED; + if (pools > 64) + return OSC_LIMIT_EXCEEDED; + if (sec[1].len < 8 || sec[1].len > 1048576) + return OSC_LIMIT_EXCEEDED; + if (sec[2].len < 4 || sec[2].len > 262144 || (sec[2].len % 4)) + return OSC_LIMIT_EXCEEDED; + if (sec[4].len >= 8) { + ncap = rd16(f + sec[4].off); + if (ncap > OSC_MAX_CAPS) + return OSC_LIMIT_EXCEEDED; + } + } + /* 6 */ + cls = rd16(f + 56); + if (cls != OSC_SIGNER_TEST && cls != OSC_SIGNER_OWNER) + return OSC_SIGNER_CLASS; + if (rd16(f + 58) != 1) + return OSC_SIGNATURE_ALGORITHM; + /* 7: section hashes in order */ + for (unsigned k = 1; k <= 4; k++) { + uint8_t h[32]; + sha256_hash(f + sec[k].off, sec[k].len, h); + if (memcmp(h, sec[k].hash, 32) != 0) + return OSC_IR_HASH_MISMATCH + (k - 1); + } + /* 8 */ + { + const uint8_t *ir = f + sec[1].off; + if (memcmp(ir, "OSC1IR\0", 7) != 0 || ir[7] != ufv) + return OSC_IR_MALFORMED; + } + /* 9: code scan */ + { + const uint8_t *code = f + sec[2].off; + for (uint32_t o = 0; o < sec[2].len; o += 4) + if (!osc_a64_word_allowed(rd32(code + o))) + return OSC_CODE_INSTRUCTION; + } + /* 10: entry table. Locals hold the parsed records until acceptance. */ + if (sec[3].len != 96u * nfn) + return OSC_ENTRY_TABLE; + { + uint64_t prev = 0; + int have_prev = 0; + for (unsigned i = 0; i < nfn; i++) { + const uint8_t *e = f + sec[3].off + 96u * i; + unsigned nregs = e[2], ret = e[3], nl; + if (rd16(e) != i) + return OSC_ENTRY_TABLE; + if (nregs > 6 || ret > 9 || !all_zero(e + 10, 2)) + return OSC_ENTRY_TABLE; + for (unsigned r = 0; r < 6; r++) { + unsigned kind = e[4 + r]; + if (r >= nregs) { + if (kind != 0) + return OSC_ENTRY_TABLE; + continue; + } + if (kind >= 1 && kind <= 9) + continue; + if (kind == 11 || kind == 12) { + if (ufv != 5 || r + 1 >= nregs || e[4 + r + 1] != 5) + return OSC_ENTRY_TABLE; + continue; + } + return OSC_ENTRY_TABLE; + } + uint32_t co = rd32(e + 12); + if ((co % 4) || co >= sec[2].len || (have_prev && co <= prev)) + return OSC_ENTRY_TABLE; + prev = co; + have_prev = 1; + nl = e[32]; + if (nl < 1 || nl > 63 || !all_zero(e + 33 + nl, 63 - nl) || !name_charset_ok(e + 33, nl)) + return OSC_ENTRY_NAME; + uint8_t nh[16]; + name_hash(e + 33, nl, nh); + if (memcmp(nh, e + 16, 16) != 0) + return OSC_ENTRY_NAME_HASH; + for (unsigned j = 0; j < i; j++) { + const uint8_t *q = f + sec[3].off + 96u * j; + if ((q[32] == nl && memcmp(q + 33, e + 33, nl) == 0) || memcmp(q + 16, e + 16, 16) == 0) + return OSC_ENTRY_NAME_DUPLICATE; + } + } + } + /* 11: capability table */ + if (sec[4].len != 8 + 64u * ncap) + return OSC_CAPS_TABLE; + if (!all_zero(f + sec[4].off + 2, 6)) + return OSC_CAPS_TABLE; + { + int have_prev = 0; + uint64_t prev = 0; + for (unsigned i = 0; i < ncap; i++) { + const uint8_t *e = f + sec[4].off + 8 + 64u * i; + unsigned kind = rd16(e), dom = rd16(e + 48); + uint32_t rid = rd32(e + 4), rights = rd32(e + 8), bk = rd32(e + 12), mops = rd32(e + 16); + uint64_t mbytes = rd64(e + 24), boff = rd64(e + 32), blen = rd64(e + 40); + if (!all_zero(e + 20, 4) || (kind != 2 && kind != 3) || rd16(e + 2) != 0 || rid == 0) + return OSC_CAPS_TABLE; + if (rights == 0 || (rights & ~63u) != 0 || mops == 0 || mbytes == 0) + return OSC_CAPS_TABLE; + if (kind == 3) { + if (bk != 1 || blen == 0 || mbytes > blen) + return OSC_CAPS_TABLE; + } else if (bk != 2 || boff != 0 || blen != 0) { + return OSC_CAPS_TABLE; + } + if ((dom != 1 && dom != 2) || rd16(e + 50) != 0 || !all_zero(e + 52, 4)) + return OSC_CAPS_TABLE; + uint64_t key = ((uint64_t)dom << 40) | ((uint64_t)kind << 32) | rid; + if (have_prev && key <= prev) + return OSC_CAPS_TABLE; + prev = key; + have_prev = 1; + } + } + /* UnitDigest = SHA256("AIENOS-OSC-UNIT-V1\0" || file[0..320)) */ + { + static const char tag[] = "AIENOS-OSC-UNIT-V1"; + sha256_ctx c; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)tag, sizeof tag); + sha256_update(&c, f, 320); + sha256_final(&c, digest); + } + /* 12 */ + if (cls == OSC_SIGNER_TEST && pol->release) + return OSC_TEST_SIGNER_IN_RELEASE; + /* 13: the key comes from the loader's anchor set of that class, never from the container */ + const uint8_t *key = 0; + if (cls == OSC_SIGNER_TEST ? !key_in(pol->test_anchors, pol->n_test, f + 64, &key) + : !key_in(pol->owner_anchors, pol->n_owner, f + 64, &key)) + return OSC_UNTRUSTED_SIGNER; + /* 14: canonical S < L, then pure Ed25519 over the domain-separated digest */ + /* scalar_below_L is defence in depth: the Ed25519 verifier also rejects a non-canonical S, so no test can separate them. */ + { + static const char tag[] = "AIENOS-OSC-UNIT-SIGNATURE-V1"; + uint8_t msg[sizeof tag + 32]; + const uint8_t *sig = f + total - 64; + memcpy(msg, tag, sizeof tag); + memcpy(msg + sizeof tag, digest, 32); + if (!scalar_below_L(sig + 32) || aienos_ed25519_verify(sig, msg, sizeof msg, key) != 0) + return OSC_BAD_SIGNATURE; + } + /* 15: capability policy, three whole passes */ + for (unsigned i = 0; i < ncap; i++) { + unsigned dom = rd16(f + sec[4].off + 8 + 64u * i + 48); + if (dom >= 32 || !((pol->cap_domains >> dom) & 1u)) + return OSC_CAP_DOMAIN_UNSUPPORTED; + } + for (unsigned i = 0; i < ncap; i++) { + const uint8_t *e = f + sec[4].off + 8 + 64u * i; + if (rd16(e + 48) == 1 && (rd64(e + 56) >> 32) != 0) + return OSC_CAP_GEN_NOT_REPRESENTABLE; + } + for (unsigned i = 0; i < ncap; i++) { + const uint8_t *e = f + sec[4].off + 8 + 64u * i; + uint64_t want = rd64(e + 56), cur = 0; + if (want == 0 || !pol->gen_lookup) + continue; + if (pol->gen_lookup(pol->ctx, rd16(e + 48), rd16(e), rd32(e + 4), &cur) != 0 || cur != want) + return OSC_CAP_GENERATION_STALE; + } + /* Build the accepted view (everything below was validated above). */ + struct osc_accept *a = out; + struct osc_accept local; + if (!a) + a = &local; + memset(a, 0, sizeof *a); + memcpy(a->unit_digest, digest, 32); + memcpy(a->ir_sha256, sec[1].hash, 32); + memcpy(a->code_sha256, sec[2].hash, 32); + memcpy(a->signer_fp, f + 64, 32); + a->unit_format = (uint16_t)ufv; + a->signer_class = (uint16_t)cls; + a->function_count = nfn; + a->pool_slots = rd16(f + 48); + a->cap_count = (uint16_t)ncap; + a->max_stack_bytes = rd32(f + 36); + a->cpu_ticks = rd64(f + 40); + a->ir_off = sec[1].off; a->ir_len = sec[1].len; + a->code_off = sec[2].off; a->code_len = sec[2].len; + for (unsigned i = 0; i < nfn; i++) { + const uint8_t *e = f + sec[3].off + 96u * i; + struct osc_entry *r = &a->entry[i]; + r->fn_index = rd16(e); + r->nregs = e[2]; + r->ret_kind = e[3]; + memcpy(r->reg_kind, e + 4, 6); + r->code_offset = rd32(e + 12); + memcpy(r->name_hash, e + 16, 16); + r->name_len = e[32]; + memcpy(r->name, e + 33, r->name_len); + r->name[r->name_len] = 0; + } + for (unsigned i = 0; i < ncap; i++) { + const uint8_t *e = f + sec[4].off + 8 + 64u * i; + struct osc_cap *c = &a->cap[i]; + c->resource_kind = rd16(e); + c->resource_id = rd32(e + 4); + c->rights = rd32(e + 8); + c->bounds_kind = rd32(e + 12); + c->max_operations = rd32(e + 16); + c->max_bytes = rd64(e + 24); + c->bounds_offset = rd64(e + 32); + c->bounds_length = rd64(e + 40); + c->domain = rd16(e + 48); + c->required_generation = rd64(e + 56); + } + /* 16 */ + if (pol->reserve && pol->reserve(pol->ctx, a) != 0) + return OSC_RESOURCE_UNAVAILABLE; + return OSC_OK; +} + +int osc_unit_lookup(const struct osc_accept *a, const char *name, size_t name_len) +{ + if (!a || !name) + return -1; + for (unsigned i = 0; i < a->function_count && i < OSC_MAX_FUNCS; i++) + if (a->entry[i].name_len == name_len && memcmp(a->entry[i].name, name, name_len) == 0) + return a->entry[i].fn_index; + return -1; +} diff --git a/native/kernel/artifact/osc_unit.h b/native/kernel/artifact/osc_unit.h new file mode 100644 index 00000000..bb87b201 --- /dev/null +++ b/native/kernel/artifact/osc_unit.h @@ -0,0 +1,116 @@ +/* osc_unit.h -- admission of OSC unit containers (OSCUNIT, container v1). + * + * Contract: aien-dev/aien-protocols PR #17, specs/osc-unit-artifact/ + * OSC_UNIT_ARTIFACT.md (section numbers below refer to it). This is a + * different format from Binary Artifact v0 (ck_artifact.h, ADR 0014); it + * reuses the in-tree SHA-256 (argus/sha256.c) and Ed25519 verify + * (sig/ed25519.c) and adds no crypto of its own. + * + * osc_unit_admit is a pure function of (bytes, policy): no allocation, no + * global state, no I/O, no unit code ever runs. Every offset and length is + * checked with 64-bit arithmetic on 32-bit fields before a byte it names is + * read. It returns 0 (accepted, *out filled) or the FIRST refusal code of the + * fixed check order of section 8.2. Admission is not launch: nothing here + * maps or runs code, and OWNER anchors are not provisioned until TRUST-1. + */ +#ifndef AIENOS_CK_OSC_UNIT_H +#define AIENOS_CK_OSC_UNIT_H +#include <stddef.h> +#include <stdint.h> + +#define OSC_MAX_FUNCS 32u +#define OSC_MAX_CAPS 16u +#define OSC_UNIT_MAX_BYTES 2097152u + +/* Section 8.3 refusal codes (v1). 0 is "accepted". */ +enum osc_code { + OSC_OK = 0, + OSC_TRUNCATED = 1, OSC_BAD_MAGIC = 2, OSC_CONTAINER_VERSION = 3, OSC_HEADER_FIELD = 4, + OSC_UNIT_FORMAT = 5, OSC_ABI_VERSION = 6, OSC_UNKNOWN_FLAGS = 7, OSC_TRAILING_BYTES = 8, + OSC_TOTAL_LENGTH = 9, OSC_SECTION_TABLE = 10, OSC_SECTION_BOUNDS = 11, OSC_SECTION_OVERLAP = 12, + OSC_SECTION_LAYOUT = 13, OSC_LIMIT_EXCEEDED = 14, OSC_IR_HASH_MISMATCH = 15, + OSC_CODE_HASH_MISMATCH = 16, OSC_ENTRY_HASH_MISMATCH = 17, OSC_CAPS_HASH_MISMATCH = 18, + OSC_IR_MALFORMED = 19, OSC_ENTRY_TABLE = 20, OSC_CAPS_TABLE = 21, OSC_SIGNER_CLASS = 22, + OSC_SIGNATURE_ALGORITHM = 23, OSC_TEST_SIGNER_IN_RELEASE = 24, OSC_UNTRUSTED_SIGNER = 25, + OSC_BAD_SIGNATURE = 26, OSC_CAP_DOMAIN_UNSUPPORTED = 27, OSC_CAP_GEN_NOT_REPRESENTABLE = 28, + OSC_CAP_GENERATION_STALE = 29, OSC_RESOURCE_UNAVAILABLE = 30, OSC_ENTRY_NAME = 31, + OSC_ENTRY_NAME_HASH = 32, OSC_ENTRY_NAME_DUPLICATE = 33, OSC_CODE_INSTRUCTION = 34, + OSC_LAUNCH_BAD_ENTRY = 40, OSC_LAUNCH_ARG_SHAPE = 41 +}; + +/* Stable spec name of a code ("?" for a number the spec does not define). */ +const char *osc_code_name(unsigned code); + +enum { OSC_SIGNER_TEST = 1, OSC_SIGNER_OWNER = 2 }; + +struct osc_entry { + uint16_t fn_index; + uint8_t nregs, ret_kind; + uint8_t reg_kind[6]; + uint32_t code_offset; + uint8_t name_hash[16]; + uint8_t name_len; + char name[64]; /* NUL terminated copy of the name */ +}; + +struct osc_cap { + uint16_t resource_kind; /* 2 channel, 3 object */ + uint32_t resource_id, rights, bounds_kind, max_operations; + uint64_t max_bytes, bounds_offset, bounds_length; + uint16_t domain; /* 1 kernel IPC table (32-bit generation), 2 hosted authority */ + uint64_t required_generation; +}; + +struct osc_accept { + uint8_t unit_digest[32]; /* UnitDigest, section 5.1: what execution and authorization bind to */ + uint8_t ir_sha256[32]; /* program identity, display only (section 5.2) */ + uint8_t code_sha256[32]; + uint8_t signer_fp[32]; + uint16_t unit_format, signer_class, function_count, pool_slots, cap_count; + uint32_t max_stack_bytes; + uint64_t cpu_ticks; + uint32_t ir_off, ir_len, code_off, code_len; /* into the admitted bytes */ + struct osc_entry entry[OSC_MAX_FUNCS]; + struct osc_cap cap[OSC_MAX_CAPS]; +}; + +/* Loader inputs (section 8.1): local, never from the container. */ +struct osc_policy { + int release; /* 1 release build (TEST signers refused), 0 qualification */ + const uint8_t (*test_anchors)[32]; /* raw Ed25519 public keys, class TEST */ + unsigned n_test; + const uint8_t (*owner_anchors)[32]; /* class OWNER; empty until TRUST-1 provisions it */ + unsigned n_owner; + uint32_t unit_formats; /* bit v set: unit_format_version v supported */ + uint32_t abi_versions; /* bit v set: runtime_abi_version v provided */ + uint32_t cap_domains; /* bit d set: capability domain d provided */ + /* Loader state for step 15(c). Returns 0 and the resource's current generation, or nonzero + * when the loader has no such resource (then a pinned request is STALE). NULL: the loader + * assumes pinned generations match (host conformance profile only; the kernel passes a + * callback). */ + int (*gen_lookup)(void *ctx, unsigned domain, unsigned kind, uint32_t id, uint64_t *current); + /* Staging maximum in bytes (0: none). A file longer than this is refused RESOURCE_UNAVAILABLE + * before step 1 (spec 8.2 step 16; the native Store holds at most CK_ART_MAX_BYTES). */ + size_t staging_max; + /* Step 16: nonzero refuses with RESOURCE_UNAVAILABLE. NULL: reservation succeeds. */ + int (*reserve)(void *ctx, const struct osc_accept *a); + void *ctx; +}; + +/* 0 accepted (*out filled), else the first refusal code. out may be NULL only if the caller + * wants the verdict alone; it is written only on acceptance. */ +unsigned osc_unit_admit(const uint8_t *f, size_t len, const struct osc_policy *pol, struct osc_accept *out); + +/* Section 6.1.1: exact, case-sensitive, equal-length name match; the hash is never consulted. + * Returns the fn_index, or -1 (the caller reports LAUNCH_BAD_ENTRY, 40). */ +int osc_unit_lookup(const struct osc_accept *a, const char *name, size_t name_len); + +/* Section 8.4 on one word: nonzero if the word is in the OSC-emitted A64 subset (class-mask + * match plus the decoder round trip; brk only with immediate 1..14). Exposed for tests. */ +int osc_a64_word_allowed(uint32_t w); + +/* Conformance profile of section 8.1 for unit format 1..5, ABI 1. */ +#define OSC_PROFILE_UNIT_FORMATS 0x3Eu /* bits 1..5 */ +#define OSC_PROFILE_ABI 0x2u /* bit 1 */ + +#endif diff --git a/native/kernel/artifact/osc_unit_test_anchor.h b/native/kernel/artifact/osc_unit_test_anchor.h new file mode 100644 index 00000000..c07ec892 --- /dev/null +++ b/native/kernel/artifact/osc_unit_test_anchor.h @@ -0,0 +1,14 @@ +/* osc_unit_test_anchor.h -- the THROWAWAY TEST public key of the OSC unit + * conformance vectors (aien-protocols PR #17, specs/osc-unit-artifact/keys/ + * test1.pub, derived from a fixed public label in tools/make-vectors.sh). + * It protects nothing. It is compiled only into the host test and into the + * TEST-anchor qualification image (CK_SEED0B_TEST_ANCHOR=1, banner "TEST + * ONLY"); never into a release image's anchor set. test_osc_unit.c checks + * these bytes against the fixture file. */ +#ifndef AIENOS_CK_OSC_UNIT_TEST_ANCHOR_H +#define AIENOS_CK_OSC_UNIT_TEST_ANCHOR_H +#include <stdint.h> +static const uint8_t osc_unit_test1_pk[32] = { + 0x1d, 0xaa, 0xec, 0x3d, 0xcf, 0x62, 0x52, 0x6c, 0x52, 0xf7, 0x1c, 0xdf, 0x3e, 0x67, 0x1a, 0x8d, + 0xe5, 0xc4, 0x6b, 0x0f, 0x9c, 0xa7, 0x3e, 0xe2, 0x0c, 0x01, 0x44, 0x5b, 0x8b, 0x34, 0x18, 0xee}; +#endif diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index ac92d830..e01ddaaf 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-08T04:37:23Z", + "compiled_at": "2026-10-08T11:00:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:b45aaa519ba778938f0e30eaa63431594398232c02f1c9f3dfdf970f3a6ed5a8", + "digest": "sha256:fd9456ff602f0eaa5937f04b46762e8dd4c2692707a6b78076b3da4667f4b4ff", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 31, - "generated_at_commit": "e387b502b58d9d15fd2751ec5ef4c60f93862244", - "source_tree": "sha256:9e9b75820a7bc682bb824c7c5e67ffecd35a9b354db74d10121c854567980b53" + "files": 33, + "generated_at_commit": "bde85f8671a90d33a46667a5d915cb948035954a", + "source_tree": "sha256:d28907a0a28224254c6733b54992ea98304de1cfb97262aa62869d65c3fcc4d5" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/artifact_loader.c b/native/kernel/core/artifact_loader.c index f40e773f..7551f2d9 100644 --- a/native/kernel/core/artifact_loader.c +++ b/native/kernel/core/artifact_loader.c @@ -28,6 +28,7 @@ #include "ck_internal.h" #include "pt.h" #include "ck_artifact.h" +#include "osc_admit.h" #include "sha256.h" #ifndef AIENOS_COMMIT @@ -1137,14 +1138,25 @@ static uint32_t disk_source(void) break; memcpy(name, a->name, nlen); name[nlen] = 0; + if (a->state == CK_DISK_ART_OK && a->bytes && ck_osc_is_unit(a->bytes, a->len)) { + /* OSCUNIT container (OSC_UNIT_ARTIFACT v1): admission verdict only, not a Binary Artifact. */ + ck_osc_candidate(name, a->bytes, a->len); + run_seen++; + continue; + } 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) + else if (a->state == CK_DISK_ART_TOO_LARGE || a->len > MAX_BATCH * PAGE) { + /* The Store never reads a too-large entry, so an OSCUNIT among them cannot be recognised: + * say so on the osc_unit: channel (RESOURCE_UNAVAILABLE) and keep the old refusal. */ + if (a->state == CK_DISK_ART_TOO_LARGE) + ck_osc_oversize(name, a->len); firmware_rejection(&rep, CKL_STAGING_TOO_LARGE); - else + } else firmware_rejection(&rep, CKL_FIRMWARE_READ); /* missing on disk */ finish_candidate(name); } + ck_osc_summary(); return count; } #else diff --git a/native/kernel/core/osc_admit.c b/native/kernel/core/osc_admit.c new file mode 100644 index 00000000..f0e80722 --- /dev/null +++ b/native/kernel/core/osc_admit.c @@ -0,0 +1,112 @@ +/* osc_admit.c -- kernel policy and reporting for OSC unit admission. + * + * Policy (OSC_UNIT_ARTIFACT.md section 8.1), all local to this build: + * - ordinary build: release mode, no TEST anchor, no OWNER anchor. A TEST + * unit is refused TEST_SIGNER_IN_RELEASE, an OWNER unit UNTRUSTED_SIGNER + * (OWNER anchors are not provisioned until TRUST-1). + * - qualification build (CK_SEED0B_TEST_ANCHOR=1): qualification mode with + * the throwaway TEST key of the conformance vectors as the only TEST + * anchor, announced TEST ONLY. OWNER anchors stay empty. + * - capability domain 1 only (kernel IPC table, 32-bit generations); the + * kernel holds no resource state at admission, so any pinned generation is + * STALE (the lookup finds nothing). No reservation: launch is a later cut. + * QEMU qualifies nothing physical. */ +#include "ck_internal.h" +#include "osc_admit.h" +#include "osc_unit.h" +#ifdef CK_SEED0B_TEST_ANCHOR +#include "osc_unit_test_anchor.h" +#endif + +static unsigned seen, accepted, refused, oversize, announced; +static struct osc_accept acc; /* ~5 KB: static, not on the kernel stack */ + +static int no_resource(void *ctx, unsigned domain, unsigned kind, uint32_t id, uint64_t *cur) +{ + (void)ctx; (void)domain; (void)kind; (void)id; (void)cur; + return 1; +} + +/* The Store and the native staging area hold at most 160 pages per artifact (= CK_ART_MAX_BYTES in + * svc/artifact_store.h, 655,360 bytes; spec 8.2 step 16). */ +#define OSC_STAGING_MAX (160u * 4096u) + +static void hex(char *out, const uint8_t *b, size_t n) +{ + static const char d[] = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { + out[2 * i] = d[b[i] >> 4]; + out[2 * i + 1] = d[b[i] & 15]; + } + out[2 * n] = 0; +} + +static void policy(struct osc_policy *p) +{ + memset(p, 0, sizeof *p); + p->unit_formats = OSC_PROFILE_UNIT_FORMATS; + p->abi_versions = OSC_PROFILE_ABI; + p->cap_domains = 1u << 1; + p->staging_max = OSC_STAGING_MAX; + p->gen_lookup = no_resource; +#ifdef CK_SEED0B_TEST_ANCHOR + static const uint8_t (*const ta)[32] = &osc_unit_test1_pk; + p->release = 0; + p->test_anchors = ta; + p->n_test = 1; +#else + p->release = 1; +#endif +} + +int ck_osc_is_unit(const uint8_t *b, size_t len) +{ + return b && len >= 8 && memcmp(b, "OSCUNIT\0", 8) == 0; +} + +unsigned ck_osc_candidate(const char *name, const uint8_t *b, size_t len) +{ + struct osc_policy p; + policy(&p); + if (!announced) { + announced = 1; +#ifdef CK_SEED0B_TEST_ANCHOR + ck_printf("osc_policy: mode=qualification test_anchors=1 (throwaway TEST key, TEST ONLY) owner_anchors=0 " + "domains=1 gen_width=32; QEMU, not physical\n"); +#else + ck_printf("osc_policy: mode=release test_anchors=0 owner_anchors=0 (none provisioned) " + "domains=1 gen_width=32; QEMU, not physical\n"); +#endif + } + unsigned rc = osc_unit_admit(b, len, &p, &acc); + seen++; + if (rc == OSC_OK) { + char d[65], i[65]; + hex(d, acc.unit_digest, 32); + hex(i, acc.ir_sha256, 32); + accepted++; + ck_printf("osc_unit: %s ACCEPT unit_digest=%s program_id=%s funcs=%u caps=%u signer=%s\n", name, d, i, + (unsigned)acc.function_count, (unsigned)acc.cap_count, + acc.signer_class == OSC_SIGNER_TEST ? "TEST" : "OWNER"); + } else { + refused++; + ck_printf("osc_unit: %s REFUSED code=%u name=%s\n", name, rc, osc_code_name(rc)); + } + return rc; +} + +void ck_osc_oversize(const char *name, uint64_t len) +{ + oversize++; + ck_printf("osc_unit: %s REFUSED code=%u name=%s reason=staging_too_large bytes=%llu limit=%u " + "(content not read, format unknown; refusal by the Store size limit)\n", + name, (unsigned)OSC_RESOURCE_UNAVAILABLE, osc_code_name(OSC_RESOURCE_UNAVAILABLE), + (unsigned long long)len, (unsigned)OSC_STAGING_MAX); +} + +void ck_osc_summary(void) +{ + if (seen || oversize) + ck_printf("osc_units: seen=%u accepted=%u refused=%u oversize=%u (admission only, nothing launched)\n", seen, accepted, + refused, oversize); +} diff --git a/native/kernel/core/osc_admit.h b/native/kernel/core/osc_admit.h new file mode 100644 index 00000000..c17e6a27 --- /dev/null +++ b/native/kernel/core/osc_admit.h @@ -0,0 +1,20 @@ +/* osc_admit.h -- kernel-side use of artifact/osc_unit.c: admission verdict + * for OSCUNIT containers that arrive through the artifact candidate path + * (boot disk Store). Admission only: nothing is mapped or launched. */ +#ifndef AIENOS_CK_OSC_ADMIT_H +#define AIENOS_CK_OSC_ADMIT_H +#include <stddef.h> +#include <stdint.h> + +/* Nonzero if the bytes begin with the container magic "OSCUNIT\0". */ +int ck_osc_is_unit(const uint8_t *b, size_t len); +/* Admit one container and print its machine-readable "osc_unit:" line. Returns 0 accepted, + * else the refusal code. */ +unsigned ck_osc_candidate(const char *name, const uint8_t *b, size_t len); +/* A Store entry the Store refused as too large (RESOURCE_UNAVAILABLE) is never read, so its format is + * unknown: print an "osc_unit:" refusal line for it and count it as oversize. The refusal itself stays + * with the old firmware path. */ +void ck_osc_oversize(const char *name, uint64_t len); +/* Print the "osc_units:" summary line if any OSC candidate or oversize entry was seen. */ +void ck_osc_summary(void); +#endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index b22fec47..83371f36 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T11:55:32Z", + "compiled_at": "2026-10-08T11:03:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:0fd32ca401eef461e59a9ac01b7879ce928c8057e8c2843d934772af8933e1c7", + "digest": "sha256:1b900c32e47ab5317c6fc9ed443f06496eb9c261857f0c7eec3cd65c97eaddcf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 26, - "generated_at_commit": "788425caf3125f809254efb433fd5bc036eb6a59", - "source_tree": "sha256:d9c30071c4af9fad1e7e9dfad14a3a5a7cc29c03f24c12bf3056df39e36dad3b" + "files": 92, + "generated_at_commit": "1ab797d6c0c42fee6b5fbebebaa81e1e86a55609", + "source_tree": "sha256:c811e379c067009dd45d383be462e93c29107473dc28b22abb8a62f342d69042" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/fixtures/osc_unit/PROVENANCE.txt b/native/kernel/tests/fixtures/osc_unit/PROVENANCE.txt new file mode 100644 index 00000000..1531bc79 --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/PROVENANCE.txt @@ -0,0 +1,6 @@ +OSC unit container conformance vectors, copied unchanged (README.md omitted) from +aien-dev/aien-protocols main at 6661009 (PR #17 merged), specs/osc-unit-artifact/{vectors,keys}; byte-identical to PR #17 head e6669cc, where r49 to r51 and a06 (the declared-limit bound vectors) were added +(contract OSC_UNIT_ARTIFACT.md, container version 1, frozen v1). +Both keys are THROWAWAY TEST keys derived from fixed labels in that spec's +tools/make-vectors.sh. They protect nothing and must never enter a real anchor set. +launch.txt (launch code 41) is copied but not run by this admission test: launch is a separate cut. diff --git a/native/kernel/tests/fixtures/osc_unit/keys/owner1.pub b/native/kernel/tests/fixtures/osc_unit/keys/owner1.pub new file mode 100644 index 00000000..1e9369e3 --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/keys/owner1.pub @@ -0,0 +1 @@ +fa8821e7d5febe2e6c7da3ffb03577ce50ba434a19d8e7517f6084586f2f42f1 diff --git a/native/kernel/tests/fixtures/osc_unit/keys/test1.pub b/native/kernel/tests/fixtures/osc_unit/keys/test1.pub new file mode 100644 index 00000000..270418c9 --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/keys/test1.pub @@ -0,0 +1 @@ +1daaec3dcf62526c52f71cdf3e671a8de5c46b0f9ca73ee20c01445b8b3418ee diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a01_valid_min.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a01_valid_min.unit new file mode 100644 index 00000000..7a021675 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a01_valid_min.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a02_valid_caps_kernel_domain.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a02_valid_caps_kernel_domain.unit new file mode 100644 index 00000000..67d6278c Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a02_valid_caps_kernel_domain.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a03_valid_hosted_domain.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a03_valid_hosted_domain.unit new file mode 100644 index 00000000..3238cd73 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a03_valid_hosted_domain.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a04_valid_owner_class.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a04_valid_owner_class.unit new file mode 100644 index 00000000..67023f15 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a04_valid_owner_class.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a05_valid_cells_entry.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a05_valid_cells_entry.unit new file mode 100644 index 00000000..863b54c2 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a05_valid_cells_entry.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/a06_valid_limits_at_max.unit b/native/kernel/tests/fixtures/osc_unit/vectors/a06_valid_limits_at_max.unit new file mode 100644 index 00000000..c06da32d Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/a06_valid_limits_at_max.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/expected.txt b/native/kernel/tests/fixtures/osc_unit/vectors/expected.txt new file mode 100644 index 00000000..cf08ca3a --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/vectors/expected.txt @@ -0,0 +1,58 @@ +# vector mode domains anchors verdict (anchors: T TEST anchor present, O OWNER anchor present, - none) +a01_valid_min.unit qualification 1 T ACCEPT unit_digest=c21c7d9b92433f29d588bd14eab2be4754fe39c05c7a926f1cb29b4388c68787 program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +a02_valid_caps_kernel_domain.unit qualification 1 T ACCEPT unit_digest=80a35e189253582322cd94ebbacf15c4e8fc48545fd5ee932c6d2af4386ddbfe program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +a03_valid_hosted_domain.unit qualification 1,2 T ACCEPT unit_digest=0c77186537f7cee304a898849b07f77426b1fe103f4adbd67f533c73b5aee8c4 program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +a04_valid_owner_class.unit release 1 O ACCEPT unit_digest=4340f53bb068015385b24114f89ffc4e584bc12afbd7afe3a7de12a60ba3b19d program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +r01_bad_magic.unit qualification 1 T REFUSED BAD_MAGIC 2 +r02_container_version_2.unit qualification 1 T REFUSED CONTAINER_VERSION 3 +r03_unit_format_6.unit qualification 1 T REFUSED UNIT_FORMAT 5 +r04_abi_version_2.unit qualification 1 T REFUSED ABI_VERSION 6 +r05_unknown_flags.unit qualification 1 T REFUSED UNKNOWN_FLAGS 7 +r06_header_field_reserved.unit qualification 1 T REFUSED HEADER_FIELD 4 +r07_truncated_file.unit qualification 1 T REFUSED TRUNCATED 1 +r08_section_bounds.unit qualification 1 T REFUSED SECTION_BOUNDS 11 +r09_section_overlap.unit qualification 1 T REFUSED SECTION_OVERLAP 12 +r10_section_layout_offset.unit qualification 1 T REFUSED SECTION_LAYOUT 13 +r11_too_many_functions.unit qualification 1 T REFUSED LIMIT_EXCEEDED 14 +r12_code_hash_mismatch.unit qualification 1 T REFUSED CODE_HASH_MISMATCH 16 +r13_ir_hash_mismatch.unit qualification 1 T REFUSED IR_HASH_MISMATCH 15 +r14_bad_signature.unit qualification 1 T REFUSED BAD_SIGNATURE 26 +r15_ir_version_mismatch.unit qualification 1 T REFUSED IR_MALFORMED 19 +r16_entry_table_unaligned.unit qualification 1 T REFUSED ENTRY_TABLE 20 +r17_too_many_functions.unit qualification 1 T REFUSED LIMIT_EXCEEDED 14 +r18_gen_not_representable.unit qualification 1 T REFUSED CAP_GEN_NOT_REPRESENTABLE 28 +r19_test_signer_release.unit release 1 T REFUSED TEST_SIGNER_IN_RELEASE 24 +r20_domain_unsupported.unit qualification 1 T REFUSED CAP_DOMAIN_UNSUPPORTED 27 +r21_untrusted_owner_signer.unit release 1 - REFUSED UNTRUSTED_SIGNER 25 +r22_trailing_byte.unit qualification 1 T REFUSED TRAILING_BYTES 8 +r23_entry_name_charset.unit qualification 1 T REFUSED ENTRY_NAME 31 +r24_entry_name_empty.unit qualification 1 T REFUSED ENTRY_NAME 31 +r25_entry_name_too_long.unit qualification 1 T REFUSED ENTRY_NAME 31 +r26_entry_name_padding.unit qualification 1 T REFUSED ENTRY_NAME 31 +r27_entry_name_duplicate.unit qualification 1 T REFUSED ENTRY_NAME_DUPLICATE 33 +r28_entry_hash_shared_other_name.unit qualification 1 T REFUSED ENTRY_NAME_HASH 32 +a05_valid_cells_entry.unit qualification 1 T ACCEPT unit_digest=5b88448cb61fc22a3e4cb235471fd70904f3ee786b15eeb5d190a9982a266c63 program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +r29_total_length_low.unit qualification 1 T REFUSED TOTAL_LENGTH 9 +r30_section_table_kind.unit qualification 1 T REFUSED SECTION_TABLE 10 +r31_entry_hash_mismatch.unit qualification 1 T REFUSED ENTRY_HASH_MISMATCH 17 +r32_caps_hash_mismatch.unit qualification 1 T REFUSED CAPS_HASH_MISMATCH 18 +r33_caps_table_bad_rights.unit qualification 1 T REFUSED CAPS_TABLE 21 +r34_signer_class_3.unit qualification 1 T REFUSED SIGNER_CLASS 22 +r35_signature_algorithm_2.unit qualification 1 T REFUSED SIGNATURE_ALGORITHM 23 +r36_domain_before_generation.unit qualification 1 T REFUSED CAP_DOMAIN_UNSUPPORTED 27 +r37_code_svc.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r38_code_hvc.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r39_code_smc.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r40_code_msr_sysreg.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r41_code_mrs_sysreg.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r42_code_brk_imm0.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r43_code_brk_imm15.unit qualification 1 T REFUSED CODE_INSTRUCTION 34 +r44_caps_disorder.unit qualification 1 T REFUSED CAPS_TABLE 21 +r45_signature_s_not_canonical.unit qualification 1 T REFUSED BAD_SIGNATURE 26 +r46_section_bounds_u32_wrap.unit qualification 1 T REFUSED SECTION_BOUNDS 11 +r47_slice_kind_in_format_4.unit qualification 1 T REFUSED ENTRY_TABLE 20 +r48_entry_hash_shared_case_variant.unit qualification 1 T REFUSED ENTRY_NAME_HASH 32 +r49_cpu_ticks_over_max.unit qualification 1 T REFUSED LIMIT_EXCEEDED 14 +r50_stack_over_max.unit qualification 1 T REFUSED LIMIT_EXCEEDED 14 +r51_pool_slots_over_max.unit qualification 1 T REFUSED LIMIT_EXCEEDED 14 +a06_valid_limits_at_max.unit qualification 1 T ACCEPT unit_digest=bb7ee3b6199fcfcbf058eecddaa7e1e1d0e06be1edbb6ae555812a10732a689d program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/launch.txt b/native/kernel/tests/fixtures/osc_unit/vectors/launch.txt new file mode 100644 index 00000000..e69c2a58 --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/vectors/launch.txt @@ -0,0 +1,17 @@ +# kinds args verdict (kinds: 1 bool 2 u8 3 u16 4 u32 5 u64 6 i8 7 i16 8 i32 9 i64 11 bytes 12 cells) +11,5 0x1000,16 OK +11,5 0,0 OK +5,5 7,9 OK +11,5 0x1000 REFUSED LAUNCH_ARG_SHAPE 41 +11,5 0,4 REFUSED LAUNCH_ARG_SHAPE 41 +12,5 0x1004,2 REFUSED LAUNCH_ARG_SHAPE 41 +12,5 0x1000,0x2000000000000000 REFUSED LAUNCH_ARG_SHAPE 41 +11,5 0xfffffffffffffff0,32 REFUSED LAUNCH_ARG_SHAPE 41 +11,5,11,5 0x1000,16,0x1008,16 OK +12,5,11,5 0x1000,2,0x1008,8 REFUSED LAUNCH_ARG_SHAPE 41 +12,5,12,5 0x1000,2,0x1008,2 REFUSED LAUNCH_ARG_SHAPE 41 +12,5,12,5 0x1000,2,0x1010,2 OK +12,5,11,5 0x1000,0,0x1000,8 OK +2,5 256,0 REFUSED LAUNCH_ARG_SHAPE 41 +6,5 0xffffffffffffff80,0 OK +6,5 0x80,0 REFUSED LAUNCH_ARG_SHAPE 41 diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/lookups.txt b/native/kernel/tests/fixtures/osc_unit/vectors/lookups.txt new file mode 100644 index 00000000..08e09874 --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/vectors/lookups.txt @@ -0,0 +1,7 @@ +# vector mode domains anchors name verdict +a01_valid_min.unit qualification 1 T add ACCEPT fn_index=0 +a01_valid_min.unit qualification 1 T first_byte ACCEPT fn_index=1 +a01_valid_min.unit qualification 1 T ADD REFUSED LAUNCH_BAD_ENTRY 40 +a01_valid_min.unit qualification 1 T ad REFUSED LAUNCH_BAD_ENTRY 40 +a01_valid_min.unit qualification 1 T first_byt REFUSED LAUNCH_BAD_ENTRY 40 +a01_valid_min.unit qualification 1 T missing REFUSED LAUNCH_BAD_ENTRY 40 diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r01_bad_magic.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r01_bad_magic.unit new file mode 100644 index 00000000..ac3932c4 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r01_bad_magic.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r02_container_version_2.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r02_container_version_2.unit new file mode 100644 index 00000000..936685bf Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r02_container_version_2.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r03_unit_format_6.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r03_unit_format_6.unit new file mode 100644 index 00000000..485b0401 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r03_unit_format_6.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r04_abi_version_2.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r04_abi_version_2.unit new file mode 100644 index 00000000..13a8584d Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r04_abi_version_2.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r05_unknown_flags.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r05_unknown_flags.unit new file mode 100644 index 00000000..086bef40 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r05_unknown_flags.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r06_header_field_reserved.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r06_header_field_reserved.unit new file mode 100644 index 00000000..56a3265b Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r06_header_field_reserved.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r07_truncated_file.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r07_truncated_file.unit new file mode 100644 index 00000000..3b0ef8e1 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r07_truncated_file.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r08_section_bounds.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r08_section_bounds.unit new file mode 100644 index 00000000..18f5a954 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r08_section_bounds.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r09_section_overlap.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r09_section_overlap.unit new file mode 100644 index 00000000..e73c50d4 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r09_section_overlap.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r10_section_layout_offset.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r10_section_layout_offset.unit new file mode 100644 index 00000000..405ceca5 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r10_section_layout_offset.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r11_too_many_functions.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r11_too_many_functions.unit new file mode 100644 index 00000000..42cfbf51 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r11_too_many_functions.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r12_code_hash_mismatch.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r12_code_hash_mismatch.unit new file mode 100644 index 00000000..d1c8052c Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r12_code_hash_mismatch.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r13_ir_hash_mismatch.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r13_ir_hash_mismatch.unit new file mode 100644 index 00000000..257c30fb Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r13_ir_hash_mismatch.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r14_bad_signature.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r14_bad_signature.unit new file mode 100644 index 00000000..57400b37 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r14_bad_signature.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r15_ir_version_mismatch.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r15_ir_version_mismatch.unit new file mode 100644 index 00000000..ea451b4f Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r15_ir_version_mismatch.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r16_entry_table_unaligned.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r16_entry_table_unaligned.unit new file mode 100644 index 00000000..43bbac96 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r16_entry_table_unaligned.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r17_too_many_functions.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r17_too_many_functions.unit new file mode 100644 index 00000000..2cb08e55 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r17_too_many_functions.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r18_gen_not_representable.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r18_gen_not_representable.unit new file mode 100644 index 00000000..4531fc63 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r18_gen_not_representable.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r19_test_signer_release.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r19_test_signer_release.unit new file mode 100644 index 00000000..7a021675 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r19_test_signer_release.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r20_domain_unsupported.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r20_domain_unsupported.unit new file mode 100644 index 00000000..3238cd73 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r20_domain_unsupported.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r21_untrusted_owner_signer.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r21_untrusted_owner_signer.unit new file mode 100644 index 00000000..67023f15 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r21_untrusted_owner_signer.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r22_trailing_byte.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r22_trailing_byte.unit new file mode 100644 index 00000000..c6e412a8 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r22_trailing_byte.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r23_entry_name_charset.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r23_entry_name_charset.unit new file mode 100644 index 00000000..a2e0803c Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r23_entry_name_charset.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r24_entry_name_empty.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r24_entry_name_empty.unit new file mode 100644 index 00000000..0caad489 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r24_entry_name_empty.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r25_entry_name_too_long.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r25_entry_name_too_long.unit new file mode 100644 index 00000000..d8d84975 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r25_entry_name_too_long.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r26_entry_name_padding.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r26_entry_name_padding.unit new file mode 100644 index 00000000..0e8f2246 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r26_entry_name_padding.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r27_entry_name_duplicate.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r27_entry_name_duplicate.unit new file mode 100644 index 00000000..b3bdf488 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r27_entry_name_duplicate.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r28_entry_hash_shared_other_name.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r28_entry_hash_shared_other_name.unit new file mode 100644 index 00000000..fa085a0b Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r28_entry_hash_shared_other_name.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r29_total_length_low.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r29_total_length_low.unit new file mode 100644 index 00000000..1411ee25 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r29_total_length_low.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r30_section_table_kind.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r30_section_table_kind.unit new file mode 100644 index 00000000..14880452 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r30_section_table_kind.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r31_entry_hash_mismatch.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r31_entry_hash_mismatch.unit new file mode 100644 index 00000000..920239a5 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r31_entry_hash_mismatch.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r32_caps_hash_mismatch.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r32_caps_hash_mismatch.unit new file mode 100644 index 00000000..4f7d97bd Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r32_caps_hash_mismatch.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r33_caps_table_bad_rights.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r33_caps_table_bad_rights.unit new file mode 100644 index 00000000..063ddaf4 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r33_caps_table_bad_rights.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r34_signer_class_3.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r34_signer_class_3.unit new file mode 100644 index 00000000..b0c35e60 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r34_signer_class_3.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r35_signature_algorithm_2.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r35_signature_algorithm_2.unit new file mode 100644 index 00000000..4066d0bd Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r35_signature_algorithm_2.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r36_domain_before_generation.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r36_domain_before_generation.unit new file mode 100644 index 00000000..52952cab Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r36_domain_before_generation.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r37_code_svc.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r37_code_svc.unit new file mode 100644 index 00000000..66e1c078 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r37_code_svc.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r38_code_hvc.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r38_code_hvc.unit new file mode 100644 index 00000000..aff0a788 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r38_code_hvc.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r39_code_smc.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r39_code_smc.unit new file mode 100644 index 00000000..e84935d3 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r39_code_smc.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r40_code_msr_sysreg.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r40_code_msr_sysreg.unit new file mode 100644 index 00000000..60f40a47 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r40_code_msr_sysreg.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r41_code_mrs_sysreg.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r41_code_mrs_sysreg.unit new file mode 100644 index 00000000..4c68dd2b Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r41_code_mrs_sysreg.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r42_code_brk_imm0.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r42_code_brk_imm0.unit new file mode 100644 index 00000000..53132df7 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r42_code_brk_imm0.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r43_code_brk_imm15.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r43_code_brk_imm15.unit new file mode 100644 index 00000000..13c99cb4 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r43_code_brk_imm15.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r44_caps_disorder.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r44_caps_disorder.unit new file mode 100644 index 00000000..f1544013 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r44_caps_disorder.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r45_signature_s_not_canonical.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r45_signature_s_not_canonical.unit new file mode 100644 index 00000000..e8f07235 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r45_signature_s_not_canonical.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r46_section_bounds_u32_wrap.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r46_section_bounds_u32_wrap.unit new file mode 100644 index 00000000..f87b72d4 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r46_section_bounds_u32_wrap.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r47_slice_kind_in_format_4.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r47_slice_kind_in_format_4.unit new file mode 100644 index 00000000..0fc0d117 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r47_slice_kind_in_format_4.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r48_entry_hash_shared_case_variant.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r48_entry_hash_shared_case_variant.unit new file mode 100644 index 00000000..a4eb7eb2 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r48_entry_hash_shared_case_variant.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r49_cpu_ticks_over_max.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r49_cpu_ticks_over_max.unit new file mode 100644 index 00000000..c69b74f0 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r49_cpu_ticks_over_max.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r50_stack_over_max.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r50_stack_over_max.unit new file mode 100644 index 00000000..8254d7e2 Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r50_stack_over_max.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/r51_pool_slots_over_max.unit b/native/kernel/tests/fixtures/osc_unit/vectors/r51_pool_slots_over_max.unit new file mode 100644 index 00000000..2d549e6d Binary files /dev/null and b/native/kernel/tests/fixtures/osc_unit/vectors/r51_pool_slots_over_max.unit differ diff --git a/native/kernel/tests/fixtures/osc_unit/vectors/state.txt b/native/kernel/tests/fixtures/osc_unit/vectors/state.txt new file mode 100644 index 00000000..234fb83a --- /dev/null +++ b/native/kernel/tests/fixtures/osc_unit/vectors/state.txt @@ -0,0 +1,4 @@ +# vector mode domains anchors state verdict +a02_valid_caps_kernel_domain.unit qualification 1 T gens=1:3:1=7 ACCEPT unit_digest=80a35e189253582322cd94ebbacf15c4e8fc48545fd5ee932c6d2af4386ddbfe program_id=084803c232dc44effcc44704c9ed0600dec1231451e0b76693633e98a48d1a1d +a02_valid_caps_kernel_domain.unit qualification 1 T gens=1:3:1=8 REFUSED CAP_GENERATION_STALE 29 +a01_valid_min.unit qualification 1 T noreserve REFUSED RESOURCE_UNAVAILABLE 30 diff --git a/native/kernel/tests/test_osc_admit.c b/native/kernel/tests/test_osc_admit.c new file mode 100644 index 00000000..1bee972f --- /dev/null +++ b/native/kernel/tests/test_osc_admit.c @@ -0,0 +1,33 @@ +/* test_osc_admit.c -- host test for the kernel reporting in core/osc_admit.c. + * Covers the one case the QEMU gate cannot build: a Store entry over the Store size limit is never read + * (the Store writer refuses to make one), so the loader cannot tell whether it is an OSCUNIT. It must + * still say, on the osc_unit: channel, that it was refused RESOURCE_UNAVAILABLE, and count it as oversize. + * ck_printf is replaced by a buffer so the exact lines can be checked. */ +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include "ck_test.h" +#include "osc_admit.h" + +static char out[4096]; +static size_t outn; + +void ck_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int n = vsnprintf(out + outn, sizeof out - outn, fmt, ap); + va_end(ap); + if (n > 0) outn += (size_t)n; +} + +int main(void) +{ + ck_osc_oversize("big.unit", 667648); + CHECK(strstr(out, "osc_unit: big.unit REFUSED code=30 name=RESOURCE_UNAVAILABLE reason=staging_too_large " + "bytes=667648 limit=655360 ") != NULL); + outn = 0; out[0] = 0; + ck_osc_summary(); + CHECK(strstr(out, "osc_units: seen=0 accepted=0 refused=0 oversize=1 (admission only, nothing launched)\n") != NULL); + return ck_t_verdict("test_osc_admit"); +} diff --git a/native/kernel/tests/test_osc_unit.c b/native/kernel/tests/test_osc_unit.c new file mode 100644 index 00000000..4ac0077d --- /dev/null +++ b/native/kernel/tests/test_osc_unit.c @@ -0,0 +1,297 @@ +/* test_osc_unit.c -- host conformance harness for artifact/osc_unit.c. + * Runs every line of the frozen OSC_UNIT_ARTIFACT v1 vector files (copied to + * tests/fixtures/osc_unit, provenance there): expected.txt (container + * verdicts), lookups.txt (exact-name lookups) and state.txt (loader-state + * scenarios, codes 29 and 30), and requires the stated verdict byte for byte. + * Keys are the spec's THROWAWAY TEST keys. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "ck_test.h" +#include "osc_unit.h" +#include "osc_unit_test_anchor.h" +#include "sha256.h" + +#ifndef OSC_FIXTURE_DIR +#define OSC_FIXTURE_DIR "native/kernel/tests/fixtures/osc_unit" +#endif + +static const char *fixdir = OSC_FIXTURE_DIR; +static uint8_t key_test[32], key_owner[32]; + +static int hexval(int c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1; } + +static int load_key(const char *name, uint8_t out[32]) +{ + char path[512], line[256]; + snprintf(path, sizeof path, "%s/keys/%s", fixdir, name); + FILE *f = fopen(path, "r"); + if (!f) return 0; + if (!fgets(line, sizeof line, f)) { fclose(f); return 0; } + fclose(f); + for (int i = 0; i < 32; i++) { + int h = hexval(line[2 * i]), l = hexval(line[2 * i + 1]); + if (h < 0 || l < 0) return 0; + out[i] = (uint8_t)(h * 16 + l); + } + return 1; +} + +static uint8_t *slurp(const char *path, size_t *n) +{ + FILE *f = fopen(path, "rb"); + if (!f) return 0; + fseek(f, 0, SEEK_END); + long l = ftell(f); + fseek(f, 0, SEEK_SET); + uint8_t *b = malloc(l > 0 ? (size_t)l : 1); + if (b && fread(b, 1, (size_t)l, f) != (size_t)l) { free(b); b = 0; } + fclose(f); + *n = b ? (size_t)l : 0; + return b; +} + +static void hex(const uint8_t *p, size_t n, char *out) +{ + for (size_t i = 0; i < n; i++) sprintf(out + 2 * i, "%02x", p[i]); +} + +/* loader-state scenario */ +struct state { int noreserve; int have_gens; unsigned dom[8], kind[8]; uint32_t id[8]; uint64_t gen[8]; int n; }; + +static int st_lookup(void *ctx, unsigned dom, unsigned kind, uint32_t id, uint64_t *cur) +{ + struct state *s = ctx; + for (int i = 0; i < s->n; i++) + if (s->dom[i] == dom && s->kind[i] == kind && s->id[i] == id) { *cur = s->gen[i]; return 0; } + return 1; +} +static int st_reserve(void *ctx, const struct osc_accept *a) { (void)a; return ((struct state *)ctx)->noreserve; } + +static struct osc_policy policy_for(const char *mode, const char *domains, const char *anchors, struct state *st) +{ + static uint8_t t[1][32], o[1][32]; + struct osc_policy p; + memset(&p, 0, sizeof p); + p.release = strcmp(mode, "release") == 0; + memcpy(t[0], key_test, 32); + memcpy(o[0], key_owner, 32); + if (strchr(anchors, 'T')) { p.test_anchors = t; p.n_test = 1; } + if (strchr(anchors, 'O')) { p.owner_anchors = o; p.n_owner = 1; } + p.unit_formats = OSC_PROFILE_UNIT_FORMATS; + p.abi_versions = OSC_PROFILE_ABI; + for (const char *c = domains; *c; c++) + if (*c >= '0' && *c <= '9') p.cap_domains |= 1u << (*c - '0'); + p.ctx = st; + if (st && st->have_gens) p.gen_lookup = st_lookup; + if (st && st->noreserve) p.reserve = st_reserve; + return p; +} + +static void verdict_text(unsigned rc, const struct osc_accept *a, char *out, size_t n) +{ + if (rc == OSC_OK) { + char d[65], i[65]; + hex(a->unit_digest, 32, d); + hex(a->ir_sha256, 32, i); + snprintf(out, n, "ACCEPT unit_digest=%s program_id=%s", d, i); + } else { + snprintf(out, n, "REFUSED %s %u", osc_code_name(rc), rc); + } +} + +static unsigned admit_file(const char *vec, const struct osc_policy *p, struct osc_accept *a, uint8_t **keep, size_t *kn) +{ + char path[512]; + size_t n; + snprintf(path, sizeof path, "%s/vectors/%s", fixdir, vec); + uint8_t *b = slurp(path, &n); + if (!b) return 999; + unsigned rc = osc_unit_admit(b, n, p, a); + if (keep) { *keep = b; *kn = n; } else free(b); + return rc; +} + +/* run lines of a vector file; kind 0 expected, 1 lookups, 2 state. Returns lines checked. */ +static int run_file(const char *name, int kind) +{ + char path[512], line[1024]; + int count = 0; + snprintf(path, sizeof path, "%s/vectors/%s", fixdir, name); + FILE *f = fopen(path, "r"); + CHECK(f != 0); + if (!f) return 0; + static struct osc_accept acc; + while (fgets(line, sizeof line, f)) { + char vec[128], mode[32], dom[32], anc[8], arg[128] = "", want[600]; + size_t l = strlen(line); + while (l && (line[l - 1] == '\n' || line[l - 1] == '\r')) line[--l] = 0; + if (!l || line[0] == '#') continue; + int ok; + if (kind == 0) ok = sscanf(line, "%127s %31s %31s %7s %599[^\n]", vec, mode, dom, anc, want) == 5; + else ok = sscanf(line, "%127s %31s %31s %7s %127s %599[^\n]", vec, mode, dom, anc, arg, want) == 6; + CHECK(ok); + if (!ok) continue; + struct state st; + memset(&st, 0, sizeof st); + if (kind == 2) { + if (strcmp(arg, "noreserve") == 0) st.noreserve = 1; + else if (strncmp(arg, "gens=", 5) == 0) { + st.have_gens = 1; + unsigned d, k, id; unsigned long long g; + if (sscanf(arg + 5, "%u:%u:%u=%llu", &d, &k, &id, &g) == 4) { + st.dom[0] = d; st.kind[0] = k; st.id[0] = id; st.gen[0] = g; st.n = 1; + } else CHECK(0); + } else CHECK(0); + } + struct osc_policy p = policy_for(mode, dom, anc, &st); + uint8_t *bytes = 0; size_t bn = 0; + unsigned rc = admit_file(vec, &p, &acc, &bytes, &bn); + char got[700]; + if (kind == 1) { + if (rc != OSC_OK) { snprintf(got, sizeof got, "REFUSED %s %u", osc_code_name(rc), rc); } + else { + int fi = osc_unit_lookup(&acc, arg, strlen(arg)); + if (fi < 0) snprintf(got, sizeof got, "REFUSED %s %d", osc_code_name(OSC_LAUNCH_BAD_ENTRY), OSC_LAUNCH_BAD_ENTRY); + else snprintf(got, sizeof got, "ACCEPT fn_index=%d", fi); + } + } else { + verdict_text(rc, &acc, got, sizeof got); + } + free(bytes); + if (strcmp(got, want) != 0) { + printf(" MISMATCH %s [%s]\n want: %s\n got: %s\n", vec, arg, want, got); + ck_t_fail++; + } + ck_t_run++; + count++; + } + fclose(f); + return count; +} + +/* Every single-byte flip and every truncation of the valid vector must be refused, never crash + * (run under ASAN/UBSAN by `make sanitize`). */ +static void test_flips_and_truncations(void) +{ + char path[512]; + size_t n; + snprintf(path, sizeof path, "%s/vectors/a01_valid_min.unit", fixdir); + uint8_t *b = slurp(path, &n); + CHECK(b != 0); + if (!b) return; + struct state st; memset(&st, 0, sizeof st); + struct osc_policy p = policy_for("qualification", "1", "T", &st); + static struct osc_accept acc; + CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_OK); + int accepted = 0; + for (size_t i = 0; i < n; i++) { + b[i] ^= 0x01; + if (osc_unit_admit(b, n, &p, &acc) == OSC_OK) accepted++; + b[i] ^= 0x01; + } + CHECK(accepted == 0); + for (size_t l = 0; l < n; l++) { + unsigned rc = osc_unit_admit(b, l, &p, &acc); + CHECK(rc != OSC_OK); + } + CHECK(osc_unit_admit(0, 0, &p, &acc) != OSC_OK); + free(b); +} + +/* Section 8.4 on hand-picked words. */ +static void test_words(void) +{ + static const uint32_t allowed[] = { + 0x8B010002u, /* add x2, x0, x1 */ 0xD2800020u, /* movz x0, #1 */ + 0xD65F03C0u, /* ret */ 0xD63F0200u, /* blr x16 */ + 0xA9BF7BFDu, /* stp x29, x30, [sp, #-16]! */ 0xA8C17BFDu, /* ldp x29, x30, [sp], #16 */ + 0xD4200060u, /* brk #3 */ 0x94000010u, /* bl */ + 0x38606800u | (1u << 16) | (2u << 5) | 3u, /* ldrb w3, [x2, x0] shape */ + }; + static const uint32_t refused[] = { + 0xD4000001u, /* svc #0 */ 0xD4000002u, /* hvc #0 */ + 0xD4000003u, /* smc #0 */ 0xD5033FDFu, /* isb */ + 0xD503201Fu, /* nop */ 0xD53B4200u, /* mrs x0, nzcv-ish */ + 0xD51B4200u, /* msr */ 0xD69F03E0u, /* eret */ + 0xC85F7C00u, /* ldxr */ 0x1E602800u, /* fadd d0, d0, d0 */ + 0xD4200000u, /* brk #0 */ 0xD42001E0u, /* brk #15 */ + 0xD63F03E0u, /* blr xzr-slot (rn=31) */ 0xD4400000u, /* hlt */ + 0x00000000u, /* udf */ 0xFFFFFFFFu, + 0xCB000000u | (3u << 22), /* sub with ROR shift */ + 0x9A80F400u, /* csinc cond NV */ 0x5400000Eu, /* b.al */ + 0x5400000Fu, /* b.nv */ + }; + for (size_t i = 0; i < sizeof allowed / sizeof allowed[0]; i++) CHECK(osc_a64_word_allowed(allowed[i])); + for (size_t i = 0; i < sizeof refused / sizeof refused[0]; i++) { + CHECK(!osc_a64_word_allowed(refused[i])); + } + /* ldp Rt == Rt2 (x1, x1) refused by the round trip; stp with Rt == Rt2 is allowed. */ + CHECK(!osc_a64_word_allowed(0xA9400000u | (1u << 10) | (2u << 5) | 1u)); + CHECK(osc_a64_word_allowed(0xA9000000u | (1u << 10) | (2u << 5) | 1u)); + /* writeback base equal to a transfer register */ + CHECK(!osc_a64_word_allowed(0xA9800000u | (3u << 10) | (2u << 5) | 2u)); +} + +/* Lookup is by exact name only. Two entries forced to share a name_hash (a + * case no signed vector can carry: it needs a SHA-256 collision) must still + * resolve by name, and an absent name with that hash must not match. */ +static void test_lookup_ignores_name_hash(void) +{ + static struct osc_accept a; + memset(&a, 0, sizeof a); + a.function_count = 2; + const char *nm[2] = {"add", "sub"}; + for (unsigned i = 0; i < 2; i++) { + a.entry[i].fn_index = (uint16_t)(7 + i); + a.entry[i].name_len = 3; + memcpy(a.entry[i].name, nm[i], 4); + memset(a.entry[i].name_hash, 0xAB, 16); + } + CHECK(osc_unit_lookup(&a, "add", 3) == 7); + CHECK(osc_unit_lookup(&a, "sub", 3) == 8); + CHECK(osc_unit_lookup(&a, "mul", 3) == -1); + CHECK(osc_unit_lookup(&a, "ad", 2) == -1); + CHECK(osc_unit_lookup(&a, "addx", 4) == -1); +} + +static void test_anchor_matches_fixture(void) +{ + CHECK(memcmp(osc_unit_test1_pk, key_test, 32) == 0); +} + +/* Spec 8.2 step 16: a loader whose staging maximum is smaller than the file refuses with + * RESOURCE_UNAVAILABLE before parsing; a file that fits is judged normally. */ +static void test_staging_maximum(void) +{ + static struct osc_accept acc; + struct state st; + memset(&st, 0, sizeof st); + struct osc_policy p = policy_for("qualification", "1", "T", &st); + uint8_t *b = 0; size_t n = 0; + CHECK(admit_file("a01_valid_min.unit", &p, &acc, &b, &n) == OSC_OK); + CHECK(n > 128); + p.staging_max = n - 1; + CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_RESOURCE_UNAVAILABLE); + p.staging_max = n; + CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_OK); + free(b); +} + +int main(int argc, char **argv) +{ + if (argc > 1) fixdir = argv[1]; + CHECK(load_key("test1.pub", key_test)); + CHECK(load_key("owner1.pub", key_owner)); + test_anchor_matches_fixture(); + int ne = run_file("expected.txt", 0); + int nl = run_file("lookups.txt", 1); + int ns = run_file("state.txt", 2); + printf("osc_unit conformance: container=%d lookups=%d state=%d\n", ne, nl, ns); + CHECK(ne == 57 && nl == 6 && ns == 3); + test_staging_maximum(); + test_words(); + test_lookup_ignores_name_hash(); + test_flips_and_truncations(); + return ck_t_verdict("test_osc_unit"); +} diff --git a/scripts/.crumb b/scripts/.crumb index 95b52de5..45e10e31 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-08T04:37:23Z", + "compiled_at": "2026-10-08T11:00:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:f64c580898dae2fab2c5fcbe9c65f99dfe0ef2bb9b5df76a9d61f3d4d658b4cf", + "digest": "sha256:76d29d214c40f89bd922e34a16760a2652ee259bc0f16d5bf6a847fcad3e5cad", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 70, - "generated_at_commit": "e387b502b58d9d15fd2751ec5ef4c60f93862244", - "source_tree": "sha256:f811420836da4e0f4d8229c8d1f06912a441e2ad3d97ab0babd164d6f9a0116b" + "files": 71, + "generated_at_commit": "bde85f8671a90d33a46667a5d915cb948035954a", + "source_tree": "sha256:e5b59853bf58aaaf4d1e0b20c7ae66becb340940d413b37abe93c08e8c000a53" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index 72e93725..7bebf215 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -11,10 +11,10 @@ # scripts/qemu_ck_keyboard_test.sh, scripts/qemu_ck_fpu_test.sh, # scripts/qemu_ck_infer_test.sh, scripts/qemu_ck_screen_test.sh, # scripts/qemu_ck_console_test.sh, -# scripts/qemu_ck_rollback_test.sh), and prints +# scripts/qemu_ck_rollback_test.sh, scripts/qemu_ck_osc_unit_test.sh), and prints # one line per gate: # AIENOS_CK_<gate>: PASS|FAIL|NOT_RUN [(reason)] -# for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH +# for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK OSC_UNIT M4_NVME M4_STORE M4_STORE_CRASH # M4_CONTINUITY M4_RECOVERY M4_ALLEN ARGUS1_REVOKE KEYBOARD NET SMP DISK_LAYOUT FPU INFER SCREEN CONSOLE. Gates with no C # implementation print NOT_RUN (MISSING_IMPLEMENTATION: <reason>) and never # run anything. A missing child script, a child that reports NOT_RUN, a child @@ -63,6 +63,7 @@ set -uo pipefail # screen -> verdict line from scripts/qemu_ck_screen_test.sh (GOP framebuffer console vs a QMP screendump) # console -> verdict line from scripts/qemu_ck_console_test.sh (C-only: sustained serial + USB console session in a TEST-ONLY QEMU image; NOT_RUN on gate lock or quiet flag) # rollback -> verdict line from scripts/qemu_ck_rollback_test.sh (C image as the one-time BootNext candidate, AAVMF) +# osc -> verdict line from scripts/qemu_ck_osc_unit_test.sh (OSC unit admission, OSC_UNIT_ARTIFACT v1; QEMU, TEST signer, not physical) # missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS # =========================================================================== CK_GATE_TABLE=' @@ -72,6 +73,7 @@ SMMU|store|- NVME_SHUTDOWN|store|- P2_ARTIFACT|artifact|- M0_ROLLBACK|rollback|- +OSC_UNIT|osc|- M4_NVME|store|- M4_STORE|store|- M4_STORE_CRASH|crash|- @@ -88,7 +90,7 @@ INFER|infer|- SCREEN|screen|- CONSOLE|console|- ' -CHILDREN=(boot store net artifact smp crash disk cont recov allen kbd fpu infer screen console rollback) +CHILDREN=(boot store net artifact smp crash disk cont recov allen kbd fpu infer screen console rollback osc) # =========================================================================== repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -142,6 +144,7 @@ set_default_children() { [screen]="${repo_root}/scripts/qemu_ck_screen_test.sh" [console]="${repo_root}/scripts/qemu_ck_console_test.sh" [rollback]="${repo_root}/scripts/qemu_ck_rollback_test.sh" + [osc]="${repo_root}/scripts/qemu_ck_osc_unit_test.sh" ) } @@ -371,11 +374,11 @@ self_test() { } expect_missing_all() { local g - for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY M4_ALLEN KEYBOARD DISK_LAYOUT FPU INFER SCREEN CONSOLE; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R; DISK_LAYOUT: no disk child in A..S + for g in P2_ARTIFACT M0_ROLLBACK OSC_UNIT M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY M4_ALLEN KEYBOARD DISK_LAYOUT FPU INFER SCREEN CONSOLE; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R; DISK_LAYOUT: no disk child in A..S } scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] [CRASH_SCRIPT] [DISK_SCRIPT] (absent: missing) scen="$1" - declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh" [allen]="${tmp}/kids/no_allen_child.sh" [kbd]="${kbd_kid:-${tmp}/kids/no_kbd_child.sh}" [fpu]="${tmp}/kids/no_fpu_child.sh" [infer]="${tmp}/kids/no_infer_child.sh" [screen]="${tmp}/kids/no_screen_child.sh" [console]="${console_kid:-${tmp}/kids/no_console_child.sh}" [rollback]="${rollback_kid:-${tmp}/kids/no_rollback_child.sh}") + declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh" [allen]="${tmp}/kids/no_allen_child.sh" [kbd]="${kbd_kid:-${tmp}/kids/no_kbd_child.sh}" [fpu]="${tmp}/kids/no_fpu_child.sh" [infer]="${tmp}/kids/no_infer_child.sh" [screen]="${tmp}/kids/no_screen_child.sh" [console]="${console_kid:-${tmp}/kids/no_console_child.sh}" [rollback]="${rollback_kid:-${tmp}/kids/no_rollback_child.sh}" [osc]="${osc_kid:-${tmp}/kids/no_osc_child.sh}") run_children 2>/dev/null evaluate_table >"${tmp}/${scen}.out" } @@ -391,8 +394,8 @@ self_test() { "$(fake smpA 0 'PASS 3 secondary cores checked in' 'AIENOS_CK_SMP_MUTATION: FAIL' 'AIENOS_CK_SMP: PASS')" expect M1 PASS; expect M3 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect NET PASS; expect SMP PASS; expect_missing_all count_rows - [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "9/0/12/21" && ${overall} == NOT_ALL_GATES_PASS ]] \ - && ok "A: counts 9/0/12 of 21, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" + [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "9/0/13/22" && ${overall} == NOT_ALL_GATES_PASS ]] \ + && ok "A: counts 9/0/13 of 22, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" [[ "$(nvme_dma_mode)" == "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately" ]] && ok "A: confined DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'" grep -qxF "AIENOS_CK_M4_NVME: PASS (${M4_NVME_PASS_NOTE})" "${tmp}/A.out" \ && ok "A: M4_NVME PASS line carries the confined-mode note" || bad "A: M4_NVME PASS line lacks the note" @@ -404,7 +407,7 @@ self_test() { && ok "a missing-source row is NOT_RUN (MISSING_IMPLEMENTATION: reason)" || bad "missing-source row not NOT_RUN" grep -q '^AIENOS_CK_KEYBOARD: NOT_RUN' "${tmp}/A.out" \ && ok "A: KEYBOARD claimed PASS by the store child stays NOT_RUN without its own child" || bad "A: KEYBOARD line wrong" - [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 21 ]] && ok "A: exactly 21 verdict lines" || bad "A: verdict line count" + [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 22 ]] && ok "A: exactly 22 verdict lines" || bad "A: verdict line count" # B: boot FAIL; store script missing -> its three gates NOT_RUN, never PASS. scenario B "$(fake bootB 1 'FAIL kernel: alive' 'AIENOS_CK_M3: FAIL' 'AIENOS_CK_M1: FAIL')" "${tmp}/kids/does_not_exist.sh" @@ -529,6 +532,14 @@ self_test() { scenario RC "$(fake bootRC 0 'AIENOS_CK_M1: PASS')" "$(fake storeRC 0 'AIENOS_CK_M4_NVME: PASS')" expect M0_ROLLBACK NOT_RUN unset rollback_kid + # OA, OB: OSC_UNIT comes only from its own child (scripts/qemu_ck_osc_unit_test.sh). + osc_kid="$(fake oscA 0 'PASS x' 'AIENOS_CK_OSC_UNIT: PASS')" + scenario OA "$(fake bootOA 0 'AIENOS_CK_M1: PASS')" "$(fake storeOA 0 'AIENOS_CK_OSC_UNIT: FAIL')" + expect OSC_UNIT PASS + osc_kid="$(fake oscB 1 'FAIL y' 'AIENOS_CK_OSC_UNIT: FAIL')" + scenario OB "$(fake bootOB 0 'AIENOS_CK_M1: PASS')" "$(fake storeOB 0 'AIENOS_CK_OSC_UNIT: PASS')" + expect OSC_UNIT FAIL + unset osc_kid # Receipt: named by its content hash, valid JSON, physical NOT_RUN, never overwritten. scenario R "$(fake bootR 0 'AIENOS_CK_M1: PASS')" "$(fake storeR 0 'AIENOS_CK_M4_NVME: PASS')" @@ -551,10 +562,10 @@ self_test() { # Restore the clean-tree receipt as the one the checks below look at. write_receipt if command -v jq >/dev/null; then - jq -e '.physical == "NOT_RUN" and (.gates | length) == 21 and .verdict == "NOT_ALL_GATES_PASS" + jq -e '.physical == "NOT_RUN" and (.gates | length) == 22 and .verdict == "NOT_ALL_GATES_PASS" and ([.gates[] | select(.id == "M1")][0].verdict == "PASS") and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL") - and (.children | length) == 16 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ + and (.children | length) == 17 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong" else echo "NOT_RUN jq not installed; JSON validity not machine-checked"; jq_skipped=1 @@ -567,18 +578,18 @@ self_test() { bad "an existing receipt was overwritten or the clash was not refused" fi - # The gate table parses: 21 rows, known sources, reasons only on missing rows. + # The gate table parses: 22 rows, known sources, reasons only on missing rows. local g s w rows=0 tbad=0 while IFS='|' read -r g s w; do [[ -n "${g}" ]] || continue rows=$((rows + 1)) case "${s}" in - boot|store|net|artifact|smp|crash|disk|cont|recov|allen|kbd|fpu|infer|screen|console|rollback) [[ "${w}" == - ]] || tbad=1 ;; + boot|store|net|artifact|smp|crash|disk|cont|recov|allen|kbd|fpu|infer|screen|console|rollback|osc) [[ "${w}" == - ]] || tbad=1 ;; missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;; *) tbad=1 ;; esac done <<<"${CK_GATE_TABLE}" - [[ ${tbad} == 0 && ${rows} == 21 ]] && ok "gate table: 21 rows well-formed" || bad "gate table malformed (${rows} rows)" + [[ ${tbad} == 0 && ${rows} == 22 ]] && ok "gate table: 22 rows well-formed" || bad "gate table malformed (${rows} rows)" # Dirty-tree refusal, end to end, on a private clone (never this tree). # Both runs stop before any child script (no QEMU). diff --git a/scripts/qemu_ck_osc_unit_test.sh b/scripts/qemu_ck_osc_unit_test.sh new file mode 100755 index 00000000..d12b8edf --- /dev/null +++ b/scripts/qemu_ck_osc_unit_test.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# AIENOS_CK_OSC_UNIT gate: OSC unit admission (OSC_UNIT_ARTIFACT v1, aien-protocols +# PR #17) in the C kernel, booted in QEMU AArch64 + UEFI (AAVMF). +# +# Label: QEMU (aarch64 virt), TEST signer, not physical. Nothing here ran on a +# Spark or any real machine. The only key is the spec's THROWAWAY TEST key; no +# OWNER anchor exists (TRUST-1 has not provisioned one). Admission only: no +# unit is mapped, launched or scheduled. +# +# Four conformance vectors (tests/fixtures/osc_unit/vectors) are written into +# the sealed C Store of a GPT boot disk image at build time and read back by the +# kernel's store stage, the same path as the P2 artifacts (svc/artifact_store.h): +# a01_valid_min.unit TEST-signed, valid +# a04_valid_owner_class.unit OWNER class, valid signature, no OWNER anchor +# r14_bad_signature.unit TEST-signed, signature byte flipped +# r37_code_svc.unit code contains an SVC word +# Two boots of the full image, each with QEMU iommu=smmuv3 and the NVMe image: +# 1. qualification build (CK_SEED0B_TEST_ANCHOR=1, TEST ONLY): a01 ACCEPT with +# the UnitDigest and program id of the frozen expected.txt; a04 +# UNTRUSTED_SIGNER; r14 BAD_SIGNATURE; r37 CODE_INSTRUCTION. +# 2. ordinary (release) build, no anchors: a01 TEST_SIGNER_IN_RELEASE; a04 +# UNTRUSTED_SIGNER; r14 TEST_SIGNER_IN_RELEASE (the class check precedes the +# signature); r37 CODE_INSTRUCTION; nothing accepted. +# Static: the release image carries the TEST public key nowhere. +# Last line: AIENOS_CK_OSC_UNIT: PASS|FAIL|NOT_RUN. +# Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" +verdict=AIENOS_CK_OSC_UNIT + +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +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; } + +quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" +quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_osc_unit_test $$}" +if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then + echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" + echo "${verdict}: NOT_RUN" + exit 3 +fi +own_flag=1 +release_flag() { + if [[ "${own_flag}" == 1 ]]; then + own_flag=0 + if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi + fi +} +work="$(mktemp -d)" +cleanup() { rm -rf "${work}"; release_flag; } +trap cleanup EXIT +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="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}" +out_qual="${AIENOS_CK_OUT_QUAL:-${repo_root}/target/native-kernel-seed0b-test}" +mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } +mk OUT="${out_prod}" full +mk OUT="${out_qual}" CK_SEED0B_TEST_ANCHOR=1 full +make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null +simg="${out_prod}/host/ck_store_image" + +fix="native/kernel/tests/fixtures/osc_unit" +units=(a01_valid_min.unit a04_valid_owner_class.unit r14_bad_signature.unit r37_code_svc.unit) +files=() +for u in "${units[@]}"; do files+=("${fix}/vectors/${u}"); done +a01_line=$(grep -E '^a01_valid_min\.unit ' "${fix}/vectors/expected.txt") +a01_digest=$(sed -E 's/.* unit_digest=([0-9a-f]{64}) .*/\1/' <<<"${a01_line}") +a01_prog=$(sed -E 's/.* program_id=([0-9a-f]{64})$/\1/' <<<"${a01_line}") +[[ ${#a01_digest} == 64 && ${#a01_prog} == 64 ]] || { fail "expected UnitDigest read from the frozen expected.txt"; } + +# ---- static: the release image does not carry the TEST public key ---------- +key_hex=$(tr -d '\n' <"${fix}/keys/test1.pub") +key_bytes=$(printf "$(sed 's/../\\x&/g' <<<"${key_hex}")") +for d in "${out_prod}/full"; do + if LC_ALL=C grep -aqF -- "${key_bytes}" "${d}/BOOTAA64.EFI"; then fail "release image ${d#"${repo_root}"/} carries the OSC TEST public key" + else pass "release image ${d#"${repo_root}"/} does not carry the OSC TEST public key"; fi +done +if LC_ALL=C grep -aqF -- "${key_bytes}" "${out_qual}/full/BOOTAA64.EFI"; then + pass "the check sees the TEST key in the TEST-anchor qualification image (never counted as release)" +else + fail "the TEST key is not found in the qualification image, so the release check proves nothing" +fi + +# ---- boot disk: the vectors in the sealed Store ------------------------------ +disk="${work}/nvme.img" +"${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created" +if "${simg}" build "${disk}" 512 "${files[@]}" >"${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 +"${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store" +for f in "${files[@]}"; do + grep -qxF "$(basename "${f}") bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" \ + || fail "host read-back of $(basename "${f}")" +done + +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" + touch "${esp}/EFI/AIENOS/BOOTREPORT.TXT" + cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI" + cp "${vars_fd}" "${work}/vars.fd" + started=$(date +%s) + set +e + nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \ + -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 \ + -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=$? + set -e + tr -d '\r' <"${log}" >"$2" + echo "qemu exit ${status} after $(( $(date +%s) - started )) s (commit ${commit:0:12})" + [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)" +} +has() { grep -qE -- "$2" "$1"; } +check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; } +common_checks() { + check "$1" "left firmware and entered the kernel" "kernel: alive" + check "$1" "kernel read all ${#units[@]} candidates from the boot disk Store" "^artifact_candidates: ${#units[@]}$" + check "$1" "artifact_source=nvme_store" "^artifact_source: nvme_store generation=[0-9]+ candidates=${#units[@]} " + if has "$1" "^report-truncated:"; 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" "^artifact_bundle: malformed"; then fail "candidate list malformed"; fi + if has "$1" "TEST-ONLY QEMU fw_cfg|^artifact_source: fw_cfg"; then fail "fw_cfg source in a counted boot"; fi + if has "$1" "^artifact: "; then fail "an OSC unit was handed to the Binary Artifact v0 path"; else pass "no OSC unit went through the Binary Artifact v0 path"; fi + check "$1" "one osc_unit line per candidate" "^osc_units: seen=${#units[@]} " + check "$1" "the summary says admission only, nothing launched" "^osc_units: .*\(admission only, nothing launched\)$" +} +unit_line() { # serial name expected-regex-tail + if has "$1" "^osc_unit: ${2//./\\.} $3\$"; then pass "$2 -> $4"; else fail "$2 expected: $4 (got: $(grep -E "^osc_unit: ${2//./\\.} " "$1" | head -1 || echo none))"; fi +} + +# ---- boot 1: qualification build (TEST ONLY anchor) ------------------------- +qual="${work}/qualification.txt" +cp "${disk}" "${work}/disk-qual.img" +boot "${out_qual}/full/BOOTAA64.EFI" "${qual}" "${work}/disk-qual.img" +common_checks "${qual}" +check "${qual}" "policy line: qualification mode, one TEST anchor, no OWNER anchor, TEST ONLY, QEMU not physical" \ + "^osc_policy: mode=qualification test_anchors=1 \(throwaway TEST key, TEST ONLY\) owner_anchors=0 domains=1 gen_width=32; QEMU, not physical$" +unit_line "${qual}" a01_valid_min.unit \ + "ACCEPT unit_digest=${a01_digest} program_id=${a01_prog} funcs=2 caps=0 signer=TEST" \ + "ACCEPT, UnitDigest and program id equal the frozen expected.txt" +unit_line "${qual}" a04_valid_owner_class.unit "REFUSED code=25 name=UNTRUSTED_SIGNER" "REFUSED UNTRUSTED_SIGNER (OWNER, no OWNER anchor)" +unit_line "${qual}" r14_bad_signature.unit "REFUSED code=26 name=BAD_SIGNATURE" "REFUSED BAD_SIGNATURE" +unit_line "${qual}" r37_code_svc.unit "REFUSED code=34 name=CODE_INSTRUCTION" "REFUSED CODE_INSTRUCTION (SVC word)" +check "${qual}" "qualification totals: 1 accepted, 3 refused" "^osc_units: seen=4 accepted=1 refused=3 " + +# ---- boot 2: ordinary (release) build, no anchors ---------------------------- +prod="${work}/release.txt" +cp "${disk}" "${work}/disk-prod.img" +boot "${out_prod}/full/BOOTAA64.EFI" "${prod}" "${work}/disk-prod.img" +release_flag +common_checks "${prod}" +check "${prod}" "policy line: release mode, no anchors at all" \ + "^osc_policy: mode=release test_anchors=0 owner_anchors=0 \(none provisioned\) domains=1 gen_width=32; QEMU, not physical$" +if has "${prod}" "seed0b-test qualification build"; then fail "release build carries the qualification label"; fi +unit_line "${prod}" a01_valid_min.unit "REFUSED code=24 name=TEST_SIGNER_IN_RELEASE" "REFUSED TEST_SIGNER_IN_RELEASE (a valid TEST-signed unit)" +unit_line "${prod}" a04_valid_owner_class.unit "REFUSED code=25 name=UNTRUSTED_SIGNER" "REFUSED UNTRUSTED_SIGNER (OWNER, no anchor)" +unit_line "${prod}" r14_bad_signature.unit "REFUSED code=24 name=TEST_SIGNER_IN_RELEASE" "REFUSED TEST_SIGNER_IN_RELEASE (class is checked before the signature)" +unit_line "${prod}" r37_code_svc.unit "REFUSED code=34 name=CODE_INSTRUCTION" "REFUSED CODE_INSTRUCTION" +check "${prod}" "release totals: nothing accepted" "^osc_units: seen=4 accepted=0 refused=4 " + +if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then + cp "${qual}" "${AIENOS_LOG_DIR}/qemu_ck_osc_unit_qualification_serial.log" + cp "${prod}" "${AIENOS_LOG_DIR}/qemu_ck_osc_unit_release_serial.log" +fi +if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then + echo "---- qualification serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${qual}" | head -60 || true + echo "---- release serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${prod}" | head -60 || true +fi +echo "label: QEMU (aarch64 virt), TEST signer, not physical" +if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi