interplane.Get Omega
Read in your language

aienux

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

Brought by drakestapleton

branch main Branches Tags History

aienux / native / argus / argus_detect.c

669 lines · 34361 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * argus_detect.c -- ARGUS-0 hard-invariant detectors (lane E).
19 *
20 * Twelve deterministic detectors, one per stable finding code ARGUS_F_1..10,
21 * 14 and 15 (11 TELEMETRY_LOSS, 12 SEQUENCE_ANOMALY, 13 AUTHORITY_REPLAY and
22 * 16 MALFORMED_EVENT are raised by the core, which sees history and
23 * validation). Each detector sees the event and the shadow state BEFORE the
24 * core applies the event (argus_abi.h), reads the shadow only through
25 * ArgusStateOps, and is pure: no allocation, no I/O, no globals written, no
26 * clock, no calls into the authority. ARGUS never re-validates a capability:
27 * detectors compare the authority's decision (ev->outcome, ev->code, copied by
28 * the producer) with what ARGUS itself has already seen. A finding is
29 * evidence, never authority; `containment` is a recommendation to AEGIS, never
30 * an action.
31 *
32 * Shared conventions (argus_abi.h, agreed with lane D, the shadow-state core):
33 * - CAPABILITY_GRANTED: cap_id/cap_generation = the NEW reference,
34 * resource = authority resource, object_id = AIENOS_CAP_RIGHT_* mask,
35 * principal = authority subject, code = authority result code.
36 * - "Capability use" events: CAPABILITY_USED, CAPABILITY_USE_SUMMARY (v1.1),
37 * EXTERNAL_EFFECT_REQUESTED, EXTERNAL_EFFECT_COMMITTED, CREDENTIAL_LEASE_USED,
38 * PROVIDER_USED, when they carry cap_id != ARGUS_CAP_NONE (is_cap_use below;
39 * one definition for all). cap_id 0 is the authority OFFICE slot and is
40 * checked like any other (v1.1).
41 * - A USE_SUMMARY is a USED spanning generations: MAX in cap_generation, MIN in
42 * world_generation (64-bit; object_id unused = 0) (use_gen_max/use_gen_min).
43 * Detector 1 checks the max, detector 2 the min, detector 3 the max
44 * (<= the revoked generation), 8 and 14 as a USED.
45 * - A machine is "joined" when machine() finds it AND joined_sequence != 0.
46 * MACHINE_REMOVED leaves a tombstone (joined_sequence 0, trust kept).
47 * - Machine trust rank (argus_trust_rank): TRUSTED < OBSERVED <
48 * REATTESTATION_REQUIRED < RESTRICTED < QUARANTINED < UNTRUSTED.
49 * - PROVIDER_*: evidence_digest = provider id. Shadow state REVOKED means
50 * the provider is quarantined.
51 * - CREDENTIAL_LEASE_*: object_id = lease id, resource = scope (created) or
52 * requested resource bits (used), principal = subject. Lease ids are never
53 * reused (orchestrator ruling A).
54 * - ARTIFACT_*: evidence_digest = artifact digest.
55 * - world(store_id): ARGUS_ERR_STATE is treated as "no World committed yet" for
56 * that store (WORLD_COMMITTED object_id = store id, v1.1).
57 * - policy_digest()/runtime_digest(): an all-zero digest means "none stored".
58 *
59 * Self-reports (G-2): kinds 60-63 are a producer's say-so, not evidence. A
60 * finding derived only from one carries sync_allowed 0 and containment NONE.
61 * It keeps the detector's default sync/containment only when the shadow
62 * independently corroborates the same violation FOR THE SAME PRINCIPAL:
63 * 62 STALE_GENERATION slot seen, shadow subject == principal, and
64 * cap_generation < shadow generation;
65 * 63 FORGED_CAPABILITY slot seen, shadow subject == principal, and
66 * cap_generation > shadow generation (never minted);
67 * 60 INTEGRITY_VIOLATION, 61 SIGNATURE_FAILURE: nothing in the v1 shadow can
68 * corroborate them; always sync 0 / NONE.
69 * An unseen slot does not corroborate a forgery report: anyone can name an
70 * unseen slot together with any victim principal.
71 *
72 * Every finding: confidence DETERMINISTIC, detector == code, sequence = the
73 * triggering event, principal/cap_id/cap_generation/machine_id copied from
74 * the event, event_digest left zero (the core fills it).
75 *
76 * Freestanding: only argus_abi.h (<stdint.h>, <stddef.h>). No libc calls.
77 */
78#include "argus_detect.h"
79
80/* ---- helpers --------------------------------------------------------------- */
81
82static int bytes_equal(const uint8_t *a, const uint8_t *b, size_t n)
83{
84 uint8_t acc = 0;
85 for (size_t i = 0; i < n; i++)
86 acc |= (uint8_t)(a[i] ^ b[i]);
87 return acc == 0;
88}
89
90static int bytes_zero(const uint8_t *a, size_t n)
91{
92 uint8_t acc = 0;
93 for (size_t i = 0; i < n; i++)
94 acc |= a[i];
95 return acc == 0;
96}
97
98static int is_cap_use(const ArgusEvent *ev)
99{
100 if (ev->cap_id == ARGUS_CAP_NONE)
101 return 0;
102 switch (ev->kind) {
103 case ARGUS_EV_CAPABILITY_USED:
104 case ARGUS_EV_CAPABILITY_USE_SUMMARY:
105 case ARGUS_EV_EXTERNAL_EFFECT_REQUESTED:
106 case ARGUS_EV_EXTERNAL_EFFECT_COMMITTED:
107 case ARGUS_EV_CREDENTIAL_LEASE_USED:
108 case ARGUS_EV_PROVIDER_USED:
109 return 1;
110 default:
111 return 0;
112 }
113}
114
115static int outcome_ok(const ArgusEvent *ev)
116{
117 return ev->outcome == ARGUS_OUTCOME_OK;
118}
119
120/* Generation span of a use (v1.1). A USED carries one generation. A
121 * CAPABILITY_USE_SUMMARY carries the MAX generation seen in cap_generation and
122 * the MIN in world_generation (64-bit, header v1.1; object_id is unused = 0). */
123static uint64_t use_gen_max(const ArgusEvent *ev) { return ev->cap_generation; }
124static uint64_t use_gen_min(const ArgusEvent *ev)
125{
126 return ev->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY ? ev->world_generation : ev->cap_generation;
127}
128
129static int cap_lookup(const ArgusStateOps *ops, const ArgusStateView *v, uint32_t id, ArgusCapShadow *s)
130{
131 return ops->cap(v, id, s) == ARGUS_OK && s->state != ARGUS_SHADOW_UNSEEN;
132}
133
134static int machine_joined(const ArgusStateOps *ops, const ArgusStateView *v,
135 const uint8_t id[ARGUS_MACHINE_ID_LEN], ArgusMachineShadow *m)
136{
137 return ops->machine(v, id, m) == ARGUS_OK && m->joined_sequence != 0;
138}
139
140/* Trust rank of a shadow value; UNKNOWN (0) or garbage counts as OBSERVED. */
141static int shadow_rank(uint32_t trust)
142{
143 int r = argus_trust_rank(trust);
144 return r < 0 ? argus_trust_rank(ARGUS_TRUST_OBSERVED) : r;
145}
146
147/* Append one finding into out[*n]; ARGUS_ERR_OVERFLOW when there is no room. */
148static int emit(ArgusFinding *out, size_t cap, size_t *n, const ArgusEvent *ev,
149 uint16_t code, uint8_t severity, uint8_t sync_allowed, uint8_t containment,
150 uint64_t prior_sequence)
151{
152 if (*n >= cap)
153 return ARGUS_ERR_OVERFLOW;
154 ArgusFinding *f = &out[*n];
155 f->code = code;
156 f->severity = severity;
157 f->confidence = ARGUS_CONF_DETERMINISTIC;
158 f->sync_allowed = sync_allowed;
159 f->containment = containment;
160 f->detector = code;
161 f->sequence = ev->sequence;
162 f->prior_sequence = prior_sequence;
163 f->principal = ev->principal;
164 f->cap_id = ev->cap_id;
165 f->cap_generation = ev->cap_generation;
166 for (size_t i = 0; i < ARGUS_MACHINE_ID_LEN; i++)
167 f->machine_id[i] = ev->machine_id[i];
168 for (size_t i = 0; i < ARGUS_DIGEST_LEN; i++)
169 f->event_digest[i] = 0;
170 (*n)++;
171 return ARGUS_OK;
172}
173
174/* ---- 1 FORGED_CAPABILITY ---------------------------------------------------
175 * Trigger: (a) kind == FORGED_CAPABILITY, any outcome (self-report, see the
176 * G-2 rule at the top: sync 0 / NONE unless the shadow shows the
177 * reported principal holds the slot and the generation was never
178 * minted); or
179 * (b) a capability use with outcome OK whose slot ARGUS has never
180 * seen granted (shadow UNSEEN); or
181 * (c) a capability use with outcome OK at a generation ABOVE the
182 * shadow generation for a seen slot (summary: its MAX generation;
183 * a future generation was never minted, G-4); or
184 * (d) a capability use with outcome OK but code ERR_BOUNDS or
185 * ERR_CHAIN (the authority's own code contradicts the outcome).
186 * Evidence: ev kind/outcome/code/cap_id/cap_generation; cap shadow.
187 * NOT triggered by CAPABILITY_GRANTED: its cap_id is the new reference and is
188 * legitimately unseen (grant replays are the core's code 13). The
189 * event has no parent-reference field, so "granted from a forged
190 * parent" is not detectable in ABI v1.
191 * Code ARGUS_F_FORGED_CAPABILITY, CRITICAL, sync_allowed 1,
192 * containment FREEZE_PRINCIPAL. prior_sequence = the grant when the slot is seen.
193 */
194static int det_forged_capability(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
195 ArgusFinding *out, size_t cap, size_t *n_out)
196{
197 *n_out = 0;
198 ArgusCapShadow s = {0};
199 int seen = ev->cap_id != ARGUS_CAP_NONE && cap_lookup(ops, v, ev->cap_id, &s);
200 uint64_t prior = seen ? s.granted_sequence : 0;
201 if (ev->kind == ARGUS_EV_FORGED_CAPABILITY) {
202 int corroborated = seen && s.subject == ev->principal && ev->cap_generation > s.generation;
203 return emit(out, cap, n_out, ev, ARGUS_F_FORGED_CAPABILITY, ARGUS_SEV_CRITICAL,
204 corroborated ? 1 : 0,
205 corroborated ? ARGUS_CONTAIN_FREEZE_PRINCIPAL : ARGUS_CONTAIN_NONE, prior);
206 }
207 if (!is_cap_use(ev) || !outcome_ok(ev))
208 return ARGUS_OK;
209 int hit = ev->code == ARGUS_AUTH_ERR_BOUNDS || ev->code == ARGUS_AUTH_ERR_CHAIN || !seen ||
210 use_gen_max(ev) > s.generation;
211 if (!hit)
212 return ARGUS_OK;
213 return emit(out, cap, n_out, ev, ARGUS_F_FORGED_CAPABILITY, ARGUS_SEV_CRITICAL, 1,
214 ARGUS_CONTAIN_FREEZE_PRINCIPAL, prior);
215}
216
217/* ---- 2 STALE_GENERATION ----------------------------------------------------
218 * Trigger: (a) kind == STALE_GENERATION, any outcome (self-report: sync 0 /
219 * NONE unless the shadow shows the reported principal holds the slot
220 * at a newer generation). A normal authority refusal of an old
221 * reference is CAPABILITY_USED/DENIED code ERR_STALE_GEN, which does
222 * NOT trigger; or
223 * (b) a capability use with outcome OK and cap_generation (summary:
224 * its MIN generation, world_generation) lower than the shadow's current
225 * generation for that slot; or
226 * (c) a capability use with outcome OK but code ERR_STALE_GEN.
227 * Evidence: ev cap_id/cap_generation/outcome/code; cap shadow generation.
228 * Code ARGUS_F_STALE_GENERATION, HIGH, sync_allowed 1, containment
229 * REVOKE_CAPABILITY. prior_sequence = shadow granted_sequence when known.
230 */
231static int det_stale_generation(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
232 ArgusFinding *out, size_t cap, size_t *n_out)
233{
234 *n_out = 0;
235 ArgusCapShadow s = {0};
236 int seen = ev->cap_id != ARGUS_CAP_NONE && cap_lookup(ops, v, ev->cap_id, &s);
237 uint64_t prior = seen ? s.granted_sequence : 0;
238 if (ev->kind == ARGUS_EV_STALE_GENERATION) {
239 int corroborated = seen && s.subject == ev->principal && ev->cap_generation < s.generation;
240 return emit(out, cap, n_out, ev, ARGUS_F_STALE_GENERATION, ARGUS_SEV_HIGH,
241 corroborated ? 1 : 0,
242 corroborated ? ARGUS_CONTAIN_REVOKE_CAPABILITY : ARGUS_CONTAIN_NONE, prior);
243 }
244 if (!is_cap_use(ev) || !outcome_ok(ev))
245 return ARGUS_OK;
246 int hit = ev->code == ARGUS_AUTH_ERR_STALE_GEN || (seen && use_gen_min(ev) < s.generation);
247 if (!hit)
248 return ARGUS_OK;
249 return emit(out, cap, n_out, ev, ARGUS_F_STALE_GENERATION, ARGUS_SEV_HIGH, 1,
250 ARGUS_CONTAIN_REVOKE_CAPABILITY, prior);
251}
252
253/* ---- 3 REVOKED_CAPABILITY_USED ---------------------------------------------
254 * Trigger: a capability use with outcome OK where (a) the shadow says the
255 * slot is REVOKED and cap_generation (summary: MAX) <= the shadow
256 * generation (v1.1: a use above it was never minted, detector 1), or
257 * (b) the code is ERR_REVOKED (contradiction).
258 * A use with outcome DENIED and code ERR_REVOKED is the authority
259 * behaving correctly and never triggers.
260 * Evidence: ev cap_id/outcome/code; cap shadow state + revoked_sequence.
261 * Code ARGUS_F_REVOKED_CAPABILITY_USED, CRITICAL, sync_allowed 1,
262 * containment FREEZE_PRINCIPAL. prior_sequence = shadow revoked_sequence.
263 */
264static int det_revoked_capability_used(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
265 ArgusFinding *out, size_t cap, size_t *n_out)
266{
267 *n_out = 0;
268 if (!is_cap_use(ev) || !outcome_ok(ev))
269 return ARGUS_OK;
270 ArgusCapShadow s = {0};
271 /* v1.1: a use ABOVE the revoked generation was never minted: detector 1, not 3. */
272 int revoked = cap_lookup(ops, v, ev->cap_id, &s) && s.state == ARGUS_SHADOW_REVOKED &&
273 use_gen_max(ev) <= s.generation;
274 if (!revoked && ev->code != ARGUS_AUTH_ERR_REVOKED)
275 return ARGUS_OK;
276 return emit(out, cap, n_out, ev, ARGUS_F_REVOKED_CAPABILITY_USED, ARGUS_SEV_CRITICAL, 1,
277 ARGUS_CONTAIN_FREEZE_PRINCIPAL, revoked ? s.revoked_sequence : 0);
278}
279
280/* ---- 4 ARTIFACT_DIGEST_UNEXPECTED ------------------------------------------
281 * Trigger: ARTIFACT_ACTIVATED with outcome OK whose evidence_digest is not
282 * LIVE (admitted) in the artifact shadow: never seen, or rejected.
283 * Evidence: ev evidence_digest/outcome; artifact shadow state + sequence.
284 * Code ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED, CRITICAL, sync_allowed 1,
285 * containment REJECT_ARTIFACT. prior_sequence = the rejection, 0 if unseen.
286 */
287static int det_artifact_digest_unexpected(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
288 ArgusFinding *out, size_t cap, size_t *n_out)
289{
290 *n_out = 0;
291 if (ev->kind != ARGUS_EV_ARTIFACT_ACTIVATED || !outcome_ok(ev))
292 return ARGUS_OK;
293 ArgusArtifactShadow a = {0};
294 int found = ops->artifact(v, ev->evidence_digest, &a) == ARGUS_OK;
295 if (found && a.state == ARGUS_SHADOW_LIVE)
296 return ARGUS_OK;
297 return emit(out, cap, n_out, ev, ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED, ARGUS_SEV_CRITICAL, 1,
298 ARGUS_CONTAIN_REJECT_ARTIFACT, found ? a.sequence : 0);
299}
300
301/* ---- 5 SIGNATURE_INVALID (also: integrity reports, trusted-digest changes) --
302 * Trigger: (a) kind == SIGNATURE_FAILURE, any outcome: self-report, HIGH,
303 * sync 0, containment NONE (G-2); or
304 * (b) kind == INTEGRITY_VIOLATION, any outcome: self-report, HIGH,
305 * sync 0, containment NONE (G-14: a report reaches a finding, but a
306 * report is not evidence); or
307 * (c) ARTIFACT_ADMITTED with outcome OK but a nonzero code (admitted
308 * despite an error): the event contradicts itself, HIGH, sync 1,
309 * REJECT_ARTIFACT; or
310 * (d) POLICY_CHANGED / RUNTIME_BUILD_CHANGED with outcome OK whose
311 * evidence_digest differs from the stored expected digest, when one
312 * is stored (nonzero): HIGH, sync 0, containment NONE (G-14).
313 * ABI v1 has no signed policy-change path and no finding code for
314 * "trusted digest replaced", so SIGNATURE_INVALID is the closest v1
315 * code: every change after the first announcement is surfaced for
316 * review; re-announcing the stored digest is silent.
317 * Evidence: ev kind/outcome/code/evidence_digest; stored policy/runtime digest.
318 * Default (table): sync_allowed 1, containment REJECT_ARTIFACT (case c).
319 * prior_sequence 0 (the digest store keeps no sequence).
320 */
321static int det_signature_invalid(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
322 ArgusFinding *out, size_t cap, size_t *n_out)
323{
324 *n_out = 0;
325 if (ev->kind == ARGUS_EV_SIGNATURE_FAILURE || ev->kind == ARGUS_EV_INTEGRITY_VIOLATION)
326 return emit(out, cap, n_out, ev, ARGUS_F_SIGNATURE_INVALID, ARGUS_SEV_HIGH, 0, ARGUS_CONTAIN_NONE, 0);
327 if (ev->kind == ARGUS_EV_ARTIFACT_ADMITTED && outcome_ok(ev) && ev->code != 0)
328 return emit(out, cap, n_out, ev, ARGUS_F_SIGNATURE_INVALID, ARGUS_SEV_HIGH, 1,
329 ARGUS_CONTAIN_REJECT_ARTIFACT, 0);
330 if ((ev->kind == ARGUS_EV_POLICY_CHANGED || ev->kind == ARGUS_EV_RUNTIME_BUILD_CHANGED) && outcome_ok(ev)) {
331 uint8_t stored[ARGUS_DIGEST_LEN];
332 int rc = ev->kind == ARGUS_EV_POLICY_CHANGED ? ops->policy_digest(v, stored)
333 : ops->runtime_digest(v, stored);
334 if (rc == ARGUS_OK && !bytes_zero(stored, ARGUS_DIGEST_LEN) &&
335 !bytes_equal(stored, ev->evidence_digest, ARGUS_DIGEST_LEN))
336 return emit(out, cap, n_out, ev, ARGUS_F_SIGNATURE_INVALID, ARGUS_SEV_HIGH, 0, ARGUS_CONTAIN_NONE, 0);
337 }
338 return ARGUS_OK;
339}
340
341/* ---- 6 CREDENTIAL_SCOPE_VIOLATION ------------------------------------------
342 * Trigger: (a) CREDENTIAL_LEASE_USED with outcome OK where the lease
343 * (object_id) is unseen, not LIVE (revoked), held by another subject
344 * (lease.subject != principal), or the requested resource bits
345 * exceed the lease scope ((resource & ~scope) != 0); or
346 * (b) CREDENTIAL_LEASE_CREATED with outcome OK for a lease id the
347 * shadow already holds (LIVE or REVOKED): lease ids are never reused
348 * (ruling A, G-26); the core does not apply it.
349 * Evidence: ev kind/object_id/principal/resource/outcome; lease shadow.
350 * Code ARGUS_F_CREDENTIAL_SCOPE_VIOLATION, HIGH, sync_allowed 0,
351 * containment REVOKE_CREDENTIAL_LEASE. prior_sequence = lease sequence.
352 */
353static int det_credential_scope_violation(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
354 ArgusFinding *out, size_t cap, size_t *n_out)
355{
356 *n_out = 0;
357 if ((ev->kind != ARGUS_EV_CREDENTIAL_LEASE_USED && ev->kind != ARGUS_EV_CREDENTIAL_LEASE_CREATED) ||
358 !outcome_ok(ev))
359 return ARGUS_OK;
360 ArgusLeaseShadow l = {0};
361 int found = ops->lease(v, ev->object_id, &l) == ARGUS_OK && l.state != ARGUS_SHADOW_UNSEEN;
362 int hit;
363 if (ev->kind == ARGUS_EV_CREDENTIAL_LEASE_CREATED)
364 hit = found;
365 else
366 hit = !(found && l.state == ARGUS_SHADOW_LIVE && l.subject == ev->principal &&
367 (ev->resource & ~l.scope) == 0);
368 if (!hit)
369 return ARGUS_OK;
370 return emit(out, cap, n_out, ev, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION, ARGUS_SEV_HIGH, 0,
371 ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE, found ? l.sequence : 0);
372}
373
374/* ---- 7 MACHINE_IDENTITY_MISMATCH -------------------------------------------
375 * Trigger: (a) any event other than MACHINE_JOINED carrying a nonzero
376 * machine_id that is not a joined machine (unknown, or a REMOVED
377 * tombstone); this includes MACHINE_TRUST_CHANGED for an unknown
378 * machine (the core creates no entry); or
379 * (b) MACHINE_JOINED with outcome OK for a machine that is already
380 * joined (re-join without MACHINE_REMOVED in between); or
381 * (c) an all-zero machine_id on any event that is neither
382 * ARGUS_FLAG_SYNTHETIC nor ARGUS_FLAG_CONSUMER: a live producer must
383 * always attribute its events (G-8/G-10). Test/replay streams
384 * (SYNTHETIC) and ARGUS's own events (CONSUMER, e.g. TELEMETRY_DROPPED)
385 * are exempt, because they have no producing machine.
386 * Evidence: ev machine_id/kind/outcome/flags; machine shadow joined_sequence.
387 * Code ARGUS_F_MACHINE_IDENTITY_MISMATCH, HIGH, sync_allowed 0,
388 * containment REQUIRE_REATTESTATION. prior_sequence = earlier join, if any.
389 * MachineId is PROVISIONAL (opaque 32 bytes); this is an identity-consistency
390 * check only, not cryptographic attestation.
391 */
392static int det_machine_identity_mismatch(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
393 ArgusFinding *out, size_t cap, size_t *n_out)
394{
395 *n_out = 0;
396 ArgusMachineShadow m = {0};
397 int joined = 0;
398 int hit;
399 if (bytes_zero(ev->machine_id, ARGUS_MACHINE_ID_LEN)) {
400 hit = (ev->flags & (ARGUS_FLAG_SYNTHETIC | ARGUS_FLAG_CONSUMER)) == 0;
401 } else {
402 joined = machine_joined(ops, v, ev->machine_id, &m);
403 if (ev->kind == ARGUS_EV_MACHINE_JOINED)
404 hit = outcome_ok(ev) && joined;
405 else
406 hit = !joined;
407 }
408 if (!hit)
409 return ARGUS_OK;
410 return emit(out, cap, n_out, ev, ARGUS_F_MACHINE_IDENTITY_MISMATCH, ARGUS_SEV_HIGH, 0,
411 ARGUS_CONTAIN_REQUIRE_REATTESTATION, joined ? m.joined_sequence : 0);
412}
413
414/* ---- 8 EFFECT_CLASS_UNAUTHORIZED -------------------------------------------
415 * The effective effect class of an event is EXTERNAL for the kinds
416 * EXTERNAL_EFFECT_REQUESTED / EXTERNAL_EFFECT_COMMITTED whatever the producer
417 * wrote in effect_class (G-24: the kind wins over the label), otherwise the
418 * event's effect_class.
419 * Trigger: outcome OK and
420 * (a) cap_id == ARGUS_CAP_NONE, effective class EXTERNAL, on
421 * EXTERNAL_EFFECT_* or CAPABILITY_USED (an irreversible effect with no capability); or
422 * (b) a capability use (is_cap_use) whose granted rights in the
423 * shadow lack AIENOS_CAP_RIGHT_EFFECT for effective class EXTERNAL,
424 * or lack AIENOS_CAP_RIGHT_WRITE for effective class EVIDENCE (a
425 * durable write under a read-only capability).
426 * An unseen cap_id is left to detector 1 (no double report).
427 * Evidence: ev kind/effect_class/cap_id/outcome; cap shadow rights.
428 * Code ARGUS_F_EFFECT_CLASS_UNAUTHORIZED, CRITICAL, sync_allowed 1,
429 * containment PAUSE_EXTERNAL_EFFECTS. prior_sequence = the grant.
430 */
431static int det_effect_class_unauthorized(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
432 ArgusFinding *out, size_t cap, size_t *n_out)
433{
434 *n_out = 0;
435 if (!outcome_ok(ev))
436 return ARGUS_OK;
437 int effect_kind = ev->kind == ARGUS_EV_EXTERNAL_EFFECT_REQUESTED || ev->kind == ARGUS_EV_EXTERNAL_EFFECT_COMMITTED;
438 uint8_t eff = effect_kind ? (uint8_t)ARGUS_EFFECT_EXTERNAL : ev->effect_class;
439 uint32_t need;
440 if (eff == ARGUS_EFFECT_EXTERNAL)
441 need = ARGUS_AUTH_RIGHT_EFFECT;
442 else if (eff == ARGUS_EFFECT_EVIDENCE)
443 need = ARGUS_AUTH_RIGHT_WRITE;
444 else
445 return ARGUS_OK;
446 uint64_t prior = 0;
447 int hit = 0;
448 if (ev->cap_id == ARGUS_CAP_NONE) {
449 hit = eff == ARGUS_EFFECT_EXTERNAL && (effect_kind || ev->kind == ARGUS_EV_CAPABILITY_USED);
450 } else if (is_cap_use(ev)) {
451 ArgusCapShadow s = {0};
452 if (cap_lookup(ops, v, ev->cap_id, &s) && (s.rights & need) == 0) {
453 hit = 1;
454 prior = s.granted_sequence;
455 }
456 }
457 if (!hit)
458 return ARGUS_OK;
459 return emit(out, cap, n_out, ev, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED, ARGUS_SEV_CRITICAL, 1,
460 ARGUS_CONTAIN_PAUSE_EXTERNAL_EFFECTS, prior);
461}
462
463/* ---- 9 WORLD_PROVENANCE_INCONSISTENT ---------------------------------------
464 * Trigger: WORLD_COMMITTED with outcome OK, when a World has already been
465 * committed FOR THE SAME STORE (object_id; world(v, store_id), v1.1;
466 * shadow generation != 0), and either
467 * (a) world_generation == shadow generation with a different
468 * evidence_digest (two different Worlds claim one generation), or
469 * (b) world_generation != shadow generation + 1 otherwise (skip or
470 * rollback). Equal generation with the SAME digest is an idempotent
471 * replay and does not trigger (orchestrator brief, wave 2); the core
472 * does not re-apply it. The first commit ever is accepted.
473 * Generations are 64-bit; a shadow at UINT64_MAX has no legal
474 * successor, so any different generation after it triggers.
475 * Evidence: ev world_generation/evidence_digest; world shadow.
476 * Code ARGUS_F_WORLD_PROVENANCE_INCONSISTENT, HIGH, sync_allowed 0,
477 * containment RAISE_EFFECT_CLASS. prior_sequence = the last commit.
478 */
479static int det_world_provenance_inconsistent(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
480 ArgusFinding *out, size_t cap, size_t *n_out)
481{
482 *n_out = 0;
483 if (ev->kind != ARGUS_EV_WORLD_COMMITTED || !outcome_ok(ev))
484 return ARGUS_OK;
485 ArgusWorldShadow w = {0};
486 if (ops->world(v, ev->object_id, &w) != ARGUS_OK || w.generation == 0) /* per store (v1.1) */
487 return ARGUS_OK;
488 int hit;
489 if (ev->world_generation == w.generation)
490 hit = !bytes_equal(ev->evidence_digest, w.digest, ARGUS_DIGEST_LEN);
491 else
492 hit = w.generation == UINT64_MAX || ev->world_generation != w.generation + 1u;
493 if (!hit)
494 return ARGUS_OK;
495 return emit(out, cap, n_out, ev, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT, ARGUS_SEV_HIGH, 0,
496 ARGUS_CONTAIN_RAISE_EFFECT_CLASS, w.sequence);
497}
498
499/* ---- 10 QUARANTINED_USE ----------------------------------------------------
500 * Trigger: (a) PROVIDER_USED with outcome OK whose provider (evidence_digest)
501 * is quarantined (shadow REVOKED): CRITICAL, sync 1,
502 * QUARANTINE_PROVIDER, prior = the quarantine;
503 * (a') PROVIDER_USED with outcome OK whose provider was never
504 * discovered (unseen, G-9): HIGH, sync 0, QUARANTINE_PROVIDER,
505 * prior 0 (weaker evidence: the discovery may have been lost);
506 * (b) any event with outcome OK carrying a nonzero machine_id whose
507 * machine shadow trust is QUARANTINED (joined or tombstone):
508 * CRITICAL, sync 1, QUARANTINE_MACHINE, prior = changed_sequence.
509 * MACHINE_JOINED / MACHINE_TRUST_CHANGED / MACHINE_REMOVED are
510 * exempt from (b): they are the lifecycle of the quarantine itself
511 * (an upward TRUST_CHANGED is detector 15's).
512 * Can emit two findings for one event ((a) or (a') plus (b)).
513 * Evidence: ev kind/outcome/evidence_digest/machine_id; provider + machine shadows.
514 * Code ARGUS_F_QUARANTINED_USE.
515 */
516static int det_quarantined_use(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
517 ArgusFinding *out, size_t cap, size_t *n_out)
518{
519 *n_out = 0;
520 if (!outcome_ok(ev))
521 return ARGUS_OK;
522 int rc;
523 if (ev->kind == ARGUS_EV_PROVIDER_USED) {
524 ArgusProviderShadow p = {0};
525 int found = ops->provider(v, ev->evidence_digest, &p) == ARGUS_OK && p.state != ARGUS_SHADOW_UNSEEN;
526 if (found && p.state == ARGUS_SHADOW_REVOKED)
527 rc = emit(out, cap, n_out, ev, ARGUS_F_QUARANTINED_USE, ARGUS_SEV_CRITICAL, 1,
528 ARGUS_CONTAIN_QUARANTINE_PROVIDER, p.sequence);
529 else if (!found)
530 rc = emit(out, cap, n_out, ev, ARGUS_F_QUARANTINED_USE, ARGUS_SEV_HIGH, 0,
531 ARGUS_CONTAIN_QUARANTINE_PROVIDER, 0);
532 else
533 rc = ARGUS_OK;
534 if (rc != ARGUS_OK)
535 return rc;
536 }
537 int lifecycle = ev->kind == ARGUS_EV_MACHINE_JOINED || ev->kind == ARGUS_EV_MACHINE_TRUST_CHANGED ||
538 ev->kind == ARGUS_EV_MACHINE_REMOVED;
539 if (!lifecycle && !bytes_zero(ev->machine_id, ARGUS_MACHINE_ID_LEN)) {
540 ArgusMachineShadow m = {0};
541 if (ops->machine(v, ev->machine_id, &m) == ARGUS_OK && m.trust == ARGUS_TRUST_QUARANTINED) {
542 rc = emit(out, cap, n_out, ev, ARGUS_F_QUARANTINED_USE, ARGUS_SEV_CRITICAL, 1,
543 ARGUS_CONTAIN_QUARANTINE_MACHINE, m.changed_sequence);
544 if (rc != ARGUS_OK)
545 return rc;
546 }
547 }
548 return ARGUS_OK;
549}
550
551/* ---- 14 SUBJECT_MISMATCH ---------------------------------------------------
552 * Trigger: a capability use with outcome OK on a slot the shadow has seen
553 * (LIVE or REVOKED) whose principal differs from the granted
554 * subject (G-3). An UNSEEN slot is detector 1's.
555 * Evidence: ev principal/cap_id/outcome; cap shadow subject.
556 * Code ARGUS_F_SUBJECT_MISMATCH, HIGH, sync_allowed 1, containment
557 * FREEZE_PRINCIPAL. prior_sequence = the grant.
558 */
559static int det_subject_mismatch(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
560 ArgusFinding *out, size_t cap, size_t *n_out)
561{
562 *n_out = 0;
563 if (!is_cap_use(ev) || !outcome_ok(ev))
564 return ARGUS_OK;
565 ArgusCapShadow s = {0};
566 if (!cap_lookup(ops, v, ev->cap_id, &s) || s.subject == ev->principal)
567 return ARGUS_OK;
568 return emit(out, cap, n_out, ev, ARGUS_F_SUBJECT_MISMATCH, ARGUS_SEV_HIGH, 1,
569 ARGUS_CONTAIN_FREEZE_PRINCIPAL, s.granted_sequence);
570}
571
572/* ---- 15 TRUST_ESCALATION ---------------------------------------------------
573 * Trigger: (a) MACHINE_TRUST_CHANGED with outcome OK for a JOINED machine
574 * whose object_id (a valid ARGUS_TRUST_*, 1..6) ranks better than
575 * the machine's current trust (an upward move; the core ignores
576 * it). Unknown machines are detector 7's; values outside 1..6 are
577 * ignored by the header and never trigger; or
578 * (b) MACHINE_JOINED with outcome OK, for a machine that is not
579 * currently joined, whose object_id (1..6) ranks better than the
580 * baseline: OBSERVED for a new machine, or the worse of OBSERVED
581 * and the tombstone's preserved trust after MACHINE_REMOVED
582 * (a machine cannot declare itself TRUSTED, G-10, nor launder a
583 * quarantine by re-joining, G-8). object_id 0 means "default"
584 * (no claim) and never triggers. A re-join of a joined machine is
585 * detector 7's.
586 * Evidence: ev kind/object_id/machine_id/outcome; machine shadow trust.
587 * Code ARGUS_F_TRUST_ESCALATION, HIGH, sync_allowed 0, containment
588 * REQUIRE_REATTESTATION. prior_sequence = machine changed_sequence (0 if new).
589 */
590static int det_trust_escalation(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
591 ArgusFinding *out, size_t cap, size_t *n_out)
592{
593 *n_out = 0;
594 if ((ev->kind != ARGUS_EV_MACHINE_TRUST_CHANGED && ev->kind != ARGUS_EV_MACHINE_JOINED) || !outcome_ok(ev))
595 return ARGUS_OK;
596 int claim = argus_trust_rank(ev->object_id);
597 if (claim < 0)
598 return ARGUS_OK;
599 ArgusMachineShadow m = {0};
600 int found = ops->machine(v, ev->machine_id, &m) == ARGUS_OK;
601 int joined = found && m.joined_sequence != 0;
602 int baseline;
603 if (ev->kind == ARGUS_EV_MACHINE_TRUST_CHANGED) {
604 if (!joined)
605 return ARGUS_OK;
606 baseline = shadow_rank(m.trust);
607 } else {
608 if (joined)
609 return ARGUS_OK;
610 baseline = argus_trust_rank(ARGUS_TRUST_OBSERVED);
611 if (found && shadow_rank(m.trust) > baseline)
612 baseline = shadow_rank(m.trust);
613 }
614 if (claim >= baseline)
615 return ARGUS_OK;
616 return emit(out, cap, n_out, ev, ARGUS_F_TRUST_ESCALATION, ARGUS_SEV_HIGH, 0,
617 ARGUS_CONTAIN_REQUIRE_REATTESTATION, found ? m.changed_sequence : 0);
618}
619
620/* ---- table + runner -------------------------------------------------------- */
621
622const ArgusDetector argus_hard_detectors[ARGUS_HARD_DETECTOR_COUNT] = {
623 { ARGUS_F_FORGED_CAPABILITY, 1, ARGUS_CONTAIN_FREEZE_PRINCIPAL, "forged_capability", det_forged_capability },
624 { ARGUS_F_STALE_GENERATION, 1, ARGUS_CONTAIN_REVOKE_CAPABILITY, "stale_generation", det_stale_generation },
625 { ARGUS_F_REVOKED_CAPABILITY_USED, 1, ARGUS_CONTAIN_FREEZE_PRINCIPAL, "revoked_capability_used",
626 det_revoked_capability_used },
627 { ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED, 1, ARGUS_CONTAIN_REJECT_ARTIFACT, "artifact_digest_unexpected",
628 det_artifact_digest_unexpected },
629 { ARGUS_F_SIGNATURE_INVALID, 1, ARGUS_CONTAIN_REJECT_ARTIFACT, "signature_invalid", det_signature_invalid },
630 { ARGUS_F_CREDENTIAL_SCOPE_VIOLATION, 0, ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE, "credential_scope_violation",
631 det_credential_scope_violation },
632 { ARGUS_F_MACHINE_IDENTITY_MISMATCH, 0, ARGUS_CONTAIN_REQUIRE_REATTESTATION, "machine_identity_mismatch",
633 det_machine_identity_mismatch },
634 { ARGUS_F_EFFECT_CLASS_UNAUTHORIZED, 1, ARGUS_CONTAIN_PAUSE_EXTERNAL_EFFECTS, "effect_class_unauthorized",
635 det_effect_class_unauthorized },
636 { ARGUS_F_WORLD_PROVENANCE_INCONSISTENT, 0, ARGUS_CONTAIN_RAISE_EFFECT_CLASS, "world_provenance_inconsistent",
637 det_world_provenance_inconsistent },
638 { ARGUS_F_QUARANTINED_USE, 1, ARGUS_CONTAIN_QUARANTINE_PROVIDER, "quarantined_use", det_quarantined_use },
639 { ARGUS_F_SUBJECT_MISMATCH, 1, ARGUS_CONTAIN_FREEZE_PRINCIPAL, "subject_mismatch", det_subject_mismatch },
640 { ARGUS_F_TRUST_ESCALATION, 0, ARGUS_CONTAIN_REQUIRE_REATTESTATION, "trust_escalation", det_trust_escalation },
641};
642
643const size_t argus_hard_detector_count = ARGUS_HARD_DETECTOR_COUNT;
644
645int argus_detect_run(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
646 ArgusFinding *out, size_t cap, size_t *n_out)
647{
648 if (n_out == NULL)
649 return ARGUS_ERR_ARG;
650 *n_out = 0;
651 if (ops == NULL || v == NULL || ev == NULL || (out == NULL && cap != 0))
652 return ARGUS_ERR_ARG;
653 if (ops->cap == NULL || ops->machine == NULL || ops->artifact == NULL || ops->lease == NULL ||
654 ops->provider == NULL || ops->world == NULL || ops->policy_digest == NULL || ops->runtime_digest == NULL)
655 return ARGUS_ERR_ARG;
656 size_t n = 0;
657 for (size_t i = 0; i < argus_hard_detector_count; i++) {
658 size_t k = 0;
659 ArgusFinding *slot = out != NULL ? out + n : NULL;
660 int rc = argus_hard_detectors[i].fn(ops, v, ev, slot, cap - n, &k);
661 n += k;
662 if (rc != ARGUS_OK) {
663 *n_out = n;
664 return rc;
665 }
666 }
667 *n_out = n;
668 return ARGUS_OK;
669}