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_detect.c

1296 lines · 63451 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_detect.c -- lane E tests for the ARGUS-0 hard-invariant detectors.
19 *
20 * For every detector: positive cases (the violation fires, fields correct) and
21 * negative cases (legal behavior, including correct authority refusals, stays
22 * silent). Then the synthetic legal-day corpus (must yield zero findings) and
23 * the hostile corpus (detected findings == injected list, exactly).
24 * Shadow state is the in-memory stub (tests/stub_state.c), not the core.
25 */
26#include "../argus_abi.h"
27#include "../argus_detect.h"
28#include "../../capability/aienos_capability.h"
29#include "stub_state.h"
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35static int failures;
36static int checks;
37static int pos[ARGUS_F_MAX + 1];
38static int neg[ARGUS_F_MAX + 1];
39
40#define CHECK(cond, ...) \
41 do { \
42 checks++; \
43 if (!(cond)) { \
44 failures++; \
45 fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
46 fprintf(stderr, __VA_ARGS__); \
47 fprintf(stderr, "\n"); \
48 } \
49 } while (0)
50
51static StubState st;
52static const ArgusStateView *V;
53static const ArgusStateOps *OPS;
54
55static uint8_t MA[ARGUS_MACHINE_ID_LEN], MQ[ARGUS_MACHINE_ID_LEN], MU[ARGUS_MACHINE_ID_LEN];
56static uint8_t AL[ARGUS_DIGEST_LEN], AR[ARGUS_DIGEST_LEN], AU[ARGUS_DIGEST_LEN];
57static uint8_t PL[ARGUS_DIGEST_LEN], PQ[ARGUS_DIGEST_LEN], PN[ARGUS_DIGEST_LEN];
58static uint8_t WD[ARGUS_DIGEST_LEN], WX[ARGUS_DIGEST_LEN];
59
60static void pattern(uint8_t *b, uint8_t tag)
61{
62 for (size_t i = 0; i < 32; i++)
63 b[i] = (uint8_t)(tag + i * 7u);
64}
65
66/* Fixture:
67 * cap 1 LIVE gen 3 subject 7 rights R|W|EFFECT (granted @10)
68 * cap 2 REVOKED gen 1 subject 7 rights R|W (revoked @20)
69 * cap 3 LIVE gen 1 subject 7 rights R only (granted @30)
70 * machine MA joined TRUSTED (@5), MQ joined QUARANTINED (changed @40), MU unknown
71 * artifact AL admitted (@50), AR rejected (@51), AU unknown
72 * lease 1 LIVE subject 7 scope 0xF0 (@60), lease 2 REVOKED (@61)
73 * provider PL live (@70), PQ quarantined (@71), PN unknown
74 * world gen 5 digest WD (@80) */
75static void reset(void)
76{
77 stub_state_init(&st);
78 V = stub_state_view(&st);
79 OPS = stub_state_ops();
80 pattern(MA, 0x11); pattern(MQ, 0x12); pattern(MU, 0x13);
81 pattern(AL, 0x21); pattern(AR, 0x22); pattern(AU, 0x23);
82 pattern(PL, 0x31); pattern(PQ, 0x32); pattern(PN, 0x33);
83 pattern(WD, 0x41); pattern(WX, 0x42);
84
85 ArgusCapShadow c = {0};
86 c.cap_id = 1; c.generation = 3; c.state = ARGUS_SHADOW_LIVE; c.subject = 7;
87 c.rights = AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE | AIENOS_CAP_RIGHT_EFFECT;
88 c.resource = 0x100; c.granted_sequence = 10;
89 stub_put_cap(&st, &c);
90 c.cap_id = 2; c.generation = 1; c.state = ARGUS_SHADOW_REVOKED;
91 c.rights = AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE; c.granted_sequence = 11; c.revoked_sequence = 20;
92 stub_put_cap(&st, &c);
93 c.cap_id = 3; c.generation = 1; c.state = ARGUS_SHADOW_LIVE; c.rights = AIENOS_CAP_RIGHT_READ;
94 c.granted_sequence = 30; c.revoked_sequence = 0;
95 stub_put_cap(&st, &c);
96
97 ArgusMachineShadow m = {0};
98 memcpy(m.machine_id, MA, 32); m.trust = ARGUS_TRUST_TRUSTED; m.joined_sequence = 5; m.changed_sequence = 5;
99 stub_put_machine(&st, &m);
100 memcpy(m.machine_id, MQ, 32); m.trust = ARGUS_TRUST_QUARANTINED; m.joined_sequence = 6; m.changed_sequence = 40;
101 stub_put_machine(&st, &m);
102
103 ArgusArtifactShadow a = {0};
104 memcpy(a.digest, AL, 32); a.state = ARGUS_SHADOW_LIVE; a.sequence = 50;
105 stub_put_artifact(&st, &a);
106 memcpy(a.digest, AR, 32); a.state = ARGUS_SHADOW_REJECTED; a.sequence = 51;
107 stub_put_artifact(&st, &a);
108
109 ArgusLeaseShadow l = {0};
110 l.lease_id = 1; l.subject = 7; l.scope = 0xF0; l.state = ARGUS_SHADOW_LIVE; l.sequence = 60;
111 stub_put_lease(&st, &l);
112 l.lease_id = 2; l.state = ARGUS_SHADOW_REVOKED; l.sequence = 61;
113 stub_put_lease(&st, &l);
114
115 ArgusProviderShadow p = {0};
116 memcpy(p.provider_id, PL, 32); p.state = ARGUS_SHADOW_LIVE; p.sequence = 70;
117 stub_put_provider(&st, &p);
118 memcpy(p.provider_id, PQ, 32); p.state = ARGUS_SHADOW_REVOKED; p.sequence = 71;
119 stub_put_provider(&st, &p);
120
121 stub_set_world(&st, 0, 5, WD, 80);
122}
123
124static uint64_t next_seq = 1000;
125
126static ArgusEvent mk(uint16_t kind, uint8_t outcome, int32_t code)
127{
128 ArgusEvent e;
129 memset(&e, 0, sizeof e);
130 e.version = ARGUS_ABI_VERSION;
131 e.class_ = ARGUS_CLASS_SECURITY;
132 e.kind = kind;
133 e.outcome = outcome;
134 e.code = code;
135 e.flags = ARGUS_FLAG_SYNTHETIC;
136 e.cap_id = ARGUS_CAP_NONE; /* v1.1: cap_id 0 is the office slot, "none" is CAP_NONE */
137 e.sequence = ++next_seq;
138 e.tick = e.sequence;
139 return e;
140}
141
142static ArgusEvent use(uint16_t kind, uint8_t outcome, int32_t code, uint32_t cap_id, uint64_t gen)
143{
144 ArgusEvent e = mk(kind, outcome, code);
145 e.cap_id = cap_id;
146 e.cap_generation = gen;
147 e.principal = 7;
148 return e;
149}
150
151static ArgusFinding F[32];
152static size_t NF;
153
154static int run(const ArgusEvent *e)
155{
156 StubState before = st;
157 int rc = argus_detect_run(OPS, V, e, F, 32, &NF);
158 CHECK(memcmp(&before, &st, sizeof st) == 0, "detectors modified shadow state");
159 return rc;
160}
161
162/* Table entry for a finding code (ids are 1..10, 14, 15; not index + 1). */
163static const ArgusDetector *det_of(uint16_t id)
164{
165 for (size_t i = 0; i < argus_hard_detector_count; i++)
166 if (argus_hard_detectors[i].id == id)
167 return &argus_hard_detectors[i];
168 return NULL;
169}
170
171/* Exactly one finding, with code `det`; checks the common fields. sync/contain
172 * < 0 means "the detector table default". */
173static void expect_only_as(uint16_t det, const ArgusEvent *e, const char *what, int sync, int contain)
174{
175 int rc = run(e);
176 CHECK(rc == ARGUS_OK, "[%u %s] rc %d", det, what, rc);
177 CHECK(NF == 1, "[%u %s] expected 1 finding, got %zu (first code %u)", det, what, NF, NF ? F[0].code : 0);
178 const ArgusDetector *d = det_of(det);
179 CHECK(d != NULL, "[%u %s] no detector", det, what);
180 if (NF >= 1 && d != NULL) {
181 const ArgusFinding *f = &F[0];
182 CHECK(f->code == det, "[%u %s] code %u", det, what, f->code);
183 CHECK(f->detector == det, "[%u %s] detector %u", det, what, f->detector);
184 CHECK(f->confidence == ARGUS_CONF_DETERMINISTIC, "[%u %s] confidence", det, what);
185 CHECK(f->sync_allowed == (sync < 0 ? d->sync_allowed : sync), "[%u %s] sync %u", det, what, f->sync_allowed);
186 if (contain >= 0)
187 CHECK(f->containment == contain, "[%u %s] containment %u", det, what, f->containment);
188 CHECK(f->sequence == e->sequence, "[%u %s] sequence", det, what);
189 CHECK(f->principal == e->principal, "[%u %s] principal", det, what);
190 CHECK(f->cap_id == e->cap_id && f->cap_generation == e->cap_generation, "[%u %s] cap", det, what);
191 CHECK(memcmp(f->machine_id, e->machine_id, 32) == 0, "[%u %s] machine", det, what);
192 static const uint8_t zero[32];
193 CHECK(memcmp(f->event_digest, zero, 32) == 0, "[%u %s] event_digest must stay zero", det, what);
194 }
195 pos[det]++;
196}
197
198static void expect_only(uint16_t det, const ArgusEvent *e, const char *what)
199{
200 expect_only_as(det, e, what, -1, -1);
201}
202
203/* Self-report without shadow corroboration (G-2): sync 0, containment NONE. */
204static void expect_hearsay(uint16_t det, const ArgusEvent *e, const char *what)
205{
206 expect_only_as(det, e, what, 0, ARGUS_CONTAIN_NONE);
207}
208
209static void expect_none(uint16_t det, const ArgusEvent *e, const char *what)
210{
211 int rc = run(e);
212 CHECK(rc == ARGUS_OK, "[%u %s] rc %d", det, what, rc);
213 CHECK(NF == 0, "[%u %s] expected no finding, got %zu (first code %u)", det, what, NF, NF ? F[0].code : 0);
214 neg[det]++;
215}
216
217/* Detect on pre-state, then apply: the core's order. */
218static size_t step(const ArgusEvent *e)
219{
220 run(e);
221 size_t n = NF;
222 stub_state_apply(&st, e);
223 return n;
224}
225
226/* ---- per-detector ---------------------------------------------------------- */
227
228static void t_forged(void)
229{
230 ArgusEvent e;
231 reset();
232 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 99, 1);
233 expect_only(1, &e, "unseen ref used OK");
234 CHECK(F[0].severity == ARGUS_SEV_CRITICAL && F[0].containment == ARGUS_CONTAIN_FREEZE_PRINCIPAL, "d1 sev/contain");
235 e = mk(ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS);
236 expect_hearsay(1, &e, "producer reports forgery, no ref");
237 e = use(ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS, 99, 1);
238 e.principal = 1;
239 expect_hearsay(1, &e, "forgery report names unseen slot + victim (G-2)");
240 e = use(ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS, 1, 9);
241 e.principal = 8;
242 expect_hearsay(1, &e, "forgery report, principal is not the slot's subject");
243 e = use(ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS, 1, 3);
244 expect_hearsay(1, &e, "forgery report at the minted generation");
245 e = use(ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS, 1, 9);
246 expect_only_as(1, &e, "forgery report corroborated (subject's slot, unminted gen)", 1,
247 ARGUS_CONTAIN_FREEZE_PRINCIPAL);
248 CHECK(F[0].prior_sequence == 10, "d1 corroborated prior = grant");
249 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 4);
250 expect_only(1, &e, "future generation used OK (G-4)");
251 CHECK(F[0].prior_sequence == 10 && F[0].containment == ARGUS_CONTAIN_FREEZE_PRINCIPAL, "d1 future fields");
252 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 2, 5);
253 expect_only(1, &e, "future gen of a revoked slot: 1 only (v1.1: 3 needs gen <= shadow)");
254 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_BOUNDS, 1, 3);
255 expect_only(1, &e, "OK with ERR_BOUNDS");
256 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_CHAIN, 1, 3);
257 expect_only(1, &e, "OK with ERR_CHAIN");
258 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, 99, 1);
259 e.effect_class = ARGUS_EFFECT_EXTERNAL;
260 expect_only(1, &e, "unseen ref commits effect");
261
262 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
263 expect_none(1, &e, "live ref used OK");
264 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_BOUNDS, 99, 1);
265 expect_none(1, &e, "unseen ref refused by authority");
266 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 99, 1);
267 e.object_id = AIENOS_CAP_RIGHT_READ;
268 expect_none(1, &e, "new grant of unseen slot");
269 CHECK(step(&e) == 0, "grant step");
270 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 99, 1);
271 expect_none(1, &e, "use after grant");
272 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, ARGUS_CAP_NONE, 0);
273 expect_none(1, &e, "use with no cap reference");
274 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_STALE_GEN, 1, 4);
275 expect_none(1, &e, "future generation refused");
276 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 1, 4);
277 e.object_id = AIENOS_CAP_RIGHT_READ;
278 expect_none(1, &e, "re-grant at a higher generation");
279}
280
281static void t_stale(void)
282{
283 ArgusEvent e;
284 reset();
285 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 2);
286 expect_only(2, &e, "old gen accepted");
287 CHECK(F[0].prior_sequence == 10 && F[0].severity == ARGUS_SEV_HIGH &&
288 F[0].containment == ARGUS_CONTAIN_REVOKE_CAPABILITY, "d2 fields");
289 e = mk(ARGUS_EV_STALE_GENERATION, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_STALE_GEN);
290 expect_hearsay(2, &e, "producer reports stale gen, no ref");
291 e = use(ARGUS_EV_STALE_GENERATION, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_STALE_GEN, 1, 2);
292 e.principal = 9;
293 expect_hearsay(2, &e, "stale report names another principal");
294 e = use(ARGUS_EV_STALE_GENERATION, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_STALE_GEN, 1, 2);
295 expect_only_as(2, &e, "stale report corroborated", 1, ARGUS_CONTAIN_REVOKE_CAPABILITY);
296 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_STALE_GEN, 1, 3);
297 expect_only(2, &e, "OK with ERR_STALE_GEN");
298
299 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_STALE_GEN, 1, 2);
300 expect_none(2, &e, "old gen refused");
301 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
302 expect_none(2, &e, "current gen accepted");
303 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 1, 4);
304 e.object_id = AIENOS_CAP_RIGHT_READ;
305 expect_none(2, &e, "re-grant at next gen");
306 CHECK(step(&e) == 0, "regrant step");
307 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 4);
308 expect_none(2, &e, "use at new gen after re-grant");
309}
310
311static void t_revoked(void)
312{
313 ArgusEvent e;
314 reset();
315 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 2, 1);
316 expect_only(3, &e, "revoked ref accepted");
317 CHECK(F[0].prior_sequence == 20 && F[0].severity == ARGUS_SEV_CRITICAL &&
318 F[0].containment == ARGUS_CONTAIN_FREEZE_PRINCIPAL, "d3 fields");
319 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_REVOKED, 1, 3);
320 expect_only(3, &e, "OK with ERR_REVOKED");
321 /* revoke via apply, then accepted use */
322 e = use(ARGUS_EV_CAPABILITY_REVOKED, ARGUS_OUTCOME_OK, 0, 3, 1);
323 CHECK(step(&e) == 0, "revoke step");
324 e = use(ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_OUTCOME_OK, 0, 3, 1);
325 e.effect_class = ARGUS_EFFECT_EPHEMERAL;
326 run(&e);
327 CHECK(NF == 2 && F[0].code == 3 && F[1].code == 8, "revoked + no effect right: 3 then 8 in id order (%zu)", NF);
328 pos[3]++;
329
330 reset();
331 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_REVOKED, 2, 1);
332 expect_none(3, &e, "revoked ref refused");
333 e = use(ARGUS_EV_CAPABILITY_REVOKED, ARGUS_OUTCOME_OK, 0, 1, 3);
334 expect_none(3, &e, "revocation itself");
335 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 2, 2);
336 e.object_id = AIENOS_CAP_RIGHT_READ;
337 CHECK(step(&e) == 0, "regrant revoked slot");
338 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 2, 2);
339 expect_none(3, &e, "use of re-granted slot");
340}
341
342static void t_artifact(void)
343{
344 ArgusEvent e;
345 reset();
346 e = mk(ARGUS_EV_ARTIFACT_ACTIVATED, ARGUS_OUTCOME_OK, 0);
347 memcpy(e.evidence_digest, AU, 32);
348 expect_only(4, &e, "unknown digest activated");
349 CHECK(F[0].prior_sequence == 0 && F[0].containment == ARGUS_CONTAIN_REJECT_ARTIFACT, "d4 fields");
350 memcpy(e.evidence_digest, AR, 32);
351 expect_only(4, &e, "rejected digest activated");
352 CHECK(F[0].prior_sequence == 51, "d4 prior = rejection");
353
354 memcpy(e.evidence_digest, AL, 32);
355 expect_none(4, &e, "admitted digest activated");
356 e = mk(ARGUS_EV_ARTIFACT_ACTIVATED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED);
357 memcpy(e.evidence_digest, AU, 32);
358 expect_none(4, &e, "unknown digest refused");
359 e = mk(ARGUS_EV_ARTIFACT_ADMITTED, ARGUS_OUTCOME_OK, 0);
360 memcpy(e.evidence_digest, AU, 32);
361 expect_none(4, &e, "admission of new digest");
362 CHECK(step(&e) == 0, "admit step");
363 e = mk(ARGUS_EV_ARTIFACT_ACTIVATED, ARGUS_OUTCOME_OK, 0);
364 memcpy(e.evidence_digest, AU, 32);
365 expect_none(4, &e, "activation after admission");
366}
367
368static void t_signature(void)
369{
370 ArgusEvent e;
371 reset();
372 e = mk(ARGUS_EV_SIGNATURE_FAILURE, ARGUS_OUTCOME_DENIED, 0);
373 expect_hearsay(5, &e, "signature failure (denied)");
374 CHECK(F[0].severity == ARGUS_SEV_HIGH, "d5 severity");
375 e = mk(ARGUS_EV_SIGNATURE_FAILURE, ARGUS_OUTCOME_OK, 0);
376 expect_hearsay(5, &e, "signature failure (ok)");
377 e = mk(ARGUS_EV_ARTIFACT_ADMITTED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_RIGHTS);
378 memcpy(e.evidence_digest, AU, 32);
379 expect_only_as(5, &e, "admitted despite error code", 1, ARGUS_CONTAIN_REJECT_ARTIFACT);
380 e = mk(ARGUS_EV_INTEGRITY_VIOLATION, ARGUS_OUTCOME_ERROR, 0);
381 expect_hearsay(5, &e, "integrity violation reported (G-14)");
382 e = mk(ARGUS_EV_INTEGRITY_VIOLATION, ARGUS_OUTCOME_DENIED, 0);
383 expect_hearsay(5, &e, "integrity violation reported, denied");
384 /* stored policy / runtime digests (G-14) */
385 memcpy(st.policy, WD, 32);
386 memcpy(st.runtime, AL, 32);
387 e = mk(ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_OK, 0);
388 memcpy(e.evidence_digest, WX, 32);
389 expect_hearsay(5, &e, "policy digest replaced");
390 e = mk(ARGUS_EV_RUNTIME_BUILD_CHANGED, ARGUS_OUTCOME_OK, 0);
391 memcpy(e.evidence_digest, WX, 32);
392 expect_hearsay(5, &e, "runtime digest replaced");
393
394 e = mk(ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_OK, 0);
395 memcpy(e.evidence_digest, WD, 32);
396 expect_none(5, &e, "policy re-announced unchanged");
397 e = mk(ARGUS_EV_RUNTIME_BUILD_CHANGED, ARGUS_OUTCOME_OK, 0);
398 memcpy(e.evidence_digest, AL, 32);
399 expect_none(5, &e, "runtime re-announced unchanged");
400 e = mk(ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED);
401 memcpy(e.evidence_digest, WX, 32);
402 expect_none(5, &e, "policy change refused");
403 memset(st.policy, 0, 32);
404 memset(st.runtime, 0, 32);
405 e = mk(ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_OK, 0);
406 memcpy(e.evidence_digest, WX, 32);
407 expect_none(5, &e, "first policy announcement (nothing stored)");
408 e = mk(ARGUS_EV_RUNTIME_BUILD_CHANGED, ARGUS_OUTCOME_OK, 0);
409 memcpy(e.evidence_digest, WX, 32);
410 expect_none(5, &e, "first runtime announcement (nothing stored)");
411 CHECK(step(&e) == 0, "announce runtime");
412 CHECK(memcmp(st.runtime, WX, 32) == 0, "runtime digest stored");
413
414 e = mk(ARGUS_EV_ARTIFACT_ADMITTED, ARGUS_OUTCOME_OK, 0);
415 memcpy(e.evidence_digest, AU, 32);
416 expect_none(5, &e, "clean admission");
417 e = mk(ARGUS_EV_ARTIFACT_REJECTED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS);
418 memcpy(e.evidence_digest, AU, 32);
419 expect_none(5, &e, "rejection");
420 e = mk(ARGUS_EV_ARTIFACT_ADMITTED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS);
421 memcpy(e.evidence_digest, AU, 32);
422 expect_none(5, &e, "admission refused with code");
423}
424
425static ArgusEvent lease_ev(uint16_t kind, uint8_t outcome, int32_t code, uint32_t id, uint32_t subj, uint64_t res)
426{
427 ArgusEvent e = mk(kind, outcome, code);
428 e.object_id = id;
429 e.principal = subj;
430 e.resource = res;
431 return e;
432}
433
434static void t_lease(void)
435{
436 ArgusEvent e;
437 reset();
438 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 1, 8, 0x10);
439 expect_only(6, &e, "other subject");
440 CHECK(F[0].prior_sequence == 60 && F[0].sync_allowed == 0 &&
441 F[0].containment == ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE, "d6 fields");
442 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 1, 7, 0x110);
443 expect_only(6, &e, "out of scope");
444 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 2, 7, 0x10);
445 expect_only(6, &e, "revoked lease");
446 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 9, 7, 0x10);
447 expect_only(6, &e, "unknown lease");
448 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 1, 9, UINT64_MAX);
449 expect_only(6, &e, "CREATED re-uses a LIVE lease id (G-26)");
450 CHECK(F[0].prior_sequence == 60, "d6 recreate prior = existing lease");
451 CHECK(step(&e) == 1, "recreate step");
452 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 1, 9, 0xFF);
453 expect_only(6, &e, "hijacker's use after a refused re-create");
454 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 2, 7, 0x10);
455 expect_only(6, &e, "CREATED re-uses a REVOKED lease id (ruling A)");
456 CHECK(F[0].prior_sequence == 61, "d6 revive prior = revocation");
457 CHECK(step(&e) == 1, "revive step");
458 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 2, 7, 0x10);
459 expect_only(6, &e, "revoked lease stays revoked after a refused re-create");
460
461 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 1, 7, 0x30);
462 expect_none(6, &e, "in scope by holder");
463 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_REVOKED, 2, 7, 0x10);
464 expect_none(6, &e, "revoked lease refused");
465 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS, 1, 7, 0x110);
466 expect_none(6, &e, "out of scope refused");
467 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 9, 3, 0xFF);
468 expect_none(6, &e, "lease creation");
469 CHECK(step(&e) == 0, "create step");
470 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0, 9, 3, 0x0F);
471 expect_none(6, &e, "use after creation");
472 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED, 1, 9, 0xFF);
473 expect_none(6, &e, "re-create refused by the authority");
474 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 10, 7, 0xFF);
475 expect_none(6, &e, "fresh lease id");
476}
477
478static void t_machine(void)
479{
480 ArgusEvent e;
481 reset();
482 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
483 memcpy(e.machine_id, MU, 32);
484 expect_only(7, &e, "unknown machine");
485 CHECK(F[0].sync_allowed == 0 && F[0].containment == ARGUS_CONTAIN_REQUIRE_REATTESTATION, "d7 fields");
486 e = mk(ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0);
487 memcpy(e.machine_id, MA, 32);
488 expect_only(7, &e, "re-join without removal");
489 CHECK(F[0].prior_sequence == 5, "d7 prior = first join");
490 e = mk(ARGUS_EV_MACHINE_REMOVED, ARGUS_OUTCOME_OK, 0);
491 memcpy(e.machine_id, MA, 32);
492 CHECK(step(&e) == 0, "remove step");
493 e = mk(ARGUS_EV_WORLD_COMMITTED, ARGUS_OUTCOME_OK, 0);
494 e.world_generation = 6;
495 memcpy(e.evidence_digest, WX, 32);
496 memcpy(e.machine_id, MA, 32);
497 expect_only(7, &e, "removed machine acts");
498 e = mk(ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_OUTCOME_OK, 0);
499 memcpy(e.machine_id, MU, 32);
500 e.object_id = ARGUS_TRUST_RESTRICTED;
501 expect_only(7, &e, "TRUST_CHANGED for an unknown machine (G-10)");
502 CHECK(step(&e) == 1, "unknown trust change step");
503 ArgusMachineShadow ms;
504 CHECK(OPS->machine(V, MU, &ms) != ARGUS_OK, "unknown machine gets no entry from TRUST_CHANGED");
505 e.object_id = ARGUS_TRUST_TRUSTED;
506 expect_only(7, &e, "upward TRUST_CHANGED for an unknown machine is 7, not 15");
507 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
508 e.flags = 0;
509 expect_only(7, &e, "live producer event with zero machine_id (G-8)");
510 CHECK(F[0].prior_sequence == 0, "d7 zero id prior");
511 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS, 1, 3);
512 e.flags = 0;
513 expect_only(7, &e, "live refused event with zero machine_id");
514 e = mk(ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0);
515 e.flags = 0;
516 expect_only(7, &e, "live JOINED with zero machine_id");
517
518 reset();
519 e = mk(ARGUS_EV_TELEMETRY_DROPPED, ARGUS_OUTCOME_OK, 0);
520 e.flags = ARGUS_FLAG_CONSUMER;
521 e.class_ = ARGUS_CLASS_CRITICAL;
522 e.object_id = ARGUS_CLASS_SECURITY;
523 e.resource = 3;
524 expect_none(7, &e, "ARGUS's own TELEMETRY_DROPPED has no machine");
525 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
526 e.flags = 0;
527 memcpy(e.machine_id, MA, 32);
528 expect_none(7, &e, "live producer event attributed to a joined machine");
529 e = mk(ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0);
530 memcpy(e.machine_id, MU, 32);
531 expect_none(7, &e, "new machine joins");
532 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
533 memcpy(e.machine_id, MA, 32);
534 expect_none(7, &e, "joined machine acts");
535 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
536 expect_none(7, &e, "no machine attribution");
537 e = mk(ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED);
538 memcpy(e.machine_id, MA, 32);
539 expect_none(7, &e, "refused re-join");
540 e = mk(ARGUS_EV_MACHINE_REMOVED, ARGUS_OUTCOME_OK, 0);
541 memcpy(e.machine_id, MA, 32);
542 CHECK(step(&e) == 0, "remove");
543 e = mk(ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0);
544 memcpy(e.machine_id, MA, 32);
545 expect_none(7, &e, "re-join after removal");
546 CHECK(step(&e) == 0, "rejoin");
547 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
548 memcpy(e.machine_id, MA, 32);
549 expect_none(7, &e, "acts after re-join");
550}
551
552static void t_effect(void)
553{
554 ArgusEvent e;
555 reset();
556 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, 3, 1);
557 e.effect_class = ARGUS_EFFECT_EXTERNAL;
558 expect_only(8, &e, "committed without effect right");
559 CHECK(F[0].prior_sequence == 30 && F[0].severity == ARGUS_SEV_CRITICAL &&
560 F[0].containment == ARGUS_CONTAIN_PAUSE_EXTERNAL_EFFECTS, "d8 fields");
561 e = use(ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_OUTCOME_OK, 0, ARGUS_CAP_NONE, 0);
562 e.effect_class = ARGUS_EFFECT_EXTERNAL;
563 expect_only(8, &e, "external effect with no cap");
564 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 3, 1);
565 e.effect_class = ARGUS_EFFECT_EXTERNAL;
566 expect_only(8, &e, "use tagged external without right");
567
568 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, 1, 3);
569 e.effect_class = ARGUS_EFFECT_EXTERNAL;
570 expect_none(8, &e, "committed with effect right");
571 e = use(ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS, 3, 1);
572 e.effect_class = ARGUS_EFFECT_EXTERNAL;
573 expect_none(8, &e, "request refused");
574 e = use(ARGUS_EV_EXTERNAL_EFFECT_DENIED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS, 3, 1);
575 e.effect_class = ARGUS_EFFECT_EXTERNAL;
576 expect_none(8, &e, "effect denied event");
577 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 3, 1);
578 e.effect_class = ARGUS_EFFECT_EPHEMERAL;
579 expect_none(8, &e, "ephemeral use without effect right");
580
581 /* G-24: the kind wins over the label; EVIDENCE needs WRITE */
582 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, ARGUS_CAP_NONE, 0);
583 e.effect_class = ARGUS_EFFECT_NONE;
584 expect_only(8, &e, "EXTERNAL_EFFECT_COMMITTED labelled NONE, no cap");
585 e = use(ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_OUTCOME_OK, 0, 3, 1);
586 e.effect_class = ARGUS_EFFECT_EVIDENCE;
587 expect_only(8, &e, "EXTERNAL_EFFECT_REQUESTED labelled EVIDENCE, cap without EFFECT");
588 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 3, 1);
589 e.effect_class = ARGUS_EFFECT_EVIDENCE;
590 expect_only(8, &e, "EVIDENCE write under READ-only cap");
591 CHECK(F[0].prior_sequence == 30, "d8 evidence prior = grant");
592 e = use(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0, 3, 1);
593 memcpy(e.evidence_digest, PL, 32);
594 e.effect_class = ARGUS_EFFECT_EVIDENCE;
595 expect_only(8, &e, "provider use writes EVIDENCE under READ-only cap");
596
597 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
598 e.effect_class = ARGUS_EFFECT_EVIDENCE;
599 expect_none(8, &e, "EVIDENCE write under WRITE cap");
600 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, 1, 3);
601 e.effect_class = ARGUS_EFFECT_NONE;
602 expect_none(8, &e, "mislabelled commit under EFFECT cap");
603 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_RIGHTS, 3, 1);
604 e.effect_class = ARGUS_EFFECT_EVIDENCE;
605 expect_none(8, &e, "EVIDENCE write refused");
606 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, ARGUS_CAP_NONE, 0);
607 e.effect_class = ARGUS_EFFECT_EVIDENCE;
608 expect_none(8, &e, "EVIDENCE with no capability is out of scope");
609}
610
611static ArgusEvent world_ev(uint8_t outcome, uint64_t gen, const uint8_t *digest)
612{
613 ArgusEvent e = mk(ARGUS_EV_WORLD_COMMITTED, outcome, 0);
614 e.world_generation = gen;
615 memcpy(e.evidence_digest, digest, 32);
616 return e;
617}
618
619static void t_world(void)
620{
621 ArgusEvent e;
622 reset();
623 e = world_ev(ARGUS_OUTCOME_OK, 7, WX);
624 expect_only(9, &e, "skip");
625 CHECK(F[0].prior_sequence == 80 && F[0].sync_allowed == 0 &&
626 F[0].containment == ARGUS_CONTAIN_RAISE_EFFECT_CLASS, "d9 fields");
627 e = world_ev(ARGUS_OUTCOME_OK, 4, WX);
628 expect_only(9, &e, "rollback");
629 e = world_ev(ARGUS_OUTCOME_OK, 5, WX);
630 expect_only(9, &e, "same gen, different digest");
631 e = world_ev(ARGUS_OUTCOME_OK, 0x100000006ull, WX);
632 expect_only(9, &e, "64-bit: high bits differ");
633 stub_set_world(&st, 0, UINT64_MAX, WD, 81);
634 e = world_ev(ARGUS_OUTCOME_OK, 0, WX);
635 expect_only(9, &e, "wrap at UINT64_MAX");
636
637 reset();
638 e = world_ev(ARGUS_OUTCOME_OK, 6, WX);
639 expect_none(9, &e, "next generation");
640 e = world_ev(ARGUS_OUTCOME_OK, 5, WD);
641 expect_none(9, &e, "idempotent replay");
642 e = world_ev(ARGUS_OUTCOME_DENIED, 9, WX);
643 expect_none(9, &e, "refused commit");
644 stub_state_init(&st);
645 e = world_ev(ARGUS_OUTCOME_OK, 42, WX);
646 expect_none(9, &e, "first commit ever");
647 CHECK(step(&e) == 0, "first commit step");
648 e = world_ev(ARGUS_OUTCOME_OK, 43, WD);
649 expect_none(9, &e, "then +1");
650 stub_set_world(&st, 0, 0xFFFFFFFFull, WD, 90);
651 e = world_ev(ARGUS_OUTCOME_OK, 0x100000000ull, WX);
652 expect_none(9, &e, "64-bit: +1 across 2^32");
653}
654
655static void t_quarantine(void)
656{
657 ArgusEvent e;
658 reset();
659 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
660 memcpy(e.evidence_digest, PQ, 32);
661 expect_only(10, &e, "quarantined provider used");
662 CHECK(F[0].prior_sequence == 71 && F[0].containment == ARGUS_CONTAIN_QUARANTINE_PROVIDER, "d10 provider fields");
663 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
664 memcpy(e.machine_id, MQ, 32);
665 expect_only(10, &e, "quarantined machine acts");
666 CHECK(F[0].prior_sequence == 40 && F[0].containment == ARGUS_CONTAIN_QUARANTINE_MACHINE, "d10 machine fields");
667 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
668 memcpy(e.evidence_digest, PQ, 32);
669 memcpy(e.machine_id, MQ, 32);
670 run(&e);
671 CHECK(NF == 2 && F[0].code == 10 && F[1].code == 10 &&
672 F[0].containment == ARGUS_CONTAIN_QUARANTINE_PROVIDER &&
673 F[1].containment == ARGUS_CONTAIN_QUARANTINE_MACHINE, "d10 both clauses (%zu)", NF);
674 pos[10]++;
675
676 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
677 memcpy(e.evidence_digest, PL, 32);
678 expect_none(10, &e, "live provider used");
679 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED);
680 memcpy(e.evidence_digest, PQ, 32);
681 expect_none(10, &e, "quarantined provider refused");
682 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED, 1, 3);
683 memcpy(e.machine_id, MQ, 32);
684 expect_none(10, &e, "quarantined machine refused");
685 e = mk(ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_OUTCOME_OK, 0);
686 memcpy(e.machine_id, MQ, 32);
687 e.object_id = ARGUS_TRUST_UNTRUSTED;
688 expect_none(10, &e, "quarantined machine lowered further (lifecycle exempt)");
689 e.object_id = ARGUS_TRUST_TRUSTED;
690 run(&e);
691 CHECK(NF == 1 && F[0].code == ARGUS_F_TRUST_ESCALATION, "self-release from quarantine is 15, not 10 (%zu)", NF);
692 neg[10]++;
693 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
694 memcpy(e.evidence_digest, PN, 32);
695 expect_only_as(10, &e, "never-discovered provider used (G-9)", 0, ARGUS_CONTAIN_QUARANTINE_PROVIDER);
696 CHECK(F[0].severity == ARGUS_SEV_HIGH && F[0].prior_sequence == 0, "d10 unseen provider fields");
697 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_UNAUTHORIZED);
698 memcpy(e.evidence_digest, PN, 32);
699 expect_none(10, &e, "never-discovered provider refused");
700 e = mk(ARGUS_EV_PROVIDER_CHANGED, ARGUS_OUTCOME_OK, 0);
701 memcpy(e.evidence_digest, PN, 32);
702 expect_none(10, &e, "PROVIDER_CHANGED is observation only");
703 /* provider used before quarantine: silent; after: fires */
704 e = mk(ARGUS_EV_PROVIDER_DISCOVERED, ARGUS_OUTCOME_OK, 0);
705 memcpy(e.evidence_digest, PN, 32);
706 CHECK(step(&e) == 0, "discover");
707 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
708 memcpy(e.evidence_digest, PN, 32);
709 expect_none(10, &e, "provider used before quarantine");
710 e = mk(ARGUS_EV_PROVIDER_QUARANTINED, ARGUS_OUTCOME_OK, 0);
711 memcpy(e.evidence_digest, PN, 32);
712 CHECK(step(&e) == 0, "quarantine");
713 e = mk(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0);
714 memcpy(e.evidence_digest, PN, 32);
715 expect_only(10, &e, "provider used after quarantine");
716}
717
718static void t_subject(void)
719{
720 ArgusEvent e;
721 reset();
722 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
723 e.principal = 8;
724 expect_only(14, &e, "live ref used OK by another principal (G-3)");
725 CHECK(F[0].prior_sequence == 10 && F[0].severity == ARGUS_SEV_HIGH &&
726 F[0].containment == ARGUS_CONTAIN_FREEZE_PRINCIPAL, "d14 fields");
727 e = use(ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0, 1, 3);
728 e.effect_class = ARGUS_EFFECT_EXTERNAL;
729 e.principal = 8;
730 expect_only(14, &e, "effect committed by another principal");
731 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 2, 1);
732 e.principal = 8;
733 run(&e);
734 CHECK(NF == 2 && F[0].code == 3 && F[1].code == 14, "revoked ref by another principal: 3 then 14 (%zu)", NF);
735 pos[14]++;
736
737 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
738 expect_none(14, &e, "used by its subject");
739 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_SUBJECT, 1, 3);
740 e.principal = 8;
741 expect_none(14, &e, "other principal refused by the authority");
742 e = use(ARGUS_EV_CAPABILITY_DENIED, ARGUS_OUTCOME_DENIED, AIENOS_CAP_ERR_SUBJECT, 1, 3);
743 e.principal = 8;
744 expect_none(14, &e, "CAPABILITY_DENIED for another principal");
745 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 1, 4);
746 e.principal = 8;
747 e.object_id = AIENOS_CAP_RIGHT_READ;
748 expect_none(14, &e, "re-grant to a new subject");
749 CHECK(step(&e) == 0, "re-grant step");
750 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 4);
751 e.principal = 8;
752 expect_none(14, &e, "new subject uses its grant");
753 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 99, 1);
754 e.principal = 8;
755 run(&e);
756 CHECK(NF == 1 && F[0].code == 1, "unseen slot is detector 1's only (%zu)", NF);
757 neg[14]++;
758}
759
760static ArgusEvent trust_ev(uint16_t kind, const uint8_t *id, uint32_t trust)
761{
762 ArgusEvent e = mk(kind, ARGUS_OUTCOME_OK, 0);
763 memcpy(e.machine_id, id, 32);
764 e.object_id = trust;
765 return e;
766}
767
768static void t_trust(void)
769{
770 ArgusEvent e;
771 ArgusMachineShadow ms;
772 reset();
773 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, ARGUS_TRUST_TRUSTED);
774 expect_only(15, &e, "QUARANTINED -> TRUSTED (G-8)");
775 CHECK(F[0].prior_sequence == 40 && F[0].sync_allowed == 0 &&
776 F[0].containment == ARGUS_CONTAIN_REQUIRE_REATTESTATION, "d15 fields");
777 CHECK(step(&e) == 1, "escalation step");
778 CHECK(OPS->machine(V, MQ, &ms) == ARGUS_OK && ms.trust == ARGUS_TRUST_QUARANTINED, "escalation ignored by apply");
779 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, ARGUS_TRUST_REATTESTATION_REQUIRED);
780 expect_only(15, &e, "QUARANTINED -> REATTESTATION_REQUIRED (rank, not enum order)");
781 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MU, ARGUS_TRUST_TRUSTED);
782 expect_only(15, &e, "new machine declares itself TRUSTED (G-10)");
783 CHECK(F[0].prior_sequence == 0, "d15 new machine prior");
784 CHECK(step(&e) == 1, "declared join step");
785 CHECK(OPS->machine(V, MU, &ms) == ARGUS_OK && ms.trust == ARGUS_TRUST_OBSERVED && ms.joined_sequence != 0,
786 "JOINED capped at OBSERVED");
787 e = mk(ARGUS_EV_MACHINE_REMOVED, ARGUS_OUTCOME_OK, 0);
788 memcpy(e.machine_id, MQ, 32);
789 CHECK(step(&e) == 0, "remove quarantined machine");
790 CHECK(OPS->machine(V, MQ, &ms) == ARGUS_OK && ms.joined_sequence == 0 && ms.trust == ARGUS_TRUST_QUARANTINED,
791 "tombstone keeps quarantine");
792 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MQ, ARGUS_TRUST_OBSERVED);
793 expect_only(15, &e, "re-join over a quarantined tombstone claims OBSERVED (G-8)");
794 CHECK(F[0].prior_sequence == 40, "d15 tombstone prior");
795
796 reset();
797 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, ARGUS_TRUST_UNTRUSTED);
798 expect_none(15, &e, "downward move");
799 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, ARGUS_TRUST_QUARANTINED);
800 expect_none(15, &e, "same trust");
801 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, 0);
802 expect_none(15, &e, "UNKNOWN value ignored");
803 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, 7);
804 expect_none(15, &e, "out-of-range value ignored");
805 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MA, ARGUS_TRUST_OBSERVED);
806 expect_none(15, &e, "TRUSTED -> OBSERVED");
807 e = trust_ev(ARGUS_EV_MACHINE_TRUST_CHANGED, MQ, ARGUS_TRUST_TRUSTED);
808 e.outcome = ARGUS_OUTCOME_DENIED;
809 expect_none(15, &e, "upward move refused");
810 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MU, 0);
811 expect_none(15, &e, "join with no claim");
812 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MU, ARGUS_TRUST_OBSERVED);
813 expect_none(15, &e, "join claiming OBSERVED");
814 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MU, ARGUS_TRUST_RESTRICTED);
815 expect_none(15, &e, "join claiming RESTRICTED");
816 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MU, ARGUS_TRUST_TRUSTED);
817 e.outcome = ARGUS_OUTCOME_DENIED;
818 expect_none(15, &e, "refused join claiming TRUSTED");
819 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MA, ARGUS_TRUST_TRUSTED);
820 run(&e);
821 CHECK(NF == 1 && F[0].code == 7, "re-join of a joined machine is 7 only (%zu)", NF);
822 neg[15]++;
823 e = mk(ARGUS_EV_MACHINE_REMOVED, ARGUS_OUTCOME_OK, 0);
824 memcpy(e.machine_id, MQ, 32);
825 CHECK(step(&e) == 0, "remove quarantined machine");
826 e = trust_ev(ARGUS_EV_MACHINE_JOINED, MQ, 0);
827 expect_none(15, &e, "re-join over tombstone without a claim");
828 CHECK(step(&e) == 0, "re-join step");
829 CHECK(OPS->machine(V, MQ, &ms) == ARGUS_OK && ms.trust == ARGUS_TRUST_QUARANTINED && ms.joined_sequence != 0,
830 "re-join does not launder the quarantine");
831}
832
833/* Stub apply follows the argus_abi.h apply rules (so stub-clean == core-clean). */
834static void t_apply(void)
835{
836 ArgusEvent e;
837 ArgusCapShadow cs;
838 ArgusLeaseShadow ls;
839 ArgusWorldShadow ws;
840 ArgusProviderShadow ps;
841 reset();
842 e = use(ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0, 2, 1); /* replay of the revoked grant */
843 e.object_id = AIENOS_CAP_RIGHT_READ;
844 stub_state_apply(&st, &e);
845 CHECK(OPS->cap(V, 2, &cs) == ARGUS_OK && cs.state == ARGUS_SHADOW_REVOKED, "GRANTED at shadow gen not applied");
846 e = use(ARGUS_EV_CAPABILITY_REVOKED, ARGUS_OUTCOME_OK, 0, 77, UINT64_MAX);
847 stub_state_apply(&st, &e);
848 CHECK(OPS->cap(V, 77, &cs) != ARGUS_OK, "REVOKED of an unseen slot creates nothing");
849 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 1, 9, UINT64_MAX);
850 stub_state_apply(&st, &e);
851 CHECK(OPS->lease(V, 1, &ls) == ARGUS_OK && ls.subject == 7 && ls.scope == 0xF0, "LIVE lease not rewritten");
852 e = lease_ev(ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0, 2, 7, 1);
853 stub_state_apply(&st, &e);
854 CHECK(OPS->lease(V, 2, &ls) == ARGUS_OK && ls.state == ARGUS_SHADOW_REVOKED, "REVOKED lease not revived");
855 e = world_ev(ARGUS_OUTCOME_OK, 7, WX);
856 stub_state_apply(&st, &e);
857 CHECK(OPS->world(V, 0, &ws) == ARGUS_OK && ws.generation == 5, "skipping commit not adopted");
858 e = world_ev(ARGUS_OUTCOME_OK, 6, WX);
859 stub_state_apply(&st, &e);
860 CHECK(OPS->world(V, 0, &ws) == ARGUS_OK && ws.generation == 6, "+1 commit adopted");
861 e = mk(ARGUS_EV_PROVIDER_DISCOVERED, ARGUS_OUTCOME_OK, 0);
862 memcpy(e.evidence_digest, PQ, 32);
863 stub_state_apply(&st, &e);
864 CHECK(OPS->provider(V, PQ, &ps) == ARGUS_OK && ps.state == ARGUS_SHADOW_REVOKED, "rediscovery keeps quarantine");
865 CHECK(stub_min_class(ARGUS_EV_CAPABILITY_REVOKED) == ARGUS_CLASS_CRITICAL &&
866 stub_min_class(ARGUS_EV_WORLD_COMMITTED) == ARGUS_CLASS_SECURITY &&
867 stub_min_class(ARGUS_EV_PROVIDER_USED) == ARGUS_CLASS_AUDIT &&
868 stub_min_class(ARGUS_EV_POLICY_CHANGED) == ARGUS_CLASS_CRITICAL, "min class table spot checks");
869}
870
871/* ---- table, mirrors, runner ------------------------------------------------ */
872
873static void t_table(void)
874{
875 static const uint16_t ids[ARGUS_HARD_DETECTOR_COUNT] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15 };
876 static const uint8_t sync[ARGUS_HARD_DETECTOR_COUNT] = { 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
877 static const uint8_t contain[ARGUS_HARD_DETECTOR_COUNT] = {
878 ARGUS_CONTAIN_FREEZE_PRINCIPAL, ARGUS_CONTAIN_REVOKE_CAPABILITY, ARGUS_CONTAIN_FREEZE_PRINCIPAL,
879 ARGUS_CONTAIN_REJECT_ARTIFACT, ARGUS_CONTAIN_REJECT_ARTIFACT, ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE,
880 ARGUS_CONTAIN_REQUIRE_REATTESTATION, ARGUS_CONTAIN_PAUSE_EXTERNAL_EFFECTS, ARGUS_CONTAIN_RAISE_EFFECT_CLASS,
881 ARGUS_CONTAIN_QUARANTINE_PROVIDER, ARGUS_CONTAIN_FREEZE_PRINCIPAL, ARGUS_CONTAIN_REQUIRE_REATTESTATION,
882 };
883 CHECK(argus_hard_detector_count == ARGUS_HARD_DETECTOR_COUNT && argus_hard_detector_count == 12,
884 "detector count %zu", argus_hard_detector_count);
885 for (size_t i = 0; i < argus_hard_detector_count; i++) {
886 const ArgusDetector *d = &argus_hard_detectors[i];
887 CHECK(d->id == ids[i], "table order at %zu (id %u)", i, d->id);
888 CHECK(d->id != ARGUS_F_TELEMETRY_LOSS && d->id != ARGUS_F_SEQUENCE_ANOMALY &&
889 d->id != ARGUS_F_AUTHORITY_REPLAY && d->id != ARGUS_F_MALFORMED_EVENT, "core-only code %u in table", d->id);
890 CHECK(d->sync_allowed == sync[i], "sync_allowed of %u", d->id);
891 CHECK(d->containment == contain[i], "containment of %u", d->id);
892 CHECK(d->name != NULL && d->fn != NULL, "name/fn of %u", d->id);
893 }
894 CHECK(argus_trust_rank(ARGUS_TRUST_TRUSTED) < argus_trust_rank(ARGUS_TRUST_OBSERVED) &&
895 argus_trust_rank(ARGUS_TRUST_OBSERVED) < argus_trust_rank(ARGUS_TRUST_REATTESTATION_REQUIRED) &&
896 argus_trust_rank(ARGUS_TRUST_REATTESTATION_REQUIRED) < argus_trust_rank(ARGUS_TRUST_RESTRICTED) &&
897 argus_trust_rank(ARGUS_TRUST_RESTRICTED) < argus_trust_rank(ARGUS_TRUST_QUARANTINED) &&
898 argus_trust_rank(ARGUS_TRUST_QUARANTINED) < argus_trust_rank(ARGUS_TRUST_UNTRUSTED) &&
899 argus_trust_rank(ARGUS_TRUST_UNKNOWN) < 0 && argus_trust_rank(7) < 0, "trust rank order (header)");
900 CHECK(ARGUS_AUTH_RIGHT_WRITE == AIENOS_CAP_RIGHT_WRITE, "mirror RIGHT_WRITE");
901 CHECK(ARGUS_AUTH_OK == AIENOS_CAP_OK, "mirror OK");
902 CHECK(ARGUS_AUTH_ERR_BOUNDS == AIENOS_CAP_ERR_BOUNDS, "mirror BOUNDS");
903 CHECK(ARGUS_AUTH_ERR_STALE_GEN == AIENOS_CAP_ERR_STALE_GEN, "mirror STALE_GEN");
904 CHECK(ARGUS_AUTH_ERR_REVOKED == AIENOS_CAP_ERR_REVOKED, "mirror REVOKED");
905 CHECK(ARGUS_AUTH_ERR_CHAIN == AIENOS_CAP_ERR_CHAIN, "mirror CHAIN");
906 CHECK(ARGUS_AUTH_RIGHT_EFFECT == AIENOS_CAP_RIGHT_EFFECT, "mirror RIGHT_EFFECT");
907 CHECK(sizeof(ArgusCapRef) == 16 && offsetof(ArgusCapRef, generation) == 8, "cap ref layout");
908}
909
910static void t_runner(void)
911{
912 ArgusEvent e;
913 size_t n = 99;
914 reset();
915 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 1, 3);
916 CHECK(argus_detect_run(OPS, V, &e, F, 4, NULL) == ARGUS_ERR_ARG, "null n_out");
917 CHECK(argus_detect_run(NULL, V, &e, F, 4, &n) == ARGUS_ERR_ARG && n == 0, "null ops");
918 CHECK(argus_detect_run(OPS, NULL, &e, F, 4, &n) == ARGUS_ERR_ARG, "null view");
919 CHECK(argus_detect_run(OPS, V, NULL, F, 4, &n) == ARGUS_ERR_ARG, "null event");
920 CHECK(argus_detect_run(OPS, V, &e, NULL, 4, &n) == ARGUS_ERR_ARG, "null out with cap");
921 ArgusStateOps holed = *OPS;
922 holed.lease = NULL;
923 CHECK(argus_detect_run(&holed, V, &e, F, 4, &n) == ARGUS_ERR_ARG, "missing op");
924 holed = *OPS;
925 holed.policy_digest = NULL;
926 CHECK(argus_detect_run(&holed, V, &e, F, 4, &n) == ARGUS_ERR_ARG, "missing policy_digest op");
927 holed = *OPS;
928 holed.runtime_digest = NULL;
929 CHECK(argus_detect_run(&holed, V, &e, F, 4, &n) == ARGUS_ERR_ARG, "missing runtime_digest op");
930 CHECK(argus_detect_run(OPS, V, &e, NULL, 0, &n) == ARGUS_OK && n == 0, "clean event, no room needed");
931
932 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 99, 1);
933 CHECK(argus_detect_run(OPS, V, &e, NULL, 0, &n) == ARGUS_ERR_OVERFLOW && n == 0, "overflow at cap 0");
934 /* quarantined provider used from a quarantined machine with an unseen ref:
935 * findings 1, 10, 10 */
936 e = use(ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0, 99, 1);
937 memcpy(e.evidence_digest, PQ, 32);
938 memcpy(e.machine_id, MQ, 32);
939 CHECK(argus_detect_run(OPS, V, &e, F, 32, &n) == ARGUS_OK && n == 3 && F[0].code == 1 && F[1].code == 10,
940 "three findings in id order (%zu)", n);
941 CHECK(argus_detect_run(OPS, V, &e, F, 2, &n) == ARGUS_ERR_OVERFLOW && n == 2, "truncated at 2");
942 CHECK(argus_detect_run(OPS, V, &e, F, 1, &n) == ARGUS_ERR_OVERFLOW && n == 1 && F[0].code == 1, "truncated at 1");
943}
944
945/* ---- v1.1: cap 0, CAP_NONE, CAPABILITY_USE_SUMMARY, per-store World -------- */
946
947static ArgusEvent summ(uint32_t cap_id, uint64_t max_gen, uint64_t min_gen, uint32_t principal)
948{
949 ArgusEvent e = use(ARGUS_EV_CAPABILITY_USE_SUMMARY, ARGUS_OUTCOME_OK, 0, cap_id, max_gen);
950 e.class_ = ARGUS_CLASS_AUDIT;
951 e.tick = 0;
952 e.world_generation = min_gen; /* v1.1: MIN in world_generation (64-bit), object_id 0 */
953 e.resource = 4096;
954 e.principal = principal;
955 return e;
956}
957
958static void expect_codes(const ArgusEvent *e, uint16_t a, uint16_t b, const char *what)
959{
960 int rc = run(e);
961 size_t want = (a ? 1u : 0u) + (b ? 1u : 0u);
962 CHECK(rc == ARGUS_OK && NF == want, "[%s] expected %zu findings, got %zu (first %u)", what, want, NF, NF ? F[0].code : 0);
963 if (a && NF >= 1) CHECK(F[0].code == a, "[%s] first code %u want %u", what, F[0].code, a);
964 if (b && NF >= 2) CHECK(F[1].code == b, "[%s] second code %u want %u", what, F[1].code, b);
965}
966
967static void t_v11_cap0(void)
968{
969 ArgusEvent e;
970 reset();
971 ArgusCapShadow c = {0};
972 c.cap_id = 0; c.generation = 2; c.state = ARGUS_SHADOW_LIVE; c.subject = 7;
973 c.rights = AIENOS_CAP_RIGHT_READ; c.granted_sequence = 9;
974 stub_put_cap(&st, &c);
975 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 2);
976 expect_codes(&e, 0, 0, "cap 0 used by its subject");
977 neg[1]++; neg[14]++;
978 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 2); e.principal = 8;
979 expect_only(14, &e, "cap 0 used by the wrong subject");
980 CHECK(F[0].cap_id == 0 && F[0].prior_sequence == 9, "d14 on cap 0 fields");
981 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 3);
982 expect_only(1, &e, "cap 0 above its generation");
983 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 1);
984 expect_only(2, &e, "cap 0 stale generation");
985 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 2); e.effect_class = ARGUS_EFFECT_EVIDENCE;
986 expect_only(8, &e, "cap 0 EVIDENCE without WRITE");
987 e = use(ARGUS_EV_CAPABILITY_REVOKED, ARGUS_OUTCOME_OK, 0, 0, 2);
988 CHECK(step(&e) == 0, "revoke cap 0");
989 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 2);
990 expect_only(3, &e, "revoked cap 0 accepted");
991 /* cap 0 unseen: a use is a forgery like any other slot */
992 reset();
993 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 0, 1);
994 expect_only(1, &e, "unseen cap 0 used");
995 /* CAP_NONE: not a capability use (1, 2, 3, 14 silent) */
996 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, ARGUS_CAP_NONE, 5);
997 expect_none(1, &e, "CAP_NONE use");
998 e = summ(ARGUS_CAP_NONE, 5, 1, 99);
999 expect_none(14, &e, "CAP_NONE summary");
1000 /* USED above a REVOKED generation: detector 1 only (v1.1 precision rule) */
1001 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0, 2, 2);
1002 expect_only(1, &e, "use above the revoked generation");
1003 e = use(ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, AIENOS_CAP_ERR_REVOKED, 2, 2);
1004 expect_codes(&e, 1, 3, "above revoked + ERR_REVOKED contradiction still 3");
1005}
1006
1007static void t_v11_summary(void)
1008{
1009 ArgusEvent e;
1010 reset();
1011 /* cap 1 LIVE gen 3 subject 7; cap 2 REVOKED gen 1; cap 3 LIVE gen 1 rights R */
1012 e = summ(1, 3, 3, 7);
1013 expect_codes(&e, 0, 0, "consistent summary");
1014 neg[1]++; neg[2]++; neg[3]++; neg[14]++;
1015 e = summ(3, 1, 1, 7); e.effect_class = ARGUS_EFFECT_NONE;
1016 expect_none(8, &e, "summary with no effect class");
1017 e = summ(1, 4, 3, 7);
1018 expect_only(1, &e, "summary max above shadow");
1019 CHECK(F[0].cap_generation == 4 && F[0].prior_sequence == 10, "d1 summary fields");
1020 e = summ(1, 3, 2, 7);
1021 expect_only(2, &e, "summary min stale");
1022 CHECK(F[0].prior_sequence == 10 && F[0].containment == ARGUS_CONTAIN_REVOKE_CAPABILITY, "d2 summary fields");
1023 e = summ(1, 4, 2, 7);
1024 expect_codes(&e, 1, 2, "summary above and below");
1025 e = summ(2, 1, 1, 7);
1026 expect_only(3, &e, "summary max hits revoked slot");
1027 CHECK(F[0].prior_sequence == 20, "d3 summary prior = revocation");
1028 e = summ(2, 2, 2, 7);
1029 expect_only(1, &e, "summary above revoked generation: 1, not 3");
1030 e = summ(2, 1, 0, 7);
1031 expect_codes(&e, 2, 3, "summary on revoked slot spanning an older generation");
1032 e = summ(1, 3, 3, 8);
1033 expect_only(14, &e, "summary by the wrong subject");
1034 e = summ(150, 1, 1, 7);
1035 expect_only(1, &e, "summary of a slot never granted");
1036 e = summ(3, 1, 1, 7); e.effect_class = ARGUS_EFFECT_EXTERNAL;
1037 expect_only(8, &e, "summary labelled EXTERNAL without EFFECT right");
1038 e = summ(1, 3, 3, 7); e.outcome = ARGUS_OUTCOME_DENIED;
1039 expect_none(1, &e, "non-OK summary (malformed for validate) is not a use");
1040 e = summ(1, 3, 3, 7); memcpy(e.machine_id, MQ, 32);
1041 expect_only(10, &e, "summary from a quarantined machine");
1042 e = summ(1, 3, 3, 7); memcpy(e.machine_id, MU, 32);
1043 expect_only(7, &e, "summary from an unknown machine");
1044 /* stub apply: a summary changes nothing */
1045 StubState before = st;
1046 e = summ(1, 3, 3, 7);
1047 CHECK(stub_state_apply(&st, &e) == ARGUS_OK && memcmp(&before, &st, sizeof st) == 0, "summary applies nothing");
1048}
1049
1050static void t_v11_world_stores(void)
1051{
1052 ArgusEvent e;
1053 reset(); /* store 0 at gen 5 */
1054 e = world_ev(ARGUS_OUTCOME_OK, 1, WX); e.object_id = 3;
1055 expect_none(9, &e, "first commit of another store");
1056 CHECK(step(&e) == 0, "store 3 first commit");
1057 e = world_ev(ARGUS_OUTCOME_OK, 6, WD); e.object_id = 0;
1058 expect_none(9, &e, "store 0 +1 while store 3 exists");
1059 CHECK(step(&e) == 0, "store 0 advances");
1060 e = world_ev(ARGUS_OUTCOME_OK, 2, WD); e.object_id = 3;
1061 expect_none(9, &e, "store 3 +1 interleaved");
1062 CHECK(step(&e) == 0, "store 3 advances");
1063 e = world_ev(ARGUS_OUTCOME_OK, 3, WX); e.object_id = 0;
1064 expect_only(9, &e, "store 0 rollback (store 3 is at 2, irrelevant)");
1065 e = world_ev(ARGUS_OUTCOME_OK, 7, WX); e.object_id = 3;
1066 expect_only(9, &e, "store 3 skip");
1067 ArgusWorldShadow w;
1068 CHECK(OPS->world(V, 0, &w) == ARGUS_OK && w.generation == 6 && w.store_id == 0, "store 0 shadow");
1069 CHECK(OPS->world(V, 3, &w) == ARGUS_OK && w.generation == 2 && w.store_id == 3, "store 3 shadow");
1070 CHECK(OPS->world(V, 4, &w) == ARGUS_ERR_STATE, "unknown store");
1071 /* stub table: ARGUS_WORLD_STORES stores, then FULL */
1072 for (uint32_t s = 10; s < 10 + ARGUS_WORLD_STORES - 2; s++)
1073 CHECK(stub_set_world(&st, s, 1, WD, 100 + s) == ARGUS_OK, "store %u fits", s);
1074 CHECK(stub_set_world(&st, 99, 1, WD, 200) == ARGUS_ERR_FULL, "9th store is FULL");
1075}
1076
1077/* ---- corpora ---------------------------------------------------------------- */
1078
1079#define CORPUS_N 5000
1080
1081static ArgusEvent corpus[CORPUS_N];
1082static ArgusEvent corpus2[CORPUS_N];
1083
1084static size_t replay(const ArgusEvent *evs, size_t n, ArgusFinding *all, size_t all_max, size_t *codes)
1085{
1086 size_t total = 0;
1087 stub_state_init(&st);
1088 V = stub_state_view(&st);
1089 for (size_t i = 0; i < n; i++) {
1090 size_t k = 0;
1091 StubState before = st;
1092 int rc = argus_detect_run(OPS, V, &evs[i], F, 32, &k);
1093 CHECK(rc == ARGUS_OK, "replay rc %d at %zu", rc, i);
1094 CHECK(memcmp(&before, &st, sizeof st) == 0, "detectors modified state at %zu", i);
1095 for (size_t j = 0; j < k; j++) {
1096 if (codes && F[j].code <= ARGUS_F_MAX) codes[F[j].code]++;
1097 if (all && total < all_max) all[total] = F[j];
1098 total++;
1099 }
1100 CHECK(stub_state_apply(&st, &evs[i]) == ARGUS_OK, "apply at %zu", i);
1101 }
1102 return total;
1103}
1104
1105/* Stream obeys the argus_abi.h validate rules and the corpus conventions:
1106 * class never weaker than the kind minimum, no CONSUMER flag, cap_id <
1107 * ARGUS_CAP_MAX, EXTERNAL_EFFECT_* labelled EXTERNAL, every event attributed
1108 * to a machine, and (benign only) every LEASE_CREATED on a fresh id. */
1109static void check_stream_shape(const ArgusEvent *evs, size_t n, int benign)
1110{
1111 static uint8_t lease_seen[1u << 16];
1112 memset(lease_seen, 0, sizeof lease_seen);
1113 static const uint8_t zero[ARGUS_MACHINE_ID_LEN];
1114 size_t bad = 0, lease_reuse = 0, lease_ids = 0, n_summary = 0, n_cap0 = 0, n_store[2] = {0, 0}, n_mach = 0;
1115 static uint8_t mach[16][32];
1116 uint32_t streams[16] = {0};
1117 for (size_t i = 0; i < n; i++) {
1118 const ArgusEvent *e = &evs[i];
1119 int ok = e->version == ARGUS_ABI_VERSION && e->sequence == i + 1 && e->kind != 0 &&
1120 e->outcome >= 1 && e->outcome <= ARGUS_OUTCOME_MAX &&
1121 (e->flags & (uint16_t)~ARGUS_FLAG_STREAM_MASK) == ARGUS_FLAG_SYNTHETIC &&
1122 e->class_ >= 1 && e->class_ <= stub_min_class(e->kind) &&
1123 (e->cap_id < ARGUS_CAP_MAX || e->cap_id == ARGUS_CAP_NONE) &&
1124 memcmp(e->machine_id, zero, sizeof zero) != 0;
1125 if ((e->kind == ARGUS_EV_EXTERNAL_EFFECT_REQUESTED || e->kind == ARGUS_EV_EXTERNAL_EFFECT_DENIED ||
1126 e->kind == ARGUS_EV_EXTERNAL_EFFECT_COMMITTED) && e->effect_class != ARGUS_EFFECT_EXTERNAL)
1127 ok = 0;
1128 /* v1.1: summaries are outcome OK with tick 0 and min <= max; USED carries tick 0 */
1129 if (e->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY &&
1130 (e->outcome != ARGUS_OUTCOME_OK || e->tick != 0 || e->resource == 0 || e->world_generation > e->cap_generation || e->object_id != 0))
1131 ok = 0;
1132 if (e->kind == ARGUS_EV_CAPABILITY_USED && e->tick != 0)
1133 ok = 0;
1134 if (e->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY) n_summary++;
1135 if (e->cap_id == 0 && e->kind == ARGUS_EV_CAPABILITY_GRANTED) n_cap0++;
1136 if (e->kind == ARGUS_EV_WORLD_COMMITTED) { if (e->object_id == 0) n_store[0]++; else if (e->object_id == 5) n_store[1]++; }
1137 for (size_t m = 0; m < n_mach; m++)
1138 if (memcmp(mach[m], e->machine_id, 32) == 0) { streams[m] |= 1u << (ARGUS_STREAM_OF(e->flags) & 31u); goto seen; }
1139 if (n_mach < 16) { memcpy(mach[n_mach], e->machine_id, 32); streams[n_mach++] = 1u << (ARGUS_STREAM_OF(e->flags) & 31u); }
1140 seen:;
1141 if (!ok && bad++ < 5)
1142 CHECK(0, "stream shape at %zu (kind %u class %u)", i, e->kind, e->class_);
1143 if (e->kind == ARGUS_EV_CREDENTIAL_LEASE_CREATED && e->outcome == ARGUS_OUTCOME_OK) {
1144 CHECK(e->object_id != 0 && e->object_id < (1u << 16), "lease id range at %zu", i);
1145 if (e->object_id < (1u << 16)) {
1146 if (lease_seen[e->object_id]) lease_reuse++;
1147 else lease_ids++;
1148 lease_seen[e->object_id] = 1;
1149 }
1150 }
1151 }
1152 CHECK(bad == 0, "%zu events break the stream shape", bad);
1153 CHECK(n_summary > 0 && n_cap0 > 0 && n_store[0] > 1 && n_store[1] > 1,
1154 "v1.1 content: %zu summaries, %zu cap-0 grants, stores %zu/%zu", n_summary, n_cap0, n_store[0], n_store[1]);
1155 size_t multi = 0;
1156 for (size_t m = 0; m < n_mach; m++) multi += __builtin_popcount(streams[m]) >= 3;
1157 CHECK(multi >= 3, "only %zu machines use >= 3 streams", multi);
1158 CHECK(lease_ids <= 56, "distinct lease ids %zu exceed the core's table margin", lease_ids);
1159 if (benign)
1160 CHECK(lease_reuse == 0, "benign stream re-uses %zu lease ids (ruling A)", lease_reuse);
1161}
1162
1163static size_t benign_findings_seed1;
1164
1165static void t_benign(void)
1166{
1167 size_t kinds[ARGUS_EV_KIND_MAX + 1] = {0};
1168 size_t outcomes[4] = {0};
1169 size_t n = argus_corpus_benign(corpus, CORPUS_N, 1);
1170 CHECK(n == CORPUS_N, "benign size %zu", n);
1171 check_stream_shape(corpus, n, 1);
1172 for (size_t i = 0; i < n; i++) { kinds[corpus[i].kind]++; outcomes[corpus[i].outcome]++; }
1173 size_t codes[ARGUS_F_MAX + 1] = {0};
1174 benign_findings_seed1 = replay(corpus, n, NULL, 0, codes);
1175 CHECK(benign_findings_seed1 == 0, "benign corpus seed 1: %zu findings", benign_findings_seed1);
1176 for (int c = 1; c <= ARGUS_F_MAX; c++)
1177 if (codes[c]) fprintf(stderr, " benign FP code %d: %zu\n", c, codes[c]);
1178 printf("benign corpus seed 1: %zu events, %zu findings; OK %zu DENIED %zu; kinds:", n, benign_findings_seed1,
1179 outcomes[ARGUS_OUTCOME_OK], outcomes[ARGUS_OUTCOME_DENIED]);
1180 size_t distinct = 0;
1181 for (int k = 0; k <= ARGUS_EV_KIND_MAX; k++)
1182 if (kinds[k]) { printf(" %d:%zu", k, kinds[k]); distinct++; }
1183 printf(" (%zu distinct)\n", distinct);
1184 for (uint64_t seed = 1; seed <= 9; seed++) {
1185 n = argus_corpus_benign(corpus, CORPUS_N, seed);
1186 check_stream_shape(corpus, n, 1);
1187 size_t f = replay(corpus, n, NULL, 0, NULL);
1188 CHECK(f == 0, "benign corpus seed %llu: %zu findings", (unsigned long long)seed, f);
1189 /* the same day from a live producer (no SYNTHETIC flag): the zero
1190 * machine_id rule must not fire, every event is attributed */
1191 for (size_t i = 0; i < n; i++) corpus[i].flags &= (uint16_t)~ARGUS_FLAG_SYNTHETIC; /* keep stream ids */
1192 size_t fl = replay(corpus, n, NULL, 0, NULL);
1193 CHECK(fl == 0, "benign corpus seed %llu as live producer: %zu findings", (unsigned long long)seed, fl);
1194 printf("benign seed %llu: %zu events, %zu findings (synthetic), %zu findings (live flags)\n",
1195 (unsigned long long)seed, n, f, fl);
1196 }
1197 /* determinism of the generator */
1198 argus_corpus_benign(corpus, CORPUS_N, 7);
1199 argus_corpus_benign(corpus2, CORPUS_N, 7);
1200 CHECK(memcmp(corpus, corpus2, sizeof corpus) == 0, "benign generator deterministic");
1201 argus_corpus_benign(corpus2, CORPUS_N, 8);
1202 CHECK(memcmp(corpus, corpus2, sizeof corpus) != 0, "seed changes the stream");
1203}
1204
1205static ArgusFinding hf1[512], hf2[512];
1206
1207static void t_hostile(void)
1208{
1209 static ArgusExpectedFinding ex[512];
1210 size_t nex = 0;
1211 size_t n = argus_corpus_hostile(corpus, CORPUS_N, 1, ex, 512, &nex);
1212 CHECK(n == CORPUS_N, "hostile size");
1213 check_stream_shape(corpus, n, 0);
1214 size_t exp_codes[ARGUS_F_MAX + 1] = {0}, got_codes[ARGUS_F_MAX + 1] = {0};
1215 for (size_t i = 0; i < nex; i++) exp_codes[ex[i].code]++;
1216 /* v1.1 injections present: summaries hitting codes 1, 2, 3 and cap 0 used by the wrong subject */
1217 size_t sum_code[ARGUS_F_MAX + 1] = {0}, cap0_14 = 0;
1218 for (size_t i = 0; i < nex && i < 512; i++) {
1219 const ArgusEvent *ie = &corpus[ex[i].sequence - 1];
1220 if (ie->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY) sum_code[ex[i].code]++;
1221 if (ie->cap_id == 0 && ex[i].code == ARGUS_F_SUBJECT_MISMATCH) cap0_14++;
1222 }
1223 CHECK(sum_code[1] > 0 && sum_code[2] > 0 && sum_code[3] > 0 && cap0_14 > 0,
1224 "v1.1 hostile injections: summary codes 1:%zu 2:%zu 3:%zu, cap-0 code 14: %zu", sum_code[1], sum_code[2], sum_code[3], cap0_14);
1225 printf("hostile v1.1 injections: summaries -> code 1:%zu 2:%zu 3:%zu; cap 0 wrong subject -> 14:%zu\n",
1226 sum_code[1], sum_code[2], sum_code[3], cap0_14);
1227 size_t nf = replay(corpus, n, hf1, 512, got_codes);
1228 CHECK(nf == nex, "hostile: expected %zu findings, detected %zu", nex, nf);
1229 size_t match = 0;
1230 for (size_t i = 0; i < nf && i < nex && i < 512; i++) {
1231 if (hf1[i].sequence == ex[i].sequence && hf1[i].code == ex[i].code) match++;
1232 else CHECK(0, "hostile mismatch at %zu: got (%llu,%u) want (%llu,%u)", i,
1233 (unsigned long long)hf1[i].sequence, hf1[i].code, (unsigned long long)ex[i].sequence, ex[i].code);
1234 }
1235 printf("hostile corpus seed 1: %zu events, expected %zu, detected %zu, exact matches %zu; per code:", n, nex, nf, match);
1236 for (size_t k = 0; k < argus_hard_detector_count; k++) {
1237 uint16_t c = argus_hard_detectors[k].id;
1238 printf(" %u:%zu/%zu", c, got_codes[c], exp_codes[c]);
1239 CHECK(exp_codes[c] > 0, "hostile corpus injects code %u", c);
1240 CHECK(got_codes[c] == exp_codes[c], "hostile code %u: detected %zu expected %zu", c, got_codes[c], exp_codes[c]);
1241 }
1242 printf("\n");
1243 /* determinism: same stream twice -> byte-identical findings */
1244 size_t nex2 = 0;
1245 argus_corpus_hostile(corpus2, CORPUS_N, 1, ex, 512, &nex2);
1246 CHECK(memcmp(corpus, corpus2, sizeof corpus) == 0 && nex2 == nex, "hostile generator deterministic");
1247 size_t nf2 = replay(corpus2, n, hf2, 512, NULL);
1248 CHECK(nf2 == nf && memcmp(hf1, hf2, sizeof(ArgusFinding) * (nf < 512 ? nf : 512)) == 0,
1249 "findings byte-identical across replays");
1250 for (uint64_t seed = 2; seed <= 5; seed++) {
1251 n = argus_corpus_hostile(corpus, CORPUS_N, seed, ex, 512, &nex);
1252 nf = replay(corpus, n, hf1, 512, NULL);
1253 int ok = nf == nex;
1254 for (size_t i = 0; ok && i < nf; i++)
1255 ok = hf1[i].sequence == ex[i].sequence && hf1[i].code == ex[i].code;
1256 CHECK(ok, "hostile seed %llu: expected %zu detected %zu", (unsigned long long)seed, nex, nf);
1257 }
1258}
1259
1260int main(void)
1261{
1262 t_table();
1263 t_runner();
1264 t_forged();
1265 t_stale();
1266 t_revoked();
1267 t_artifact();
1268 t_signature();
1269 t_lease();
1270 t_machine();
1271 t_effect();
1272 t_world();
1273 t_quarantine();
1274 t_subject();
1275 t_trust();
1276 t_apply();
1277 t_v11_cap0();
1278 t_v11_summary();
1279 t_v11_world_stores();
1280 t_benign();
1281 t_hostile();
1282
1283 printf("per-detector cases (positive/negative):\n");
1284 for (size_t k = 0; k < argus_hard_detector_count; k++) {
1285 uint16_t d = argus_hard_detectors[k].id;
1286 printf(" %2u %-30s %d/%d\n", d, argus_hard_detectors[k].name, pos[d], neg[d]);
1287 CHECK(pos[d] >= 1 && neg[d] >= 2, "coverage for detector %u", d);
1288 }
1289 printf("%d checks, %d failures\n", checks, failures);
1290 if (failures) {
1291 printf("ARGUS_DETECT_FAIL\n");
1292 return 1;
1293 }
1294 printf("ARGUS_DETECT_PASS ARGUS_FALSE_POSITIVE_BASELINE(synthetic)=%zu\n", benign_findings_seed1);
1295 return 0;
1296}