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 / runtime / rx_r16_negative.c

1571 lines · 82969 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_r16_negative.c -- R16-G4: legacy paths cannot bypass authority
19 * (spec/r16-orchestrator-retirement.md ยง5 R16-G4, clarification C4).
20 *
21 * The production body (the R13 living system, built by the R15 rig in RES-4)
22 * is started and left quiescent. A legacy / reference / oracle context then
23 * tries six acts. It holds what such code could hold after R16: the world
24 * handle, the read-only Aienux view, the R9 store handle, every capability
25 * reference it could observe (the harness's external grants, the promoter's
26 * grants) and grants of its own on its own resources. It never holds the
27 * Aienux admin handle; only this harness does, to build fixtures before the
28 * acts start.
29 *
30 * (1) write authoritative AIEN belief
31 * (2) select an Omega realization (write Omega's selection)
32 * (3) mint (or revoke) authority
33 * (4) promote a generation
34 * (5) bypass the effect/authority boundary (forged publication descriptor,
35 * overwritten physical record)
36 * (6) advance the world generation (write the in-force record production
37 * reads; move the R9 active generation)
38 *
39 * Every attempt must be refused by the R7 native authority, R9's promotion
40 * rules or the publication boundary, with no change to authoritative state:
41 * the watched objects (fields, version, generation), the R9 active generation
42 * and lineage (in memory and recovered from disk) and the Aienux table (no
43 * state change reported by the authority observer).
44 *
45 * "Removing any one guard turns the test red" is checked by
46 * tests/r16_negative/mutate.sh (make test-r16-negative-mutants), which builds
47 * this test against scratch copies with one guard removed at a time.
48 * Two cases exist so that redundant guards are each load-bearing: a legacy
49 * reaction whose real grant the office revokes while it runs (the commit
50 * re-check), and at the boundary a real grant that is not the writer of
51 * record's (identity) and the writer's own grant after revocation (validation).
52 *
53 * Spec C5 (caller identity). The legacy context also holds runtime-issued
54 * caller credentials of its own (LEGACY, LEGACY2: enrolled by the rig as
55 * fixtures before the world's caller set is bound), never another subject's.
56 * The pre-fix exploit is kept: last, a promotion naming the promoter itself as
57 * subject with the promoter's grant reference, and a reaction naming the
58 * promoter as subject with its in-force grant. At 44d8c06 (before C5) both were
59 * ACCEPTED (generation moved 1 -> 3 in memory and on disk; the in-force record
60 * written) and the gate printed FAIL. Now each credential variant the legacy
61 * context could present (none, zero, random, its own, its own secret at the
62 * promoter's generation, a permissive authority callback) must be refused with
63 * the identity error, with nothing moved. Then the C5 identity probes: spoofed
64 * subject, forged / stale / unknown / absent credentials, enrollment after
65 * bind, revocation needing the credential, secret collision (rig credentials
66 * and 64 enrollments on a scratch world), an identity revoked while its
67 * reaction runs (the write must not commit, later activations blocked), the
68 * revoked credential replayed at check, revocation, proposal, promotion and
69 * registration, and a bound R9 store ignoring a permissive authority callback.
70 * Last, a positive control: the promoter, with its own credential and grant,
71 * still promotes. The gate line covers all of it and is what the mutant suite
72 * judges; the six-act core is also printed on its own line ("R16 G4 core:").
73 *
74 * Spec C7 (outside review): the promotion re-checks identity at the flip
75 * (P1), a revocation cannot land between a commit's identity check and its
76 * publish (P2), and a reaction registered before binding cannot claim an
77 * identity it did not prove (P3); on a bound store the work-accounting calls
78 * need a credential and work cannot be added already done (P4); on scratch
79 * worlds and stores, in the gate.
80 *
81 * Also checked (reported, outside the six): the SEQ reference loop refuses to
82 * drive a production world.
83 *
84 * Limits (stated in the receipt, not tested as refusals):
85 * - world-owner calls (rx_world_create, rx_world_retire) take no capability:
86 * any code holding the RxWorld pointer can retire an object, which moves
87 * that object's generation. rx_world_add_reaction now needs the named
88 * subject's credential on a bound world, and registering a reaction still
89 * grants it nothing; its grants are checked at every activation.
90 * - rx_gen_promote on a store bound with rx_gen_bind_authority uses the
91 * store's own authority and ignores the caller's RxGenAuthFn (tested
92 * below). An unbound store (older tests) still takes the caller's.
93 * - Caller credentials are secrets in process memory: code in the same
94 * address space that reads another component's keyring can act as it.
95 * - C code in the same address space can write any memory. G3 shows the
96 * production binary does not link or exec the legacy code at all.
97 */
98#include "rx_r15_rig.h"
99#include "runtime/aienos_cap.h"
100#include "sha256.h"
101
102#include <stdio.h>
103#include <stdlib.h>
104#include <string.h>
105#include <sys/random.h>
106#include <time.h>
107
108#define U(x) ((unsigned long long)(x))
109
110enum { EXTERNAL_SUBJ = 100, LEGACY_SUBJ = 160, LEGACY2_SUBJ = 161 };
111#define RES_LEGACY_SCRATCH 0x7160000ull
112
113static int g_fail;
114#define CHECK(c, ...) do { if (!(c)) { printf("R16 G4 FAIL: "); printf(__VA_ARGS__); \
115 printf("\n"); g_fail++; } } while (0)
116
117/* ---- authority observer: counts Aienux state changes ------------------------ */
118static atomic_ullong g_auth_changes;
119static void observer(void *ctx, uint32_t op, const AienosCapEntry *entry, int result) {
120 (void)ctx; (void)op;
121 if (entry && result == 0) atomic_fetch_add(&g_auth_changes, 1);
122}
123/* Aienux changes the harness itself makes during the acts (the office
124 * revoking a grant, see revoked_in_flight and the end of act 5). Any change
125 * beyond these fails the act. */
126static atomic_ullong g_expected_changes;
127
128/* ---- authoritative state snapshot ------------------------------------------ */
129#define N_WATCH 16
130typedef struct {
131 const char *name[N_WATCH];
132 RxObjRef ref[N_WATCH];
133 RxObject obj[N_WATCH];
134 uint32_t n;
135 uint64_t gen_active, gen_lineage, recovered_active, recovered_lineage;
136 uint64_t auth_changes;
137 uint8_t digest[32];
138} Snap;
139
140static void watch(Snap *s, const char *name, RxObjRef ref) {
141 if (s->n < N_WATCH) { s->name[s->n] = name; s->ref[s->n] = ref; s->n++; }
142}
143
144static int take(R15Rig *r, Snap *s) {
145 if (rx_world_wait_quiescent(&r->w, 10000) != RX_OK) return -1;
146 for (uint32_t i = 0; i < s->n; i++) {
147 memset(&s->obj[i], 0, sizeof s->obj[i]);
148 if (rx_world_read(&r->w, s->ref[i], &s->obj[i]) != RX_OK) return -1;
149 }
150 if (rx_gen_active(r->gen, &s->gen_active, &s->gen_lineage) != RX_GEN_OK) return -1;
151 RxRecoveryRecord rec;
152 if (rx_gen_recover(r->generation_dir, &rec) != RX_GEN_OK) return -1;
153 s->recovered_active = rec.active_id;
154 s->recovered_lineage = rec.lineage;
155 s->auth_changes = atomic_load(&g_auth_changes);
156 return 0;
157}
158
159/* Same authoritative state? Prints the first difference. */
160static int same_x(const Snap *a, const Snap *b, const char *act, unsigned long long expected) {
161 for (uint32_t i = 0; i < a->n; i++) {
162 const RxObject *x = &a->obj[i], *y = &b->obj[i];
163 if (x->version != y->version || x->generation != y->generation || x->live != y->live ||
164 memcmp(x->field, y->field, sizeof x->field) != 0) {
165 printf("R16 G4 FAIL: %s changed %s (version %llu -> %llu, generation %u -> %u)\n",
166 act, a->name[i], U(x->version), U(y->version), x->generation, y->generation);
167 return 0;
168 }
169 }
170 if (a->gen_active != b->gen_active || a->gen_lineage != b->gen_lineage ||
171 a->recovered_active != b->recovered_active ||
172 a->recovered_lineage != b->recovered_lineage) {
173 printf("R16 G4 FAIL: %s moved the R9 generation (%llu/%llu -> %llu/%llu, disk %llu/%llu -> %llu/%llu)\n",
174 act, U(a->gen_active), U(a->gen_lineage), U(b->gen_active), U(b->gen_lineage),
175 U(a->recovered_active), U(a->recovered_lineage), U(b->recovered_active),
176 U(b->recovered_lineage));
177 return 0;
178 }
179 if (b->auth_changes - a->auth_changes != expected) {
180 printf("R16 G4 FAIL: %s changed the AIENOS table (%llu state changes, %llu by the harness)\n",
181 act, U(b->auth_changes - a->auth_changes), U(expected));
182 return 0;
183 }
184 return 1;
185}
186static int same(const Snap *a, const Snap *b, const char *act) {
187 return same_x(a, b, act, atomic_load(&g_expected_changes));
188}
189
190/* ---- the legacy context -------------------------------------------------------- */
191typedef struct {
192 RxWorld *w; /* world handle (world-owner calls only) */
193 const AienosCapView *view; /* read-only authority view */
194 RxGenStore *gen; /* R9 store handle */
195 /* references it could observe */
196 RxCapRef stolen_goal; /* the harness's external WRITE on AIEN's goal */
197 RxCapRef borrowed_inforce; /* the promoter's WRITE on the in-force record */
198 RxCapRef borrowed_promote; /* the promoter's promotion right */
199 RxCapRef forged; /* a made-up reference */
200 /* its own grants (issued by the harness as fixtures) */
201 RxCapRef own_scratch; /* LEGACY: READ|WRITE on its scratch object */
202 RxCapRef own_promote; /* LEGACY: PROMOTE on the promotion resource */
203 RxCapRef own2_read_promote; /* LEGACY2: READ on the promotion resource */
204 RxCapRef ext_scratch; /* EXTERNAL: WRITE on the scratch object (trigger) */
205 RxCapRef inflight_belief; /* LEGACY: WRITE on AIEN's belief, revoked while it runs */
206 RxCapRef own_output; /* LEGACY: WRITE on the seat output's resource (not the
207 writer of record) */
208 AienosCapAdmin *office; /* the harness's office: used only to revoke, never by
209 the legacy context's own calls */
210 RxObjRef scratch;
211 RxObjRef scratch2; /* a second scratch object (C5 in-flight probe target) */
212 const RxCallerKeyring *keys; /* its own caller credentials (LEGACY, LEGACY2), issued
213 by the runtime at enrollment */
214 uint64_t candidate; /* a draft proposed by LEGACY */
215} Legacy;
216
217typedef struct { uint32_t tried, refused; } Tally;
218static void tally(Tally *t, int refused, const char *act, const char *what, long long rc) {
219 t->tried++;
220 if (refused) t->refused++;
221 else printf("R16 G4 FAIL: %s: %s was NOT refused (rc %lld)\n", act, what, rc);
222 if (!refused) g_fail++;
223}
224
225static int64_t publish_one(Legacy *L, RxCapRef cap, RxObjRef obj, uint32_t field, uint64_t v) {
226 RxMutation m = {obj, field, v};
227 return rx_world_publish_external(L->w, cap, &m, 1);
228}
229
230typedef struct { RxObjRef target; uint32_t field; } Poke;
231static int fn_poke(RxCtx *c) {
232 Poke *p = c->user;
233 c->out[c->n_out++] = (RxMutation){p->target, p->field, 0xBADBADull};
234 return 0;
235}
236
237/* A reaction registered by the legacy context: woken by its scratch object,
238 * it writes `target` holding `cap` declared for the target's resource. */
239static uint32_t g_named_subject; /* 0: LEGACY_SUBJ; else the subject the reaction names */
240static RxDep g_named_trigger; /* with g_named_subject: its trigger and read grant */
241static RxCapNeed g_named_read;
242/* With g_named_explicit: the credential the descriptor carries (NULL: none),
243 * instead of the one its keyring holds for the named subject. */
244static const RxCallerCred *g_named_cred;
245static int g_named_explicit;
246static int legacy_reaction_fn(Legacy *L, const char *name, RxObjRef target, uint64_t target_res,
247 RxCapRef cap, int declare_write, Poke *p, RxFn fn, void *user,
248 uint32_t *id) {
249 RxReactionDesc d;
250 memset(&d, 0, sizeof d);
251 d.name = name;
252 d.faculty = RX_FACULTY_EXTERNAL;
253 d.subject = g_named_subject ? g_named_subject : LEGACY_SUBJ;
254 d.priority = RX_PRIO_FOREGROUND;
255 d.fn = fn;
256 d.user = user;
257 *p = (Poke){target, 0};
258 d.n_triggers = 1;
259 d.triggers[0] = (RxDep){L->scratch, RX_FIELD(0)};
260 d.n_writes = 1;
261 d.writes[0] = (RxDep){target, RX_FIELD(0)};
262 d.n_caps = 1;
263 d.caps[0] = (RxCapNeed){L->own_scratch, RES_LEGACY_SCRATCH, RX_RIGHT_READ};
264 if (g_named_subject) { /* its scratch grant is LEGACY_SUBJ's */
265 d.triggers[0] = g_named_trigger;
266 d.caps[0] = g_named_read;
267 }
268 if (declare_write) d.caps[d.n_caps++] = (RxCapNeed){cap, target_res, RX_RIGHT_WRITE};
269 if (g_named_explicit) {
270 if (g_named_cred) d.caller = *g_named_cred;
271 int rc = rx_world_add_reaction(L->w, &d, id);
272 rx_caller_wipe(&d.caller);
273 return rc;
274 }
275 return rx_world_add_reaction_keyed(L->w, L->keys, &d, id);
276}
277static int legacy_reaction(Legacy *L, const char *name, RxObjRef target, uint64_t target_res,
278 RxCapRef cap, int declare_write, Poke *p, uint32_t *id) {
279 return legacy_reaction_fn(L, name, target, target_res, cap, declare_write, p, fn_poke, p, id);
280}
281
282static uint64_t g_trigger;
283/* Wake the legacy reactions: an honest external write to the scratch object. */
284static int wake_legacy(Legacy *L) {
285 if (publish_one(L, L->ext_scratch, L->scratch, 0, ++g_trigger) <= 0) return -1;
286 return rx_world_wait_quiescent(L->w, 10000);
287}
288
289/* Acts (1), (2) and the record half of (6) share one shape: a legacy write
290 * to an authoritative object, through every door it could try. */
291static void write_attempts(Legacy *L, const char *act, RxObjRef target, RxCapRef borrowed,
292 const char *borrowed_name, Tally *t) {
293 RxObject o;
294 if (rx_world_read(L->w, target, &o) != RX_OK) { CHECK(0, "%s: target unreadable", act); return; }
295 uint64_t res = o.resource;
296 int64_t rc;
297 rc = publish_one(L, L->forged, target, 0, 0xBADBADull);
298 tally(t, rc == RX_ERR_AUTHORITY, act, "outside publication with a forged reference", rc);
299 rc = publish_one(L, L->stolen_goal, target, 0, 0xBADBADull);
300 tally(t, rc == RX_ERR_AUTHORITY, act, "outside publication with the external goal grant", rc);
301 rc = publish_one(L, borrowed, target, 0, 0xBADBADull);
302 tally(t, rc == RX_ERR_AUTHORITY, act, borrowed_name, rc);
303 rc = publish_one(L, L->own_scratch, target, 0, 0xBADBADull);
304 tally(t, rc == RX_ERR_AUTHORITY, act, "outside publication with its own scratch grant", rc);
305
306 /* Registered reactions. Undeclared write: refused at registration. */
307 static Poke p_undeclared, p_forged, p_own;
308 uint32_t id_u = 0, id_f = 0, id_o = 0;
309 int add_u = legacy_reaction(L, "legacy.undeclared", target, res, L->forged, 0, &p_undeclared,
310 &id_u);
311 tally(t, add_u == RX_ERR_AUTHORITY, act, "reaction writing without a declared grant", add_u);
312 int add_f = legacy_reaction(L, "legacy.forged", target, res, L->forged, 1, &p_forged, &id_f);
313 int add_o = legacy_reaction(L, "legacy.own", target, res, L->own_scratch, 1, &p_own, &id_o);
314 CHECK(add_f == RX_OK && add_o == RX_OK, "%s: could not register legacy reactions (%d %d)",
315 act, add_f, add_o);
316 if (add_f != RX_OK || add_o != RX_OK) return;
317 uint64_t blocked0 = L->w->stats.blocked_authority;
318 CHECK(wake_legacy(L) == 0, "%s: legacy reactions did not settle", act);
319 pthread_mutex_lock(&L->w->mu);
320 RxReaction rf = L->w->reactions[id_f], ro = L->w->reactions[id_o];
321 const RxCrumb *kf = rx_world_crumb(L->w, rf.last_crumb), *ko = rx_world_crumb(L->w, ro.last_crumb);
322 int bf = kf && kf->kind == RX_CRUMB_BLOCKED_AUTHORITY, bo = ko && ko->kind == RX_CRUMB_BLOCKED_AUTHORITY;
323 uint64_t blocked = L->w->stats.blocked_authority - blocked0;
324 pthread_mutex_unlock(&L->w->mu);
325 tally(t, rf.activations >= 1 && rf.commits == 0 && bf, act,
326 "reaction holding a forged reference", (long long)rf.commits);
327 tally(t, ro.activations >= 1 && ro.commits == 0 && bo, act,
328 "reaction holding its own grant declared for the target", (long long)ro.commits);
329 CHECK(blocked >= 2, "%s: %llu blocked activations, expected 2", act, U(blocked));
330}
331
332
333/* A legacy reaction holding a genuine WRITE grant on the target, which the
334 * office revokes while the activation runs (the body is only the fixed point
335 * in time; the revocation is the harness's). The write must not land: the
336 * commit re-check refuses it (RX_CRUMB_REJECTED, no commit). */
337typedef struct { Poke p; AienosCapAdmin *office; RxCapRef cap; int revoked; } Inflight;
338static int fn_inflight(RxCtx *c) {
339 Inflight *f = c->user;
340 if (!f->revoked) {
341 AienosCapRef office = {0, 0};
342 aienos_cap_office(f->office, &office);
343 unsigned long long before = atomic_load(&g_auth_changes);
344 f->revoked = aienos_cap_revoke(f->office, office,
345 (AienosCapRef){f->cap.cap_id, f->cap.generation}) == 0;
346 atomic_fetch_add(&g_expected_changes, atomic_load(&g_auth_changes) - before);
347 }
348 c->out[c->n_out++] = (RxMutation){f->p.target, f->p.field, 0xBADBADull};
349 return 0;
350}
351
352static void revoked_in_flight(Legacy *L, const char *act, RxObjRef target, Tally *t) {
353 RxObject o;
354 if (rx_world_read(L->w, target, &o) != RX_OK) { CHECK(0, "%s: target unreadable", act); return; }
355 static Inflight f;
356 memset(&f, 0, sizeof f);
357 f.office = L->office;
358 f.cap = L->inflight_belief;
359 uint32_t id = 0;
360 int add = legacy_reaction_fn(L, "legacy.revoked-in-flight", target, o.resource, f.cap, 1, &f.p,
361 fn_inflight, &f, &id);
362 CHECK(add == RX_OK, "%s: could not register the in-flight reaction (%d)", act, add);
363 if (add != RX_OK) return;
364 CHECK(wake_legacy(L) == 0, "%s: the in-flight reaction did not settle", act);
365 pthread_mutex_lock(&L->w->mu);
366 RxReaction rr = L->w->reactions[id];
367 const RxCrumb *k = rx_world_crumb(L->w, rr.last_crumb);
368 int rejected = k && k->kind == RX_CRUMB_REJECTED;
369 pthread_mutex_unlock(&L->w->mu);
370 CHECK(f.revoked, "%s: the office could not revoke the in-flight grant", act);
371 tally(t, f.revoked && rr.activations >= 1 && rr.commits == 0 && rejected, act,
372 "reaction whose real grant is revoked while it runs", (long long)rr.commits);
373}
374static int native_auth(void *ctx, uint32_t cap_id, uint64_t generation, uint32_t subject,
375 uint64_t resource, uint32_t rights) {
376 AienosCapEntry e;
377 return aienos_cap_validate(ctx, (AienosCapRef){cap_id, generation}, subject, resource,
378 rights, &e);
379}
380
381/* A caller-supplied authority callback that accepts everything. A store bound
382 * to the runtime's authority (rx_gen_bind_authority) must ignore it. */
383static int permissive_auth(void *ctx, uint32_t cap_id, uint64_t generation, uint32_t subject,
384 uint64_t resource, uint32_t rights) {
385 (void)ctx; (void)cap_id; (void)generation; (void)subject; (void)resource; (void)rights;
386 return 0;
387}
388
389typedef struct {
390 uint32_t cap_id;
391 uint64_t generation;
392 uint32_t worker;
393 uint64_t resource;
394 int checks;
395} WorkAuth;
396
397/* The promotion grant is not the subject of P4. Work grants are accepted
398 * only for the exact worker, capability generation and derived resource the
399 * probe selected, modeling the native root's object/action/lease checks. */
400static int work_auth(void *ctx, uint32_t cap_id, uint64_t generation, uint32_t subject,
401 uint64_t resource, uint32_t rights) {
402 WorkAuth *a = ctx;
403 if (rights == RX_GEN_RIGHT_PROMOTE)
404 return resource == RX_GEN_RES_PROMOTION ? 0 : -1;
405 if (rights != RX_GEN_RIGHT_FINISH) return -1;
406 a->checks++;
407 return cap_id == a->cap_id && generation == a->generation && subject == a->worker &&
408 resource == a->resource ? 0 : -1;
409}
410
411static uint32_t work_u32(const uint8_t *p) {
412 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
413 ((uint32_t)p[3] << 24);
414}
415
416static void fill_random(uint8_t *p, size_t n) {
417 size_t k = 0;
418 while (k < n) {
419 ssize_t g = getrandom(p + k, n - k, 0);
420 if (g > 0) k += (size_t)g;
421 }
422}
423
424/* A LEGACY reaction whose body revokes LEGACY's own caller credential while
425 * the activation runs, then writes a target its real grant covers. The commit
426 * re-check must refuse the write: the identity the reaction was admitted under
427 * is gone. */
428typedef struct { Poke p; RxWorld *w; RxCallerCred cred; int revoked; } IdRevoke;
429static int fn_id_revoke(RxCtx *c) {
430 IdRevoke *f = c->user;
431 if (!f->revoked)
432 f->revoked = rx_world_revoke_caller(f->w, LEGACY_SUBJ, &f->cred) == RX_CALLER_OK;
433 c->out[c->n_out++] = (RxMutation){f->p.target, f->p.field, 0xBADBADull};
434 return 0;
435}
436
437static const char *caller_err(int rc) {
438 switch (rc) {
439 case RX_CALLER_OK: return "OK";
440 case RX_CALLER_ERR_ABSENT: return "ABSENT";
441 case RX_CALLER_ERR_UNKNOWN: return "UNKNOWN";
442 case RX_CALLER_ERR_REVOKED: return "REVOKED";
443 case RX_CALLER_ERR_STALE: return "STALE";
444 case RX_CALLER_ERR_FORGED: return "FORGED";
445 case RX_CALLER_ERR_CLOSED: return "CLOSED";
446 case RX_CALLER_ERR_EXISTS: return "EXISTS";
447 case RX_CALLER_ERR_FULL: return "FULL";
448 default: return "other";
449 }
450}
451/* One identity probe: rc must equal want. */
452static void expect(Tally *t, long long rc, long long want, const char *what) {
453 t->tried++;
454 if (rc == want) { t->refused++; return; }
455 printf("R16 G4 FAIL: identity: %s: rc %lld (%s), expected %lld (%s)\n", what, rc,
456 caller_err((int)rc), want, caller_err((int)want));
457 g_fail++;
458}
459
460static void st32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
461static void st64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
462
463static OmegaSharedWorldDesc publication(RxWorld *w, const RxObject *o, RxCapRef cap) {
464 OmegaSharedWorldDesc d;
465 memset(&d, 0, sizeof d);
466 d.msg_type = RX_RING_PUBLISH;
467 d.sequence = rx_world_publication_tail(w);
468 d.world_epoch = w->world_epoch;
469 d.object_id = o->id;
470 d.object_generation = o->generation;
471 d.object_length = (uint32_t)o->size_bytes;
472 d.payload_len = RX_CAP_PAYLOAD;
473 st32(d.payload, cap.cap_id);
474 st32(d.payload + 4, (uint32_t)cap.generation);
475 st32(d.payload + RX_CAP_GEN_HI_A, (uint32_t)(cap.generation >> 32));
476 st64(d.payload + 8, o->version);
477 st64(d.payload + 16, rx_world_explain(w, (RxObjRef){o->id, o->generation}, 0));
478 rx_world_seal_descriptor(&d);
479 return d;
480}
481
482/* ---- R16 C7: outside review of C6 -----------------------------------------
483 * Three gaps an outside review found in C6, each reproduced on scratch
484 * worlds and stores so the rig's own state is untouched:
485 * P1 the promotion checked identity only on entry; a revocation in the
486 * live barrier or during the disk writes still flipped the active
487 * pointer. The flip must re-check and refuse.
488 * P2 revocation took only the caller lock, so it could land between a
489 * commit's identity check and its publish. It must wait for the world
490 * lock: a revocation either precedes the check or follows the publish.
491 * P3 an unbound world kept any caller generation a reaction claimed, and
492 * generations are sequential, so a reaction registered before binding
493 * with a guessed generation ran under that subject after binding.
494 * Plus the bound store refuses the credential-free candidate edits.
495 * P4 (second round, below) the work-accounting calls took no credential. */
496static int permissive_world(const void *ctx, RxCapRef ref, uint32_t subject, uint64_t resource,
497 uint32_t rights, RxCapEntry *out) {
498 (void)ctx; (void)ref; (void)subject; (void)resource; (void)rights;
499 if (out) memset(out, 0, sizeof *out);
500 return RX_CAP_OK;
501}
502static int fn_noop(RxCtx *c) { (void)c; return 0; }
503
504static RxWorld *scratch_world(RxAuthValidateFn validate, const void *ctx) {
505 RxWorld *w = calloc(1, sizeof *w);
506 if (w && rx_world_init_with_auth(w, NULL, ctx, validate, NULL, 1, 1024) != RX_OK) {
507 free(w);
508 return NULL;
509 }
510 return w;
511}
512static void scratch_world_free(RxWorld *w) {
513 if (w) { rx_world_destroy(w); free(w); }
514}
515
516typedef struct { RxWorld *w; uint32_t subject; RxCallerCred cred; int rc, fired; } FlipRevoke;
517static void flip_revoke_live(void *ctx) {
518 FlipRevoke *f = ctx;
519 if (f->fired) return;
520 f->fired = 1;
521 f->rc = rx_world_revoke_caller(f->w, f->subject, &f->cred);
522}
523static void flip_revoke_disk(const char *dir, void *ctx) { (void)dir; flip_revoke_live(ctx); }
524static void count_live(void *ctx) { (*(int *)ctx)++; }
525
526static void probe_flip(Tally *t) {
527 enum { PROP = 7101, PRO_A = 7102, PRO_B = 7103, PRO_C = 7104 };
528 static RxCallerCred prop, pa, pb, pc;
529 char dir[64];
530 RxGenStore *g = NULL;
531 RxWorld *w = scratch_world(permissive_world, NULL);
532 int ok = w && rx_world_enroll_caller(w, PROP, &prop) == RX_CALLER_OK &&
533 rx_world_enroll_caller(w, PRO_A, &pa) == RX_CALLER_OK &&
534 rx_world_enroll_caller(w, PRO_B, &pb) == RX_CALLER_OK &&
535 rx_world_enroll_caller(w, PRO_C, &pc) == RX_CALLER_OK &&
536 rx_world_bind_callers(w) == RX_OK &&
537 mkdtemp(strcpy(dir, "/tmp/r16-review-XXXXXX")) &&
538 rx_gen_open(dir, &g) == RX_GEN_OK &&
539 rx_gen_bind_authority(g, rx_world_caller_check_fn, w, permissive_auth, NULL) ==
540 RX_GEN_OK;
541 expect(t, ok, 1, "C7 P1: scratch world and bound store");
542 if (!ok) goto out;
543 static const uint8_t junk[] = "r16 review";
544 RxGenObject obj = {1, 1, {0}};
545 RxGenDraft gd;
546 memset(&gd, 0, sizeof gd);
547 gd.proofs_ok = 1;
548 gd.n_objects = 1; gd.objects = &obj;
549 gd.evidence = junk; gd.evidence_len = sizeof junk - 1;
550 { /* a forged promoter is refused on entry: no barrier, no disk work */
551 uint64_t cand = 0;
552 int lives = 0;
553 int prc = rx_gen_propose_as(g, PROP, &prop, &gd, &cand);
554 RxPromotionRequest req = {cand, PRO_A, 1, 1, RX_GEN_RES_PROMOTION,
555 RX_GEN_RIGHT_PROMOTE, {pa.generation, {0}}};
556 fill_random(req.caller.secret, sizeof req.caller.secret);
557 int rc = prc != RX_GEN_OK ? prc
558 : rx_gen_promote(g, &req, NULL, NULL, NULL, NULL, count_live, &lives);
559 expect(t, rc == RX_GEN_ERR_IDENTITY && lives == 0, 1,
560 "C7 P1: forged promoter refused before the live barrier");
561 }
562 for (int v = 0; v < 3; v++) {
563 uint64_t cand = 0, a0 = 0, l0 = 0, a1 = 0, l1 = 0;
564 int prc = rx_gen_propose_as(g, PROP, &prop, &gd, &cand);
565 FlipRevoke f = {w, v == 0 ? PRO_A : v == 1 ? PRO_B : PRO_C,
566 v == 0 ? pa : v == 1 ? pb : pc, -99, 0};
567 if (v == 2) { /* the credential-free candidate edits, on a bound store */
568 uint8_t dg[32] = {0};
569 expect(t, rx_gen_mutate_object(g, cand, 0, 2, dg), RX_GEN_ERR_IDENTITY,
570 "C7: rx_gen_mutate_object on a bound store");
571 expect(t, rx_gen_set_evidence(g, cand, junk, 3), RX_GEN_ERR_IDENTITY,
572 "C7: rx_gen_set_evidence on a bound store");
573 }
574 rx_gen_active(g, &a0, &l0);
575 RxPromotionRequest req = {cand, f.subject, 1, 1, RX_GEN_RES_PROMOTION,
576 RX_GEN_RIGHT_PROMOTE, {0, {0}}};
577 req.caller = f.cred;
578 if (v == 1) rx_gen_set_disk_hook(g, flip_revoke_disk, &f);
579 int rc = prc != RX_GEN_OK ? prc
580 : rx_gen_promote(g, &req, NULL, NULL, NULL, NULL,
581 v == 0 ? flip_revoke_live : NULL, v == 0 ? &f : NULL);
582 if (v == 1) rx_gen_set_disk_hook(g, NULL, NULL);
583 rx_caller_wipe(&req.caller);
584 rx_gen_active(g, &a1, &l1);
585 RxRecoveryRecord d;
586 memset(&d, 0, sizeof d);
587 rx_gen_recover(dir, &d);
588 if (v < 2) {
589 const char *where = v == 0 ? "the live barrier" : "the disk writes";
590 char what[160];
591 snprintf(what, sizeof what, "C7 P1: revoke the promoter during %s", where);
592 expect(t, f.rc, RX_CALLER_OK, what);
593 snprintf(what, sizeof what, "C7 P1: promotion whose promoter was revoked during %s",
594 where);
595 expect(t, rc, RX_GEN_ERR_IDENTITY, what);
596 expect(t, a1 == a0 && l1 == l0 && d.active_id == a0, 1,
597 "C7 P1: active generation unchanged, in memory and on disk");
598 printf("R16 C7 P1 revoke during %s: promote rc %d; generation %llu -> %llu, "
599 "on disk %llu\n", where, rc, U(a0), U(a1), U(d.active_id));
600 } else {
601 expect(t, rc == RX_GEN_OK && a1 == cand && d.active_id == cand, 1,
602 "C7 P1 control: a live promoter still promotes");
603 printf("R16 C7 P1 control: promote rc %d; generation %llu -> %llu, on disk %llu\n",
604 rc, U(a0), U(a1), U(d.active_id));
605 }
606 }
607out:
608 if (g) rx_gen_close(g);
609 scratch_world_free(w);
610 rx_caller_wipe(&prop); rx_caller_wipe(&pa); rx_caller_wipe(&pb); rx_caller_wipe(&pc);
611}
612
613typedef struct {
614 RxWorld *w;
615 uint32_t subject;
616 RxCallerCred cred;
617 RxObjRef target;
618 atomic_int fn_ran, spawned, done;
619 int revoke_rc, revoked_during, joined;
620 pthread_t th;
621} Race;
622static void *race_revoker(void *arg) {
623 Race *r = arg;
624 r->revoke_rc = rx_world_revoke_caller(r->w, r->subject, &r->cred);
625 atomic_store(&r->done, 1);
626 return NULL;
627}
628/* Runs inside the commit's capability check, after its identity check, with
629 * the world lock held: starts a revocation and gives it 300 ms to land. */
630static int race_validate(const void *ctx, RxCapRef ref, uint32_t subject, uint64_t resource,
631 uint32_t rights, RxCapEntry *out) {
632 Race *r = (Race *)ctx;
633 if (subject == r->subject && atomic_load(&r->fn_ran) && !atomic_exchange(&r->spawned, 1) &&
634 pthread_create(&r->th, NULL, race_revoker, r) == 0) {
635 r->joined = 1;
636 struct timespec ts = {0, 300000000}; /* one fixed window, no polling */
637 nanosleep(&ts, NULL);
638 r->revoked_during = atomic_load(&r->done);
639 }
640 return permissive_world(ctx, ref, subject, resource, rights, out);
641}
642static int fn_race(RxCtx *c) {
643 Race *r = c->user;
644 atomic_store(&r->fn_ran, 1);
645 c->out[c->n_out++] = (RxMutation){r->target, 0, 0xBADBADull};
646 return 0;
647}
648
649static void probe_race(Tally *t) {
650 enum { RACER = 7201, OUTSIDE = 7299 };
651 static Race race;
652 memset(&race, 0, sizeof race);
653 RxWorld *w = scratch_world(race_validate, &race);
654 RxObjRef trig = {0, 0};
655 uint64_t z[RX_MAX_FIELDS] = {0};
656 uint32_t id = 0;
657 int ok = w && rx_world_enroll_caller(w, RACER, &race.cred) == RX_CALLER_OK &&
658 rx_world_bind_callers(w) == RX_OK &&
659 rx_world_create(w, 1, RX_PERSIST_RESIDENT, 0x7201, z, &trig) == RX_OK &&
660 rx_world_create(w, 1, RX_PERSIST_RESIDENT, 0x7202, z, &race.target) == RX_OK;
661 race.w = w;
662 race.subject = RACER;
663 if (ok) {
664 w->external_subject = OUTSIDE;
665 RxReactionDesc d;
666 memset(&d, 0, sizeof d);
667 d.name = "c7.race";
668 d.faculty = RX_FACULTY_EXTERNAL;
669 d.subject = RACER;
670 d.caller = race.cred;
671 d.priority = RX_PRIO_FOREGROUND;
672 d.fn = fn_race;
673 d.user = &race;
674 d.n_triggers = 1;
675 d.triggers[0] = (RxDep){trig, RX_FIELD(0)};
676 d.n_writes = 1;
677 d.writes[0] = (RxDep){race.target, RX_FIELD(0)};
678 d.n_caps = 2;
679 d.caps[0] = (RxCapNeed){(RxCapRef){1, 1}, 0x7201, RX_RIGHT_READ};
680 d.caps[1] = (RxCapNeed){(RxCapRef){2, 1}, 0x7202, RX_RIGHT_WRITE};
681 ok = rx_world_add_reaction(w, &d, &id) == RX_OK;
682 rx_caller_wipe(&d.caller);
683 }
684 expect(t, ok, 1, "C7 P2: scratch world with an admitted reaction");
685 if (!ok) goto out;
686 RxMutation m = {trig, 0, 1};
687 int woke = rx_world_publish_external(w, (RxCapRef){3, 1}, &m, 1) > 0 &&
688 rx_world_wait_quiescent(w, 10000) == RX_OK;
689 int join_rc = race.joined ? pthread_join(race.th, NULL) : 0;
690 expect(t, join_rc, 0, "C7 P2: join the revocation thread");
691 pthread_mutex_lock(&w->mu);
692 uint64_t commits1 = w->reactions[id].commits;
693 pthread_mutex_unlock(&w->mu);
694 expect(t, woke && atomic_load(&race.spawned), 1,
695 "C7 P2: revocation started inside the commit's check");
696 expect(t, race.revoke_rc, RX_CALLER_OK, "C7 P2: the racing revocation itself");
697 expect(t, race.revoked_during && commits1 > 0, 0,
698 "C7 P2: a revocation completed between the commit's identity check and its publish");
699 printf("R16 C7 P2 revoke racing the commit: revocation finished inside the check window: %s;"
700 " commits %llu\n", race.revoked_during ? "yes" : "no (it waited)", U(commits1));
701 /* after the revocation: woken again, blocked */
702 m.value = 2;
703 woke = rx_world_publish_external(w, (RxCapRef){3, 1}, &m, 1) > 0 &&
704 rx_world_wait_quiescent(w, 10000) == RX_OK;
705 pthread_mutex_lock(&w->mu);
706 RxReaction rr = w->reactions[id];
707 const RxCrumb *k = rx_world_crumb(w, rr.last_crumb);
708 int blocked = k && k->kind == RX_CRUMB_BLOCKED_AUTHORITY;
709 pthread_mutex_unlock(&w->mu);
710 expect(t, woke && blocked && rr.commits == commits1, 1,
711 "C7 P2: activation after the revocation is blocked");
712out:
713 scratch_world_free(w);
714 rx_caller_wipe(&race.cred);
715}
716
717static int prebind_reaction(RxWorld *w, uint32_t subject, const RxCallerCred *cred, RxObjRef trig) {
718 RxReactionDesc d;
719 memset(&d, 0, sizeof d);
720 d.name = "c7.prebind";
721 d.faculty = RX_FACULTY_EXTERNAL;
722 d.subject = subject;
723 if (cred) d.caller = *cred;
724 d.priority = RX_PRIO_FOREGROUND;
725 d.fn = fn_noop;
726 d.n_triggers = 1;
727 d.triggers[0] = (RxDep){trig, RX_FIELD(0)};
728 d.n_caps = 1;
729 d.caps[0] = (RxCapNeed){(RxCapRef){1, 1}, 0x7301, RX_RIGHT_READ};
730 uint32_t id = 0;
731 int rc = rx_world_add_reaction(w, &d, &id);
732 rx_caller_wipe(&d.caller);
733 return rc;
734}
735
736static void probe_prebind(Tally *t) {
737 enum { VICTIM = 7301 };
738 for (int v = 0; v < 2; v++) {
739 static RxCallerCred cred;
740 RxWorld *w = scratch_world(permissive_world, NULL);
741 RxObjRef trig = {0, 0};
742 uint64_t z[RX_MAX_FIELDS] = {0};
743 int ok = w && rx_world_enroll_caller(w, VICTIM, &cred) == RX_CALLER_OK &&
744 rx_world_create(w, 1, RX_PERSIST_RESIDENT, 0x7301, z, &trig) == RX_OK;
745 expect(t, ok, 1, "C7 P3: unbound scratch world");
746 if (ok && v == 0) {
747 /* generations are sequential: a guessed one with no secret, or a
748 * random secret, must not be admitted before binding either */
749 RxCallerCred guess = {cred.generation, {0}};
750 expect(t, prebind_reaction(w, VICTIM, &guess, trig), RX_ERR_IDENTITY,
751 "C7 P3: pre-bind registration, guessed generation and zero secret");
752 fill_random(guess.secret, sizeof guess.secret);
753 expect(t, prebind_reaction(w, VICTIM, &guess, trig), RX_ERR_IDENTITY,
754 "C7 P3: pre-bind registration, guessed generation and random secret");
755 rx_caller_wipe(&guess);
756 expect(t, prebind_reaction(w, VICTIM, &cred, trig), RX_OK,
757 "C7 P3: pre-bind registration with the issued credential");
758 expect(t, rx_world_bind_callers(w), RX_OK,
759 "C7 P3: bind with only authenticated reactions");
760 }
761 if (ok && v == 1) {
762 /* an unauthenticated registration is allowed while unbound (the
763 * worlds that never bind), but then the world cannot bind */
764 expect(t, prebind_reaction(w, VICTIM, NULL, trig), RX_OK,
765 "C7 P3: unauthenticated registration on an unbound world");
766 expect(t, rx_world_bind_callers(w), RX_ERR_IDENTITY,
767 "C7 P3: bind with an unauthenticated reaction registered");
768 pthread_mutex_lock(&w->mu);
769 int bound = w->callers_bound;
770 pthread_mutex_unlock(&w->mu);
771 expect(t, bound, 0, "C7 P3: the refused bind left the world unbound");
772 }
773 scratch_world_free(w);
774 rx_caller_wipe(&cred);
775 }
776}
777
778/* R16 C7 P4 (second-round review): the work-accounting calls. On a bound store
779 * rx_gen_finish_work and rx_gen_add_work took no credential, and add_work
780 * stored the caller's work state verbatim, so anyone could complete, or
781 * insert already done, a required evidence item and make a promotion succeed
782 * that classify would refuse. The before run of this probe was built with
783 * -DR16_BEFORE_WORK against the runtime without the _as calls. */
784#ifdef R16_BEFORE_WORK
785#define rx_gen_add_work_as(s, subj, cred, c, wk) rx_gen_add_work(s, c, wk)
786#define rx_gen_finish_work_as(s, subj, cred, c, id) rx_gen_finish_work(s, c, id)
787#endif
788static int drain_never(uint64_t work_id, void *ctx) {
789 (void)work_id;
790 (*(int *)ctx)++;
791 return -1;
792}
793
794static void probe_work(Tally *t) {
795 enum { PROP = 7401, PRO = 7402, WORKER = 7403 };
796 static RxCallerCred prop, pro, worker;
797 WorkAuth auth = {0};
798 char dir[64];
799 RxGenStore *g = NULL;
800 RxWorld *w = scratch_world(permissive_world, NULL);
801 int ok = w && rx_world_enroll_caller(w, PROP, &prop) == RX_CALLER_OK &&
802 rx_world_enroll_caller(w, PRO, &pro) == RX_CALLER_OK &&
803 rx_world_enroll_caller(w, WORKER, &worker) == RX_CALLER_OK &&
804 rx_world_bind_callers(w) == RX_OK &&
805 mkdtemp(strcpy(dir, "/tmp/r16-work-XXXXXX")) &&
806 rx_gen_open(dir, &g) == RX_GEN_OK &&
807 rx_gen_bind_authority(g, rx_world_caller_check_fn, w, work_auth, &auth) ==
808 RX_GEN_OK;
809 expect(t, ok, 1, "C7 P4: scratch world and bound store");
810 if (!ok) goto out;
811 static const uint8_t junk[] = "r16 work";
812 RxGenObject obj = {1, 1, {0}};
813 RxGenDraft gd;
814 memset(&gd, 0, sizeof gd);
815 gd.proofs_ok = 1;
816 gd.n_objects = 1; gd.objects = &obj;
817 gd.evidence = junk; gd.evidence_len = sizeof junk - 1;
818 RxCallerCred forged = {prop.generation, {0}};
819 fill_random(forged.secret, sizeof forged.secret);
820 /* v0 baseline: an unfinished required item refuses the promotion.
821 * v1 attack: nobody completes it with the bare call.
822 * v2 attack: the proposer inserts it already done.
823 * v3 attack: bare, forged, cross-subject and cross-work-capability calls.
824 * v4 control: proposer-owned and explicitly delegated work both complete.
825 * The control runs first: a refused candidate keeps its slot (4 in all). */
826 static const int order[5] = {4, 0, 1, 2, 3};
827 for (int k = 0; k < 5; k++) {
828 int v = order[k];
829 uint64_t cand = 0, a0 = 0, l0 = 0, a1 = 0, l1 = 0;
830 int drains = 0;
831 int prc = rx_gen_propose_as(g, PROP, &prop, &gd, &cand);
832 RxGenWork item = {.id = 100 + (uint64_t)v, .class = RX_WORK_EVIDENCE,
833 .state = RX_WORK_PENDING, .required = 1};
834 item.finish_cap_id = 900 + (uint32_t)v;
835 item.finish_cap_generation = 70 + (uint64_t)v;
836 uint64_t item_resource = 0;
837 int rrc = prc == RX_GEN_OK
838 ? rx_gen_work_resource(g, cand, item.id, &item_resource) : prc;
839 auth.cap_id = item.finish_cap_id;
840 auth.generation = item.finish_cap_generation;
841 auth.worker = PROP;
842 auth.resource = item_resource;
843 int arc = RX_GEN_OK, frc = RX_GEN_OK;
844 if (prc == RX_GEN_OK && v != 2) arc = rx_gen_add_work_as(g, PROP, &prop, cand, &item);
845 expect(t, prc == RX_GEN_OK && rrc == RX_GEN_OK && arc == RX_GEN_OK, 1,
846 "C7 P4: propose, scope and add the item");
847 if (v == 1) {
848 frc = rx_gen_finish_work(g, cand, item.id);
849 expect(t, frc, RX_GEN_ERR_IDENTITY,
850 "C7 P4: rx_gen_finish_work (no credential) on a bound store");
851 } else if (v == 2) {
852 RxGenWork done = item;
853 done.state = RX_WORK_DONE;
854 expect(t, rx_gen_add_work_as(g, PROP, &prop, cand, &done), RX_GEN_ERR_ARG,
855 "C7 P4: a required evidence item added already done");
856 done.state = RX_WORK_CANCELLED;
857 expect(t, rx_gen_add_work_as(g, PROP, &prop, cand, &done), RX_GEN_ERR_ARG,
858 "C7 P4: an item added already cancelled");
859 /* the item itself, so the promotion below still needs it */
860 expect(t, rx_gen_add_work_as(g, PROP, &prop, cand, &item), RX_GEN_OK,
861 "C7 P4: the item added pending");
862 } else if (v == 3) {
863 RxGenWork more = {.id = 200, .class = RX_WORK_EVIDENCE,
864 .state = RX_WORK_PENDING, .required = 0};
865 more.finish_cap_id = item.finish_cap_id;
866 more.finish_cap_generation = item.finish_cap_generation;
867 uint64_t more_resource = 0;
868 expect(t, rx_gen_work_resource(g, cand, more.id, &more_resource) == RX_GEN_OK &&
869 more_resource != item_resource,
870 1, "C7 P4: derive a distinct sibling work resource");
871 expect(t, rx_gen_add_work(g, cand, &more), RX_GEN_ERR_IDENTITY,
872 "C7 P4: rx_gen_add_work (no credential) on a bound store");
873 expect(t, rx_gen_add_work_as(g, PRO, &pro, cand, &more), RX_GEN_ERR_AUTHORITY,
874 "C7 P4: another subject adds work to the candidate");
875 expect(t, rx_gen_add_work_as(g, PROP, &prop, cand, &more), RX_GEN_ERR_AUTHORITY,
876 "C7 P4: a capability for one work id is reused for another");
877 expect(t, rx_gen_finish_work_as(g, PRO, &pro, cand, item.id),
878 RX_GEN_ERR_AUTHORITY, "C7 P4: another subject finishes the proposer's work");
879 expect(t, rx_gen_add_work_as(g, PROP, &forged, cand, &more), RX_GEN_ERR_IDENTITY,
880 "C7 P4: rx_gen_add_work_as with a forged credential");
881 expect(t, rx_gen_finish_work_as(g, PROP, &forged, cand, item.id),
882 RX_GEN_ERR_IDENTITY, "C7 P4: rx_gen_finish_work_as with a forged credential");
883 } else if (v == 4) {
884 frc = rx_gen_finish_work_as(g, PROP, &prop, cand, item.id);
885 expect(t, frc, RX_GEN_OK, "C7 P4 control: the proposer completes its item");
886 RxGenWork delegated = {.id = 204, .class = RX_WORK_EVIDENCE,
887 .state = RX_WORK_PENDING, .required = 1};
888 delegated.worker = WORKER;
889 delegated.worker_set = 1;
890 delegated.finish_cap_id = 1204;
891 delegated.finish_cap_generation = 94;
892 uint64_t delegated_resource = 0;
893 expect(t, rx_gen_work_resource(g, cand, delegated.id, &delegated_resource),
894 RX_GEN_OK, "C7 P4 control: derive the delegated work resource");
895 auth.cap_id = delegated.finish_cap_id;
896 auth.generation = delegated.finish_cap_generation;
897 auth.worker = WORKER;
898 auth.resource = delegated_resource;
899 expect(t, rx_gen_add_work_as(g, PROP, &prop, cand, &delegated), RX_GEN_OK,
900 "C7 P4 control: proposer delegates its required work");
901 expect(t, rx_gen_finish_work_as(g, WORKER, &worker, cand, delegated.id), RX_GEN_OK,
902 "C7 P4 control: delegated worker completes its item");
903 }
904 rx_gen_active(g, &a0, &l0);
905 RxPromotionRequest req = {cand, PRO, 1, 1, RX_GEN_RES_PROMOTION,
906 RX_GEN_RIGHT_PROMOTE, {0, {0}}};
907 req.caller = pro;
908 int rc = rx_gen_promote(g, &req, NULL, NULL, drain_never, &drains, NULL, NULL);
909 rx_caller_wipe(&req.caller);
910 rx_gen_active(g, &a1, &l1);
911 static const char *const name[5] = {"baseline, item unfinished",
912 "item completed with no credential",
913 "item offered already done",
914 "bare, forged and cross-subject work calls",
915 "control, proposer and delegate complete their items"};
916 char what[160];
917 if (v < 4) {
918 snprintf(what, sizeof what, "C7 P4: promotion, %s", name[v]);
919 expect(t, rc, RX_GEN_ERR_VERIFY, what);
920 expect(t, a1 == a0, 1, "C7 P4: active generation unchanged");
921 } else {
922 expect(t, rc == RX_GEN_OK && a1 == cand, 1, "C7 P4 control: promotion succeeds");
923 uint8_t work_blob[2048] = {0}, digest[32];
924 size_t work_len = 0;
925 int wrc = rx_gen_read_blob(g, cand, "work", work_blob, sizeof work_blob, &work_len);
926 sha256_hash(junk, sizeof junk - 1, digest);
927 const uint8_t *own = work_blob + 16;
928 const uint8_t *deleg = own + 80;
929 expect(t, wrc == RX_GEN_OK && work_len == 176 &&
930 memcmp(work_blob, "R9WORK1", 7) == 0 && work_u32(work_blob + 8) == 2,
931 1, "C7 P4 control: root-committed work provenance blob");
932 expect(t, work_u32(own + 20) == PROP && work_u32(own + 28) == PROP &&
933 memcmp(own + 48, digest, sizeof digest) == 0,
934 1, "C7 P4 control: proposer completion provenance");
935 expect(t, work_u32(deleg + 20) == WORKER && work_u32(deleg + 28) == WORKER &&
936 memcmp(deleg + 48, digest, sizeof digest) == 0,
937 1, "C7 P4 control: delegated completion provenance");
938 expect(t, auth.checks >= 4, 1,
939 "C7 P4 control: work capabilities checked at assignment and completion");
940 }
941 printf("R16 C7 P4 %s: promote rc %d; generation %llu -> %llu; drain calls %d\n",
942 name[v], rc, U(a0), U(a1), drains);
943 }
944out:
945 if (g) rx_gen_close(g);
946 scratch_world_free(w);
947 rx_caller_wipe(&prop); rx_caller_wipe(&pro); rx_caller_wipe(&worker);
948}
949
950static int review_probes(void) {
951 Tally t = {0, 0};
952 uint32_t f0 = (uint32_t)g_fail;
953 probe_flip(&t);
954 uint32_t f1 = (uint32_t)g_fail;
955 probe_race(&t);
956 uint32_t f2 = (uint32_t)g_fail;
957 probe_prebind(&t);
958 uint32_t f3 = (uint32_t)g_fail;
959 probe_work(&t);
960 uint32_t f4 = (uint32_t)g_fail;
961 int ok = t.tried > 0 && t.refused == t.tried;
962 printf("R16 C7 review probes: P1 flip %s, P2 revoke race %s, P3 pre-bind %s, "
963 "P4 work accounting %s; %u/%u as expected %s\n", f1 == f0 ? "pass" : "FAIL",
964 f2 == f1 ? "pass" : "FAIL", f3 == f2 ? "pass" : "FAIL", f4 == f3 ? "pass" : "FAIL",
965 t.refused, t.tried, ok ? "PASS" : "FAIL");
966 return ok;
967}
968
969int main(void) {
970 R15Rig *r = calloc(1, sizeof *r);
971 static const uint32_t fixtures[2] = {LEGACY_SUBJ, LEGACY2_SUBJ};
972 if (!r || r15_start_with(r, R15_RES4, fixtures, 2) != 0) {
973 printf("R16 G4 FAIL: production body did not start (stage %d%s%s)\n", r ? r->stage : 0,
974 r && r->stage_why ? ": " : "", r && r->stage_why ? r->stage_why : "");
975 printf("R16 gate: R16_G4_LEGACY_REFUSED=FAIL\n");
976 return 1;
977 }
978 aienos_cap_set_observer(r->admin, observer, NULL);
979
980 /* ---- fixtures (harness authority; before the acts) ---- */
981 Legacy L;
982 memset(&L, 0, sizeof L);
983 L.w = &r->w;
984 L.view = r->view;
985 L.gen = r->gen;
986 L.keys = &r->fixtures;
987 L.stolen_goal = r->ext_goal;
988 L.borrowed_inforce = r->promoter.inforce_write;
989 L.borrowed_promote = r->promoter.promotion_authority;
990 L.forged = (RxCapRef){777, 3};
991 L.own_scratch = r15_mint(r, LEGACY_SUBJ, RES_LEGACY_SCRATCH, RX_RIGHT_READ | RX_RIGHT_WRITE);
992 L.own_promote = r15_mint(r, LEGACY_SUBJ, RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE);
993 L.own2_read_promote = r15_mint(r, LEGACY2_SUBJ, RX_GEN_RES_PROMOTION, RX_RIGHT_READ);
994 L.ext_scratch = r15_mint(r, EXTERNAL_SUBJ, RES_LEGACY_SCRATCH, RX_RIGHT_WRITE);
995 L.office = r->admin;
996 RxObject belief_obj, output_obj;
997 int fx0 = rx_world_read(&r->w, r->aien.o.belief, &belief_obj) == RX_OK &&
998 rx_world_read(&r->w, r->living.o.output, &output_obj) == RX_OK;
999 L.inflight_belief = r15_mint(r, LEGACY_SUBJ, belief_obj.resource, RX_RIGHT_WRITE);
1000 L.own_output = r15_mint(r, LEGACY_SUBJ, output_obj.resource, RX_RIGHT_WRITE);
1001 uint64_t init[RX_MAX_FIELDS] = {0};
1002 int fx = fx0 && rx_world_create(&r->w, 0x7160, RX_PERSIST_EPHEMERAL, RES_LEGACY_SCRATCH, init,
1003 &L.scratch) == RX_OK;
1004 fx = fx && rx_world_create(&r->w, 0x7161, RX_PERSIST_EPHEMERAL, RES_LEGACY_SCRATCH, init,
1005 &L.scratch2) == RX_OK;
1006 RxGenDraft gd;
1007 memset(&gd, 0, sizeof gd);
1008 static const uint8_t junk[] = "legacy";
1009 gd.proofs_ok = 1;
1010 gd.evidence = junk; gd.evidence_len = sizeof junk - 1;
1011 gd.realization = junk; gd.realization_len = sizeof junk - 1;
1012 fx = fx && rx_gen_propose_as(r->gen, LEGACY_SUBJ, rx_caller_find(L.keys, LEGACY_SUBJ), &gd,
1013 &L.candidate) == RX_GEN_OK;
1014 RxCapRef honest_output = r->seat_output;
1015 CHECK(fx, "fixtures could not be built");
1016
1017 Snap s0;
1018 memset(&s0, 0, sizeof s0);
1019 watch(&s0, "aien.belief", r->aien.o.belief);
1020 watch(&s0, "aien.experiment_belief", r->aien.o.experiment_belief);
1021 watch(&s0, "aien.prediction", r->aien.o.prediction);
1022 watch(&s0, "aien.plan", r->aien.o.plan);
1023 watch(&s0, "aien.assessment", r->aien.o.assessment);
1024 watch(&s0, "omega.selection", r->omega.o.selection);
1025 watch(&s0, "omega.search", r->omega.o.search);
1026 watch(&s0, "omega.result", r->omega.o.result);
1027 watch(&s0, "living.candidate", r->living.o.candidate);
1028 watch(&s0, "living.promotion", r->living.o.promotion);
1029 watch(&s0, "living.inforce", r->living.o.inforce);
1030 watch(&s0, "living.evidence", r->living.o.evidence);
1031 watch(&s0, "living.output", r->living.o.output);
1032 CHECK(take(r, &s0) == 0, "baseline snapshot failed");
1033 Snap s = s0;
1034
1035 Tally t[7];
1036 memset(t, 0, sizeof t);
1037
1038 /* (1) write authoritative AIEN belief */
1039 write_attempts(&L, "(1) belief", r->aien.o.belief, L.borrowed_inforce,
1040 "outside publication with the promoter's in-force grant", &t[1]);
1041 write_attempts(&L, "(1) experiment belief", r->aien.o.experiment_belief, L.borrowed_inforce,
1042 "outside publication with the promoter's in-force grant", &t[1]);
1043 revoked_in_flight(&L, "(1) belief", r->aien.o.belief, &t[1]);
1044 CHECK(take(r, &s) == 0 && same(&s0, &s, "(1)"), "(1) authoritative state moved");
1045
1046 /* (2) select an Omega realization */
1047 write_attempts(&L, "(2) selection", r->omega.o.selection, L.borrowed_inforce,
1048 "outside publication with the promoter's in-force grant", &t[2]);
1049 CHECK(take(r, &s) == 0 && same(&s0, &s, "(2)"), "(2) authoritative state moved");
1050
1051 /* (3) mint or revoke authority through the view it holds */
1052 {
1053 AienosCapRef office = {0, 0};
1054 aienos_cap_office(r->admin, &office); /* a reference value, guessable */
1055 AienosCapMint m = {3, LEGACY_SUBJ, 0, 0, 0, {UINT32_MAX, 0}, office};
1056 RxObject bo;
1057 rx_world_read(&r->w, r->aien.o.belief, &bo);
1058 m.resource = bo.resource;
1059 m.rights = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_MINT;
1060 uint8_t guessed[32];
1061 memset(guessed, 0, sizeof guessed);
1062 int rc = aienos_cap_cognition_mint(L.view, &m, guessed);
1063 tally(&t[3], rc != 0, "(3) mint", "cognition mint with a guessed token", rc);
1064 rc = aienos_cap_cognition_mint(L.view, &m, NULL);
1065 tally(&t[3], rc != 0, "(3) mint", "cognition mint with no token", rc);
1066 rc = aienos_cap_cognition_admin(L.view, 2, office, (AienosCapRef){r->ext_goal.cap_id,
1067 r->ext_goal.generation});
1068 tally(&t[3], rc != 0, "(3) mint", "cognition revoke of the external goal grant", rc);
1069 CHECK(rx_world_validate_cap(&r->w, r->ext_goal, EXTERNAL_SUBJ,
1070 RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, RX_RIGHT_WRITE,
1071 NULL) == RX_CAP_OK, "(3) the external goal grant was revoked");
1072 }
1073 CHECK(take(r, &s) == 0 && same(&s0, &s, "(3)"), "(3) authoritative state moved");
1074
1075 /* (4) promote a generation (the legacy draft) */
1076 {
1077 typedef struct { uint32_t subject; RxCapRef cap; uint32_t rights; const char *what; } Try;
1078 Try tries[4] = {
1079 {LEGACY_SUBJ, L.own_promote, RX_GEN_RIGHT_PROMOTE,
1080 "self-promotion with a real promotion grant"},
1081 {LEGACY2_SUBJ, L.borrowed_promote, RX_GEN_RIGHT_PROMOTE,
1082 "promotion with the promoter's grant, another subject"},
1083 {LEGACY2_SUBJ, L.forged, RX_GEN_RIGHT_PROMOTE, "promotion with a forged reference"},
1084 {LEGACY2_SUBJ, L.own2_read_promote, RX_RIGHT_READ,
1085 "promotion asking a READ right it really holds"},
1086 };
1087 for (int i = 0; i < 4; i++) {
1088 RxPromotionRequest req = {L.candidate, tries[i].subject, tries[i].cap.cap_id,
1089 tries[i].cap.generation, RX_GEN_RES_PROMOTION,
1090 tries[i].rights, {0, {0}}};
1091 const RxCallerCred *c = rx_caller_find(L.keys, tries[i].subject);
1092 if (c) req.caller = *c;
1093 int rc = rx_gen_promote(L.gen, &req, native_auth, (void *)L.view, NULL, NULL, NULL,
1094 NULL);
1095 tally(&t[4], rc == RX_GEN_ERR_AUTHORITY, "(4) promote", tries[i].what, rc);
1096 }
1097 }
1098 CHECK(take(r, &s) == 0 && same(&s0, &s, "(4)"), "(4) authoritative state moved");
1099
1100 /* (5) bypass the effect/authority boundary */
1101 {
1102 RxObject out;
1103 CHECK(rx_world_read(&r->w, r->living.o.output, &out) == RX_OK && out.placed,
1104 "(5) the seat output is not placed");
1105 uint32_t fault = 0;
1106 OmegaSharedWorldDesc honest = publication(&r->w, &out, honest_output);
1107 int hrc = rx_world_check_descriptor(&r->w, &honest, honest.sequence, &fault);
1108 CHECK(fault != RX_FAULT_CAP, "(5) control: the honest grant was refused (rc %d fault 0x%x)",
1109 hrc, fault);
1110 RxCapRef tries[4] = {L.forged, L.own_scratch, L.borrowed_inforce, L.own_output};
1111 const char *what[4] = {"publication descriptor with a forged reference",
1112 "publication descriptor with its own scratch grant",
1113 "publication descriptor with the promoter's in-force grant",
1114 "publication descriptor with its own real grant on the output"};
1115 for (int i = 0; i < 4; i++) {
1116 OmegaSharedWorldDesc d = publication(&r->w, &out, tries[i]);
1117 fault = 0;
1118 int rc = rx_world_check_descriptor(&r->w, &d, d.sequence, &fault);
1119 tally(&t[5], rc == RX_ERR_AUTHORITY && fault == RX_FAULT_CAP, "(5) boundary", what[i],
1120 rc);
1121 }
1122 /* Overwrite the shared physical record: the canonical object is not
1123 * moved, and the boundary then refuses the diverged object. */
1124 OmegaSharedWorldObject phys, forged_phys;
1125 CHECK(rx_world_physical(&r->w, r->living.o.output, &phys) == RX_OK, "(5) physical");
1126 forged_phys = phys;
1127 forged_phys.generation = phys.generation + 7;
1128 rx_world_overwrite_physical(&r->w, out.id, &forged_phys);
1129 RxObject after;
1130 rx_world_read(&r->w, r->living.o.output, &after);
1131 OmegaSharedWorldDesc d = publication(&r->w, &out, honest_output);
1132 fault = 0;
1133 int rc = rx_world_check_descriptor(&r->w, &d, d.sequence, &fault);
1134 tally(&t[5], after.generation == out.generation && after.version == out.version &&
1135 rc != RX_OK && fault == RX_FAULT_DIVERGED,
1136 "(5) boundary", "overwritten physical record", rc);
1137 rx_world_overwrite_physical(&r->w, out.id, &phys); /* put the projection back */
1138 }
1139 CHECK(take(r, &s) == 0 && same(&s0, &s, "(5)"), "(5) authoritative state moved");
1140
1141 /* (6) advance the world generation: the in-force record production reads,
1142 * and the R9 active generation (checked by the snapshot after every act) */
1143 write_attempts(&L, "(6) in-force", r->living.o.inforce, L.borrowed_inforce,
1144 "outside publication with the promoter's in-force grant", &t[6]);
1145 write_attempts(&L, "(6) promotion record", r->living.o.promotion, L.borrowed_inforce,
1146 "outside publication with the promoter's in-force grant", &t[6]);
1147 CHECK(take(r, &s) == 0 && same(&s0, &s, "(6)"), "(6) authoritative state moved");
1148
1149 /* Outside the six: the SEQ reference loop refuses the production world. */
1150 uint32_t order[1] = {0}, ran = 0;
1151 RxSeqPlan plan = {order, 1, NULL, NULL, 1000};
1152 int seq_rc = rx_seq_pulse(&r->w, &plan, &ran);
1153 pthread_mutex_lock(&r->w.mu);
1154 int act_rc = rx_world_seq_activate_locked(&r->w, 0, 0);
1155 pthread_mutex_unlock(&r->w.mu);
1156 int seq_refused = seq_rc == RX_ERR_ARG && act_rc == RX_ERR_ARG && ran == 0;
1157 CHECK(seq_refused, "the SEQ reference drove the production world (%d %d)", seq_rc, act_rc);
1158 CHECK(take(r, &s) == 0 && same(&s0, &s, "SEQ"), "SEQ authoritative state moved");
1159
1160 /* The body still works after the attempts: one honest production request. */
1161 uint64_t served0 = r->served;
1162 int prod = r15_producer_start(r) == 0;
1163 struct timespec ts = {0, 200000000};
1164 for (int i = 0; i < 50 && r->served == served0; i++) nanosleep(&ts, NULL);
1165 prod = r15_producer_stop(r) == 0 && prod && r->served > served0;
1166 CHECK(prod, "the production body did not serve after the attempts");
1167
1168 /* (5), last: the office revokes the seat's output grant (production is
1169 * stopped and served above), then a publication presenting the writer of
1170 * record's own, now stale, grant must be refused at the boundary. */
1171 {
1172 RxObject out;
1173 CHECK(rx_world_read(&r->w, r->living.o.output, &out) == RX_OK, "(5) output unreadable");
1174 Snap sb = s0, sa;
1175 CHECK(take(r, &sb) == 0, "(5) snapshot before the revocation failed");
1176 unsigned long long e0 = atomic_load(&g_expected_changes);
1177 AienosCapRef office = {0, 0};
1178 aienos_cap_office(r->admin, &office);
1179 unsigned long long before = atomic_load(&g_auth_changes);
1180 int rv = aienos_cap_revoke(r->admin, office,
1181 (AienosCapRef){honest_output.cap_id, honest_output.generation});
1182 atomic_fetch_add(&g_expected_changes, atomic_load(&g_auth_changes) - before);
1183 CHECK(rv == 0, "(5) the office could not revoke the seat output grant (%d)", rv);
1184 OmegaSharedWorldDesc d = publication(&r->w, &out, honest_output);
1185 uint32_t fault = 0;
1186 int rc = rx_world_check_descriptor(&r->w, &d, d.sequence, &fault);
1187 tally(&t[5], rv == 0 && rc == RX_ERR_AUTHORITY && fault == RX_FAULT_CAP, "(5) boundary",
1188 "publication descriptor with the writer's revoked grant", rc);
1189 sa = sb;
1190 CHECK(take(r, &sa) == 0 && same_x(&sb, &sa, "(5) revoked", atomic_load(&g_expected_changes) - e0),
1191 "(5) authoritative state moved");
1192 }
1193
1194 /* (4), last: promotion naming the promoter itself as the subject, with the
1195 * promoter's grant reference (which the legacy context can observe). This
1196 * is the pre-fix exploit: before R16 C5 the request's subject was a value
1197 * the caller supplied, and this promotion was accepted (generation moved in
1198 * memory and on disk). Now the request must also carry the promoter's
1199 * runtime-issued caller credential, which the legacy context does not hold;
1200 * every way it could fill that field is tried. Run last among the acts
1201 * because an accepted promotion moves the generation. A fresh draft is
1202 * proposed so the parent is current. */
1203 int probe_refused = 0;
1204 uint64_t probe_cand = 0;
1205 {
1206 const RxCallerCred *own = rx_caller_find(L.keys, LEGACY_SUBJ);
1207 const RxCallerCred *prom = rx_caller_find(&r->keys_promoter, RX_LIVING_PROMOTE_SUBJ);
1208 RxCallerCred zero, rnd, relabel, mixed;
1209 memset(&zero, 0, sizeof zero);
1210 zero.generation = prom->generation; /* generations are guessable */
1211 rnd = zero;
1212 fill_random(rnd.secret, sizeof rnd.secret);
1213 relabel = *own; /* its own real credential */
1214 mixed = *own;
1215 mixed.generation = prom->generation; /* its own secret, promoter's generation */
1216 struct { const RxCallerCred *c; RxGenAuthFn auth; const char *what; } v[6] = {
1217 {NULL, native_auth, "no credential"},
1218 {&zero, native_auth, "zero secret at the promoter's generation"},
1219 {&rnd, native_auth, "random secret at the promoter's generation"},
1220 {&relabel, native_auth, "its own real credential (LEGACY's)"},
1221 {&mixed, native_auth, "its own secret at the promoter's generation"},
1222 {&rnd, permissive_auth, "random secret with a permissive authority callback"},
1223 };
1224 uint64_t a0 = 0, l0 = 0, a1 = 0, l1 = 0;
1225 RxRecoveryRecord d0, d1;
1226 memset(&d0, 0, sizeof d0); memset(&d1, 0, sizeof d1);
1227 int prc = rx_gen_propose_as(L.gen, LEGACY_SUBJ, own, &gd, &probe_cand);
1228 CHECK(prc == RX_GEN_OK, "(4) the probe draft could not be proposed (%d)", prc);
1229 rx_gen_active(L.gen, &a0, &l0);
1230 rx_gen_recover(r->generation_dir, &d0);
1231 int refused = 0, last_rc = 0;
1232 for (int i = 0; i < 6; i++) {
1233 RxPromotionRequest req = {probe_cand, RX_LIVING_PROMOTE_SUBJ, L.borrowed_promote.cap_id,
1234 L.borrowed_promote.generation, RX_GEN_RES_PROMOTION,
1235 RX_GEN_RIGHT_PROMOTE, {0, {0}}};
1236 if (v[i].c) req.caller = *v[i].c;
1237 int rc = rx_gen_promote(L.gen, &req, v[i].auth, (void *)L.view, NULL, NULL, NULL, NULL);
1238 rx_caller_wipe(&req.caller);
1239 if (rc == RX_GEN_ERR_IDENTITY) refused++;
1240 else printf("R16 G4 FAIL: (4) promoter-subject promotion, %s: rc %d (expected "
1241 "IDENTITY %d)\n", v[i].what, rc, RX_GEN_ERR_IDENTITY);
1242 if (rc != RX_GEN_ERR_IDENTITY || i == 0) last_rc = rc;
1243 }
1244 rx_gen_active(L.gen, &a1, &l1);
1245 rx_gen_recover(r->generation_dir, &d1);
1246 probe_refused = prc == RX_GEN_OK && refused == 6 && a1 == a0 && l1 == l0 &&
1247 d1.active_id == d0.active_id && d1.lineage == d0.lineage;
1248 printf("R16 G4 (4) promotion naming the promoter as subject, with the promoter's grant: "
1249 "%s (%d/6 credential variants refused with IDENTITY; rc %d; generation %llu -> %llu "
1250 "in memory, %llu -> %llu on disk)\n",
1251 probe_refused ? "REFUSED" : "ACCEPTED", refused, last_rc, U(a0), U(a1),
1252 U(d0.active_id), U(d1.active_id));
1253 if (!probe_refused)
1254 printf("R16 G4 OPEN: promotion accepts the subject the caller names; any in-process "
1255 "holder of the promoter's grant reference can promote.\n");
1256 }
1257
1258 /* (6), last: the same exploit on the reaction path. A legacy reaction whose
1259 * descriptor names the promoter as its subject and carries the promoter's
1260 * in-force grant. Before R16 C5 validate_caps checked the grant against the
1261 * subject the descriptor names and this write committed. Now registration
1262 * needs the named subject's credential; each variant the legacy context
1263 * could present is tried, and any that registers is woken to show its
1264 * effect. */
1265 int rprobe_refused = 0;
1266 {
1267 RxObject b0, b1;
1268 rx_world_read(&r->w, r->living.o.inforce, &b0);
1269 const RxCallerCred *own = rx_caller_find(L.keys, LEGACY_SUBJ);
1270 const RxCallerCred *prom = rx_caller_find(&r->keys_promoter, RX_LIVING_PROMOTE_SUBJ);
1271 RxCallerCred rnd;
1272 rnd.generation = prom->generation;
1273 fill_random(rnd.secret, sizeof rnd.secret);
1274 const RxCallerCred *v[3] = {NULL, own, &rnd};
1275 const char *vw[3] = {"no credential", "its own real credential",
1276 "random secret at the promoter's generation"};
1277 /* Wake: a harness fixture grant lets the promoter subject read the
1278 * legacy scratch object, so the probe can be woken on demand (in normal
1279 * operation a candidate trigger with the promoter's candidate grant
1280 * would wake it). The grant under test is the borrowed in-force one. */
1281 g_named_trigger = (RxDep){L.scratch, RX_FIELD(0)};
1282 g_named_read = (RxCapNeed){r15_mint(r, RX_LIVING_PROMOTE_SUBJ, RES_LEGACY_SCRATCH,
1283 RX_RIGHT_READ), RES_LEGACY_SCRATCH, RX_RIGHT_READ};
1284 static Poke p_named[3];
1285 uint32_t id_n[3] = {0, 0, 0};
1286 int add[3], refused = 0, any_added = 0;
1287 for (int i = 0; i < 3; i++) {
1288 g_named_subject = RX_LIVING_PROMOTE_SUBJ;
1289 g_named_cred = v[i];
1290 g_named_explicit = 1;
1291 add[i] = legacy_reaction(&L, "legacy.named-subject", r->living.o.inforce, b0.resource,
1292 L.borrowed_inforce, 1, &p_named[i], &id_n[i]);
1293 g_named_subject = 0;
1294 g_named_explicit = 0;
1295 g_named_cred = NULL;
1296 if (add[i] == RX_ERR_IDENTITY) refused++;
1297 else printf("R16 G4 FAIL: (6) promoter-subject reaction, %s: register rc %d "
1298 "(expected IDENTITY %d)\n", vw[i], add[i], RX_ERR_IDENTITY);
1299 if (add[i] == RX_OK) any_added = 1;
1300 }
1301 int woke = -1;
1302 uint64_t commits = 0, acts = 0;
1303 int crumb = -1;
1304 if (any_added) {
1305 woke = wake_legacy(&L);
1306 pthread_mutex_lock(&r->w.mu);
1307 for (int i = 0; i < 3; i++) {
1308 if (add[i] != RX_OK) continue;
1309 commits += r->w.reactions[id_n[i]].commits;
1310 acts += r->w.reactions[id_n[i]].activations;
1311 const RxCrumb *k = rx_world_crumb(&r->w, r->w.reactions[id_n[i]].last_crumb);
1312 crumb = k ? (int)k->kind : -1;
1313 }
1314 pthread_mutex_unlock(&r->w.mu);
1315 }
1316 rx_world_read(&r->w, r->living.o.inforce, &b1);
1317 int moved = commits > 0 || b1.field[0] == 0xBADBADull || b1.version != b0.version;
1318 rprobe_refused = refused == 3 && !moved;
1319 printf("R16 G4 (6) reaction naming the promoter as subject, with the promoter's in-force "
1320 "grant: %s (%d/3 credential variants refused at registration with IDENTITY; woken "
1321 "%s; activations %llu, commits %llu, last crumb %d; in-force field0 0x%llx)\n",
1322 rprobe_refused ? "REFUSED" : moved ? "ACCEPTED" : "NOT REFUSED AT REGISTRATION",
1323 refused, woke == 0 ? "yes" : "no", U(acts), U(commits), crumb, U(b1.field[0]));
1324 if (!rprobe_refused)
1325 printf("R16 G4 OPEN: a reaction's subject is whatever its descriptor names; any "
1326 "in-process holder of a writer's grant reference can write as that writer.\n");
1327 }
1328
1329 /* ---- R16 C5: the caller credential itself (hostile probes) ----------- */
1330 Tally tc = {0, 0};
1331 {
1332 RxWorld *w = &r->w;
1333 const RxCallerCred *own = rx_caller_find(L.keys, LEGACY_SUBJ);
1334 const RxCallerCred *own2 = rx_caller_find(L.keys, LEGACY2_SUBJ);
1335 const RxCallerCred *prom = rx_caller_find(&r->keys_promoter, RX_LIVING_PROMOTE_SUBJ);
1336 RxCallerCred x;
1337 /* spoofed caller: every credential it could present for the promoter */
1338 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, NULL), RX_CALLER_ERR_ABSENT,
1339 "promoter subject, no credential");
1340 memset(&x, 0, sizeof x);
1341 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, &x), RX_CALLER_ERR_ABSENT,
1342 "promoter subject, all-zero credential");
1343 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, own), RX_CALLER_ERR_STALE,
1344 "promoter subject, LEGACY's real credential");
1345 x = *own; x.generation = prom->generation;
1346 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, &x), RX_CALLER_ERR_FORGED,
1347 "promoter subject, LEGACY's secret at the promoter's generation");
1348 x = *prom; x.secret[0] ^= 1;
1349 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, &x), RX_CALLER_ERR_FORGED,
1350 "promoter subject, promoter's secret with one bit flipped");
1351 x = *prom; x.generation += 1000;
1352 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, &x), RX_CALLER_ERR_STALE,
1353 "promoter subject, promoter's secret at another generation");
1354 expect(&tc, rx_world_check_caller(w, 999, own), RX_CALLER_ERR_UNKNOWN,
1355 "never-enrolled subject");
1356 expect(&tc, rx_world_check_caller(w, LEGACY_SUBJ, own), RX_CALLER_OK,
1357 "control: LEGACY's own credential for LEGACY");
1358 /* enrollment is closed once the world is bound */
1359 expect(&tc, rx_world_enroll_caller(w, 170, &x), RX_CALLER_ERR_CLOSED, "enroll after bind");
1360 expect(&tc, x.generation, 0, "enroll after bind hands out no credential");
1361 expect(&tc, rx_world_enroll_caller(w, RX_LIVING_PROMOTE_SUBJ, &x), RX_CALLER_ERR_CLOSED,
1362 "re-enroll the promoter after bind");
1363 /* revocation needs the credential itself */
1364 expect(&tc, rx_world_revoke_caller(w, RX_LIVING_PROMOTE_SUBJ, own), RX_CALLER_ERR_STALE,
1365 "revoke the promoter with LEGACY's credential");
1366 expect(&tc, rx_world_revoke_caller(w, RX_LIVING_PROMOTE_SUBJ, NULL), RX_CALLER_ERR_ABSENT,
1367 "revoke the promoter with no credential");
1368 expect(&tc, rx_world_check_caller(w, RX_LIVING_PROMOTE_SUBJ, prom), RX_CALLER_OK,
1369 "promoter still enrolled after the revoke attempts");
1370 /* R9 proposal: the proposer is checked as well */
1371 uint64_t c2 = 0;
1372 expect(&tc, rx_gen_propose_as(L.gen, RX_LIVING_PREPARE_SUBJ, NULL, &gd, &c2),
1373 RX_GEN_ERR_IDENTITY, "propose as the prepare subject with no credential");
1374 expect(&tc, rx_gen_propose_as(L.gen, RX_LIVING_PREPARE_SUBJ, own, &gd, &c2),
1375 RX_GEN_ERR_IDENTITY, "propose as the prepare subject with LEGACY's credential");
1376 expect(&tc, rx_gen_propose(L.gen, LEGACY_SUBJ, &gd, &c2), RX_GEN_ERR_IDENTITY,
1377 "legacy rx_gen_propose (no credential) as LEGACY");
1378 /* A bound store ignores a permissive authority callback: LEGACY2 with
1379 * its real credential and a forged grant is refused by the native
1380 * authority. */
1381 uint64_t a0 = 0, l0 = 0, a1 = 0, l1 = 0;
1382 rx_gen_active(L.gen, &a0, &l0);
1383 RxPromotionRequest req = {probe_cand, LEGACY2_SUBJ, L.forged.cap_id, L.forged.generation,
1384 RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, {0, {0}}};
1385 req.caller = *own2;
1386 expect(&tc, rx_gen_promote(L.gen, &req, permissive_auth, NULL, NULL, NULL, NULL, NULL),
1387 RX_GEN_ERR_AUTHORITY, "permissive authority callback on a bound store");
1388 rx_gen_active(L.gen, &a1, &l1);
1389 expect(&tc, a1 == a0 && l1 == l0, 1, "generation unchanged by the permissive callback");
1390 rx_caller_wipe(&req.caller);
1391
1392 /* collision: every credential the rig issued is distinct, and none
1393 * validates for another subject */
1394 const RxCallerKeyring *rings[6] = {&r->keys_omega, &r->keys_aien, &r->keys_aegis,
1395 &r->keys_living, &r->keys_promoter, &r->fixtures};
1396 uint32_t subj[32]; const RxCallerCred *cred[32]; uint32_t n = 0;
1397 for (int k = 0; k < 6; k++)
1398 for (uint32_t i = 0; i < rings[k]->n && n < 32; i++) {
1399 subj[n] = rings[k]->subject[i]; cred[n++] = &rings[k]->cred[i];
1400 }
1401 uint32_t dup = 0, cross_ok = 0, cross = 0;
1402 for (uint32_t i = 0; i < n; i++)
1403 for (uint32_t j = 0; j < n; j++) {
1404 if (i == j) continue;
1405 if (j > i && memcmp(cred[i]->secret, cred[j]->secret, RX_CALLER_SECRET_LEN) == 0) dup++;
1406 x = *cred[i]; x.generation = cred[j]->generation;
1407 cross++;
1408 cross_ok += rx_world_check_caller(w, subj[j], cred[i]) != RX_CALLER_OK &&
1409 rx_world_check_caller(w, subj[j], &x) == RX_CALLER_ERR_FORGED;
1410 }
1411 expect(&tc, dup, 0, "duplicate secrets among the rig's credentials");
1412 expect(&tc, cross_ok, cross, "rig credentials cross-validating for another subject");
1413 /* collision at capacity, on a scratch world */
1414 RxWorld *sw = calloc(1, sizeof *sw);
1415 int sw_ok = sw && rx_world_init_native(sw, r->view, 1, 1024) == RX_OK;
1416 expect(&tc, sw_ok, 1, "scratch world for the capacity collision probe");
1417 if (sw_ok) {
1418 static RxCallerCred many[RX_CALLER_MAX];
1419 uint32_t enrolled = 0, sdup = 0, scross = 0, sbad = 0;
1420 for (uint32_t i = 0; i < RX_CALLER_MAX; i++)
1421 enrolled += rx_world_enroll_caller(sw, 1000 + i, &many[i]) == RX_CALLER_OK;
1422 expect(&tc, enrolled, RX_CALLER_MAX, "enroll to capacity");
1423 expect(&tc, rx_world_enroll_caller(sw, 5000, &x), RX_CALLER_ERR_FULL, "enroll past capacity");
1424 expect(&tc, rx_world_enroll_caller(sw, 1000, &x), RX_CALLER_ERR_EXISTS,
1425 "enroll a subject twice");
1426 for (uint32_t i = 0; i < RX_CALLER_MAX; i++)
1427 for (uint32_t j = 0; j < RX_CALLER_MAX; j++) {
1428 if (i == j) {
1429 sbad += rx_world_check_caller(sw, 1000 + i, &many[i]) != RX_CALLER_OK;
1430 continue;
1431 }
1432 if (j > i && memcmp(many[i].secret, many[j].secret, RX_CALLER_SECRET_LEN) == 0) sdup++;
1433 x = many[i]; x.generation = many[j].generation;
1434 scross += rx_world_check_caller(sw, 1000 + j, &x) == RX_CALLER_ERR_FORGED;
1435 }
1436 expect(&tc, sdup, 0, "duplicate secrets among 64 enrollments");
1437 expect(&tc, sbad, 0, "a credential not valid for its own subject");
1438 expect(&tc, scross, RX_CALLER_MAX * (RX_CALLER_MAX - 1),
1439 "cross-subject checks refused as FORGED");
1440 rx_world_bind_callers(sw);
1441 expect(&tc, rx_world_enroll_caller(sw, 6000, &x), RX_CALLER_ERR_CLOSED,
1442 "scratch world: enroll after bind");
1443 for (uint32_t i = 0; i < RX_CALLER_MAX; i++) rx_caller_wipe(&many[i]);
1444 rx_world_destroy(sw);
1445 }
1446 free(sw);
1447 }
1448
1449 /* ---- R16 C5: revocation, and the credential replayed after it --------- */
1450 {
1451 RxWorld *w = &r->w;
1452 const RxCallerCred *own = rx_caller_find(L.keys, LEGACY_SUBJ);
1453 const RxCallerCred *own2 = rx_caller_find(L.keys, LEGACY2_SUBJ);
1454 RxObject s2a, s2b;
1455 rx_world_read(w, L.scratch2, &s2a);
1456 /* LEGACY's identity is revoked while its admitted reaction runs; the
1457 * write its real grant covers must not commit. */
1458 static IdRevoke f;
1459 memset(&f, 0, sizeof f);
1460 f.w = w;
1461 f.cred = *own;
1462 uint32_t id = 0;
1463 int add = legacy_reaction_fn(&L, "legacy.identity-revoked-in-flight", L.scratch2,
1464 RES_LEGACY_SCRATCH, L.own_scratch, 1, &f.p, fn_id_revoke, &f,
1465 &id);
1466 expect(&tc, add, RX_OK, "register LEGACY's reaction under its own credential");
1467 if (add == RX_OK) {
1468 CHECK(wake_legacy(&L) == 0, "C5: the in-flight identity reaction did not settle");
1469 pthread_mutex_lock(&w->mu);
1470 RxReaction rr = w->reactions[id];
1471 const RxCrumb *k = rx_world_crumb(w, rr.last_crumb);
1472 int rejected = k && k->kind == RX_CRUMB_REJECTED;
1473 pthread_mutex_unlock(&w->mu);
1474 expect(&tc, f.revoked, 1, "LEGACY revoked its own identity in flight");
1475 expect(&tc, rr.activations >= 1 && rr.commits == 0 && rejected, 1,
1476 "write of a reaction whose identity was revoked in flight");
1477 /* woken again: blocked at activation */
1478 CHECK(wake_legacy(&L) == 0, "C5: the revoked reaction did not settle");
1479 pthread_mutex_lock(&w->mu);
1480 rr = w->reactions[id];
1481 k = rx_world_crumb(w, rr.last_crumb);
1482 int blocked = k && k->kind == RX_CRUMB_BLOCKED_AUTHORITY;
1483 pthread_mutex_unlock(&w->mu);
1484 expect(&tc, rr.commits == 0 && blocked, 1,
1485 "activation of a reaction whose identity is revoked");
1486 }
1487 rx_world_read(w, L.scratch2, &s2b);
1488 expect(&tc, s2b.version == s2a.version && s2b.field[0] == s2a.field[0], 1,
1489 "scratch target unchanged");
1490 /* replay: the revoked credential opens nothing */
1491 expect(&tc, rx_world_check_caller(w, LEGACY_SUBJ, own), RX_CALLER_ERR_REVOKED,
1492 "revoked credential replayed at the check");
1493 expect(&tc, rx_world_revoke_caller(w, LEGACY_SUBJ, own), RX_CALLER_ERR_REVOKED,
1494 "revoked credential replayed at revocation");
1495 uint64_t c3 = 0;
1496 expect(&tc, rx_gen_propose_as(L.gen, LEGACY_SUBJ, own, &gd, &c3), RX_GEN_ERR_IDENTITY,
1497 "revoked credential replayed at proposal");
1498 RxPromotionRequest req = {probe_cand, LEGACY_SUBJ, L.own_promote.cap_id,
1499 L.own_promote.generation, RX_GEN_RES_PROMOTION,
1500 RX_GEN_RIGHT_PROMOTE, {0, {0}}};
1501 req.caller = *own;
1502 expect(&tc, rx_gen_promote(L.gen, &req, native_auth, (void *)L.view, NULL, NULL, NULL,
1503 NULL), RX_GEN_ERR_IDENTITY,
1504 "revoked credential replayed at promotion");
1505 rx_caller_wipe(&req.caller);
1506 static Poke p_replay;
1507 uint32_t id_r = 0;
1508 expect(&tc, legacy_reaction(&L, "legacy.replay", L.scratch2, RES_LEGACY_SCRATCH,
1509 L.own_scratch, 1, &p_replay, &id_r), RX_ERR_IDENTITY,
1510 "revoked credential replayed at registration");
1511 expect(&tc, rx_world_check_caller(w, LEGACY2_SUBJ, own2), RX_CALLER_OK,
1512 "another subject's enrollment unaffected");
1513 rx_caller_wipe(&f.cred);
1514 }
1515 int ident_ok = tc.tried > 0 && tc.refused == tc.tried;
1516 printf("R16 G4 C5 identity probes: %u/%u as expected %s\n", tc.refused, tc.tried,
1517 ident_ok ? "PASS" : "FAIL");
1518
1519 /* ---- positive control: promotion is not disabled ----------------------- */
1520 int control_ok = 0;
1521 {
1522 uint64_t cand = 0, a0 = 0, l0 = 0, a1 = 0, l1 = 0;
1523 int prc = rx_gen_propose_as(r->gen, RX_LIVING_PREPARE_SUBJ,
1524 rx_caller_find(&r->keys_living, RX_LIVING_PREPARE_SUBJ), &gd,
1525 &cand);
1526 rx_gen_active(r->gen, &a0, &l0);
1527 RxPromotionRequest req = {cand, RX_LIVING_PROMOTE_SUBJ,
1528 r->promoter.promotion_authority.cap_id,
1529 r->promoter.promotion_authority.generation,
1530 RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, {0, {0}}};
1531 req.caller = *rx_caller_find(&r->keys_promoter, RX_LIVING_PROMOTE_SUBJ);
1532 int rc = prc == RX_GEN_OK
1533 ? rx_gen_promote(r->gen, &req, NULL, NULL, NULL, NULL, NULL, NULL) : prc;
1534 rx_caller_wipe(&req.caller);
1535 rx_gen_active(r->gen, &a1, &l1);
1536 RxRecoveryRecord d1;
1537 memset(&d1, 0, sizeof d1);
1538 rx_gen_recover(r->generation_dir, &d1);
1539 control_ok = rc == RX_GEN_OK && a1 == cand && a1 != a0 && d1.active_id == cand;
1540 printf("R16 G4 control: the promoter, with its own credential and grant, promotes: %s "
1541 "(propose rc %d, promote rc %d; generation %llu -> %llu, on disk %llu)\n",
1542 control_ok ? "yes" : "NO", prc, rc, U(a0), U(a1), U(d1.active_id));
1543 }
1544
1545 int review_ok = review_probes();
1546
1547 int acts_ok = 0;
1548 const char *names[7] = {"", "belief", "selection", "mint", "promote", "boundary", "generation"};
1549 for (int a = 1; a <= 6; a++) {
1550 int ok = t[a].tried > 0 && t[a].refused == t[a].tried;
1551 acts_ok += ok;
1552 printf("R16 G4 (%d) %-10s %u/%u attempts refused %s\n", a, names[a], t[a].refused,
1553 t[a].tried, ok ? "REFUSED" : "NOT REFUSED");
1554 }
1555 printf("R16 G4 SEQ reference on a production world: %s\n", seq_refused ? "refused" : "RAN");
1556 printf("R16 G4 authoritative state unchanged: %s; AIENOS state changes other than the "
1557 "harness office's own revocations (%llu): %s; production served after: %s\n",
1558 g_fail ? "see failures" : "yes", U(atomic_load(&g_expected_changes)),
1559 g_fail ? "see failures" : "none", prod ? "yes" : "no");
1560 r15_stop(r);
1561 free(r);
1562 int core = acts_ok == 6 && g_fail == 0;
1563 int pass = core && probe_refused && rprobe_refused && ident_ok && control_ok && review_ok;
1564 printf("R16 G4 acts refused: %d/6\n", acts_ok);
1565 printf("R16 G4 core: %s\n", core ? "six acts refused, state unchanged, for the counted per-act attempts" : "FAIL");
1566 printf("R16 G4 promoter-subject exploit (4)+(6): %s; C5 identity probes: %s; promotion control: %s\n",
1567 probe_refused && rprobe_refused ? "refused" : "ACCEPTED", ident_ok ? "pass" : "FAIL",
1568 control_ok ? "promotes" : "FAIL");
1569 printf("R16 gate: R16_G4_LEGACY_REFUSED=%s\n", pass ? "PASS" : "FAIL");
1570 return pass ? 0 : 1;
1571}