From 30e82c427f363c529bfcde32f0aaadf98133e948 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 15 Nov 2026 10:47:48 -0600 Subject: [PATCH] Fix security finding acf_be9bee6f50030a805a463ca807fe59bb --- src/runtime/rx_world.c | 62 ++++++++++++++++++++++++----- src/runtime/rx_world.h | 3 +- tests/runtime/rx_r15_instr.c | 77 +++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 13 deletions(-) diff --git a/src/runtime/rx_world.c b/src/runtime/rx_world.c index 379e41c85..2deaa8aff 100644 --- a/src/runtime/rx_world.c +++ b/src/runtime/rx_world.c @@ -202,12 +202,19 @@ static void add_parent(RxCrumb *k, uint64_t p) { if (k->n_parents < RX_MAX_PARENTS) k->parents[k->n_parents++] = p; } -/* Append a crumb; returns its id (0 if the log is full). Caller holds mu. */ -static uint64_t crumb_append(RxWorld *w, RxCrumb *k) { +/* Reserve room for one crumb. Caller holds mu through the matching append, so + * no other publisher can consume the checked slot. */ +static int crumb_preflight(RxWorld *w) { if (w->n_crumbs >= w->crumb_cap) { w->stats.crumb_overflow++; - return 0; + return RX_ERR_FULL; } + return RX_OK; +} + +/* Append a crumb; returns its id (0 if the log is full). Caller holds mu. */ +static uint64_t crumb_append(RxWorld *w, RxCrumb *k) { + if (crumb_preflight(w) != RX_OK) return 0; k->id = w->n_crumbs + 1; /* Canonical parent order: ascending. */ for (uint32_t i = 1; i < k->n_parents; i++) { @@ -232,6 +239,15 @@ static uint64_t crumb_append(RxWorld *w, RxCrumb *k) { return k->id; } +/* A successful preflight under mu makes append failure an internal invariant + * violation. Continuing after visible state changed would expose it without a + * causal id, so publication paths must fail closed instead. */ +static uint64_t crumb_append_preflighted(RxWorld *w, RxCrumb *k) { + uint64_t id = crumb_append(w, k); + if (id == 0) abort(); + return id; +} + /* R6 containment engaged. The crumb names the reaction, the limit and the * crumb that caused it (so its episode). Wakes the quarantine suppresses * later leave nothing, so the log stays bounded. Caller holds mu. */ @@ -1921,6 +1937,14 @@ static void run_one(RxWorld *w, uint32_t rid, uint32_t worker) { end_activation(w, rid); return; } + /* Reserve causal evidence before staging, a binder reaches its point of no + * return, or any canonical object changes. mu stays held through append. */ + if (crumb_preflight(w) != RX_OK) { + set_state(w, r, RX_REJECTED); + w->stats.rejected++; + end_activation(w, rid); + return; + } /* 3. Mutations inside the declared write set, targets not stale. */ PendingWrite pw[RX_MAX_WRITES]; uint32_t n_pw = 0; @@ -1964,7 +1988,7 @@ static void run_one(RxWorld *w, uint32_t rid, uint32_t worker) { note_value(w, r, 0, 0); } k.t_end_ns = now_ns(); - uint64_t cid = crumb_append(w, &k); + uint64_t cid = crumb_append_preflighted(w, &k); set_state(w, r, RX_COMMITTED); if (k.n_outputs) { w->stats.commits++; @@ -2212,6 +2236,10 @@ int rx_world_create(RxWorld *w, uint32_t type, RxPersist persist, uint64_t resou for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) { RxObject *o = &w->objects[i]; if (o->live) continue; + if (crumb_preflight(w) != RX_OK) { + pthread_mutex_unlock(&w->mu); + return RX_ERR_FULL; + } o->live = true; o->type = type; o->persist = persist; @@ -2242,7 +2270,7 @@ int rx_world_create(RxWorld *w, uint32_t type, RxPersist persist, uint64_t resou k.outputs[0].version = 1; k.outputs[0].mask = RX_ALL_FIELDS; k.t_start_ns = k.t_end_ns = now_ns(); - uint64_t cid = crumb_append(w, &k); + uint64_t cid = crumb_append_preflighted(w, &k); for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) o->field_writer[f] = cid; rx_coherent_project(w, i); out->id = i; @@ -2272,6 +2300,10 @@ int rx_world_retire(RxWorld *w, RxObjRef ref) { pthread_mutex_unlock(&w->mu); return RX_ERR_FULL; } + if (crumb_preflight(w) != RX_OK) { + pthread_mutex_unlock(&w->mu); + return RX_ERR_FULL; + } uint32_t next_gen = o->generation + 1u; o->live = false; o->generation = next_gen; @@ -2286,7 +2318,7 @@ int rx_world_retire(RxWorld *w, RxObjRef ref) { k.inputs[0].version = o->version; k.inputs[0].mask = RX_ALL_FIELDS; k.t_start_ns = k.t_end_ns = now_ns(); - crumb_append(w, &k); + crumb_append_preflighted(w, &k); pthread_mutex_unlock(&w->mu); return RX_OK; } @@ -2475,6 +2507,10 @@ int64_t rx_world_publish_external(RxWorld *w, RxCapRef cap, const RxMutation *mu w->objects[muts[i].obj.id].resource, RX_RIGHT_WRITE, NULL); if (rc != RX_CAP_OK) { pthread_mutex_unlock(&w->mu); return RX_ERR_AUTHORITY; } } + if (crumb_preflight(w) != RX_OK) { + pthread_mutex_unlock(&w->mu); + return RX_ERR_FULL; + } PendingWrite pw[RX_MAX_WRITES]; uint32_t n_pw = 0; int rc = stage_mutations(w, muts, n, NULL, 0, pw, &n_pw); @@ -2492,7 +2528,7 @@ int64_t rx_world_publish_external(RxWorld *w, RxCapRef cap, const RxMutation *mu k.t_start_ns = now_ns(); commit_writes(w, pw, n_pw, &k); k.t_end_ns = now_ns(); - uint64_t cid = crumb_append(w, &k); + uint64_t cid = crumb_append_preflighted(w, &k); finish_writes(w, pw, n_pw, cid, false); pthread_mutex_unlock(&w->mu); return (int64_t)cid; @@ -2527,7 +2563,7 @@ static void seat_fail(RxWorld *w, uint32_t rid, RxCrumb *k, RxCrumbKind kind, in k->kind = kind; k->reason = reason; k->t_end_ns = now_ns(); - crumb_append(w, k); + crumb_append_preflighted(w, k); r->resident_seat = false; w->stats.resident_closed++; if (kind == RX_CRUMB_INVALIDATED) w->stats.invalidations++; @@ -2539,6 +2575,12 @@ static void seat_fail(RxWorld *w, uint32_t rid, RxCrumb *k, RxCrumbKind kind, in int rx_resident_accept(RxWorld *w) { if (!w || !w->coherent) return RX_ERR_ARG; pthread_mutex_lock(&w->mu); + /* Leave the result ring and activation untouched when no causal record can + * be committed; the caller receives an explicit retry refusal. */ + if (crumb_preflight(w) != RX_OK) { + pthread_mutex_unlock(&w->mu); + return RX_ERR_FULL; + } OmegaSharedWorldDesc d; int tr = rx_resident_take_result(w, &d); if (tr == RX_ERR_NOT_FOUND || tr == RX_ERR_ARG) { @@ -2591,7 +2633,7 @@ int rx_resident_accept(RxWorld *w) { k.kind = RX_CRUMB_CANCELLED; k.reason = RX_ERR_HALTED; k.t_end_ns = now_ns(); - crumb_append(w, &k); + crumb_append_preflighted(w, &k); r->resident_seat = false; w->stats.resident_closed++; w->stats.resident_halted++; @@ -2691,7 +2733,7 @@ int rx_resident_accept(RxWorld *w) { k.kind = RX_CRUMB_COMMIT; k.t_end_ns = now_ns(); note_value(w, r, 1, f1); - uint64_t cid = crumb_append(w, &k); + uint64_t cid = crumb_append_preflighted(w, &k); set_state(w, r, RX_COMMITTED); w->stats.commits++; r->commits++; diff --git a/src/runtime/rx_world.h b/src/runtime/rx_world.h index f9f2603d3..0d89d8a4e 100644 --- a/src/runtime/rx_world.h +++ b/src/runtime/rx_world.h @@ -852,7 +852,8 @@ int rx_world_operator_authorize(RxWorld *w, uint32_t subject, const RxCallerCre /* A stimulus from outside the organism (sensor, human input). Requires a * capability for (external_subject, object resource, WRITE). Returns the - * causal id of the EXTERNAL crumb, or a negative error. */ + * causal id of the EXTERNAL crumb, or a negative error (including RX_ERR_FULL + * without changing an object when the causal log has no room). */ int64_t rx_world_publish_external(RxWorld *w, RxCapRef cap, const RxMutation *muts, uint32_t n); diff --git a/tests/runtime/rx_r15_instr.c b/tests/runtime/rx_r15_instr.c index 47b4f7f22..9afe7f1ff 100644 --- a/tests/runtime/rx_r15_instr.c +++ b/tests/runtime/rx_r15_instr.c @@ -102,6 +102,14 @@ static RxObjRef mkobj(Env *e, uint64_t resource) { /* Publishes nothing: the only propagation wave is the stimulus's. */ static int fn_quiet(RxCtx *c) { (void)c; return 0; } +typedef struct { RxObjRef out; } FullLogWrite; + +static int fn_full_log_write(RxCtx *c) { + const FullLogWrite *a = c->user; + c->out[c->n_out++] = (RxMutation){ a->out, 0, c->in[0].field[0] + 1 }; + return 0; +} + static uint32_t add_listener(Env *e, RxObjRef src, uint64_t mask, RxCapRef rd) { RxReactionDesc d; memset(&d, 0, sizeof(d)); @@ -295,9 +303,74 @@ static void t_long_log(void) { rx_world_create(&small, 1, RX_PERSIST_RESIDENT, RES_SRC, init, &s2); RxMutation m2 = {s2, 0, 1}; int64_t last = 1; - for (int i = 0; i < 40 && last > 0; i++) { m2.value = (uint64_t)i + 2; last = rx_world_publish_external(&small, ext, &m2, 1); } - CHECK(last <= 0 && small.stats.crumb_overflow > 0, "a full log did not refuse the publication"); + RxObject before, after; + uint64_t crumbs_before = 0, externals_before = 0; + for (int i = 0; i < 40 && last > 0; i++) { + m2.value = (uint64_t)i + 2; + rx_world_read(&small, s2, &before); + crumbs_before = small.n_crumbs; + externals_before = small.stats.externals; + last = rx_world_publish_external(&small, ext, &m2, 1); + } + rx_world_read(&small, s2, &after); + CHECK(last == RX_ERR_FULL && small.stats.crumb_overflow > 0, + "a full log did not refuse the publication with RX_ERR_FULL"); + CHECK(small.n_crumbs == crumbs_before && small.stats.externals == externals_before && + after.version == before.version && + memcmp(after.field, before.field, sizeof after.field) == 0 && + memcmp(after.field_version, before.field_version, sizeof after.field_version) == 0 && + memcmp(after.field_writer, before.field_writer, sizeof after.field_writer) == 0 && + memcmp(after.digest, before.digest, sizeof after.digest) == 0, + "a refused external publication changed object state or claimed a receipt"); rx_world_destroy(&small); + + /* Two CREATE crumbs leave one slot. The EXTERNAL stimulus legitimately + * takes it; the reaction it wakes must not publish without a fourth slot. */ + static RxWorld reaction; + CHECK(rx_world_init(&reaction, &root, 1, 3) == RX_OK, "reaction-full world"); + reaction.external_subject = SUBJ_EXTERNAL; + RxObjRef rsrc = {UINT32_MAX, 0}, rout = {UINT32_MAX, 0}; + CHECK(rx_world_create(&reaction, 1, RX_PERSIST_RESIDENT, RES_SRC, init, &rsrc) == RX_OK && + rx_world_create(&reaction, 1, RX_PERSIST_RESIDENT, RES_OUT, init, &rout) == RX_OK, + "reaction-full objects"); + mm.subject = SUBJ_WORK; mm.resource = RES_SRC; mm.rights = RX_RIGHT_READ; + RxCapRef rd = {UINT32_MAX, 0}, wr = {UINT32_MAX, 0}; + CHECK(rx_capadmin_mint(&admin, &mm, &rd) == RX_CAP_OK, "reaction-full read cap"); + mm.resource = RES_OUT; mm.rights = RX_RIGHT_WRITE; + CHECK(rx_capadmin_mint(&admin, &mm, &wr) == RX_CAP_OK, "reaction-full write cap"); + FullLogWrite arg = { rout }; + RxReactionDesc d; + memset(&d, 0, sizeof d); + d.name = "r15.full-log-writer"; + d.faculty = RX_FACULTY_OMEGA; + d.subject = SUBJ_WORK; + d.priority = RX_PRIO_FOREGROUND; + d.fn = fn_full_log_write; + d.user = &arg; + d.triggers[d.n_triggers++] = (RxDep){ rsrc, RX_FIELD(0) }; + d.writes[d.n_writes++] = (RxDep){ rout, RX_FIELD(0) }; + d.caps[d.n_caps++] = (RxCapNeed){ rd, RES_SRC, RX_RIGHT_READ }; + d.caps[d.n_caps++] = (RxCapNeed){ wr, RES_OUT, RX_RIGHT_WRITE }; + uint32_t rid = UINT32_MAX; + CHECK(rx_world_add_reaction(&reaction, &d, &rid) == RX_OK, "reaction-full add writer"); + RxObject out_before, out_after, src_after; + rx_world_read(&reaction, rout, &out_before); + RxMutation trigger = { rsrc, 0, 9 }; + CHECK(rx_world_publish_external(&reaction, ext, &trigger, 1) == 3, + "last slot still accepts a recorded external publication"); + CHECK(rx_world_wait_quiescent(&reaction, 5000) == RX_OK, "reaction-full quiet"); + rx_world_read(&reaction, rout, &out_after); + rx_world_read(&reaction, rsrc, &src_after); + CHECK(src_after.field[0] == 9 && src_after.field_writer[0] == 3, + "recorded external publication was not preserved"); + CHECK(reaction.n_crumbs == 3 && reaction.stats.crumb_overflow > 0 && + reaction.reactions[rid].commits == 0 && out_after.version == out_before.version && + memcmp(out_after.field, out_before.field, sizeof out_after.field) == 0 && + memcmp(out_after.field_version, out_before.field_version, sizeof out_after.field_version) == 0 && + memcmp(out_after.field_writer, out_before.field_writer, sizeof out_after.field_writer) == 0 && + memcmp(out_after.digest, out_before.digest, sizeof out_after.digest) == 0, + "a reaction published object state after causal capacity was exhausted"); + rx_world_destroy(&reaction); rx_caproot_stop(&root, &admin); }