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/svc/continuity_subject_provision.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 / svc / continuity_subject_provision.c` 170 lines ยท 6768 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 /* continuity_subject_provision.c -- see continuity_subject_provision.h. */ 18 #include "continuity_subject_provision.h" 19 20 #include "store_sealed.h" 21 #include "store_v1.h" 22 23 static int fail(const char **why, int cls, const char *w) 24 { 25 if (why) *why = w; 26 return cls; 27 } 28 29 static int map_cc(int rc) 30 { 31 return rc == CC_E_LIMIT ? CR_LIMIT : rc == CC_E_ARG ? CR_E_ARG : CR_CORRUPT; 32 } 33 34 /* The genesis sink: forwards cr_provision's one transaction with the genesis 35 * subject object appended. It accepts exactly one call carrying exactly one 36 * AgentRoot; anything else is refused before the inner sink is touched, so a 37 * provisioning write without its subject cannot reach the Store. */ 38 struct gsink { 39 const struct cr_sink *inner; 40 const struct cs_genesis *g; 41 int calls; 42 int rc; /* 0, or why the sink refused (CR_*) */ 43 const char *why; 44 struct cs_subject subj; /* the genesis object that was sent */ 45 uint8_t id[32]; 46 uint8_t buf[CS_MAX_BYTES]; 47 size_t len; 48 }; 49 50 #define GSINK_REFUSED (-7700) /* store-style error code for a refused call */ 51 52 static int gsink_transact(void *ctx, const struct cr_wobj *objs, size_t n) 53 { 54 struct gsink *k = ctx; 55 struct cr_wobj all[SS_MAX_OBJECTS]; 56 const struct cr_wobj *root = 0; 57 static struct cr_view rv; /* only root_id and root.agent_id are used */ 58 const char *y = 0; 59 int rc; 60 if (k->calls++ != 0) { 61 k->rc = fail(&k->why, CR_E_ARG, "provisioning made a second transaction"); 62 return GSINK_REFUSED; 63 } 64 if (n + 1 > SS_MAX_OBJECTS) { 65 k->rc = fail(&k->why, CR_LIMIT, "no room for the subject in the provisioning transaction"); 66 return GSINK_REFUSED; 67 } 68 for (size_t i = 0; i < n; i++) { 69 if (objs[i].kind != CC_KIND_AGENT_ROOT) continue; 70 if (root) { 71 k->rc = fail(&k->why, CR_CORRUPT, "provisioning transaction carries two agent roots"); 72 return GSINK_REFUSED; 73 } 74 root = &objs[i]; 75 } 76 if (!root) { 77 k->rc = fail(&k->why, CR_CORRUPT, "provisioning transaction carries no agent root"); 78 return GSINK_REFUSED; 79 } 80 sv1_zero(&rv, sizeof rv); 81 if ((rc = cc_root_decode(root->bytes, root->len, &rv.root, &y)) != CC_OK) { 82 k->rc = fail(&k->why, map_cc(rc), y); 83 return GSINK_REFUSED; 84 } 85 if (cc_object_id(CC_KIND_AGENT_ROOT, root->bytes, root->len, rv.root_id) != CC_OK) { 86 k->rc = fail(&k->why, CR_E_ARG, "agent root object id"); 87 return GSINK_REFUSED; 88 } 89 cs_subject_genesis(&k->subj, &rv, k->g->provenance, k->g->origin); 90 cs_subject_bind_cortex(&k->subj, k->g->cortex); 91 if ((rc = cs_subject_encode(&k->subj, k->buf, sizeof k->buf, &k->len, &y)) != CC_OK) { 92 k->rc = fail(&k->why, map_cc(rc), y); 93 return GSINK_REFUSED; 94 } 95 if (cc_object_id(CC_KIND_SUBJECT, k->buf, k->len, k->id) != CC_OK) { 96 k->rc = fail(&k->why, CR_E_ARG, "subject object id"); 97 return GSINK_REFUSED; 98 } 99 for (size_t i = 0; i < n; i++) all[i] = objs[i]; 100 all[n].kind = CC_KIND_SUBJECT; 101 all[n].version = CC_STORE_OBJECT_VERSION; 102 all[n].bytes = k->buf; 103 all[n].len = k->len; 104 #ifdef CS_MUTANT_PROVISION_SPLIT_TXN 105 /* MUTANT: identity first, subject in a second transaction (a crash 106 * between them leaves an identity without its subject). */ 107 if ((rc = k->inner->transact(k->inner->ctx, all, n))) return rc; 108 return k->inner->transact(k->inner->ctx, all + n, 1); 109 #else 110 return k->inner->transact(k->inner->ctx, all, n + 1); 111 #endif 112 } 113 114 int cs_provision(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w, 115 struct cr_txwork *tw, struct ck_rng *rng, const uint8_t store_uuid[16], 116 uint8_t source, const struct cs_genesis *g, struct cr_view *out, 117 struct cs_subject *subj, uint8_t subj_id[32], const char **why, int *store_rc) 118 { 119 static struct gsink k; 120 struct cr_sink wrap; 121 uint32_t n, roots = 0, subjects = 0; 122 int rc; 123 if (why) *why = 0; 124 if (store_rc) *store_rc = 0; 125 if (!src || !src->count || !src->entry || !snk || !snk->transact || !w || !tw || !rng || 126 !store_uuid || !g || !out || !subj || !subj_id) 127 return fail(why, CR_E_ARG, "null argument"); 128 if (sv1_all_zero(g->provenance, 32)) 129 return fail(why, CR_E_ARG, "subject genesis needs a provenance"); 130 if (sv1_all_zero(g->cortex, 32)) 131 return fail(why, CR_E_ARG, "subject genesis needs a memory lineage reference"); 132 if (g->origin != CS_ORIGIN_OPERATOR && g->origin != CS_ORIGIN_PROMOTED) 133 return fail(why, CR_E_ARG, "subject genesis origin"); 134 135 /* A subject object without an agent root is never provisioned over. */ 136 n = src->count(src->ctx); 137 for (uint32_t i = 0; i < n; i++) { 138 uint16_t kind = 0, version = 0; 139 if ((rc = src->entry(src->ctx, i, &kind, &version))) { 140 if (store_rc) *store_rc = rc; 141 return fail(why, CR_STORE, "store entry failed"); 142 } 143 roots += kind == CC_KIND_AGENT_ROOT; 144 subjects += kind == CC_KIND_SUBJECT; 145 } 146 if (subjects && !roots) 147 return fail(why, CR_CORRUPT, "subject objects without an agent root"); 148 149 sv1_zero(&k, sizeof k); 150 k.inner = snk; 151 k.g = g; 152 wrap.ctx = &k; 153 wrap.transact = gsink_transact; 154 rc = cr_provision(src, &wrap, w, tw, rng, store_uuid, source, out, why, store_rc); 155 if (k.rc) { /* the sink refused: nothing reached the Store */ 156 if (why) *why = k.why; 157 if (store_rc) *store_rc = 0; 158 return k.rc; 159 } 160 if (rc != CR_RESOLVED) return rc; 161 162 /* Post-condition, read back from the Store: one subject, sequence 1, 163 * this root and agent, the object that was sent. */ 164 rc = cs_resolve(src, w, out, subj, subj_id, why, store_rc); 165 if (rc == CS_ABSENT) return fail(why, CR_CORRUPT, "identity provisioned without its subject"); 166 if (rc != CS_RESOLVED) return rc; 167 if (subj->sequence != 1 || !sv1_equal(subj_id, k.id, 32)) 168 return fail(why, CR_CORRUPT, "provisioned subject is not the genesis object"); 169 return CR_RESOLVED; 170 }