interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / runtime / rx_aegis.c

330 lines · 13623 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 * rx_aegis.c -- AEGIS resident authority (ADR 0016 ยง41, R8).
19 *
20 * See rx_aegis.h. aegis.decide.k is a reaction body with no side effect
21 * outside the world. root.install.k mints and revokes through the AIENOS
22 * root; it keeps a per-slot record of what it minted for which decision, so
23 * a run that is invalidated and repeated never mints twice.
24 */
25#include "rx_aegis.h"
26#include "rx_argus.h"
27#include "omega_native.h"
28
29#include <string.h>
30
31/* ARGUS tick source: the native view when the world is bound to it. */
32static inline const AienosCapView *aegis_view(const RxAegisFaculty *f) {
33 return f->w && f->w->auth_validate ? (const AienosCapView *)f->w->auth_ctx : NULL;
34}
35
36static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) {
37 for (uint32_t i = 0; i < c->n_in; i++)
38 if (c->in[i].obj.id == r.id) return &c->in[i];
39 return NULL;
40}
41
42static void put(RxCtx *c, RxObjRef o, uint32_t field, uint64_t v) {
43 c->out[c->n_out++] = (RxMutation){ o, field, v };
44}
45
46/* ---- policy (pure OM-2 in seed/authority/aegis_om2.om) ---- */
47
48static void pack_rules(const RxAegisPolicy *p, uint64_t out[112]) {
49 memset(out, 0, 112 * sizeof(uint64_t));
50 uint32_t n = p->n_rules < RX_AEGIS_RULES ? p->n_rules : RX_AEGIS_RULES;
51 for (uint32_t i = 0; i < n; i++) {
52 const RxAegisRule *r = &p->rules[i];
53 out[i * 7 + 0] = r->id;
54 out[i * 7 + 1] = r->subject;
55 out[i * 7 + 2] = r->res_lo;
56 out[i * 7 + 3] = r->res_hi;
57 out[i * 7 + 4] = r->rights;
58 out[i * 7 + 5] = r->max_lease;
59 out[i * 7 + 6] = r->needs_approval;
60 }
61}
62
63uint32_t rx_aegis_evaluate(const RxAegisPolicy *p, uint32_t subject, uint64_t resource,
64 uint32_t rights, uint64_t lease, uint64_t *out_lease,
65 uint32_t *rule_or_why, uint32_t *needs_approval) {
66 uint64_t rules[112];
67 pack_rules(p, rules);
68 uint64_t req[4] = { subject, resource, rights, lease };
69 uint64_t res[3] = { 0, 0, 0 }, verdict = 0;
70 (void)omega2_aegis_evaluate(&verdict, rules, 112, p->n_rules, req, 4, res, 3);
71 *out_lease = res[0];
72 *rule_or_why = (uint32_t)res[1];
73 *needs_approval = (uint32_t)res[2];
74 return (uint32_t)verdict;
75}
76
77/* ---- aegis.decide.k ---- */
78
79static int fn_decide(RxCtx *c) {
80 struct { RxAegisFaculty *f; uint32_t k; } *x = c->user;
81 RxAegisFaculty *f = x->f;
82 uint32_t k = x->k;
83 const RxAegisClientObjects *o = &f->o[k];
84 const RxSnapshotDep *req = in_of(c, o->request);
85 const RxSnapshotDep *ap = in_of(c, o->approval);
86 const RxSnapshotDep *dec = in_of(c, o->decision);
87 if (!req || !ap || !dec) return -1;
88 uint64_t rules[112];
89 pack_rules(&f->policy, rules);
90 uint64_t in_f[10] = {
91 req->field[0], req->field[1], req->field[2], req->field[3], req->field[4], req->field[5],
92 ap->field[0], ap->field[1], dec->field[0], dec->field[1]
93 };
94 uint64_t out_dec[8] = { 0 }, emit = 0;
95 (void)omega2_aegis_decide(&emit, rules, 112, f->policy.n_rules, f->client[k].subject,
96 in_f, 10, out_dec, 8);
97 if (!emit) return 0;
98 if (out_dec[1] == RX_AEGIS_DENY)
99 RX_ARGUS_EMIT(rx_argus_emit_cap_denied(aegis_view(f), f->client[k].subject, ARGUS_CAP_NONE, 0,
100 out_dec[2], (uint32_t)req->field[2], (int)out_dec[6]));
101 for (uint32_t i = 0; i < 8; i++) put(c, o->decision, i, out_dec[i]);
102 return 0;
103}
104
105/* ---- root.install.k ---- */
106
107static int written_by(RxAegisFaculty *f, const RxSnapshotDep *d, uint32_t n_fields,
108 uint32_t reaction, uint32_t subject, uint32_t faculty, int any_reaction) {
109 for (uint32_t i = 0; i < n_fields; i++) {
110 uint32_t r, s, fa;
111 int rc = rx_world_crumb_origin(f->w, d->field_writer[i], &r, &s, &fa);
112 if (rc == 1) continue; /* never written since creation */
113 if (rc != RX_OK) return 0;
114 if (r == UINT32_MAX || s != subject) return 0;
115 if (!any_reaction && (r != reaction || fa != faculty)) return 0;
116 }
117 return 1;
118}
119
120static void refuse(RxCtx *c, RxObjRef slot, uint64_t seq, uint64_t why) {
121#if RX_ARGUS
122 /* ARGUS: every root refusal is a denial of the client. */
123 struct { RxAegisFaculty *f; uint32_t k; } *x = c->user;
124 rx_argus_emit_cap_denied(aegis_view(x->f), x->f->client[x->k].subject, ARGUS_CAP_NONE, 0, 0, 0,
125 (int)why);
126#endif
127 put(c, slot, 5, seq);
128 put(c, slot, 7, why);
129}
130
131/* Revoke the reference the root itself minted for this slot. The slot's
132 * contents are never trusted for this: a hostile writer could put another
133 * principal's reference there. Caller holds f->mu. */
134static void revoke_minted(RxAegisFaculty *f, uint32_t k, uint64_t j, AienosCapRef office) {
135 RxCapRef r = f->minted[k][j].ref;
136 if (r.cap_id == UINT32_MAX) return;
137 int rc = aienos_cap_revoke(f->admin, office, (AienosCapRef){ r.cap_id, r.generation });
138 if (rc == 0) f->revokes++;
139 RX_ARGUS_EMIT_AUTH(rx_argus_emit_cap_revoked(aegis_view(f), f->client[k].subject, r.cap_id,
140 (uint64_t)r.generation, rc));
141 f->minted[k][j].ref = (RxCapRef){ UINT32_MAX, 0 };
142}
143
144static int fn_install(RxCtx *c) {
145 struct { RxAegisFaculty *f; uint32_t k; } *x = c->user;
146 RxAegisFaculty *f = x->f;
147 uint32_t k = x->k;
148 const RxAegisClientObjects *o = &f->o[k];
149 const RxAegisClient *cl = &f->client[k];
150 const RxSnapshotDep *dec = in_of(c, o->decision);
151 const RxSnapshotDep *req = in_of(c, o->request);
152 if (!dec || !req) return -1;
153 uint64_t seq = dec->field[0], verdict = dec->field[1], j = dec->field[5];
154 if (seq == 0 || (verdict != RX_AEGIS_GRANT && verdict != RX_AEGIS_REVOKE)) return 0;
155 if (j >= RX_AEGIS_SLOTS) return 0;
156 const RxSnapshotDep *s = in_of(c, o->slot[j]);
157 if (!s) return -1;
158 if (s->field[5] == seq) return 0; /* already answered */
159
160 if (!written_by(f, dec, RX_MAX_FIELDS, f->r_decide[k], RX_AEGIS_SUBJ, RX_FACULTY_AEGIS, 0)) {
161 refuse(c, o->slot[j], seq, RX_AEGIS_WHY_DECISION_ORIGIN);
162 return 0;
163 }
164 if (!written_by(f, req, 6, 0, cl->subject, 0, 1)) {
165 refuse(c, o->slot[j], seq, RX_AEGIS_WHY_REQUEST_ORIGIN);
166 return 0;
167 }
168 uint64_t client_vec[3] = { cl->domain_lo, cl->domain_hi, cl->max_rights };
169 uint64_t chk = 0;
170 (void)omega2_aegis_install_check(&chk, dec->field, 8, req->field, 6, client_vec, 3);
171 if (chk != 0) {
172 refuse(c, o->slot[j], seq, chk);
173 return 0;
174 }
175
176 AienosCapRef office;
177 aienos_cap_office(f->admin, &office);
178 uint64_t res = dec->field[2];
179 uint32_t rights = (uint32_t)dec->field[3];
180
181 if (verdict == RX_AEGIS_REVOKE) {
182 pthread_mutex_lock(&f->mu);
183 if (f->minted[k][j].seq != seq) {
184 revoke_minted(f, k, j, office);
185 f->minted[k][j].seq = seq;
186 }
187 pthread_mutex_unlock(&f->mu);
188 put(c, o->slot[j], 2, RX_AEGIS_SLOT_REVOKED);
189 put(c, o->slot[j], 5, seq);
190 put(c, o->slot[j], 7, 0);
191 return 0;
192 }
193
194 RxCapRef ref;
195 int ok = 1;
196 pthread_mutex_lock(&f->mu);
197 if (f->minted[k][j].seq == seq) {
198 ref = f->minted[k][j].ref;
199 } else {
200 revoke_minted(f, k, j, office); /* renewal replaces the old grant */
201 AienosCapMint m = { RX_AEGIS_SUBJ, cl->subject, res, rights, dec->field[4],
202 { UINT32_MAX, 0 }, office };
203 AienosCapRef got = { UINT32_MAX, 0 };
204 if (aienos_cap_mint(f->admin, &m, &got) == 0) {
205 ref = (RxCapRef){ got.cap_id, got.generation };
206 f->minted[k][j] = (RxAegisMinted){ seq, ref };
207 f->mints++;
208 RX_ARGUS_EMIT_AUTH(rx_argus_emit_cap_granted(aegis_view(f), cl->subject, got.cap_id,
209 (uint64_t)got.generation, res, rights, 0));
210 } else {
211 ok = 0;
212 ref = (RxCapRef){ UINT32_MAX, 0 };
213 }
214 }
215 pthread_mutex_unlock(&f->mu);
216 if (!ok) {
217 refuse(c, o->slot[j], seq, RX_AEGIS_WHY_MINT);
218 return 0;
219 }
220 RxCapEntry e;
221 uint64_t expiry = rx_world_inspect_cap(f->w, ref, &e) == RX_CAP_OK ? e.lease_expiry : 0;
222 put(c, o->slot[j], 0, ref.cap_id);
223 put(c, o->slot[j], 1, ref.generation);
224 put(c, o->slot[j], 2, RX_AEGIS_SLOT_LIVE);
225 put(c, o->slot[j], 3, res);
226 put(c, o->slot[j], 4, rights);
227 put(c, o->slot[j], 5, seq);
228 put(c, o->slot[j], 6, expiry);
229 put(c, o->slot[j], 7, 0);
230 return 0;
231}
232
233/* ---- setup ---- */
234
235int rx_aegis_create(RxAegisFaculty *f, RxWorld *w, AienosCapAdmin *admin,
236 const RxAegisPolicy *policy, const RxAegisClient *clients, uint32_t n) {
237 memset(f, 0, sizeof(*f));
238 if (!admin || n == 0 || n > RX_AEGIS_CLIENTS || policy->n_rules > RX_AEGIS_RULES)
239 return RX_ERR_ARG;
240 f->w = w;
241 f->admin = admin;
242 f->policy = *policy;
243 f->n_clients = n;
244 if (pthread_mutex_init(&f->mu, NULL) != 0) return RX_ERR_ARG;
245 uint64_t z[RX_MAX_FIELDS] = { 0 };
246 for (uint32_t k = 0; k < n; k++) {
247 f->client[k] = clients[k];
248 for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++)
249 f->minted[k][j] = (RxAegisMinted){ 0, { UINT32_MAX, 0 } };
250#define MK(ref, type, idx) do { \
251 int rc_ = rx_world_create(w, (type), RX_PERSIST_RESIDENT, rx_aegis_res(k, (idx)), \
252 z, &(ref)); \
253 if (rc_ != RX_OK) return rc_; \
254 } while (0)
255 MK(f->o[k].request, RX_OT_AUTH_REQUEST, RX_AEGIS_RES_REQUEST);
256 MK(f->o[k].approval, RX_OT_AUTH_APPROVAL, RX_AEGIS_RES_APPROVAL);
257 MK(f->o[k].decision, RX_OT_AUTH_DECISION, RX_AEGIS_RES_DECISION);
258 for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++)
259 MK(f->o[k].slot[j], RX_OT_CAP_SLOT, RX_AEGIS_RES_SLOT0 + j);
260#undef MK
261 }
262 return RX_OK;
263}
264
265static uint64_t res_of(const RxWorld *w, RxObjRef o) { return w->objects[o.id].resource; }
266
267int rx_aegis_register(RxAegisFaculty *f, const RxAegisCaps caps[]) {
268 RxWorld *w = f->w;
269 const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE;
270 for (uint32_t k = 0; k < f->n_clients; k++) {
271 const RxAegisClientObjects *o = &f->o[k];
272 f->ctx[k].f = f;
273 f->ctx[k].k = k;
274 RxReactionDesc d;
275 int rc;
276
277 memset(&d, 0, sizeof d);
278 d.name = "aegis.decide";
279 d.faculty = RX_FACULTY_AEGIS;
280 d.subject = RX_AEGIS_SUBJ;
281 d.priority = RX_PRIO_FOREGROUND;
282 d.fn = fn_decide;
283 d.user = &f->ctx[k];
284 d.stamp_proposed = true; /* the root checks every field's author */
285 d.n_triggers = 2;
286 d.triggers[0] = (RxDep){ o->request, RX_ALL_FIELDS };
287 d.triggers[1] = (RxDep){ o->approval, RX_ALL_FIELDS };
288 d.n_reads = 1;
289 d.reads[0] = (RxDep){ o->decision, RX_ALL_FIELDS };
290 d.n_writes = 1;
291 d.writes[0] = (RxDep){ o->decision, RX_ALL_FIELDS };
292 d.n_caps = 3;
293 d.caps[0] = (RxCapNeed){ caps[k].aegis_request, res_of(w, o->request), R };
294 d.caps[1] = (RxCapNeed){ caps[k].aegis_approval, res_of(w, o->approval), R };
295 d.caps[2] = (RxCapNeed){ caps[k].aegis_decision, res_of(w, o->decision), RW };
296 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &d, &f->r_decide[k])) != RX_OK) return rc;
297
298 memset(&d, 0, sizeof d);
299 d.name = "root.install";
300 d.faculty = RX_FACULTY_ROOT;
301 d.subject = RX_AEGIS_ROOT_SUBJ;
302 d.priority = RX_PRIO_FOREGROUND;
303 d.fn = fn_install;
304 d.user = &f->ctx[k];
305 d.n_triggers = 1;
306 d.triggers[0] = (RxDep){ o->decision, RX_ALL_FIELDS };
307 d.n_reads = 1 + RX_AEGIS_SLOTS;
308 d.reads[0] = (RxDep){ o->request, RX_ALL_FIELDS };
309 d.n_writes = RX_AEGIS_SLOTS;
310 d.n_caps = 2 + RX_AEGIS_SLOTS;
311 d.caps[0] = (RxCapNeed){ caps[k].root_request, res_of(w, o->request), R };
312 d.caps[1] = (RxCapNeed){ caps[k].root_decision, res_of(w, o->decision), R };
313 for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) {
314 d.reads[1 + j] = (RxDep){ o->slot[j], RX_ALL_FIELDS };
315 d.writes[j] = (RxDep){ o->slot[j], RX_ALL_FIELDS };
316 d.caps[2 + j] = (RxCapNeed){ caps[k].root_slot[j], res_of(w, o->slot[j]), RW };
317 }
318 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &d, &f->r_install[k])) != RX_OK) return rc;
319 }
320 return RX_OK;
321}
322
323void rx_aegis_destroy(RxAegisFaculty *f) { pthread_mutex_destroy(&f->mu); }
324
325void rx_aegis_use_slot(RxReactionDesc *d, uint32_t i, RxObjRef slot) {
326 d->cap_slotted[i] = true;
327 d->cap_slot[i] = slot;
328 if (d->n_triggers < RX_MAX_DEPS)
329 d->triggers[d->n_triggers++] = (RxDep){ slot, RX_FIELD(0) | RX_FIELD(1) | RX_FIELD(2) };
330}