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 / svc / security.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 | /* security.c -- capability authority + ARGUS boot stage (see security.h). |
| 18 | * Uses native/capability and native/argus through their public headers, |
| 19 | * unmodified. Memory comes from ck_alloc. */ |
| 20 | #include "security.h" |
| 21 | #include <stddef.h> |
| 22 | #include "argus_abi.h" |
| 23 | #include "argus_contain.h" |
| 24 | #include "argus_core.h" |
| 25 | #include "bridge/argus_aegis_bridge.h" |
| 26 | #include "aienos_contain.h" |
| 27 | #include "ck.h" |
| 28 | #include "ck_compat.h" |
| 29 | #ifdef CK_HARDWARE_STAGING |
| 30 | #include "ck_owner_prov.h" /* generated: owner public material + machine id */ |
| 31 | #if !defined(CK_OWNER_PROVISIONED) || CK_OWNER_PROVISIONED != 1 |
| 32 | #error "CK_HARDWARE_STAGING needs the generated owner provisioning header (CK_OWNER_PUBKEYS=, CK_MACHINE_ID=)" |
| 33 | #endif |
| 34 | _Static_assert(sizeof ck_owner_machine_id == ARGUS_MACHINE_ID_LEN, "machine id length"); |
| 35 | #endif |
| 36 | |
| 37 | /* Rust ipc demo resource ids are irrelevant here; these are local labels. */ |
| 38 | #define SUBJ_A 1u |
| 39 | #define SUBJ_B 2u |
| 40 | #define RES_IPC 0xA1u |
| 41 | #define SUBJ_T 7u |
| 42 | #define SUBJ_U 8u |
| 43 | #define RES_T 0x1234u |
| 44 | #define RES_U 0x5678u |
| 45 | |
| 46 | typedef struct { |
| 47 | ArgusAegisBridge *bridge; |
| 48 | uint32_t mints, revokes, executor_mints; |
| 49 | } obs_tap; |
| 50 | |
| 51 | static struct { |
| 52 | AienosCapAdmin *admin; |
| 53 | AienosCapView *view; |
| 54 | AienosCapRef office; |
| 55 | void *core_mem, *contain_mem, *gate_mem, *bridge_mem; |
| 56 | ArgusCore *core; |
| 57 | ArgusContain *contain; |
| 58 | AienosContain *gate; |
| 59 | ArgusAegisBridge *bridge; |
| 60 | AienosContainTablePolicy policy; |
| 61 | AienosContainAuthorizer authz; |
| 62 | obs_tap tap; |
| 63 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; |
| 64 | uint64_t seq; |
| 65 | int up; |
| 66 | } S; |
| 67 | |
| 68 | /* Counts what the authority announces, then forwards to the bridge (the |
| 69 | * bridge's observer is what keeps ARGUS shadows and the gate lineage). */ |
| 70 | static void tap_observer(void *ctx, uint32_t op, const AienosCapEntry *e, int result) |
| 71 | { |
| 72 | obs_tap *t = ctx; |
| 73 | if (op == AIENOS_CAP_OBS_MINT && e) { |
| 74 | t->mints++; |
| 75 | if (e->subject == AIENOS_CONTAIN_SUBJ) t->executor_mints++; |
| 76 | } |
| 77 | if (op == AIENOS_CAP_OBS_REVOKE && e) t->revokes++; |
| 78 | if (t->bridge) argus_aegis_bridge_observer(t->bridge, op, e, result); |
| 79 | } |
| 80 | |
| 81 | static void zero(void *p, size_t n) |
| 82 | { |
| 83 | volatile uint8_t *b = p; |
| 84 | while (n--) *b++ = 0; |
| 85 | } |
| 86 | |
| 87 | static void ev_init(ArgusEvent *e, uint8_t cls, uint16_t kind) |
| 88 | { |
| 89 | zero(e, sizeof *e); |
| 90 | e->version = ARGUS_ABI_VERSION; |
| 91 | e->class_ = cls; |
| 92 | e->kind = kind; |
| 93 | e->flags = (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT); |
| 94 | e->sequence = ++S.seq; |
| 95 | for (uint32_t i = 0; i < ARGUS_MACHINE_ID_LEN; i++) e->machine_id[i] = S.machine[i]; |
| 96 | } |
| 97 | |
| 98 | static int bring_up(ck_sec_report *r) |
| 99 | { |
| 100 | if (aienos_cap_start(&S.admin, &S.view) != AIENOS_CAP_OK) return r->fail = "cap_start", -1; |
| 101 | if (aienos_cap_office(S.admin, &S.office) != AIENOS_CAP_OK) return r->fail = "cap_office", -1; |
| 102 | S.core_mem = ck_alloc(argus_core_footprint()); |
| 103 | S.contain_mem = ck_alloc(argus_contain_footprint()); |
| 104 | S.gate_mem = ck_alloc(aienos_contain_footprint()); |
| 105 | S.bridge_mem = ck_alloc(argus_aegis_bridge_footprint()); |
| 106 | if (!S.core_mem || !S.contain_mem || !S.gate_mem || !S.bridge_mem) return r->fail = "alloc", -1; |
| 107 | zero(S.core_mem, argus_core_footprint()); |
| 108 | zero(S.contain_mem, argus_contain_footprint()); |
| 109 | zero(S.gate_mem, aienos_contain_footprint()); |
| 110 | zero(S.bridge_mem, argus_aegis_bridge_footprint()); |
| 111 | if (argus_core_init(&S.core, S.core_mem, argus_core_footprint()) != ARGUS_OK) return r->fail = "argus_core_init", -1; |
| 112 | if (argus_contain_init(&S.contain, S.contain_mem, argus_contain_footprint()) != ARGUS_OK) |
| 113 | return r->fail = "argus_contain_init", -1; |
| 114 | #ifdef CK_HARDWARE_STAGING |
| 115 | /* this machine's ARGUS id: provisioned at build time (CK_MACHINE_ID) */ |
| 116 | for (uint32_t i = 0; i < ARGUS_MACHINE_ID_LEN; i++) S.machine[i] = ck_owner_machine_id[i]; |
| 117 | #else |
| 118 | S.machine[0] = 0xA1; /* this machine's ARGUS id: fixed label, TEST */ |
| 119 | #endif |
| 120 | if (argus_aegis_bridge_init(&S.bridge, S.bridge_mem, argus_aegis_bridge_footprint(), S.core, S.contain, &S.gate, |
| 121 | S.view, S.machine) != ARGUS_OK) |
| 122 | return r->fail = "bridge_init", -1; |
| 123 | ArgusEvent joined; |
| 124 | ev_init(&joined, ARGUS_CLASS_SECURITY, ARGUS_EV_MACHINE_JOINED); |
| 125 | joined.outcome = ARGUS_OUTCOME_OK; |
| 126 | joined.flags |= ARGUS_FLAG_SYNTHETIC; |
| 127 | joined.object_id = ARGUS_TRUST_OBSERVED; |
| 128 | joined.cap_id = ARGUS_CAP_NONE; |
| 129 | if (argus_aegis_bridge_ingest(S.bridge, &joined) != ARGUS_OK) return r->fail = "ingest machine_joined", -1; |
| 130 | /* Observer before create: the executor mint seeds both shadows. */ |
| 131 | S.tap.bridge = S.bridge; |
| 132 | if (aienos_cap_set_observer(S.admin, tap_observer, &S.tap) != AIENOS_CAP_OK) return r->fail = "set_observer", -1; |
| 133 | S.policy.verdict[AIENOS_CONTAIN_REVOKE_CAPABILITY] = AIENOS_CONTAIN_GRANT; /* all other types: 0 -> ESCALATE */ |
| 134 | S.authz.decide = aienos_contain_table_decide; |
| 135 | S.authz.ctx = &S.policy; |
| 136 | if (aienos_contain_create(&S.gate, S.gate_mem, aienos_contain_footprint(), S.admin, S.view, S.office, &S.authz) != |
| 137 | AIENOS_CONTAIN_OK) |
| 138 | return r->fail = "contain_create", -1; |
| 139 | if (argus_aegis_bridge_attach_gate(S.bridge) != ARGUS_OK) return r->fail = "attach_gate", -1; |
| 140 | S.up = 1; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int mint(uint32_t subject, uint64_t res, uint32_t rights, AienosCapRef parent, AienosCapRef auth, |
| 145 | AienosCapRef *out) |
| 146 | { |
| 147 | AienosCapMint m; |
| 148 | zero(&m, sizeof m); |
| 149 | m.issuer = 0; |
| 150 | m.subject = subject; |
| 151 | m.resource = res; |
| 152 | m.rights = rights; |
| 153 | m.lease_ticks = 0; |
| 154 | m.parent = parent; |
| 155 | m.authority = auth; |
| 156 | return aienos_cap_mint(S.admin, &m, out); |
| 157 | } |
| 158 | |
| 159 | /* Rust ipc demo semantics on the C authority. */ |
| 160 | static int caps_check(ck_sec_report *r) |
| 161 | { |
| 162 | AienosCapRef none = {AIENOS_CAP_PARENT_NONE, 0}, a, b, c; |
| 163 | AienosCapEntry e; |
| 164 | if (mint(SUBJ_A, RES_IPC, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE | AIENOS_CAP_RIGHT_DELEGATE, none, |
| 165 | S.office, &a) != AIENOS_CAP_OK) |
| 166 | return r->fail = "caps mint A", -1; |
| 167 | r->caps_granted = aienos_cap_validate(S.view, a, SUBJ_A, RES_IPC, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE, |
| 168 | &e) == AIENOS_CAP_OK; |
| 169 | if (mint(SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ, a, a, &b) != AIENOS_CAP_OK) return r->fail = "caps delegate B", -1; |
| 170 | r->caps_attenuated = |
| 171 | aienos_cap_validate(S.view, b, SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) == AIENOS_CAP_OK && |
| 172 | aienos_cap_validate(S.view, b, SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_WRITE, &e) == AIENOS_CAP_ERR_RIGHTS; |
| 173 | r->caps_amplify_denied = mint(SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_EFFECT, a, a, &c) == |
| 174 | AIENOS_CAP_ERR_AMPLIFY; |
| 175 | AienosCapRef forged = b; |
| 176 | forged.generation ^= 0x5au; /* right slot, wrong generation */ |
| 177 | AienosCapRef forged2 = {AIENOS_CAP_MAX - 1u, b.generation}; |
| 178 | r->caps_forged_denied = aienos_cap_validate(S.view, forged, SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) != 0 && |
| 179 | aienos_cap_validate(S.view, forged2, SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) != 0 && |
| 180 | aienos_cap_validate(S.view, b, SUBJ_A, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) != 0; |
| 181 | if (aienos_cap_revoke(S.admin, S.office, a) != AIENOS_CAP_OK) return r->fail = "caps revoke A", -1; |
| 182 | r->caps_revoked_denied = |
| 183 | aienos_cap_validate(S.view, b, SUBJ_B, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) == AIENOS_CAP_ERR_REVOKED && |
| 184 | aienos_cap_validate(S.view, a, SUBJ_A, RES_IPC, AIENOS_CAP_RIGHT_READ, &e) == AIENOS_CAP_ERR_REVOKED; |
| 185 | r->caps_ok = r->caps_granted && r->caps_attenuated && r->caps_amplify_denied && r->caps_forged_denied && |
| 186 | r->caps_revoked_denied; |
| 187 | if (!r->caps_ok) return r->fail = "caps semantics", -1; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* ARGUS-1 first narrow revoke (ARGUS1_SPEC I1, I5; mirrors |
| 192 | * native/argus/tests/test_argus_containment_e2e.c and adds the unrelated |
| 193 | * and authority checks). */ |
| 194 | static int argus_check(ck_sec_report *r) |
| 195 | { |
| 196 | ArgusFinding found[16]; |
| 197 | r->findings_before = (uint32_t)argus_aegis_bridge_take_findings(S.bridge, found, 16); |
| 198 | AienosCapRef exec0; |
| 199 | if (aienos_contain_executor(S.gate, &exec0) != AIENOS_CONTAIN_OK) return r->fail = "executor ref", -1; |
| 200 | uint32_t exec_mints0 = S.tap.executor_mints; |
| 201 | |
| 202 | AienosCapRef none = {AIENOS_CAP_PARENT_NONE, 0}, t, u1, u2; |
| 203 | if (mint(SUBJ_T, RES_T, AIENOS_CAP_RIGHT_READ, none, S.office, &t) || mint(SUBJ_T, RES_U, AIENOS_CAP_RIGHT_READ, none, S.office, &u1) || |
| 204 | mint(SUBJ_U, RES_T, AIENOS_CAP_RIGHT_READ, none, S.office, &u2)) |
| 205 | return r->fail = "argus mint target/unrelated", -1; |
| 206 | AienosCapEntry e; |
| 207 | if (aienos_cap_validate(S.view, t, SUBJ_T, RES_T, AIENOS_CAP_RIGHT_READ, &e) != AIENOS_CAP_OK) |
| 208 | return r->fail = "target not granted before", -1; |
| 209 | uint32_t mints0 = S.tap.mints, revokes0 = S.tap.revokes; |
| 210 | |
| 211 | /* Trigger: a successful use reported at a stale generation. */ |
| 212 | ArgusEvent use; |
| 213 | ev_init(&use, ARGUS_CLASS_AUDIT, ARGUS_EV_CAPABILITY_USED); |
| 214 | use.effect_class = ARGUS_EFFECT_NONE; |
| 215 | use.outcome = ARGUS_OUTCOME_OK; |
| 216 | use.code = AIENOS_CAP_ERR_STALE_GEN; |
| 217 | use.cap_id = t.cap_id; |
| 218 | use.cap_generation = t.generation; |
| 219 | use.principal = SUBJ_T; |
| 220 | if (argus_aegis_bridge_ingest(S.bridge, &use) != ARGUS_OK) return r->fail = "ingest trigger", -1; |
| 221 | size_t nf = argus_aegis_bridge_take_findings(S.bridge, found, 16); |
| 222 | r->trigger_findings = (uint32_t)nf; |
| 223 | if (nf != 1 || found[0].code != ARGUS_F_STALE_GENERATION || |
| 224 | found[0].containment != ARGUS_CONTAIN_REVOKE_CAPABILITY) |
| 225 | return r->fail = "trigger finding", -1; |
| 226 | ArgusContainmentRequest req[1]; |
| 227 | ArgusEvent proposed[1]; |
| 228 | size_t np = 0; |
| 229 | if (argus_contain_propose(S.contain, S.core, found, nf, &use, req, 1, proposed, 1, &np) != ARGUS_OK || np != 1) |
| 230 | return r->fail = "propose", -1; |
| 231 | if (argus_aegis_bridge_ingest(S.bridge, &proposed[0]) != ARGUS_OK) return r->fail = "ingest proposal", -1; |
| 232 | AienosContainDecision dec; |
| 233 | AienosContainResult res; |
| 234 | zero(&dec, sizeof dec); |
| 235 | zero(&res, sizeof res); |
| 236 | if (argus_aegis_bridge_submit(S.bridge, &req[0], &dec, &res) != ARGUS_OK) return r->fail = "submit", -1; |
| 237 | r->decision = (int)dec.status; |
| 238 | r->execution = (int)res.status; |
| 239 | r->revokes_during = S.tap.revokes - revokes0; |
| 240 | r->mints_during = S.tap.mints - mints0; |
| 241 | r->executor_mints = S.tap.executor_mints - exec_mints0; |
| 242 | |
| 243 | r->target_denied_code = aienos_cap_validate(S.view, t, SUBJ_T, RES_T, AIENOS_CAP_RIGHT_READ, &e); |
| 244 | r->unrelated_same_subject = aienos_cap_validate(S.view, u1, SUBJ_T, RES_U, AIENOS_CAP_RIGHT_READ, &e); |
| 245 | r->unrelated_other_subject = aienos_cap_validate(S.view, u2, SUBJ_U, RES_T, AIENOS_CAP_RIGHT_READ, &e); |
| 246 | AienosCapRef exec1; |
| 247 | AienosCapEntry ex; |
| 248 | r->executor_unchanged = aienos_contain_executor(S.gate, &exec1) == AIENOS_CONTAIN_OK && |
| 249 | exec1.cap_id == exec0.cap_id && exec1.generation == exec0.generation && |
| 250 | aienos_cap_inspect(S.view, exec0, &ex) == AIENOS_CAP_OK && |
| 251 | ex.state == AIENOS_CAP_STATE_LIVE && ex.rights == AIENOS_CAP_RIGHT_REVOKE && |
| 252 | ex.subject == AIENOS_CONTAIN_SUBJ && r->executor_mints == 0 && r->mints_during == 0; |
| 253 | ArgusContainHealth h; |
| 254 | argus_contain_health(S.contain, &h); |
| 255 | r->health_ok = h.requested == 1 && h.granted == 1 && h.confirmed == 1 && h.failed_scope == 0 && |
| 256 | h.executions_unauthorized == 0; |
| 257 | ArgusCoreHealth ch; |
| 258 | argus_core_health(S.core, &ch); |
| 259 | r->bridge_ok = argus_aegis_bridge_error(S.bridge) == ARGUS_OK && ch.events_rejected == 0; |
| 260 | r->argus_ok = r->decision == AIENOS_CONTAIN_GRANT && r->execution == AIENOS_CONTAIN_DONE && |
| 261 | r->revokes_during == 1 && r->target_denied_code == AIENOS_CAP_ERR_REVOKED && |
| 262 | r->unrelated_same_subject == AIENOS_CAP_OK && r->unrelated_other_subject == AIENOS_CAP_OK && |
| 263 | r->executor_unchanged && r->health_ok && r->bridge_ok; |
| 264 | if (!r->argus_ok) return r->fail = "argus invariant", -1; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int ck_security_run(ck_sec_report *r) |
| 269 | { |
| 270 | zero(r, sizeof *r); |
| 271 | if (S.up) return r->fail = "already up", -1; |
| 272 | zero(&S, sizeof S); |
| 273 | if (bring_up(r)) return -1; |
| 274 | int rc = caps_check(r); |
| 275 | if (rc) return rc; |
| 276 | return argus_check(r); |
| 277 | } |
| 278 | |
| 279 | void ck_security_shutdown(void) |
| 280 | { |
| 281 | if (S.admin) aienos_cap_set_observer(S.admin, 0, 0); |
| 282 | if (S.gate) aienos_contain_destroy(S.gate); |
| 283 | if (S.bridge) argus_aegis_bridge_destroy(S.bridge); |
| 284 | if (S.admin) aienos_cap_stop(S.admin, S.view); |
| 285 | if (S.bridge_mem) ck_free(S.bridge_mem); |
| 286 | if (S.gate_mem) ck_free(S.gate_mem); |
| 287 | if (S.contain_mem) ck_free(S.contain_mem); |
| 288 | if (S.core_mem) ck_free(S.core_mem); |
| 289 | zero(&S, sizeof S); |
| 290 | } |
| 291 | |
| 292 | static const char *pick(int v, const char *y, const char *n) { return v ? y : n; } |
| 293 | |
| 294 | int ck_stage_security(void) |
| 295 | { |
| 296 | ck_sec_report r; |
| 297 | int rc = ck_security_run(&r); |
| 298 | int ent = ck_compat_entropy_source(); |
| 299 | #ifndef CK_HARDWARE_STAGING |
| 300 | ck_printf("argus: TEST machine id 0xA1 (fixed label, not a real machine identity, not production)\n"); |
| 301 | #endif |
| 302 | ck_printf("caps: %s granted=%s attenuated=%s amplify=%s forged=%s revoked=%s office_token=%s\n", |
| 303 | pick(r.caps_ok, "ok", "failed"), pick(r.caps_granted, "yes", "no"), pick(r.caps_attenuated, "yes", "no"), |
| 304 | pick(r.caps_amplify_denied, "denied", "ALLOWED"), pick(r.caps_forged_denied, "denied", "ALLOWED"), |
| 305 | pick(r.caps_revoked_denied, "denied", "ALLOWED"), |
| 306 | ent == CK_ENTROPY_RNDR ? "rndr" : ent == CK_ENTROPY_HOST ? "host-urandom" : "none"); |
| 307 | if (ck_entropy_status() != 0) |
| 308 | ck_printf("security: REFUSED kernel entropy %s (office token needs RNDR); capability office and ARGUS fail closed\n", |
| 309 | ck_entropy_reason()); |
| 310 | if (r.argus_ok) { |
| 311 | ck_printf("argus: ok narrow_revoke=1 revoked=denied unrelated=granted authority=unchanged\n"); |
| 312 | } else { |
| 313 | ck_printf("argus: FAIL step=%s narrow_revoke=%u decision=%d execution=%d trigger_findings=%u " |
| 314 | "revoked_rc=%d unrelated=%d/%d authority=%s mints=%u health=%s bridge=%s\n", |
| 315 | r.fail ? r.fail : "?", r.revokes_during, r.decision, r.execution, r.trigger_findings, |
| 316 | r.target_denied_code, r.unrelated_same_subject, r.unrelated_other_subject, |
| 317 | pick(r.executor_unchanged, "unchanged", "CHANGED"), r.mints_during, pick(r.health_ok, "ok", "bad"), |
| 318 | pick(r.bridge_ok, "ok", "error")); |
| 319 | } |
| 320 | return rc; |
| 321 | } |