interplane.Get Omega
Read in your language

aienux

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

Brought by drakestapleton

branch main Branches Tags History

aienux / native / argus / argus_core.c

1136 lines · 46333 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 * argus_core.c -- ARGUS resident deterministic state engine (lane D; ARGUS-0, v1.2 additive).
19 *
20 * The core keeps fixed-capacity shadow tables of what the authority and the
21 * other producers have announced, runs the hard-invariant detectors against
22 * the state as it was BEFORE each event, then applies the event if the apply
23 * rules allow it. It never authorizes anything and never re-checks a capability.
24 *
25 * Properties: caller-provided memory (argus_core_footprint/argus_core_init),
26 * no heap use at all, no I/O, no clock, no mutable globals (only constant
27 * tables). The core holds no pointers, so a byte copy of its memory is a valid
28 * snapshot. Hashing goes only through header-declared functions
29 * (argus_event_validate, argus_event_digest, argus_chain_extend,
30 * argus_detect_run), so the lane-B/E implementations swap in transparently.
31 *
32 * ---- Ingest pipeline (hostile-review rules, argus_abi.h) -------------------
33 *
34 * 0. Well-formedness: argus_event_validate, plus two core guards that mirror
35 * the header's validate rules (cap_id >= ARGUS_CAP_MAX, sequence ==
36 * UINT64_MAX), so they hold even against an older validate. A malformed
37 * event raises ARGUS_F_MALFORMED_EVENT (MEDIUM, sequence = the event's,
38 * event_digest stamped), is counted in events_rejected, is NOT applied, NOT
39 * chained, does not touch the producer table, and ingest returns
40 * ARGUS_ERR_MALFORMED. A cap_id out of range is therefore never "table full".
41 * 1. Sequence check per producer stream, keyed by (machine_id,
42 * ARGUS_STREAM_OF(flags), flags & ARGUS_FLAG_CONSUMER) (v1.1: one stream per
43 * producer thread/ring; ARGUS_CORE_PRODUCERS streams in total). A sequence
44 * <= the stream's high-water mark raises
45 * ARGUS_F_SEQUENCE_ANOMALY (HIGH, prior_sequence = high-water mark); the
46 * event is detected (lane E runs) but NOT applied; the mark never moves
47 * backwards. Gaps are not findings. With the producer table full, a new
48 * stream is not tracked (producers_untracked counts its events); the FIRST
49 * untracked event returns ARGUS_ERR_FULL + one CRITICAL TELEMETRY_LOSS,
50 * later ones do not (no per-event CRITICAL amplifier), and are applied.
51 * 2. Apply verdict (only for an in-order event whose outcome would update
52 * state; decided on the pre-state):
53 * CAPABILITY_GRANTED applies iff the slot is UNSEEN or cap_generation >
54 * the shadow generation; otherwise ARGUS_F_AUTHORITY_REPLAY
55 * (code 13, HIGH, prior_sequence = the slot's revoked
56 * sequence if REVOKED, else its granted sequence).
57 * CAPABILITY_REVOKED applies iff the slot is LIVE and cap_generation ==
58 * the shadow generation. Every other case (UNSEEN, already
59 * REVOKED, generation above or below) is code 13 and is
60 * not applied, so a forged REVOKED at UINT64_MAX creates
61 * nothing and poisons nothing.
62 * LEASE_CREATED on a LIVE or REVOKED lease id: not applied (lease ids
63 * are never reused; lane E raises code 6).
64 * LEASE_REVOKED on an unknown lease id: not applied, no entry.
65 * WORLD_COMMITTED per store (object_id = store id, v1.1): adopted iff
66 * world_generation == that store's shadow+1, or the store
67 * has no commit yet (first commit). Same generation +
68 * same digest is an idempotent no-op. Anything else is not adopted (lane E
69 * raises code 9). world_generation 0 (= unknown) is a no-op.
70 * MACHINE_TRUST_CHANGED unknown machine: not applied, no entry (lane E: code 7).
71 * A move UP the rank order is not applied (lane E: code 15);
72 * equal rank is a no-op; object_id outside 1..6 is ignored.
73 * Code-13 findings come from the core. Every event flagged by step 1 or
74 * not applied by step 2 (13, lease reuse, unknown lease revoke, world not
75 * adopted, trust escalation, unknown machine) counts once in
76 * events_not_applied.
77 * 3. Detectors (lane E) on the PRE-update state, into the scratch after the
78 * core's step-1/2 finding (at most one: anomaly and verdict are exclusive).
79 * 4. Apply (if allowed). Table full (machines, leases, providers, artifacts,
80 * worlds): update dropped, ERR_FULL, counted in tables_full AND
81 * events_not_applied. Loud once PER TABLE (ruling a): the first overflow of
82 * each table adds one CRITICAL ARGUS_F_TELEMETRY_LOSS (the shadow can no
83 * longer mirror the authority); later overflows of that table add no
84 * finding (no per-event CRITICAL amplifier). ARGUS-1 turns a saturated
85 * table into a ContainmentRequest.
86 * 5. TELEMETRY_DROPPED class CRITICAL -> TELEMETRY_LOSS CRITICAL, class
87 * SECURITY -> HIGH. Lane E must not raise this code for TELEMETRY_DROPPED.
88 * 6. Chain: every well-formed event (applied or not) extends the chain.
89 * 7. Stamp event_digest on every finding, note incidents, copy out.
90 *
91 * v1.2 containment kinds 90-93 (ARGUS1_SPEC section 3; additive). Once validate
92 * accepts one (lane B rules), the core treats it like any other well-formed
93 * event in steps 0, 1, 6 and 7 (sequence-checked on its stream, counted in
94 * events_received, chained) with two differences: step 2/4 apply nothing (no
95 * shadow change; they fall through the default verdict/apply as no-ops) and
96 * step 3 is SKIPPED: the detectors never see a 90-93 as a trigger, so no code
97 * 1-16 detector fires on ARGUS's own containment record. Codes 17-21 are raised
98 * by argus_contain (lane C), never by the core. No new core state: the layout,
99 * CORE_MAGIC and state digest (v3) are unchanged, and an ARGUS-0 stream (which
100 * cannot contain a valid 90-93) replays to byte-identical digests.
101 *
102 * argus_core_incident (v1.2) is a read-only lookup of the incident table:
103 * first_sequence of the tracked (principal, code) pair (severity >= HIGH).
104 * Incidents are already part of the state digest (tag 11) and are derived
105 * only from ingested events, so the value replays exactly.
106 *
107 * Outcome gating. A state update is applied only when outcome == OK, except
108 * ARTIFACT_REJECTED, which applies on OK or DENIED (the rejection is itself
109 * the authority's decision). ERROR outcomes never change state.
110 *
111 * ---- Field conventions ------------------------------------------------------
112 * CAPABILITY_GRANTED resource = authority resource, object_id = rights mask,
113 * principal = subject, (cap_id, cap_generation) = the ref.
114 * cap_id ARGUS_CAP_NONE = none (no update). cap_id 0 is
115 * the authority OFFICE slot and is tracked like any other (v1.1).
116 * CREDENTIAL_LEASE_* lease id = object_id (0 = none). CREATED: subject =
117 * principal, scope = resource, LIVE. REVOKED: REVOKED.
118 * ARTIFACT_* keyed by evidence_digest (all-zero = none). ADMITTED:
119 * LIVE only with outcome OK and code 0. REJECTED: REJECTED,
120 * sticky (a later ADMITTED does not un-reject).
121 * MACHINE_* keyed by machine_id; table kept sorted by machine_id.
122 * Trust rank (weakest last): TRUSTED < OBSERVED <
123 * REATTESTATION_REQUIRED < RESTRICTED < QUARANTINED <
124 * UNTRUSTED (NOT the enum order). JOINED: trust = the
125 * weaker of OBSERVED and object_id (0 or out of range =>
126 * OBSERVED), so a machine can never declare itself TRUSTED.
127 * A JOINED of a known machine keeps the weaker of its
128 * current trust and the claim (never upward); if the entry
129 * is a tombstone it becomes joined again at the event's
130 * sequence, so a quarantine survives remove + rejoin.
131 * REMOVED: tombstone = entry kept with trust preserved and
132 * joined_sequence 0 (ArgusMachineShadow has no state field;
133 * joined_sequence 0 is the tombstone marker, and the ops
134 * lookup still returns it so detectors see its trust).
135 * PROVIDER_* keyed by evidence_digest. DISCOVERED: LIVE if new; a
136 * rediscovery never clears a quarantine. QUARANTINED:
137 * REVOKED (created if unseen).
138 * WORLD_COMMITTED object_id = store id; world_generation + evidence_digest
139 * per store (rules above). ARGUS_CORE_WORLDS (8) stores, kept
140 * sorted by store_id; a 9th store is table-full (step 4).
141 * CAPABILITY_USE_SUMMARY (v1.1) applies nothing; it is sequence-checked, detected
142 * (lane E treats it as a USED) and chained like any event.
143 * POLICY_CHANGED / RUNTIME_BUILD_CHANGED evidence_digest = expected digest.
144 * TELEMETRY_DROPPED object_id = class, resource = count.
145 *
146 * Findings. Core findings: confidence DETERMINISTIC, detector == code,
147 * sync_allowed 0, containment NONE. Every finding (core and lane E) gets
148 * event_digest = argus_event_digest(ev). Findings are collected in an internal
149 * scratch of ARGUS_CORE_MAX_FINDINGS (two slots reserved for the step-4/5 core
150 * findings), state is applied regardless of the caller's buffer, then
151 * min(n, cap) are copied out. If n > cap, ingest returns ARGUS_ERR_OVERFLOW with
152 * *n_out = cap: findings are lost to the caller, state never is.
153 * Return precedence: MALFORMED > FULL > OVERFLOW > detector error > OK.
154 *
155 * Health: incidents_open = tracked (principal, code) pairs with severity >= HIGH
156 * (table ARGUS_CORE_INCIDENTS); pairs beyond it count in incidents_untracked.
157 * producers_untracked counts events on untracked streams; tables_full counts
158 * ingest calls that returned ARGUS_ERR_FULL.
159 *
160 * Lookups: caps O(1) direct index; machines and producers binary search over
161 * sorted tables; artifacts, leases, providers, incidents linear scans over at
162 * most 256 entries (bounded, no allocation).
163 */
164#include "argus_core.h"
165
166#include <string.h>
167
168#if ARGUS_CORE_CAPS != ARGUS_CAP_MAX
169#error "ARGUS_CORE_CAPS must equal ARGUS_CAP_MAX (== AIENOS_CAP_MAX)"
170#endif
171
172#define CORE_MAGIC 0x3053554752410004ull /* "ARGUS0" + layout 4 (v1.1: streams, per-store worlds; per-table loud-once) */
173#define CORE_RESERVED_SLOTS 2u /* telemetry-drop + table-full findings */
174
175typedef struct {
176 uint8_t machine_id[ARGUS_MACHINE_ID_LEN];
177 uint16_t stream; /* ARGUS_STREAM_OF(flags), v1.1 */
178 uint16_t consumer; /* 0 = live producer, 1 = ring consumer stream */
179 uint64_t last_sequence;
180} CoreProducer;
181
182typedef struct {
183 uint32_t principal;
184 uint16_t code;
185 uint8_t severity; /* highest seen */
186 uint64_t first_sequence;
187} CoreIncident;
188
189struct ArgusStateView {
190 ArgusCapShadow caps[ARGUS_CORE_CAPS];
191 ArgusMachineShadow machines[ARGUS_CORE_MACHINES]; /* sorted by machine_id */
192 ArgusArtifactShadow artifacts[ARGUS_CORE_ARTIFACTS];
193 ArgusLeaseShadow leases[ARGUS_CORE_LEASES];
194 ArgusProviderShadow providers[ARGUS_CORE_PROVIDERS];
195 ArgusWorldShadow worlds[ARGUS_CORE_WORLDS]; /* sorted by store_id */
196 uint8_t policy_digest[ARGUS_DIGEST_LEN];
197 uint8_t runtime_digest[ARGUS_DIGEST_LEN];
198 uint32_t n_caps_live; /* slots not UNSEEN */
199 uint32_t n_machines, n_artifacts, n_leases, n_providers, n_worlds;
200};
201
202struct ArgusCore {
203 uint64_t magic;
204 struct ArgusStateView view;
205 CoreProducer producers[ARGUS_CORE_PRODUCERS]; /* sorted by (machine_id, stream, consumer) */
206 CoreIncident incidents[ARGUS_CORE_INCIDENTS];
207 uint32_t n_producers, n_incidents;
208 uint64_t incidents_untracked, producers_untracked;
209 uint64_t events_received, events_rejected, findings_emitted;
210 uint64_t tables_full, events_not_applied;
211 uint32_t full_reported; /* CORE_TBL_* bits: tables whose first overflow was already reported */
212 uint8_t chain[ARGUS_DIGEST_LEN];
213 ArgusFinding scratch[ARGUS_CORE_MAX_FINDINGS];
214};
215
216/* ---- small helpers -------------------------------------------------------- */
217
218static int digest_is_zero(const uint8_t d[ARGUS_DIGEST_LEN])
219{
220 uint8_t acc = 0;
221 for (unsigned i = 0; i < ARGUS_DIGEST_LEN; i++)
222 acc |= d[i];
223 return acc == 0;
224}
225
226/* Rank of each ARGUS_TRUST_* value; higher = weaker. Index = enum value. */
227static const uint8_t trust_rank_tab[ARGUS_TRUST_REATTESTATION_REQUIRED + 1] = {
228 [ARGUS_TRUST_UNKNOWN] = 0,
229 [ARGUS_TRUST_TRUSTED] = 1,
230 [ARGUS_TRUST_OBSERVED] = 2,
231 [ARGUS_TRUST_REATTESTATION_REQUIRED] = 3,
232 [ARGUS_TRUST_RESTRICTED] = 4,
233 [ARGUS_TRUST_QUARANTINED] = 5,
234 [ARGUS_TRUST_UNTRUSTED] = 6,
235};
236
237static int trust_valid(uint32_t t)
238{
239 return t >= ARGUS_TRUST_TRUSTED && t <= ARGUS_TRUST_REATTESTATION_REQUIRED;
240}
241
242static uint32_t trust_weaker(uint32_t a, uint32_t b)
243{
244 return trust_rank_tab[a] >= trust_rank_tab[b] ? a : b;
245}
246
247/* Binary search: index if found, else -(insertion point) - 1. */
248static int find_machine(const struct ArgusStateView *v, const uint8_t id[ARGUS_MACHINE_ID_LEN])
249{
250 uint32_t lo = 0, hi = v->n_machines;
251 while (lo < hi) {
252 uint32_t mid = lo + (hi - lo) / 2u;
253 int c = memcmp(v->machines[mid].machine_id, id, ARGUS_MACHINE_ID_LEN);
254 if (c == 0)
255 return (int)mid;
256 if (c < 0)
257 lo = mid + 1u;
258 else
259 hi = mid;
260 }
261 return -(int)lo - 1;
262}
263
264static int find_artifact(const struct ArgusStateView *v, const uint8_t d[ARGUS_DIGEST_LEN])
265{
266 for (uint32_t i = 0; i < v->n_artifacts; i++)
267 if (memcmp(v->artifacts[i].digest, d, ARGUS_DIGEST_LEN) == 0)
268 return (int)i;
269 return -1;
270}
271
272static int find_lease(const struct ArgusStateView *v, uint32_t lease_id)
273{
274 for (uint32_t i = 0; i < v->n_leases; i++)
275 if (v->leases[i].lease_id == lease_id)
276 return (int)i;
277 return -1;
278}
279
280static int find_provider(const struct ArgusStateView *v, const uint8_t id[ARGUS_DIGEST_LEN])
281{
282 for (uint32_t i = 0; i < v->n_providers; i++)
283 if (memcmp(v->providers[i].provider_id, id, ARGUS_DIGEST_LEN) == 0)
284 return (int)i;
285 return -1;
286}
287
288/* Binary search over the (tiny) store table: index if found, else -(insertion point) - 1. */
289static int find_world(const struct ArgusStateView *v, uint32_t store_id)
290{
291 uint32_t lo = 0, hi = v->n_worlds;
292 while (lo < hi) {
293 uint32_t mid = lo + (hi - lo) / 2u;
294 if (v->worlds[mid].store_id == store_id)
295 return (int)mid;
296 if (v->worlds[mid].store_id < store_id)
297 lo = mid + 1u;
298 else
299 hi = mid;
300 }
301 return -(int)lo - 1;
302}
303
304/* ---- ops vtable (what detectors may ask) ---------------------------------- */
305
306static int op_cap(const ArgusStateView *v, uint32_t cap_id, ArgusCapShadow *out)
307{
308 if (!v || !out)
309 return ARGUS_ERR_ARG;
310 if (cap_id >= ARGUS_CORE_CAPS || v->caps[cap_id].state == ARGUS_SHADOW_UNSEEN)
311 return ARGUS_ERR_STATE;
312 *out = v->caps[cap_id];
313 return ARGUS_OK;
314}
315
316/* Tombstones (joined_sequence 0) are returned too: their trust still matters. */
317static int op_machine(const ArgusStateView *v, const uint8_t id[ARGUS_MACHINE_ID_LEN], ArgusMachineShadow *out)
318{
319 if (!v || !id || !out)
320 return ARGUS_ERR_ARG;
321 int i = find_machine(v, id);
322 if (i < 0)
323 return ARGUS_ERR_STATE;
324 *out = v->machines[i];
325 return ARGUS_OK;
326}
327
328static int op_artifact(const ArgusStateView *v, const uint8_t d[ARGUS_DIGEST_LEN], ArgusArtifactShadow *out)
329{
330 if (!v || !d || !out)
331 return ARGUS_ERR_ARG;
332 int i = find_artifact(v, d);
333 if (i < 0)
334 return ARGUS_ERR_STATE;
335 *out = v->artifacts[i];
336 return ARGUS_OK;
337}
338
339static int op_lease(const ArgusStateView *v, uint32_t lease_id, ArgusLeaseShadow *out)
340{
341 if (!v || !out)
342 return ARGUS_ERR_ARG;
343 int i = find_lease(v, lease_id);
344 if (i < 0)
345 return ARGUS_ERR_STATE;
346 *out = v->leases[i];
347 return ARGUS_OK;
348}
349
350static int op_provider(const ArgusStateView *v, const uint8_t id[ARGUS_DIGEST_LEN], ArgusProviderShadow *out)
351{
352 if (!v || !id || !out)
353 return ARGUS_ERR_ARG;
354 int i = find_provider(v, id);
355 if (i < 0)
356 return ARGUS_ERR_STATE;
357 *out = v->providers[i];
358 return ARGUS_OK;
359}
360
361static int op_world(const ArgusStateView *v, uint32_t store_id, ArgusWorldShadow *out)
362{
363 if (!v || !out)
364 return ARGUS_ERR_ARG;
365 int i = find_world(v, store_id);
366 if (i < 0 || v->worlds[i].generation == 0)
367 return ARGUS_ERR_STATE;
368 *out = v->worlds[i];
369 return ARGUS_OK;
370}
371
372/* Zero digest = none announced; still returns ARGUS_OK with zeros (per header). */
373static int op_policy_digest(const ArgusStateView *v, uint8_t out[ARGUS_DIGEST_LEN])
374{
375 if (!v || !out)
376 return ARGUS_ERR_ARG;
377 memcpy(out, v->policy_digest, ARGUS_DIGEST_LEN);
378 return ARGUS_OK;
379}
380
381static int op_runtime_digest(const ArgusStateView *v, uint8_t out[ARGUS_DIGEST_LEN])
382{
383 if (!v || !out)
384 return ARGUS_ERR_ARG;
385 memcpy(out, v->runtime_digest, ARGUS_DIGEST_LEN);
386 return ARGUS_OK;
387}
388
389static const ArgusStateOps core_ops = {
390 .cap = op_cap,
391 .machine = op_machine,
392 .artifact = op_artifact,
393 .lease = op_lease,
394 .provider = op_provider,
395 .world = op_world,
396 .policy_digest = op_policy_digest,
397 .runtime_digest = op_runtime_digest,
398};
399
400const ArgusStateOps *argus_core_ops(void)
401{
402 return &core_ops;
403}
404
405/* ---- lifecycle ------------------------------------------------------------ */
406
407size_t argus_core_footprint(void)
408{
409 return sizeof(struct ArgusCore);
410}
411
412int argus_core_init(ArgusCore **core, void *memory, size_t bytes)
413{
414 if (!core || !memory || bytes < sizeof(struct ArgusCore))
415 return ARGUS_ERR_ARG;
416 if (((uintptr_t)memory & (_Alignof(struct ArgusCore) - 1u)) != 0)
417 return ARGUS_ERR_ARG;
418 struct ArgusCore *c = (struct ArgusCore *)memory;
419 memset(c, 0, sizeof *c);
420 c->magic = CORE_MAGIC;
421 *core = c;
422 return ARGUS_OK;
423}
424
425const ArgusStateView *argus_core_view(const ArgusCore *core)
426{
427 if (!core || core->magic != CORE_MAGIC)
428 return NULL;
429 return &core->view;
430}
431
432/* ---- findings ------------------------------------------------------------- */
433
434static void core_finding(ArgusFinding *f, uint16_t code, uint8_t severity,
435 const ArgusEvent *ev, uint64_t prior_sequence)
436{
437 memset(f, 0, sizeof *f);
438 f->code = code;
439 f->severity = severity;
440 f->confidence = ARGUS_CONF_DETERMINISTIC;
441 f->sync_allowed = 0;
442 f->containment = ARGUS_CONTAIN_NONE;
443 f->detector = code;
444 f->sequence = ev->sequence;
445 f->prior_sequence = prior_sequence;
446 f->principal = ev->principal;
447 f->cap_id = ev->cap_id;
448 f->cap_generation = ev->cap_generation;
449 memcpy(f->machine_id, ev->machine_id, ARGUS_MACHINE_ID_LEN);
450}
451
452static void note_incident(struct ArgusCore *c, const ArgusFinding *f)
453{
454 if (f->severity < ARGUS_SEV_HIGH)
455 return;
456 for (uint32_t i = 0; i < c->n_incidents; i++) {
457 CoreIncident *in = &c->incidents[i];
458 if (in->principal == f->principal && in->code == f->code) {
459 if (f->severity > in->severity)
460 in->severity = f->severity;
461 return;
462 }
463 }
464 if (c->n_incidents >= ARGUS_CORE_INCIDENTS) {
465 c->incidents_untracked++;
466 return;
467 }
468 CoreIncident *in = &c->incidents[c->n_incidents++];
469 in->principal = f->principal;
470 in->code = f->code;
471 in->severity = f->severity;
472 in->first_sequence = f->sequence;
473}
474
475/* Stamp, count, copy out. Returns the number copied. */
476static size_t deliver(struct ArgusCore *c, const ArgusEvent *ev, size_t n, ArgusFinding *out, size_t cap)
477{
478 ArgusFinding *sf = c->scratch;
479 if (n) {
480 uint8_t ed[ARGUS_DIGEST_LEN];
481 argus_event_digest(ev, ed);
482 for (size_t i = 0; i < n; i++) {
483 memcpy(sf[i].event_digest, ed, ARGUS_DIGEST_LEN);
484 note_incident(c, &sf[i]);
485 }
486 }
487 c->findings_emitted += n;
488 size_t k = n < cap ? n : cap;
489 if (k)
490 memcpy(out, sf, k * sizeof *sf);
491 return k;
492}
493
494/* ---- well-formedness ------------------------------------------------------ */
495
496/* v1.2: containment lifecycle kinds 90-93 (never detector triggers, no shadow change). */
497static int is_containment_kind(uint16_t kind)
498{
499 return kind >= ARGUS_EV_CONTAINMENT_PROPOSED && kind <= ARGUS_EV_AUTHORITY_ESCALATED;
500}
501
502static int event_well_formed(const ArgusEvent *ev)
503{
504 if (argus_event_validate(ev) != ARGUS_OK)
505 return 0;
506 if (ev->cap_id >= ARGUS_CAP_MAX && ev->cap_id != ARGUS_CAP_NONE) /* header rule; never "table full" */
507 return 0;
508 if (ev->sequence == UINT64_MAX) /* header rule; high-water poison */
509 return 0;
510 return 1;
511}
512
513/* ---- sequence check ------------------------------------------------------- */
514
515static int producer_cmp(const CoreProducer *p, const uint8_t id[ARGUS_MACHINE_ID_LEN], uint16_t stream, uint16_t consumer)
516{
517 int c = memcmp(p->machine_id, id, ARGUS_MACHINE_ID_LEN);
518 if (c)
519 return c;
520 if (p->stream != stream)
521 return p->stream < stream ? -1 : 1;
522 return p->consumer < consumer ? -1 : p->consumer > consumer;
523}
524
525/* Returns 1 on anomaly (and sets *prior), 0 if in order, -1 if the producer
526 * table is full (stream not tracked). Advances the high-water mark. */
527static int sequence_check(struct ArgusCore *c, const ArgusEvent *ev, uint64_t *prior)
528{
529 uint16_t consumer = (ev->flags & ARGUS_FLAG_CONSUMER) ? 1u : 0u;
530 uint16_t stream = ARGUS_STREAM_OF(ev->flags);
531 uint32_t lo = 0, hi = c->n_producers;
532 while (lo < hi) {
533 uint32_t mid = lo + (hi - lo) / 2u;
534 int r = producer_cmp(&c->producers[mid], ev->machine_id, stream, consumer);
535 if (r == 0) {
536 CoreProducer *p = &c->producers[mid];
537 if (ev->sequence <= p->last_sequence) {
538 *prior = p->last_sequence;
539 return 1;
540 }
541 p->last_sequence = ev->sequence;
542 return 0;
543 }
544 if (r < 0)
545 lo = mid + 1u;
546 else
547 hi = mid;
548 }
549 if (c->n_producers >= ARGUS_CORE_PRODUCERS)
550 return -1;
551 if (lo < c->n_producers)
552 memmove(&c->producers[lo + 1u], &c->producers[lo], (c->n_producers - lo) * sizeof c->producers[0]);
553 c->n_producers++;
554 CoreProducer *p = &c->producers[lo];
555 memset(p, 0, sizeof *p);
556 memcpy(p->machine_id, ev->machine_id, ARGUS_MACHINE_ID_LEN);
557 p->stream = stream;
558 p->consumer = consumer;
559 p->last_sequence = ev->sequence;
560 return 0;
561}
562
563/* ---- apply verdict (pre-state) -------------------------------------------- */
564
565enum { V_APPLY = 0, V_REPLAY = 1, V_SKIP = 2, V_NOOP = 3 };
566
567static int apply_verdict(const struct ArgusStateView *v, const ArgusEvent *ev, uint64_t *prior)
568{
569 if (ev->outcome != ARGUS_OUTCOME_OK)
570 return V_APPLY; /* outcome gating in apply_event */
571 switch (ev->kind) {
572 case ARGUS_EV_CAPABILITY_GRANTED:
573 case ARGUS_EV_CAPABILITY_REVOKED: {
574 if (ev->cap_id == ARGUS_CAP_NONE)
575 return V_APPLY; /* no capability: apply_cap is a no-op */
576 const ArgusCapShadow *s = &v->caps[ev->cap_id];
577 if (ev->kind == ARGUS_EV_CAPABILITY_GRANTED) {
578 if (s->state == ARGUS_SHADOW_UNSEEN || ev->cap_generation > s->generation)
579 return V_APPLY;
580 } else if (s->state == ARGUS_SHADOW_LIVE && ev->cap_generation == s->generation) {
581 return V_APPLY;
582 }
583 *prior = s->state == ARGUS_SHADOW_REVOKED ? s->revoked_sequence : s->granted_sequence;
584 return V_REPLAY;
585 }
586 case ARGUS_EV_CREDENTIAL_LEASE_CREATED:
587 if (ev->object_id != 0 && find_lease(v, ev->object_id) >= 0)
588 return V_SKIP; /* lease ids are never reused */
589 return V_APPLY;
590 case ARGUS_EV_CREDENTIAL_LEASE_REVOKED:
591 if (ev->object_id != 0 && find_lease(v, ev->object_id) < 0)
592 return V_SKIP; /* unknown lease: no entry */
593 return V_APPLY;
594 case ARGUS_EV_WORLD_COMMITTED: { /* per store: object_id = store id (v1.1) */
595 if (ev->world_generation == 0)
596 return V_NOOP;
597 int wi = find_world(v, ev->object_id);
598 const ArgusWorldShadow *w = wi >= 0 ? &v->worlds[wi] : NULL;
599 if (!w || w->generation == 0 || ev->world_generation == w->generation + 1u)
600 return V_APPLY; /* first commit of this store, or +1 */
601 if (ev->world_generation == w->generation &&
602 memcmp(ev->evidence_digest, w->digest, ARGUS_DIGEST_LEN) == 0)
603 return V_NOOP; /* idempotent repeat */
604 return V_SKIP;
605 }
606 case ARGUS_EV_MACHINE_TRUST_CHANGED: {
607 int i = find_machine(v, ev->machine_id);
608 if (i < 0)
609 return V_SKIP; /* unknown machine: no entry */
610 if (!trust_valid(ev->object_id))
611 return V_NOOP;
612 uint32_t cur = v->machines[i].trust;
613 if (trust_rank_tab[ev->object_id] > trust_rank_tab[cur])
614 return V_APPLY; /* downward move */
615 return trust_rank_tab[ev->object_id] == trust_rank_tab[cur] ? V_NOOP : V_SKIP;
616 }
617 default:
618 return V_APPLY;
619 }
620}
621
622/* ---- state update (only called when the verdict allows it) ---------------- */
623
624static int apply_cap(struct ArgusStateView *v, const ArgusEvent *ev, int revoke)
625{
626 if (ev->cap_id >= ARGUS_CORE_CAPS)
627 return ARGUS_OK; /* ARGUS_CAP_NONE: no capability; cap_id 0 is a real slot */
628 ArgusCapShadow *s = &v->caps[ev->cap_id];
629 if (revoke) {
630 s->state = ARGUS_SHADOW_REVOKED; /* verdict: slot LIVE at this generation */
631 s->revoked_sequence = ev->sequence;
632 return ARGUS_OK;
633 }
634 if (s->state == ARGUS_SHADOW_UNSEEN) {
635 memset(s, 0, sizeof *s);
636 v->n_caps_live++;
637 }
638 s->cap_id = ev->cap_id;
639 s->generation = ev->cap_generation;
640 s->state = ARGUS_SHADOW_LIVE;
641 s->subject = ev->principal;
642 s->rights = (uint32_t)ev->object_id;
643 s->resource = ev->resource;
644 s->granted_sequence = ev->sequence;
645 s->revoked_sequence = 0;
646 return ARGUS_OK;
647}
648
649static int apply_lease(struct ArgusStateView *v, const ArgusEvent *ev, int revoke)
650{
651 if (ev->object_id == 0)
652 return ARGUS_OK;
653 int i = find_lease(v, ev->object_id);
654 if (revoke) {
655 if (i >= 0) {
656 v->leases[i].state = ARGUS_SHADOW_REVOKED;
657 v->leases[i].sequence = ev->sequence;
658 }
659 return ARGUS_OK;
660 }
661 if (i >= 0)
662 return ARGUS_OK; /* verdict already refused reuse */
663 if (v->n_leases >= ARGUS_CORE_LEASES)
664 return ARGUS_ERR_FULL;
665 ArgusLeaseShadow *s = &v->leases[v->n_leases++];
666 memset(s, 0, sizeof *s);
667 s->lease_id = ev->object_id;
668 s->state = ARGUS_SHADOW_LIVE;
669 s->subject = ev->principal;
670 s->scope = ev->resource;
671 s->sequence = ev->sequence;
672 return ARGUS_OK;
673}
674
675static int apply_artifact(struct ArgusStateView *v, const ArgusEvent *ev, int reject)
676{
677 if (digest_is_zero(ev->evidence_digest))
678 return ARGUS_OK;
679 int i = find_artifact(v, ev->evidence_digest);
680 if (i < 0) {
681 if (v->n_artifacts >= ARGUS_CORE_ARTIFACTS)
682 return ARGUS_ERR_FULL;
683 i = (int)v->n_artifacts++;
684 ArgusArtifactShadow *n = &v->artifacts[i];
685 memset(n, 0, sizeof *n);
686 memcpy(n->digest, ev->evidence_digest, ARGUS_DIGEST_LEN);
687 }
688 ArgusArtifactShadow *s = &v->artifacts[i];
689 if (reject) {
690 s->state = ARGUS_SHADOW_REJECTED;
691 s->sequence = ev->sequence;
692 } else if (s->state != ARGUS_SHADOW_REJECTED) {
693 s->state = ARGUS_SHADOW_LIVE;
694 s->sequence = ev->sequence;
695 }
696 return ARGUS_OK;
697}
698
699static int apply_machine(struct ArgusStateView *v, const ArgusEvent *ev)
700{
701 int i = find_machine(v, ev->machine_id);
702 if (ev->kind == ARGUS_EV_MACHINE_REMOVED) {
703 if (i >= 0) { /* tombstone: trust preserved */
704 v->machines[i].joined_sequence = 0;
705 v->machines[i].changed_sequence = ev->sequence;
706 }
707 return ARGUS_OK;
708 }
709 if (ev->kind == ARGUS_EV_MACHINE_TRUST_CHANGED) {
710 if (i >= 0) { /* verdict: known machine, downward move */
711 v->machines[i].trust = ev->object_id;
712 v->machines[i].changed_sequence = ev->sequence;
713 }
714 return ARGUS_OK;
715 }
716 /* JOINED */
717 uint32_t claim = trust_valid(ev->object_id) ? trust_weaker(ARGUS_TRUST_OBSERVED, ev->object_id)
718 : (uint32_t)ARGUS_TRUST_OBSERVED;
719 if (i >= 0) {
720 ArgusMachineShadow *m = &v->machines[i];
721 uint32_t t = trust_weaker(m->trust, claim);
722 if (m->joined_sequence == 0)
723 m->joined_sequence = ev->sequence; /* rejoin of a tombstone */
724 if (t != m->trust) {
725 m->trust = t;
726 m->changed_sequence = ev->sequence;
727 }
728 return ARGUS_OK;
729 }
730 if (v->n_machines >= ARGUS_CORE_MACHINES)
731 return ARGUS_ERR_FULL;
732 uint32_t at = (uint32_t)(-(i + 1));
733 if (at < v->n_machines)
734 memmove(&v->machines[at + 1u], &v->machines[at], (v->n_machines - at) * sizeof v->machines[0]);
735 v->n_machines++;
736 ArgusMachineShadow *n = &v->machines[at];
737 memset(n, 0, sizeof *n);
738 memcpy(n->machine_id, ev->machine_id, ARGUS_MACHINE_ID_LEN);
739 n->trust = claim;
740 n->joined_sequence = ev->sequence;
741 n->changed_sequence = ev->sequence;
742 return ARGUS_OK;
743}
744
745static int apply_provider(struct ArgusStateView *v, const ArgusEvent *ev, int quarantine)
746{
747 if (digest_is_zero(ev->evidence_digest))
748 return ARGUS_OK;
749 int i = find_provider(v, ev->evidence_digest);
750 if (i >= 0 && !quarantine)
751 return ARGUS_OK; /* rediscovery never clears quarantine */
752 if (i < 0) {
753 if (v->n_providers >= ARGUS_CORE_PROVIDERS)
754 return ARGUS_ERR_FULL;
755 i = (int)v->n_providers++;
756 ArgusProviderShadow *n = &v->providers[i];
757 memset(n, 0, sizeof *n);
758 memcpy(n->provider_id, ev->evidence_digest, ARGUS_DIGEST_LEN);
759 }
760 v->providers[i].state = quarantine ? ARGUS_SHADOW_REVOKED : ARGUS_SHADOW_LIVE;
761 v->providers[i].sequence = ev->sequence;
762 return ARGUS_OK;
763}
764
765/* verdict: first commit of the store or shadow+1. A new store needs a table slot;
766 * with ARGUS_CORE_WORLDS stores taken the update is dropped (ERR_FULL). */
767static int apply_world(struct ArgusStateView *v, const ArgusEvent *ev)
768{
769 int i = find_world(v, ev->object_id);
770 if (i < 0) {
771 if (v->n_worlds >= ARGUS_CORE_WORLDS)
772 return ARGUS_ERR_FULL;
773 uint32_t at = (uint32_t)(-(i + 1));
774 if (at < v->n_worlds)
775 memmove(&v->worlds[at + 1u], &v->worlds[at], (v->n_worlds - at) * sizeof v->worlds[0]);
776 v->n_worlds++;
777 i = (int)at;
778 memset(&v->worlds[i], 0, sizeof v->worlds[i]);
779 v->worlds[i].store_id = ev->object_id;
780 }
781 ArgusWorldShadow *w = &v->worlds[i];
782 w->generation = ev->world_generation;
783 memcpy(w->digest, ev->evidence_digest, ARGUS_DIGEST_LEN);
784 w->sequence = ev->sequence;
785 return ARGUS_OK;
786}
787
788static int apply_event(struct ArgusStateView *v, const ArgusEvent *ev)
789{
790 int ok = ev->outcome == ARGUS_OUTCOME_OK;
791 switch (ev->kind) {
792 case ARGUS_EV_CAPABILITY_GRANTED:
793 return ok ? apply_cap(v, ev, 0) : ARGUS_OK;
794 case ARGUS_EV_CAPABILITY_REVOKED:
795 return ok ? apply_cap(v, ev, 1) : ARGUS_OK;
796 case ARGUS_EV_CREDENTIAL_LEASE_CREATED:
797 return ok ? apply_lease(v, ev, 0) : ARGUS_OK;
798 case ARGUS_EV_CREDENTIAL_LEASE_REVOKED:
799 return ok ? apply_lease(v, ev, 1) : ARGUS_OK;
800 case ARGUS_EV_ARTIFACT_ADMITTED:
801 return (ok && ev->code == 0) ? apply_artifact(v, ev, 0) : ARGUS_OK;
802 case ARGUS_EV_ARTIFACT_REJECTED:
803 return (ok || ev->outcome == ARGUS_OUTCOME_DENIED) ? apply_artifact(v, ev, 1) : ARGUS_OK;
804 case ARGUS_EV_MACHINE_JOINED:
805 case ARGUS_EV_MACHINE_TRUST_CHANGED:
806 case ARGUS_EV_MACHINE_REMOVED:
807 return ok ? apply_machine(v, ev) : ARGUS_OK;
808 case ARGUS_EV_PROVIDER_DISCOVERED:
809 return ok ? apply_provider(v, ev, 0) : ARGUS_OK;
810 case ARGUS_EV_PROVIDER_QUARANTINED:
811 return ok ? apply_provider(v, ev, 1) : ARGUS_OK;
812 case ARGUS_EV_WORLD_COMMITTED:
813 return ok ? apply_world(v, ev) : ARGUS_OK;
814 case ARGUS_EV_POLICY_CHANGED:
815 if (ok)
816 memcpy(v->policy_digest, ev->evidence_digest, ARGUS_DIGEST_LEN);
817 return ARGUS_OK;
818 case ARGUS_EV_RUNTIME_BUILD_CHANGED:
819 if (ok)
820 memcpy(v->runtime_digest, ev->evidence_digest, ARGUS_DIGEST_LEN);
821 return ARGUS_OK;
822 default:
823 return ARGUS_OK; /* observation-only kinds */
824 }
825}
826
827/* Shadow tables that can overflow (caps are a direct index and cannot). */
828enum { CORE_TBL_MACHINES = 1u, CORE_TBL_LEASES = 2u, CORE_TBL_PROVIDERS = 4u, CORE_TBL_ARTIFACTS = 8u,
829 CORE_TBL_WORLDS = 16u };
830
831static uint32_t table_of(uint16_t kind)
832{
833 switch (kind) {
834 case ARGUS_EV_MACHINE_JOINED: case ARGUS_EV_MACHINE_TRUST_CHANGED: case ARGUS_EV_MACHINE_REMOVED:
835 return CORE_TBL_MACHINES;
836 case ARGUS_EV_CREDENTIAL_LEASE_CREATED: case ARGUS_EV_CREDENTIAL_LEASE_REVOKED:
837 return CORE_TBL_LEASES;
838 case ARGUS_EV_PROVIDER_DISCOVERED: case ARGUS_EV_PROVIDER_QUARANTINED:
839 return CORE_TBL_PROVIDERS;
840 case ARGUS_EV_ARTIFACT_ADMITTED: case ARGUS_EV_ARTIFACT_REJECTED:
841 return CORE_TBL_ARTIFACTS;
842 case ARGUS_EV_WORLD_COMMITTED:
843 return CORE_TBL_WORLDS;
844 default:
845 return 0;
846 }
847}
848
849/* ---- ingest --------------------------------------------------------------- */
850
851int argus_core_ingest(ArgusCore *core, const ArgusEvent *ev, ArgusFinding *out, size_t cap, size_t *n_out)
852{
853 if (!core || core->magic != CORE_MAGIC || !ev || !n_out || (!out && cap != 0))
854 return ARGUS_ERR_ARG;
855 struct ArgusCore *c = core;
856 ArgusFinding *sf = c->scratch;
857 *n_out = 0;
858 c->events_received++;
859
860 /* 0. malformed: one MEDIUM finding, not applied, not chained. */
861 if (!event_well_formed(ev)) {
862 c->events_rejected++;
863 core_finding(&sf[0], ARGUS_F_MALFORMED_EVENT, ARGUS_SEV_MEDIUM, ev, 0);
864 *n_out = deliver(c, ev, 1, out, cap);
865 return ARGUS_ERR_MALFORMED;
866 }
867
868 size_t n = 0;
869 int full = 0, loud = 0, apply = 1;
870
871 /* 1. per-producer sequence check (lane E cannot see history). */
872 uint64_t prior = 0;
873 int sq = sequence_check(c, ev, &prior);
874 if (sq > 0) {
875 core_finding(&sf[n++], ARGUS_F_SEQUENCE_ANOMALY, ARGUS_SEV_HIGH, ev, prior);
876 apply = 0;
877 } else if (sq < 0) {
878 if (c->producers_untracked++ == 0)
879 full = loud = 1; /* reported once, not per event */
880 }
881
882 /* 2. apply verdict on the pre-state. */
883 if (apply) {
884 uint64_t rp = 0;
885 int vd = apply_verdict(&c->view, ev, &rp);
886 if (vd == V_REPLAY)
887 core_finding(&sf[n++], ARGUS_F_AUTHORITY_REPLAY, ARGUS_SEV_HIGH, ev, rp);
888 if (vd != V_APPLY)
889 apply = 0;
890 if (vd == V_REPLAY || vd == V_SKIP)
891 c->events_not_applied++;
892 } else {
893 c->events_not_applied++;
894 }
895
896 /* 3. detectors on PRE-update state; two slots stay reserved for the core.
897 * v1.2: containment kinds 90-93 are never detector triggers (spec 3). */
898 int drc = ARGUS_OK;
899 if (!is_containment_kind(ev->kind)) {
900 size_t dcap = ARGUS_CORE_MAX_FINDINGS - CORE_RESERVED_SLOTS - n;
901 size_t dn = 0;
902 drc = argus_detect_run(&core_ops, &c->view, ev, &sf[n], dcap, &dn);
903 if (dn > dcap)
904 dn = dcap;
905 n += dn;
906 }
907
908 /* 4. apply the event's own update. */
909 if (apply && apply_event(&c->view, ev) == ARGUS_ERR_FULL) {
910 /* ruling (a), loud once PER TABLE: the first overflow of a table raises one
911 * CRITICAL TELEMETRY_LOSS; later overflows of the same table return
912 * ERR_FULL with no finding. Every overflow is counted (tables_full and
913 * events_not_applied): the update was dropped. */
914 uint32_t t = table_of(ev->kind);
915 full = 1;
916 c->events_not_applied++;
917 if (!(c->full_reported & t)) {
918 c->full_reported |= t;
919 loud = 1;
920 }
921 }
922
923 /* 5. evidence gaps. */
924 if (ev->kind == ARGUS_EV_TELEMETRY_DROPPED &&
925 (ev->object_id == ARGUS_CLASS_CRITICAL || ev->object_id == ARGUS_CLASS_SECURITY)) {
926 uint8_t sev = ev->object_id == ARGUS_CLASS_CRITICAL ? ARGUS_SEV_CRITICAL : ARGUS_SEV_HIGH;
927 core_finding(&sf[n++], ARGUS_F_TELEMETRY_LOSS, sev, ev, 0);
928 }
929 if (loud)
930 core_finding(&sf[n++], ARGUS_F_TELEMETRY_LOSS, ARGUS_SEV_CRITICAL, ev, 0);
931 if (full)
932 c->tables_full++;
933
934 /* 6. chain. */
935 argus_chain_extend(c->chain, ev);
936
937 /* 7. stamp, count, copy out. */
938 size_t k = deliver(c, ev, n, out, cap);
939 *n_out = k;
940
941 if (full)
942 return ARGUS_ERR_FULL;
943 if (k < n)
944 return ARGUS_ERR_OVERFLOW;
945 return drc;
946}
947
948/* ---- canonical state digest ---------------------------------------------- *
949 * Every table is encoded, in fixed order, into ArgusEvent-shaped records and
950 * folded with argus_chain_extend. Domain separation from the event chain is
951 * explicit and twofold: (a) the fold starts from STATE_DOMAIN (a fixed
952 * non-zero 32-byte tag), while the event chain starts from zeros; (b) every
953 * record has version = STATE_REC_VERSION (0xA5), a version no well-formed
954 * event can carry. The records therefore deliberately do NOT pass
955 * argus_event_validate; argus_chain_extend does not validate (header convention).
956 * `code` carries a record tag so records of different tables never collide.
957 * tag 1 header: object_id=n_caps, cap_id=n_machines, principal=n_artifacts,
958 * cap_generation=(n_leases<<32)|n_providers,
959 * resource=(n_producers<<32)|n_incidents, tick=incidents_untracked,
960 * world_generation=producers_untracked, sequence=n_worlds
961 * tag 2 cap, 3 machine, 4 artifact, 5 lease, 6 provider,
962 * tag 7 world (one per store: object_id=store_id; v1.1),
963 * tag 8 policy, 9 runtime, 10 producer (object_id=consumer, principal=stream; v1.1),
964 * tag 11 incident
965 * Machines are folded in machine_id order (the table is kept sorted), so the
966 * digest depends on the set of machines, not on their join order. Producers
967 * (machine_id, stream, consumer) and World stores (store_id) are sorted likewise.
968 * Other tables fold in insertion order (replay-deterministic).
969 * Counters (received/rejected/findings/tables_full/not_applied) and the event
970 * chain are not state and are excluded. */
971
972#define STATE_REC_VERSION 0xA5u
973static const uint8_t STATE_DOMAIN[ARGUS_DIGEST_LEN] = {
974 'A', 'R', 'G', 'U', 'S', '-', '0', ' ', 'c', 'o', 'r', 'e', ' ', 's', 't', 'a',
975 't', 'e', ' ', 'd', 'i', 'g', 'e', 's', 't', ' ', 'v', '3', 0xA5, 0x5A, 0xA5, 0x5A
976};
977
978static void rec_init(ArgusEvent *r, uint16_t kind, int32_t tag)
979{
980 memset(r, 0, sizeof *r);
981 r->version = STATE_REC_VERSION;
982 r->class_ = ARGUS_CLASS_CRITICAL;
983 r->kind = kind;
984 r->effect_class = ARGUS_EFFECT_NONE;
985 r->outcome = ARGUS_OUTCOME_OK;
986 r->flags = ARGUS_FLAG_CONSUMER;
987 r->code = tag;
988}
989
990void argus_core_state_digest(const ArgusCore *core, uint8_t out[ARGUS_DIGEST_LEN])
991{
992 if (!out)
993 return;
994 memset(out, 0, ARGUS_DIGEST_LEN);
995 if (!core || core->magic != CORE_MAGIC)
996 return;
997 memcpy(out, STATE_DOMAIN, ARGUS_DIGEST_LEN);
998 const struct ArgusStateView *v = &core->view;
999 ArgusEvent r;
1000
1001 rec_init(&r, ARGUS_EV_INTEGRITY_VIOLATION, 1);
1002 r.object_id = v->n_caps_live;
1003 r.cap_id = v->n_machines;
1004 r.principal = v->n_artifacts;
1005 r.cap_generation = ((uint64_t)v->n_leases << 32) | v->n_providers;
1006 r.resource = ((uint64_t)core->n_producers << 32) | core->n_incidents;
1007 r.tick = core->incidents_untracked;
1008 r.world_generation = core->producers_untracked;
1009 r.sequence = v->n_worlds;
1010 argus_chain_extend(out, &r);
1011
1012 for (uint32_t i = 0; i < ARGUS_CORE_CAPS; i++) {
1013 const ArgusCapShadow *s = &v->caps[i];
1014 if (s->state == ARGUS_SHADOW_UNSEEN)
1015 continue;
1016 rec_init(&r, ARGUS_EV_CAPABILITY_GRANTED, 2);
1017 r.cap_id = s->cap_id;
1018 r.cap_generation = s->generation;
1019 r.world_generation = s->state;
1020 r.principal = s->subject;
1021 r.object_id = s->rights;
1022 r.resource = s->resource;
1023 r.sequence = s->granted_sequence;
1024 r.tick = s->revoked_sequence;
1025 argus_chain_extend(out, &r);
1026 }
1027 for (uint32_t i = 0; i < v->n_machines; i++) {
1028 const ArgusMachineShadow *s = &v->machines[i];
1029 rec_init(&r, ARGUS_EV_MACHINE_JOINED, 3);
1030 memcpy(r.machine_id, s->machine_id, ARGUS_MACHINE_ID_LEN);
1031 r.object_id = s->trust;
1032 r.sequence = s->joined_sequence;
1033 r.tick = s->changed_sequence;
1034 argus_chain_extend(out, &r);
1035 }
1036 for (uint32_t i = 0; i < v->n_artifacts; i++) {
1037 const ArgusArtifactShadow *s = &v->artifacts[i];
1038 rec_init(&r, ARGUS_EV_ARTIFACT_ADMITTED, 4);
1039 memcpy(r.evidence_digest, s->digest, ARGUS_DIGEST_LEN);
1040 r.object_id = s->state;
1041 r.sequence = s->sequence;
1042 argus_chain_extend(out, &r);
1043 }
1044 for (uint32_t i = 0; i < v->n_leases; i++) {
1045 const ArgusLeaseShadow *s = &v->leases[i];
1046 rec_init(&r, ARGUS_EV_CREDENTIAL_LEASE_CREATED, 5);
1047 r.object_id = s->lease_id;
1048 r.principal = s->subject;
1049 r.resource = s->scope;
1050 r.cap_id = s->state;
1051 r.sequence = s->sequence;
1052 argus_chain_extend(out, &r);
1053 }
1054 for (uint32_t i = 0; i < v->n_providers; i++) {
1055 const ArgusProviderShadow *s = &v->providers[i];
1056 rec_init(&r, ARGUS_EV_PROVIDER_DISCOVERED, 6);
1057 memcpy(r.evidence_digest, s->provider_id, ARGUS_DIGEST_LEN);
1058 r.object_id = s->state;
1059 r.sequence = s->sequence;
1060 argus_chain_extend(out, &r);
1061 }
1062 for (uint32_t i = 0; i < v->n_worlds; i++) {
1063 const ArgusWorldShadow *w = &v->worlds[i];
1064 rec_init(&r, ARGUS_EV_WORLD_COMMITTED, 7);
1065 r.object_id = w->store_id;
1066 r.world_generation = w->generation;
1067 memcpy(r.evidence_digest, w->digest, ARGUS_DIGEST_LEN);
1068 r.sequence = w->sequence;
1069 argus_chain_extend(out, &r);
1070 }
1071
1072 rec_init(&r, ARGUS_EV_POLICY_CHANGED, 8);
1073 memcpy(r.evidence_digest, v->policy_digest, ARGUS_DIGEST_LEN);
1074 argus_chain_extend(out, &r);
1075
1076 rec_init(&r, ARGUS_EV_RUNTIME_BUILD_CHANGED, 9);
1077 memcpy(r.evidence_digest, v->runtime_digest, ARGUS_DIGEST_LEN);
1078 argus_chain_extend(out, &r);
1079
1080 for (uint32_t i = 0; i < core->n_producers; i++) {
1081 const CoreProducer *p = &core->producers[i];
1082 rec_init(&r, ARGUS_EV_STALE_GENERATION, 10);
1083 memcpy(r.machine_id, p->machine_id, ARGUS_MACHINE_ID_LEN);
1084 r.object_id = p->consumer;
1085 r.principal = p->stream;
1086 r.sequence = p->last_sequence;
1087 argus_chain_extend(out, &r);
1088 }
1089 for (uint32_t i = 0; i < core->n_incidents; i++) {
1090 const CoreIncident *in = &core->incidents[i];
1091 rec_init(&r, ARGUS_EV_SIGNATURE_FAILURE, 11);
1092 r.principal = in->principal;
1093 r.object_id = in->code;
1094 r.cap_id = in->severity;
1095 r.sequence = in->first_sequence;
1096 argus_chain_extend(out, &r);
1097 }
1098}
1099
1100void argus_core_health(const ArgusCore *core, ArgusCoreHealth *out)
1101{
1102 if (!out)
1103 return;
1104 memset(out, 0, sizeof *out);
1105 if (!core || core->magic != CORE_MAGIC)
1106 return;
1107 out->events_received = core->events_received;
1108 out->events_rejected = core->events_rejected;
1109 out->findings_emitted = core->findings_emitted;
1110 out->incidents_open = core->n_incidents;
1111 out->detector_ns_total = 0;
1112 out->incidents_untracked = core->incidents_untracked;
1113 out->producers_untracked = core->producers_untracked;
1114 out->tables_full = core->tables_full;
1115 out->events_not_applied = core->events_not_applied;
1116 memcpy(out->chain, core->chain, ARGUS_DIGEST_LEN);
1117}
1118
1119/* v1.2 (spec 4): first_sequence of the tracked (principal, code) incident.
1120 * ARGUS_OK and *first_sequence set when the pair is in the incident table;
1121 * ARGUS_ERR_STATE when it is not (never raised, below HIGH, or untracked
1122 * because the table was full); ARGUS_ERR_ARG on a bad core or NULL output.
1123 * Read-only, bounded scan (ARGUS_CORE_INCIDENTS), no new state. */
1124int argus_core_incident(const ArgusCore *core, uint32_t principal, uint16_t code, uint64_t *first_sequence)
1125{
1126 if (!core || core->magic != CORE_MAGIC || !first_sequence)
1127 return ARGUS_ERR_ARG;
1128 for (uint32_t i = 0; i < core->n_incidents; i++) {
1129 const CoreIncident *in = &core->incidents[i];
1130 if (in->principal == principal && in->code == code) {
1131 *first_sequence = in->first_sequence;
1132 return ARGUS_OK;
1133 }
1134 }
1135 return ARGUS_ERR_STATE;
1136}