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 / tests / visor / test_visor_authority.c

695 lines · 35030 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 * test_visor_authority.c -- Omega Visor V1, lane 7 hostile authority tests.
19 *
20 * Invariant: THE VISOR CAN ASK. THE VISOR CANNOT GRANT.
21 *
22 * The Visor turns a canonical KIND_EFFECT object into an unauthorized
23 * VisorEffectRequest. This test plays the hostile UI: it forges, replays,
24 * edits and resubmits those requests and checks that the EXISTING authority
25 * code refuses each one:
26 * rx_caproot_validate / rx_world_validate_cap (capability root)
27 * rx_capadmin_mint (root's own mint checks)
28 * rx_aegis_evaluate (AEGIS policy)
29 * rx_gen_reject_replay (+ rx_gen_promote) (generation barrier record)
30 * rx_world_publish_external (World publish check)
31 * omega_validate_object (Omega structure)
32 * Every refusal is paired with a positive control (the honest request passes
33 * the same call), so a refusal cannot come from a harness that refuses all.
34 *
35 * The harness itself is the authority side: it mints, promotes and publishes
36 * with the runtime. Only the Visor objects are subject to the link check
37 * (tests/visor/check_authority_link.sh).
38 *
39 * Test conventions (not existing runtime semantics; see spec/visor-authority.md):
40 * resource = RES_BASE + EffectPayload.resource_class
41 * rights = op 1 READ, op 2 WRITE, op 3 EFFECT, op 16 MINT (privileged)
42 * effect id for the generation record = first 8 bytes (LE) of request_digest
43 */
44#include "visor_effect_request.h"
45
46#include "omega_canonical.h"
47#include "omega_core.h"
48#include "omega_validate.h"
49#include "runtime/rx_aegis.h"
50#include "runtime/rx_caproot.h"
51#include "runtime/rx_generation.h"
52#include "runtime/rx_world.h"
53
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <sys/random.h>
58#include <unistd.h>
59
60#define SUBJ_CLIENT 7u /* the principal the Visor user would act as */
61#define SUBJ_OTHER 9u
62#define SUBJ_PROMOTER 4u
63#define SUBJ_PROPOSER 8u
64#define ISSUER 3u
65#define RES_BASE 0x7100ull
66
67enum { OPC_READ = 1, OPC_WRITE = 2, OPC_EFFECT = 3, OPC_MINT = 16 };
68
69static int g_pass, g_total, g_nonclaims;
70
71#define CHECK(cond, ...) \
72 do { \
73 g_total++; \
74 if (cond) g_pass++; \
75 else { \
76 fprintf(stderr, "FAIL %s:%d ", __FILE__, __LINE__); \
77 fprintf(stderr, __VA_ARGS__); \
78 fputc('\n', stderr); \
79 } \
80 } while (0)
81
82static void case_header(const char *name) { printf("-- %s\n", name); }
83static void non_claim(const char *text) {
84 g_nonclaims++;
85 printf("NON-CLAIM: %s\n", text);
86}
87
88/* Since spec/effect-cap64-migration.md, EffectPayload and VisorEffectRequest
89 * carry the full 64-bit capability generation (effect object version 0x02), so
90 * every honest runtime reference is representable and every Visor-path check
91 * below is counted. (Before the migration these checks were gated on the
92 * generation fitting in 32 bits and printed a NON-CLAIM otherwise.) */
93
94/* ---- conventions ---- */
95
96static uint32_t rights_for_op(uint16_t op) {
97 switch (op) {
98 case OPC_READ: return RX_RIGHT_READ;
99 case OPC_WRITE: return RX_RIGHT_WRITE;
100 case OPC_EFFECT: return RX_RIGHT_EFFECT;
101 case OPC_MINT: return RX_RIGHT_MINT;
102 default: return 0;
103 }
104}
105
106static uint64_t resource_for(uint16_t cls) { return RES_BASE + cls; }
107
108static uint64_t effect_work_id(const VisorEffectRequest *r) {
109 uint64_t v = 0;
110 for (int i = 0; i < 8; i++) v |= (uint64_t)r->request_digest[i] << (8 * i);
111 return v;
112}
113
114/* ---- the existing authority, as the outside consumer would call it ---- */
115
116typedef struct {
117 RxCapRoot root;
118 RxCapAdmin admin;
119 RxWorld w;
120 RxAegisPolicy policy;
121} Env;
122
123static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
124 RxCapMint m;
125 memset(&m, 0, sizeof m);
126 m.issuer = ISSUER;
127 m.subject = subject;
128 m.resource = resource;
129 m.rights = rights;
130 m.parent = (RxCapRef){ UINT32_MAX, 0 };
131 m.authority = rx_capadmin_office(&e->admin);
132 RxCapRef r = { UINT32_MAX, 0 };
133 int rc = rx_capadmin_mint(&e->admin, &m, &r);
134 if (rc != RX_CAP_OK) fprintf(stderr, "harness mint failed: %s\n", rx_cap_strerror(rc));
135 return r;
136}
137
138/* The root / World on a full 64-bit reference (runtime only, no Visor format). */
139static int root_ref(Env *e, RxCapRef ref, uint16_t cls, uint16_t op) {
140 return rx_caproot_validate(&e->root, ref, SUBJ_CLIENT, resource_for(cls), rights_for_op(op),
141 NULL);
142}
143
144static int world_ref(Env *e, RxCapRef ref, uint16_t cls, uint16_t op) {
145 return rx_world_validate_cap(&e->w, ref, SUBJ_CLIENT, resource_for(cls), rights_for_op(op),
146 NULL);
147}
148
149/* What the capability root says about the reference the request carries. */
150static int root_check(Env *e, const VisorEffectRequest *r) {
151 RxCapRef ref = { r->capability_slot, r->capability_generation };
152 return root_ref(e, ref, r->resource_class, r->operation_code);
153}
154
155static int world_check(Env *e, const VisorEffectRequest *r) {
156 RxCapRef ref = { r->capability_slot, r->capability_generation };
157 return world_ref(e, ref, r->resource_class, r->operation_code);
158}
159
160static uint32_t aegis_check(Env *e, const VisorEffectRequest *r, uint32_t *why) {
161 uint64_t lease = 0;
162 uint32_t approval = 0;
163 return rx_aegis_evaluate(&e->policy, SUBJ_CLIENT, resource_for(r->resource_class),
164 rights_for_op(r->operation_code), 0, &lease, why, &approval);
165}
166
167/* ---- graph helpers (the test builds canonical objects the honest way) ---- */
168
169static OmegaObject *add_effect(OmegaGraph *g, uint16_t cls, uint16_t op, RxCapRef cap,
170 const char *params) {
171 OmegaObject *o = omega_build_effect(g, cls, op, cap.cap_id, cap.generation);
172 if (!o) return NULL;
173 if (params) {
174 EffectPayload eff;
175 if (omega_effect_read(o, &eff) != OMEGA_EFFECT_OK) return NULL;
176 eff.param_len = (uint16_t)strlen(params);
177 memcpy(eff.param_bytes, params, eff.param_len);
178 omega_effect_write(o, &eff);
179 omega_compute_semantic_id(o);
180 }
181 return o;
182}
183
184/* Writes the v2 wire bytes; the id is NOT recomputed (tamper cases rely on it). */
185static void set_payload(OmegaObject *o, const EffectPayload *eff) {
186 omega_effect_write(o, eff);
187}
188
189/* ---- generation store helpers (replay record) ---- */
190
191typedef struct { Env *e; } GenAuth;
192
193/* Linux-oracle promotion check: the capability root validates the promoter. */
194static int gen_auth(void *ctx, uint32_t cap_id, uint64_t cap_generation, uint32_t subject,
195 uint64_t resource, uint32_t rights) {
196 GenAuth *a = ctx;
197 return rx_caproot_validate(&a->e->root, (RxCapRef){ cap_id, cap_generation }, subject,
198 resource, rights, NULL);
199}
200
201static int record_committed_effect(Env *e, const char *dir, uint64_t effect_id) {
202 RxCapRef promo = mint(e, SUBJ_PROMOTER, RX_GEN_RES_PROMOTION, RX_RIGHT_PROMOTE);
203 RxGenStore *store = NULL;
204 if (rx_gen_open(dir, &store) != RX_GEN_OK) return -1;
205 static const uint8_t evidence[] = "VISOR-EFFECT", model[] = "m", real[] = "r",
206 config[] = "c", prov[] = "visor-authority-test";
207 RxGenObject obj;
208 memset(&obj, 0, sizeof obj);
209 obj.id = 1;
210 obj.generation = 1;
211 obj.digest[0] = 7;
212 RxGenDraft d;
213 memset(&d, 0, sizeof d);
214 d.authority_epoch = 1;
215 d.authority_generation = 1;
216 d.proofs_ok = 1;
217 d.objects = &obj;
218 d.n_objects = 1;
219 d.evidence = evidence; d.evidence_len = sizeof evidence - 1;
220 d.model = model; d.model_len = sizeof model - 1;
221 d.realization = real; d.realization_len = sizeof real - 1;
222 d.config = config; d.config_len = sizeof config - 1;
223 d.provenance = prov; d.provenance_len = sizeof prov - 1;
224 uint64_t cand = 0;
225 int rc = rx_gen_propose(store, SUBJ_PROPOSER, &d, &cand);
226 if (rc == RX_GEN_OK) {
227 RxGenWork work = {.id = effect_id, .class = RX_WORK_EXTERNAL,
228 .state = RX_WORK_ISSUED, .required = 0};
229 rc = rx_gen_add_work(store, cand, &work);
230 }
231 if (rc == RX_GEN_OK) {
232 RxPromotionRequest req;
233 memset(&req, 0, sizeof req);
234 req.candidate_id = cand;
235 req.subject = SUBJ_PROMOTER;
236 req.cap_id = promo.cap_id;
237 req.cap_generation = promo.generation;
238 req.resource = RX_GEN_RES_PROMOTION;
239 req.rights = RX_GEN_RIGHT_PROMOTE;
240 GenAuth a = { e };
241 rc = rx_gen_promote(store, &req, gen_auth, &a, NULL, NULL, NULL, NULL);
242 }
243 rx_gen_close(store);
244 return rc;
245}
246
247/* ---- compile-time facts about the request type ---- */
248
249_Static_assert(sizeof(((VisorEffectRequest *)0)->authorized) == sizeof(bool),
250 "authorized is a plain flag, not a handle or capability");
251_Static_assert(sizeof(((VisorEffectRequest *)0)->request_digest) == 32,
252 "request_digest is a sha256");
253/* The adapter API has no setter / submitter / granter. These file-scope
254 * definitions conflict with any declaration of the same name in
255 * visor_effect_request.h, so adding one stops this test from compiling. */
256static const char visor_effect_request_authorize = 0;
257static const char visor_effect_request_grant = 0;
258static const char visor_effect_request_submit = 0;
259static const char visor_effect_request_set_authorized = 0;
260
261int main(void) {
262 Env *e = calloc(1, sizeof *e);
263 OmegaGraph *g = calloc(1, sizeof *g);
264 OmegaGraph *snap = calloc(1, sizeof *snap);
265 if (!e || !g || !snap) { fprintf(stderr, "oom\n"); return 2; }
266 (void)visor_effect_request_authorize;
267 (void)visor_effect_request_grant;
268 (void)visor_effect_request_submit;
269 (void)visor_effect_request_set_authorized;
270 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) {
271 fprintf(stderr, "capability root did not start\n");
272 return 2;
273 }
274 if (rx_world_init(&e->w, &e->root, 1, 1u << 12) != RX_OK) {
275 fprintf(stderr, "world did not start\n");
276 rx_caproot_stop(&e->root, &e->admin);
277 return 2;
278 }
279 e->w.external_subject = SUBJ_CLIENT;
280
281 /* Policy: the client may hold READ|WRITE on resource class 1 only. */
282 e->policy.n_rules = 1;
283 e->policy.rules[0] = (RxAegisRule){ .id = 1, .subject = SUBJ_CLIENT,
284 .res_lo = resource_for(1), .res_hi = resource_for(1),
285 .rights = RX_RIGHT_READ | RX_RIGHT_WRITE };
286
287 /* The honest grant the outside authority made for class 1 WRITE. */
288 RxCapRef cap = mint(e, SUBJ_CLIENT, resource_for(1), RX_RIGHT_READ | RX_RIGHT_WRITE);
289 RxCapRef other = mint(e, SUBJ_OTHER, resource_for(2), RX_RIGHT_WRITE);
290
291 OmegaObject *honest_obj = add_effect(g, 1, OPC_WRITE, cap, "set=42");
292 SemanticId honest_id = honest_obj->id;
293 VisorEffectRequest honest;
294 memcpy(snap, g, sizeof *g);
295 CHECK(visor_effect_request_build(g, &honest_id, &honest) == 0, "honest request builds");
296 CHECK(memcmp(snap, g, sizeof *g) == 0, "build never mutates the graph");
297
298 /* ================= 0. adapter contract + positive control ============ */
299 case_header("0 adapter contract and positive control");
300 CHECK(!honest.authorized, "request is unauthorized");
301 CHECK(strcmp(honest.status, "UNAUTHORIZED_REQUEST") == 0, "status");
302 CHECK(strcmp(honest.route, VISOR_EFFECT_ROUTE) == 0, "route");
303 CHECK(memcmp(honest.request_digest, honest_id.bytes, 32) == 0,
304 "digest equals the canonical SemanticId");
305 CHECK(root_ref(e, cap, 1, OPC_WRITE) == RX_CAP_OK,
306 "control (64-bit ref, runtime only): root accepts the minted reference");
307 CHECK(world_ref(e, cap, 1, OPC_WRITE) == RX_CAP_OK,
308 "control (64-bit ref, runtime only): world accepts the minted reference");
309 CHECK(root_check(e, &honest) == RX_CAP_OK, "control: root accepts the honest reference");
310 CHECK(world_check(e, &honest) == RX_CAP_OK, "control: world accepts the honest reference");
311 uint32_t why = 0;
312 CHECK(aegis_check(e, &honest, &why) == RX_AEGIS_GRANT, "control: AEGIS policy grants");
313 char text[1024], json[1024];
314 CHECK(visor_effect_request_format_text(&honest, text, sizeof text) == 0, "text");
315 CHECK(strstr(text, "authorized: false") && strstr(text, "UNAUTHORIZED_REQUEST"), "text says unauthorized");
316 CHECK(visor_effect_request_format_json(&honest, json, sizeof json) == 0, "json");
317 CHECK(strstr(json, "\"authorized\":false") != NULL, "json says unauthorized");
318 CHECK(visor_effect_request_format_json(&honest, json, 16) == -1, "json truncation refused");
319 {
320 bool need = false;
321 memcpy(snap, g, sizeof *g);
322 CHECK(visor_effect_request_classify(g, &honest_id, &need) == 0 && need,
323 "effect object requires authority");
324 CHECK(memcmp(snap, g, sizeof *g) == 0, "classify never mutates the graph");
325 OmegaObject *t = omega_build_type_uint(g, 64);
326 SemanticId tid = t->id;
327 OmegaObject *op = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &tid);
328 SemanticId opid = op->id;
329 OmegaObject *a = omega_build_val_uint(g, &tid, 64, 2);
330 SemanticId aid = a->id;
331 OmegaObject *b = omega_build_val_uint(g, &tid, 64, 3);
332 SemanticId bid = b->id;
333 OmegaObject *ap = omega_build_apply(g, &opid, &aid, &bid);
334 SemanticId apid = ap->id;
335 CHECK(visor_effect_request_classify(g, &apid, &need) == 0 && !need,
336 "pure apply needs no authority");
337 OmegaObject *cr = omega_build_type_cap_ref(g);
338 SemanticId crid = cr->id;
339 OmegaObject *holder = omega_build_val_uint(g, &crid, 64, 1);
340 SemanticId hid = holder->id;
341 CHECK(visor_effect_request_classify(g, &hid, &need) == 0 && need,
342 "value typed CAPABILITY_REF requires authority");
343 OmegaObject *rel = omega_build_val_uint(g, &tid, 64, 99);
344 omega_object_add_relation(rel, REL_DEPENDS_ON, &honest_id);
345 omega_compute_semantic_id(rel);
346 SemanticId relid = rel->id;
347 CHECK(visor_effect_request_classify(g, &relid, &need) == 0 && need,
348 "effect reachable through a relation requires authority");
349 SemanticId ghost;
350 memset(&ghost, 0xAB, sizeof ghost);
351 OmegaObject *dangling = omega_build_val_uint(g, &tid, 64, 100);
352 omega_object_add_relation(dangling, REL_DEPENDS_ON, &ghost);
353 omega_compute_semantic_id(dangling);
354 SemanticId did = dangling->id;
355 need = false;
356 CHECK(visor_effect_request_classify(g, &did, &need) == -1 && need,
357 "missing reference fails closed");
358 CHECK(visor_effect_request_classify(g, &ghost, &need) == -1, "unknown id fails closed");
359 CHECK(visor_effect_request_build(g, &apid, &(VisorEffectRequest){0}) == -1,
360 "non-effect object refused");
361 }
362
363 /* ================= 12. 64-bit generation through the effect format ==== */
364 case_header("12 honest 64-bit generation carried through the v2 effect format");
365 CHECK(honest.capability_slot == cap.cap_id && honest.capability_generation == cap.generation,
366 "format carries the slot and the full 64-bit generation");
367 CHECK(root_check(e, &honest) == RX_CAP_OK && world_check(e, &honest) == RX_CAP_OK,
368 "the honest reference carried through the Visor validates at root and world");
369 {
370 VisorEffectRequest t = honest;
371 t.capability_generation &= 0xFFFFFFFFull; /* what the old 32-bit format kept */
372 if (t.capability_generation != honest.capability_generation) {
373 CHECK(root_check(e, &t) == RX_CAP_ERR_STALE_GEN && world_check(e, &t) == RX_CAP_ERR_STALE_GEN,
374 "the same reference with the high 32 bits cut fails closed (STALE_GEN)");
375 } else {
376 non_claim("the minted generation fits in 32 bits on this run; the truncated-reference "
377 "case is covered by test-effect-cap64 with forced generations.");
378 }
379 }
380
381 /* ================= 1. forged capability ============================== */
382 case_header("1 forged capability (random bytes / wrong slot)");
383 {
384 VisorEffectRequest f = honest;
385 uint32_t rnd[2] = { 0, 0 };
386 if (getrandom(rnd, sizeof rnd, 0) != (ssize_t)sizeof rnd) rnd[0] = 0xDEADBEEFu;
387 f.capability_slot = rnd[0] | 0x80000000u;
388 f.capability_generation = rnd[1];
389 f.authorized = true; /* hostile caller flips the flag; nothing reads it */
390 CHECK(root_check(e, &f) == RX_CAP_ERR_BOUNDS, "random slot: root %d", root_check(e, &f));
391 CHECK(world_check(e, &f) == RX_CAP_ERR_BOUNDS, "random slot: world");
392 CHECK(visor_effect_request_format_json(&f, json, sizeof json) == 0 &&
393 strstr(json, "\"authorized\":false"), "flipped flag still renders unauthorized");
394 f = honest;
395 f.capability_generation ^= 0x5A5A5A5Au;
396 CHECK(root_check(e, &f) == RX_CAP_ERR_STALE_GEN, "random generation: root");
397 CHECK(root_ref(e, (RxCapRef){ cap.cap_id, cap.generation ^ 0x5A5A5A5Aull }, 1, OPC_WRITE) ==
398 RX_CAP_ERR_STALE_GEN,
399 "random generation (64-bit ref, runtime only): root STALE_GEN");
400 f = honest;
401 f.capability_slot = other.cap_id; /* someone else's live cap */
402 f.capability_generation = other.generation;
403 CHECK(root_check(e, &f) == RX_CAP_ERR_SUBJECT, "wrong slot: root");
404 CHECK(world_check(e, &f) == RX_CAP_ERR_SUBJECT, "wrong slot: world");
405 CHECK(root_ref(e, other, 2, OPC_WRITE) == RX_CAP_ERR_SUBJECT,
406 "wrong slot (64-bit ref, runtime only): root SUBJECT");
407 CHECK(world_ref(e, other, 2, OPC_WRITE) == RX_CAP_ERR_SUBJECT,
408 "wrong slot (64-bit ref, runtime only): world SUBJECT");
409 }
410
411 /* ================= 5. malformed request ============================== */
412 case_header("5 malformed effect request (bad payload_len / bad resource)");
413 {
414 OmegaObject *m = add_effect(g, 1, OPC_WRITE, cap, NULL);
415 SemanticId mid = m->id;
416 char err[160];
417 uint32_t keep = m->payload_len;
418 m->payload_len = 8; /* truncated */
419 VisorEffectRequest out;
420 CHECK(omega_validate_object(g, m, err, sizeof err) != 0, "validate rejects short payload");
421 CHECK(visor_effect_request_build(g, &mid, &out) == -1 && !out.authorized, "build refuses short payload");
422 m->payload_len = keep + 16; /* oversized */
423 CHECK(visor_effect_request_build(g, &mid, &out) == -1, "build refuses oversized payload");
424 m->payload_len = keep;
425 EffectPayload eff;
426 CHECK(omega_effect_read(m, &eff) == OMEGA_EFFECT_OK, "decode");
427 eff.param_len = 500; /* > 128 */
428 set_payload(m, &eff);
429 omega_compute_semantic_id(m);
430 mid = m->id;
431 CHECK(visor_effect_request_build(g, &mid, &out) == -1, "build refuses param_len > 128");
432 eff.param_len = 0;
433 eff.resource_class = 0; /* invalid resource */
434 set_payload(m, &eff);
435 omega_compute_semantic_id(m);
436 mid = m->id;
437 CHECK(omega_validate_object(g, m, err, sizeof err) != 0, "validate rejects resource 0");
438 CHECK(visor_effect_request_build(g, &mid, &out) == -1, "build refuses resource 0");
439 }
440
441 /* ================= 6. request exceeding rights ======================= */
442 case_header("6 request exceeding rights (operation outside the grant)");
443 {
444 OmegaObject *x = add_effect(g, 1, OPC_EFFECT, cap, "set=42");
445 SemanticId xid = x->id;
446 VisorEffectRequest r;
447 CHECK(visor_effect_request_build(g, &xid, &r) == 0, "build (a question is allowed)");
448 CHECK(root_check(e, &r) == RX_CAP_ERR_RIGHTS, "root: EFFECT not in grant");
449 CHECK(root_ref(e, cap, 1, OPC_EFFECT) == RX_CAP_ERR_RIGHTS,
450 "root (64-bit ref, runtime only): EFFECT not in grant");
451 CHECK(aegis_check(e, &r, &why) == RX_AEGIS_DENY && why == RX_AEGIS_WHY_NO_RULE,
452 "AEGIS: no rule covers EFFECT (why=%u)", why);
453 OmegaObject *y = add_effect(g, 1, OPC_WRITE, cap, "set=42");
454 EffectPayload eff;
455 CHECK(omega_effect_read(y, &eff) == OMEGA_EFFECT_OK, "decode");
456 eff.resource_class = 2; /* outside the client's domain */
457 set_payload(y, &eff);
458 omega_compute_semantic_id(y);
459 SemanticId yid = y->id;
460 CHECK(visor_effect_request_build(g, &yid, &r) == 0, "build class 2");
461 CHECK(aegis_check(e, &r, &why) == RX_AEGIS_DENY && why == RX_AEGIS_WHY_NO_RULE,
462 "AEGIS: resource outside policy");
463 }
464
465 /* ================= 4. UI attempts a direct privileged call =========== */
466 case_header("4 UI attempts direct privileged call");
467 {
468 OmegaObject *p = add_effect(g, 1, OPC_MINT, cap, NULL);
469 SemanticId pid = p->id;
470 VisorEffectRequest r;
471 CHECK(visor_effect_request_build(g, &pid, &r) == 0, "build privileged question");
472 CHECK(!r.authorized, "still unauthorized");
473 CHECK(aegis_check(e, &r, &why) == RX_AEGIS_DENY && why == RX_AEGIS_WHY_PRIVILEGED,
474 "AEGIS: privileged right refused (why=%u)", why);
475 CHECK(root_check(e, &r) == RX_CAP_ERR_RIGHTS, "root: client cap has no MINT");
476 CHECK(root_ref(e, cap, 1, OPC_MINT) == RX_CAP_ERR_RIGHTS,
477 "root (64-bit ref, runtime only): client cap has no MINT");
478 /* The UI holds only its own reference. Presenting it as mint authority: */
479 RxCapMint m;
480 memset(&m, 0, sizeof m);
481 m.issuer = ISSUER;
482 m.subject = SUBJ_CLIENT;
483 m.resource = resource_for(1);
484 m.rights = RX_RIGHT_EFFECT;
485 m.parent = (RxCapRef){ UINT32_MAX, 0 };
486 m.authority = cap;
487 RxCapRef got;
488 int rc = rx_capadmin_mint(&e->admin, &m, &got);
489 CHECK(rc == RX_CAP_ERR_UNAUTHORIZED, "root refuses mint under a non-office authority (%d)", rc);
490 /* A forged admin handle (right channel, guessed token). */
491 RxCapAdmin forged = e->admin;
492 if (getrandom(forged.token, sizeof forged.token, 0) != (ssize_t)sizeof forged.token)
493 memset(forged.token, 0x42, sizeof forged.token);
494 m.authority = rx_capadmin_office(&e->admin);
495 rc = rx_capadmin_mint(&forged, &m, &got);
496 CHECK(rc == RX_CAP_ERR_UNAUTHORIZED, "root refuses mint with a forged token (%d)", rc);
497 /* No handle at all. */
498 RxCapAdmin none;
499 memset(&none, 0, sizeof none);
500 none.ctl_fd = -1;
501 CHECK(rx_capadmin_mint(&none, &m, &got) == RX_CAP_ERR_IO, "no admin channel, no mint");
502 printf(" link isolation is checked separately: make visor-authority-check\n");
503 }
504
505 /* ================= 7/8. resource or arguments changed after authorization */
506 case_header("7 resource changed after authorization");
507 {
508 /* The authorized request is `honest` (digest D). The object is edited in place. */
509 OmegaObject *o = omega_graph_find_object(g, &honest_id);
510 EffectPayload saved;
511 CHECK(omega_effect_read(o, &saved) == OMEGA_EFFECT_OK, "decode");
512 EffectPayload eff = saved;
513 eff.resource_class = 2;
514 set_payload(o, &eff);
515 VisorEffectRequest r;
516 CHECK(visor_effect_request_build(g, &honest_id, &r) == -1,
517 "edited without re-id: digest != SemanticId, refused");
518 /* The attacker re-identifies the object: it is now a different object. */
519 omega_compute_semantic_id(o);
520 SemanticId nid = o->id;
521 CHECK(visor_effect_request_build(g, &nid, &r) == 0, "re-identified object builds");
522 CHECK(memcmp(r.request_digest, honest.request_digest, 32) != 0,
523 "digest mismatch with the authorized request");
524 CHECK(root_check(e, &r) == RX_CAP_ERR_RESOURCE, "root: cap is bound to the old resource");
525 CHECK(world_check(e, &r) == RX_CAP_ERR_RESOURCE, "world: same");
526 CHECK(root_ref(e, cap, 2, OPC_WRITE) == RX_CAP_ERR_RESOURCE,
527 "root (64-bit ref, runtime only): cap is bound to the old resource");
528 CHECK(world_ref(e, cap, 2, OPC_WRITE) == RX_CAP_ERR_RESOURCE,
529 "world (64-bit ref, runtime only): same");
530 set_payload(o, &saved);
531 omega_compute_semantic_id(o);
532 CHECK(omega_compare_semantic_id(&o->id, &honest_id) == 0, "restored");
533 }
534 case_header("8 arguments changed after authorization");
535 {
536 OmegaObject *o = omega_graph_find_object(g, &honest_id);
537 EffectPayload saved;
538 CHECK(omega_effect_read(o, &saved) == OMEGA_EFFECT_OK, "decode");
539 EffectPayload eff = saved;
540 eff.param_bytes[4] = '9'; /* set=42 -> set=92 */
541 set_payload(o, &eff);
542 VisorEffectRequest r;
543 CHECK(visor_effect_request_build(g, &honest_id, &r) == -1,
544 "edited params without re-id: refused");
545 omega_compute_semantic_id(o);
546 SemanticId nid = o->id;
547 CHECK(visor_effect_request_build(g, &nid, &r) == 0 &&
548 memcmp(r.request_digest, honest.request_digest, 32) != 0,
549 "re-identified params: digest mismatch with the authorized request");
550 CHECK(root_check(e, &r) == RX_CAP_OK,
551 "observed: params-only change still validates at the root (basis of the non-claim)");
552 non_claim("the capability root binds (subject, resource, rights, generation), not effect "
553 "parameters: a params-only change still validates at rx_caproot_validate; only the "
554 "request digest differs. No existing runtime call compares effect digests.");
555 eff = saved;
556 eff.operation_code = OPC_EFFECT; /* operation is an argument too */
557 set_payload(o, &eff);
558 omega_compute_semantic_id(o);
559 nid = o->id;
560 CHECK(visor_effect_request_build(g, &nid, &r) == 0, "operation changed: builds");
561 CHECK(root_check(e, &r) == RX_CAP_ERR_RIGHTS, "operation changed: root refuses rights");
562 set_payload(o, &saved);
563 omega_compute_semantic_id(o);
564 }
565
566 /* ================= 2/3/9. stale generation, replay, duplicate ========= */
567 char dir[] = "/tmp/visor-authority-XXXXXX";
568 int have_dir = mkdtemp(dir) != NULL;
569 CHECK(have_dir, "temp dir");
570 case_header("9 duplicate submission");
571 {
572 uint64_t id = effect_work_id(&honest);
573 int rc = have_dir ? record_committed_effect(e, dir, id) : -1;
574 CHECK(rc == RX_GEN_OK, "harness: first submission committed as external effect (%d)", rc);
575 CHECK(rx_gen_reject_replay(dir, id) == RX_GEN_ERR_REPLAY, "duplicate refused by the generation record");
576 VisorEffectRequest fresh;
577 OmegaObject *n = add_effect(g, 1, OPC_WRITE, cap, "set=43");
578 SemanticId nid = n->id;
579 CHECK(visor_effect_request_build(g, &nid, &fresh) == 0 &&
580 rx_gen_reject_replay(dir, effect_work_id(&fresh)) == RX_GEN_OK,
581 "control: a different request is not a replay");
582 non_claim("the World does not deduplicate external stimuli; rx_world_publish_external accepts "
583 "the same mutation twice under a valid cap. Duplicate refusal exists only in the "
584 "generation barrier record (rx_gen_reject_replay) of committed external effects.");
585 }
586 case_header("2 stale generation");
587 {
588 RxCapRef office = rx_capadmin_office(&e->admin);
589 CHECK(rx_capadmin_revoke(&e->admin, office, cap) == RX_CAP_OK, "harness revokes");
590 CHECK(root_check(e, &honest) == RX_CAP_ERR_REVOKED, "revoked: root");
591 CHECK(root_ref(e, cap, 1, OPC_WRITE) == RX_CAP_ERR_REVOKED,
592 "revoked (64-bit ref, runtime only): root");
593 CHECK(rx_capadmin_reclaim(&e->admin, office, cap.cap_id) == RX_CAP_OK, "harness reclaims (generation advances)");
594 /* The request carries the full 64-bit generation (case 12 shows it
595 * validated before the revoke), so STALE_GEN here comes from the
596 * reclaim, not from truncation. */
597 CHECK(root_check(e, &honest) == RX_CAP_ERR_STALE_GEN, "stale generation: root");
598 CHECK(world_check(e, &honest) == RX_CAP_ERR_STALE_GEN, "stale generation: world");
599 CHECK(root_ref(e, cap, 1, OPC_WRITE) == RX_CAP_ERR_STALE_GEN,
600 "stale generation (64-bit ref, runtime only): root");
601 CHECK(world_ref(e, cap, 1, OPC_WRITE) == RX_CAP_ERR_STALE_GEN,
602 "stale generation (64-bit ref, runtime only): world");
603 }
604 case_header("3 replayed authorization after generation advance");
605 {
606 /* The authority grants again; the slot may be reused at a newer generation. */
607 RxCapRef again = mint(e, SUBJ_CLIENT, resource_for(1), RX_RIGHT_READ | RX_RIGHT_WRITE);
608 CHECK(again.cap_id != UINT32_MAX, "harness re-grants");
609 CHECK(root_check(e, &honest) == RX_CAP_ERR_STALE_GEN,
610 "old request replayed against the new grant: root refuses");
611 CHECK(root_ref(e, cap, 1, OPC_WRITE) == RX_CAP_ERR_STALE_GEN &&
612 root_ref(e, again, 1, OPC_WRITE) == RX_CAP_OK,
613 "old 64-bit ref replayed (runtime only): root STALE_GEN while the new grant validates");
614 CHECK(rx_gen_reject_replay(dir, effect_work_id(&honest)) == RX_GEN_ERR_REPLAY,
615 "old request replayed: generation record refuses");
616 OmegaObject *n = add_effect(g, 1, OPC_WRITE, again, "set=42");
617 SemanticId nid = n->id;
618 VisorEffectRequest r;
619 CHECK(visor_effect_request_build(g, &nid, &r) == 0, "request naming the new grant builds");
620 CHECK(root_check(e, &r) == RX_CAP_OK,
621 "control: a request naming the new grant validates");
622 }
623
624 /* ================= 10. bypass the publish gate ======================= */
625 case_header("10 bypass publish gate (publish without a valid cap)");
626 {
627 RxObjRef obj = { UINT32_MAX, 0 };
628 uint64_t init[RX_MAX_FIELDS] = { 0 };
629 CHECK(rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource_for(1), init, &obj) == RX_OK,
630 "harness creates the target object");
631 RxMutation mu = { obj, 0, 42 };
632 RxCapRef forged = { honest.capability_slot, honest.capability_generation }; /* stale */
633 CHECK(rx_world_publish_external(&e->w, forged, &mu, 1) == RX_ERR_AUTHORITY, "stale ref: refused");
634 CHECK(rx_world_publish_external(&e->w, cap, &mu, 1) == RX_ERR_AUTHORITY,
635 "stale 64-bit ref (reclaimed grant): refused");
636 RxCapRef junk = { 0x7FFFFFFFu, 1 };
637 CHECK(rx_world_publish_external(&e->w, junk, &mu, 1) == RX_ERR_AUTHORITY, "junk ref: refused");
638 RxCapRef ro = mint(e, SUBJ_CLIENT, resource_for(1), RX_RIGHT_READ);
639 CHECK(rx_world_publish_external(&e->w, ro, &mu, 1) == RX_ERR_AUTHORITY, "read-only cap: refused");
640 RxCapRef theirs = mint(e, SUBJ_OTHER, resource_for(1), RX_RIGHT_WRITE);
641 CHECK(rx_world_publish_external(&e->w, theirs, &mu, 1) == RX_ERR_AUTHORITY, "other subject's cap: refused");
642 RxObjRef gone = obj;
643 gone.generation ^= 1u;
644 RxCapRef rw = mint(e, SUBJ_CLIENT, resource_for(1), RX_RIGHT_WRITE);
645 RxMutation stale_obj = { gone, 0, 42 };
646 CHECK(rx_world_publish_external(&e->w, rw, &stale_obj, 1) == RX_ERR_STALE_GEN, "stale object: refused");
647 CHECK(rx_world_publish_external(&e->w, rw, &mu, 1) >= 0, "control: valid WRITE cap publishes");
648 CHECK(rx_world_publish_external(&e->w, rw, &mu, 1) >= 0,
649 "observed: the World accepts the same stimulus twice (basis of the duplicate non-claim)");
650 non_claim("the typed-result publish gate (rc_gate_register, resident reaction) is not driven "
651 "here; only the World's external publish check is exercised.");
652 }
653
654 /* ================= 11. bypass AEGIS ================================== */
655 case_header("11 bypass AEGIS (submit a reference nobody granted)");
656 {
657 /* The Visor has no path to AEGIS or the root. Whatever reference it
658 * copies from an object, only a reference the root minted validates. */
659 OmegaObject *n = add_effect(g, 1, OPC_WRITE, (RxCapRef){ RX_CAP_MAX - 1u, 1 }, "set=1");
660 SemanticId nid = n->id;
661 VisorEffectRequest r;
662 CHECK(visor_effect_request_build(g, &nid, &r) == 0, "build ungranted reference");
663 int rc = root_check(e, &r);
664 CHECK(rc != RX_CAP_OK, "root refuses an ungranted slot (%s)", rx_cap_strerror(rc));
665 CHECK(world_check(e, &r) == rc, "world agrees");
666 RxAegisPolicy empty;
667 memset(&empty, 0, sizeof empty);
668 uint64_t lease;
669 uint32_t appr;
670 CHECK(rx_aegis_evaluate(&empty, SUBJ_CLIENT, resource_for(1), RX_RIGHT_WRITE, 0, &lease,
671 &why, &appr) == RX_AEGIS_DENY, "no policy, no grant");
672 non_claim("AEGIS is not invoked on a request path (rx_aegis.h: nothing is called); "
673 "'bypass' is refused because only root.install mints and the root validates every "
674 "presented reference. The resident aegis.decide/root.install reactions are not run here.");
675 }
676
677 if (have_dir) {
678 char cmd[128];
679 snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
680 if (system(cmd) != 0) fprintf(stderr, "warning: could not remove %s\n", dir);
681 }
682 rx_world_destroy(&e->w);
683 rx_caproot_stop(&e->root, &e->admin);
684 free(snap);
685 free(g);
686 free(e);
687
688 printf("non-claims: %d\n", g_nonclaims);
689 printf("PASS %d/%d\n", g_pass, g_total);
690 if (g_pass == g_total) {
691 printf("OMEGA_VISOR_AUTHORITY_HOSTILE_PASS\n");
692 return 0;
693 }
694 return 1;
695}