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 / stub_lanes_d.c

168 lines · 7753 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 * stub_lanes_d.c -- TEST-ONLY stand-ins for lane B (argus_event.c) and lane E
19 * (argus_detect.c), so lane D's tests link before those lanes merge. The real
20 * lanes replace this file at integration. The hash is FNV-1a spread over
21 * 32 bytes: deterministic, NOT cryptographic, NOT SHA-256.
22 *
23 * argus_detect_run modes (set stub_detect_mode from a test):
24 * 0 = tiny semantic detector using only the ops vtable (default)
25 * 1 = no findings
26 * 2 = emit as many canned findings as `cap` allows (overflow test)
27 * 3 = record what the ops said about ev->cap_id (pre-state probe), no findings
28 */
29#include "../argus_abi.h"
30#include "stub_lanes_d.h"
31
32#include <string.h>
33
34int stub_detect_mode = 0;
35int stub_probe_rc = 0;
36ArgusCapShadow stub_probe_cap;
37uint64_t stub_detect_calls = 0;
38
39static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }
40static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
41static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
42
43static void stub_encode(const ArgusEvent *ev, uint8_t b[ARGUS_EVENT_SIZE])
44{
45 b[0] = ev->version; b[1] = ev->class_; put16(b + 2, ev->kind);
46 b[4] = ev->effect_class; b[5] = ev->outcome; put16(b + 6, ev->flags);
47 put64(b + 8, ev->sequence); put64(b + 16, ev->tick);
48 put32(b + 24, ev->principal); put32(b + 28, (uint32_t)ev->code);
49 put32(b + 32, ev->cap_id); put32(b + 36, ev->object_id);
50 put64(b + 40, ev->cap_generation); put64(b + 48, ev->world_generation);
51 put64(b + 56, ev->resource);
52 memcpy(b + 64, ev->machine_id, ARGUS_MACHINE_ID_LEN);
53 memcpy(b + 96, ev->evidence_digest, ARGUS_DIGEST_LEN);
54}
55
56static void fnv32(const uint8_t *a, size_t na, const uint8_t *b, size_t nb, uint8_t out[ARGUS_DIGEST_LEN])
57{
58 for (int lane = 0; lane < 4; lane++) {
59 uint64_t h = 0xcbf29ce484222325ull ^ ((uint64_t)(lane + 1) * 0x9e3779b97f4a7c15ull);
60 for (size_t i = 0; i < na; i++) { h ^= a[i]; h *= 0x100000001b3ull; }
61 for (size_t i = 0; i < nb; i++) { h ^= b[i]; h *= 0x100000001b3ull; }
62 put64(out + 8 * lane, h);
63 }
64}
65
66static int kind_known(uint16_t k)
67{
68 switch (k) {
69 case 1: case 2: case 3: case 4: case 10: case 11: case 12: case 20: case 21: case 22:
70 case 30: case 31: case 32: case 40: case 41: case 42: case 43: case 50: case 51: case 52:
71 case 60: case 61: case 62: case 63: case 70: case 71: case 72: case 80: case 81:
72 case 90: case 91: case 92: case 93: /* v1.2 containment kinds (TEST-ONLY stand-in for lane B) */
73 return 1;
74 default:
75 return 0;
76 }
77}
78
79int argus_event_validate(const ArgusEvent *ev)
80{
81 if (!ev) return ARGUS_ERR_ARG;
82 if (ev->version != ARGUS_ABI_VERSION) return ARGUS_ERR_VERSION;
83 if (ev->class_ < ARGUS_CLASS_CRITICAL || ev->class_ > ARGUS_CLASS_MAX) return ARGUS_ERR_MALFORMED;
84 if (!kind_known(ev->kind)) return ARGUS_ERR_MALFORMED;
85 if (ev->effect_class > ARGUS_EFFECT_MAX) return ARGUS_ERR_MALFORMED;
86 if (ev->outcome < ARGUS_OUTCOME_OK || ev->outcome > ARGUS_OUTCOME_MAX) return ARGUS_ERR_MALFORMED;
87 if (ev->flags & (uint16_t)~ARGUS_FLAG_KNOWN) return ARGUS_ERR_MALFORMED;
88 if (ev->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY && ev->outcome != ARGUS_OUTCOME_OK) return ARGUS_ERR_MALFORMED; /* v1.1 */
89 /* v1.2 TEST-ONLY stand-in for lane B's validate (spec section 3), replace when argus_event.c
90 * lands: 90-93 class CRITICAL, effect NONE; CONSUMER flag on 90 always, never on 91-93. */
91 if (ev->kind >= ARGUS_EV_CONTAINMENT_PROPOSED && ev->kind <= ARGUS_EV_AUTHORITY_ESCALATED) {
92 if (ev->class_ != ARGUS_CLASS_CRITICAL || ev->effect_class != ARGUS_EFFECT_NONE) return ARGUS_ERR_MALFORMED;
93 if (((ev->flags & ARGUS_FLAG_CONSUMER) != 0) != (ev->kind == ARGUS_EV_CONTAINMENT_PROPOSED)) return ARGUS_ERR_MALFORMED;
94 }
95 return ARGUS_OK;
96}
97
98void argus_event_digest(const ArgusEvent *ev, uint8_t out[ARGUS_DIGEST_LEN])
99{
100 uint8_t b[ARGUS_EVENT_SIZE];
101 stub_encode(ev, b);
102 fnv32(b, sizeof b, NULL, 0, out);
103}
104
105void argus_chain_extend(uint8_t chain[ARGUS_DIGEST_LEN], const ArgusEvent *ev)
106{
107 uint8_t b[ARGUS_EVENT_SIZE], prev[ARGUS_DIGEST_LEN];
108 stub_encode(ev, b);
109 memcpy(prev, chain, ARGUS_DIGEST_LEN);
110 fnv32(prev, sizeof prev, b, sizeof b, chain);
111}
112
113void argus_finding_digest(const ArgusFinding *f, uint8_t out[ARGUS_DIGEST_LEN])
114{
115 fnv32((const uint8_t *)f, sizeof *f, NULL, 0, out);
116}
117
118static void mk(ArgusFinding *f, uint16_t code, uint8_t sev, uint8_t contain, const ArgusEvent *ev, uint64_t prior)
119{
120 memset(f, 0, sizeof *f);
121 f->code = code; f->severity = sev; f->confidence = ARGUS_CONF_DETERMINISTIC;
122 f->containment = contain; f->detector = code;
123 f->sequence = ev->sequence; f->prior_sequence = prior;
124 f->principal = ev->principal; f->cap_id = ev->cap_id; f->cap_generation = ev->cap_generation;
125 memcpy(f->machine_id, ev->machine_id, ARGUS_MACHINE_ID_LEN);
126}
127
128int argus_detect_run(const ArgusStateOps *ops, const ArgusStateView *v, const ArgusEvent *ev,
129 ArgusFinding *out, size_t cap, size_t *n_out)
130{
131 stub_detect_calls++;
132 *n_out = 0;
133 if (stub_detect_mode == 1)
134 return ARGUS_OK;
135 if (stub_detect_mode == 2) {
136 for (size_t i = 0; i < cap; i++)
137 mk(&out[i], ARGUS_F_FORGED_CAPABILITY, ARGUS_SEV_HIGH, ARGUS_CONTAIN_REVOKE_CAPABILITY, ev, 0);
138 *n_out = cap;
139 return ARGUS_OK;
140 }
141 if (stub_detect_mode == 3) {
142 memset(&stub_probe_cap, 0, sizeof stub_probe_cap);
143 stub_probe_rc = ops->cap(v, ev->cap_id, &stub_probe_cap);
144 return ARGUS_OK;
145 }
146 size_t n = 0;
147 if (ev->kind == ARGUS_EV_CAPABILITY_USED && ev->outcome == ARGUS_OUTCOME_OK && cap > 0) {
148 ArgusCapShadow s;
149 if (ops->cap(v, ev->cap_id, &s) != ARGUS_OK)
150 mk(&out[n++], ARGUS_F_FORGED_CAPABILITY, ARGUS_SEV_CRITICAL, ARGUS_CONTAIN_REVOKE_CAPABILITY, ev, 0);
151 else if (s.state == ARGUS_SHADOW_REVOKED)
152 mk(&out[n++], ARGUS_F_REVOKED_CAPABILITY_USED, ARGUS_SEV_CRITICAL, ARGUS_CONTAIN_REVOKE_CAPABILITY, ev, s.revoked_sequence);
153 else if (ev->cap_generation < s.generation)
154 mk(&out[n++], ARGUS_F_STALE_GENERATION, ARGUS_SEV_HIGH, ARGUS_CONTAIN_REVOKE_CAPABILITY, ev, s.granted_sequence);
155 }
156 if (ev->kind == ARGUS_EV_PROVIDER_USED && ev->outcome == ARGUS_OUTCOME_OK && n < cap) {
157 ArgusProviderShadow p;
158 if (ops->provider(v, ev->evidence_digest, &p) == ARGUS_OK && p.state == ARGUS_SHADOW_REVOKED)
159 mk(&out[n++], ARGUS_F_QUARANTINED_USE, ARGUS_SEV_HIGH, ARGUS_CONTAIN_QUARANTINE_PROVIDER, ev, p.sequence);
160 }
161 if (ev->kind == ARGUS_EV_ARTIFACT_ACTIVATED && ev->outcome == ARGUS_OUTCOME_OK && n < cap) {
162 ArgusArtifactShadow a;
163 if (ops->artifact(v, ev->evidence_digest, &a) != ARGUS_OK || a.state != ARGUS_SHADOW_LIVE)
164 mk(&out[n++], ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED, ARGUS_SEV_MEDIUM, ARGUS_CONTAIN_REJECT_ARTIFACT, ev, 0);
165 }
166 *n_out = n;
167 return ARGUS_OK;
168}