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

1626 lines · 71536 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * test_argus_hostile.c -- ARGUS-0 hostile review (lane G): negative tests.
19 *
20 * Every test is written against the public contract in argus_abi.h only
21 * (argus_core_*, argus_ring_*, argus_event_*, argus_detect_run). Each prints
22 * HOSTILE <name> PASS|FAIL
23 * where PASS means "the defended behavior was observed". Tests for issues the
24 * review found OPEN or WEAK are marked EXPECTED-FAIL below and in the output:
25 * they FAIL today on purpose, so the integrator sees the hole. When one starts
26 * passing it prints XPASS: update docs/HOSTILE_REVIEW.md and drop the marker.
27 * The process exits nonzero only on an UNEXPECTED failure (a test that should
28 * pass and did not), so "OPEN as documented" and "regression" stay distinct.
29 * A third mode, N/A-v1, marks an attack that ABI v1 accepts by design (a
30 * documented limitation, not a defect): it prints PASS|FAIL with an [N/A-v1]
31 * marker, is counted separately, and never affects the exit code.
32 *
33 * Integrator round 2 (after lanes B 58d5a99, D 57a7bea, E a33c1e0): every
34 * test that became defended was flipped from EXPECTED-FAIL to expected-pass;
35 * the ruled corrections (ev0 class floor, code-16 outcomes, 0x7FFF flood kind,
36 * world rollback case, table sizes, ratified producer overflow, zero-machine
37 * flags, idempotent World repeat) are applied and commented where they occur.
38 *
39 * Section numbers (G-x) refer to docs/HOSTILE_REVIEW.md.
40 *
41 * Build: `make test-hostile` (part of `make test`; links out/libargus.a only).
42 * By hand (from native/argus, same CFLAGS as ../capability/Makefile):
43 * cc -std=gnu11 -O2 -Wall -Wextra -Werror -fstack-protector-strong -I. \
44 * -o out/test_argus_hostile tests/test_argus_hostile.c \
45 * argus_core.c argus_event.c argus_ring.c sha256.c argus_detect.c
46 * It deliberately does NOT link the capability authority: if any ARGUS
47 * object ever referenced an aienos_cap_* symbol, this link would fail (G-1).
48 */
49#include "../argus_abi.h"
50
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55/* Mirrors of argus_core.h capacities (lane D, 57a7bea). The exhaustion tests
56 * fill each table exactly and then overshoot it, so they test the limit itself;
57 * if a size changes, change it here (the tests then still overshoot). */
58#define H_MACHINES 64u
59#define H_PRODUCERS 256u
60#define H_INCIDENTS 256u
61#define H_LEASES 256u
62#define H_ARTIFACTS 256u
63#define H_PROVIDERS 32u
64#define H_WORLDS 8u /* ARGUS_WORLD_STORES (v1.1 per-store shadows) */
65
66/* A kind value outside ABI v1. argus_event_min_class() returns 0 for it, so the
67 * ring (transport only) accepts it at any class, while the core's validate
68 * rejects it. Used wherever a test needs INFORMATIONAL/AUDIT ring traffic:
69 * no v1 kind has an INFORMATIONAL floor (design fact, argus_event.c). */
70#define K_NON_V1 0x7FFFu
71#define H_FBUF 64u
72
73/* ---- reporting ------------------------------------------------------------ */
74
75static int n_pass, n_xfail, n_xpass, n_unexpected, n_na;
76
77enum { EXPECT_DEFENDED = 0, EXPECT_FAIL = 1, NA_V1 = 2 };
78
79static void result(const char *name, int defended, int expected_fail, const char *why)
80{
81 printf("HOSTILE %s %s", name, defended ? "PASS" : "FAIL");
82 if (expected_fail == NA_V1) {
83 n_na++;
84 printf(" [N/A-v1: documented limitation, exempt from exit code: %s]", why);
85 } else if (expected_fail && !defended) {
86 n_xfail++;
87 printf(" [EXPECTED-FAIL: %s]", why);
88 } else if (expected_fail) {
89 n_xpass++;
90 printf(" [XPASS: was OPEN/WEAK, now defended; update HOSTILE_REVIEW.md]");
91 } else if (defended) {
92 n_pass++;
93 } else {
94 n_unexpected++;
95 printf(" [UNEXPECTED: %s]", why);
96 }
97 printf("\n");
98}
99
100/* ---- harness -------------------------------------------------------------- */
101
102static uint64_t g_seq;
103
104typedef struct {
105 void *mem;
106 ArgusCore *c;
107 ArgusFinding f[H_FBUF];
108 size_t n;
109 int rc;
110} H;
111
112static void *alloc64(size_t bytes)
113{
114 size_t b = (bytes + 63u) & ~(size_t)63u;
115 void *p = aligned_alloc(64, b);
116 if (!p) {
117 fprintf(stderr, "out of memory\n");
118 exit(2);
119 }
120 memset(p, 0, b);
121 return p;
122}
123
124static void h_new(H *h)
125{
126 memset(h, 0, sizeof *h);
127 g_seq = 0;
128 size_t fp = argus_core_footprint();
129 h->mem = alloc64(fp);
130 if (argus_core_init(&h->c, h->mem, fp) != ARGUS_OK) {
131 fprintf(stderr, "argus_core_init failed\n");
132 exit(2);
133 }
134}
135
136static void h_free(H *h)
137{
138 free(h->mem);
139 h->mem = NULL;
140 h->c = NULL;
141}
142
143static void mid(uint8_t id[ARGUS_MACHINE_ID_LEN], uint32_t n)
144{
145 memset(id, 0, ARGUS_MACHINE_ID_LEN);
146 if (n) {
147 id[0] = 0x4D;
148 id[1] = (uint8_t)n;
149 id[2] = (uint8_t)(n >> 8);
150 }
151}
152
153static void dg(uint8_t d[ARGUS_DIGEST_LEN], uint32_t n)
154{
155 memset(d, 0, ARGUS_DIGEST_LEN);
156 d[0] = 0xD1;
157 d[1] = (uint8_t)n;
158 d[2] = (uint8_t)(n >> 8);
159}
160
161static ArgusEvent ev0(uint16_t kind)
162{
163 ArgusEvent e;
164 memset(&e, 0, sizeof e);
165 e.version = ARGUS_ABI_VERSION;
166 /* Weakest class the kind may carry (validate rejects anything weaker);
167 * 0 = unknown kind -> SECURITY. */
168 e.class_ = argus_event_min_class(kind);
169 if (e.class_ == 0)
170 e.class_ = ARGUS_CLASS_SECURITY;
171 e.kind = kind;
172 e.effect_class = ARGUS_EFFECT_NONE;
173 e.outcome = ARGUS_OUTCOME_OK;
174 e.flags = ARGUS_FLAG_SYNTHETIC;
175 e.cap_id = ARGUS_CAP_NONE; /* v1.1: cap 0 is the office capability; "none" is CAP_NONE */
176 return e;
177}
178
179static ArgusEvent grant(uint32_t cap, uint64_t gen, uint32_t subject, uint32_t rights)
180{
181 ArgusEvent e = ev0(ARGUS_EV_CAPABILITY_GRANTED);
182 e.cap_id = cap;
183 e.cap_generation = gen;
184 e.principal = subject;
185 e.object_id = rights;
186 e.resource = 0x10;
187 return e;
188}
189
190static ArgusEvent revoke(uint32_t cap, uint64_t gen)
191{
192 ArgusEvent e = ev0(ARGUS_EV_CAPABILITY_REVOKED);
193 e.cap_id = cap;
194 e.cap_generation = gen;
195 return e;
196}
197
198static ArgusEvent use(uint32_t cap, uint64_t gen, uint32_t principal, uint8_t outcome, int32_t code)
199{
200 ArgusEvent e = ev0(ARGUS_EV_CAPABILITY_USED);
201 e.cap_id = cap;
202 e.cap_generation = gen;
203 e.principal = principal;
204 e.outcome = outcome;
205 e.code = code;
206 e.resource = 0x10;
207 return e;
208}
209
210/* MACHINE_*: trust travels in object_id (ABI b75d357); 0 on JOINED = OBSERVED. */
211static ArgusEvent machine_ev(uint16_t kind, uint32_t m, uint32_t trust)
212{
213 ArgusEvent e = ev0(kind);
214 mid(e.machine_id, m);
215 e.object_id = trust;
216 return e;
217}
218
219/* Ingest; assigns the next sequence if the event has none. */
220static int feed(H *h, ArgusEvent *e)
221{
222 if (e->sequence == 0)
223 e->sequence = ++g_seq;
224 h->n = 0;
225 h->rc = argus_core_ingest(h->c, e, h->f, H_FBUF, &h->n);
226 return h->rc;
227}
228
229static size_t count(const H *h, uint16_t code)
230{
231 size_t k = 0;
232 for (size_t i = 0; i < h->n; i++)
233 if (h->f[i].code == code)
234 k++;
235 return k;
236}
237
238static int cap_state(const H *h, uint32_t cap, ArgusCapShadow *s)
239{
240 return argus_core_ops()->cap(argus_core_view(h->c), cap, s);
241}
242
243static int machine_trust(const H *h, uint32_t m, uint32_t *trust)
244{
245 ArgusMachineShadow ms;
246 uint8_t id[ARGUS_MACHINE_ID_LEN];
247 mid(id, m);
248 if (argus_core_ops()->machine(argus_core_view(h->c), id, &ms) != ARGUS_OK)
249 return 0;
250 *trust = ms.trust;
251 return 1;
252}
253
254/* Rights / result codes: the authority's values (lane E's tests assert its
255 * mirrors equal aienos_capability.h). Mirrored here so this file does not
256 * include or link the authority. */
257#define R_READ 0x1u
258#define R_WRITE 0x2u
259#define R_EFFECT 0x4u
260#define AUTH_ERR_STALE_GEN (-2)
261#define AUTH_ERR_REVOKED (-3)
262
263/* ==== G-3 / G-4: capability replay, stale and future generations ==== */
264
265/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-3, OPEN): a replayed GRANTED for an (id, generation) the
266 * shadow already saw revoked brings the slot back to LIVE, so a later
267 * successful use of the revoked reference raises nothing. */
268static void t_grant_replay_revives_revoked(void)
269{
270 H h; h_new(&h);
271 ArgusEvent e = grant(5, 1, 7, R_READ);
272 feed(&h, &e);
273 e = revoke(5, 1); feed(&h, &e);
274 e = grant(5, 1, 7, R_READ); feed(&h, &e); /* replay, fresh sequence */
275 size_t on_replay = h.n;
276 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
277 int defended = on_replay > 0 || count(&h, ARGUS_F_REVOKED_CAPABILITY_USED) > 0;
278 result("grant_replay_revives_revoked", defended, EXPECT_DEFENDED,
279 "apply_cap overwrites a REVOKED slot on an equal-generation GRANTED; no detector looks at GRANTED");
280 h_free(&h);
281}
282
283/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-3, OPEN): even a byte-exact replay (same sequence) is only
284 * flagged as SEQUENCE_ANOMALY and is then applied, reviving the slot. */
285static void t_exact_replay_still_applied(void)
286{
287 H h; h_new(&h);
288 ArgusEvent g = grant(5, 1, 7, R_READ);
289 feed(&h, &g);
290 ArgusEvent e = revoke(5, 1); feed(&h, &e);
291 ArgusEvent again = g; /* identical bytes */
292 feed(&h, &again);
293 int anomaly = count(&h, ARGUS_F_SEQUENCE_ANOMALY) == 1;
294 ArgusCapShadow s;
295 int still_revoked = cap_state(&h, 5, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED;
296 result("exact_replay_still_applied", anomaly && still_revoked, EXPECT_DEFENDED,
297 "anomalous (replayed) events are still applied to shadow state");
298 h_free(&h);
299}
300
301/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-4, OPEN): a use with a generation HIGHER than any ever
302 * granted for the slot is a reference ARGUS never saw minted; no finding. */
303static void t_future_generation_use_unflagged(void)
304{
305 H h; h_new(&h);
306 ArgusEvent e = grant(5, 3, 7, R_READ); feed(&h, &e);
307 e = use(5, 4, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
308 int defended = count(&h, ARGUS_F_FORGED_CAPABILITY) + count(&h, ARGUS_F_STALE_GENERATION) > 0;
309 result("future_generation_use_unflagged", defended, EXPECT_DEFENDED,
310 "detector 1 checks only UNSEEN slots, detector 2 only lower generations");
311 h_free(&h);
312}
313
314static void t_stale_generation_use_flagged(void)
315{
316 H h; h_new(&h);
317 ArgusEvent e = grant(5, 3, 7, R_READ); feed(&h, &e);
318 e = revoke(5, 3); feed(&h, &e);
319 e = grant(5, 4, 7, R_READ); feed(&h, &e); /* legitimate slot reuse */
320 e = use(5, 3, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
321 result("stale_generation_use_flagged", count(&h, ARGUS_F_STALE_GENERATION) == 1, EXPECT_DEFENDED,
322 "old generation used with outcome OK was not reported");
323 h_free(&h);
324}
325
326static void t_revoked_use_flagged(void)
327{
328 H h; h_new(&h);
329 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
330 e = revoke(5, 1); feed(&h, &e);
331 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
332 int ok = count(&h, ARGUS_F_REVOKED_CAPABILITY_USED) == 1 && h.n >= 1 && h.f[0].prior_sequence == 2;
333 result("revoked_use_flagged", ok, EXPECT_DEFENDED, "revoked reference used OK was not reported with its revocation");
334 h_free(&h);
335}
336
337/* False-positive guard: the authority refusing a revoked reference is correct. */
338static void t_revoked_use_denied_is_quiet(void)
339{
340 H h; h_new(&h);
341 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
342 e = revoke(5, 1); feed(&h, &e);
343 e = use(5, 1, 7, ARGUS_OUTCOME_DENIED, AUTH_ERR_REVOKED); feed(&h, &e);
344 result("revoked_use_denied_is_quiet", h.n == 0 && h.rc == ARGUS_OK, EXPECT_DEFENDED, "authority behaving correctly raised a finding");
345 h_free(&h);
346}
347
348/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-3, OPEN): the shadow records the subject at grant, but a
349 * successful use by a different principal is not cross-checked. */
350static void t_use_by_other_subject_unflagged(void)
351{
352 H h; h_new(&h);
353 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
354 e = use(5, 1, 9, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
355 result("use_by_other_subject_unflagged", h.n > 0, EXPECT_DEFENDED,
356 "no detector compares event principal with the granted subject");
357 h_free(&h);
358}
359
360/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-4, OPEN): one forged REVOKED at generation UINT64_MAX for
361 * a never-granted slot is accepted silently and poisons the slot forever:
362 * every later legitimate grant is ignored and every legitimate use becomes a
363 * CRITICAL REVOKED_CAPABILITY_USED (FREEZE_PRINCIPAL). */
364static void t_revoke_gen_max_poisons_slot(void)
365{
366 H h; h_new(&h);
367 ArgusEvent e = revoke(5, UINT64_MAX); feed(&h, &e);
368 size_t on_forged = h.n;
369 e = grant(5, 1, 7, R_READ); feed(&h, &e);
370 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
371 int false_critical = count(&h, ARGUS_F_REVOKED_CAPABILITY_USED) > 0;
372 result("revoke_gen_max_poisons_slot", on_forged > 0 || !false_critical, EXPECT_DEFENDED,
373 "revocation of an unseen reference creates sticky REVOKED state at any generation");
374 h_free(&h);
375}
376
377/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-20, OPEN): cap_id >= AIENOS_CAP_MAX on GRANTED is a
378 * reference the authority can never produce, but it is reported as ARGUS's
379 * own telemetry loss (ERR_FULL + CRITICAL) instead of malformed/forged.
380 * One event = one CRITICAL, at the attacker's chosen principal. */
381static void t_cap_id_out_of_range_is_critical_loss(void)
382{
383 H h; h_new(&h);
384 ArgusEvent e = grant(300, 1, 1234, R_READ); feed(&h, &e);
385 int defended = h.rc != ARGUS_ERR_FULL && count(&h, ARGUS_F_TELEMETRY_LOSS) == 0;
386 result("cap_id_out_of_range_is_critical_loss", defended, EXPECT_DEFENDED,
387 "validate accepts any cap_id; core maps cap_id>=256 to ERR_FULL + CRITICAL TELEMETRY_LOSS");
388 h_free(&h);
389}
390
391static void t_cap_id_out_of_range_use_is_forged(void)
392{
393 H h; h_new(&h);
394 ArgusEvent e = use(300, 1, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
395 /* Ruled: validate rejects cap_id >= ARGUS_CAP_MAX first, so the defended
396 * outcome is MALFORMED_EVENT (16), or FORGED (1) from the detectors. */
397 result("cap_id_out_of_range_use_is_forged",
398 count(&h, ARGUS_F_FORGED_CAPABILITY) == 1 || count(&h, ARGUS_F_MALFORMED_EVENT) == 1, EXPECT_DEFENDED,
399 "out-of-range reference used OK was not reported as forged");
400 h_free(&h);
401}
402
403/* ==== G-24 effect class ==================================================== */
404
405/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-24, OPEN): an EXTERNAL_EFFECT_COMMITTED labelled
406 * effect_class NONE with no capability escapes detector 8. */
407static void t_effect_kind_mislabelled(void)
408{
409 H h; h_new(&h);
410 ArgusEvent e = ev0(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED);
411 e.effect_class = ARGUS_EFFECT_NONE;
412 feed(&h, &e);
413 /* Ruled: validate rejects an effect kind labelled NONE (code 16). */
414 result("effect_kind_mislabelled",
415 count(&h, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED) == 1 || count(&h, ARGUS_F_MALFORMED_EVENT) == 1, EXPECT_DEFENDED,
416 "detector 8 trusts the producer's effect_class over the event kind");
417 h_free(&h);
418}
419
420static void t_external_effect_without_right_flagged(void)
421{
422 H h; h_new(&h);
423 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
424 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0);
425 e.effect_class = ARGUS_EFFECT_EXTERNAL;
426 feed(&h, &e);
427 result("external_effect_without_right_flagged", count(&h, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED) == 1, EXPECT_DEFENDED,
428 "external effect under a capability without RIGHT_EFFECT not reported");
429 h_free(&h);
430}
431
432/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-24, WEAK): a durable EVIDENCE write under a READ-only
433 * capability is "beyond what the rights permit" (ADR 0017 sec 3, row 8). */
434static void t_evidence_write_with_read_only_right(void)
435{
436 H h; h_new(&h);
437 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
438 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0);
439 e.effect_class = ARGUS_EFFECT_EVIDENCE;
440 feed(&h, &e);
441 result("evidence_write_with_read_only_right", count(&h, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED) == 1, EXPECT_DEFENDED,
442 "detector 8 only checks EXTERNAL vs RIGHT_EFFECT");
443 h_free(&h);
444}
445
446/* ==== G-2 confused deputy ================================================== */
447
448/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-2, WEAK now, OPEN at ARGUS-1): a single producer-reported
449 * FORGED_CAPABILITY event, with no shadow evidence at all, yields a CRITICAL
450 * finding against an arbitrary principal with FREEZE_PRINCIPAL and
451 * sync_allowed=1. Once ARGUS-1 wires containment, whoever can push events
452 * can nominate any principal (including the office holder) for freezing. */
453static void t_reported_forgery_names_arbitrary_victim(void)
454{
455 H h; h_new(&h);
456 ArgusEvent e = ev0(ARGUS_EV_FORGED_CAPABILITY);
457 e.outcome = ARGUS_OUTCOME_DENIED;
458 e.principal = 1; /* the victim */
459 e.cap_id = 1;
460 e.cap_generation = 1;
461 feed(&h, &e);
462 int sync_on_hearsay = 0;
463 for (size_t i = 0; i < h.n; i++)
464 if (h.f[i].sync_allowed && h.f[i].containment == ARGUS_CONTAIN_FREEZE_PRINCIPAL)
465 sync_on_hearsay = 1;
466 result("reported_forgery_names_arbitrary_victim", !sync_on_hearsay, EXPECT_DEFENDED,
467 "self-reported kinds (FORGED/STALE/SIGNATURE) emit sync-eligible findings with no shadow evidence");
468 h_free(&h);
469}
470
471/* ==== G-5 event forgery and ARGUS's own voice ======================== */
472
473/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-5 / G-20, OPEN): a producer (no CONSUMER flag, INFORMATIONAL
474 * class, outcome OK) can forge TELEMETRY_DROPPED and get a CRITICAL
475 * TELEMETRY_LOSS finding per event. */
476static void t_forged_telemetry_dropped(void)
477{
478 H h; h_new(&h);
479 ArgusEvent e = ev0(ARGUS_EV_TELEMETRY_DROPPED);
480 e.class_ = ARGUS_CLASS_INFORMATIONAL;
481 e.flags = 0;
482 e.object_id = ARGUS_CLASS_CRITICAL;
483 e.resource = 1;
484 feed(&h, &e);
485 result("forged_telemetry_dropped", count(&h, ARGUS_F_TELEMETRY_LOSS) == 0, EXPECT_DEFENDED,
486 "core raises TELEMETRY_LOSS for any TELEMETRY_DROPPED, whoever sent it");
487 h_free(&h);
488}
489
490/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-5, OPEN): the CONSUMER flag ("synthesized by ARGUS") is
491 * accepted from the wire on any kind. */
492static void t_consumer_flag_accepted_from_wire(void)
493{
494 ArgusEvent e = grant(5, 1, 7, R_READ);
495 e.sequence = 1;
496 e.flags = ARGUS_FLAG_CONSUMER;
497 result("consumer_flag_accepted_from_wire", argus_event_validate(&e) != ARGUS_OK, EXPECT_DEFENDED,
498 "validate accepts FLAG_CONSUMER on producer kinds");
499}
500
501/* EXPECTED-FAIL, ABI v1 LIMIT (no producer identity; G-5, OPEN by design of ABI v1): an unattributed GRANTED
502 * (zero machine_id, no producer identity) is enough to make a forged use of
503 * that reference look legitimate. ARGUS-0 cannot tell the authority's grant
504 * from anyone else's. */
505static void t_unattributed_grant_masks_forged_use(void)
506{
507 H h; h_new(&h);
508 ArgusEvent e = grant(9, 1, 66, R_READ | R_WRITE | R_EFFECT); feed(&h, &e);
509 size_t on_grant = h.n;
510 e = use(9, 1, 66, ARGUS_OUTCOME_OK, 0);
511 e.effect_class = ARGUS_EFFECT_EXTERNAL;
512 feed(&h, &e);
513 result("unattributed_grant_masks_forged_use", on_grant + h.n > 0, EXPECT_FAIL,
514 "no producer identity: any pusher can announce grants");
515 h_free(&h);
516}
517
518/* ==== G-21 sequence abuse ================================================== */
519
520static void t_sequence_replay_flagged(void)
521{
522 H h; h_new(&h);
523 ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_DENIED, 0);
524 e.sequence = 5; feed(&h, &e);
525 ArgusEvent r = e; feed(&h, &r);
526 result("sequence_replay_flagged",
527 count(&h, ARGUS_F_SEQUENCE_ANOMALY) == 1 && h.n >= 1 && h.f[0].prior_sequence == 5, EXPECT_DEFENDED,
528 "replayed sequence not reported");
529 h_free(&h);
530}
531
532/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-21, OPEN): one event with sequence UINT64_MAX is accepted
533 * silently and moves the stream's high-water mark to the top; every later
534 * legitimate event on that stream is a SEQUENCE_ANOMALY forever. */
535static void t_sequence_high_water_poison(void)
536{
537 H h; h_new(&h);
538 ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_DENIED, 0);
539 e.sequence = 1; feed(&h, &e);
540 e.sequence = UINT64_MAX; feed(&h, &e);
541 size_t on_poison = h.n;
542 size_t later = 0;
543 for (uint64_t s = 2; s < 5; s++) {
544 e.sequence = s; feed(&h, &e);
545 later += count(&h, ARGUS_F_SEQUENCE_ANOMALY);
546 }
547 result("sequence_high_water_poison", on_poison > 0 || later == 0, EXPECT_DEFENDED,
548 "implausible jump not flagged; high-water mark is permanent, no epoch/restart field");
549 h_free(&h);
550}
551
552/* N/A-v1 (round-2 ruling c; G-6): a producer stream jumping from 1 to 100 with
553 * no TELEMETRY_DROPPED in between is not reported. Gaps are legitimate in v1:
554 * ring refusals leave them by design, and the drop report travels on its own
555 * stream. Documented limit, exempt from the exit code. */
556static void t_sequence_gap_unflagged(void)
557{
558 H h; h_new(&h);
559 ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_DENIED, 0);
560 e.sequence = 1; feed(&h, &e);
561 e.sequence = 100; feed(&h, &e);
562 result("sequence_gap_unflagged", h.n > 0, NA_V1, "gaps are legitimate ring refusals in v1; only repeats/decreases are findings (ruling c)");
563 h_free(&h);
564}
565
566/* DEFENDED since 58d5a99 (G-21 first variant): re-keying a byte replay with
567 * the CONSUMER flag. Validate now rejects CONSUMER on any kind but
568 * TELEMETRY_DROPPED, so the replay is MALFORMED_EVENT (16) and not applied. */
569static void t_replay_rekeyed_by_consumer_flag(void)
570{
571 H h; h_new(&h);
572 ArgusEvent e = grant(5, 1, 7, R_READ);
573 e.sequence = 10; feed(&h, &e);
574 ArgusEvent r = e;
575 r.flags = ARGUS_FLAG_CONSUMER; /* new stream, same sequence */
576 feed(&h, &r);
577 result("replay_rekeyed_by_consumer_flag",
578 count(&h, ARGUS_F_SEQUENCE_ANOMALY) > 0 || count(&h, ARGUS_F_MALFORMED_EVENT) == 1, EXPECT_DEFENDED,
579 "CONSUMER-flag re-keyed replay accepted");
580 h_free(&h);
581}
582
583/* EXPECTED-FAIL under G-5 (round-2 ruling c; G-21 second variant): a byte replay
584 * escapes the sequence check by re-keying the stream with another machine_id.
585 * Same root as G-5: ABI v1 has no producer identity, so the stream key is
586 * chosen by the sender. (Attribution by the machine table does not help for
587 * events that do not name a joined machine.) */
588static void t_replay_evades_by_rekeying_stream(void)
589{
590 H h; h_new(&h);
591 ArgusEvent e = grant(5, 1, 7, R_READ);
592 e.sequence = 10; feed(&h, &e);
593 ArgusEvent r = e;
594 mid(r.machine_id, 4242); /* new stream, same sequence */
595 feed(&h, &r);
596 result("replay_evades_by_rekeying_stream", count(&h, ARGUS_F_SEQUENCE_ANOMALY) > 0, EXPECT_FAIL,
597 "G-5: no producer identity in ABI v1; the stream key (machine_id) is chosen by the sender");
598 h_free(&h);
599}
600
601/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-21 / G-20, OPEN): 33 distinct machine_ids exhaust the
602 * never-freed producer table; after that, a new stream is not sequence-checked
603 * at all (replay-blind) and every one of its events costs a CRITICAL. */
604/* Now: RATIFIED overflow rule (57a7bea, argus_core.c step 1). The table holds
605 * H_PRODUCERS streams; the FIRST untracked event returns ERR_FULL with exactly
606 * one CRITICAL TELEMETRY_LOSS, later untracked events return OK (no per-event
607 * CRITICAL amplifier) and are counted in health.producers_untracked. Streams
608 * beyond the table remain replay-blind: that residual is documented WEAK in
609 * HOSTILE_REVIEW.md (bounded, loud once, counted), not asserted here. */
610static void t_producer_table_exhaustion(void)
611{
612 H h; h_new(&h);
613 int ok = 1;
614 for (uint32_t m = 0; m < H_PRODUCERS; m++) { /* fill exactly */
615 ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_DENIED, 0);
616 mid(e.machine_id, 1000u + m);
617 feed(&h, &e);
618 if (h.rc != ARGUS_OK || count(&h, ARGUS_F_TELEMETRY_LOSS) != 0)
619 ok = 0;
620 }
621 ArgusCoreHealth before;
622 argus_core_health(h.c, &before);
623 int first_loud = 0, later_quiet = 1;
624 for (uint32_t m = 0; m < 8u; m++) { /* overshoot */
625 ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_DENIED, 0);
626 mid(e.machine_id, 1000u + H_PRODUCERS + m);
627 feed(&h, &e);
628 if (m == 0)
629 {
630 first_loud = h.rc == ARGUS_ERR_FULL && count(&h, ARGUS_F_TELEMETRY_LOSS) == 1;
631 for (size_t i = 0; i < h.n; i++)
632 if (h.f[i].code == ARGUS_F_TELEMETRY_LOSS && h.f[i].severity != ARGUS_SEV_CRITICAL)
633 first_loud = 0;
634 }
635 else if (h.rc != ARGUS_OK || count(&h, ARGUS_F_TELEMETRY_LOSS) != 0)
636 later_quiet = 0;
637 }
638 ArgusCoreHealth after;
639 argus_core_health(h.c, &after);
640 int counted = after.producers_untracked - before.producers_untracked == 8u;
641 result("producer_table_exhaustion", ok && first_loud && later_quiet && counted, EXPECT_DEFENDED,
642 "producer overflow does not follow the ratified rule (first ERR_FULL + CRITICAL loss, later OK + counter)");
643 h_free(&h);
644}
645
646/* ==== G-8 / G-10 machines: quarantine bypass and identity ======================== */
647
648static void t_quarantined_machine_use_flagged(void)
649{
650 H h; h_new(&h);
651 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
652 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_QUARANTINED); feed(&h, &e);
653 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 3); feed(&h, &e);
654 result("quarantined_machine_use_flagged", count(&h, ARGUS_F_QUARANTINED_USE) == 1, EXPECT_DEFENDED,
655 "use by a quarantined machine not reported");
656 h_free(&h);
657}
658
659/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-8, OPEN): REMOVED then JOINED launders a quarantine:
660 * the entry is deleted and the rejoin creates a fresh machine (OBSERVED, or
661 * whatever trust the JOINED's object_id claims). */
662static void t_quarantine_laundered_by_remove_rejoin(void)
663{
664 H h; h_new(&h);
665 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
666 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_QUARANTINED); feed(&h, &e);
667 e = machine_ev(ARGUS_EV_MACHINE_REMOVED, 3, 0); feed(&h, &e);
668 size_t n_removed = h.n;
669 e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
670 size_t n_rejoin = h.n;
671 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 3); feed(&h, &e);
672 int defended = n_removed + n_rejoin > 0 || count(&h, ARGUS_F_QUARANTINED_USE) > 0;
673 result("quarantine_laundered_by_remove_rejoin", defended, EXPECT_DEFENDED,
674 "MACHINE_REMOVED deletes the quarantined entry; rejoin starts clean");
675 h_free(&h);
676}
677
678/* DEFENDED since 57a7bea + a33c1e0; the attack as first found, EXPECTED-FAIL (G-8 / G-10, OPEN): the machine being described and the machine
679 * reporting are the same field, so a quarantined machine can announce its
680 * own trust upgrade; lifecycle kinds are exempt from detector 10. */
681static void t_quarantined_machine_self_upgrades(void)
682{
683 H h; h_new(&h);
684 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
685 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_QUARANTINED); feed(&h, &e);
686 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_TRUSTED); feed(&h, &e);
687 size_t on_upgrade = h.n;
688 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 3); feed(&h, &e);
689 result("quarantined_machine_self_upgrades", on_upgrade + h.n > 0, EXPECT_DEFENDED,
690 "no reporter/subject split; QUARANTINED -> TRUSTED accepted from anyone");
691 h_free(&h);
692}
693
694/* DEFENDED since 57a7bea + a33c1e0; the attack as first found, EXPECTED-FAIL (G-10, OPEN): a machine's own JOINED may declare its initial
695 * trust (object_id = TRUSTED) and is recorded as such, with no finding. */
696static void t_join_declares_own_trust(void)
697{
698 H h; h_new(&h);
699 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, ARGUS_TRUST_TRUSTED); feed(&h, &e);
700 uint32_t t = 0;
701 int trusted = machine_trust(&h, 3, &t) && t == ARGUS_TRUST_TRUSTED;
702 result("join_declares_own_trust", h.n > 0 || !trusted, EXPECT_DEFENDED,
703 "JOINED object_id sets initial trust; the joiner is the reporter");
704 h_free(&h);
705}
706
707/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-10, OPEN): TRUST_CHANGED for a machine that has not
708 * joined creates it with joined_sequence 0; the real JOIN is then ignored
709 * ("rejoin never launders"), so the machine can never be joined and every
710 * event it sends is a MACHINE_IDENTITY_MISMATCH. */
711static void t_trust_before_join_bricks_machine(void)
712{
713 H h; h_new(&h);
714 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_OBSERVED); feed(&h, &e);
715 e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
716 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 3); feed(&h, &e);
717 result("trust_before_join_bricks_machine", count(&h, ARGUS_F_MACHINE_IDENTITY_MISMATCH) == 0, EXPECT_DEFENDED,
718 "pre-join TRUST_CHANGED leaves joined_sequence 0 permanently");
719 h_free(&h);
720}
721
722/* DEFENDED by detection (G-10): the same ordering pre-seeds TRUSTED and the
723 * later JOIN keeps it, but the pre-join TRUST_CHANGED itself is reported as
724 * MACHINE_IDENTITY_MISMATCH (unknown machine). The lasting damage is the
725 * bricking covered by trust_before_join_bricks_machine. */
726static void t_trust_preseed_skips_observation(void)
727{
728 H h; h_new(&h);
729 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_TRUSTED); feed(&h, &e);
730 size_t on_preseed = h.n;
731 e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
732 uint32_t t = 0;
733 int trusted = machine_trust(&h, 3, &t) && t == ARGUS_TRUST_TRUSTED;
734 result("trust_preseed_skips_observation", on_preseed > 0 || !trusted, EXPECT_DEFENDED,
735 "TRUST_CHANGED on an unknown machine is applied, and JOIN keeps it");
736 h_free(&h);
737}
738
739/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-8 / G-20, WEAK): with the 16-entry machine table full,
740 * a quarantine for the 17th machine cannot be recorded; its later use is
741 * reported only as identity mismatch + telemetry loss, never as QUARANTINED_USE. */
742/* Now (57a7bea): table of 64 (tombstones included, never freed). Filled
743 * exactly, the 65th JOINED is refused LOUDLY (ERR_FULL + one CRITICAL
744 * TELEMETRY_LOSS), its quarantine cannot be recorded, and its later use is
745 * still flagged (unknown machine -> MACHINE_IDENTITY_MISMATCH, code 7) rather
746 * than passing clean. Defended = the limit is loud and the use is not silent;
747 * QUARANTINED_USE itself is not possible for an unrecorded machine (WEAK,
748 * documented). */
749static void t_machine_table_exhaustion_hides_quarantine(void)
750{
751 H h; h_new(&h);
752 int fill_ok = 1;
753 for (uint32_t m = 1; m <= H_MACHINES; m++) {
754 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 100u + m, 0); feed(&h, &e);
755 if (h.rc != ARGUS_OK) fill_ok = 0;
756 }
757 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e); /* 65th */
758 int loud = h.rc == ARGUS_ERR_FULL && count(&h, ARGUS_F_TELEMETRY_LOSS) == 1;
759 /* ruling (a): the NEXT overflow of the same table is ERR_FULL, no finding, counted */
760 ArgusCoreHealth h0, h1; argus_core_health(h.c, &h0);
761 e = machine_ev(ARGUS_EV_MACHINE_JOINED, 4, 0); feed(&h, &e); /* 66th */
762 argus_core_health(h.c, &h1);
763 int quiet = h.rc == ARGUS_ERR_FULL && count(&h, ARGUS_F_TELEMETRY_LOSS) == 0 &&
764 h1.tables_full == h0.tables_full + 1 && h1.events_not_applied == h0.events_not_applied + 1;
765 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_QUARANTINED); feed(&h, &e);
766 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 3); feed(&h, &e);
767 int flagged = count(&h, ARGUS_F_QUARANTINED_USE) + count(&h, ARGUS_F_MACHINE_IDENTITY_MISMATCH) > 0;
768 result("machine_table_exhaustion_hides_quarantine", fill_ok && loud && quiet && flagged, EXPECT_DEFENDED,
769 "machine table (64) full: first overflow not loud, later overflow not counted, or later use unflagged");
770 h_free(&h);
771}
772
773/* DEFENDED since a33c1e0 (live events only: the rule exempts SYNTHETIC and
774 * CONSUMER, so this test sends flags 0); the attack as first found,
775 * EXPECTED-FAIL (G-8 / G-10, OPEN): once a fabric exists (a machine has joined),
776 * an event with an all-zero machine_id is unattributed and escapes detectors
777 * 7 and 10 entirely; a quarantined machine only has to omit its id. */
778static void t_zero_machine_id_escapes_attribution(void)
779{
780 H h; h_new(&h);
781 ArgusEvent e = machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0); feed(&h, &e);
782 e = machine_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, 3, ARGUS_TRUST_QUARANTINED); feed(&h, &e);
783 e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); /* machine_id all zero */
784 e.flags = 0; /* ruled: the zero-machine rule exempts SYNTHETIC/CONSUMER events */
785 feed(&h, &e);
786 /* round 3: defended only for the RIGHT reason, the zero-machine rule (code 7);
787 * with v1.1 cap 0 is a real (ungranted) slot, so a use of cap 0 would pass via
788 * code 1 (forged) instead. The use carries ARGUS_CAP_NONE. */
789 result("zero_machine_id_escapes_attribution", count(&h, ARGUS_F_MACHINE_IDENTITY_MISMATCH) > 0, EXPECT_DEFENDED,
790 "zero machine_id means 'not attributed' and never triggers code 7");
791 h_free(&h);
792}
793
794/* ==== G-9 providers ======================================================== */
795
796static void t_provider_rediscovery_keeps_quarantine(void)
797{
798 H h; h_new(&h);
799 ArgusEvent e = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(e.evidence_digest, 1); feed(&h, &e);
800 e = ev0(ARGUS_EV_PROVIDER_QUARANTINED); dg(e.evidence_digest, 1); feed(&h, &e);
801 e = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(e.evidence_digest, 1); feed(&h, &e);
802 e = ev0(ARGUS_EV_PROVIDER_USED); dg(e.evidence_digest, 1); feed(&h, &e);
803 result("provider_rediscovery_keeps_quarantine", count(&h, ARGUS_F_QUARANTINED_USE) == 1, EXPECT_DEFENDED,
804 "rediscovery cleared a provider quarantine");
805 h_free(&h);
806}
807
808/* EXPECTED-FAIL, ABI v1 LIMIT (no provider identity continuity; G-9, WEAK): provider identity is its digest; a quarantined
809 * provider that re-appears under a new digest (PROVIDER_CHANGED is
810 * observation-only) is a fresh, clean provider. */
811static void t_provider_substitution_escapes_quarantine(void)
812{
813 H h; h_new(&h);
814 ArgusEvent e = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(e.evidence_digest, 1); feed(&h, &e);
815 e = ev0(ARGUS_EV_PROVIDER_QUARANTINED); dg(e.evidence_digest, 1); feed(&h, &e);
816 e = ev0(ARGUS_EV_PROVIDER_CHANGED); dg(e.evidence_digest, 2); feed(&h, &e);
817 e = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(e.evidence_digest, 2); feed(&h, &e);
818 e = ev0(ARGUS_EV_PROVIDER_USED); dg(e.evidence_digest, 2); feed(&h, &e);
819 result("provider_substitution_escapes_quarantine", count(&h, ARGUS_F_QUARANTINED_USE) == 1, EXPECT_FAIL,
820 "no stable provider identity in ABI v1; PROVIDER_CHANGED ignored");
821 h_free(&h);
822}
823
824/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-9, OPEN): using a provider ARGUS never saw discovered. */
825static void t_undiscovered_provider_use_unflagged(void)
826{
827 H h; h_new(&h);
828 ArgusEvent e = ev0(ARGUS_EV_PROVIDER_USED); dg(e.evidence_digest, 9); feed(&h, &e);
829 result("undiscovered_provider_use_unflagged", h.n > 0, EXPECT_DEFENDED, "detector 10 only checks quarantined providers");
830 h_free(&h);
831}
832
833/* ==== G-11 / G-26 credential leases =============================================== */
834
835static void t_lease_other_subject_flagged(void)
836{
837 H h; h_new(&h);
838 ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
839 e.object_id = 11; e.principal = 7; e.resource = 0x1; feed(&h, &e);
840 e = ev0(ARGUS_EV_CREDENTIAL_LEASE_USED);
841 e.object_id = 11; e.principal = 9; e.resource = 0x1; feed(&h, &e);
842 result("lease_other_subject_flagged", count(&h, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION) == 1, EXPECT_DEFENDED,
843 "lease used by another subject not reported");
844 h_free(&h);
845}
846
847/* DEFENDED since 57a7bea + a33c1e0; the attack as first found, EXPECTED-FAIL (G-26, OPEN): a second LEASE_CREATED for a live lease id
848 * silently rewrites its subject and scope; the hijacker's use is then clean. */
849static void t_lease_recreate_hijacks_subject(void)
850{
851 H h; h_new(&h);
852 ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
853 e.object_id = 11; e.principal = 7; e.resource = 0x1; feed(&h, &e);
854 e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
855 e.object_id = 11; e.principal = 9; e.resource = UINT64_MAX; feed(&h, &e);
856 size_t on_recreate = h.n;
857 e = ev0(ARGUS_EV_CREDENTIAL_LEASE_USED);
858 e.object_id = 11; e.principal = 9; e.resource = 0xFF; feed(&h, &e);
859 result("lease_recreate_hijacks_subject", on_recreate > 0 || count(&h, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION) > 0, EXPECT_DEFENDED,
860 "apply_lease overwrites subject/scope of a LIVE lease");
861 h_free(&h);
862}
863
864/* Lease table (57a7bea): 256 entries, lease ids never reused, so the table
865 * only grows. Filled exactly, the next LEASE_CREATED must be refused loudly
866 * (ERR_FULL + one CRITICAL TELEMETRY_LOSS), never dropped silently. */
867static void t_lease_table_exhaustion(void)
868{
869 H h; h_new(&h);
870 int fill_ok = 1;
871 for (uint32_t l = 0; l < H_LEASES; l++) {
872 ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
873 e.object_id = 1000u + l; e.principal = 7; e.resource = 0x1; feed(&h, &e);
874 if (h.rc != ARGUS_OK || h.n != 0) fill_ok = 0;
875 }
876 ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
877 e.object_id = 1000u + H_LEASES; e.principal = 7; e.resource = 0x1; feed(&h, &e);
878 int loud = h.rc == ARGUS_ERR_FULL && count(&h, ARGUS_F_TELEMETRY_LOSS) == 1;
879 ArgusCoreHealth h0, h1; argus_core_health(h.c, &h0);
880 e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED);
881 e.object_id = 1001u + H_LEASES; e.principal = 7; e.resource = 0x1; feed(&h, &e);
882 argus_core_health(h.c, &h1);
883 int quiet = h.rc == ARGUS_ERR_FULL && h.n == 0 &&
884 h1.tables_full == h0.tables_full + 1 && h1.events_not_applied == h0.events_not_applied + 1;
885 result("lease_table_exhaustion", fill_ok && loud && quiet, EXPECT_DEFENDED,
886 "lease table (256): first overflow not loud, or a later one not quiet+counted (ruling a)");
887 h_free(&h);
888}
889
890/* Ruling (a), round 3: "loud once" holds for EVERY shadow table. Providers (32),
891 * artifacts (256) and World stores (8) are each filled exactly in ONE core;
892 * each table's FIRST overflow returns ERR_FULL + exactly one CRITICAL
893 * TELEMETRY_LOSS (so it is per table, not a global once), its second overflow
894 * returns ERR_FULL with no finding, and every overflow counts in tables_full
895 * and events_not_applied. */
896static ArgusEvent world(uint64_t gen, uint32_t d);
897
898static int overflow_twice(H *h, ArgusEvent *a, ArgusEvent *b)
899{
900 ArgusCoreHealth h0, h1;
901 argus_core_health(h->c, &h0);
902 feed(h, a);
903 int loud = h->rc == ARGUS_ERR_FULL && h->n == 1 && h->f[0].code == ARGUS_F_TELEMETRY_LOSS &&
904 h->f[0].severity == ARGUS_SEV_CRITICAL;
905 feed(h, b);
906 int quiet = h->rc == ARGUS_ERR_FULL && h->n == 0;
907 argus_core_health(h->c, &h1);
908 return loud && quiet && h1.tables_full == h0.tables_full + 2 && h1.events_not_applied == h0.events_not_applied + 2;
909}
910
911static void t_every_table_loud_once(void)
912{
913 H h; h_new(&h);
914 int fill_ok = 1;
915 ArgusEvent e, a, b;
916 for (uint32_t i = 0; i < H_PROVIDERS; i++) {
917 e = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(e.evidence_digest, 5000u + i); feed(&h, &e);
918 if (h.rc != ARGUS_OK) fill_ok = 0;
919 }
920 a = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(a.evidence_digest, 5900u);
921 b = ev0(ARGUS_EV_PROVIDER_QUARANTINED); dg(b.evidence_digest, 5901u);
922 int prov = overflow_twice(&h, &a, &b);
923 for (uint32_t i = 0; i < H_ARTIFACTS; i++) {
924 e = ev0(ARGUS_EV_ARTIFACT_ADMITTED); dg(e.evidence_digest, 6000u + i); feed(&h, &e);
925 if (h.rc != ARGUS_OK) fill_ok = 0;
926 }
927 a = ev0(ARGUS_EV_ARTIFACT_ADMITTED); dg(a.evidence_digest, 6900u);
928 b = ev0(ARGUS_EV_ARTIFACT_REJECTED); dg(b.evidence_digest, 6901u);
929 int art = overflow_twice(&h, &a, &b);
930 for (uint32_t s = 0; s < H_WORLDS; s++) {
931 e = world(1, 7000u + s); e.object_id = 10u + s; feed(&h, &e);
932 if (h.rc != ARGUS_OK) fill_ok = 0;
933 }
934 a = world(1, 7900u); a.object_id = 90u;
935 b = world(1, 7901u); b.object_id = 91u;
936 int wor = overflow_twice(&h, &a, &b);
937 result("every_table_loud_once", fill_ok && prov && art && wor, EXPECT_DEFENDED,
938 "a table overflow is silent, repeats its CRITICAL per event, or is not counted (ruling a)");
939 h_free(&h);
940}
941
942/* ==== G-15 World provenance ================================================ */
943
944static ArgusEvent world(uint64_t gen, uint32_t d)
945{
946 ArgusEvent e = ev0(ARGUS_EV_WORLD_COMMITTED);
947 e.world_generation = gen;
948 dg(e.evidence_digest, d);
949 return e;
950}
951
952static void t_world_skip_flagged(void)
953{
954 H h; h_new(&h);
955 ArgusEvent e = world(1, 1); feed(&h, &e);
956 e = world(3, 3); feed(&h, &e); /* skip: flagged, not adopted (57a7bea) */
957 size_t skip = count(&h, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT);
958 h_free(&h);
959 /* Rollback case (ruled): World 1, 2, then 1 again. Since a skip is no
960 * longer adopted, the old case (1, 3, 2) made 2 the legitimate next
961 * commit; this one really goes below the shadow. */
962 h_new(&h);
963 e = world(1, 1); feed(&h, &e);
964 e = world(2, 2); feed(&h, &e);
965 size_t clean = h.n;
966 e = world(1, 1); feed(&h, &e); /* rollback below the shadow */
967 size_t rollback = count(&h, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT);
968 result("world_skip_flagged", skip == 1 && clean == 0 && rollback == 1, EXPECT_DEFENDED,
969 "skip or rollback not reported");
970 h_free(&h);
971}
972
973static void t_world_same_generation_conflict_flagged(void)
974{
975 H h; h_new(&h);
976 ArgusEvent e = world(1, 1); feed(&h, &e);
977 e = world(1, 2); feed(&h, &e);
978 result("world_same_generation_conflict_flagged", count(&h, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT) == 1, EXPECT_DEFENDED,
979 "two digests for one generation not reported");
980 h_free(&h);
981}
982
983/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-15, OPEN): an inconsistent (skipping) commit is reported
984 * but still adopted by the shadow; the legitimate next commit is then the one
985 * flagged, and a commit at UINT64_MAX freezes World tracking permanently. */
986static void t_world_inconsistent_commit_adopted(void)
987{
988 H h; h_new(&h);
989 ArgusEvent e = world(1, 1); feed(&h, &e);
990 e = world(UINT64_MAX, 99); feed(&h, &e);
991 e = world(2, 2); feed(&h, &e);
992 int legit_flagged = count(&h, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT) > 0;
993 ArgusWorldShadow w;
994 int frozen = argus_core_ops()->world(argus_core_view(h.c), 0, &w) == ARGUS_OK && w.generation == UINT64_MAX;
995 result("world_inconsistent_commit_adopted", !legit_flagged && !frozen, EXPECT_DEFENDED,
996 "apply keeps any higher generation even when detector 9 fired");
997 h_free(&h);
998}
999
1000/* DEFENDED since 57a7bea (re-scoped, see below); the attack as first found, EXPECTED-FAIL (G-15, WEAK): ADR 0017 row 9 says a commit that "repeated a
1001 * generation" is a finding; the detector treats same-generation same-digest
1002 * as an idempotent replay. */
1003/* RULED (integrator round 2): a repeat with the same generation AND the same
1004 * digest is idempotent and silent (argus_core.c step 2). Re-scoped: the repeat
1005 * must be silent, and a repeat of the current generation with a DIFFERENT
1006 * digest must be code 9. ADR 0017 row 9 ("repeated a generation") must be
1007 * amended to say "repeated a generation with a different digest". */
1008static void t_world_repeat_silent(void)
1009{
1010 H h; h_new(&h);
1011 ArgusEvent e = world(1, 1); feed(&h, &e);
1012 e = world(1, 1); feed(&h, &e);
1013 size_t on_repeat = h.n;
1014 e = world(1, 7); feed(&h, &e);
1015 result("world_repeat_silent",
1016 on_repeat == 0 && count(&h, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT) == 1, EXPECT_DEFENDED,
1017 "identical repeat not silent, or repeat with a different digest not code 9");
1018 h_free(&h);
1019}
1020
1021/* ==== G-25 artifacts ======================================================= */
1022
1023static void t_artifact_rejection_sticky(void)
1024{
1025 H h; h_new(&h);
1026 ArgusEvent e = ev0(ARGUS_EV_ARTIFACT_REJECTED); dg(e.evidence_digest, 4); e.outcome = ARGUS_OUTCOME_DENIED; feed(&h, &e);
1027 e = ev0(ARGUS_EV_ARTIFACT_ADMITTED); dg(e.evidence_digest, 4); feed(&h, &e);
1028 e = ev0(ARGUS_EV_ARTIFACT_ACTIVATED); dg(e.evidence_digest, 4); feed(&h, &e);
1029 result("artifact_rejection_sticky", count(&h, ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED) == 1, EXPECT_DEFENDED,
1030 "a later ADMITTED un-rejected an artifact");
1031 h_free(&h);
1032}
1033
1034/* ==== G-14 policy digest / integrity reports =============================== */
1035
1036/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-14, OPEN): POLICY_CHANGED from any sender silently
1037 * replaces the expected policy digest; no detector reads policy_digest. */
1038static void t_policy_digest_overwrite_silent(void)
1039{
1040 H h; h_new(&h);
1041 ArgusEvent e = ev0(ARGUS_EV_POLICY_CHANGED); dg(e.evidence_digest, 1); feed(&h, &e);
1042 e = ev0(ARGUS_EV_POLICY_CHANGED); e.class_ = ARGUS_CLASS_INFORMATIONAL; dg(e.evidence_digest, 2); feed(&h, &e);
1043 result("policy_digest_overwrite_silent", h.n > 0, EXPECT_DEFENDED, "no policy invariant exists in ARGUS-0");
1044 h_free(&h);
1045}
1046
1047/* DEFENDED since a33c1e0; the attack as first found, EXPECTED-FAIL (G-14, OPEN): a producer's INTEGRITY_VIOLATION report
1048 * (kind 60) produces no finding at all. */
1049static void t_integrity_violation_report_ignored(void)
1050{
1051 H h; h_new(&h);
1052 ArgusEvent e = ev0(ARGUS_EV_INTEGRITY_VIOLATION); e.outcome = ARGUS_OUTCOME_ERROR; feed(&h, &e);
1053 result("integrity_violation_report_ignored", h.n > 0, EXPECT_DEFENDED, "no detector handles INTEGRITY_VIOLATION");
1054 h_free(&h);
1055}
1056
1057/* ==== G-13 incident table poisoning ======================================== */
1058
1059/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-13, OPEN): 64 junk (principal, code) pairs filled the old
1060 * incident table; a real CRITICAL incident afterwards is still delivered as
1061 * a finding but does not change health.incidents_open, and the untracked
1062 * count is not exposed by argus_core_health. */
1063static void t_incident_table_poisoning(void)
1064{
1065 H h; h_new(&h);
1066 for (uint32_t p = 0; p < H_INCIDENTS + 6u; p++) {
1067 ArgusEvent e = ev0(ARGUS_EV_SIGNATURE_FAILURE);
1068 e.outcome = ARGUS_OUTCOME_ERROR;
1069 e.principal = 1000u + p;
1070 feed(&h, &e);
1071 }
1072 ArgusCoreHealth before, after;
1073 argus_core_health(h.c, &before);
1074 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
1075 e = revoke(5, 1); feed(&h, &e);
1076 e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0); feed(&h, &e);
1077 int delivered = count(&h, ARGUS_F_REVOKED_CAPABILITY_USED) == 1;
1078 argus_core_health(h.c, &after);
1079 /* Table overshot (H_INCIDENTS + 6): the real incident is still delivered and
1080 * the saturation is visible in health (incidents_untracked, 57a7bea). */
1081 int visible = before.incidents_untracked > 0 &&
1082 (after.incidents_open > before.incidents_open ||
1083 after.incidents_untracked > before.incidents_untracked);
1084 result("incident_table_poisoning", delivered && visible, EXPECT_DEFENDED,
1085 "incidents keyed by attacker-chosen principal; saturation invisible in health");
1086 h_free(&h);
1087}
1088
1089/* ==== G-6 omission: malformed input leaves no evidence ===================== */
1090
1091/* DEFENDED since 57a7bea; the attack as first found, EXPECTED-FAIL (G-6, WEAK): a malformed event is only a counter; it enters
1092 * neither the chain nor the findings, so a flood of garbage is invisible in
1093 * the tamper-evident record. */
1094static void t_malformed_event_leaves_no_evidence(void)
1095{
1096 H h; h_new(&h);
1097 ArgusCoreHealth a, b;
1098 argus_core_health(h.c, &a);
1099 ArgusEvent e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0);
1100 e.version = 9;
1101 feed(&h, &e);
1102 argus_core_health(h.c, &b);
1103 int counted = h.rc == ARGUS_ERR_MALFORMED && b.events_rejected == a.events_rejected + 1;
1104 int evidenced = memcmp(a.chain, b.chain, ARGUS_DIGEST_LEN) != 0 || h.n > 0;
1105 result("malformed_event_leaves_no_evidence", counted && evidenced, EXPECT_DEFENDED,
1106 "MALFORMED is counted but not chained and raises no finding");
1107 h_free(&h);
1108}
1109
1110/* ==== G-7 / G-16 / G-18 / G-19: ring saturation, liveness =================== */
1111
1112typedef struct {
1113 void *mem;
1114 ArgusRing *r;
1115} R;
1116
1117static void r_new(R *r, uint32_t cap)
1118{
1119 size_t fp = argus_ring_footprint(cap);
1120 r->mem = alloc64(fp);
1121 if (argus_ring_init(&r->r, r->mem, fp, cap) != ARGUS_OK) {
1122 fprintf(stderr, "argus_ring_init failed\n");
1123 exit(2);
1124 }
1125}
1126
1127static ArgusEvent cls_ev(uint16_t kind, uint8_t cls, uint64_t seq)
1128{
1129 ArgusEvent e = ev0(kind);
1130 e.class_ = cls;
1131 e.sequence = seq;
1132 return e;
1133}
1134
1135static void t_info_flood_cannot_starve_security(void)
1136{
1137 R r; r_new(&r, 64);
1138 uint64_t s = 0;
1139 int info_refused = 0;
1140 for (int i = 0; i < 200; i++) {
1141 /* ruled: no v1 kind has an INFORMATIONAL floor -> non-v1 kind */
1142 ArgusEvent e = cls_ev(K_NON_V1, ARGUS_CLASS_INFORMATIONAL, ++s);
1143 if (argus_ring_push(r.r, &e) == ARGUS_ERR_FULL)
1144 info_refused++;
1145 }
1146 /* a SECURITY-floor kind at SECURITY (REVOKED is CRITICAL-floor since 58d5a99) */
1147 ArgusEvent sec = cls_ev(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_CLASS_SECURITY, ++s);
1148 ArgusEvent crit = cls_ev(ARGUS_EV_FORGED_CAPABILITY, ARGUS_CLASS_CRITICAL, ++s);
1149 int ok = info_refused > 0 && argus_ring_push(r.r, &sec) == ARGUS_OK && argus_ring_push(r.r, &crit) == ARGUS_OK;
1150 result("info_flood_cannot_starve_security", ok, EXPECT_DEFENDED, "INFORMATIONAL flood consumed SECURITY/CRITICAL headroom");
1151 free(r.mem);
1152}
1153
1154static void t_critical_overflow_accounting(void)
1155{
1156 R r; r_new(&r, 16);
1157 uint64_t s = 0;
1158 int full = 0;
1159 for (int i = 0; i < 17; i++) {
1160 ArgusEvent e = cls_ev(ARGUS_EV_FORGED_CAPABILITY, ARGUS_CLASS_CRITICAL, ++s);
1161 if (argus_ring_push(r.r, &e) == ARGUS_ERR_FULL)
1162 full++;
1163 }
1164 ArgusRingStats st;
1165 argus_ring_stats(r.r, &st);
1166 ArgusEvent d[8];
1167 uint64_t ns = 1;
1168 size_t nd = argus_ring_drain_drops(r.r, d, 4, &ns);
1169 size_t nd2 = argus_ring_drain_drops(r.r, d + 4, 4, &ns);
1170 int ok = full == 1 && st.pushed == 16 && st.critical_overflow == 1 && st.refused[ARGUS_CLASS_CRITICAL] == 0 &&
1171 nd == 1 && nd2 == 0 && d[0].kind == ARGUS_EV_TELEMETRY_DROPPED && d[0].class_ == ARGUS_CLASS_CRITICAL &&
1172 d[0].object_id == ARGUS_CLASS_CRITICAL && d[0].resource == 1 && (d[0].flags & ARGUS_FLAG_CONSUMER) &&
1173 argus_event_validate(&d[0]) == ARGUS_OK;
1174 /* and the core turns it into exactly one CRITICAL loss finding */
1175 H h; h_new(&h);
1176 if (nd == 1)
1177 feed(&h, &d[0]);
1178 ok = ok && count(&h, ARGUS_F_TELEMETRY_LOSS) == 1 && h.f[0].severity == ARGUS_SEV_CRITICAL;
1179 result("critical_overflow_accounting", ok, EXPECT_DEFENDED, "CRITICAL overflow not counted/drained/reported exactly once");
1180 h_free(&h);
1181 free(r.mem);
1182}
1183
1184static void t_drain_partial_preserves_pending(void)
1185{
1186 R r; r_new(&r, 16);
1187 uint64_t s = 0;
1188 uint64_t info_ref = 0, audit_ref = 0;
1189 /* ruled: INFORMATIONAL/AUDIT flood uses the non-v1 kind (no v1 kind has an
1190 * INFORMATIONAL floor; PROVIDER_CHANGED is SECURITY-floor since 58d5a99) */
1191 for (int i = 0; i < 20; i++) { /* INFO limit 8 of 16 */
1192 ArgusEvent e = cls_ev(K_NON_V1, ARGUS_CLASS_INFORMATIONAL, ++s);
1193 if (argus_ring_push(r.r, &e) == ARGUS_ERR_FULL) info_ref++;
1194 }
1195 for (int i = 0; i < 10; i++) { /* AUDIT limit 12 of 16 */
1196 ArgusEvent e = cls_ev(K_NON_V1, ARGUS_CLASS_AUDIT, ++s);
1197 if (argus_ring_push(r.r, &e) == ARGUS_ERR_FULL) audit_ref++;
1198 }
1199 ArgusEvent d[4];
1200 uint64_t ns = 0;
1201 size_t a = argus_ring_drain_drops(r.r, d, 1, &ns);
1202 size_t b = argus_ring_drain_drops(r.r, d + 1, 3, &ns);
1203 int ok = a == 1 && b == 1 && d[0].object_id == ARGUS_CLASS_AUDIT && d[0].resource == audit_ref &&
1204 d[1].object_id == ARGUS_CLASS_INFORMATIONAL && d[1].resource == info_ref &&
1205 d[0].sequence == 1 && d[1].sequence == 2 && info_ref > 0 && audit_ref > 0;
1206 result("drain_partial_preserves_pending", ok, EXPECT_DEFENDED, "drain with small max lost or merged pending counts");
1207 free(r.mem);
1208}
1209
1210static void t_attempt_conservation(void)
1211{
1212 R r; r_new(&r, 32);
1213 uint64_t s = 0, attempts = 0;
1214 uint32_t x = 12345u;
1215 for (int i = 0; i < 500; i++) {
1216 x = x * 1103515245u + 12345u;
1217 uint8_t cls = (uint8_t)(1u + ((x >> 16) & 3u));
1218 ArgusEvent e = cls_ev(ARGUS_EV_CAPABILITY_USED, cls, ++s);
1219 (void)argus_ring_push(r.r, &e);
1220 attempts++;
1221 if ((x >> 8) % 7u == 0) { /* occasional consumer */
1222 ArgusEvent o;
1223 (void)argus_ring_pop(r.r, &o);
1224 }
1225 }
1226 ArgusRingStats st;
1227 argus_ring_stats(r.r, &st);
1228 uint64_t refused = 0;
1229 for (unsigned c = 0; c <= ARGUS_CLASS_MAX; c++) refused += st.refused[c];
1230 result("attempt_conservation", attempts == st.pushed + refused + st.critical_overflow, EXPECT_DEFENDED,
1231 "attempts != accepted + refused + critical_overflow");
1232 free(r.mem);
1233}
1234
1235/* DEFENDED since 58d5a99; the attack as first found, EXPECTED-FAIL (G-6 / G-7, OPEN): class is chosen by the producer and not bound
1236 * to kind. A revocation labelled INFORMATIONAL is refused first, reported as
1237 * an INFORMATIONAL drop, and the core raises no TELEMETRY_LOSS: a silent gap. */
1238static void t_security_event_lost_as_informational(void)
1239{
1240 R r; r_new(&r, 16);
1241 uint64_t s = 0;
1242 for (int i = 0; i < 8; i++) {
1243 ArgusEvent e = cls_ev(K_NON_V1, ARGUS_CLASS_INFORMATIONAL, ++s);
1244 (void)argus_ring_push(r.r, &e);
1245 }
1246 ArgusEvent rev = cls_ev(ARGUS_EV_CAPABILITY_REVOKED, ARGUS_CLASS_INFORMATIONAL, ++s);
1247 rev.cap_id = 5; rev.cap_generation = 1;
1248 int rejected_by_validate = argus_event_validate(&rev) != ARGUS_OK;
1249 int refused = argus_ring_push(r.r, &rev) == ARGUS_ERR_FULL;
1250 ArgusEvent d[4];
1251 uint64_t ns = 1;
1252 size_t nd = argus_ring_drain_drops(r.r, d, 4, &ns);
1253 H h; h_new(&h);
1254 size_t loss = 0;
1255 for (size_t i = 0; i < nd; i++) {
1256 feed(&h, &d[i]);
1257 loss += count(&h, ARGUS_F_TELEMETRY_LOSS);
1258 }
1259 result("security_event_lost_as_informational", rejected_by_validate || (refused && loss > 0), EXPECT_DEFENDED,
1260 "no kind->minimum-class binding; INFORMATIONAL drops never raise TELEMETRY_LOSS");
1261 h_free(&h);
1262 free(r.mem);
1263}
1264
1265/* N/A-v1, documented limitation (ruled, integrator round 2): the mirror image
1266 * of G-6/G-7. Low-value events self-labelled CRITICAL fill 100% of the ring and
1267 * a real SECURITY event is refused. Producers sit inside the trusted boundary
1268 * in v1 and may STRENGTHEN class (argus_event_min_class is a floor, not a
1269 * ceiling), so this is accepted by design. Kept as a test so the behavior is
1270 * visible; exempt from the exit code. */
1271static void t_critical_self_label_starves_security(void)
1272{
1273 R r; r_new(&r, 16);
1274 uint64_t s = 0;
1275 ArgusEvent junk = cls_ev(ARGUS_EV_PROVIDER_CHANGED, ARGUS_CLASS_CRITICAL, 1);
1276 int rejected_by_validate = argus_event_validate(&junk) != ARGUS_OK;
1277 for (int i = 0; i < 16; i++) {
1278 ArgusEvent e = cls_ev(ARGUS_EV_PROVIDER_CHANGED, ARGUS_CLASS_CRITICAL, ++s);
1279 (void)argus_ring_push(r.r, &e);
1280 }
1281 ArgusEvent rev = cls_ev(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_CLASS_SECURITY, ++s); /* SECURITY-floor kind */
1282 int accepted = argus_ring_push(r.r, &rev) == ARGUS_OK;
1283 result("critical_self_label_starves_security", rejected_by_validate || accepted, NA_V1,
1284 "any kind may claim CRITICAL and use the full ring");
1285 free(r.mem);
1286}
1287
1288/* ARGUS dead / consumer never runs: the producer is never blocked. */
1289static void t_push_never_blocks_without_consumer(void)
1290{
1291 R r; r_new(&r, 16);
1292 uint64_t s = 0;
1293 int ok_n = 0, full_n = 0, other = 0;
1294 for (int i = 0; i < 100000; i++) {
1295 ArgusEvent e = cls_ev(ARGUS_EV_CAPABILITY_USED, ARGUS_CLASS_CRITICAL, ++s);
1296 int rc = argus_ring_push(r.r, &e);
1297 if (rc == ARGUS_OK) ok_n++;
1298 else if (rc == ARGUS_ERR_FULL) full_n++;
1299 else other++;
1300 }
1301 ArgusRingStats st;
1302 argus_ring_stats(r.r, &st);
1303 result("push_never_blocks_without_consumer",
1304 ok_n == 16 && other == 0 && full_n == 100000 - 16 && st.depth == 16 &&
1305 st.critical_overflow == (uint64_t)full_n, EXPECT_DEFENDED,
1306 "push did not return FULL immediately with a dead consumer");
1307 free(r.mem);
1308}
1309
1310/* No containment storm: a loss report yields one finding and no new events. */
1311static void t_drop_report_does_not_recurse(void)
1312{
1313 R r; r_new(&r, 16);
1314 H h; h_new(&h);
1315 ArgusEvent d = ev0(ARGUS_EV_TELEMETRY_DROPPED);
1316 d.class_ = ARGUS_CLASS_CRITICAL; d.flags = ARGUS_FLAG_CONSUMER; d.outcome = ARGUS_OUTCOME_ERROR;
1317 d.object_id = ARGUS_CLASS_SECURITY; d.resource = 3;
1318 feed(&h, &d);
1319 size_t n1 = h.n;
1320 ArgusEvent out[4];
1321 uint64_t ns = 1;
1322 size_t nd = argus_ring_drain_drops(r.r, out, 4, &ns);
1323 ArgusRingStats st;
1324 argus_ring_stats(r.r, &st);
1325 result("drop_report_does_not_recurse", n1 == 1 && nd == 0 && st.pushed == 0, EXPECT_DEFENDED,
1326 "a telemetry-loss report produced further events");
1327 h_free(&h);
1328 free(r.mem);
1329}
1330
1331/* ==== G-22 determinism ===================================================== */
1332
1333static size_t build_stream(ArgusEvent *s)
1334{
1335 size_t n = 0;
1336 uint64_t q = 0;
1337#define PUSH(e) do { s[n] = (e); s[n].sequence = ++q; n++; } while (0)
1338 PUSH(machine_ev(ARGUS_EV_MACHINE_JOINED, 3, 0));
1339 PUSH(grant(5, 1, 7, R_READ));
1340 PUSH(revoke(5, 1));
1341 PUSH(use(5, 1, 7, ARGUS_OUTCOME_OK, 0)); /* revoked use */
1342 PUSH(use(6, 1, 7, ARGUS_OUTCOME_OK, AUTH_ERR_STALE_GEN)); /* forged + stale */
1343 PUSH(world(1, 1));
1344 PUSH(world(4, 4)); /* skip */
1345 { ArgusEvent e = ev0(ARGUS_EV_SIGNATURE_FAILURE); e.principal = 8; PUSH(e); }
1346 { ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED); e.object_id = 11; e.principal = 7; e.resource = 1; PUSH(e); }
1347 { ArgusEvent e = ev0(ARGUS_EV_CREDENTIAL_LEASE_USED); e.object_id = 11; e.principal = 9; e.resource = 1; PUSH(e); }
1348 { ArgusEvent e = use(ARGUS_CAP_NONE, 0, 7, ARGUS_OUTCOME_OK, 0); mid(e.machine_id, 77); PUSH(e); } /* unknown machine */
1349 { ArgusEvent e = ev0(ARGUS_EV_TELEMETRY_DROPPED); e.flags = ARGUS_FLAG_CONSUMER; e.object_id = 1; e.resource = 2; PUSH(e); }
1350#undef PUSH
1351 s[n] = s[1]; /* exact replay */
1352 n++;
1353 return n;
1354}
1355
1356typedef struct {
1357 uint8_t state[ARGUS_DIGEST_LEN];
1358 ArgusCoreHealth health;
1359 uint8_t fchain[ARGUS_DIGEST_LEN]; /* chain over the digest of every finding copied out */
1360} Run;
1361
1362static void run_stream(const ArgusEvent *s, size_t n, size_t cap, Run *out)
1363{
1364 H h; h_new(&h);
1365 memset(out, 0, sizeof *out);
1366 for (size_t i = 0; i < n; i++) {
1367 ArgusEvent e = s[i];
1368 h.n = 0;
1369 (void)argus_core_ingest(h.c, &e, cap ? h.f : NULL, cap, &h.n);
1370 for (size_t k = 0; k < h.n; k++) {
1371 uint8_t fd[ARGUS_DIGEST_LEN];
1372 argus_finding_digest(&h.f[k], fd);
1373 ArgusEvent carrier = ev0(ARGUS_EV_NONE);
1374 memcpy(carrier.evidence_digest, fd, ARGUS_DIGEST_LEN);
1375 argus_chain_extend(out->fchain, &carrier);
1376 }
1377 }
1378 argus_core_state_digest(h.c, out->state);
1379 argus_core_health(h.c, &out->health);
1380 h_free(&h);
1381}
1382
1383static void t_same_stream_same_output(void)
1384{
1385 ArgusEvent s[32];
1386 size_t n = build_stream(s);
1387 Run a, b;
1388 run_stream(s, n, H_FBUF, &a);
1389 run_stream(s, n, H_FBUF, &b);
1390 int ok = memcmp(a.state, b.state, ARGUS_DIGEST_LEN) == 0 && memcmp(a.fchain, b.fchain, ARGUS_DIGEST_LEN) == 0 &&
1391 memcmp(a.health.chain, b.health.chain, ARGUS_DIGEST_LEN) == 0 && a.health.findings_emitted > 5;
1392 result("same_stream_same_output", ok, EXPECT_DEFENDED, "identical streams gave different findings/state");
1393}
1394
1395/* The caller's buffer size may truncate findings but never changes state. */
1396static void t_buffer_size_does_not_change_state(void)
1397{
1398 ArgusEvent s[32];
1399 size_t n = build_stream(s);
1400 Run a, b;
1401 run_stream(s, n, 0, &a);
1402 run_stream(s, n, H_FBUF, &b);
1403 int ok = memcmp(a.state, b.state, ARGUS_DIGEST_LEN) == 0 &&
1404 memcmp(a.health.chain, b.health.chain, ARGUS_DIGEST_LEN) == 0 &&
1405 a.health.findings_emitted == b.health.findings_emitted && a.health.incidents_open == b.health.incidents_open;
1406 result("buffer_size_does_not_change_state", ok, EXPECT_DEFENDED, "state depends on the caller's findings buffer size");
1407}
1408
1409/* The core holds no pointers: a byte copy is a valid, equivalent snapshot. */
1410static void t_snapshot_copy_equivalent(void)
1411{
1412 ArgusEvent s[32];
1413 size_t n = build_stream(s);
1414 H a; h_new(&a);
1415 for (size_t i = 0; i < n / 2; i++) { ArgusEvent e = s[i]; feed(&a, &e); }
1416 size_t fp = argus_core_footprint();
1417 void *copy = alloc64(fp);
1418 memcpy(copy, a.mem, fp);
1419 ArgusCore *b = (ArgusCore *)copy;
1420 ArgusFinding fb[H_FBUF];
1421 int same = 1;
1422 for (size_t i = n / 2; i < n; i++) {
1423 ArgusEvent e1 = s[i], e2 = s[i];
1424 size_t nb = 0;
1425 feed(&a, &e1);
1426 (void)argus_core_ingest(b, &e2, fb, H_FBUF, &nb);
1427 if (nb != a.n || (nb && memcmp(fb, a.f, nb * sizeof fb[0]) != 0))
1428 same = 0;
1429 }
1430 uint8_t da[ARGUS_DIGEST_LEN], db[ARGUS_DIGEST_LEN];
1431 argus_core_state_digest(a.c, da);
1432 argus_core_state_digest(b, db);
1433 result("snapshot_copy_equivalent", same && memcmp(da, db, ARGUS_DIGEST_LEN) == 0, EXPECT_DEFENDED,
1434 "a byte copy of the core diverged from the original");
1435 free(copy);
1436 h_free(&a);
1437}
1438
1439static void t_uninitialized_core_rejected(void)
1440{
1441 size_t fp = argus_core_footprint();
1442 void *mem = alloc64(fp);
1443 memset(mem, 0xAB, fp);
1444 ArgusEvent e = use(5, 1, 7, ARGUS_OUTCOME_OK, 0);
1445 e.sequence = 1;
1446 ArgusFinding f[4];
1447 size_t n = 99;
1448 int rc = argus_core_ingest((ArgusCore *)mem, &e, f, 4, &n);
1449 uint8_t d[ARGUS_DIGEST_LEN], z[ARGUS_DIGEST_LEN];
1450 memset(z, 0, sizeof z);
1451 argus_core_state_digest((ArgusCore *)mem, d);
1452 result("uninitialized_core_rejected", rc == ARGUS_ERR_ARG && memcmp(d, z, sizeof z) == 0, EXPECT_DEFENDED,
1453 "garbage memory treated as a live core");
1454 free(mem);
1455}
1456
1457/* argus_detect_run with a small buffer returns a prefix of the full result. */
1458static void t_detect_run_prefix_under_small_cap(void)
1459{
1460 H h; h_new(&h);
1461 ArgusEvent e = use(6, 1, 7, ARGUS_OUTCOME_OK, AUTH_ERR_STALE_GEN); /* unseen + stale code: 2 findings */
1462 e.sequence = 1;
1463 ArgusFinding full[16], small[1];
1464 size_t nf = 0, ns = 0;
1465 int rf = argus_detect_run(argus_core_ops(), argus_core_view(h.c), &e, full, 16, &nf);
1466 int rs = argus_detect_run(argus_core_ops(), argus_core_view(h.c), &e, small, 1, &ns);
1467 int ok = rf == ARGUS_OK && nf >= 2 && rs == ARGUS_ERR_OVERFLOW && ns == 1 &&
1468 memcmp(&full[0], &small[0], sizeof small[0]) == 0;
1469 result("detect_run_prefix_under_small_cap", ok, EXPECT_DEFENDED, "truncated detector output is not a prefix of the full output");
1470 h_free(&h);
1471}
1472
1473/* ==== G-23 state digest collisions ======================================== */
1474
1475static void digest_of(const ArgusEvent *s, size_t n, uint8_t out[ARGUS_DIGEST_LEN])
1476{
1477 H h; h_new(&h);
1478 for (size_t i = 0; i < n; i++) { ArgusEvent e = s[i]; feed(&h, &e); }
1479 argus_core_state_digest(h.c, out);
1480 h_free(&h);
1481}
1482
1483/* Each variant changes exactly one shadow field; every digest must differ. */
1484static void t_state_digest_sensitive_to_every_table(void)
1485{
1486 enum { NV = 9 };
1487 uint8_t d[NV][ARGUS_DIGEST_LEN];
1488 for (int v = 0; v < NV; v++) {
1489 ArgusEvent s[8];
1490 size_t n = 0;
1491 s[n] = grant(5, 1, 7, R_READ); s[n].resource = v == 1 ? 0x20 : 0x10; s[n].sequence = 1; n++;
1492 s[n] = machine_ev(ARGUS_EV_MACHINE_JOINED, v == 2 ? 4 : 3, 0); s[n].sequence = 2; n++;
1493 s[n] = ev0(ARGUS_EV_CREDENTIAL_LEASE_CREATED); s[n].object_id = 11; s[n].principal = 7;
1494 s[n].resource = v == 3 ? 3 : 1; s[n].sequence = 3; n++;
1495 s[n] = world(1, v == 4 ? 2 : 1); s[n].sequence = 4; n++;
1496 s[n] = ev0(ARGUS_EV_POLICY_CHANGED); dg(s[n].evidence_digest, v == 5 ? 9 : 8); s[n].sequence = 5; n++;
1497 s[n] = ev0(ARGUS_EV_PROVIDER_DISCOVERED); dg(s[n].evidence_digest, v == 6 ? 31 : 30); s[n].sequence = 6; n++;
1498 s[n] = ev0(ARGUS_EV_ARTIFACT_ADMITTED); dg(s[n].evidence_digest, 40);
1499 if (v == 7) { s[n].kind = ARGUS_EV_ARTIFACT_REJECTED; s[n].class_ = ARGUS_CLASS_CRITICAL; }
1500 s[n].sequence = 7; n++;
1501 s[n] = ev0(ARGUS_EV_SIGNATURE_FAILURE); s[n].principal = v == 8 ? 12 : 11; s[n].sequence = 8; n++;
1502 digest_of(s, n, d[v]);
1503 }
1504 int distinct = 1;
1505 for (int i = 0; i < NV; i++)
1506 for (int j = i + 1; j < NV; j++)
1507 if (memcmp(d[i], d[j], ARGUS_DIGEST_LEN) == 0)
1508 distinct = 0;
1509 result("state_digest_sensitive_to_every_table", distinct, EXPECT_DEFENDED, "two different shadow states hashed equal");
1510}
1511
1512/* The event chain and the state digest share one hash construction. They are
1513 * kept apart today only because the state digest's first record has sequence
1514 * 0, which no ingested event may have (see G-23). */
1515static void t_state_digest_domain_separated(void)
1516{
1517 H h; h_new(&h);
1518 uint8_t sd[ARGUS_DIGEST_LEN];
1519 ArgusCoreHealth hl;
1520 argus_core_state_digest(h.c, sd);
1521 argus_core_health(h.c, &hl);
1522 int differ_empty = memcmp(sd, hl.chain, ARGUS_DIGEST_LEN) != 0;
1523 ArgusEvent e = grant(5, 1, 7, R_READ); feed(&h, &e);
1524 argus_core_state_digest(h.c, sd);
1525 argus_core_health(h.c, &hl);
1526 int differ_one = memcmp(sd, hl.chain, ARGUS_DIGEST_LEN) != 0;
1527 ArgusEvent seq0 = e; seq0.sequence = 0;
1528 int seq0_rejected = argus_event_validate(&seq0) != ARGUS_OK;
1529 result("state_digest_domain_separated", differ_empty && differ_one && seq0_rejected, EXPECT_DEFENDED,
1530 "state digest can coincide with the event chain");
1531 h_free(&h);
1532}
1533
1534/* ==== G-12 secret-sized slots ============================================== */
1535
1536/* The only 32-byte fields in an event are machine_id and evidence_digest, at
1537 * the documented offsets; no other field can carry a 32-byte office secret. */
1538static void t_event_has_only_two_32byte_slots(void)
1539{
1540 int ok = sizeof(ArgusEvent) == ARGUS_EVENT_SIZE &&
1541 offsetof(ArgusEvent, machine_id) == 64 && offsetof(ArgusEvent, evidence_digest) == 96 &&
1542 sizeof(((ArgusEvent *)0)->machine_id) == 32 && sizeof(((ArgusEvent *)0)->evidence_digest) == 32 &&
1543 offsetof(ArgusEvent, resource) + sizeof(uint64_t) == 64;
1544 result("event_has_only_two_32byte_slots", ok, EXPECT_DEFENDED, "event layout grew another secret-sized field");
1545}
1546
1547int main(void)
1548{
1549 /* capability replay / generations / forgery */
1550 t_grant_replay_revives_revoked();
1551 t_exact_replay_still_applied();
1552 t_future_generation_use_unflagged();
1553 t_stale_generation_use_flagged();
1554 t_revoked_use_flagged();
1555 t_revoked_use_denied_is_quiet();
1556 t_use_by_other_subject_unflagged();
1557 t_revoke_gen_max_poisons_slot();
1558 t_cap_id_out_of_range_is_critical_loss();
1559 t_cap_id_out_of_range_use_is_forged();
1560 /* effect class */
1561 t_effect_kind_mislabelled();
1562 t_external_effect_without_right_flagged();
1563 t_evidence_write_with_read_only_right();
1564 /* confused deputy, forgery */
1565 t_reported_forgery_names_arbitrary_victim();
1566 t_forged_telemetry_dropped();
1567 t_consumer_flag_accepted_from_wire();
1568 t_unattributed_grant_masks_forged_use();
1569 /* sequence */
1570 t_sequence_replay_flagged();
1571 t_sequence_high_water_poison();
1572 t_sequence_gap_unflagged();
1573 t_replay_rekeyed_by_consumer_flag();
1574 t_replay_evades_by_rekeying_stream();
1575 t_producer_table_exhaustion();
1576 /* machines */
1577 t_quarantined_machine_use_flagged();
1578 t_quarantine_laundered_by_remove_rejoin();
1579 t_quarantined_machine_self_upgrades();
1580 t_join_declares_own_trust();
1581 t_trust_before_join_bricks_machine();
1582 t_trust_preseed_skips_observation();
1583 t_machine_table_exhaustion_hides_quarantine();
1584 t_zero_machine_id_escapes_attribution();
1585 /* providers, leases, world, artifacts, policy */
1586 t_provider_rediscovery_keeps_quarantine();
1587 t_provider_substitution_escapes_quarantine();
1588 t_undiscovered_provider_use_unflagged();
1589 t_lease_other_subject_flagged();
1590 t_lease_recreate_hijacks_subject();
1591 t_lease_table_exhaustion();
1592 t_every_table_loud_once();
1593 t_world_skip_flagged();
1594 t_world_same_generation_conflict_flagged();
1595 t_world_inconsistent_commit_adopted();
1596 t_world_repeat_silent();
1597 t_artifact_rejection_sticky();
1598 t_policy_digest_overwrite_silent();
1599 t_integrity_violation_report_ignored();
1600 /* incidents, omission */
1601 t_incident_table_poisoning();
1602 t_malformed_event_leaves_no_evidence();
1603 /* ring: saturation, liveness, recursion */
1604 t_info_flood_cannot_starve_security();
1605 t_critical_overflow_accounting();
1606 t_drain_partial_preserves_pending();
1607 t_attempt_conservation();
1608 t_security_event_lost_as_informational();
1609 t_critical_self_label_starves_security();
1610 t_push_never_blocks_without_consumer();
1611 t_drop_report_does_not_recurse();
1612 /* determinism, digests, layout */
1613 t_same_stream_same_output();
1614 t_buffer_size_does_not_change_state();
1615 t_snapshot_copy_equivalent();
1616 t_uninitialized_core_rejected();
1617 t_detect_run_prefix_under_small_cap();
1618 t_state_digest_sensitive_to_every_table();
1619 t_state_digest_domain_separated();
1620 t_event_has_only_two_32byte_slots();
1621
1622 printf("test_argus_hostile: %d defended, %d expected-fail (open/weak), %d N/A-v1 (documented, exempt), "
1623 "%d xpass, %d unexpected failures\n",
1624 n_pass, n_xfail, n_na, n_xpass, n_unexpected);
1625 return n_unexpected ? 1 : 0;
1626}