aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / osc_admit.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* osc_admit.c -- kernel policy and reporting for OSC unit admission. |
| 18 | * |
| 19 | * Policy (OSC_UNIT_ARTIFACT.md section 8.1), all local to this build: |
| 20 | * - ordinary build: release mode, no TEST anchor, no OWNER anchor. A TEST |
| 21 | * unit is refused TEST_SIGNER_IN_RELEASE, an OWNER unit UNTRUSTED_SIGNER |
| 22 | * (OWNER anchors are not provisioned until TRUST-1). |
| 23 | * - qualification build (CK_SEED0B_TEST_ANCHOR=1): qualification mode with |
| 24 | * the throwaway TEST key of the conformance vectors as the only TEST |
| 25 | * anchor, announced TEST ONLY. OWNER anchors stay empty. |
| 26 | * - capability domain 1 only (kernel IPC table, 32-bit generations); the |
| 27 | * kernel holds no resource state at admission, so any pinned generation is |
| 28 | * STALE (the lookup finds nothing). No reservation: launch is a later cut. |
| 29 | * QEMU qualifies nothing physical. */ |
| 30 | #include "ck_internal.h" |
| 31 | #include "osc_admit.h" |
| 32 | #include "osc_unit.h" |
| 33 | #include "osc_receipt.h" |
| 34 | #include "osc_receipt_anchor.h" |
| 35 | #include "sha256.h" |
| 36 | #ifdef CK_OSH_TEST |
| 37 | void ck_osh_save(const char *name, const uint8_t *b, size_t len, const struct osc_accept *acc); |
| 38 | void ck_osh_all(void); |
| 39 | #endif |
| 40 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 41 | #include "osc_unit_test_anchor.h" |
| 42 | #include "osc_receipt_test_anchor.h" |
| 43 | #endif |
| 44 | /* Variant 3 flips one admitted code byte after admission and restores it after |
| 45 | * launch (ck_osc_receipt_test_code_flip). The flip is its own opt-in, |
| 46 | * CK_OSC_RECEIPT_CODE_FLIP. The Makefile defines that macro only inside the |
| 47 | * CK_OSC_RECEIPT_TEST=1 block. CK_OSC_RECEIPT_TEST does not enable the flip, |
| 48 | * and neither does CK_SEED0B_TEST_ANCHOR: a compiler command that passes those |
| 49 | * two and skips native/kernel/Makefile fails below. CK_RELEASE_IMAGE (every |
| 50 | * build that is not the qualification anchor), CK_HARDWARE_STAGING and |
| 51 | * CK_SERVICE are positive production or release defines; the flip #errors |
| 52 | * when any of them is present. */ |
| 53 | #if defined(CK_OSC_RECEIPT_CODE_FLIP) && !defined(CK_OSC_RECEIPT_TEST) |
| 54 | #error "CK_OSC_RECEIPT_CODE_FLIP (TEST-ONLY post-admission code byte flip) requires CK_OSC_RECEIPT_TEST" |
| 55 | #endif |
| 56 | #if defined(CK_OSC_RECEIPT_TEST) && !defined(CK_OSC_RECEIPT_CODE_FLIP) |
| 57 | #error "CK_OSC_RECEIPT_TEST requires CK_OSC_RECEIPT_CODE_FLIP; CK_SEED0B_TEST_ANCHOR does not enable the post-admission code byte flip" |
| 58 | #endif |
| 59 | #if defined(CK_OSC_RECEIPT_CODE_FLIP) && defined(CK_RELEASE_IMAGE) |
| 60 | #error "CK_OSC_RECEIPT_CODE_FLIP (TEST-ONLY post-admission code byte flip) cannot be compiled when a production or release define is present: CK_RELEASE_IMAGE" |
| 61 | #endif |
| 62 | #if defined(CK_OSC_RECEIPT_CODE_FLIP) && defined(CK_HARDWARE_STAGING) |
| 63 | #error "CK_OSC_RECEIPT_CODE_FLIP (TEST-ONLY post-admission code byte flip) cannot be compiled when a production or release define is present: CK_HARDWARE_STAGING" |
| 64 | #endif |
| 65 | #if defined(CK_OSC_RECEIPT_CODE_FLIP) && defined(CK_SERVICE) |
| 66 | #error "CK_OSC_RECEIPT_CODE_FLIP (TEST-ONLY post-admission code byte flip) cannot be compiled when a production or release define is present: CK_SERVICE" |
| 67 | #endif |
| 68 | #if defined(CK_OSC_RECEIPT_CODE_FLIP) && !defined(CK_SEED0B_TEST_ANCHOR) |
| 69 | #error "CK_OSC_RECEIPT_CODE_FLIP (TEST-ONLY post-admission code byte flip) cannot be compiled into a release build" |
| 70 | #endif |
| 71 | |
| 72 | static unsigned seen, accepted, refused, oversize, announced; |
| 73 | static struct osc_accept acc; /* ~5 KB: static, not on the kernel stack */ |
| 74 | |
| 75 | static int no_resource(void *ctx, unsigned domain, unsigned kind, uint32_t id, uint64_t *cur) |
| 76 | { |
| 77 | (void)ctx; (void)domain; (void)kind; (void)id; (void)cur; |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | /* The Store and the native staging area hold at most 160 pages per artifact (= CK_ART_MAX_BYTES in |
| 82 | * svc/artifact_store.h, 655,360 bytes; spec 8.2 step 16). */ |
| 83 | #define OSC_STAGING_MAX (160u * 4096u) |
| 84 | |
| 85 | static void hex(char *out, const uint8_t *b, size_t n) |
| 86 | { |
| 87 | static const char d[] = "0123456789abcdef"; |
| 88 | for (size_t i = 0; i < n; i++) { |
| 89 | out[2 * i] = d[b[i] >> 4]; |
| 90 | out[2 * i + 1] = d[b[i] & 15]; |
| 91 | } |
| 92 | out[2 * n] = 0; |
| 93 | } |
| 94 | |
| 95 | /* Realization receipt policy (artifact/osc_receipt.h, aienos#383), local to this build: |
| 96 | * - both builds: the accepted checker2 identities of osc_receipt_anchor.h, target aienos-ck-aarch64-el0, no |
| 97 | * firmware, no dependencies, no producer (all zero: the unit names no producer), epoch floor and current epoch |
| 98 | * fixed below (the kernel has no epoch authority yet, so they are compile-time constants), and the owner root |
| 99 | * osc_rcpt_owner_root_pk as the one OWNER receipt anchor (aienos#387: docs/OWNER_KEY_CEREMONY.md section 7 |
| 100 | * records that the owner root may sign realization receipts). |
| 101 | * - release build: release mode, so every TEST receipt is TEST_SIGNER_IN_PRODUCTION; no TEST receipt anchor. An |
| 102 | * owner-signed receipt is valid, but a release build still launches nothing: unit admission has no OWNER unit |
| 103 | * anchor (OWNER anchors are not provisioned until TRUST-1), so no unit is admitted to launch. |
| 104 | * - qualification build (CK_SEED0B_TEST_ANCHOR=1): in addition, checker2's TEST key as the TEST receipt anchor and |
| 105 | * the fixture n_revoked_id.rcpt on the revoked-id list (osc_receipt_test_anchor.h). TEST ONLY. */ |
| 106 | #define CK_OSC_RCPT_EPOCH_FLOOR 10u |
| 107 | #define CK_OSC_RCPT_EPOCH_NOW 20u |
| 108 | static uint8_t rcpt_target[32]; |
| 109 | #ifdef CK_OSC_LAUNCH_TEST |
| 110 | int ck_osc_rcpt_variant; /* TEST ONLY: 1 release mode, 2 the TEST key fingerprint revoked (receipt self-test) */ |
| 111 | static uint8_t rcpt_test_fp[1][32]; |
| 112 | #endif |
| 113 | |
| 114 | void ck_osc_policy(struct osc_policy *p) |
| 115 | { |
| 116 | memset(p, 0, sizeof *p); |
| 117 | p->unit_formats = OSC_PROFILE_UNIT_FORMATS; |
| 118 | p->abi_versions = OSC_PROFILE_ABI; |
| 119 | p->cap_domains = 1u << 1; |
| 120 | p->staging_max = OSC_STAGING_MAX; |
| 121 | p->gen_lookup = no_resource; |
| 122 | osc_receipt_target_hash(OSC_RCPT_TARGET_NAME, sizeof OSC_RCPT_TARGET_NAME - 1, rcpt_target); |
| 123 | p->rcpt_checkers = osc_rcpt_checker_ids; |
| 124 | p->n_rcpt_checkers = OSC_RCPT_N_CHECKERS; |
| 125 | p->rcpt_epoch_floor = CK_OSC_RCPT_EPOCH_FLOOR; |
| 126 | p->rcpt_epoch_now = CK_OSC_RCPT_EPOCH_NOW; |
| 127 | p->rcpt_target = rcpt_target; |
| 128 | static const uint8_t (*const roa)[32] = &osc_rcpt_owner_root_pk; |
| 129 | p->rcpt_owner_anchors = roa; |
| 130 | p->n_rcpt_owner = 1; |
| 131 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 132 | static const uint8_t (*const ta)[32] = &osc_unit_test1_pk; |
| 133 | static const uint8_t (*const rta)[32] = &osc_rcpt_test_pk; |
| 134 | static const uint8_t (*const rid)[32] = &osc_rcpt_qual_revoked_id; |
| 135 | p->release = 0; |
| 136 | p->test_anchors = ta; |
| 137 | p->n_test = 1; |
| 138 | p->rcpt_test_anchors = rta; |
| 139 | p->n_rcpt_test = 1; |
| 140 | p->rcpt_revoked_ids = rid; |
| 141 | p->n_rcpt_revoked_ids = 1; |
| 142 | #else |
| 143 | p->release = 1; |
| 144 | #endif |
| 145 | #ifdef CK_OSC_LAUNCH_TEST |
| 146 | if (ck_osc_rcpt_variant == 1) |
| 147 | p->release = 1; |
| 148 | if (ck_osc_rcpt_variant == 2) { |
| 149 | sha256_hash(osc_rcpt_test_pk, 32, rcpt_test_fp[0]); |
| 150 | p->rcpt_revoked_keys = (const uint8_t (*)[32])rcpt_test_fp; |
| 151 | p->n_rcpt_revoked_keys = 1; |
| 152 | } |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | /* ---- receipts that arrived through the Store, kept by name until a launch asks for one ---- */ |
| 157 | #define RCPT_SLOTS 24u |
| 158 | #define RCPT_KEEP 512u /* more than 416 so a wrong-length receipt reaches the verifier and is refused SIZE */ |
| 159 | static struct { |
| 160 | char name[33]; |
| 161 | uint8_t bytes[RCPT_KEEP]; |
| 162 | size_t len; |
| 163 | } rcpt[RCPT_SLOTS]; |
| 164 | static unsigned rcpt_n, rcpt_seen, rcpt_dropped; |
| 165 | |
| 166 | static int name_eq(const char *a, const char *b) |
| 167 | { |
| 168 | size_t n = strlen(a); |
| 169 | return n == strlen(b) && memcmp(a, b, n) == 0; |
| 170 | } |
| 171 | |
| 172 | int ck_osc_is_receipt(const uint8_t *b, size_t len) |
| 173 | { |
| 174 | return b && len >= 8 && memcmp(b, "OMRCPT1\0", 8) == 0; |
| 175 | } |
| 176 | |
| 177 | void ck_osc_receipt_candidate(const char *name, const uint8_t *b, size_t len) |
| 178 | { |
| 179 | rcpt_seen++; |
| 180 | size_t nl = strlen(name); |
| 181 | if (rcpt_n >= RCPT_SLOTS || nl >= sizeof rcpt[0].name) { |
| 182 | rcpt_dropped++; |
| 183 | ck_printf("osc_receipt: %s DROPPED (receipt table full or name too long; a launch that needs it is refused " |
| 184 | "RECEIPT_MISSING)\n", name); |
| 185 | return; |
| 186 | } |
| 187 | memcpy(rcpt[rcpt_n].name, name, nl + 1); |
| 188 | /* a receipt longer than RCPT_KEEP is kept as its first RCPT_KEEP bytes: still not 416, so refused SIZE */ |
| 189 | rcpt[rcpt_n].len = len <= RCPT_KEEP ? len : RCPT_KEEP; |
| 190 | memcpy(rcpt[rcpt_n].bytes, b, rcpt[rcpt_n].len); |
| 191 | char idh[65] = "-"; |
| 192 | if (len == OSC_RCPT_BYTES) { |
| 193 | uint8_t id[32]; |
| 194 | sha256_hash(b, len, id); |
| 195 | hex(idh, id, 32); |
| 196 | } |
| 197 | ck_printf("osc_receipt: %s STORED bytes=%llu id=%s (verified at launch against the unit launched)\n", name, |
| 198 | (unsigned long long)len, idh); |
| 199 | rcpt_n++; |
| 200 | } |
| 201 | |
| 202 | const uint8_t *ck_osc_receipt_find(const char *name, size_t *len) |
| 203 | { |
| 204 | *len = 0; |
| 205 | for (unsigned i = 0; name && i < rcpt_n; i++) |
| 206 | if (name_eq(rcpt[i].name, name)) { |
| 207 | *len = rcpt[i].len; |
| 208 | return rcpt[i].bytes; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | #ifdef CK_OSC_LAUNCH_TEST |
| 215 | /* ====================================================================================================== |
| 216 | * C3-3a launch self-test (TEST ONLY, qualification build with CK_OSC_LAUNCH_TEST=1). |
| 217 | * Runs a fixed script of launches over the admitted units the Store handed over and prints one |
| 218 | * "osc_launch:" line each. QEMU (aarch64 virt), TEST signer, not physical. |
| 219 | * ==================================================================================================== */ |
| 220 | #include "osc_task.h" |
| 221 | #define LT_UNITS 4 |
| 222 | #define LT_BYTES 8192 |
| 223 | static struct { |
| 224 | char name[48]; |
| 225 | uint8_t bytes[LT_BYTES]; |
| 226 | size_t len; |
| 227 | struct osc_accept acc; |
| 228 | } lt_unit[LT_UNITS]; |
| 229 | static unsigned lt_n; |
| 230 | |
| 231 | static int lt_streq(const char *a, const char *b) |
| 232 | { |
| 233 | size_t n = strlen(a); |
| 234 | return n == strlen(b) && memcmp(a, b, n) == 0; |
| 235 | } |
| 236 | |
| 237 | static void lt_save(const char *name, const uint8_t *b, size_t len) |
| 238 | { |
| 239 | if (lt_n >= LT_UNITS || len > LT_BYTES) |
| 240 | return; |
| 241 | unsigned i = 0; |
| 242 | while (name[i] && i < sizeof lt_unit[0].name - 1) { |
| 243 | lt_unit[lt_n].name[i] = name[i]; |
| 244 | i++; |
| 245 | } |
| 246 | lt_unit[lt_n].name[i] = 0; |
| 247 | memcpy(lt_unit[lt_n].bytes, b, len); |
| 248 | lt_unit[lt_n].len = len; |
| 249 | lt_unit[lt_n].acc = acc; |
| 250 | lt_n++; |
| 251 | } |
| 252 | |
| 253 | #define IN_PTR (1ull << 62) /* placeholders for the task's input and workspace windows */ |
| 254 | #define WS_PTR (1ull << 63) |
| 255 | #define KERN_PTR (1ull << 61) /* an address in the kernel image (mapped EL1-only in the task address space) */ |
| 256 | struct lt_step { |
| 257 | const char *unit, *fn; |
| 258 | unsigned nargs; |
| 259 | uint64_t a[3]; |
| 260 | const char *in; |
| 261 | int ws; /* 0 none, 1..2 which caller workspace */ |
| 262 | uint64_t cap; |
| 263 | const char *rcpt; /* receipt Store entry; NULL: the unit's own (lt_rcpt_for), "-": none */ |
| 264 | int variant; /* TEST ONLY: 0 qual, 1 release, 2 revoked key, 3 changed code under qual policy */ |
| 265 | }; |
| 266 | |
| 267 | /* The receipt of each launched unit: a receipt binds the IR and the code, so l02 (l01 with a bigger stack) shares |
| 268 | * l01's and a06 (a01's IR and code, other limits) shares a01's. */ |
| 269 | static const char *lt_rcpt_for(const char *unit) |
| 270 | { |
| 271 | if (lt_streq(unit, "a01_valid_min.unit") || lt_streq(unit, "a06_valid_limits_at_max.unit")) |
| 272 | return "a01.rcpt"; |
| 273 | if (lt_streq(unit, "l01_launch_fns.unit") || lt_streq(unit, "l02_stack_over.unit")) |
| 274 | return "l01.rcpt"; |
| 275 | return "-"; |
| 276 | } |
| 277 | |
| 278 | extern void ck_osc_state_range(uint64_t *addr, uint64_t *len); |
| 279 | static uint8_t lt_ws_mem1[4 * 4096], lt_ws_mem2[4 * 4096], lt_ws_mem3[23 * 4096], lt_ws_mem4[33 * 4096] |
| 280 | __attribute__((aligned(4096))); |
| 281 | static struct ck_osc_ws lt_ws[8] = { { lt_ws_mem1, 4 }, { lt_ws_mem2, 4 }, { lt_ws_mem3, 23 }, { lt_ws_mem4, 33 }, |
| 282 | { 0, 1 }, { lt_ws_mem1 + 8, 1 }, { 0, 1 }, { lt_ws_mem2, 4 } }; |
| 283 | static unsigned lt_counts[5]; |
| 284 | |
| 285 | #ifdef CK_OSC_RECEIPT_CODE_FLIP |
| 286 | /* TEST ONLY. One call flips one admitted code byte; the second call restores |
| 287 | * it. The launch between the two calls rehashes the private copy. */ |
| 288 | static __attribute__((noinline)) void ck_osc_receipt_test_code_flip(uint8_t *code_byte) |
| 289 | { |
| 290 | *code_byte ^= 1; |
| 291 | } |
| 292 | #endif |
| 293 | |
| 294 | static void lt_run(const struct lt_step *s) |
| 295 | { |
| 296 | for (unsigned u = 0; u < lt_n; u++) { |
| 297 | if (!lt_streq(lt_unit[u].name, s->unit)) |
| 298 | continue; |
| 299 | struct ck_osc_launch_req rq; |
| 300 | memset(&rq, 0, sizeof rq); |
| 301 | rq.name = s->fn; |
| 302 | rq.name_len = strlen(s->fn); |
| 303 | rq.nargs = s->nargs; |
| 304 | rq.max_ticks = s->cap; |
| 305 | for (unsigned i = 0; i < s->nargs; i++) { |
| 306 | rq.args[i] = s->a[i]; |
| 307 | if (s->a[i] & IN_PTR) |
| 308 | rq.args[i] = ck_osc_va_in() + (s->a[i] & 0xffffff); |
| 309 | else if (s->a[i] & WS_PTR) |
| 310 | rq.args[i] = ck_osc_va_ws() + (s->a[i] & 0xffffff); |
| 311 | else if (s->a[i] & KERN_PTR) |
| 312 | rq.args[i] = (uint64_t)(uintptr_t)<_counts; |
| 313 | } |
| 314 | if (s->in) { |
| 315 | rq.in = (const uint8_t *)s->in; |
| 316 | rq.in_len = strlen(s->in); |
| 317 | } |
| 318 | if (s->ws) |
| 319 | rq.ws = <_ws[s->ws - 1]; |
| 320 | struct osc_result r; |
| 321 | struct ck_osc_launch_info info; |
| 322 | const char *rn = s->rcpt ? s->rcpt : lt_rcpt_for(s->unit); |
| 323 | size_t rlen = 0; |
| 324 | const uint8_t *rb = lt_streq(rn, "-") ? 0 : ck_osc_receipt_find(rn, &rlen); |
| 325 | ck_osc_rcpt_variant = s->variant == 3 ? 0 : s->variant; |
| 326 | #ifdef CK_OSC_RECEIPT_CODE_FLIP |
| 327 | /* Preserve the valid receipt and admitted view; alter only the bytes |
| 328 | * privately copied by the launcher, exercising its final rehash. */ |
| 329 | if (s->variant == 3) |
| 330 | ck_osc_receipt_test_code_flip(<_unit[u].bytes[lt_unit[u].acc.code_off]); |
| 331 | #endif |
| 332 | ck_osc_launch(lt_unit[u].bytes, lt_unit[u].len, <_unit[u].acc, rb, rlen, &rq, &r, &info); |
| 333 | #ifdef CK_OSC_RECEIPT_CODE_FLIP |
| 334 | if (s->variant == 3) |
| 335 | ck_osc_receipt_test_code_flip(<_unit[u].bytes[lt_unit[u].acc.code_off]); |
| 336 | #endif |
| 337 | ck_osc_rcpt_variant = 0; |
| 338 | char rs[96], as[80]; |
| 339 | ck_osc_result_str(&r, rs, sizeof rs); |
| 340 | unsigned n = 0; |
| 341 | as[0] = 0; |
| 342 | for (unsigned i = 0; i < s->nargs && n < sizeof as - 24; i++) { |
| 343 | uint64_t v = s->a[i]; |
| 344 | if (v & IN_PTR) |
| 345 | n += (unsigned)ck_snprintf(as + n, sizeof as - n, "%sin+%u", i ? "," : "", (unsigned)(v & 0xffffff)); |
| 346 | else if (v & WS_PTR) |
| 347 | n += (unsigned)ck_snprintf(as + n, sizeof as - n, "%sws+%u", i ? "," : "", (unsigned)(v & 0xffffff)); |
| 348 | else if (v & KERN_PTR) |
| 349 | n += (unsigned)ck_snprintf(as + n, sizeof as - n, "%skernel", i ? "," : ""); |
| 350 | else |
| 351 | n += (unsigned)ck_snprintf(as + n, sizeof as - n, "%s%llu", i ? "," : "", (unsigned long long)v); |
| 352 | } |
| 353 | #ifdef CK_OSC_RECEIPT_TEST |
| 354 | static const char *const vname[3] = { "qualification", "release", "revoked_test_key" }; |
| 355 | char idh[17] = "-"; |
| 356 | if (info.rcpt.have_id) |
| 357 | hex(idh, info.rcpt.id, 8); |
| 358 | ck_printf("osc_rcpt_launch: %s %s(%s) receipt=%s policy=%s -> %s receipt_id=%s revoked=%s ticks=%llu " |
| 359 | "pages_mapped=%u page_tables_zeroed=%u slot_free=%u; QEMU (aarch64 virt), TEST signer, not physical\n", |
| 360 | s->unit, s->fn, as, rb ? rn : "none", vname[s->variant == 3 ? 0 : s->variant], rs, idh, |
| 361 | info.rcpt.revoked == OSC_RCPT_REVOKED_ID ? "receipt_id" |
| 362 | : info.rcpt.revoked == OSC_RCPT_REVOKED_KEY ? "signer_key" : "-", |
| 363 | (unsigned long long)r.ticks, info.pages_mapped, (unsigned)info.tables_zeroed, |
| 364 | (unsigned)info.slot_free_after); |
| 365 | #else |
| 366 | ck_printf("osc_launch: %s %s(%s) -> %s ticks=%llu budget=%llu pages_mapped=%u page_tables_zeroed=%u slot_free=%u; " |
| 367 | "QEMU (aarch64 virt), TEST signer, not physical\n", |
| 368 | s->unit, s->fn, as, rs, (unsigned long long)r.ticks, (unsigned long long)info.budget, |
| 369 | info.pages_mapped, (unsigned)info.tables_zeroed, (unsigned)info.slot_free_after); |
| 370 | #endif |
| 371 | lt_counts[r.cls]++; |
| 372 | return; |
| 373 | } |
| 374 | ck_printf("osc_launch: %s %s -> NOT_RUN (unit was not admitted)\n", s->unit, s->fn); |
| 375 | } |
| 376 | |
| 377 | #ifdef CK_OSC_RECEIPT_TEST |
| 378 | /* aienos#383 receipt self-test: every launch of a01_valid_min.unit add(a, b), each with one receipt from the Store |
| 379 | * (tests/fixtures/osc_receipt, minted by omega checker2 with its TEST key; a01.owner.rcpt, an OWNER receipt for the |
| 380 | * same unit signed with the owner key, tests/fixtures/osc_receipt_owner, aienos#387) or none. Only a valid |
| 381 | * receipt may run the unit; every refusal names its reason and maps nothing. Variant 1 and 2 change the policy for |
| 382 | * that one launch (release mode; the TEST key fingerprint revoked), to show the release rule, the owner receipt |
| 383 | * under it and key revocation in one boot. */ |
| 384 | static void rt_all(void) |
| 385 | { |
| 386 | static const struct lt_step steps[] = { |
| 387 | { "a01_valid_min.unit", "add", 2, { 1, 2 }, 0, 0, 0, "a01.rcpt", 0 }, |
| 388 | { "a01_valid_min.unit", "add", 2, { 2, 3 }, 0, 0, 0, "-", 0 }, |
| 389 | { "a01_valid_min.unit", "add", 2, { 3, 4 }, 0, 0, 0, "n_altered.rcpt", 0 }, |
| 390 | { "a01_valid_min.unit", "add", 2, { 4, 5 }, 0, 0, 0, "n_stale.rcpt", 0 }, |
| 391 | { "a01_valid_min.unit", "add", 2, { 5, 6 }, 0, 0, 0, "n_revoked_id.rcpt", 0 }, |
| 392 | { "a01_valid_min.unit", "add", 2, { 6, 7 }, 0, 0, 0, "a01.rcpt", 2 }, |
| 393 | { "a01_valid_min.unit", "add", 2, { 7, 8 }, 0, 0, 0, "n_expired.rcpt", 0 }, |
| 394 | { "a01_valid_min.unit", "add", 2, { 8, 9 }, 0, 0, 0, "n_future.rcpt", 0 }, |
| 395 | { "a01_valid_min.unit", "add", 2, { 9, 10 }, 0, 0, 0, "n_target.rcpt", 0 }, |
| 396 | { "a01_valid_min.unit", "add", 2, { 10, 11 }, 0, 0, 0, "l01.rcpt", 0 }, |
| 397 | { "a01_valid_min.unit", "add", 2, { 11, 12 }, 0, 0, 0, "n_program.rcpt", 0 }, |
| 398 | { "a01_valid_min.unit", "add", 2, { 12, 13 }, 0, 0, 0, "n_length.rcpt", 0 }, |
| 399 | { "a01_valid_min.unit", "add", 2, { 13, 14 }, 0, 0, 0, "a01.rcpt", 1 }, |
| 400 | { "a01_valid_min.unit", "add", 2, { 14, 15 }, 0, 0, 0, "n_firmware.rcpt", 0 }, |
| 401 | { "a01_valid_min.unit", "add", 2, { 15, 16 }, 0, 0, 0, "n_deps.rcpt", 0 }, |
| 402 | { "a01_valid_min.unit", "add", 2, { 16, 17 }, 0, 0, 0, "n_checker.rcpt", 0 }, |
| 403 | { "a01_valid_min.unit", "add", 2, { 17, 18 }, 0, 0, 0, "a01.owner.rcpt", 1 }, |
| 404 | { "a01_valid_min.unit", "add", 2, { 18, 19 }, 0, 0, 0, "a01.rcpt", 3 }, |
| 405 | { "a01_valid_min.unit", "add", 2, { 20, 22 }, 0, 0, 0, "a01.rcpt", 0 }, |
| 406 | }; |
| 407 | ck_printf("osc_rcpt_rehash: TEST-ONLY post-admission code byte flip; QEMU (aarch64 virt), TEST signer, not physical\n"); |
| 408 | ck_printf("osc_rcpt_selftest: launches=%u unit=a01_valid_min.unit; QEMU (aarch64 virt), TEST signer, not physical\n", |
| 409 | (unsigned)(sizeof steps / sizeof steps[0])); |
| 410 | for (unsigned i = 0; i < sizeof steps / sizeof steps[0]; i++) |
| 411 | lt_run(&steps[i]); |
| 412 | ck_printf("osc_rcpt_launches: returned=%u trapped=%u refused=%u unknown=%u; QEMU (aarch64 virt), TEST signer, not " |
| 413 | "physical\n", |
| 414 | lt_counts[OSC_RES_RETURNED], lt_counts[OSC_RES_TRAPPED], lt_counts[OSC_RES_REFUSED], |
| 415 | lt_counts[OSC_RES_UNKNOWN]); |
| 416 | } |
| 417 | #endif |
| 418 | |
| 419 | static void lt_all(void) |
| 420 | { |
| 421 | #ifdef CK_OSC_RECEIPT_TEST |
| 422 | rt_all(); |
| 423 | return; |
| 424 | #endif |
| 425 | uint64_t sa, sl; |
| 426 | ck_osc_state_range(&sa, &sl); |
| 427 | lt_ws[6].mem = (uint8_t *)(uintptr_t)sa; /* a workspace aimed at the launcher's own state */ |
| 428 | lt_ws[6].pages = 1; |
| 429 | memcpy(lt_ws_mem2 + 64, "Zeta", 5); /* an input buffer that lies inside workspace 8 (index 7) */ |
| 430 | *(uint32_t *)(lt_ws_mem1 + 512) = 0xd65f03c0u; /* a valid `ret` in workspace 1, cell 64, for jump_ws */ |
| 431 | static const struct lt_step steps[] = { |
| 432 | { "a01_valid_min.unit", "add", 2, { 1000000007, 998244353 }, 0, 0, 0, 0, 0 }, |
| 433 | { "a01_valid_min.unit", "add", 1, { 5 }, 0, 0, 0, 0, 0 }, |
| 434 | { "a01_valid_min.unit", "nosuch", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 435 | { "a01_valid_min.unit", "ADD", 2, { 1, 2 }, 0, 0, 0, 0, 0 }, |
| 436 | { "a01_valid_min.unit", "first_byte", 2, { IN_PTR, 4 }, "Zeta", 0, 0, 0, 0 }, |
| 437 | { "a01_valid_min.unit", "first_byte", 2, { 0x1000, 16 }, "Zeta", 0, 0, 0, 0 }, |
| 438 | { "l01_launch_fns.unit", "trap", 1, { 3 }, 0, 0, 0, 0, 0 }, |
| 439 | { "l01_launch_fns.unit", "trap", 1, { 14 }, 0, 0, 0, 0, 0 }, |
| 440 | { "l01_launch_fns.unit", "trap", 1, { 15 }, 0, 0, 0, 0, 0 }, |
| 441 | { "l01_launch_fns.unit", "bare_brk", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 442 | { "l01_launch_fns.unit", "spin", 0, { 0 }, 0, 0, 5, 0, 0 }, |
| 443 | { "a01_valid_min.unit", "add", 2, { 40, 2 }, 0, 0, 0, 0, 0 }, |
| 444 | { "l01_launch_fns.unit", "peek0", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 445 | { "a01_valid_min.unit", "add", 2, { 20, 22 }, 0, 0, 0, 0, 0 }, |
| 446 | { "l01_launch_fns.unit", "poke_rt", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 447 | { "l01_launch_fns.unit", "exec_stack", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 448 | { "l01_launch_fns.unit", "overflow", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 449 | { "a01_valid_min.unit", "add", 2, { 6, 36 }, 0, 0, 0, 0, 0 }, |
| 450 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR, 1 }, 0, 1, 0, 0, 0 }, |
| 451 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR, 1 }, 0, 1, 0, 0, 0 }, |
| 452 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR, 1 }, 0, 1, 0, 0, 0 }, |
| 453 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR, 1 }, 0, 2, 0, 0, 0 }, |
| 454 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 4, 1 }, 0, 1, 0, 0, 0 }, |
| 455 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 94200, 1 }, 0, 3, 0, 0, 0 }, |
| 456 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 94200, 1 }, 0, 3, 0, 0, 0 }, |
| 457 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR, 1 }, 0, 4, 0, 0, 0 }, |
| 458 | { "a06_valid_limits_at_max.unit", "add", 2, { 1, 2 }, 0, 0, 0, 0, 0 }, |
| 459 | { "l02_stack_over.unit", "peek0", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 460 | { "a01_valid_min.unit", "add", 2, { 3, 4 }, 0, 0, 0, 0, 0 }, |
| 461 | { "l01_launch_fns.unit", "peek_kernel", 1, { KERN_PTR }, 0, 0, 0, 0, 0 }, |
| 462 | { "a01_valid_min.unit", "add", 2, { 5, 6 }, 0, 0, 0, 0, 0 }, |
| 463 | { "l01_launch_fns.unit", "jump_ws", 2, { WS_PTR | 512, 1 }, 0, 1, 0, 0, 0 }, |
| 464 | { "a01_valid_min.unit", "add", 2, { 7, 8 }, 0, 0, 0, 0, 0 }, |
| 465 | { "l01_launch_fns.unit", "jump_in", 2, { IN_PTR, 4 }, "\xc0\x03\x5f\xd6", 0, 0, 0, 0 }, |
| 466 | { "a01_valid_min.unit", "add", 2, { 9, 10 }, 0, 0, 0, 0, 0 }, |
| 467 | { "l01_launch_fns.unit", "dirty", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 468 | { "l01_launch_fns.unit", "regs_or", 0, { 0 }, 0, 0, 0, 0, 0 }, |
| 469 | { "l01_launch_fns.unit", "touch_past", 2, { WS_PTR | 16376, 1 }, 0, 1, 0, 0, 0 }, |
| 470 | { "a01_valid_min.unit", "add", 2, { 11, 12 }, 0, 0, 0, 0, 0 }, |
| 471 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 8, 1 }, 0, 5, 0, 0, 0 }, |
| 472 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 16, 1 }, 0, 6, 0, 0, 0 }, |
| 473 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 24, 1 }, 0, 7, 0, 0, 0 }, |
| 474 | { "l01_launch_fns.unit", "counter", 2, { WS_PTR | 32, 1 }, (const char *)(lt_ws_mem2 + 64), 8, 0, 0, 0 }, |
| 475 | { "a01_valid_min.unit", "add", 2, { 13, 14 }, 0, 0, 0, 0, 0 }, |
| 476 | }; |
| 477 | ck_printf("osc_launch_policy: units=%u code_max=%u stack_max=%u in_max=%u ws_max=%u pages; 1 tick = 10 ms; " |
| 478 | "QEMU (aarch64 virt), TEST signer, not physical\n", |
| 479 | lt_n, (unsigned)CK_OSC_CODE_MAX_BYTES, (unsigned)CK_OSC_STACK_MAX_BYTES, (unsigned)CK_OSC_IN_MAX_BYTES, |
| 480 | (unsigned)OSC_WS_MAX_PAGES); |
| 481 | for (unsigned i = 0; i < sizeof steps / sizeof steps[0]; i++) |
| 482 | lt_run(&steps[i]); |
| 483 | ck_printf("osc_launches: returned=%u trapped=%u refused=%u unknown=%u; QEMU (aarch64 virt), TEST signer, not " |
| 484 | "physical\n", |
| 485 | lt_counts[OSC_RES_RETURNED], lt_counts[OSC_RES_TRAPPED], lt_counts[OSC_RES_REFUSED], |
| 486 | lt_counts[OSC_RES_UNKNOWN]); |
| 487 | } |
| 488 | #endif |
| 489 | |
| 490 | int ck_osc_is_unit(const uint8_t *b, size_t len) |
| 491 | { |
| 492 | return b && len >= 8 && memcmp(b, "OSCUNIT\0", 8) == 0; |
| 493 | } |
| 494 | |
| 495 | unsigned ck_osc_candidate(const char *name, const uint8_t *b, size_t len) |
| 496 | { |
| 497 | struct osc_policy p; |
| 498 | ck_osc_policy(&p); |
| 499 | if (!announced) { |
| 500 | announced = 1; |
| 501 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 502 | ck_printf("osc_policy: mode=qualification test_anchors=1 (throwaway TEST key, TEST ONLY) owner_anchors=0 " |
| 503 | "domains=1 gen_width=32; QEMU, not physical\n"); |
| 504 | #else |
| 505 | ck_printf("osc_policy: mode=release test_anchors=0 owner_anchors=0 (none provisioned) " |
| 506 | "domains=1 gen_width=32; QEMU, not physical\n"); |
| 507 | #endif |
| 508 | ck_printf("osc_receipt_policy: mode=%s receipt_test_anchors=%u receipt_owner_anchors=%u (the owner root) " |
| 509 | "checkers=%u target=%s epoch_floor=%u epoch_now=%u revoked_ids=%u revoked_keys=0 " |
| 510 | "firmware=none dependencies=none producer=none; launch requires a valid receipt; QEMU, not " |
| 511 | "physical\n", |
| 512 | p.release ? "release" : "qualification", p.n_rcpt_test, p.n_rcpt_owner, p.n_rcpt_checkers, |
| 513 | OSC_RCPT_TARGET_NAME, |
| 514 | (unsigned)CK_OSC_RCPT_EPOCH_FLOOR, (unsigned)CK_OSC_RCPT_EPOCH_NOW, p.n_rcpt_revoked_ids); |
| 515 | } |
| 516 | unsigned rc = osc_unit_admit(b, len, &p, &acc); |
| 517 | seen++; |
| 518 | if (rc == OSC_OK) { |
| 519 | char d[65], i[65]; |
| 520 | hex(d, acc.unit_digest, 32); |
| 521 | hex(i, acc.ir_sha256, 32); |
| 522 | accepted++; |
| 523 | #ifdef CK_OSC_LAUNCH_TEST |
| 524 | lt_save(name, b, len); |
| 525 | #endif |
| 526 | #ifdef CK_OSH_TEST |
| 527 | ck_osh_save(name, b, len, &acc); |
| 528 | #endif |
| 529 | ck_printf("osc_unit: %s ACCEPT unit_digest=%s program_id=%s funcs=%u caps=%u signer=%s\n", name, d, i, |
| 530 | (unsigned)acc.function_count, (unsigned)acc.cap_count, |
| 531 | acc.signer_class == OSC_SIGNER_TEST ? "TEST" : "OWNER"); |
| 532 | } else { |
| 533 | refused++; |
| 534 | ck_printf("osc_unit: %s REFUSED code=%u name=%s\n", name, rc, osc_code_name(rc)); |
| 535 | } |
| 536 | return rc; |
| 537 | } |
| 538 | |
| 539 | void ck_osc_oversize(const char *name, uint64_t len) |
| 540 | { |
| 541 | oversize++; |
| 542 | ck_printf("osc_unit: %s REFUSED code=%u name=%s reason=staging_too_large bytes=%llu limit=%u " |
| 543 | "(content not read, format unknown; refusal by the Store size limit)\n", |
| 544 | name, (unsigned)OSC_RESOURCE_UNAVAILABLE, osc_code_name(OSC_RESOURCE_UNAVAILABLE), |
| 545 | (unsigned long long)len, (unsigned)OSC_STAGING_MAX); |
| 546 | } |
| 547 | |
| 548 | void ck_osc_summary(void) |
| 549 | { |
| 550 | if (rcpt_seen) |
| 551 | ck_printf("osc_receipts: seen=%u stored=%u dropped=%u (each is verified when a launch presents it)\n", rcpt_seen, |
| 552 | rcpt_n, rcpt_dropped); |
| 553 | if (seen || oversize) |
| 554 | #if defined(CK_OSC_LAUNCH_TEST) || defined(CK_OSH_TEST) |
| 555 | { |
| 556 | ck_printf("osc_units: seen=%u accepted=%u refused=%u oversize=%u (admission; launches follow)\n", seen, accepted, |
| 557 | refused, oversize); |
| 558 | #ifdef CK_OSC_LAUNCH_TEST |
| 559 | lt_all(); |
| 560 | #else |
| 561 | ck_osh_all(); |
| 562 | #endif |
| 563 | } |
| 564 | #else |
| 565 | ck_printf("osc_units: seen=%u accepted=%u refused=%u oversize=%u (admission only, nothing launched)\n", seen, accepted, |
| 566 | refused, oversize); |
| 567 | #endif |
| 568 | } |