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/kernel/tests/test_continuity_recovery.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 / kernel / tests / test_continuity_recovery.c` 650 lines ยท 27728 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 /* test_continuity_recovery.c -- host tests of svc/continuity_recovery.c (C 18 * continuity port, cut 5: Recovery Core inspection, repair, operator 19 * provisioning; contract native/kernel/CONTINUITY_RECOVERY_CONTRACT.md section 20 * 6.2, aienos#222). Oracle: crates/aienos-kernel/src/recovery_core.rs and 21 * recovery_core_tests.rs. 22 * 23 * Storage: the REAL sealed Store (native/store, M5 envelopes, anchor) in a 24 * file, at 4096 and 512 byte blocks. Entropy: the real ck_rng over a mocked 25 * RNDR. Every inspection and every refused action must leave the image byte 26 * for byte unchanged (INV-13, INV-16). 27 * 28 * One test per Rust recovery_core test row: 91d inspection never writes, 91e 29 * repair needs the operator, 91f provisioning is operator only, 91g identity 30 * lost in the newest root never looks unprovisioned, 91h malformed peer 31 * without identity is not repaired, 91i orphans are Corrupt, 91j identity loss 32 * offers no minting action, 91k challenges bind state and action. Rows 91l-91p 33 * (operator auth primitive) are test_continuity_resolve (cut 3). The QEMU rows 34 * 88-91c are NOT_RUN. 35 * Mutants (make continuity-mutants) must each turn this test FAIL. */ 36 #include 37 #include 38 #include 39 #include 40 #include 41 42 #include "ck_test.h" 43 #include "continuity_recovery.h" 44 #include "disk_file.h" 45 46 #define CK_(c, ...) \ 47 do { \ 48 ck_t_run++; \ 49 if (!(c)) { \ 50 ck_t_fail++; \ 51 printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #c); \ 52 printf(__VA_ARGS__); \ 53 printf("\n"); \ 54 } \ 55 } while (0) 56 57 #define A_UNITS 4u 58 #define S_UNITS 512u 59 #define T_UNITS (A_UNITS + S_UNITS) 60 #define IMG_BYTES ((size_t)T_UNITS * 4096u) 61 62 static ss_workspace g_ws; 63 static ss_store g_s; 64 static disk_file g_f; 65 static disk_dev g_d; 66 static st_disk g_sd; 67 static st_dev g_sdev; 68 static ts_device g_tdev; 69 static ss_keys g_k; 70 static uint32_t g_bs; 71 static char g_path[256]; 72 static struct cr_work g_w; 73 static struct cr_txwork g_tw; 74 static struct cr_view g_v, g_v2, g_out; 75 static struct cr_source g_src; 76 static struct cr_sink g_snk; 77 static struct cr_sealed_sink g_sk; 78 static struct rc_env E; 79 static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 80 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; 81 static uint8_t snap_img[IMG_BYTES], now_img[IMG_BYTES]; 82 static uint8_t OPKEY[32], BADKEY[32]; 83 84 static int g_open; 85 static void dev_close(void) 86 { 87 if (g_open) disk_file_close(&g_f); 88 g_open = 0; 89 } 90 static int dev_open(int create) 91 { 92 dev_close(); 93 uint32_t bpu = 4096u / g_bs; 94 if (create) unlink(g_path); 95 memset(&g_f, 0, sizeof g_f); 96 if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + S_UNITS) * bpu, create) != 0) return -1; 97 g_open = 1; 98 if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; 99 ss_ts_device(&g_d, &g_tdev); 100 return 0; 101 } 102 static void make_keys(void) 103 { 104 uint8_t kvol[32]; 105 memset(kvol, 0x77, 32); 106 m5_subkeys sk; 107 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 108 memset(&g_k, 0, sizeof g_k); 109 g_k.identity_class = M5_ID_TEST; 110 g_k.key_generation = 1; 111 memcpy(g_k.k_root_auth, sk.k_root_auth, 32); 112 memcpy(g_k.k_domain, sk.k_artifact, 32); 113 m5_subkeys_wipe(&sk); 114 } 115 static void bind(void) 116 { 117 g_sk.s = &g_s; 118 g_sk.hook = NULL; 119 g_sk.hook_arg = NULL; 120 cr_bind_sealed(&g_src, &g_s); 121 cr_bind_sealed_sink(&g_snk, &g_sk); 122 } 123 /* A formatted store, no identity. */ 124 static void fresh(void) 125 { 126 CK_(dev_open(1) == 0, "rig create"); 127 CK_(ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) == 0, "format"); 128 CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "open genesis"); 129 bind(); 130 } 131 static void env_open(void) 132 { 133 CK_(dev_open(0) == 0, "reopen rig"); 134 E.dev = &g_sdev; 135 E.anchor = &g_tdev; 136 E.anchor_lba = 0; 137 E.keys = &g_k; 138 E.ws = &g_ws; 139 E.store = &g_s; 140 E.w = &g_w; 141 E.tw = &g_tw; 142 E.view = &g_v2; 143 } 144 static int file_load(uint8_t *dst) 145 { 146 dev_close(); 147 FILE *f = fopen(g_path, "rb"); 148 if (!f) return -1; 149 size_t n = fread(dst, 1, IMG_BYTES, f); 150 fclose(f); 151 return n == IMG_BYTES ? 0 : -2; 152 } 153 static void snapshot(void) { CK_(file_load(snap_img) == 0, "snapshot"); } 154 static int unchanged(void) { return file_load(now_img) == 0 && memcmp(now_img, snap_img, IMG_BYTES) == 0; } 155 /* Store-region unit u of the image now equals the snapshot. */ 156 static int unit_same(unsigned u) 157 { 158 size_t o = (size_t)(A_UNITS + u) * 4096u; 159 return file_load(now_img) == 0 && memcmp(now_img + o, snap_img + o, 4096) == 0; 160 } 161 static int unit_zero(unsigned u) 162 { 163 size_t o = (size_t)(A_UNITS + u) * 4096u; 164 if (file_load(now_img) != 0) return 0; 165 for (size_t i = 0; i < 4096; i++) 166 if (now_img[o + i]) return 0; 167 return 1; 168 } 169 static void fill_unit(unsigned u, uint8_t fillb) 170 { 171 dev_close(); 172 FILE *f = fopen(g_path, "r+b"); 173 CK_(f != NULL, "open image"); 174 static uint8_t junk[4096]; 175 memset(junk, fillb, sizeof junk); 176 if (f) { 177 fseek(f, (long)((A_UNITS + u) * 4096u), SEEK_SET); 178 CK_(fwrite(junk, 1, sizeof junk, f) == sizeof junk, "write junk"); 179 fclose(f); 180 } 181 } 182 static void flip_byte(uint64_t unit, unsigned off) 183 { 184 dev_close(); 185 FILE *f = fopen(g_path, "r+b"); 186 CK_(f != NULL, "open image"); 187 if (!f) return; 188 long pos = (long)((A_UNITS + unit) * 4096u + off); 189 uint8_t b = 0; 190 fseek(f, pos, SEEK_SET); 191 CK_(fread(&b, 1, 1, f) == 1, "read byte"); 192 b ^= 0xff; 193 fseek(f, pos, SEEK_SET); 194 CK_(fwrite(&b, 1, 1, f) == 1, "write byte"); 195 fclose(f); 196 } 197 198 /* ---- mocked RNDR ---- */ 199 struct mock { 200 int present; 201 uint64_t ctr; 202 }; 203 static struct mock g_m; 204 static struct ck_rng_ops g_ops; 205 static struct ck_rng g_rng; 206 static int m_present(void *c) { return ((struct mock *)c)->present; } 207 static int m_read(void *c, uint64_t *out) 208 { 209 struct mock *m = c; 210 uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); 211 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 212 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 213 *out = z ^ (z >> 31); 214 return 1; 215 } 216 static void rng_init(uint64_t seed, int present) 217 { 218 g_m.present = present; 219 g_m.ctr = seed; 220 g_ops.present = m_present; 221 g_ops.read = m_read; 222 g_ops.ctx = &g_m; 223 memset(&g_rng, 0, sizeof g_rng); 224 (void)ck_rng_probe(&g_rng, &g_ops); 225 } 226 227 static struct cc_record fact(const char *s) 228 { 229 struct cc_record r; 230 memset(&r, 0, sizeof r); 231 r.status = CC_EPI_DIRECT_OBSERVATION; 232 r.len = (uint16_t)strlen(s); 233 r.statement = (const uint8_t *)s; 234 return r; 235 } 236 237 static uint8_t g_agent[32], g_mem[32]; 238 239 /* Provisioned (Qualification, as the Rust fixture) with one remembered fact. */ 240 static void remembered(void) 241 { 242 fresh(); 243 rng_init(5005, 1); 244 const char *why = NULL; 245 int rc = 0; 246 CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &rc) == CR_RESOLVED, 247 "provision"); 248 struct cc_record r1 = fact("keep this"); 249 struct cr_update up = {NULL, &r1, 1}; 250 static struct cr_view cur; 251 memcpy(&cur, &g_v, sizeof cur); 252 CK_(cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v, &why, &rc) == CR_RESOLVED, "remember"); 253 memcpy(g_agent, g_v.root.agent_id, 32); 254 memcpy(g_mem, g_v.memory, 32); 255 } 256 static void degrade(uint8_t fillb) 257 { 258 uint32_t active = st_active_slot(&g_s.st); 259 fill_unit(1u - active, fillb); 260 } 261 static int raw_commit(uint16_t kind, const uint8_t *b, size_t n) 262 { 263 ss_object so = {kind, 1, b, n}; 264 return ss_transact(&g_s, &so, 1, NULL, NULL, NULL); 265 } 266 /* first Store unit of the first AgentRoot envelope */ 267 static uint64_t root_unit(void) 268 { 269 for (uint32_t i = 0; i < g_s.nclaims; i++) { 270 if (g_ws.claims[i].c.obj.object_kind != CC_KIND_AGENT_ROOT) continue; 271 uint32_t n = 0; 272 const sv1_entry *cat = st_catalog(&g_s.st, &n); 273 for (uint32_t j = 0; j < n; j++) 274 if (memcmp(cat[j].object_id, g_ws.claims[i].sid, 32) == 0) return cat[j].first_unit; 275 } 276 CK_(0, "no agent root envelope found"); 277 return 0; 278 } 279 280 static struct rc_record R, R2; 281 static void inspect(struct rc_record *r) 282 { 283 env_open(); 284 CK_(rc_inspect(&E, r) == 0, "inspect"); 285 } 286 static void respond(const struct rc_record *r, uint8_t action, const uint8_t key[32], uint8_t out[32]) 287 { 288 uint8_t ch[32]; 289 CK_(rc_challenge(r, action, ch) == 1, "challenge available"); 290 cr_operator_response(key, ch, out); 291 } 292 static int do_repair(const uint8_t resp[32], struct rc_record *after) 293 { 294 env_open(); 295 return rc_repair_degraded_peer(&E, OPKEY, resp, after); 296 } 297 static int do_prov(const uint8_t key[32], const uint8_t resp[32], int *cro, const char **why) 298 { 299 env_open(); 300 return rc_provision_identity(&E, key, resp, &g_rng, &g_out, cro, why, NULL); 301 } 302 303 /* ---- 91d: inspection never writes and names the reason (MR-1) ---- */ 304 static void t_91d(void) 305 { 306 char t[200]; 307 /* blank device */ 308 CK_(dev_open(1) == 0, "blank"); 309 snapshot(); 310 inspect(&R); 311 CK_(unchanged(), "bs=%u inspect of a blank device wrote", g_bs); 312 CK_(R.reason == RC_STORE_UNFORMATTED && !R.have_mount && rc_applicable(&R) == 0, "blank: reason %d mount_rc %d", R.reason, R.mount_rc); 313 CK_(R.slot[0] == RC_SLOT_ZERO && R.slot[1] == RC_SLOT_ZERO && !R.have_uuid, "blank slots"); 314 CK_(rc_challenge(&R, CR_ACTION_PROVISION_IDENTITY, (uint8_t[32]){0}) == 0, "no challenge without uuid and mount"); 315 CK_(rc_marker_entry(t, sizeof t, &R) > 0 && strcmp(t, "RECOVERY_CORE: ENTERED reason=StoreUnformatted") == 0, "marker '%s'", t); 316 317 /* formatted, no identity */ 318 fresh(); 319 snapshot(); 320 inspect(&R); 321 CK_(unchanged(), "inspect of an unprovisioned store wrote"); 322 CK_(R.reason == RC_UNPROVISIONED && R.have_mount && R.mount_state == ST_VALID && !R.have_identity, "unprovisioned reason %d", R.reason); 323 CK_(R.slot[0] == RC_SLOT_SUPERBLOCK && R.slot[1] == RC_SLOT_ZERO && R.have_uuid && memcmp(R.uuid, RUUID, 16) == 0, 324 "slot views and uuid"); 325 CK_(R.have_catalog && R.n_roots == 0 && R.n_manifests == 0 && R.n_other == 0, "catalog counts"); 326 327 /* healthy */ 328 remembered(); 329 snapshot(); 330 inspect(&R); 331 CK_(unchanged(), "inspect of a healthy store wrote"); 332 CK_(R.reason == RC_REASON_NONE && R.have_identity && memcmp(R.agent_id, g_agent, 32) == 0, "healthy: reason %d", R.reason); 333 CK_(R.n_roots == 1 && R.n_manifests == 2 && rc_applicable(&R) == 0, "one root, two manifests, nothing applicable"); 334 CK_(rc_marker_entry(t, sizeof t, &R) > 0 && strcmp(t, "RECOVERY_CORE: NOT_NEEDED") == 0, "marker '%s'", t); 335 336 /* degraded: malformed peer */ 337 degrade(0xa5); 338 snapshot(); 339 uint32_t active_before = 0; 340 inspect(&R); 341 CK_(unchanged(), "inspect of a degraded store wrote (MR-1)"); 342 inspect(&R); 343 CK_(unchanged(), "second inspect wrote"); 344 (void)active_before; 345 CK_(R.reason == RC_DEGRADED && R.peer == ST_PEER_MALFORMED && R.mount_state == ST_DEGRADED_RECOVERY, "degraded reason %d", R.reason); 346 CK_(R.have_identity && memcmp(R.agent_id, g_agent, 32) == 0, "record shows the same agent"); 347 CK_(R.slot[0] != R.slot[1] && (R.slot[0] == RC_SLOT_UNDECODABLE || R.slot[1] == RC_SLOT_UNDECODABLE), "one undecodable slot"); 348 CK_(rc_marker_entry(t, sizeof t, &R) > 0 && strcmp(t, "RECOVERY_CORE: ENTERED reason=Degraded(Malformed)") == 0, "marker '%s'", t); 349 CK_(rc_applicable(&R) == CR_ACTION_REPAIR_DEGRADED_PEER, "88: repair offered"); 350 CK_(rc_marker_challenge(t, sizeof t, &R, CR_ACTION_REPAIR_DEGRADED_PEER) > 0 && 351 strncmp(t, "RECOVERY_CHALLENGE: action=repair-degraded-peer challenge=", 58) == 0 && strlen(t) == 58 + 64, 352 "challenge marker '%s'", t); 353 CK_(unchanged(), "markers wrote nothing"); 354 /* NULL arguments */ 355 CK_(rc_inspect(NULL, &R) == -1 && rc_inspect(&E, NULL) == -1, "NULL args"); 356 } 357 358 /* ---- 91e (88a-c, 89, 89a, 89b): repair needs the operator, restores a writable store (MR-6) ---- */ 359 static void t_91e(void) 360 { 361 remembered(); 362 degrade(0xa5); 363 inspect(&R); 364 CK_(rc_applicable(&R) == CR_ACTION_REPAIR_DEGRADED_PEER, "applicable"); 365 uint8_t good[32], wrong_key[32], wrong_action[32], zero[32] = {0}, other_state[32]; 366 respond(&R, CR_ACTION_REPAIR_DEGRADED_PEER, OPKEY, good); 367 respond(&R, CR_ACTION_REPAIR_DEGRADED_PEER, BADKEY, wrong_key); 368 respond(&R, CR_ACTION_PROVISION_IDENTITY, OPKEY, wrong_action); 369 /* a response to another state's challenge (88c) */ 370 cr_operator_response(OPKEY, (uint8_t[32]){0x42}, other_state); 371 uint32_t active = st_active_slot(&g_s.st); /* g_s is the store inspect opened */ 372 snapshot(); 373 const uint8_t *bad[4] = {zero, wrong_key, wrong_action, other_state}; 374 char t[100]; 375 for (int i = 0; i < 4; i++) { 376 int rc = do_repair(bad[i], NULL); 377 CK_(rc == RC_UNAUTHORISED, "bad response %d: %s", i, rc_outcome_name(rc)); 378 CK_(unchanged(), "refused repair %d wrote", i); 379 } 380 CK_(rc_marker_refused(t, sizeof t, RC_UNAUTHORISED, 0) > 0 && strcmp(t, "RECOVERY_REFUSED (Unauthorised)") == 0, "marker '%s'", t); 381 /* provisioning is not the applicable action */ 382 { 383 int cro = -1; 384 int rc = do_prov(OPKEY, wrong_action, &cro, NULL); 385 CK_(rc == RC_NOT_APPLICABLE && unchanged(), "provision on a degraded store: %s", rc_outcome_name(rc)); 386 } 387 /* 89: the authorised repair */ 388 snapshot(); 389 uint64_t gen = R.generation; 390 int rc = do_repair(good, &R2); 391 CK_(rc == RC_OK, "authorised repair: %s", rc_outcome_name(rc)); 392 CK_(R2.reason == RC_REASON_NONE && R2.mount_state == ST_VALID && R2.peer == ST_PEER_ZERO, "healthy after repair (reason %d)", R2.reason); 393 CK_(unit_zero(1u - active), "the inactive slot is zero (MR-6)"); 394 CK_(unit_same(active), "the ACTIVE slot is byte for byte untouched (MR-6)"); 395 CK_(R2.generation == gen && R2.have_identity, "generation unchanged, identity kept"); 396 CK_(rc_marker_done(t, sizeof t, CR_ACTION_REPAIR_DEGRADED_PEER, NULL) > 0 && 397 strcmp(t, "RECOVERY_ACTION: repair-degraded-peer DONE") == 0, "marker '%s'", t); 398 /* 89a: same identity and memory, writable again */ 399 env_open(); 400 { 401 ss_store *s = &g_s; 402 CK_(ss_open(s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0 && s->st.state == ST_VALID, "reopen valid"); 403 bind(); 404 int committed = 0; 405 CK_(cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v, &committed, NULL, NULL) == CR_RESOLVED && committed == 1, "resume writable"); 406 CK_(memcmp(g_v.root.agent_id, g_agent, 32) == 0 && memcmp(g_v.memory, g_mem, 32) == 0 && g_v.cortex_count == 1, 407 "same agent, same memory"); 408 } 409 /* 89b: replay of the old authorisation on the new state */ 410 snapshot(); 411 rc = do_repair(good, NULL); 412 CK_(rc == RC_NOT_APPLICABLE, "replay: %s", rc_outcome_name(rc)); 413 CK_(unchanged(), "replay wrote"); 414 CK_(rc_marker_refused(t, sizeof t, rc, 0) > 0 && strcmp(t, "RECOVERY_REFUSED (NotApplicable)") == 0, "marker '%s'", t); 415 } 416 417 /* ---- 91f (90, 90a, 90b): provisioning is operator only and only on an unprovisioned store (MR-9) ---- */ 418 static void t_91f(void) 419 { 420 char t[200]; 421 fresh(); 422 inspect(&R); 423 CK_(rc_applicable(&R) == CR_ACTION_PROVISION_IDENTITY, "90: provision offered"); 424 CK_(rc_marker_challenge(t, sizeof t, &R, CR_ACTION_PROVISION_IDENTITY) > 0, "challenge marker"); 425 uint8_t good[32], wrong_action[32], seven[32]; 426 memset(seven, 7, 32); 427 respond(&R, CR_ACTION_PROVISION_IDENTITY, OPKEY, good); 428 respond(&R, CR_ACTION_REPAIR_DEGRADED_PEER, OPKEY, wrong_action); 429 snapshot(); 430 rng_init(6006, 1); 431 int cro = -1; 432 CK_(do_prov(OPKEY, seven, &cro, NULL) == RC_UNAUTHORISED && unchanged(), "90a: wrong response"); 433 CK_(do_prov(BADKEY, good, &cro, NULL) == RC_UNAUTHORISED && unchanged(), "90a: wrong key"); 434 CK_(do_prov(OPKEY, wrong_action, &cro, NULL) == RC_UNAUTHORISED && unchanged(), "response for the other action"); 435 /* repair is not applicable here */ 436 CK_(do_repair(wrong_action, NULL) == RC_NOT_APPLICABLE && unchanged(), "repair on an unprovisioned store"); 437 /* no entropy: authorised, but nothing may be minted without it */ 438 rng_init(6007, 0); 439 CK_(do_prov(OPKEY, good, &cro, NULL) == RC_CONTINUITY && cro == CR_NO_ENTROPY && unchanged(), "no entropy: outcome %d", cro); 440 CK_(rc_marker_refused(t, sizeof t, RC_CONTINUITY, cro) > 0 && strncmp(t, "RECOVERY_REFUSED (Continuity(", 29) == 0, 441 "marker '%s'", t); 442 /* 90b: authorised */ 443 rng_init(6008, 1); 444 uint64_t gen = R.generation; 445 int rc = do_prov(OPKEY, good, &cro, NULL); 446 CK_(rc == RC_OK, "authorised provisioning: %s (cr %d)", rc_outcome_name(rc), cro); 447 CK_(g_out.root.source == CC_SOURCE_OPERATOR, "source is Operator, not Qualification (INV-18, MR-9): %u", g_out.root.source); 448 CK_(memcmp(g_out.root.store_uuid, RUUID, 16) == 0, "Store uuid from the inspection"); 449 CK_(g_out.manifest.sequence == 1 && g_out.manifest.incarnation == 1 && g_out.state.written_at == 1, "genesis view"); 450 CK_(g_out.root.provisioned_generation == gen + 1, "provisioned_generation"); 451 CK_(rc_marker_done(t, sizeof t, CR_ACTION_PROVISION_IDENTITY, g_out.root.agent_id) > 0 && 452 strncmp(t, "RECOVERY_ACTION: provision-identity DONE agent=", 47) == 0, "marker '%s'", t); 453 uint8_t agent[32]; 454 memcpy(agent, g_out.root.agent_id, 32); 455 /* a cold resume returns that agent, and the stored root says Operator */ 456 env_open(); 457 CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "cold open"); 458 bind(); 459 CK_(cr_resolve(&g_src, &g_w, &g_v, NULL, NULL) == CR_RESOLVED && memcmp(g_v.root.agent_id, agent, 32) == 0, "cold resolve"); 460 CK_(g_v.root.source == CC_SOURCE_OPERATOR, "stored source is Operator (MR-9)"); 461 int committed = 0; 462 CK_(cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v, &committed, NULL, NULL) == CR_RESOLVED && committed == 1 && 463 memcmp(g_v.root.agent_id, agent, 32) == 0, 464 "cold resume returns that agent"); 465 /* the same authorisation cannot provision again */ 466 snapshot(); 467 CK_(do_prov(OPKEY, good, &cro, NULL) == RC_NOT_APPLICABLE && unchanged(), "replay"); 468 } 469 470 static const char *refusal_ok(const struct rc_record *r) 471 { 472 /* never Unprovisioned, never repairable */ 473 return (r->reason != RC_UNPROVISIONED && r->reason != RC_REASON_NONE && rc_applicable(r) == 0) ? NULL : "bad reason"; 474 } 475 476 /* every action, with a response valid for the exact state if one can be formed, is refused and writes nothing */ 477 static void all_actions_refused(const struct rc_record *r, const char *what) 478 { 479 uint8_t resp[32]; 480 for (int a = 1; a <= 2; a++) { 481 if (rc_challenge(r, (uint8_t)a, resp)) cr_operator_response(OPKEY, resp, resp); 482 else memset(resp, 0x55, 32); 483 int rc = a == 1 ? do_repair(resp, NULL) : do_prov(OPKEY, resp, &(int){0}, NULL); 484 CK_(rc == RC_NOT_APPLICABLE, "%s: action %d: %s", what, a, rc_outcome_name(rc)); 485 CK_(unchanged(), "%s: action %d wrote", what, a); 486 } 487 } 488 489 /* ---- 91g: identity lost in the newest root never looks unprovisioned (MR-5) ---- */ 490 static void t_91g(void) 491 { 492 char t[100]; 493 fresh(); 494 rng_init(7007, 1); 495 const char *why = NULL; 496 int rc = 0; 497 CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &rc) == CR_RESOLVED, "provision"); 498 flip_byte(root_unit(), 20); 499 snapshot(); 500 inspect(&R); 501 CK_(unchanged(), "inspect wrote"); 502 CK_(refusal_ok(&R) == NULL, "reason %d", R.reason); 503 CK_(R.reason == RC_SEALED_REFUSAL || R.reason == RC_DEGRADED || R.reason == RC_STORE_MOUNT || R.reason == RC_CONTINUITY_CORRUPT, 504 "a refusal reason"); 505 /* K-3 observed: the flipped ciphertext byte is refused by the sealed mount before continuity runs */ 506 CK_(R.reason == RC_SEALED_REFUSAL, "K-3 expected a sealed refusal, got reason %d rc %d", R.reason, R.mount_rc); 507 CK_(rc_reason_text(t, sizeof t, &R) > 0 && strncmp(t, "SealedRefusal(", 14) == 0, "reason text '%s'", t); 508 if (g_bs == 4096) printf("K-3/K-4 answer: byte 20 of the newest AgentRoot envelope flipped: reason=%d (RC_SEALED_REFUSAL=%d) mount_rc=%d (%s), mounted=%d\n", R.reason, RC_SEALED_REFUSAL, R.mount_rc, ss_strerror(R.mount_rc), R.have_mount); 509 all_actions_refused(&R, "91g"); 510 } 511 512 /* ---- 91h: a malformed peer with no resolvable identity is not repaired (MR-5, MR-7) ---- */ 513 static void t_91h(void) 514 { 515 fresh(); 516 degrade(0xa5); 517 snapshot(); 518 inspect(&R); 519 CK_(unchanged(), "inspect wrote"); 520 CK_(R.have_mount && R.mount_state == ST_DEGRADED_RECOVERY && R.peer == ST_PEER_MALFORMED, "degraded on a malformed peer"); 521 CK_(R.reason == RC_DEGRADED, "reason is Degraded, never Unprovisioned (MR-5): %d", R.reason); 522 CK_(!R.have_identity && rc_applicable(&R) == 0, "no identity: nothing applicable (MR-5, MR-7): %d", rc_applicable(&R)); 523 all_actions_refused(&R, "91h"); 524 } 525 526 /* ---- 91i: orphaned continuity objects are Corrupt, not Unprovisioned ---- */ 527 static void t_91i(void) 528 { 529 fresh(); 530 struct cc_manifest m; 531 memset(&m, 0, sizeof m); 532 memset(m.root, 0x77, 32); 533 m.sequence = 1; 534 m.incarnation = 1; 535 memset(m.agent_state, 0x66, 32); 536 static uint8_t b[CC_MAX_OBJECT_BYTES]; 537 size_t n = 0; 538 CK_(cc_manifest_encode(&m, b, sizeof b, &n, NULL) == CC_OK && raw_commit(CC_KIND_MANIFEST, b, n) == 0, "orphan manifest"); 539 snapshot(); 540 inspect(&R); 541 CK_(unchanged(), "inspect wrote"); 542 CK_(R.reason == RC_CONTINUITY_CORRUPT && R.why && !strcmp(R.why, "continuity objects without an agent root"), 543 "reason %d why %s", R.reason, R.why ? R.why : "-"); 544 CK_(rc_applicable(&R) == 0, "nothing applicable"); 545 all_actions_refused(&R, "91i"); 546 } 547 548 /* ---- 91j: identity loss through corruption offers no action that mints ---- */ 549 static void t_91j(void) 550 { 551 remembered(); 552 flip_byte(root_unit(), 20); 553 snapshot(); 554 inspect(&R); 555 CK_(unchanged(), "inspect wrote"); 556 CK_(R.reason == RC_SEALED_REFUSAL || R.reason == RC_STORE_MOUNT || R.reason == RC_CONTINUITY_CORRUPT || R.reason == RC_DEGRADED, 557 "refusal reason %d", R.reason); 558 CK_(rc_applicable(&R) == 0, "no action applicable"); 559 /* 91: ENTERED, and no challenge is offered */ 560 char t[200]; 561 CK_(rc_marker_entry(t, sizeof t, &R) > 0 && strncmp(t, "RECOVERY_CORE: ENTERED reason=", 30) == 0, "marker '%s'", t); 562 CK_(rc_marker_challenge(t, sizeof t, &R, CR_ACTION_PROVISION_IDENTITY) == 0, "no challenge when no mount"); 563 uint8_t forged[32]; 564 memset(forged, 0x55, 32); 565 CK_(do_prov(OPKEY, forged, &(int){0}, NULL) != RC_OK, "provision refused"); 566 CK_(do_repair(forged, NULL) != RC_OK, "repair refused"); 567 CK_(unchanged(), "no action wrote after identity loss"); 568 all_actions_refused(&R, "91j"); 569 } 570 571 /* ---- 91k: challenges bind state and action (MR-2, MR-3 primitives are cut 3) ---- */ 572 static void t_91k(void) 573 { 574 remembered(); 575 degrade(0xa5); 576 inspect(&R); 577 uint8_t c1[32], c2[32], c3[32]; 578 CK_(rc_challenge(&R, CR_ACTION_REPAIR_DEGRADED_PEER, c1) == 1, "c1"); 579 degrade(0x5a); /* different on-disk state, same generation */ 580 inspect(&R2); 581 CK_(rc_challenge(&R2, CR_ACTION_REPAIR_DEGRADED_PEER, c2) == 1, "c2"); 582 CK_(memcmp(c1, c2, 32) != 0, "different on-disk state, different challenge"); 583 CK_(memcmp(R.state_digest, R2.state_digest, 32) != 0, "digests differ"); 584 CK_(rc_challenge(&R, CR_ACTION_PROVISION_IDENTITY, c3) == 1 && memcmp(c1, c3, 32) != 0, "different action, different challenge"); 585 /* a response to the old state does not authorise a repair of the new one */ 586 uint8_t old[32]; 587 cr_operator_response(OPKEY, c1, old); 588 snapshot(); 589 CK_(do_repair(old, NULL) == RC_UNAUTHORISED && unchanged(), "stale authorisation refused"); 590 } 591 592 /* ---- k4: a newest generation that is sealed-valid but graph-broken (contract K-4) ---- 593 * Provision and remember one fact, then commit through the real sealed path a validly 594 * encoded manifest (sequence 2, previous = the live manifest, right root) whose agent_state 595 * object was never written. The anchor is fresh, so ss_open succeeds. Predicted from 596 * continuity_recovery.c:51-61,83,102-111 and continuity_resolve.c:75,240-243: 597 * mount Valid, resolve Corrupt "referenced object is absent", reason ContinuityCorrupt, 598 * rc_applicable() none. Unbuilt: a graph-broken newest AgentRoot (needs a second root, 599 * which resolves as Conflict, not graph damage) and a foreign-agent state. */ 600 static void t_k4(void) 601 { 602 char t[200]; 603 remembered(); 604 struct cc_manifest m; 605 memset(&m, 0, sizeof m); 606 memcpy(m.root, g_v.root_id, 32); 607 memcpy(m.previous, g_v.manifest_id, 32); 608 m.sequence = g_v.manifest.sequence + 1; 609 m.incarnation = g_v.manifest.incarnation; 610 memset(m.agent_state, 0x66, 32); /* never written */ 611 static uint8_t b[CC_MAX_OBJECT_BYTES]; 612 size_t n = 0; 613 CK_(cc_manifest_encode(&m, b, sizeof b, &n, NULL) == CC_OK && raw_commit(CC_KIND_MANIFEST, b, n) == 0, "broken newer manifest"); 614 snapshot(); 615 inspect(&R); 616 CK_(unchanged(), "inspect wrote"); 617 CK_(R.have_mount && R.mount_state == ST_VALID, "the store mounts (fresh anchor): have_mount %d state %d", R.have_mount, R.mount_state); 618 CK_(R.reason == RC_CONTINUITY_CORRUPT && R.why && !strcmp(R.why, "referenced object is absent"), 619 "K-4 reason %d why %s", R.reason, R.why ? R.why : "-"); 620 CK_(!R.have_identity, "no identity resolved"); 621 CK_(rc_reason_text(t, sizeof t, &R) > 0 && strncmp(t, "ContinuityCorrupt(", 18) == 0, "reason text '%s'", t); 622 /* INV-15: no action mints. A mutant that returned an action here fails this line and all_actions_refused. */ 623 CK_(rc_applicable(&R) == 0, "no action applicable (INV-15)"); 624 if (g_bs == 4096) printf("K-4 answer: sealed-valid newest manifest with a missing agent_state: reason=%d (RC_CONTINUITY_CORRUPT=%d) why='%s' mounted=%d state=%d applicable=%d\n", R.reason, RC_CONTINUITY_CORRUPT, R.why ? R.why : "-", R.have_mount, R.mount_state, rc_applicable(&R)); 625 all_actions_refused(&R, "k4"); 626 } 627 628 int main(void) 629 { 630 make_keys(); 631 memset(OPKEY, 0x0f, 32); 632 memset(BADKEY, 0x0e, 32); 633 snprintf(g_path, sizeof g_path, "/tmp/ck_rc_test_%d.img", (int)getpid()); 634 static const uint32_t geos[2] = {4096u, 512u}; 635 for (int g = 0; g < 2; g++) { 636 g_bs = geos[g]; 637 t_91d(); 638 t_91e(); 639 t_91f(); 640 t_91g(); 641 t_91h(); 642 t_91i(); 643 t_91j(); 644 t_91k(); 645 t_k4(); 646 } 647 dev_close(); 648 unlink(g_path); 649 return ck_t_verdict("test_continuity_recovery"); 650 }