Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/boot/slot_select.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / boot / slot_select.c` 492 lines ยท 18210 bytes Raw Blame History 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 /* slot_select.c -- AIENOS native image slots: structural checks, slot 18 * verification and the loader's / kernel's decision rules (slot_format.h). 19 * 20 * Freestanding C: no libc, no allocation, no I/O. The only outside symbols 21 * are native/sig (aienos_sha512, aienos_`ed25519`_verify) and 22 * native/argus/sha256.c (sha256_hash). The same object is meant for the UEFI 23 * stub, the kernel and the hosted tool (native/kernel/tools/ck_slot_tool.c), 24 * so the decision a test sees is the decision a loader would take. 25 * 26 * Rules (TRUST-1 Q19, ADR 0006), in the order ck_slot_select applies them: 27 * usable(s) = ev[s].valid && ev[s].generation >= floor 28 * 1. no valid control record: recovery required. Disk loss must not 29 * silently reset a previously established floor to zero. 30 * 2. a candidate is set, has tries left and is usable: boot it and hand 31 * back the control record with tries - 1 for the loader to COMMIT 32 * BEFORE jumping (that is what bounds attempts when the candidate hangs). 33 * 3. a candidate is set but has no tries left, or is not usable: boot the 34 * active slot if usable (the candidate is abandoned by the kernel later; 35 * the loader writes nothing here, so a wedged kernel cannot loop). 36 * 4. active usable: boot it. 37 * 5. active not usable, the other slot usable: boot the other slot 38 * ("last-good never erased": media damage to one slot does not strand 39 * the machine). 40 * 6. otherwise recovery required: the loader must not boot either slot. 41 * A validly signed image below the currently read disk record's floor never 42 * boots. Without an external anchor, replaying an older record can lower 43 * this floor across boots. */ 44 #include "slot_format.h" 45 #include "../sig/aienos_sig.h" 46 #include "../argus/sha256.h" 47 48 static int eq_bytes(const uint8_t *a, const uint8_t *b, size_t n) 49 { 50 uint8_t d = 0; 51 for (size_t i = 0; i < n; i++) 52 d |= (uint8_t)(a[i] ^ b[i]); 53 return d == 0; 54 } 55 56 static int all_zero(const uint8_t *p, size_t n) 57 { 58 uint8_t d = 0; 59 for (size_t i = 0; i < n; i++) 60 d |= p[i]; 61 return d == 0; 62 } 63 64 static void copy_bytes(uint8_t *dst, const uint8_t *src, size_t n) 65 { 66 for (size_t i = 0; i < n; i++) 67 dst[i] = src[i]; 68 } 69 70 static int is_lower_hex(uint8_t c) 71 { 72 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); 73 } 74 75 int ck_slot_manifest_check(const struct ck_slot_manifest *m, const char **why) 76 { 77 static const uint8_t magic[8] = CK_SLOT_MANIFEST_MAGIC; 78 const char *w = 0; 79 if (!m) 80 w = "null manifest"; 81 else if (!eq_bytes(m->magic, magic, 8)) 82 w = "bad magic"; 83 else if (m->version != CK_SLOT_MANIFEST_VERSION) 84 w = "unsupported manifest version"; 85 else if (m->payload_kind != CK_SLOT_PAYLOAD_BOOTAA64) 86 w = "unsupported payload kind"; 87 else if (m->loader_abi != CK_SLOT_LOADER_ABI) 88 w = "loader ABI mismatch"; 89 else if (m->kernel_abi != CK_SLOT_KERNEL_ABI) 90 w = "kernel ABI mismatch"; 91 else if (m->generation == 0) 92 w = "generation is zero"; 93 else if (m->policy_generation > m->generation) 94 w = "policy_generation above generation"; 95 else if (m->min_rollback_generation > m->generation) 96 w = "min_rollback_generation above generation"; 97 else if (m->image_len == 0) 98 w = "image_len is zero"; 99 else if (!eq_bytes(m->kernel_sha512, m->image_sha512, 64)) 100 w = "kernel_sha512 differs from image_sha512 (payload kind 1)"; 101 else if (m->timestamp == 0) 102 w = "timestamp is zero"; 103 else if (m->flags != 0) 104 w = "flags nonzero"; 105 else if (m->reserved0 != 0 || !all_zero(m->reserved1, sizeof m->reserved1)) 106 w = "reserved bytes nonzero"; 107 else if (!all_zero(m->pad, sizeof m->pad)) 108 w = "pad nonzero"; 109 if (!w) { 110 for (unsigned i = 0; i < CK_SLOT_COMMIT_CHARS; i++) 111 if (!is_lower_hex(m->commit[i])) { 112 w = "commit is not 40 lower-case hex characters"; 113 break; 114 } 115 } 116 if (!w) { 117 int ended = 0; 118 for (unsigned i = 0; i < CK_SLOT_PROVENANCE_CHARS; i++) { 119 uint8_t c = m->provenance[i]; 120 if (c == 0) { 121 ended = 1; 122 } else if (ended || c < 0x20 || c > 0x7e) { 123 w = "provenance is not zero-padded printable ASCII"; 124 break; 125 } 126 } 127 } 128 if (why) 129 *why = w; 130 return w ? -1 : 0; 131 } 132 133 int ck_slot_control_check(const struct ck_slot_control *c, const char **why) 134 { 135 static const uint8_t magic[8] = CK_SLOT_CONTROL_MAGIC; 136 const char *w = 0; 137 if (!c) 138 w = "null control"; 139 else if (!eq_bytes(c->magic, magic, 8)) 140 w = "bad control magic"; 141 else if (c->version != CK_SLOT_CONTROL_VERSION) 142 w = "unsupported control version"; 143 else if (c->active_slot >= CK_SLOT_COUNT) 144 w = "active_slot out of range"; 145 else if (c->candidate_slot != CK_SLOT_NONE && c->candidate_slot >= CK_SLOT_COUNT) 146 w = "candidate_slot out of range"; 147 else if (c->candidate_slot == c->active_slot) 148 w = "candidate_slot equals active_slot"; 149 else if (c->tries_remaining > CK_SLOT_MAX_TRIES) 150 w = "tries_remaining above the bound"; 151 else if (c->candidate_slot == CK_SLOT_NONE && c->tries_remaining != 0) 152 w = "tries_remaining without a candidate"; 153 else if (c->flags & ~(uint8_t)CK_SLOT_CTL_FLAG_CANDIDATE_FAILED) 154 w = "unknown control flags"; 155 else if (c->last_good_generation != 0 && c->last_good_generation < c->generation_floor) 156 w = "last_good_generation below the floor"; 157 else if (!all_zero(c->reserved, sizeof c->reserved)) 158 w = "control reserved bytes nonzero"; 159 if (why) 160 *why = w; 161 return w ? -1 : 0; 162 } 163 164 int ck_slot_verify(const struct ck_slot_manifest *m, const uint8_t *image, uint64_t image_cap, 165 const uint8_t (*keys)[32], unsigned nkeys, struct ck_slot_eval *ev) 166 { 167 uint8_t digest[AIENOS_SHA512_DIGEST_LEN]; 168 uint8_t id[SHA256_DIGEST_SIZE]; 169 const char *why = 0; 170 unsigned k; 171 172 ev->valid = 0; 173 ev->generation = 0; 174 ev->policy_generation = 0; 175 ev->why = 0; 176 if (ck_slot_manifest_check(m, &why) != 0) { 177 ev->why = why; 178 return 0; 179 } 180 if (m->image_len > image_cap) { 181 ev->why = "image_len exceeds the slot capacity"; 182 return 0; 183 } 184 if (!image) { 185 ev->why = "no image bytes"; 186 return 0; 187 } 188 /* Signature before the (long) digest so a forged manifest costs nothing. */ 189 for (k = 0; k < nkeys; k++) { 190 sha256_hash(keys[k], 32, id); 191 if (eq_bytes(id, m->signer_id, 32)) 192 break; 193 } 194 if (k == nkeys) { 195 ev->why = "signer_id names no trusted key"; 196 return 0; 197 } 198 if (aienos_`ed25519`_verify(m->signature, (const uint8_t *)m, CK_SLOT_TBS_BYTES, keys[k]) != 199 AIENOS_SIG_OK) { 200 ev->why = "signature does not verify"; 201 return 0; 202 } 203 aienos_sha512(image, (size_t)m->image_len, digest); 204 if (!eq_bytes(digest, m->image_sha512, 64)) { 205 ev->why = "image SHA-512 mismatch"; 206 return 0; 207 } 208 ev->valid = 1; 209 ev->generation = m->generation; 210 ev->policy_generation = m->policy_generation; 211 return 1; 212 } 213 214 static int usable(const struct ck_slot_eval *e, uint64_t floor) 215 { 216 return e->valid && e->generation >= floor; 217 } 218 219 void ck_slot_select(const struct ck_slot_control *ctl, const struct ck_slot_eval ev[CK_SLOT_COUNT], 220 struct ck_slot_decision *d) 221 { 222 const char *why; 223 d->boot_slot = -1; 224 d->reason = CK_SLOT_R_RECOVERY_REQUIRED; 225 d->loader_write = 0; 226 for (size_t i = 0; i < sizeof d->ctl_out; i++) 227 ((uint8_t *)&d->ctl_out)[i] = 0; 228 229 if (!ctl || ck_slot_control_check(ctl, &why) != 0) { 230 /* Neither the floor nor the active slot survives loss of control. 231 * Require explicit recovery rather than booting an old signed image. */ 232 return; 233 } 234 235 { 236 uint64_t floor = ctl->generation_floor; 237 unsigned act = ctl->active_slot; 238 unsigned other = act ^ 1u; 239 if (ctl->candidate_slot != CK_SLOT_NONE) { 240 unsigned cand = ctl->candidate_slot; 241 if (ctl->tries_remaining > 0 && usable(&ev[cand], floor)) { 242 d->boot_slot = (int)cand; 243 d->reason = CK_SLOT_R_CANDIDATE_TRY; 244 d->loader_write = 1; 245 copy_bytes((uint8_t *)&d->ctl_out, (const uint8_t *)ctl, sizeof d->ctl_out); 246 d->ctl_out.tries_remaining = (uint8_t)(ctl->tries_remaining - 1u); 247 return; 248 } 249 if (usable(&ev[act], floor)) { 250 d->boot_slot = (int)act; 251 d->reason = ctl->tries_remaining == 0 ? CK_SLOT_R_CANDIDATE_EXHAUSTED 252 : CK_SLOT_R_CANDIDATE_INVALID; 253 return; 254 } 255 /* Active unusable too. The candidate is the other slot and was 256 * found unusable or out of tries, so the fallback rule below 257 * cannot pick it: an exhausted candidate never boots on its own 258 * authority (bounded attempts), and recovery is required. */ 259 } 260 if (usable(&ev[act], floor)) { 261 d->boot_slot = (int)act; 262 d->reason = CK_SLOT_R_ACTIVE; 263 return; 264 } 265 if (usable(&ev[other], floor) && ctl->candidate_slot != other) { 266 d->boot_slot = (int)other; 267 d->reason = CK_SLOT_R_ACTIVE_INVALID_FALLBACK; 268 return; 269 } 270 } 271 } 272 273 void ck_slot_control_init(struct ck_slot_control *c, unsigned active_slot) 274 { 275 static const uint8_t magic[8] = CK_SLOT_CONTROL_MAGIC; 276 for (size_t i = 0; i < sizeof *c; i++) 277 ((uint8_t *)c)[i] = 0; 278 copy_bytes(c->magic, magic, 8); 279 c->version = CK_SLOT_CONTROL_VERSION; 280 c->active_slot = (uint8_t)(active_slot & 1u); 281 c->candidate_slot = CK_SLOT_NONE; 282 c->tries_remaining = 0; 283 } 284 285 int ck_slot_stage_candidate(struct ck_slot_control *c, unsigned slot, uint64_t generation, const char **why) 286 { 287 const char *w = 0; 288 if (ck_slot_control_check(c, &w) != 0) { 289 } else if (slot >= CK_SLOT_COUNT) 290 w = "slot out of range"; 291 else if (slot == c->active_slot) 292 w = "cannot stage the active slot as a candidate"; 293 else if (generation < c->generation_floor) 294 w = "candidate generation below the security floor"; 295 else if (c->last_good_generation != 0 && generation <= c->last_good_generation) 296 w = "candidate generation not above the last good generation"; 297 if (w) { 298 if (why) 299 *why = w; 300 return -1; 301 } 302 c->candidate_slot = (uint8_t)slot; 303 c->tries_remaining = CK_SLOT_MAX_TRIES; 304 c->flags &= (uint8_t)~CK_SLOT_CTL_FLAG_CANDIDATE_FAILED; 305 if (why) 306 *why = 0; 307 return 0; 308 } 309 310 int ck_slot_promote(struct ck_slot_control *c, unsigned booted_slot, uint64_t generation, 311 uint64_t policy_generation, const char **why) 312 { 313 const char *w = 0; 314 if (ck_slot_control_check(c, &w) != 0) { 315 } else if (booted_slot >= CK_SLOT_COUNT) 316 w = "slot out of range"; 317 else if (c->candidate_slot == CK_SLOT_NONE || booted_slot != c->candidate_slot) 318 w = "only the staged candidate can be promoted"; 319 else if (generation < c->generation_floor) 320 w = "promoted generation below the security floor"; 321 else if (policy_generation > generation) 322 w = "policy_generation above generation"; 323 if (w) { 324 if (why) 325 *why = w; 326 return -1; 327 } 328 c->active_slot = (uint8_t)booted_slot; 329 c->candidate_slot = CK_SLOT_NONE; 330 c->tries_remaining = 0; 331 c->flags &= (uint8_t)~CK_SLOT_CTL_FLAG_CANDIDATE_FAILED; 332 c->last_good_generation = generation; 333 if (policy_generation > c->generation_floor) 334 c->generation_floor = policy_generation; /* never lowers */ 335 if (why) 336 *why = 0; 337 return 0; 338 } 339 340 int ck_slot_abandon_candidate(struct ck_slot_control *c, const char **why) 341 { 342 const char *w = 0; 343 if (ck_slot_control_check(c, &w) != 0) { 344 } else if (c->candidate_slot == CK_SLOT_NONE) 345 w = "no candidate to abandon"; 346 if (w) { 347 if (why) 348 *why = w; 349 return -1; 350 } 351 c->candidate_slot = CK_SLOT_NONE; 352 c->tries_remaining = 0; 353 c->flags |= CK_SLOT_CTL_FLAG_CANDIDATE_FAILED; 354 if (why) 355 *why = 0; 356 return 0; 357 } 358 359 const char *ck_slot_reason_text(enum ck_slot_reason r) 360 { 361 switch (r) { 362 case CK_SLOT_R_NONE: return "none"; 363 case CK_SLOT_R_CANDIDATE_TRY: return "candidate_try"; 364 case CK_SLOT_R_ACTIVE: return "active"; 365 case CK_SLOT_R_CANDIDATE_EXHAUSTED: return "candidate_exhausted_active"; 366 case CK_SLOT_R_CANDIDATE_INVALID: return "candidate_invalid_active"; 367 case CK_SLOT_R_ACTIVE_INVALID_FALLBACK: return "active_invalid_fallback"; 368 case CK_SLOT_R_NO_CONTROL: return "no_control_highest_generation"; 369 case CK_SLOT_R_RECOVERY_REQUIRED: return "recovery_required"; 370 } 371 return "unknown"; 372 } 373 374 /* ---- boot record (P11 cut 2b) ---------------------------------------------- */ 375 void ck_slot_boot_seal(struct ck_slot_boot_record *b) 376 { 377 sha256_hash((const uint8_t *)b, CK_SLOT_BOOT_SIGNED, b->digest); 378 } 379 380 int ck_slot_boot_check(const struct ck_slot_boot_record *b, const char **why) 381 { 382 static const uint8_t magic[8] = CK_SLOT_BOOT_MAGIC; 383 uint8_t d[SHA256_DIGEST_SIZE]; 384 const char *w = 0; 385 if (!b) 386 w = "no boot record"; 387 else if (!eq_bytes(b->magic, magic, 8)) 388 w = "bad boot record magic"; 389 else if (b->version != CK_SLOT_BOOT_VERSION) 390 w = "unsupported boot record version"; 391 else { 392 sha256_hash((const uint8_t *)b, CK_SLOT_BOOT_SIGNED, d); 393 if (!eq_bytes(d, b->digest, sizeof d)) 394 w = "boot record digest mismatch"; 395 else if (b->boot_slot >= CK_SLOT_COUNT) 396 w = "boot record slot out of range"; 397 else if (b->reason <= CK_SLOT_R_NONE || b->reason >= CK_SLOT_R_RECOVERY_REQUIRED) 398 w = "boot record reason out of range"; 399 else if (b->control_present > 1) 400 w = "boot record control flag out of range"; 401 else if (b->tries > CK_SLOT_MAX_TRIES) 402 w = "boot record tries above the bound"; 403 else if (b->generation == 0 || b->policy_generation > b->generation) 404 w = "boot record generation fields"; 405 else if (!all_zero(b->reserved, sizeof b->reserved)) 406 w = "boot record reserved bytes nonzero"; 407 else if (!b->control_present && (b->reason != CK_SLOT_R_NO_CONTROL || b->control_seq != 0)) 408 w = "boot record claims a decision without a control record"; 409 else if (b->control_present && b->reason == CK_SLOT_R_NO_CONTROL) 410 w = "boot record claims no control with a control record"; 411 } 412 if (why) 413 *why = w; 414 return w ? -1 : 0; 415 } 416 417 enum ck_slot_action ck_slot_kernel_action(const struct ck_slot_boot_record *b, const struct ck_slot_control *ctl, 418 const struct ck_slot_manifest *booted, int healthy, 419 struct ck_slot_control *out, const char **why) 420 { 421 const char *w = 0; 422 unsigned s; 423 if (ck_slot_boot_check(b, &w) != 0) { 424 if (why) 425 *why = w; 426 return CK_SLOT_A_REFUSE; 427 } 428 s = b->boot_slot; 429 if (!ctl || ck_slot_control_check(ctl, &w) != 0) { 430 if (why) 431 *why = ctl ? w : "no control record on disk"; 432 return CK_SLOT_A_REFUSE; 433 } 434 if (!booted || ck_slot_manifest_check(booted, &w) != 0) { 435 if (why) 436 *why = booted ? w : "started slot's manifest unreadable"; 437 return CK_SLOT_A_REFUSE; 438 } 439 if (booted->generation != b->generation || booted->policy_generation != b->policy_generation || 440 !eq_bytes(booted->image_sha512, b->image_sha512, 64)) { 441 if (why) 442 *why = "started slot's manifest on disk differs from the boot record"; 443 return CK_SLOT_A_REFUSE; 444 } 445 copy_bytes((uint8_t *)out, (const uint8_t *)ctl, sizeof *out); 446 if (b->reason == CK_SLOT_R_CANDIDATE_TRY) { 447 if (ctl->candidate_slot != s) { 448 if (why) 449 *why = "control no longer names the started slot as candidate"; 450 return CK_SLOT_A_NONE; 451 } 452 if (!healthy) { 453 if (why) 454 *why = "health milestone not reached"; 455 return CK_SLOT_A_WAIT; 456 } 457 if (ck_slot_promote(out, s, booted->generation, booted->policy_generation, &w) != 0) { 458 if (why) 459 *why = w; 460 return CK_SLOT_A_REFUSE; 461 } 462 if (why) 463 *why = 0; 464 return CK_SLOT_A_PROMOTE; 465 } 466 if ((b->reason == CK_SLOT_R_CANDIDATE_EXHAUSTED || b->reason == CK_SLOT_R_CANDIDATE_INVALID) && 467 ctl->candidate_slot != CK_SLOT_NONE && ctl->active_slot == s) { 468 if (ck_slot_abandon_candidate(out, &w) != 0) { 469 if (why) 470 *why = w; 471 return CK_SLOT_A_REFUSE; 472 } 473 if (why) 474 *why = 0; 475 return CK_SLOT_A_ABANDON; 476 } 477 if (why) 478 *why = "nothing to write"; 479 return CK_SLOT_A_NONE; 480 } 481 482 const char *ck_slot_action_text(enum ck_slot_action a) 483 { 484 switch (a) { 485 case CK_SLOT_A_NONE: return "none"; 486 case CK_SLOT_A_PROMOTE: return "promote"; 487 case CK_SLOT_A_ABANDON: return "abandon"; 488 case CK_SLOT_A_WAIT: return "wait"; 489 case CK_SLOT_A_REFUSE: return "refuse"; 490 } 491 return "unknown"; 492 }