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 / tools / ck_cortex_image.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 | /* ck_cortex_image -- host tool for the N3 memory gate (aienos#329, |
| 18 | * scripts/qemu_ck_memory_test.sh): writes one owner memory record into the |
| 19 | * canonical Omega Cortex journal (svc/cortex_journal.c) of the sealed Store |
| 20 | * on a boot-disk image that a TEST-keys service kernel has already |
| 21 | * provisioned. It reuses the kernel's own code paths (store_boot.c opens the |
| 22 | * Store as a boot would, cortex_journal.c + rx_cortex append the record, |
| 23 | * core/context.c encodes it), so there is no second disk or record format. |
| 24 | * TEST keys only; hosted C test tooling, never part of an image. |
| 25 | * |
| 26 | * ck_cortex_image foreign IMG BS AGENT_HEX64 TEXT |
| 27 | * append a v1 preference (permitted for context) bound to AGENT (a |
| 28 | * foreign identity: not the agent the disk was provisioned with) |
| 29 | * ck_cortex_image corrupt IMG BS TEXT |
| 30 | * append a legacy fact whose journal bytes have one byte flipped AFTER |
| 31 | * Cortex computed the record digest: replay must refuse the journal |
| 32 | * (the tool re-opens it and prints the refusal it gets) |
| 33 | * Both refuse an image whose Store is blank (the kernel must have booted it |
| 34 | * first, so the disk has a provisioned identity). No Python, no outside |
| 35 | * libraries. */ |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include "context.h" |
| 40 | #include "cortex_journal.h" |
| 41 | #include "disk.h" |
| 42 | #include "disk_file.h" |
| 43 | #include "disk_part.h" |
| 44 | #include "store_boot.h" |
| 45 | |
| 46 | #define SUBJECTS 2u /* = CK_CX_SUBJECTS of core/lifecycle.c */ |
| 47 | #define HEAP_BYTES (4u << 20) |
| 48 | |
| 49 | static int usage(void) |
| 50 | { |
| 51 | fprintf(stderr, "usage: ck_cortex_image foreign IMG BS AGENT_HEX64 TEXT | corrupt IMG BS TEXT\n"); |
| 52 | return 2; |
| 53 | } |
| 54 | |
| 55 | static int g_flip; /* 1: the next journal append flips one byte */ |
| 56 | static int flip_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) |
| 57 | { |
| 58 | static uint8_t c[CK_CJ_MAX_RECORD]; |
| 59 | if (!g_flip || n == 0 || n > sizeof c) return ck_cx_journal_store.append(ctx, h, b, n, sync); |
| 60 | memcpy(c, b, n); |
| 61 | c[n - 9] ^= 0x01; /* inside the record, before its trailing word: the digest no longer matches */ |
| 62 | g_flip = 0; |
| 63 | return ck_cx_journal_store.append(ctx, h, c, n, sync); |
| 64 | } |
| 65 | |
| 66 | static int hexval(char c) |
| 67 | { |
| 68 | if (c >= '0' && c <= '9') return c - '0'; |
| 69 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char **argv) |
| 74 | { |
| 75 | int foreign; |
| 76 | uint8_t agent[32]; |
| 77 | const char *text; |
| 78 | if (argc == 6 && !strcmp(argv[1], "foreign")) { |
| 79 | foreign = 1; |
| 80 | if (strlen(argv[4]) != 64) return usage(); |
| 81 | for (int i = 0; i < 32; i++) { |
| 82 | int a = hexval(argv[4][2 * i]), b = hexval(argv[4][2 * i + 1]); |
| 83 | if (a < 0 || b < 0) return usage(); |
| 84 | agent[i] = (uint8_t)(a << 4 | b); |
| 85 | } |
| 86 | text = argv[5]; |
| 87 | } else if (argc == 5 && !strcmp(argv[1], "corrupt")) { |
| 88 | foreign = 0; |
| 89 | text = argv[4]; |
| 90 | } else { |
| 91 | return usage(); |
| 92 | } |
| 93 | size_t tn = strlen(text); |
| 94 | if (tn == 0 || tn > CK_CTX_TEXT_MAX) { |
| 95 | fprintf(stderr, "text must be 1..%u bytes\n", CK_CTX_TEXT_MAX); |
| 96 | return 2; |
| 97 | } |
| 98 | uint32_t bs = (uint32_t)strtoul(argv[3], NULL, 10); |
| 99 | disk_file f; |
| 100 | disk_dev raw; |
| 101 | ck_part part; |
| 102 | ck_gpt_info gi; |
| 103 | if (bs != 512 && bs != 4096) return usage(); |
| 104 | if (disk_file_open(&f, &raw, argv[2], bs, 0, 0)) { |
| 105 | fprintf(stderr, "cannot open %s\n", argv[2]); |
| 106 | return 1; |
| 107 | } |
| 108 | int rc = ck_gpt_find_aienos(&raw, &part, &gi); |
| 109 | if (rc) { |
| 110 | fprintf(stderr, "%s: no AIENOS partition (gpt: %s)\n", argv[2], ck_gpt_strerror(rc)); |
| 111 | return 1; |
| 112 | } |
| 113 | /* Refuse a blank Store BEFORE store_boot_run, which would format it: the |
| 114 | * anchor unit of a provisioned disk is never all zero. */ |
| 115 | { |
| 116 | static uint8_t blk[4096]; |
| 117 | int blank = 1; |
| 118 | if (disk_read(&part.dev, 0, 1, blk)) { |
| 119 | fprintf(stderr, "%s: cannot read the anchor\n", argv[2]); |
| 120 | return 1; |
| 121 | } |
| 122 | for (uint32_t i = 0; i < bs; i++) blank &= blk[i] == 0; |
| 123 | if (blank) { |
| 124 | fprintf(stderr, "%s: Store is blank: boot the kernel on it first (nothing written)\n", argv[2]); |
| 125 | disk_file_close(&f); |
| 126 | return 1; |
| 127 | } |
| 128 | } |
| 129 | ss_keys keys; |
| 130 | ck_store_report r; |
| 131 | ck_store_test_keys(&keys); |
| 132 | rc = store_boot_run(&part.dev, &keys, ck_store_test_uuid, "ck_cortex_image", &r); |
| 133 | memset(&keys, 0, sizeof keys); |
| 134 | if (rc || r.formatted) { |
| 135 | fprintf(stderr, "%s: %s\n", argv[2], rc ? "Store refused" : "Store was blank (formatted by this run; use a fresh image)"); |
| 136 | disk_file_close(&f); |
| 137 | return 1; |
| 138 | } |
| 139 | ss_store *s = store_boot_store(); |
| 140 | static ck_cj cj; |
| 141 | static CxStore cx; |
| 142 | CxJournalOps ops = ck_cx_journal_store; |
| 143 | void *heap = malloc(HEAP_BYTES); |
| 144 | if (!heap) return 1; |
| 145 | ops.append = flip_append; |
| 146 | ck_cx_heap_init(heap, HEAP_BYTES); |
| 147 | ck_cj_init(&cj, s); |
| 148 | rc = cx_open_with(&cx, &ops, &cj, "cortex", SUBJECTS, 0); |
| 149 | if (rc != CX_OK) { |
| 150 | fprintf(stderr, "cortex open refused rc=%d\n", rc); |
| 151 | return 1; |
| 152 | } |
| 153 | uint64_t pay[CK_MEM_MAX_WORDS], id = 0; |
| 154 | uint32_t nw; |
| 155 | memset(pay, 0, sizeof pay); |
| 156 | if (foreign) { |
| 157 | nw = ck_mem_encode(pay, CK_MEM_TYPE_PREF, CK_MEM_PERM_CONTEXT, agent, text, tn); |
| 158 | if (!nw) return usage(); |
| 159 | } else { |
| 160 | nw = (uint32_t)((tn + 7) / 8); |
| 161 | for (size_t i = 0; i < tn; i++) pay[i / 8] |= (uint64_t)(uint8_t)text[i] << (8 * (i % 8)); |
| 162 | g_flip = 1; |
| 163 | } |
| 164 | CxHeader h; |
| 165 | memset(&h, 0, sizeof h); |
| 166 | h.cls = CX_CLAIM; |
| 167 | h.kind = CX_K_CLAIM; |
| 168 | h.subject = CK_CTX_OWNER_SUBJECT; |
| 169 | h.t = cx.n + 1; |
| 170 | h.generation = ss_generation(s) + 1; |
| 171 | h.tag = tn; |
| 172 | rc = cx_append(&cx, &h, pay, nw, &id); |
| 173 | uint64_t gen = ss_generation(s); |
| 174 | cx_close(&cx); |
| 175 | if (rc != CX_OK || g_flip) { |
| 176 | fprintf(stderr, "append refused rc=%d\n", rc); |
| 177 | return 1; |
| 178 | } |
| 179 | /* read back exactly as the kernel will, from a fresh replay */ |
| 180 | ck_cx_heap_init(heap, HEAP_BYTES); |
| 181 | ck_cj_init(&cj, s); |
| 182 | int reopen = cx_open_with(&cx, &ck_cx_journal_store, &cj, "cortex", SUBJECTS, CX_OPEN_READONLY); |
| 183 | uint64_t n = reopen == CX_OK ? cx.n : 0; |
| 184 | if (reopen == CX_OK) cx_close(&cx); |
| 185 | rc = disk_flush(&part.dev); |
| 186 | disk_file_close(&f); |
| 187 | free(heap); |
| 188 | if (rc) return 1; |
| 189 | if (foreign) { |
| 190 | printf("ck_cortex_image: foreign preference id=%llu store_generation=%llu reopen=%d records=%llu agent=%.16s " |
| 191 | "(TEST keys)\n", (unsigned long long)id, (unsigned long long)gen, reopen, (unsigned long long)n, argv[4]); |
| 192 | return reopen == CX_OK ? 0 : 1; |
| 193 | } |
| 194 | printf("ck_cortex_image: corrupt record id=%llu store_generation=%llu reopen=%d (journal refused at replay) " |
| 195 | "(TEST keys)\n", (unsigned long long)id, (unsigned long long)gen, reopen); |
| 196 | return reopen != CX_OK ? 0 : 1; |
| 197 | } |