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

373 lines · 17901 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_event.c -- ARGUS event ABI v1: canonical encoding, validation, digests.
19 *
20 * Encoding is field by field, little-endian, at the offsets documented in
21 * argus_abi.h. The in-memory struct is never copied to the wire, so host
22 * endianness and padding cannot leak into the bytes or the digests.
23 *
24 * argus_event_encode, argus_event_digest and argus_chain_extend never
25 * validate: they produce canonical bytes for any event (the core hashes
26 * internal records with sequence 0). Only decode and validate reject.
27 *
28 * Validation (argus_event_decode and argus_event_validate):
29 * version == ARGUS_ABI_VERSION else ARGUS_ERR_VERSION
30 * class_ in 1..ARGUS_CLASS_MAX else ARGUS_ERR_MALFORMED (all below too)
31 * kind in the enumerated ARGUS_EV_* set (ARGUS_EV_NONE is NOT accepted)
32 * class_ <= argus_event_min_class(kind) (as strong as the kind requires, or stronger)
33 * effect_class in 0..ARGUS_EFFECT_MAX
34 * outcome in 1..ARGUS_OUTCOME_MAX
35 * flags: any value (v1.1: bits 0-1 markers, bits 2-15 = producer stream id)
36 * ARGUS_FLAG_CONSUMER set if and only if kind == TELEMETRY_DROPPED or (v1.2)
37 * CONTAINMENT_PROPOSED
38 * v1.2 kinds 90-92: packed type/status/outcome rules, request_id != 0
39 * (validate_containment below); 93: floor CRITICAL, no CONSUMER
40 * cap_id < ARGUS_CAP_MAX, or == ARGUS_CAP_NONE ("no capability"; cap_id 0 is
41 * the authority OFFICE slot, a real capability)
42 * CAPABILITY_USE_SUMMARY (81): outcome OK (a summary counts successful
43 * validates only); any tick is accepted (producers write 0);
44 * world_generation (MIN generation) <= cap_generation (MAX), else
45 * malformed. object_id is not checked (forward-compat; producers write 0)
46 * sequence != 0 and sequence != UINT64_MAX
47 * EXTERNAL_EFFECT_REQUESTED/DENIED/COMMITTED carry effect_class EXTERNAL
48 * Every one of the 128 bytes is carried by some field, so a buffer that
49 * decodes successfully re-encodes to exactly the same bytes.
50 *
51 * Finding digest: SHA-256 over this 104-byte little-endian encoding of
52 * ArgusFinding (never the struct):
53 * off 0 u16 code off 2 u8 severity off 3 u8 confidence
54 * off 4 u8 sync_allowed off 5 u8 containment off 6 u16 detector
55 * off 8 u64 sequence off 16 u64 prior_sequence
56 * off 24 u32 principal off 28 u32 cap_id off 32 u64 cap_generation
57 * off 40 machine_id[32] off 72 event_digest[32] end 104
58 */
59#include "argus_abi.h"
60#include "sha256.h"
61
62#define ARGUS_FINDING_WIRE_SIZE 104u
63
64static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }
65static void put32(uint8_t *p, uint32_t v)
66{
67 p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24);
68}
69static void put64(uint8_t *p, uint64_t v) { put32(p, (uint32_t)v); put32(p + 4, (uint32_t)(v >> 32)); }
70static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] | ((uint16_t)p[1] << 8)); }
71static uint32_t get32(const uint8_t *p)
72{
73 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
74}
75static uint64_t get64(const uint8_t *p) { return (uint64_t)get32(p) | ((uint64_t)get32(p + 4) << 32); }
76static void copy_bytes(uint8_t *dst, const uint8_t *src, size_t n) { for (size_t i = 0; i < n; i++) dst[i] = src[i]; }
77
78/*
79 * Weakest class a producer may give each kind (smaller number = stronger).
80 * A producer may always label an event STRONGER than this, never weaker:
81 * the ring's watermarks and the core's loss reporting are keyed on class,
82 * so a weak label on a security-relevant kind would let it be dropped
83 * first and reported as a harmless loss (HOSTILE_REVIEW G-6/G-7).
84 * CRITICAL: evidence of compromise, loss of evidence, trust/authority
85 * withdrawn, the trusted base changed, irreversible effects done
86 * SECURITY: authority granted or refused, new objects/machines/providers
87 * entering the system, effects requested/denied, World commits
88 * AUDIT: routine use of authority that is already recorded
89 * v1.2: the containment kinds 90-93 are CRITICAL (spec section 3).
90 * No v1 kind has an INFORMATIONAL floor. Unknown kinds (including NONE) -> 0.
91 */
92uint8_t argus_event_min_class(uint16_t kind)
93{
94 switch (kind) {
95 case ARGUS_EV_INTEGRITY_VIOLATION: case ARGUS_EV_SIGNATURE_FAILURE:
96 case ARGUS_EV_STALE_GENERATION: case ARGUS_EV_FORGED_CAPABILITY:
97 case ARGUS_EV_TELEMETRY_DROPPED: case ARGUS_EV_PROVIDER_QUARANTINED:
98 case ARGUS_EV_MACHINE_TRUST_CHANGED: case ARGUS_EV_MACHINE_REMOVED:
99 case ARGUS_EV_POLICY_CHANGED: case ARGUS_EV_RUNTIME_BUILD_CHANGED:
100 case ARGUS_EV_CAPABILITY_REVOKED: case ARGUS_EV_CREDENTIAL_LEASE_REVOKED:
101 case ARGUS_EV_ARTIFACT_REJECTED: case ARGUS_EV_EXTERNAL_EFFECT_COMMITTED:
102 case ARGUS_EV_CONTAINMENT_PROPOSED: case ARGUS_EV_CONTAINMENT_DECIDED: /* v1.2 (spec 3) */
103 case ARGUS_EV_CONTAINMENT_EXECUTED: case ARGUS_EV_AUTHORITY_ESCALATED:
104 return ARGUS_CLASS_CRITICAL;
105 case ARGUS_EV_CAPABILITY_GRANTED: case ARGUS_EV_CAPABILITY_DENIED:
106 case ARGUS_EV_CREDENTIAL_LEASE_CREATED:
107 case ARGUS_EV_ARTIFACT_ADMITTED: case ARGUS_EV_ARTIFACT_ACTIVATED:
108 case ARGUS_EV_MACHINE_JOINED:
109 case ARGUS_EV_PROVIDER_DISCOVERED: case ARGUS_EV_PROVIDER_CHANGED:
110 case ARGUS_EV_EXTERNAL_EFFECT_REQUESTED: case ARGUS_EV_EXTERNAL_EFFECT_DENIED:
111 case ARGUS_EV_WORLD_COMMITTED:
112 return ARGUS_CLASS_SECURITY;
113 case ARGUS_EV_CAPABILITY_USED: case ARGUS_EV_CREDENTIAL_LEASE_USED:
114 case ARGUS_EV_PROVIDER_USED: case ARGUS_EV_CAPABILITY_USE_SUMMARY:
115 return ARGUS_CLASS_AUDIT;
116 default:
117 return 0;
118 }
119}
120
121/*
122 * v1.2 containment kinds 90-92 (spec section 3, "Validate / ring rules"). object_id packs
123 * type | status << 8 | finding_code << 16; world_generation is the request_id.
124 * type in 1..ARGUS_CONTAIN_MAX; request_id != 0
125 * 90 PROPOSED: status 0, outcome OK
126 * 91 DECIDED: GRANT<->OK, DENY<->DENIED, ESCALATE<->ERROR
127 * 92 EXECUTED: DONE/PARTIAL<->OK, FAILED<->DENIED or ERROR, UNAVAILABLE/FAILED_SCOPE<->ERROR;
128 * DONE for any type but RevokeCapability needs ARGUS_FLAG_SYNTHETIC (I2)
129 * finding_code, code, tick, resource and effect_class are producer conventions, not checked
130 * here (lane C / the offline checker judge them). 93 has only the floor and no-CONSUMER rules.
131 */
132static int validate_containment(const ArgusEvent *ev)
133{
134 unsigned type = ARGUS_CONTAIN_TYPE_OF(ev->object_id);
135 unsigned status = ARGUS_CONTAIN_STATUS_OF(ev->object_id);
136 unsigned out = ev->outcome;
137 if (type < 1 || type > ARGUS_CONTAIN_MAX) return ARGUS_ERR_MALFORMED;
138 if (ev->world_generation == 0) return ARGUS_ERR_MALFORMED; /* request_id */
139 switch (ev->kind) {
140 case ARGUS_EV_CONTAINMENT_PROPOSED:
141 return (status == ARGUS_CSTATUS_PROPOSED && out == ARGUS_OUTCOME_OK) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
142 case ARGUS_EV_CONTAINMENT_DECIDED:
143 if (status == ARGUS_CSTATUS_GRANT) return out == ARGUS_OUTCOME_OK ? ARGUS_OK : ARGUS_ERR_MALFORMED;
144 if (status == ARGUS_CSTATUS_DENY) return out == ARGUS_OUTCOME_DENIED ? ARGUS_OK : ARGUS_ERR_MALFORMED;
145 if (status == ARGUS_CSTATUS_ESCALATE) return out == ARGUS_OUTCOME_ERROR ? ARGUS_OK : ARGUS_ERR_MALFORMED;
146 return ARGUS_ERR_MALFORMED;
147 default: /* ARGUS_EV_CONTAINMENT_EXECUTED */
148 switch (status) {
149 case ARGUS_CSTATUS_DONE:
150 if (type != ARGUS_CONTAIN_REVOKE_CAPABILITY && (ev->flags & ARGUS_FLAG_SYNTHETIC) == 0)
151 return ARGUS_ERR_MALFORMED; /* I2: no live executor for it */
152 return out == ARGUS_OUTCOME_OK ? ARGUS_OK : ARGUS_ERR_MALFORMED;
153 case ARGUS_CSTATUS_PARTIAL:
154 return out == ARGUS_OUTCOME_OK ? ARGUS_OK : ARGUS_ERR_MALFORMED;
155 case ARGUS_CSTATUS_FAILED:
156 return (out == ARGUS_OUTCOME_DENIED || out == ARGUS_OUTCOME_ERROR) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
157 case ARGUS_CSTATUS_UNAVAILABLE: case ARGUS_CSTATUS_FAILED_SCOPE:
158 return out == ARGUS_OUTCOME_ERROR ? ARGUS_OK : ARGUS_ERR_MALFORMED;
159 default:
160 return ARGUS_ERR_MALFORMED;
161 }
162 }
163}
164
165int argus_event_validate(const ArgusEvent *ev)
166{
167 if (!ev) return ARGUS_ERR_ARG;
168 if (ev->version != ARGUS_ABI_VERSION) return ARGUS_ERR_VERSION;
169 if (ev->class_ < ARGUS_CLASS_CRITICAL || ev->class_ > ARGUS_CLASS_MAX) return ARGUS_ERR_MALFORMED;
170 uint8_t min = argus_event_min_class(ev->kind);
171 if (min == 0) return ARGUS_ERR_MALFORMED; /* unknown kind */
172 if (ev->class_ > min) return ARGUS_ERR_MALFORMED; /* weaker than the kind allows */
173 if (ev->effect_class > ARGUS_EFFECT_MAX) return ARGUS_ERR_MALFORMED;
174 if (ev->outcome < ARGUS_OUTCOME_OK || ev->outcome > ARGUS_OUTCOME_MAX) return ARGUS_ERR_MALFORMED;
175 /* v1.1: every flag bit is defined (bits 2-15 = stream id), so no reserved-bit check. */
176 /* CONSUMER is set exactly on TELEMETRY_DROPPED and (v1.2) CONTAINMENT_PROPOSED: only
177 * ARGUS reports its own losses and synthesizes its own proposals. */
178 if (((ev->flags & ARGUS_FLAG_CONSUMER) != 0) !=
179 (ev->kind == ARGUS_EV_TELEMETRY_DROPPED || ev->kind == ARGUS_EV_CONTAINMENT_PROPOSED))
180 return ARGUS_ERR_MALFORMED;
181 if (ev->kind >= ARGUS_EV_CONTAINMENT_PROPOSED && ev->kind <= ARGUS_EV_CONTAINMENT_EXECUTED) {
182 int rc = validate_containment(ev);
183 if (rc != ARGUS_OK) return rc;
184 }
185 if (ev->cap_id >= ARGUS_CAP_MAX && ev->cap_id != ARGUS_CAP_NONE)
186 return ARGUS_ERR_MALFORMED; /* the authority cannot mint it */
187 if (ev->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY && ev->outcome != ARGUS_OUTCOME_OK)
188 return ARGUS_ERR_MALFORMED; /* a summary counts successful validates only */
189 if (ev->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY && ev->world_generation > ev->cap_generation)
190 return ARGUS_ERR_MALFORMED; /* summary MIN generation above its MAX */
191 if (ev->sequence == 0 || ev->sequence == UINT64_MAX) return ARGUS_ERR_MALFORMED;
192 if ((ev->kind == ARGUS_EV_EXTERNAL_EFFECT_REQUESTED || ev->kind == ARGUS_EV_EXTERNAL_EFFECT_DENIED ||
193 ev->kind == ARGUS_EV_EXTERNAL_EFFECT_COMMITTED) && ev->effect_class != ARGUS_EFFECT_EXTERNAL)
194 return ARGUS_ERR_MALFORMED;
195 return ARGUS_OK;
196}
197
198/* Canonical bytes of any event, valid or not. */
199static void encode_raw(const ArgusEvent *ev, uint8_t o[ARGUS_EVENT_SIZE])
200{
201 o[0] = ev->version;
202 o[1] = ev->class_;
203 put16(o + 2, ev->kind);
204 o[4] = ev->effect_class;
205 o[5] = ev->outcome;
206 put16(o + 6, ev->flags);
207 put64(o + 8, ev->sequence);
208 put64(o + 16, ev->tick);
209 put32(o + 24, ev->principal);
210 put32(o + 28, (uint32_t)ev->code);
211 put32(o + 32, ev->cap_id);
212 put32(o + 36, ev->object_id);
213 put64(o + 40, ev->cap_generation);
214 put64(o + 48, ev->world_generation);
215 put64(o + 56, ev->resource);
216 copy_bytes(o + 64, ev->machine_id, ARGUS_MACHINE_ID_LEN);
217 copy_bytes(o + 96, ev->evidence_digest, ARGUS_DIGEST_LEN);
218}
219
220int argus_event_encode(const ArgusEvent *ev, uint8_t out[ARGUS_EVENT_SIZE])
221{
222 if (!ev || !out) return ARGUS_ERR_ARG;
223 encode_raw(ev, out); /* never validates (header convention); decode/validate do */
224 return ARGUS_OK;
225}
226
227int argus_event_decode(const uint8_t in[ARGUS_EVENT_SIZE], ArgusEvent *out)
228{
229 if (!in || !out) return ARGUS_ERR_ARG;
230 ArgusEvent ev;
231 ev.version = in[0];
232 ev.class_ = in[1];
233 ev.kind = get16(in + 2);
234 ev.effect_class = in[4];
235 ev.outcome = in[5];
236 ev.flags = get16(in + 6);
237 ev.sequence = get64(in + 8);
238 ev.tick = get64(in + 16);
239 ev.principal = get32(in + 24);
240 ev.code = (int32_t)get32(in + 28);
241 ev.cap_id = get32(in + 32);
242 ev.object_id = get32(in + 36);
243 ev.cap_generation = get64(in + 40);
244 ev.world_generation = get64(in + 48);
245 ev.resource = get64(in + 56);
246 copy_bytes(ev.machine_id, in + 64, ARGUS_MACHINE_ID_LEN);
247 copy_bytes(ev.evidence_digest, in + 96, ARGUS_DIGEST_LEN);
248 int rc = argus_event_validate(&ev);
249 if (rc != ARGUS_OK) return rc;
250 *out = ev;
251 return ARGUS_OK;
252}
253
254void argus_event_digest(const ArgusEvent *ev, uint8_t out[ARGUS_DIGEST_LEN])
255{
256 uint8_t b[ARGUS_EVENT_SIZE];
257 encode_raw(ev, b);
258 sha256_hash(b, sizeof b, out);
259}
260
261void argus_chain_extend(uint8_t chain[ARGUS_DIGEST_LEN], const ArgusEvent *ev)
262{
263 uint8_t b[ARGUS_EVENT_SIZE];
264 sha256_ctx c;
265 encode_raw(ev, b);
266 sha256_init(&c);
267 sha256_update(&c, chain, ARGUS_DIGEST_LEN);
268 sha256_update(&c, b, sizeof b);
269 sha256_final(&c, chain);
270}
271
272void argus_finding_digest(const ArgusFinding *f, uint8_t out[ARGUS_DIGEST_LEN])
273{
274 uint8_t b[ARGUS_FINDING_WIRE_SIZE];
275 put16(b + 0, f->code);
276 b[2] = f->severity;
277 b[3] = f->confidence;
278 b[4] = f->sync_allowed;
279 b[5] = f->containment;
280 put16(b + 6, f->detector);
281 put64(b + 8, f->sequence);
282 put64(b + 16, f->prior_sequence);
283 put32(b + 24, f->principal);
284 put32(b + 28, f->cap_id);
285 put64(b + 32, f->cap_generation);
286 copy_bytes(b + 40, f->machine_id, ARGUS_MACHINE_ID_LEN);
287 copy_bytes(b + 72, f->event_digest, ARGUS_DIGEST_LEN);
288 sha256_hash(b, sizeof b, out);
289}
290
291/*
292 * v1.2 ContainmentRequest canonical encoding (spec section 2): the fields in struct order,
293 * little-endian, packed, ARGUS_CONTAIN_REQUEST_SIZE = 152 bytes. ArgusCapRef is 16 bytes in
294 * memory but 12 on the wire (cap_id u32 at 16, generation u64 at 20).
295 * off 0 u64 incident_id off 8 u8 containment off 9 u8 severity
296 * off 10 u16 finding_code off 12 u32 principal off 16 u32 target.cap_id
297 * off 20 u64 target.generation off 28 machine_id[32] off 60 finding_digest[32]
298 * off 92 u64 request_id off 100 u64 finding_sequence
299 * off 108 u32 target_object off 112 u32 target_rights off 116 target_digest[32]
300 * off 148 u16 flags off 150 u8 version off 151 u8 reserved end 152
301 * Encode and digest never validate. Decode checks version first (ARGUS_ERR_VERSION), then
302 * containment 1..ARGUS_CONTAIN_MAX, severity 1..ARGUS_SEV_CRITICAL, reserved 0, flags within
303 * ARGUS_CREQ_KNOWN, request_id != 0, finding_sequence != 0 (else ARGUS_ERR_MALFORMED); the
304 * output is untouched on error. Every byte is carried by a field, so a buffer that decodes
305 * re-encodes to exactly the same bytes. Request digest = SHA-256 of the 152 bytes.
306 */
307static void creq_encode_raw(const ArgusContainmentRequest *r, uint8_t o[ARGUS_CONTAIN_REQUEST_SIZE])
308{
309 put64(o + 0, r->incident_id);
310 o[8] = r->containment;
311 o[9] = r->severity;
312 put16(o + 10, r->finding_code);
313 put32(o + 12, r->principal);
314 put32(o + 16, r->target.cap_id);
315 put64(o + 20, r->target.generation);
316 copy_bytes(o + 28, r->machine_id, ARGUS_MACHINE_ID_LEN);
317 copy_bytes(o + 60, r->finding_digest, ARGUS_DIGEST_LEN);
318 put64(o + 92, r->request_id);
319 put64(o + 100, r->finding_sequence);
320 put32(o + 108, r->target_object);
321 put32(o + 112, r->target_rights);
322 copy_bytes(o + 116, r->target_digest, ARGUS_DIGEST_LEN);
323 put16(o + 148, r->flags);
324 o[150] = r->version;
325 o[151] = r->reserved;
326}
327
328int argus_contain_request_encode(const ArgusContainmentRequest *r, uint8_t out[ARGUS_CONTAIN_REQUEST_SIZE])
329{
330 if (!r || !out) return ARGUS_ERR_ARG;
331 creq_encode_raw(r, out);
332 return ARGUS_OK;
333}
334
335int argus_contain_request_decode(const uint8_t in[ARGUS_CONTAIN_REQUEST_SIZE], ArgusContainmentRequest *out)
336{
337 if (!in || !out) return ARGUS_ERR_ARG;
338 if (in[150] != ARGUS_CONTAIN_REQUEST_VERSION) return ARGUS_ERR_VERSION;
339 ArgusContainmentRequest r;
340 uint8_t *z = (uint8_t *)&r;
341 for (size_t i = 0; i < sizeof r; i++) z[i] = 0; /* padding deterministic (target, tail) */
342 r.incident_id = get64(in + 0);
343 r.containment = in[8];
344 r.severity = in[9];
345 r.finding_code = get16(in + 10);
346 r.principal = get32(in + 12);
347 r.target.cap_id = get32(in + 16);
348 r.target.generation = get64(in + 20);
349 copy_bytes(r.machine_id, in + 28, ARGUS_MACHINE_ID_LEN);
350 copy_bytes(r.finding_digest, in + 60, ARGUS_DIGEST_LEN);
351 r.request_id = get64(in + 92);
352 r.finding_sequence = get64(in + 100);
353 r.target_object = get32(in + 108);
354 r.target_rights = get32(in + 112);
355 copy_bytes(r.target_digest, in + 116, ARGUS_DIGEST_LEN);
356 r.flags = get16(in + 148);
357 r.version = in[150];
358 r.reserved = in[151];
359 if (r.containment < 1 || r.containment > ARGUS_CONTAIN_MAX) return ARGUS_ERR_MALFORMED;
360 if (r.severity < ARGUS_SEV_INFO || r.severity > ARGUS_SEV_CRITICAL) return ARGUS_ERR_MALFORMED;
361 if (r.reserved != 0) return ARGUS_ERR_MALFORMED;
362 if ((r.flags & (uint16_t)~ARGUS_CREQ_KNOWN) != 0) return ARGUS_ERR_MALFORMED;
363 if (r.request_id == 0 || r.finding_sequence == 0) return ARGUS_ERR_MALFORMED; /* I1.c */
364 *out = r;
365 return ARGUS_OK;
366}
367
368void argus_contain_request_digest(const ArgusContainmentRequest *r, uint8_t out[ARGUS_DIGEST_LEN])
369{
370 uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE];
371 creq_encode_raw(r, b);
372 sha256_hash(b, sizeof b, out);
373}