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 / artifact_loader.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 | /* artifact_loader.c -- P2 sealed-artifact loader of the C kernel core. |
| 18 | * |
| 19 | * Port of crates/aienos-kernel/src/artifact_loader.rs (load_bound, |
| 20 | * process_candidate, shadow code alias, W^X audit, receipts) and |
| 21 | * task_runtime.rs (EL0 run, syscall table, budgets) onto the C core. The |
| 22 | * format, signature and admission logic is native/kernel/artifact/ (ported |
| 23 | * from aienos-artifact and admission.rs, cross-checked byte for byte against |
| 24 | * the Rust tool). |
| 25 | * |
| 26 | * Candidates come from QEMU's fw_cfg file "opt/aienos/artifacts" (a bundle, |
| 27 | * see tools/ck_artifact_tool.c), not from ESP files read by the Rust UEFI |
| 28 | * loader: the C boot stub (native/boot) does not read files. The fw_cfg base |
| 29 | * is taken from the ACPI DSDT (device QEMU0002); nothing is assumed. |
| 30 | * |
| 31 | * Trust: the qualification build (CK_SEED0B_TEST_ANCHOR=1) trusts the |
| 32 | * RFC 8032 TEST 1 public key ONLY (SEED-0B, TEST ONLY). The ordinary build |
| 33 | * has no anchors and refuses every candidate. No real key material here. |
| 34 | * |
| 35 | * Differences from the Rust kernel, all recorded in GATES.md: |
| 36 | * - one address space at a time on ASID 0 with a full TLB flush on every |
| 37 | * TTBR0 switch (Rust: TASK_ASID 2); |
| 38 | * - EL0 FP/SIMD traps (CPACR FPEN = 01) instead of zeroed FP state; |
| 39 | * - content+table frames and shadow frames are each one contiguous batch; |
| 40 | * - the loader scheduler is a single occupancy flag (one task at a time, |
| 41 | * LOADED_TASK_SLOTS = 4 for the availability snapshot, as in Rust). */ |
| 42 | |
| 43 | #include "arch.h" |
| 44 | #include "ck_internal.h" |
| 45 | #include "pt.h" |
| 46 | #include "ck_artifact.h" |
| 47 | #include "osc_admit.h" |
| 48 | #include "sha256.h" |
| 49 | |
| 50 | #ifndef AIENOS_COMMIT |
| 51 | #define AIENOS_COMMIT "unknown" |
| 52 | #endif |
| 53 | |
| 54 | #define PAGE 4096ull |
| 55 | #define ENTRIES 512u |
| 56 | #define TABLE_FRAMES 4u |
| 57 | #define MAX_BATCH 160u |
| 58 | #define MAX_CODE_PAGES 64u |
| 59 | #define MAX_DATA_PAGES 64u |
| 60 | #define MAX_STACK_PAGES 16u |
| 61 | #define TASK_CAPS 16u |
| 62 | #define LOADED_TASK_SLOTS 4u |
| 63 | #define MAX_CANDIDATES 48u |
| 64 | #define MAX_NAME 32u |
| 65 | |
| 66 | #define ADDR_MASK 0x0000fffffffff000ull |
| 67 | #define ATTR_MASK 0xffff000000000ffcull |
| 68 | #define BLOCK_1G 0x0000ffffc0000000ull |
| 69 | #define BLOCK_2M 0x0000ffffffe00000ull |
| 70 | #define DESC_TYPE 3ull |
| 71 | #define DESC_TABLE 3ull |
| 72 | #define DESC_BLOCK 1ull |
| 73 | #define AP_RO (1ull << 7) |
| 74 | #define PXN (1ull << 53) |
| 75 | #define UXN (1ull << 54) |
| 76 | #define LEAF_COMMON \ |
| 77 | (CK_PTE_ATTRIDX(CK_MAIR_IDX_NORMAL) | CK_PTE_SH_INNER | CK_PTE_AF | (1ull << 11) | PXN | \ |
| 78 | DESC_TABLE) |
| 79 | #define USER_CODE(pa) (((pa) & ADDR_MASK) | LEAF_COMMON | (3ull << 6)) |
| 80 | #define USER_DATA(pa) (((pa) & ADDR_MASK) | LEAF_COMMON | (1ull << 6) | UXN) |
| 81 | |
| 82 | #define SYS_EXIT 2u |
| 83 | #define SYS_OBJECT_READ 6u |
| 84 | #define SYS_OBJECT_WRITE 7u |
| 85 | #define DENIED (~0ull) |
| 86 | #define SEED_OBJECT_ID 1u |
| 87 | #define SEED_OBJECT_BYTES 32u |
| 88 | #define SPSR_EL1H_MASKED 0x3c5ull |
| 89 | #define RIGHT_READ 0x1u |
| 90 | #define RIGHT_WRITE 0x2u |
| 91 | |
| 92 | struct ck_artifact_summary { |
| 93 | unsigned candidates, admitted, rejected; |
| 94 | }; |
| 95 | static struct ck_artifact_summary ck_artifact_summary; |
| 96 | |
| 97 | extern struct ck_frame *(*ck_tick_switch)(struct ck_frame *f); |
| 98 | extern int (*ck_lower_sync)(struct ck_frame *f, uint64_t esr); |
| 99 | void ck_el0_enter(uint64_t entry, uint64_t user_sp, const uint64_t regs[31]); |
| 100 | void ck_el0_resume(void); |
| 101 | |
| 102 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 103 | static const uint8_t (*const anchors)[32] = &cka_test1_pk; |
| 104 | #define NANCHORS 1u |
| 105 | #define FEATURE_TEST_ANCHOR 1u |
| 106 | #else |
| 107 | static const uint8_t (*const anchors)[32] = 0; |
| 108 | #define NANCHORS 0u |
| 109 | #define FEATURE_TEST_ANCHOR 0u |
| 110 | #endif |
| 111 | |
| 112 | /* ============================ platform ============================== */ |
| 113 | |
| 114 | static inline uint64_t *tbl(uint64_t pa) { return (uint64_t *)(uintptr_t)pa; } |
| 115 | static inline uint8_t *mem(uint64_t pa) { return (uint8_t *)(uintptr_t)pa; } |
| 116 | static inline unsigned tidx(uint64_t va, unsigned level) |
| 117 | { |
| 118 | return (unsigned)((va >> (39 - 9 * level)) & 511); |
| 119 | } |
| 120 | static inline uint64_t free_frames(void) { return ck_mm_free_frames(); } |
| 121 | |
| 122 | static void clean_range(uint64_t pa, uint64_t len, int icache) |
| 123 | { |
| 124 | uint64_t ctr = ck_rd(ctr_el0); |
| 125 | uint64_t dline = 4ull << ((ctr >> 16) & 0xf), iline = 4ull << (ctr & 0xf); |
| 126 | uint64_t end = pa + len; |
| 127 | for (uint64_t x = pa & ~(dline - 1); x < end; x += dline) |
| 128 | __asm__ volatile("dc civac, %0" ::"r"(x) : "memory"); |
| 129 | ck_dsb_ish(); |
| 130 | if (icache) { |
| 131 | for (uint64_t x = pa & ~(iline - 1); x < end; x += iline) |
| 132 | __asm__ volatile("ic ivau, %0" ::"r"(x) : "memory"); |
| 133 | ck_dsb_ish(); |
| 134 | } |
| 135 | ck_isb(); |
| 136 | } |
| 137 | |
| 138 | static void swap_ttbr0(uint64_t ttbr0) |
| 139 | { |
| 140 | ck_dsb_ish(); |
| 141 | ck_wr(ttbr0_el1, ttbr0); |
| 142 | ck_isb(); |
| 143 | __asm__ volatile("tlbi vmalle1" ::: "memory"); |
| 144 | ck_dsb_ish(); |
| 145 | ck_isb(); |
| 146 | } |
| 147 | |
| 148 | /* ============================ task layout =========================== */ |
| 149 | |
| 150 | struct layout { |
| 151 | unsigned window; |
| 152 | uint64_t base; |
| 153 | unsigned cp, dp, sp; /* code, data, stack pages */ |
| 154 | uint64_t code_len, data_len, data_mlen, entry; |
| 155 | }; |
| 156 | |
| 157 | static unsigned code_first(void) { return 1; } |
| 158 | static unsigned data_first(const struct layout *l) { return code_first() + l->cp + 1; } |
| 159 | static unsigned stack_first(const struct layout *l) { return data_first(l) + l->dp + 1; } |
| 160 | static unsigned window_pages(const struct layout *l) { return stack_first(l) + l->sp + 1; } |
| 161 | static unsigned content_frames(const struct layout *l) { return l->cp + l->dp + l->sp; } |
| 162 | static uint64_t code_base(const struct layout *l) { return l->base + code_first() * PAGE; } |
| 163 | static uint64_t code_end(const struct layout *l) { return code_base(l) + l->cp * PAGE; } |
| 164 | static uint64_t data_base(const struct layout *l) { return l->base + data_first(l) * PAGE; } |
| 165 | static uint64_t data_end(const struct layout *l) { return data_base(l) + l->dp * PAGE; } |
| 166 | static uint64_t stack_base(const struct layout *l) { return l->base + stack_first(l) * PAGE; } |
| 167 | static uint64_t stack_top(const struct layout *l) { return stack_base(l) + l->sp * PAGE; } |
| 168 | |
| 169 | /* TaskLayout::new: 0 or CKA_RESOURCE_LIMIT. */ |
| 170 | static int layout_new(struct layout *l, unsigned window, const struct cka_env *env, |
| 171 | uint64_t code_len, uint64_t data_len, uint64_t data_mlen, uint64_t entry) |
| 172 | { |
| 173 | l->window = window; |
| 174 | l->base = (uint64_t)window << 39; |
| 175 | l->cp = env->code_pages; |
| 176 | l->dp = env->data_pages; |
| 177 | l->sp = env->stack_pages; |
| 178 | l->code_len = code_len; |
| 179 | l->data_len = data_len; |
| 180 | l->data_mlen = data_mlen; |
| 181 | l->entry = entry; |
| 182 | if (window < 1 || window >= 256 || env->code_pages == 0 || env->code_pages > MAX_CODE_PAGES || |
| 183 | env->data_pages == 0 || env->data_pages > MAX_DATA_PAGES || env->stack_pages == 0 || |
| 184 | env->stack_pages > MAX_STACK_PAGES || code_len == 0 || code_len > l->cp * PAGE || |
| 185 | data_len > data_mlen || data_mlen > l->dp * PAGE || (entry & 3) || entry + 4 > code_len || |
| 186 | window_pages(l) > ENTRIES) |
| 187 | return CKA_RESOURCE_LIMIT; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static uint64_t expected_leaf(const struct layout *l, unsigned index, const uint64_t *frames) |
| 192 | { |
| 193 | unsigned d = data_first(l), s = stack_first(l); |
| 194 | if (index >= 1 && index < 1 + l->cp) |
| 195 | return USER_CODE(frames[index - 1]); |
| 196 | if (index >= d && index < d + l->dp) |
| 197 | return USER_DATA(frames[l->cp + index - d]); |
| 198 | if (index >= s && index < s + l->sp) |
| 199 | return USER_DATA(frames[l->cp + l->dp + index - s]); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* ============================ reports =============================== */ |
| 204 | |
| 205 | struct bindings { |
| 206 | uint8_t id[32], payload[32], signer[32], policy[32], requested[32], granted[32], |
| 207 | resources[32]; |
| 208 | int mapped_bytes_matched, wx_sealed; |
| 209 | }; |
| 210 | |
| 211 | enum { ST_NOT_RUN, ST_EXITED, ST_TIMEOUT, ST_FAULT, ST_BAD_SYSCALL, ST_OVERRUN }; |
| 212 | struct outcome { |
| 213 | unsigned status; |
| 214 | uint64_t value; /* exit code / syscall number */ |
| 215 | uint64_t esr, far, elr; |
| 216 | uint32_t syscalls, reads_ok, denials, write_denials, invalid_handle; |
| 217 | }; |
| 218 | |
| 219 | struct report { |
| 220 | int admitted; |
| 221 | unsigned stage, error; /* failed stage and reason code (0 = none) */ |
| 222 | int have_id; |
| 223 | uint8_t id[32]; |
| 224 | int tier_test; |
| 225 | uint64_t frames_reserved, free_before, free_after; |
| 226 | unsigned caps_installed, caps_live_after; |
| 227 | struct cka_cap granted[TASK_CAPS]; |
| 228 | uint64_t code_base, code_end, data_base, data_end, stack_base, stack_end; |
| 229 | int wx_enforced, byte_chain; |
| 230 | struct outcome out; |
| 231 | struct bindings b; |
| 232 | }; |
| 233 | |
| 234 | static int reclaimed(const struct report *r) |
| 235 | { |
| 236 | return r->free_after == r->free_before && r->caps_live_after == 0; |
| 237 | } |
| 238 | |
| 239 | /* ============================ loader caps =========================== */ |
| 240 | |
| 241 | struct cap_slot { |
| 242 | int live; |
| 243 | uint32_t gen, resource, rights; |
| 244 | }; |
| 245 | static struct cap_slot caps[TASK_CAPS]; |
| 246 | static uint32_t cap_gens[TASK_CAPS]; |
| 247 | |
| 248 | static void caps_reset(void) |
| 249 | { |
| 250 | memset(caps, 0, sizeof caps); |
| 251 | memset(cap_gens, 0, sizeof cap_gens); |
| 252 | } |
| 253 | |
| 254 | static int caps_insert(uint32_t resource, uint32_t rights, uint64_t *raw) |
| 255 | { |
| 256 | for (unsigned i = 0; i < TASK_CAPS; i++) { |
| 257 | if (caps[i].live || cap_gens[i] == 0xffffffffu) |
| 258 | continue; |
| 259 | uint32_t g = ++cap_gens[i]; |
| 260 | caps[i] = (struct cap_slot){1, g, resource, rights}; |
| 261 | *raw = (uint64_t)g << 32 | i; |
| 262 | return 0; |
| 263 | } |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | enum { LK_OK = 0, LK_INVALID = 1, LK_MISSING_RIGHTS = 2 }; |
| 268 | static int caps_lookup(uint64_t raw, uint32_t right, uint32_t *resource) |
| 269 | { |
| 270 | uint32_t index = (uint32_t)raw, gen = (uint32_t)(raw >> 32); |
| 271 | if (gen == 0 || index >= TASK_CAPS || !caps[index].live || caps[index].gen != gen) |
| 272 | return LK_INVALID; |
| 273 | if ((caps[index].rights & right) != right) |
| 274 | return LK_MISSING_RIGHTS; |
| 275 | *resource = caps[index].resource; |
| 276 | return LK_OK; |
| 277 | } |
| 278 | |
| 279 | static void caps_remove(uint64_t raw) |
| 280 | { |
| 281 | uint32_t index = (uint32_t)raw, gen = (uint32_t)(raw >> 32); |
| 282 | if (gen && index < TASK_CAPS && caps[index].live && caps[index].gen == gen) |
| 283 | caps[index].live = 0; |
| 284 | } |
| 285 | |
| 286 | static unsigned caps_live(void) |
| 287 | { |
| 288 | unsigned n = 0; |
| 289 | for (unsigned i = 0; i < TASK_CAPS; i++) |
| 290 | n += caps[i].live ? 1u : 0u; |
| 291 | return n; |
| 292 | } |
| 293 | |
| 294 | /* ============================ loaded task =========================== */ |
| 295 | |
| 296 | struct task { |
| 297 | struct layout l; |
| 298 | uint64_t batch, nframes; /* content + table frames, contiguous */ |
| 299 | uint64_t shadow, nshadow, used; /* shadow batch, frames used by the alias */ |
| 300 | uint64_t frames[MAX_BATCH]; |
| 301 | uint64_t root; |
| 302 | uint64_t entry_pc, sp, args[4]; |
| 303 | unsigned ngrants; |
| 304 | struct cka_cap grants[TASK_CAPS]; |
| 305 | uint64_t handles[TASK_CAPS]; |
| 306 | uint64_t budget_cpu, budget_elapsed; |
| 307 | uint32_t budget_syscalls; |
| 308 | uint8_t code_digest[32]; |
| 309 | }; |
| 310 | static struct task task; |
| 311 | static int slot_busy; /* loader scheduler: one admitted task at a time */ |
| 312 | |
| 313 | static int release(struct task *t) |
| 314 | { |
| 315 | int ok = 1; |
| 316 | if (t->nframes) { |
| 317 | memset(mem(t->batch), 0, t->nframes * PAGE); |
| 318 | if (ck_mm_frames_free(t->batch, t->nframes)) |
| 319 | ok = 0; |
| 320 | t->nframes = 0; |
| 321 | } |
| 322 | if (t->nshadow) { |
| 323 | memset(mem(t->shadow), 0, t->nshadow * PAGE); |
| 324 | if (ck_mm_frames_free(t->shadow, t->nshadow)) |
| 325 | ok = 0; |
| 326 | t->nshadow = 0; |
| 327 | } |
| 328 | slot_busy = 0; |
| 329 | return ok; |
| 330 | } |
| 331 | |
| 332 | /* kernel_translation: the descriptor mapping va (level-3 page or L1/L2 |
| 333 | * block) and its level, or -1. */ |
| 334 | static int translate(uint64_t root, uint64_t va, uint64_t *desc) |
| 335 | { |
| 336 | uint64_t table = root; |
| 337 | for (unsigned level = 0; level < 4; level++) { |
| 338 | uint64_t e = tbl(table)[tidx(va, level)]; |
| 339 | if (level == 3 && (e & DESC_TYPE) == DESC_TABLE) { |
| 340 | *desc = e; |
| 341 | return 3; |
| 342 | } |
| 343 | if ((level == 1 || level == 2) && (e & DESC_TYPE) == DESC_BLOCK) { |
| 344 | *desc = e; |
| 345 | return (int)level; |
| 346 | } |
| 347 | if (level <= 2 && (e & DESC_TYPE) == DESC_TABLE) { |
| 348 | table = e & ADDR_MASK; |
| 349 | continue; |
| 350 | } |
| 351 | return -1; |
| 352 | } |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | static int shadow_needed(uint64_t kroot, const uint64_t *frames, unsigned n, uint64_t *count) |
| 357 | { |
| 358 | uint64_t keys[MAX_CODE_PAGES][3]; |
| 359 | uint64_t c = 0, d; |
| 360 | for (unsigned i = 0; i < n; i++) { |
| 361 | uint64_t pa = frames[i]; |
| 362 | if (translate(kroot, pa, &d) < 0) |
| 363 | return CKL_MAPPING; |
| 364 | uint64_t k[3] = {pa >> 39, pa >> 30, pa >> 21}; |
| 365 | for (unsigned level = 0; level < 3; level++) { |
| 366 | int seen = 0; |
| 367 | for (unsigned j = 0; j < i && !seen; j++) |
| 368 | seen = keys[j][level] == k[level]; |
| 369 | if (!seen) |
| 370 | c++; |
| 371 | } |
| 372 | keys[i][0] = k[0]; |
| 373 | keys[i][1] = k[1]; |
| 374 | keys[i][2] = k[2]; |
| 375 | } |
| 376 | *count = c; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int shadow_alias(struct task *t) |
| 381 | { |
| 382 | uint64_t used = 0; |
| 383 | for (unsigned f = 0; f < t->l.cp; f++) { |
| 384 | uint64_t pa = t->frames[f], table = t->root; |
| 385 | for (unsigned level = 0; level < 3; level++) { |
| 386 | unsigned index = tidx(pa, level); |
| 387 | uint64_t entry = tbl(table)[index], child = entry & ADDR_MASK; |
| 388 | int priv = 0; |
| 389 | for (uint64_t j = 0; j < used && !priv; j++) |
| 390 | priv = t->shadow + j * PAGE == child; |
| 391 | if ((entry & DESC_TYPE) == DESC_TABLE && priv) { |
| 392 | table = child; |
| 393 | continue; |
| 394 | } |
| 395 | if (used >= t->nshadow) |
| 396 | return CKL_MAPPING; |
| 397 | uint64_t fresh = t->shadow + used * PAGE; |
| 398 | used++; |
| 399 | if ((entry & DESC_TYPE) == DESC_TABLE) { |
| 400 | memcpy(mem(fresh), mem(child), PAGE); |
| 401 | } else if (level == 1 && (entry & DESC_TYPE) == DESC_BLOCK) { |
| 402 | uint64_t attrs = entry & ATTR_MASK, base = entry & BLOCK_1G; |
| 403 | for (uint64_t k = 0; k < ENTRIES; k++) |
| 404 | tbl(fresh)[k] = (base + (k << 21)) | attrs | DESC_BLOCK; |
| 405 | } else if (level == 2 && (entry & DESC_TYPE) == DESC_BLOCK) { |
| 406 | uint64_t attrs = entry & ATTR_MASK, base = entry & BLOCK_2M; |
| 407 | for (uint64_t k = 0; k < ENTRIES; k++) |
| 408 | tbl(fresh)[k] = (base + (k << 12)) | attrs | DESC_TABLE; |
| 409 | } else { |
| 410 | return CKL_MAPPING; |
| 411 | } |
| 412 | tbl(table)[index] = (entry & ~ADDR_MASK & ~DESC_TYPE) | fresh | DESC_TABLE; |
| 413 | table = fresh; |
| 414 | } |
| 415 | unsigned index = tidx(pa, 3); |
| 416 | uint64_t leaf = tbl(table)[index]; |
| 417 | if ((leaf & DESC_TYPE) != DESC_TABLE || (leaf & ADDR_MASK) != pa) |
| 418 | return CKL_MAPPING; |
| 419 | tbl(table)[index] = leaf | AP_RO | PXN | UXN; |
| 420 | } |
| 421 | t->used = used; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int audit_wx(const struct task *t) |
| 426 | { |
| 427 | const struct layout *l = &t->l; |
| 428 | unsigned content = content_frames(l); |
| 429 | uint64_t l0e = tbl(t->root)[l->window], l1 = l0e & ADDR_MASK; |
| 430 | if (l0e != ((t->frames[content + 1] & ADDR_MASK) | DESC_TABLE)) |
| 431 | return 0; |
| 432 | for (unsigned i = 1; i < ENTRIES; i++) |
| 433 | if (tbl(l1)[i]) |
| 434 | return 0; |
| 435 | uint64_t l2 = tbl(l1)[0] & ADDR_MASK; |
| 436 | if (l2 != t->frames[content + 2]) |
| 437 | return 0; |
| 438 | for (unsigned i = 1; i < ENTRIES; i++) |
| 439 | if (tbl(l2)[i]) |
| 440 | return 0; |
| 441 | uint64_t l3 = tbl(l2)[0] & ADDR_MASK; |
| 442 | if (l3 != t->frames[content + 3]) |
| 443 | return 0; |
| 444 | for (unsigned i = 0; i < ENTRIES; i++) { |
| 445 | uint64_t leaf = tbl(l3)[i]; |
| 446 | if (leaf != expected_leaf(l, i, t->frames)) |
| 447 | return 0; |
| 448 | int writable = !(leaf & AP_RO), executable = !(leaf & UXN) || !(leaf & PXN); |
| 449 | if (leaf && writable && executable) |
| 450 | return 0; |
| 451 | } |
| 452 | for (unsigned f = 0; f < l->cp; f++) { |
| 453 | uint64_t pa = t->frames[f], leaf; |
| 454 | if (translate(t->root, pa, &leaf) != 3) |
| 455 | return 0; |
| 456 | if ((leaf & ADDR_MASK) != pa || !(leaf & AP_RO) || !(leaf & PXN) || !(leaf & UXN)) |
| 457 | return 0; |
| 458 | } |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static void copy_section(const struct task *t, unsigned first, const uint8_t *src, uint64_t len) |
| 463 | { |
| 464 | for (uint64_t off = 0; off < len; off += PAGE) { |
| 465 | uint64_t n = len - off < PAGE ? len - off : PAGE; |
| 466 | memcpy(mem(t->frames[first + off / PAGE]), src + off, n); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | static void hash_loaded(const struct task *t, uint8_t payload[32], uint8_t code[32]) |
| 471 | { |
| 472 | sha256_ctx p, c; |
| 473 | sha256_init(&p); |
| 474 | sha256_init(&c); |
| 475 | for (unsigned i = 0; i < t->l.cp && i * PAGE < t->l.code_len; i++) { |
| 476 | uint64_t n = t->l.code_len - i * PAGE; |
| 477 | n = n < PAGE ? n : PAGE; |
| 478 | sha256_update(&p, mem(t->frames[i]), n); |
| 479 | sha256_update(&c, mem(t->frames[i]), n); |
| 480 | } |
| 481 | for (unsigned i = 0; i < t->l.dp && i * PAGE < t->l.data_len; i++) { |
| 482 | uint64_t n = t->l.data_len - i * PAGE; |
| 483 | n = n < PAGE ? n : PAGE; |
| 484 | sha256_update(&p, mem(t->frames[t->l.cp + i]), n); |
| 485 | } |
| 486 | sha256_final(&p, payload); |
| 487 | sha256_final(&c, code); |
| 488 | } |
| 489 | |
| 490 | static void code_digest(const struct task *t, uint8_t out[32]) |
| 491 | { |
| 492 | sha256_ctx c; |
| 493 | sha256_init(&c); |
| 494 | for (unsigned i = 0; i < t->l.cp && i * PAGE < t->l.code_len; i++) { |
| 495 | uint64_t n = t->l.code_len - i * PAGE; |
| 496 | sha256_update(&c, mem(t->frames[i]), n < PAGE ? n : PAGE); |
| 497 | } |
| 498 | sha256_final(&c, out); |
| 499 | } |
| 500 | |
| 501 | /* ============================ load_bound ============================ */ |
| 502 | |
| 503 | static struct cka_policy policy; |
| 504 | static struct cka_parsed parsed; |
| 505 | static struct cka_ident ident; |
| 506 | static struct cka_decision decision; |
| 507 | |
| 508 | /* Returns 0 (task loaded) or the failure stage in *stage and reason. */ |
| 509 | static unsigned load_bound(uint64_t staged, uint64_t len, struct bindings *b, unsigned *stage, |
| 510 | int *tier_test) |
| 511 | { |
| 512 | struct task *t = &task; |
| 513 | const uint8_t *bytes = mem(staged); |
| 514 | cka_policy_digest(&policy, b->policy); |
| 515 | unsigned free_slots = LOADED_TASK_SLOTS - (slot_busy ? 1u : 0u); |
| 516 | if (cka_identify(bytes, len, &parsed, &ident) == 0) { |
| 517 | memcpy(b->id, ident.id, 32); |
| 518 | memcpy(b->payload, ident.payload, 32); |
| 519 | memcpy(b->requested, ident.requested, 32); |
| 520 | memcpy(b->resources, ident.resources, 32); |
| 521 | } |
| 522 | uint8_t fp[32]; |
| 523 | int rc = cka_verify(bytes, len, anchors, NANCHORS, &parsed, &ident, fp); |
| 524 | if (rc) { |
| 525 | *stage = CKS_VERIFIED; |
| 526 | return (unsigned)rc; |
| 527 | } |
| 528 | memcpy(b->signer, fp, 32); |
| 529 | *tier_test = FEATURE_TEST_ANCHOR; |
| 530 | uint64_t ff = free_frames(); |
| 531 | uint32_t frames = 0; |
| 532 | if (free_slots) |
| 533 | frames = ff > TABLE_FRAMES ? (ff - TABLE_FRAMES > 0xffffffffull ? 0xffffffffu |
| 534 | : (uint32_t)(ff - TABLE_FRAMES)) |
| 535 | : 0; |
| 536 | struct cka_limits avail = { |
| 537 | .code_pages = frames, .data_pages = frames, .stack_pages = frames, .max_caps = TASK_CAPS, |
| 538 | .ipc_msgs = 0, .ipc_bytes = 0, .cpu = policy.limits.cpu, .elapsed = policy.limits.elapsed, |
| 539 | .syscalls = policy.limits.syscalls, |
| 540 | }; |
| 541 | rc = cka_evaluate(&policy, &parsed, fp, &avail, &decision); |
| 542 | if (rc) { |
| 543 | *stage = CKS_AUTHORIZED; |
| 544 | return (unsigned)rc; |
| 545 | } |
| 546 | memcpy(b->granted, decision.granted_digest, 32); |
| 547 | /* The admission binds the identity it was computed over (cka_evaluate |
| 548 | * works on the same parsed artifact, so this holds by construction). */ |
| 549 | if (decision.ngrants > TASK_CAPS) { |
| 550 | *stage = CKS_AUTHORIZED; |
| 551 | return CKA_RESOURCE_LIMIT; |
| 552 | } |
| 553 | uint64_t kroot = ck_mm_report()->root; |
| 554 | unsigned window = 0; |
| 555 | for (unsigned i = 1; i < 256 && !window; i++) |
| 556 | if (!tbl(kroot)[i]) |
| 557 | window = i; |
| 558 | if (!window) { |
| 559 | *stage = CKS_RESERVED; |
| 560 | return CKL_MAPPING; |
| 561 | } |
| 562 | memset(t, 0, sizeof *t); |
| 563 | const struct cka_section *cs = &parsed.code, *ds = &parsed.data; |
| 564 | rc = layout_new(&t->l, window, &parsed.env, cs->flen, ds->flen, ds->mlen, parsed.entry); |
| 565 | if (rc) { |
| 566 | *stage = CKS_AUTHORIZED; |
| 567 | return (unsigned)rc; |
| 568 | } |
| 569 | const uint8_t *code_src = bytes + parsed.payload_off; /* code_bytes(): payload[..flen] */ |
| 570 | const uint8_t *data_src = bytes + parsed.payload_off + ds->rel; |
| 571 | |
| 572 | /* ---- Reserved: scheduler slot, content+table frames, shadow frames. */ |
| 573 | *stage = CKS_RESERVED; |
| 574 | slot_busy = 1; |
| 575 | unsigned content = content_frames(&t->l), total = content + TABLE_FRAMES; |
| 576 | unsigned why; |
| 577 | if (total > MAX_BATCH || ck_mm_frames_alloc(total, &t->batch)) { |
| 578 | why = CKL_NO_FRAMES; |
| 579 | goto fail; |
| 580 | } |
| 581 | t->nframes = total; |
| 582 | for (unsigned i = 0; i < total; i++) |
| 583 | t->frames[i] = t->batch + i * PAGE; |
| 584 | uint64_t need; |
| 585 | if ((why = (unsigned)shadow_needed(kroot, t->frames, t->l.cp, &need))) |
| 586 | goto fail; |
| 587 | if (need > MAX_BATCH || ck_mm_frames_alloc(need, &t->shadow)) { |
| 588 | why = CKL_NO_FRAMES; |
| 589 | goto fail; |
| 590 | } |
| 591 | t->nshadow = need; |
| 592 | |
| 593 | /* ---- Mapped */ |
| 594 | *stage = CKS_MAPPED; |
| 595 | uint64_t l0 = t->frames[content], l1 = l0 + PAGE, l2 = l1 + PAGE, l3 = l2 + PAGE; |
| 596 | memset(mem(t->batch), 0, total * PAGE); |
| 597 | memset(mem(t->shadow), 0, need * PAGE); |
| 598 | memcpy(mem(l0), mem(kroot), PAGE); |
| 599 | t->root = l0; |
| 600 | tbl(l0)[window] = l1 | DESC_TABLE; |
| 601 | tbl(l1)[0] = l2 | DESC_TABLE; |
| 602 | tbl(l2)[0] = l3 | DESC_TABLE; |
| 603 | for (unsigned i = data_first(&t->l); i < window_pages(&t->l); i++) { |
| 604 | uint64_t leaf = expected_leaf(&t->l, i, t->frames); |
| 605 | if (leaf) |
| 606 | tbl(l3)[i] = leaf; |
| 607 | } |
| 608 | copy_section(t, 0, code_src, t->l.code_len); |
| 609 | copy_section(t, t->l.cp, data_src, t->l.data_len); |
| 610 | |
| 611 | /* ---- Hashed */ |
| 612 | *stage = CKS_HASHED; |
| 613 | uint8_t payload[32]; |
| 614 | hash_loaded(t, payload, t->code_digest); |
| 615 | if (memcmp(payload, ident.payload, 32)) { |
| 616 | why = CKL_MAPPED_DIGEST; |
| 617 | goto fail; |
| 618 | } |
| 619 | b->mapped_bytes_matched = 1; |
| 620 | |
| 621 | /* ---- Sealed */ |
| 622 | *stage = CKS_SEALED; |
| 623 | clean_range(t->batch, t->l.cp * PAGE, 1); |
| 624 | if ((why = (unsigned)shadow_alias(t))) |
| 625 | goto fail; |
| 626 | for (unsigned i = 0; i < t->l.cp; i++) |
| 627 | tbl(l3)[1 + i] = USER_CODE(t->frames[i]); |
| 628 | clean_range(t->batch, total * PAGE, 0); |
| 629 | if (t->used) |
| 630 | clean_range(t->shadow, t->used * PAGE, 0); |
| 631 | if (!audit_wx(t)) { |
| 632 | why = CKL_WX_AUDIT; |
| 633 | goto fail; |
| 634 | } |
| 635 | b->wx_sealed = 1; |
| 636 | |
| 637 | /* ---- Caps */ |
| 638 | *stage = CKS_CAPS; |
| 639 | caps_reset(); |
| 640 | t->ngrants = decision.ngrants; |
| 641 | memcpy(t->grants, decision.grants, sizeof t->grants); |
| 642 | for (unsigned i = 0; i < t->ngrants; i++) { |
| 643 | if (caps_insert(i, decision.grants[i].rights, &t->handles[i])) { |
| 644 | why = CKL_CAP_INSTALL; |
| 645 | goto fail; |
| 646 | } |
| 647 | } |
| 648 | uint64_t top = stack_top(&t->l), handles_va = top - 8ull * t->ngrants; |
| 649 | uint64_t top_frame = t->frames[content - 1]; |
| 650 | for (unsigned i = 0; i < t->ngrants; i++) |
| 651 | cka_wr64(mem(top_frame) + (handles_va + 8ull * i - (top - PAGE)), t->handles[i]); |
| 652 | clean_range(top_frame, PAGE, 0); |
| 653 | t->entry_pc = code_base(&t->l) + t->l.entry; |
| 654 | t->sp = handles_va & ~15ull; |
| 655 | t->args[0] = handles_va; |
| 656 | t->args[1] = t->ngrants; |
| 657 | t->args[2] = data_base(&t->l); |
| 658 | t->args[3] = t->l.data_mlen; |
| 659 | t->budget_cpu = parsed.env.cpu; |
| 660 | t->budget_elapsed = parsed.env.elapsed; |
| 661 | t->budget_syscalls = parsed.env.syscalls; |
| 662 | return 0; |
| 663 | fail: |
| 664 | caps_reset(); |
| 665 | release(t); |
| 666 | return why; |
| 667 | } |
| 668 | |
| 669 | /* ============================ EL0 runtime =========================== */ |
| 670 | |
| 671 | static struct { |
| 672 | int active, done; |
| 673 | uint64_t start, limit; |
| 674 | uint32_t budget_syscalls; |
| 675 | struct outcome out; |
| 676 | struct { uint64_t ops, bytes; } usage[TASK_CAPS]; |
| 677 | uint64_t object[4]; |
| 678 | } rt; |
| 679 | |
| 680 | static void terminate(struct ck_frame *f, unsigned status, uint64_t value) |
| 681 | { |
| 682 | if (!rt.done) { |
| 683 | rt.done = 1; |
| 684 | rt.out.status = status; |
| 685 | rt.out.value = value; |
| 686 | } |
| 687 | f->elr = (uint64_t)(uintptr_t)ck_el0_resume; |
| 688 | f->spsr = SPSR_EL1H_MASKED; |
| 689 | } |
| 690 | |
| 691 | static int expired(uint64_t now) { return now - rt.start >= rt.limit; } |
| 692 | |
| 693 | enum { D_AUTHORITY = 1, D_INVALID, D_BOUNDS, D_BUDGET }; |
| 694 | static int object_access(uint64_t raw, uint32_t right, uint64_t off, unsigned *word) |
| 695 | { |
| 696 | uint32_t index; |
| 697 | int lk = caps_lookup(raw, right, &index); |
| 698 | if (lk == LK_MISSING_RIGHTS) |
| 699 | return D_AUTHORITY; |
| 700 | if (lk != LK_OK) |
| 701 | return D_INVALID; |
| 702 | if (index >= task.ngrants) |
| 703 | return D_AUTHORITY; |
| 704 | const struct cka_cap *g = &task.grants[index]; |
| 705 | if (g->kind != CKA_KIND_OBJECT || g->id != SEED_OBJECT_ID || (g->rights & right) != right) |
| 706 | return D_AUTHORITY; |
| 707 | uint64_t end = off + 8, gend = g->off + g->len; |
| 708 | if (end < off || gend < g->off) |
| 709 | return D_BOUNDS; |
| 710 | if ((off & 7) || off < g->off || end > gend || end > SEED_OBJECT_BYTES) |
| 711 | return D_BOUNDS; |
| 712 | uint64_t bytes = rt.usage[index].bytes + 8; |
| 713 | if (bytes < rt.usage[index].bytes || rt.usage[index].ops >= g->max_ops || bytes > g->max_bytes) |
| 714 | return D_BUDGET; |
| 715 | rt.usage[index].ops++; |
| 716 | rt.usage[index].bytes = bytes; |
| 717 | *word = (unsigned)(off / 8); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static uint64_t deny(int why, int write) |
| 722 | { |
| 723 | if (rt.out.denials != 0xffffffffu) |
| 724 | rt.out.denials++; |
| 725 | if (write && rt.out.write_denials != 0xffffffffu) |
| 726 | rt.out.write_denials++; |
| 727 | if (why == D_INVALID && rt.out.invalid_handle != 0xffffffffu) |
| 728 | rt.out.invalid_handle++; |
| 729 | return DENIED; |
| 730 | } |
| 731 | |
| 732 | static int loader_sync(struct ck_frame *f, uint64_t esr) |
| 733 | { |
| 734 | if (!rt.active || (f->spsr & 0xf) != 0) |
| 735 | return -1; |
| 736 | uint64_t ec = (esr >> 26) & 0x3f; |
| 737 | if (ec != 0x15) { |
| 738 | uint64_t far; |
| 739 | __asm__ volatile("mrs %0, far_el1" : "=r"(far)); |
| 740 | if (!rt.done) { |
| 741 | rt.out.esr = esr; |
| 742 | rt.out.far = far; |
| 743 | rt.out.elr = f->elr; |
| 744 | } |
| 745 | terminate(f, ST_FAULT, 0); |
| 746 | return 0; |
| 747 | } |
| 748 | if (rt.out.syscalls != 0xffffffffu) |
| 749 | rt.out.syscalls++; |
| 750 | if (rt.out.syscalls > rt.budget_syscalls) { |
| 751 | terminate(f, ST_OVERRUN, 0); |
| 752 | return 0; |
| 753 | } |
| 754 | if (expired(ck_rd(cntpct_el0))) { |
| 755 | terminate(f, ST_TIMEOUT, 0); |
| 756 | return 0; |
| 757 | } |
| 758 | uint64_t number = f->x[8]; |
| 759 | if ((esr & 0xffff) != 0) { |
| 760 | terminate(f, ST_BAD_SYSCALL, number); |
| 761 | return 0; |
| 762 | } |
| 763 | unsigned word; |
| 764 | int why; |
| 765 | switch (number) { |
| 766 | case SYS_EXIT: |
| 767 | terminate(f, ST_EXITED, f->x[0]); |
| 768 | return 0; |
| 769 | case SYS_OBJECT_READ: |
| 770 | if ((why = object_access(f->x[0], RIGHT_READ, f->x[1], &word)) == 0) { |
| 771 | if (rt.out.reads_ok != 0xffffffffu) |
| 772 | rt.out.reads_ok++; |
| 773 | f->x[0] = rt.object[word]; |
| 774 | } else { |
| 775 | f->x[0] = deny(why, 0); |
| 776 | } |
| 777 | return 0; |
| 778 | case SYS_OBJECT_WRITE: |
| 779 | if ((why = object_access(f->x[0], RIGHT_WRITE, f->x[1], &word)) == 0) { |
| 780 | rt.object[word] = f->x[2]; |
| 781 | f->x[0] = 0; |
| 782 | } else { |
| 783 | f->x[0] = deny(why, 1); |
| 784 | } |
| 785 | return 0; |
| 786 | default: |
| 787 | terminate(f, ST_BAD_SYSCALL, number); |
| 788 | return 0; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static struct ck_frame *loader_tick(struct ck_frame *f) |
| 793 | { |
| 794 | if ((f->spsr & 0xf) == 0 && rt.active && expired(ck_rd(cntpct_el0))) |
| 795 | terminate(f, ST_TIMEOUT, 0); |
| 796 | return f; |
| 797 | } |
| 798 | |
| 799 | static void run_task(struct task *t, struct outcome *o) |
| 800 | { |
| 801 | memset(&rt, 0, sizeof rt); |
| 802 | rt.object[0] = 0xa1e05eed00000001ull; |
| 803 | rt.object[1] = 0xa1e05eed00000002ull; |
| 804 | rt.object[2] = 0xa1e05eed00000003ull; |
| 805 | rt.object[3] = 0xa1e05eed00000004ull; |
| 806 | rt.limit = t->budget_cpu < t->budget_elapsed ? t->budget_cpu : t->budget_elapsed; |
| 807 | rt.budget_syscalls = t->budget_syscalls; |
| 808 | uint64_t regs[31] = {0}; |
| 809 | for (unsigned i = 0; i < 4; i++) |
| 810 | regs[i] = t->args[i]; |
| 811 | uint64_t hz = ck_rd(cntfrq_el0); |
| 812 | uint64_t interval = hz >= 100 ? hz / 100 : 625000; |
| 813 | uint64_t kttbr = ck_rd(ttbr0_el1), cpacr = ck_rd(cpacr_el1), cntkctl = ck_rd(cntkctl_el1); |
| 814 | ck_lower_sync = loader_sync; |
| 815 | ck_tick_switch = loader_tick; |
| 816 | rt.start = ck_rd(cntpct_el0); |
| 817 | rt.active = 1; |
| 818 | ck_wr(cpacr_el1, (cpacr & ~(3ull << 20)) | (1ull << 20)); /* FP/SIMD traps at EL0 */ |
| 819 | /* EL0 must not touch the physical/virtual timers (EL0PTEN, EL0VTEN): the |
| 820 | * budget timer is the kernel's, whatever firmware left in CNTKCTL_EL1. */ |
| 821 | ck_wr(cntkctl_el1, cntkctl & ~((1ull << 9) | (1ull << 8))); |
| 822 | swap_ttbr0(t->root); |
| 823 | ck_wr(cntp_cval_el0, rt.start + interval); |
| 824 | ck_wr(cntp_ctl_el0, 1); |
| 825 | ck_isb(); |
| 826 | ck_el0_enter(t->entry_pc, t->sp, regs); |
| 827 | __asm__ volatile("msr daifset, #2" ::: "memory"); |
| 828 | ck_wr(cntp_ctl_el0, 0); |
| 829 | ck_isb(); |
| 830 | swap_ttbr0(kttbr); |
| 831 | ck_wr(cpacr_el1, cpacr); |
| 832 | ck_wr(cntkctl_el1, cntkctl); |
| 833 | ck_isb(); |
| 834 | rt.active = 0; |
| 835 | ck_lower_sync = 0; |
| 836 | ck_tick_switch = 0; |
| 837 | *o = rt.out; |
| 838 | } |
| 839 | |
| 840 | /* ============================ candidates ============================ */ |
| 841 | |
| 842 | static uint64_t receipt_seq = 1; |
| 843 | static uint8_t verifier_id[32]; |
| 844 | |
| 845 | static void report_empty(struct report *r, uint64_t free_before) |
| 846 | { |
| 847 | memset(r, 0, sizeof *r); |
| 848 | r->free_before = r->free_after = free_before; |
| 849 | } |
| 850 | |
| 851 | static void firmware_rejection(struct report *r, unsigned error) |
| 852 | { |
| 853 | report_empty(r, free_frames()); |
| 854 | r->stage = CKS_RECEIVED; |
| 855 | r->error = error; |
| 856 | cka_policy_digest(&policy, r->b.policy); |
| 857 | } |
| 858 | |
| 859 | /* process_candidate: bytes at in_pa (len bytes, len >= 0). */ |
| 860 | static void process_candidate(const uint8_t *bytes, uint64_t len, struct report *r) |
| 861 | { |
| 862 | report_empty(r, free_frames()); |
| 863 | uint64_t pages = (len + PAGE - 1) / PAGE; |
| 864 | if (len == 0 || pages > MAX_BATCH) { |
| 865 | r->stage = CKS_RECEIVED; |
| 866 | r->error = CKL_STAGING_TOO_LARGE; |
| 867 | r->free_after = free_frames(); |
| 868 | return; |
| 869 | } |
| 870 | uint64_t staged; |
| 871 | if (ck_mm_frames_alloc(pages, &staged)) { |
| 872 | r->stage = CKS_STAGED; |
| 873 | r->error = CKL_NO_FRAMES; |
| 874 | r->free_after = free_frames(); |
| 875 | return; |
| 876 | } |
| 877 | memset(mem(staged), 0, pages * PAGE); |
| 878 | memcpy(mem(staged), bytes, len); |
| 879 | unsigned stage = 0; |
| 880 | int tier_test = 0; |
| 881 | unsigned why = load_bound(staged, len, &r->b, &stage, &tier_test); |
| 882 | memset(mem(staged), 0, pages * PAGE); |
| 883 | int staging_released = ck_mm_frames_free(staged, pages) == 0; |
| 884 | if (why) { |
| 885 | r->stage = stage; |
| 886 | r->error = staging_released ? why : CKL_RECLAIM; |
| 887 | r->free_after = free_frames(); |
| 888 | return; |
| 889 | } |
| 890 | struct task *t = &task; |
| 891 | r->have_id = 1; |
| 892 | memcpy(r->id, ident.id, 32); |
| 893 | r->tier_test = tier_test; |
| 894 | r->frames_reserved = t->nframes + t->nshadow; |
| 895 | r->caps_installed = t->ngrants; |
| 896 | memcpy(r->granted, t->grants, sizeof r->granted); |
| 897 | r->code_base = code_base(&t->l); |
| 898 | r->code_end = code_end(&t->l); |
| 899 | r->data_base = data_base(&t->l); |
| 900 | r->data_end = data_end(&t->l); |
| 901 | r->stack_base = stack_base(&t->l); |
| 902 | r->stack_end = stack_top(&t->l); |
| 903 | r->wx_enforced = r->b.wx_sealed; |
| 904 | run_task(t, &r->out); |
| 905 | /* destroy */ |
| 906 | uint8_t now[32]; |
| 907 | code_digest(t, now); |
| 908 | int executed = memcmp(now, t->code_digest, 32) == 0; |
| 909 | for (unsigned i = 0; i < t->ngrants; i++) |
| 910 | caps_remove(t->handles[i]); |
| 911 | r->caps_live_after = caps_live(); |
| 912 | int released = release(t); |
| 913 | r->admitted = 1; |
| 914 | r->byte_chain = r->b.mapped_bytes_matched && executed; |
| 915 | r->free_after = free_frames(); |
| 916 | if (!executed) |
| 917 | r->error = CKL_EXECUTED_DIGEST; |
| 918 | else if (!released || !staging_released) |
| 919 | r->error = CKL_RECLAIM; |
| 920 | } |
| 921 | |
| 922 | static void hex(char *out, const uint8_t *b, size_t n) |
| 923 | { |
| 924 | static const char d[] = "0123456789abcdef"; |
| 925 | for (size_t i = 0; i < n; i++) { |
| 926 | out[2 * i] = d[b[i] >> 4]; |
| 927 | out[2 * i + 1] = d[b[i] & 15]; |
| 928 | } |
| 929 | out[2 * n] = 0; |
| 930 | } |
| 931 | |
| 932 | static const char *classify_fault(const struct report *r) |
| 933 | { |
| 934 | uint64_t esr = r->out.esr, far = r->out.far, ec = (esr >> 26) & 0x3f; |
| 935 | uint64_t status = (esr >> 2) & 0xf; |
| 936 | #define WITHIN(b, e) (far >= (b) && far < (e)) |
| 937 | int in_ds = WITHIN(r->data_base, r->data_end) || WITHIN(r->stack_base, r->stack_end); |
| 938 | if (ec == 0x24 && (esr & (1u << 6)) && status == 3 && WITHIN(r->code_base, r->code_end)) |
| 939 | return "code-write"; |
| 940 | if (ec == 0x20 && status == 3 && in_ds) |
| 941 | return "exec-nx"; |
| 942 | if (ec == 0x20 && status == 1) |
| 943 | return "exec-unmapped"; |
| 944 | #ifdef CK_ARTIFACT_CARD |
| 945 | /* Arm ARM (DDI 0487) ESR_ELx.EC: 0x24 data abort from a lower exception level (bit 6 = WnR, write not read); |
| 946 | * 0x18 trapped MSR/MRS/system instruction; 0x00 unknown reason (an UNDEFINED instruction). Only the card |
| 947 | * qualification image names these: every other image keeps the two-name set above and "other". */ |
| 948 | if (ec == 0x24 && !WITHIN(r->code_base, r->code_end) && !in_ds) |
| 949 | return (esr & (1u << 6)) ? "outside-write" : "outside-read"; |
| 950 | if (ec == 0x18 || ec == 0x00) |
| 951 | return "privileged-insn"; |
| 952 | #endif |
| 953 | #undef WITHIN |
| 954 | return "other"; |
| 955 | } |
| 956 | |
| 957 | static void print_candidate(const char *name, const struct report *r) |
| 958 | { |
| 959 | const char *yn[2] = {"no", "yes"}; |
| 960 | if (!r->admitted) { |
| 961 | ck_printf("artifact: %s rejected stage=%s reason=%s reclaimed=%s\n", name, |
| 962 | r->stage ? cka_stage_name(r->stage) : "unknown", |
| 963 | r->error ? cka_error_name(r->error) : "unknown", yn[reclaimed(r)]); |
| 964 | return; |
| 965 | } |
| 966 | char id[17]; |
| 967 | hex(id, r->id, 8); |
| 968 | ck_printf("artifact: %s admitted id=%s tier=%s exec=", name, r->have_id ? id : "", |
| 969 | r->tier_test ? "seed0b-test" : "production"); |
| 970 | switch (r->out.status) { |
| 971 | case ST_EXITED: |
| 972 | ck_printf("exited:0x%llx", (unsigned long long)r->out.value); |
| 973 | break; |
| 974 | case ST_TIMEOUT: |
| 975 | ck_puts("timeout"); |
| 976 | break; |
| 977 | case ST_FAULT: |
| 978 | ck_printf("fault:%s", classify_fault(r)); |
| 979 | break; |
| 980 | case ST_BAD_SYSCALL: |
| 981 | ck_printf("bad-syscall:%llu", (unsigned long long)r->out.value); |
| 982 | break; |
| 983 | case ST_OVERRUN: |
| 984 | ck_puts("resource-overrun"); |
| 985 | break; |
| 986 | default: |
| 987 | ck_puts("not-run"); |
| 988 | break; |
| 989 | } |
| 990 | ck_printf(" bytes=%s wx=%s caps=%u revoked=%s reclaimed=%s frames=%llu syscalls=%u reads=%u " |
| 991 | "denials=%u\n", |
| 992 | r->byte_chain ? "identified=verified=admitted=mapped=executed" : "mismatch", |
| 993 | r->wx_enforced ? "enforced" : "violated", r->caps_installed, |
| 994 | yn[r->caps_live_after == 0], yn[reclaimed(r)], |
| 995 | (unsigned long long)r->frames_reserved, r->out.syscalls, r->out.reads_ok, |
| 996 | r->out.denials); |
| 997 | } |
| 998 | |
| 999 | static void print_grants(const char *name, const struct report *r) |
| 1000 | { |
| 1001 | if (!r->admitted) |
| 1002 | return; |
| 1003 | for (unsigned i = 0; i < r->caps_installed; i++) { |
| 1004 | const struct cka_cap *g = &r->granted[i]; |
| 1005 | const char *kind = g->kind == CKA_KIND_OBJECT ? "object" |
| 1006 | : g->kind == CKA_KIND_CHANNEL ? "channel" |
| 1007 | : "other"; |
| 1008 | ck_printf("grant: %s[%u] kind=%s id=%u rights=0x%x bounds=%llu+%llu max_ops=%u " |
| 1009 | "max_bytes=%llu\n", |
| 1010 | name, i, kind, g->id, g->rights, (unsigned long long)g->off, |
| 1011 | (unsigned long long)g->len, g->max_ops, (unsigned long long)g->max_bytes); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | static void print_receipt(const char *name, const struct report *r) |
| 1016 | { |
| 1017 | uint64_t seq = receipt_seq++; |
| 1018 | struct cka_receipt rc; |
| 1019 | memset(&rc, 0, sizeof rc); |
| 1020 | int adm = r->admitted; |
| 1021 | rc.decision = adm ? CKR_ADMITTED : CKR_REJECTED; |
| 1022 | rc.tier = CKA_IMAGE_TIER; |
| 1023 | rc.seq = seq; |
| 1024 | memcpy(rc.verifier, verifier_id, 32); |
| 1025 | cka_receipt_nonce(verifier_id, seq, rc.nonce); |
| 1026 | rc.status = CKR_NOT_RUN; |
| 1027 | if (adm) { |
| 1028 | static const uint32_t map[] = {CKR_NOT_RUN, CKR_EXITED, CKR_TIMEOUT, CKR_FAULT, |
| 1029 | CKR_BAD_SYSCALL, CKR_RESOURCE_OVERRUN}; |
| 1030 | rc.status = map[r->out.status]; |
| 1031 | if (r->out.status == ST_EXITED) |
| 1032 | rc.exit = (int32_t)(uint32_t)r->out.value; |
| 1033 | } |
| 1034 | uint32_t fl = 0; |
| 1035 | if (reclaimed(r)) |
| 1036 | fl |= CKR_F_RECLAIMED; |
| 1037 | if (adm) { |
| 1038 | if (r->out.reads_ok) |
| 1039 | fl |= CKR_F_READ_OK; |
| 1040 | if (r->out.write_denials) |
| 1041 | fl |= CKR_F_WRITE_DENIED; |
| 1042 | if (r->out.invalid_handle) |
| 1043 | fl |= CKR_F_FORGED_DENIED; |
| 1044 | if (rc.status == CKR_EXITED && rc.exit == 0) |
| 1045 | fl |= CKR_F_CANARY; |
| 1046 | if (r->b.mapped_bytes_matched) |
| 1047 | fl |= CKR_F_MAPPED; |
| 1048 | if (r->byte_chain) |
| 1049 | fl |= CKR_F_EXECUTED; |
| 1050 | if (r->b.wx_sealed) |
| 1051 | fl |= CKR_F_WX; |
| 1052 | } |
| 1053 | rc.result_flags = fl; |
| 1054 | rc.stage = adm ? 0 : (uint16_t)r->stage; |
| 1055 | rc.reason = adm ? 0 : (uint16_t)r->error; |
| 1056 | rc.syscalls = adm ? r->out.syscalls : 0; |
| 1057 | rc.reads = adm ? r->out.reads_ok : 0; |
| 1058 | rc.denials = adm ? r->out.denials : 0; |
| 1059 | rc.frames = r->frames_reserved > 0xffffffffull ? 0xffffffffu : (uint32_t)r->frames_reserved; |
| 1060 | memcpy(rc.id, r->b.id, 32); |
| 1061 | memcpy(rc.payload, r->b.payload, 32); |
| 1062 | memcpy(rc.signer, r->b.signer, 32); |
| 1063 | memcpy(rc.policy, r->b.policy, 32); |
| 1064 | memcpy(rc.requested, r->b.requested, 32); |
| 1065 | memcpy(rc.granted, r->b.granted, 32); |
| 1066 | memcpy(rc.resources, r->b.resources, 32); |
| 1067 | if (cka_receipt_validate(&rc)) { |
| 1068 | ck_printf("receipt: %s seq=%llu invalid\n", name, (unsigned long long)seq); |
| 1069 | return; |
| 1070 | } |
| 1071 | static uint8_t rec[CKA_RECEIPT_SIZE]; |
| 1072 | static char line[2 * CKA_RECEIPT_SIZE + 1]; |
| 1073 | uint8_t dg[32]; |
| 1074 | char dh[65]; |
| 1075 | cka_receipt_encode(&rc, rec); |
| 1076 | cka_receipt_digest(rec, dg); |
| 1077 | hex(dh, dg, 32); |
| 1078 | hex(line, rec, CKA_RECEIPT_SIZE); |
| 1079 | ck_printf("receipt: %s seq=%llu digest=%s record=", name, (unsigned long long)seq, dh); |
| 1080 | ck_puts(line); |
| 1081 | ck_puts("\n"); |
| 1082 | } |
| 1083 | |
| 1084 | /* ============================ candidate sources ===================== */ |
| 1085 | |
| 1086 | /* Default source: the boot disk. The Store stage (svc/artifact_store.c) read |
| 1087 | * the candidates from the sealed Store on NVMe before revoking the disk's DMA; |
| 1088 | * here they are untrusted bytes and take the same verification path as any |
| 1089 | * candidate. A test-only QEMU fw_cfg side channel exists only in images built |
| 1090 | * with CK_TEST_FWCFG_ARTIFACTS=1 and is compiled out of every default image. */ |
| 1091 | |
| 1092 | /* Qualification tier label printed in every receipt header. A Pi 5 image is never |
| 1093 | * the QEMU tier and never production: the label names the board. */ |
| 1094 | #if defined(CK_PI5_UNLOCK) |
| 1095 | #define CK_RECEIPT_TIER "PI5-UNLOCK-QUALIFICATION" |
| 1096 | #elif defined(CK_PI5) |
| 1097 | #define CK_RECEIPT_TIER "SEED-0B-PI5" |
| 1098 | #else |
| 1099 | #define CK_RECEIPT_TIER "SEED-0B-QEMU" |
| 1100 | #endif |
| 1101 | |
| 1102 | static struct report rep; |
| 1103 | static unsigned run_admitted, run_rejected, run_seen; |
| 1104 | |
| 1105 | static void source_preamble(uint32_t count) |
| 1106 | { |
| 1107 | char vh[65], ph[65]; |
| 1108 | uint8_t pd[32]; |
| 1109 | cka_policy_digest(&policy, pd); |
| 1110 | hex(vh, verifier_id, 32); |
| 1111 | hex(ph, pd, 32); |
| 1112 | ck_printf("artifact_candidates: %u\n", count); |
| 1113 | ck_printf("artifact_receipt_tier: %s\nartifact_verifier_identity: %s\n" |
| 1114 | "artifact_policy_digest: %s\n", |
| 1115 | CK_RECEIPT_TIER, vh, ph); |
| 1116 | ck_printf("artifact_frames_free_before: %llu\n", (unsigned long long)free_frames()); |
| 1117 | } |
| 1118 | |
| 1119 | /* Candidate names come from untrusted input: 1..MAX_NAME printable bytes. */ |
| 1120 | static int candidate_name_ok(const char *name, unsigned *nlen) |
| 1121 | { |
| 1122 | unsigned n = 0; |
| 1123 | if (!name) |
| 1124 | return 0; |
| 1125 | while (n <= MAX_NAME && name[n]) { |
| 1126 | if (name[n] <= ' ' || name[n] > '~') |
| 1127 | return 0; |
| 1128 | n++; |
| 1129 | } |
| 1130 | *nlen = n; |
| 1131 | return n >= 1 && n <= MAX_NAME; |
| 1132 | } |
| 1133 | |
| 1134 | static void finish_candidate(const char *name) |
| 1135 | { |
| 1136 | run_seen++; |
| 1137 | if (rep.admitted) |
| 1138 | run_admitted++; |
| 1139 | else |
| 1140 | run_rejected++; |
| 1141 | print_candidate(name, &rep); |
| 1142 | #ifdef CK_ARTIFACT_CARD |
| 1143 | if (rep.admitted && rep.out.status == ST_FAULT) { |
| 1144 | /* FAR_EL1 is architecturally defined only for instruction (0x20) and data (0x24) aborts from EL0. */ |
| 1145 | uint64_t fec = (rep.out.esr >> 26) & 0x3f; |
| 1146 | ck_printf("artifact_fault: %s class=%s esr=0x%llx ec=0x%llx ", name, classify_fault(&rep), |
| 1147 | (unsigned long long)rep.out.esr, (unsigned long long)fec); |
| 1148 | if (fec == 0x20 || fec == 0x24) |
| 1149 | ck_printf("far=0x%llx ", (unsigned long long)rep.out.far); |
| 1150 | else |
| 1151 | ck_puts("far=n/a "); |
| 1152 | ck_printf("elr=0x%llx contained=yes\n", (unsigned long long)rep.out.elr); |
| 1153 | } |
| 1154 | #endif |
| 1155 | print_grants(name, &rep); |
| 1156 | print_receipt(name, &rep); |
| 1157 | } |
| 1158 | |
| 1159 | #ifndef CK_TEST_FWCFG_ARTIFACTS |
| 1160 | static uint32_t disk_source(void) |
| 1161 | { |
| 1162 | struct ck_disk_artifacts da; |
| 1163 | const char *why = 0; |
| 1164 | memset(&da, 0, sizeof da); |
| 1165 | if (!ck_stage_disk_artifacts) |
| 1166 | why = "no Store stage in this image"; |
| 1167 | else if (ck_stage_disk_artifacts(&da)) |
| 1168 | why = "Store stage did not run"; |
| 1169 | else if (!da.available) |
| 1170 | why = da.why ? da.why : "no artifacts"; |
| 1171 | uint32_t count = why ? 0 : da.count; |
| 1172 | if (count > MAX_CANDIDATES) |
| 1173 | count = MAX_CANDIDATES; |
| 1174 | if (why) |
| 1175 | ck_printf("artifact_source: none (%s)\n", why); |
| 1176 | #ifdef CK_ARTIFACT_CARD |
| 1177 | else |
| 1178 | ck_printf("artifact_source: card_file candidates=%u " |
| 1179 | "(read from the card by the UEFI stub; bytes untrusted until verified)\n", count); |
| 1180 | #else |
| 1181 | else |
| 1182 | ck_printf("artifact_source: nvme_store generation=%llu candidates=%u " |
| 1183 | "(read by the store stage; bytes untrusted until verified)\n", |
| 1184 | (unsigned long long)da.generation, count); |
| 1185 | #endif |
| 1186 | source_preamble(count); |
| 1187 | for (uint32_t c = 0; c < count; c++) { |
| 1188 | const struct ck_disk_artifact *a = &da.a[c]; |
| 1189 | char name[MAX_NAME + 1]; |
| 1190 | unsigned nlen = 0; |
| 1191 | if (!candidate_name_ok(a->name, &nlen)) |
| 1192 | break; |
| 1193 | memcpy(name, a->name, nlen); |
| 1194 | name[nlen] = 0; |
| 1195 | if (a->state == CK_DISK_ART_OK && a->bytes && ck_osc_is_unit(a->bytes, a->len)) { |
| 1196 | /* OSCUNIT container (OSC_UNIT_ARTIFACT v1): admission verdict only, not a Binary Artifact. */ |
| 1197 | ck_osc_candidate(name, a->bytes, a->len); |
| 1198 | run_seen++; |
| 1199 | continue; |
| 1200 | } |
| 1201 | if (a->state == CK_DISK_ART_OK && a->bytes && ck_osc_is_receipt(a->bytes, a->len)) { |
| 1202 | /* Omega realization receipt (aienos#383): kept for the launch that presents it, never an artifact. */ |
| 1203 | ck_osc_receipt_candidate(name, a->bytes, a->len); |
| 1204 | run_seen++; |
| 1205 | continue; |
| 1206 | } |
| 1207 | if (a->state == CK_DISK_ART_OK && a->bytes && a->len >= 1 && a->len <= MAX_BATCH * PAGE) |
| 1208 | process_candidate(a->bytes, a->len, &rep); |
| 1209 | else if (a->state == CK_DISK_ART_TOO_LARGE || a->len > MAX_BATCH * PAGE) { |
| 1210 | /* The Store never reads a too-large entry, so an OSCUNIT among them cannot be recognised: |
| 1211 | * say so on the osc_unit: channel (RESOURCE_UNAVAILABLE) and keep the old refusal. */ |
| 1212 | if (a->state == CK_DISK_ART_TOO_LARGE) |
| 1213 | ck_osc_oversize(name, a->len); |
| 1214 | firmware_rejection(&rep, CKL_STAGING_TOO_LARGE); |
| 1215 | } else |
| 1216 | firmware_rejection(&rep, CKL_FIRMWARE_READ); /* missing on disk */ |
| 1217 | finish_candidate(name); |
| 1218 | } |
| 1219 | ck_osc_summary(); |
| 1220 | return count; |
| 1221 | } |
| 1222 | #else |
| 1223 | /* ======== TEST-ONLY fw_cfg source (CK_TEST_FWCFG_ARTIFACTS=1) ======== */ |
| 1224 | #define FWCFG_TEST_BANNER \ |
| 1225 | "artifact_source_mode: TEST-ONLY QEMU fw_cfg side channel " \ |
| 1226 | "(CK_TEST_FWCFG_ARTIFACTS=1); not the boot disk; never counts toward a PASS\n" |
| 1227 | |
| 1228 | static volatile uint8_t *fwcfg; |
| 1229 | |
| 1230 | static void fw_select(uint16_t sel) |
| 1231 | { |
| 1232 | *(volatile uint16_t *)(fwcfg + 8) = (uint16_t)((sel >> 8) | (sel << 8)); /* big endian */ |
| 1233 | __asm__ volatile("dsb sy" ::: "memory"); |
| 1234 | } |
| 1235 | static void fw_read(uint8_t *out, uint64_t n) |
| 1236 | { |
| 1237 | for (uint64_t i = 0; i < n; i++) |
| 1238 | out[i] = fwcfg[0]; |
| 1239 | } |
| 1240 | static void fw_skip(uint64_t n) |
| 1241 | { |
| 1242 | for (uint64_t i = 0; i < n; i++) |
| 1243 | (void)fwcfg[0]; |
| 1244 | } |
| 1245 | static uint32_t be32(const uint8_t *b) |
| 1246 | { |
| 1247 | return (uint32_t)b[0] << 24 | (uint32_t)b[1] << 16 | (uint32_t)b[2] << 8 | b[3]; |
| 1248 | } |
| 1249 | |
| 1250 | /* The fw_cfg MMIO window from the DSDT: device _HID "QEMU0002", then its |
| 1251 | * _CRS Memory32Fixed descriptor (0x86, length 9). 0 if absent. */ |
| 1252 | static uint64_t fwcfg_from_dsdt(const char **why) |
| 1253 | { |
| 1254 | const uint8_t *fadt = ck_acpi_find("FACP"); |
| 1255 | if (!fadt) { |
| 1256 | *why = "no FADT"; |
| 1257 | return 0; |
| 1258 | } |
| 1259 | uint32_t flen = cka_rd32(fadt + 4); |
| 1260 | uint64_t dsdt = 0; |
| 1261 | if (flen >= 148) |
| 1262 | dsdt = cka_rd64(fadt + 140); |
| 1263 | if (!dsdt && flen >= 44) |
| 1264 | dsdt = cka_rd32(fadt + 40); |
| 1265 | if (!dsdt || !ck_mm_mapped(dsdt, 36)) { |
| 1266 | *why = "DSDT not mapped"; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | const uint8_t *d = (const uint8_t *)(uintptr_t)dsdt; |
| 1270 | uint32_t len = cka_rd32(d + 4); |
| 1271 | if (len < 36 || memcmp(d, "DSDT", 4) || !ck_mm_mapped(dsdt, len)) { |
| 1272 | *why = "DSDT not mapped"; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | static const char hid[] = "QEMU0002"; |
| 1276 | for (uint32_t i = 36; i + 9 <= len; i++) { |
| 1277 | if (memcmp(d + i, hid, 8)) |
| 1278 | continue; |
| 1279 | for (uint32_t j = i + 8; j + 12 <= len && j < i + 8 + 96; j++) |
| 1280 | if (d[j] == 0x86 && d[j + 1] == 0x09 && d[j + 2] == 0x00) { |
| 1281 | uint64_t base = cka_rd32(d + j + 4), size = cka_rd32(d + j + 8); |
| 1282 | if (base && size >= 0x10) |
| 1283 | return base; |
| 1284 | } |
| 1285 | } |
| 1286 | *why = "no QEMU0002 device"; |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | /* Finds "opt/aienos/artifacts": selector and size, -1 if absent. */ |
| 1291 | static int fw_find(uint16_t *sel, uint32_t *size) |
| 1292 | { |
| 1293 | uint8_t b[64]; |
| 1294 | fw_select(0x19); |
| 1295 | fw_read(b, 4); |
| 1296 | uint32_t n = be32(b); |
| 1297 | for (uint32_t i = 0; i < n && i < 4096; i++) { |
| 1298 | fw_read(b, 64); |
| 1299 | b[63] = 0; |
| 1300 | if (!memcmp(b + 8, "opt/aienos/artifacts", 21)) { |
| 1301 | *sel = (uint16_t)(b[4] << 8 | b[5]); |
| 1302 | *size = be32(b); |
| 1303 | return 0; |
| 1304 | } |
| 1305 | } |
| 1306 | return -1; |
| 1307 | } |
| 1308 | |
| 1309 | static uint32_t fwcfg_source(void) |
| 1310 | { |
| 1311 | const char *why = 0; |
| 1312 | uint64_t base = fwcfg_from_dsdt(&why); |
| 1313 | uint16_t sel = 0; |
| 1314 | uint32_t size = 0; |
| 1315 | if (base && !ck_mm_mmio_try_map(base, 0x18)) |
| 1316 | why = "fw_cfg window not mappable", base = 0; |
| 1317 | if (base) { |
| 1318 | fwcfg = (volatile uint8_t *)(uintptr_t)base; |
| 1319 | uint8_t sig[4]; |
| 1320 | fw_select(0); |
| 1321 | fw_read(sig, 4); |
| 1322 | if (memcmp(sig, "QEMU", 4)) |
| 1323 | why = "fw_cfg signature", base = 0; |
| 1324 | else if (fw_find(&sel, &size)) |
| 1325 | why = "no opt/aienos/artifacts", base = 0; |
| 1326 | } |
| 1327 | uint8_t hdr[12]; |
| 1328 | uint32_t count = 0; |
| 1329 | if (base) { |
| 1330 | fw_select(sel); |
| 1331 | if (size < 12) |
| 1332 | why = "bundle too short", base = 0; |
| 1333 | else { |
| 1334 | fw_read(hdr, 12); |
| 1335 | if (memcmp(hdr, "AIENBND", 8)) |
| 1336 | why = "bundle magic", base = 0; |
| 1337 | else |
| 1338 | count = cka_rd32(hdr + 8); |
| 1339 | } |
| 1340 | } |
| 1341 | if (!base) |
| 1342 | ck_printf("artifact_source: none (%s)\n", why); |
| 1343 | else |
| 1344 | ck_printf("artifact_source: fw_cfg TEST-ONLY base=0x%llx file=opt/aienos/artifacts bytes=%u\n", |
| 1345 | (unsigned long long)base, size); |
| 1346 | if (count > MAX_CANDIDATES) |
| 1347 | count = MAX_CANDIDATES; |
| 1348 | source_preamble(count); |
| 1349 | |
| 1350 | uint64_t left = size >= 12 ? size - 12 : 0; |
| 1351 | for (uint32_t c = 0; c < count; c++) { |
| 1352 | uint8_t nb[2], lb[4]; |
| 1353 | char name[MAX_NAME + 1]; |
| 1354 | unsigned ok_len = 0; |
| 1355 | if (left < 2) |
| 1356 | break; |
| 1357 | fw_read(nb, 2); |
| 1358 | left -= 2; |
| 1359 | unsigned nlen = cka_rd16(nb); |
| 1360 | if (nlen == 0 || nlen > MAX_NAME || left < nlen + 4ull) |
| 1361 | break; |
| 1362 | fw_read((uint8_t *)name, nlen); |
| 1363 | name[nlen] = 0; |
| 1364 | fw_read(lb, 4); |
| 1365 | left -= nlen + 4ull; |
| 1366 | uint64_t len = cka_rd32(lb); |
| 1367 | if (len > left) |
| 1368 | break; |
| 1369 | left -= len; |
| 1370 | if (!candidate_name_ok(name, &ok_len) || ok_len != nlen) |
| 1371 | break; |
| 1372 | uint64_t pages = (len + PAGE - 1) / PAGE, in = 0; |
| 1373 | if (len > MAX_BATCH * PAGE) { |
| 1374 | fw_skip(len); |
| 1375 | firmware_rejection(&rep, CKL_STAGING_TOO_LARGE); |
| 1376 | } else if (len && ck_mm_frames_alloc(pages, &in)) { |
| 1377 | fw_skip(len); |
| 1378 | firmware_rejection(&rep, CKL_FIRMWARE_READ); |
| 1379 | } else { |
| 1380 | if (len) |
| 1381 | fw_read(mem(in), len); |
| 1382 | process_candidate(len ? mem(in) : (const uint8_t *)"", len, &rep); |
| 1383 | if (len) { |
| 1384 | memset(mem(in), 0, pages * PAGE); |
| 1385 | ck_mm_frames_free(in, pages); |
| 1386 | } |
| 1387 | } |
| 1388 | finish_candidate(name); |
| 1389 | } |
| 1390 | return count; |
| 1391 | } |
| 1392 | #endif |
| 1393 | |
| 1394 | void ck_artifact_run(void) |
| 1395 | { |
| 1396 | static int ran; |
| 1397 | if (ran) |
| 1398 | return; |
| 1399 | ran = 1; |
| 1400 | ck_set_stage("artifacts"); |
| 1401 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 1402 | ck_puts("artifact_trust: seed0b-test qualification build \xe2\x80\x94 TEST ONLY\n"); |
| 1403 | #endif |
| 1404 | #ifdef CK_TEST_FWCFG_ARTIFACTS |
| 1405 | ck_puts(FWCFG_TEST_BANNER); |
| 1406 | #endif |
| 1407 | if (NANCHORS) |
| 1408 | cka_boot_policy(&policy, anchors, NANCHORS); |
| 1409 | else |
| 1410 | cka_boot_policy(&policy, 0, 0); |
| 1411 | cka_verifier_identity(AIENOS_COMMIT, FEATURE_TEST_ANCHOR, verifier_id); |
| 1412 | |
| 1413 | #ifdef CK_TEST_FWCFG_ARTIFACTS |
| 1414 | uint32_t count = fwcfg_source(); |
| 1415 | #else |
| 1416 | uint32_t count = disk_source(); |
| 1417 | #endif |
| 1418 | if (run_seen != count) |
| 1419 | ck_printf("artifact_bundle: malformed after %u of %u candidates\n", run_seen, count); |
| 1420 | ck_printf("artifact_frames_free_after: %llu\n", (unsigned long long)free_frames()); |
| 1421 | ck_artifact_summary.candidates = count; |
| 1422 | ck_artifact_summary.admitted = run_admitted; |
| 1423 | ck_artifact_summary.rejected = run_rejected; |
| 1424 | #ifndef CK_TEST_FWCFG_ARTIFACTS |
| 1425 | if (ck_stage_disk_artifacts_free) |
| 1426 | ck_stage_disk_artifacts_free(); |
| 1427 | #endif |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | void ck_artifact_final(void) |
| 1432 | { |
| 1433 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 1434 | ck_puts("artifact_trust: seed0b-test qualification build \xe2\x80\x94 TEST ONLY\n"); |
| 1435 | #endif |
| 1436 | #ifdef CK_TEST_FWCFG_ARTIFACTS |
| 1437 | ck_puts(FWCFG_TEST_BANNER); |
| 1438 | #endif |
| 1439 | ck_printf("artifacts: candidates=%u admitted=%u rejected=%u\n", ck_artifact_summary.candidates, |
| 1440 | ck_artifact_summary.admitted, ck_artifact_summary.rejected); |
| 1441 | } |