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/tools/ck_cont_tool.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 / tools / ck_cont_tool.c` 429 lines ยท 16364 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 /* ck_cont_tool -- host tool for the continuity / Recovery Core QEMU gates 18 * (scripts/qemu_ck_continuity_test.sh, scripts/qemu_ck_recovery_test.sh). It 19 * reuses the kernel's own Store and continuity code on a boot-disk image made by 20 * tools/ck_gpt_image.c (AIENOS GPT partition, TEST keys). Hosted C; test 21 * tooling, never in an image. No Python, no outside libraries. 22 * 23 * ck_cont_tool respond KEY_HEX CHALLENGE_HEX 24 * operator response (HMAC-SHA256, contract 1.7 / recovery.rs OperatorAuth), hex on stdout 25 * ck_cont_tool inject-peer IMG BS 26 * fill the INACTIVE superblock slot (Store-region unit 1 - active) with seeded garbage 27 * (the Rust `store-tool inject ... inactive seeded_garbage`); needs a mountable Store 28 * ck_cont_tool corrupt-root IMG BS 29 * flip byte 20 of the first AgentRoot (kind 16) envelope (the Rust `corrupt-kind ... 16` 30 * stand-in; contract K-3/K-4: the sealed mount refuses it) 31 * ck_cont_tool subject-dump IMG BS [HEAD_OUT] 32 * read-only: the ALLEN subject (kind 24) on the image as the kernel resolves it; optional 33 * canonical bytes of the head object (ARCH-0035 / OS-0018, PROPOSED) 34 * ck_cont_tool subject-plant IMG BS fork | foreign SRC_IMG 35 * TEST fixture: a second genesis subject (fork), or every subject object of another image, verbatim 36 * ck_cont_tool corrupt-subject IMG BS 37 * flip byte 20 of the first subject envelope 38 */ 39 #include 40 #include 41 #include 42 #include "continuity_resolve.h" 43 #include "continuity_resolve_sealed.h" 44 #include "continuity_subject.h" 45 #include "disk_file.h" 46 #include "disk_layout.h" 47 #include "disk_part.h" 48 #include "store_boot.h" 49 50 static int usage(void) 51 { 52 fprintf(stderr, "usage: ck_cont_tool respond KEY CHALLENGE | inject-peer IMG BS | corrupt-root IMG BS |\n" 53 " subject-dump IMG BS [HEAD_OUT] | subject-plant IMG BS fork | subject-plant IMG BS foreign SRC_IMG |\n" 54 " corrupt-subject IMG BS\n"); 55 return 2; 56 } 57 58 static int unhex(const char *s, uint8_t out[32]) 59 { 60 if (strlen(s) != 64) return -1; 61 for (int i = 0; i < 32; i++) { 62 unsigned v; 63 char t[3] = {s[2 * i], s[2 * i + 1], 0}; 64 char *end; 65 v = (unsigned)strtoul(t, &end, 16); 66 if (*end) return -1; 67 out[i] = (uint8_t)v; 68 } 69 return 0; 70 } 71 72 typedef struct { 73 disk_file f; 74 disk_dev raw; 75 ck_part part; 76 const disk_dev *d; 77 st_disk sd; 78 st_dev sdev; 79 ts_device tdev; 80 uint64_t base_lba, units; 81 uint32_t bpu; 82 } image; 83 84 static int img_open(image *im, const char *path, const char *bs_s) 85 { 86 uint32_t bs = (uint32_t)strtoul(bs_s, NULL, 10); 87 if (bs != 512 && bs != 4096) { 88 fprintf(stderr, "block size must be 512 or 4096\n"); 89 return -1; 90 } 91 memset(im, 0, sizeof *im); 92 if (disk_file_open(&im->f, &im->raw, path, bs, 0, 0)) { 93 fprintf(stderr, "cannot open %s\n", path); 94 return -1; 95 } 96 ck_gpt_info gi; 97 int grc = ck_gpt_find_aienos(&im->raw, &im->part, &gi); 98 if (grc) { 99 fprintf(stderr, "%s: no AIENOS partition (gpt: %s, rc=%d)\n", path, ck_gpt_strerror(grc), grc); 100 disk_file_close(&im->f); 101 return -1; 102 } 103 im->d = &im->part.dev; 104 im->bpu = CK_LAYOUT_UNIT / bs; 105 uint64_t units = im->d->block_count / im->bpu; 106 im->base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * im->bpu; 107 im->units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; 108 return 0; 109 } 110 111 static int img_mount(image *im, ss_store *s, ss_workspace *ws) 112 { 113 ss_keys keys; 114 ck_store_test_keys(&keys); 115 int rc = st_disk_bind(&im->sd, im->d, im->base_lba, im->units, &im->sdev); 116 if (!rc) { 117 ss_ts_device(im->d, &im->tdev); 118 memset(s, 0, sizeof *s); 119 rc = ss_open(s, &im->sdev, &im->tdev, 0, &keys, ws); 120 } 121 memset(&keys, 0, sizeof keys); 122 if (rc) fprintf(stderr, "Store refused: rc=%d\n", rc); 123 return rc; 124 } 125 126 static int cmd_respond(int argc, char **argv) 127 { 128 uint8_t key[32], ch[32], out[32]; 129 if (argc != 4 || unhex(argv[2], key) || unhex(argv[3], ch)) return usage(); 130 cr_operator_response(key, ch, out); 131 for (int i = 0; i < 32; i++) printf("%02x", out[i]); 132 printf("\n"); 133 return 0; 134 } 135 136 static int cmd_inject_peer(int argc, char **argv) 137 { 138 if (argc != 4) return usage(); 139 image im; 140 if (img_open(&im, argv[2], argv[3])) return 1; 141 ss_workspace *ws = calloc(1, sizeof *ws); 142 ss_store *s = calloc(1, sizeof *s); 143 int rc = !ws || !s ? -1 : img_mount(&im, s, ws); 144 if (!rc) { 145 uint32_t active = st_active_slot(&s->st); 146 uint8_t *junk = malloc(CK_LAYOUT_UNIT); 147 for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) junk[i] = (uint8_t)(i * 37u + 11u); 148 uint64_t lba = im.base_lba + (uint64_t)(1u - active) * im.bpu; 149 rc = disk_write(im.d, lba, im.bpu, junk); 150 if (!rc) rc = disk_flush(im.d); 151 free(junk); 152 if (!rc) printf("inject-peer: slot %u of the Store region filled (active slot %u)\n", 1u - active, active); 153 } 154 free(s); 155 free(ws); 156 disk_file_close(&im.f); 157 return rc ? 1 : 0; 158 } 159 160 static int cmd_corrupt_root(int argc, char **argv) 161 { 162 if (argc != 4) return usage(); 163 image im; 164 if (img_open(&im, argv[2], argv[3])) return 1; 165 ss_workspace *ws = calloc(1, sizeof *ws); 166 ss_store *s = calloc(1, sizeof *s); 167 int rc = !ws || !s ? -1 : img_mount(&im, s, ws), done = 0; 168 for (uint32_t k = 0; !rc && !done && k < s->nclaims; k++) { 169 if (ws->claims[k].c.obj.object_kind != 16u) continue; /* AgentRoot */ 170 uint32_t n = 0; 171 const sv1_entry *cat = st_catalog(&s->st, &n); 172 for (uint32_t j = 0; j < n; j++) { 173 if (memcmp(cat[j].object_id, ws->claims[k].sid, 32)) continue; 174 uint8_t *u = malloc(CK_LAYOUT_UNIT); 175 uint64_t lba = im.base_lba + cat[j].first_unit * im.bpu; 176 rc = disk_read(im.d, lba, im.bpu, u); 177 if (!rc) { 178 u[20] ^= 0xff; 179 rc = disk_write(im.d, lba, im.bpu, u); 180 } 181 if (!rc) rc = disk_flush(im.d); 182 free(u); 183 done = 1; 184 if (!rc) printf("corrupt-root: byte 20 of the AgentRoot envelope (store unit %llu) flipped\n", (unsigned long long)cat[j].first_unit); 185 break; 186 } 187 } 188 if (!rc && !done) { 189 fprintf(stderr, "no AgentRoot envelope found\n"); 190 rc = -1; 191 } 192 free(s); 193 free(ws); 194 disk_file_close(&im.f); 195 return rc ? 1 : 0; 196 } 197 198 /* ---- ALLEN subject (kind 24; ARCH-0035 / OS-0018, PROPOSED) ---- 199 * subject-dump reads the Store of a boot-disk image the way the kernel does 200 * (sealed mount, cr_resolve, cs_resolve) and prints what is ON THE DISK, 201 * independent of the guest's serial output. It writes nothing; the gate runs 202 * it on a copy. subject-plant and corrupt-subject make TEST fixtures. */ 203 static void hexp(const uint8_t *b, size_t n) 204 { 205 for (size_t i = 0; i < n; i++) printf("%02x", b[i]); 206 } 207 208 struct subj_ctx { 209 image im; 210 ss_workspace *ws; 211 ss_store *s; 212 struct cr_source src; 213 struct cr_work w; 214 struct cr_view v; 215 struct cs_subject subj; 216 uint8_t id[32]; 217 }; 218 219 /* Mount, resolve identity, resolve subject. Returns the cr_resolve outcome 220 * in *io and the cs_resolve outcome in *so (-1 if not reached); 0 or -1 when 221 * the image could not be mounted. */ 222 static int subj_open(struct subj_ctx *c, const char *path, const char *bs, int *io, int *so, const char **why) 223 { 224 int src_rc = 0; 225 *io = *so = -1; 226 if (img_open(&c->im, path, bs)) return -1; 227 c->ws = calloc(1, sizeof *c->ws); 228 c->s = calloc(1, sizeof *c->s); 229 if (!c->ws || !c->s || img_mount(&c->im, c->s, c->ws)) return -1; 230 cr_bind_sealed(&c->src, c->s); 231 *io = cr_resolve(&c->src, &c->w, &c->v, why, &src_rc); 232 if (*io != CR_RESOLVED) return 0; 233 *so = cs_resolve(&c->src, &c->w, &c->v, &c->subj, c->id, why, &src_rc); 234 return 0; 235 } 236 static void subj_close(struct subj_ctx *c) 237 { 238 free(c->s); 239 free(c->ws); 240 disk_file_close(&c->im.f); 241 } 242 243 static int cmd_subject_dump(int argc, char **argv) 244 { 245 if (argc != 4 && argc != 5) return usage(); 246 static struct subj_ctx c; 247 const char *why = NULL; 248 int io, so; 249 if (subj_open(&c, argv[2], argv[3], &io, &so, &why)) { 250 printf("subject-dump: store=Refused\n"); 251 return 1; 252 } 253 uint32_t n = c.src.count(c.src.ctx), objs = 0, genesis = 0; 254 for (uint32_t i = 0; i < n; i++) { 255 uint16_t kind = 0, ver = 0; 256 size_t len = 0; 257 if (c.src.entry(c.src.ctx, i, &kind, &ver) || kind != CC_KIND_SUBJECT) continue; 258 objs++; 259 static struct cs_subject t; 260 if (c.src.read(c.src.ctx, i, c.w.buf, sizeof c.w.buf, &len) == 0 && 261 cs_subject_decode(c.w.buf, len, &t, NULL) == CC_OK && t.sequence == 1) 262 genesis++; 263 } 264 printf("subject-dump: generation=%llu identity=%s", (unsigned long long)ss_generation(c.s), cr_outcome_name(io)); 265 if (io == CR_RESOLVED) { 266 printf(" agent="); 267 hexp(c.v.root.agent_id, 32); 268 } 269 printf(" subject_objects=%u genesis_objects=%u", objs, genesis); 270 if (so == CS_RESOLVED) { 271 uint32_t active = 0; 272 for (uint32_t i = 0; i < c.subj.n_intents; i++) active += c.subj.in[i].state == CS_ACTIVE; 273 printf(" subject="); 274 hexp(c.id, 32); 275 printf(" sequence=%llu lineage=", (unsigned long long)c.subj.sequence); 276 hexp(c.subj.cortex, 32); 277 printf(" active=%u\n", active); 278 for (uint32_t i = 0; i < c.subj.n_intents; i++) { 279 const struct cs_intent *a = &c.subj.in[i]; 280 if (a->state != CS_ACTIVE) continue; 281 printf("subject-dump: intent id="); 282 hexp(a->id, 32); 283 printf(" kind=%u regime=%llu target_ns=%llu since=%llu\n", (unsigned)a->kind, 284 (unsigned long long)a->payload[0], (unsigned long long)a->payload[1], (unsigned long long)a->since); 285 } 286 if (argc == 5) { /* the head object, canonical bytes, for omega's allen tool */ 287 static uint8_t b[CS_MAX_BYTES]; 288 size_t len = 0; 289 uint8_t id2[32]; 290 FILE *f = NULL; 291 if (cs_subject_encode(&c.subj, b, sizeof b, &len, &why) != CC_OK || cs_subject_id(b, len, id2) != CC_OK || 292 memcmp(id2, c.id, 32) || !(f = fopen(argv[4], "wb")) || fwrite(b, 1, len, f) != len) { 293 if (f) fclose(f); 294 fprintf(stderr, "subject-dump: cannot write the head object\n"); 295 subj_close(&c); 296 return 1; 297 } 298 fclose(f); 299 } 300 } else if (so == CS_ABSENT) { 301 printf(" subject=ABSENT\n"); 302 } else if (so >= 0) { 303 printf(" subject=%s (%s)\n", cr_outcome_name(so), why ? why : "-"); 304 } else { 305 printf("\n"); 306 } 307 subj_close(&c); 308 return 0; 309 } 310 311 static int plant(struct subj_ctx *c, const uint8_t *b, size_t len) 312 { 313 struct cr_sealed_sink sk = {c->s, 0, 0}; 314 struct cr_sink snk; 315 struct cr_wobj o = {CC_KIND_SUBJECT, CC_STORE_OBJECT_VERSION, b, len}; 316 cr_bind_sealed_sink(&snk, &sk); 317 return snk.transact(snk.ctx, &o, 1); 318 } 319 320 /* subject-plant IMG BS fork : a second sequence-1 subject for the same root and agent 321 * subject-plant IMG BS foreign SRC : every subject object of image SRC, verbatim */ 322 static int cmd_subject_plant(int argc, char **argv) 323 { 324 if (argc < 5) return usage(); 325 static struct subj_ctx c, f; 326 static uint8_t b[CS_MAX_BYTES]; 327 size_t len = 0; 328 const char *why = NULL; 329 int io, so, rc = 1; 330 if (subj_open(&c, argv[2], argv[3], &io, &so, &why) || io != CR_RESOLVED) { 331 fprintf(stderr, "subject-plant: target identity does not resolve\n"); 332 return 1; 333 } 334 if (!strcmp(argv[4], "fork") && argc == 5) { 335 if (so != CS_RESOLVED) { 336 fprintf(stderr, "subject-plant: target has no subject to fork\n"); 337 } else { 338 uint8_t prov[32]; 339 memset(prov, 0xf0, 32); 340 static struct cs_subject g; 341 cs_subject_genesis(&g, &c.v, prov, CS_ORIGIN_OPERATOR); 342 cs_subject_bind_cortex(&g, c.subj.cortex); 343 if (cs_subject_encode(&g, b, sizeof b, &len, &why) == CC_OK && plant(&c, b, len) == 0) { 344 printf("subject-plant: second genesis subject planted (fork)\n"); 345 rc = 0; 346 } 347 } 348 } else if (!strcmp(argv[4], "foreign") && argc == 6) { 349 int fio, fso; 350 if (subj_open(&f, argv[5], argv[3], &fio, &fso, &why) || fso != CS_RESOLVED) { 351 fprintf(stderr, "subject-plant: source image has no subject\n"); 352 } else if (!memcmp(f.v.root.agent_id, c.v.root.agent_id, 32)) { 353 fprintf(stderr, "subject-plant: source and target share an agent\n"); 354 } else { 355 /* every subject object of the source image, verbatim */ 356 uint32_t n = f.src.count(f.src.ctx), k = 0; 357 rc = 0; 358 for (uint32_t i = 0; i < n && !rc; i++) { 359 uint16_t kind = 0, ver = 0; 360 if (f.src.entry(f.src.ctx, i, &kind, &ver) || kind != CC_KIND_SUBJECT) continue; 361 if (f.src.read(f.src.ctx, i, b, sizeof b, &len) || plant(&c, b, len)) rc = 1; 362 else k++; 363 } 364 if (!rc) { 365 printf("subject-plant: %u foreign subject objects of agent ", k); 366 hexp(f.subj.agent, 32); 367 printf(" planted\n"); 368 } 369 } 370 if (f.s) subj_close(&f); 371 } else { 372 subj_close(&c); 373 return usage(); 374 } 375 subj_close(&c); 376 return rc; 377 } 378 379 /* corrupt-subject IMG BS: flip byte 20 of the first subject (kind 24) envelope. */ 380 static int cmd_corrupt_subject(int argc, char **argv) 381 { 382 if (argc != 4) return usage(); 383 image im; 384 if (img_open(&im, argv[2], argv[3])) return 1; 385 ss_workspace *ws = calloc(1, sizeof *ws); 386 ss_store *s = calloc(1, sizeof *s); 387 int rc = !ws || !s ? -1 : img_mount(&im, s, ws), done = 0; 388 for (uint32_t k = 0; !rc && !done && k < s->nclaims; k++) { 389 if (ws->claims[k].c.obj.object_kind != CC_KIND_SUBJECT) continue; 390 uint32_t n = 0; 391 const sv1_entry *cat = st_catalog(&s->st, &n); 392 for (uint32_t j = 0; j < n; j++) { 393 if (memcmp(cat[j].object_id, ws->claims[k].sid, 32)) continue; 394 uint8_t *u = malloc(CK_LAYOUT_UNIT); 395 uint64_t lba = im.base_lba + cat[j].first_unit * im.bpu; 396 rc = disk_read(im.d, lba, im.bpu, u); 397 if (!rc) { 398 u[20] ^= 0xff; 399 rc = disk_write(im.d, lba, im.bpu, u); 400 } 401 if (!rc) rc = disk_flush(im.d); 402 free(u); 403 done = 1; 404 if (!rc) printf("corrupt-subject: byte 20 of a subject envelope (store unit %llu) flipped\n", 405 (unsigned long long)cat[j].first_unit); 406 break; 407 } 408 } 409 if (!rc && !done) { 410 fprintf(stderr, "no subject envelope found\n"); 411 rc = -1; 412 } 413 free(s); 414 free(ws); 415 disk_file_close(&im.f); 416 return rc ? 1 : 0; 417 } 418 419 int main(int argc, char **argv) 420 { 421 if (argc < 2) return usage(); 422 if (!strcmp(argv[1], "respond")) return cmd_respond(argc, argv); 423 if (!strcmp(argv[1], "inject-peer")) return cmd_inject_peer(argc, argv); 424 if (!strcmp(argv[1], "corrupt-root")) return cmd_corrupt_root(argc, argv); 425 if (!strcmp(argv[1], "subject-dump")) return cmd_subject_dump(argc, argv); 426 if (!strcmp(argv[1], "subject-plant")) return cmd_subject_plant(argc, argv); 427 if (!strcmp(argv[1], "corrupt-subject")) return cmd_corrupt_subject(argc, argv); 428 return usage(); 429 }