interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / capability / aienos_contain.h

328 lines · 17384 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/*
18 * aienos_contain.h -- AEGIS containment gate (ARGUS-1 lane G).
19 *
20 * Spec: native/argus/docs/ARGUS1_SPEC.md sections 0 (I1-I5) and 5.
21 *
22 * The gate sits beside the capability authority. It takes a containment
23 * request (a proposal, never authority), decides GRANT / DENY / ESCALATE, and
24 * for a granted RevokeCapability performs exactly one revoke of one ordinary,
25 * leaf capability of one subject, using a REVOKE-only executor capability it
26 * minted once at create. It never mints, widens, delegates or promotes
27 * anything else, and no request can make it.
28 *
29 * Every decision and every execution attempt yields a receipt (request
30 * digest, generation, actor, subject, a strictly increasing gate sequence
31 * number), returned to the caller and sent to the sink, so every exercise of
32 * the defensive right can be audited (I5).
33 *
34 * Invariants as enforced here:
35 * I1 Automatic GRANT only for RevokeCapability of a LIVE, leaf, non-
36 * privileged cap at exactly the requested generation, of a subject
37 * that holds no privileged cap, with neither SYNTHETIC nor SATURATION.
38 * Re-checked right before the revoke. The authority always cascades,
39 * so the gate counts the authority's REVOKE announcements during its
40 * call: anything but exactly one -> FAILED_SCOPE and the gate goes
41 * inert (every later request ESCALATE).
42 * I2 No executor exists for any type but RevokeCapability. The type
43 * AIENOS_CONTAIN_FREEZE_PRINCIPAL is a named stand-in: execute answers
44 * UNAVAILABLE; a test executor (seam) may answer, and its receipts
45 * always carry AIENOS_CREQ_SYNTHETIC.
46 * I3 No request type expresses a whole-system action. Non-revoke types are
47 * never granted without resolve. Budget: at most AIENOS_CONTAIN_BUDGET
48 * automatic grants per AIENOS_CONTAIN_BUDGET_WINDOW decisions; beyond
49 * that ESCALATE.
50 * I4 The gate is never called by aienos_cap_validate; it acts only on a
51 * submitted request. aienos_capability.c is not modified.
52 * I5 aienos_cap_mint is called once, in aienos_contain_create, for the
53 * executor (rights REVOKE only, subject AIENOS_CONTAIN_SUBJ, no parent,
54 * no lease). A second create for the same authority is refused. If the
55 * executor stops being LIVE the gate never re-mints: it escalates.
56 *
57 * No malloc and no clock: the caller supplies memory, time is the authority
58 * clock (aienos_cap_clock). Thread use: one bridge thread calls submit /
59 * resolve / execute; lineage_observe may arrive from any thread.
60 */
61#ifndef AIENOS_CONTAIN_H
62#define AIENOS_CONTAIN_H
63
64#include <stddef.h>
65#include <stdint.h>
66
67#include "aienos_capability.h"
68
69/* The gate's executor subject. Chosen by lane G: 43, next to the AEGIS
70 * subjects 41 (RX_AEGIS_SUBJ) and 42 (RX_AEGIS_ROOT_SUBJ), unused by any
71 * Omega or AIENOS subject. Protected like 41 and 42 (I5.b). */
72#define AIENOS_CONTAIN_SUBJ 43u
73#define AIENOS_CONTAIN_SUBJ_AEGIS 41u /* == RX_AEGIS_SUBJ, ARGUS_SUBJ_AEGIS */
74#define AIENOS_CONTAIN_SUBJ_AEGIS_ROOT 42u /* == RX_AEGIS_ROOT_SUBJ, ARGUS_SUBJ_AEGIS_ROOT */
75
76/* Actor in a receipt: who made the step. */
77#define AIENOS_CONTAIN_ACTOR_GATE AIENOS_CONTAIN_SUBJ
78#define AIENOS_CONTAIN_ACTOR_OFFICE 0u /* resolve with the office secret (the human) */
79
80#define AIENOS_CONTAIN_BUDGET 8u /* automatic grants per window (I3.b) */
81#define AIENOS_CONTAIN_BUDGET_WINDOW 64u /* decisions per window */
82#define AIENOS_CONTAIN_DECISIONS 32u /* decisions remembered for resolve / execute */
83#define AIENOS_CONTAIN_REGISTRY 256u /* authorities a process may create gates for */
84
85#define AIENOS_CONTAIN_REQUEST_SIZE 152u /* canonical encoding, == ARGUS_CONTAIN_REQUEST_SIZE */
86#define AIENOS_CONTAIN_REQUEST_VERSION 2u /* == ARGUS_CONTAIN_REQUEST_VERSION */
87#define AIENOS_CONTAIN_DIGEST_LEN 32u
88#define AIENOS_CONTAIN_MACHINE_ID_LEN 32u
89#define AIENOS_CONTAIN_SECRET_LEN 32u /* office secret presented to resolve */
90#define AIENOS_CONTAIN_CAP_NONE UINT32_MAX /* == ARGUS_CAP_NONE */
91
92/* Request types, == ARGUS_CONTAIN_* 1..10. Only RevokeCapability is LIVE. */
93enum {
94 AIENOS_CONTAIN_REVOKE_CAPABILITY = 1,
95 AIENOS_CONTAIN_FREEZE_PRINCIPAL = 2, /* stand-in only: no executor exists (I2) */
96 AIENOS_CONTAIN_RESTRICT_PRINCIPAL = 3,
97 AIENOS_CONTAIN_QUARANTINE_MACHINE = 4,
98 AIENOS_CONTAIN_QUARANTINE_PROVIDER = 5,
99 AIENOS_CONTAIN_REVOKE_CREDENTIAL_LEASE = 6,
100 AIENOS_CONTAIN_REJECT_ARTIFACT = 7,
101 AIENOS_CONTAIN_REQUIRE_REATTESTATION = 8,
102 AIENOS_CONTAIN_RAISE_EFFECT_CLASS = 9,
103 AIENOS_CONTAIN_PAUSE_EXTERNAL_EFFECTS = 10,
104 AIENOS_CONTAIN_TYPE_MAX = 10
105};
106
107/* Request flags, == ARGUS_CREQ_*. */
108#define AIENOS_CREQ_SYNTHETIC 0x0001u
109#define AIENOS_CREQ_SATURATION 0x0002u
110#define AIENOS_CREQ_KNOWN 0x0003u
111
112/* Receipt status, == ARGUS_CSTATUS_*: 1-3 for a decision, 4-8 for an execution. */
113enum {
114 AIENOS_CONTAIN_GRANT = 1,
115 AIENOS_CONTAIN_DENY = 2,
116 AIENOS_CONTAIN_ESCALATE = 3,
117 AIENOS_CONTAIN_DONE = 4,
118 AIENOS_CONTAIN_PARTIAL = 5,
119 AIENOS_CONTAIN_FAILED = 6,
120 AIENOS_CONTAIN_UNAVAILABLE = 7,
121 AIENOS_CONTAIN_FAILED_SCOPE = 8
122};
123
124/* Receipt kind, == ARGUS_EV_CONTAINMENT_DECIDED / _EXECUTED. */
125#define AIENOS_CONTAIN_KIND_DECIDED 91u
126#define AIENOS_CONTAIN_KIND_EXECUTED 92u
127
128/* WHY codes (spec 5.2). */
129enum {
130 AIENOS_CONTAIN_WHY_POLICY_GRANT = 1100,
131 AIENOS_CONTAIN_WHY_POLICY_DENY = 1101,
132 AIENOS_CONTAIN_WHY_POLICY_ESCALATE = 1102,
133 AIENOS_CONTAIN_WHY_PROTECTED_TARGET = 1103,
134 AIENOS_CONTAIN_WHY_TARGET_NOT_LIVE = 1104,
135 AIENOS_CONTAIN_WHY_SUBJECT_MISMATCH = 1105,
136 AIENOS_CONTAIN_WHY_REPLAYED_REQUEST = 1106,
137 AIENOS_CONTAIN_WHY_BUDGET = 1107,
138 AIENOS_CONTAIN_WHY_BAD_REQUEST = 1108,
139 AIENOS_CONTAIN_WHY_NO_EXECUTOR = 1109,
140 AIENOS_CONTAIN_WHY_HUMAN_DENY = 1110,
141 AIENOS_CONTAIN_WHY_HUMAN_GRANT = 1111,
142 AIENOS_CONTAIN_WHY_BAD_SECRET = 1112,
143 AIENOS_CONTAIN_WHY_DECISION_REPLAY = 1113,
144 AIENOS_CONTAIN_WHY_NOT_LEAF = 1114,
145 AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED = 1115,
146 AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE = 1116
147};
148
149/* Receipt gate flags (receipt.gate_flags; not request flags). */
150#define AIENOS_CONTAIN_RF_REFUSED_CALL 0x0001u /* a refused call, no state change (e.g. bad secret, second execute) */
151#define AIENOS_CONTAIN_RF_HUMAN 0x0002u /* the step came from resolve */
152#define AIENOS_CONTAIN_RF_SEAM 0x0004u /* a test seam was active */
153#define AIENOS_CONTAIN_RF_INERT 0x0008u /* the gate is inert after this step */
154
155/* Function results. A decision (even DENY) is AIENOS_CONTAIN_OK: the verdict
156 * is in the receipt. Errors mean the call itself was refused. */
157#define AIENOS_CONTAIN_OK 0
158#define AIENOS_CONTAIN_ERR_ARG (-1)
159#define AIENOS_CONTAIN_ERR_SPACE (-2)
160#define AIENOS_CONTAIN_ERR_EXISTS (-3) /* a gate already exists for this authority */
161#define AIENOS_CONTAIN_ERR_AUTHORITY (-4) /* the executor mint failed */
162#define AIENOS_CONTAIN_ERR_UNKNOWN (-5) /* no such decision */
163#define AIENOS_CONTAIN_ERR_REFUSED (-6) /* refused and announced */
164#define AIENOS_CONTAIN_ERR_FULL (-7) /* the create registry is full */
165
166/* Layout twin of ArgusContainmentRequest (native/argus/argus_abi.h). The
167 * gate never includes argus_abi.h nor links libargus; contain_test.c asserts
168 * every offsetof and the size are equal. */
169typedef struct {
170 uint64_t incident_id;
171 uint8_t containment; /* AIENOS_CONTAIN_* request type */
172 uint8_t severity; /* 1..5 (ARGUS_SEV_*) */
173 uint16_t finding_code;
174 uint32_t principal; /* target subject; 0 = not set */
175 AienosCapRef target; /* cap and generation to act on */
176 uint8_t machine_id[AIENOS_CONTAIN_MACHINE_ID_LEN];
177 uint8_t finding_digest[AIENOS_CONTAIN_DIGEST_LEN];
178 uint64_t request_id; /* strictly increasing from 1 */
179 uint64_t finding_sequence; /* never 0 */
180 uint32_t target_object;
181 uint32_t target_rights; /* RestrictPrincipal only; 0 otherwise */
182 uint8_t target_digest[AIENOS_CONTAIN_DIGEST_LEN];
183 uint16_t flags; /* AIENOS_CREQ_* */
184 uint8_t version; /* AIENOS_CONTAIN_REQUEST_VERSION */
185 uint8_t reserved; /* 0 */
186} AienosContainRequest;
187
188_Static_assert(sizeof(AienosCapRef) == 16, "AienosCapRef layout");
189_Static_assert(offsetof(AienosContainRequest, incident_id) == 0, "twin layout");
190_Static_assert(offsetof(AienosContainRequest, containment) == 8, "twin layout");
191_Static_assert(offsetof(AienosContainRequest, severity) == 9, "twin layout");
192_Static_assert(offsetof(AienosContainRequest, finding_code) == 10, "twin layout");
193_Static_assert(offsetof(AienosContainRequest, principal) == 12, "twin layout");
194_Static_assert(offsetof(AienosContainRequest, target) == 16, "twin layout");
195_Static_assert(offsetof(AienosContainRequest, machine_id) == 32, "twin layout");
196_Static_assert(offsetof(AienosContainRequest, finding_digest) == 64, "twin layout");
197_Static_assert(offsetof(AienosContainRequest, request_id) == 96, "twin layout");
198_Static_assert(offsetof(AienosContainRequest, finding_sequence) == 104, "twin layout");
199_Static_assert(offsetof(AienosContainRequest, target_object) == 112, "twin layout");
200_Static_assert(offsetof(AienosContainRequest, target_rights) == 116, "twin layout");
201_Static_assert(offsetof(AienosContainRequest, target_digest) == 120, "twin layout");
202_Static_assert(offsetof(AienosContainRequest, flags) == 152, "twin layout");
203_Static_assert(offsetof(AienosContainRequest, version) == 154, "twin layout");
204_Static_assert(offsetof(AienosContainRequest, reserved) == 155, "twin layout");
205_Static_assert(sizeof(AienosContainRequest) == 160, "twin layout");
206
207/* One receipt per decision and per execution attempt, including refused
208 * calls. seq is strictly increasing over every receipt of the gate. */
209typedef struct {
210 uint64_t seq; /* gate sequence, from 1 */
211 uint64_t decision_id; /* strictly increasing from 1; 0 if none was made */
212 uint64_t request_id; /* echo */
213 uint64_t finding_sequence; /* echo */
214 uint64_t tick; /* aienos_cap_clock at the step */
215 uint64_t resource; /* DECIDED: decision_id; EXECUTED: low32 slots revoked, high32 refused */
216 AienosCapRef target; /* echo; its generation is the one the gate checked */
217 AienosCapRef executor; /* the gate's executor ref */
218 uint32_t principal; /* subject (echo) */
219 uint32_t actor; /* AIENOS_CONTAIN_ACTOR_GATE or _OFFICE */
220 int32_t rc; /* authority rc of the revoke (EXECUTED), else 0 */
221 uint16_t why; /* AIENOS_CONTAIN_WHY_*; 0 for a successful execution */
222 uint16_t finding_code; /* echo */
223 uint16_t flags; /* request flags echo, plus AIENOS_CREQ_SYNTHETIC for a test executor */
224 uint16_t gate_flags; /* AIENOS_CONTAIN_RF_* */
225 uint8_t kind; /* AIENOS_CONTAIN_KIND_DECIDED / _EXECUTED */
226 uint8_t status; /* AIENOS_CONTAIN_GRANT .. _FAILED_SCOPE */
227 uint8_t type; /* request type echo */
228 uint8_t pad;
229 uint8_t request_digest[AIENOS_CONTAIN_DIGEST_LEN];
230} AienosContainReceipt;
231
232typedef AienosContainReceipt AienosContainDecision;
233typedef AienosContainReceipt AienosContainResult;
234
235/* Policy. decide sets *verdict to GRANT / DENY / ESCALATE and *why. It is
236 * asked only after every structural rule passed; the gate may still turn a
237 * GRANT into ESCALATE (not auto-eligible, budget). Any other verdict is
238 * treated as ESCALATE. */
239typedef struct {
240 int (*decide)(void *ctx, const AienosContainRequest *r, const AienosCapView *view,
241 uint32_t *verdict, uint32_t *why);
242 void *ctx;
243} AienosContainAuthorizer;
244
245/* Table policy for ARGUS-1: one verdict per request type (index 1..10). */
246typedef struct {
247 uint32_t verdict[AIENOS_CONTAIN_TYPE_MAX + 1];
248} AienosContainTablePolicy;
249int aienos_contain_table_decide(void *ctx, const AienosContainRequest *r,
250 const AienosCapView *view, uint32_t *verdict, uint32_t *why);
251
252/* Receipt sink (the gate observer): called after every receipt, outside the
253 * gate lock, on the calling thread. */
254typedef void (*AienosContainSink)(void *ctx, const AienosContainReceipt *receipt);
255
256typedef struct AienosContain AienosContain;
257
258size_t aienos_contain_footprint(void);
259
260/* Mints the executor (the only mint, I5.a). *g is set before the mint, so an
261 * observer that forwards to lineage_observe through *g sees the executor's
262 * MINT; the lineage index is trusted only if it saw it. The index knows only
263 * caps minted after create; any other target is ESCALATE (WHY 1115). */
264int aienos_contain_create(AienosContain **g, void *mem, size_t bytes, AienosCapAdmin *admin,
265 const AienosCapView *view, AienosCapRef office,
266 const AienosContainAuthorizer *authz);
267/* Clears the memory. Revokes nothing; the authority entry stays registered,
268 * so a second create for the same authority is refused even after destroy.
269 * Before destroy, the caller must detach the authority observer and ensure no
270 * gate calls or lineage_observe callbacks are in flight. The registry is
271 * bounded to AIENOS_CONTAIN_REGISTRY authority instances per process; failed
272 * creates release their reservation. */
273void aienos_contain_destroy(AienosContain *g);
274
275int aienos_contain_set_sink(AienosContain *g, AienosContainSink fn, void *ctx);
276int aienos_contain_executor(const AienosContain *g, AienosCapRef *out);
277int aienos_contain_inert(const AienosContain *g);
278
279int aienos_contain_submit(AienosContain *g, const AienosContainRequest *r,
280 const uint8_t request_digest[AIENOS_CONTAIN_DIGEST_LEN],
281 AienosContainDecision *out);
282/* Canonical bytes in: len must be AIENOS_CONTAIN_REQUEST_SIZE and decode
283 * clean, else DENY WHY 1108 (receipt digest = SHA-256 of the bytes given). */
284int aienos_contain_submit_bytes(AienosContain *g, const uint8_t *bytes, size_t len,
285 AienosContainDecision *out);
286/* ESCALATE -> GRANT / DENY, human only: office_secret is checked with
287 * aienos_cap_authorize. A wrong secret is refused and announced (WHY 1112),
288 * the decision stays ESCALATED. */
289int aienos_contain_resolve(AienosContain *g, uint64_t decision_id, int approve,
290 const uint8_t *office_secret, AienosContainDecision *out);
291/* Executes this gate's own GRANT, once. Only the last AIENOS_CONTAIN_DECISIONS
292 * GRANT/ESCALATE decisions are remembered: an older one returns ERR_UNKNOWN. */
293int aienos_contain_execute(AienosContain *g, uint64_t decision_id, AienosContainResult *out);
294/* Fed by the bridge with every authority observer call. */
295void aienos_contain_lineage_observe(AienosContain *g, uint32_t op, const AienosCapEntry *e,
296 int result);
297
298/* Canonical encoding (spec 2): 152 bytes, packed little-endian, field order. */
299void aienos_contain_request_encode(const AienosContainRequest *r,
300 uint8_t out[AIENOS_CONTAIN_REQUEST_SIZE]);
301/* 0 if the fields are well formed (type, severity, version, reserved, flags,
302 * request_id, finding_sequence), else AIENOS_CONTAIN_ERR_ARG. */
303int aienos_contain_request_decode(const uint8_t in[AIENOS_CONTAIN_REQUEST_SIZE],
304 AienosContainRequest *out);
305void aienos_contain_request_digest(const AienosContainRequest *r,
306 uint8_t out[AIENOS_CONTAIN_DIGEST_LEN]);
307void aienos_contain_sha256(const uint8_t *data, size_t len, uint8_t out[AIENOS_CONTAIN_DIGEST_LEN]);
308
309/* Test seams (spec lane G brief). Enabling them needs the admin handle the
310 * gate was created with plus a LIVE office ref: a holder of both can already
311 * revoke anything, so the seams add no power. Every receipt made while a seam
312 * is active carries AIENOS_CONTAIN_RF_SEAM. */
313#define AIENOS_CONTAIN_SEAM_NO_PROTECTED 0x1u /* disable the protected list (layer 2, G9) */
314typedef struct {
315 uint32_t flags;
316 /* Called after the re-check, right before aienos_cap_revoke, without the
317 * gate lock (G13 I1.b: force a cascade). */
318 void (*pre_revoke)(void *ctx);
319 void *pre_revoke_ctx;
320 /* Test executor for non-LIVE types; its DONE / PARTIAL always carries
321 * AIENOS_CREQ_SYNTHETIC (I2). Returns an AIENOS_CONTAIN_* execution status. */
322 uint32_t (*test_executor)(void *ctx, const AienosContainRequest *r);
323 void *test_executor_ctx;
324} AienosContainSeams;
325int aienos_contain_set_seams(AienosContain *g, const AienosContainSeams *s, AienosCapAdmin *admin,
326 AienosCapRef office);
327
328#endif