aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / argus / tests / stub_state.c
| 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_state.c -- test-only in-memory shadow state + deterministic corpora (lane E). |
| 19 | * |
| 20 | * Reference apply rules (stub_state_apply). They follow the apply rules and |
| 21 | * machine lifecycle fixed in argus_abi.h (4cd73cc) so that a stream that is |
| 22 | * clean against this stub is also clean against the real core (lane D). |
| 23 | * Only events with outcome OK change state, except ARTIFACT_REJECTED, which |
| 24 | * records the rejection at any outcome. |
| 25 | * CAPABILITY_GRANTED applies only when the slot is UNSEEN or cap_generation |
| 26 | * is strictly above the shadow generation: state LIVE, |
| 27 | * generation, subject = principal, rights = object_id, |
| 28 | * resource, granted_sequence. Otherwise not applied. |
| 29 | * CAPABILITY_REVOKED seen slot: state REVOKED, revoked_sequence. An UNSEEN |
| 30 | * slot creates no entry. |
| 31 | * CREDENTIAL_LEASE_CREATED new lease id only: LIVE, subject = principal, |
| 32 | * scope = resource, sequence. A LIVE or REVOKED id is |
| 33 | * not applied (lease ids are never reused, ruling A). |
| 34 | * CREDENTIAL_LEASE_REVOKED lease[object_id]: REVOKED, sequence. |
| 35 | * ARTIFACT_ADMITTED (code 0 only) LIVE, sequence; a rejection is sticky. |
| 36 | * ARTIFACT_REJECTED REJECTED, sequence. |
| 37 | * MACHINE_JOINED not currently joined: trust = the worse (by rank) of |
| 38 | * max(OBSERVED, object_id) and a tombstone's preserved |
| 39 | * trust; joined_sequence = changed_sequence = sequence. |
| 40 | * A joined machine is unchanged. |
| 41 | * MACHINE_TRUST_CHANGED joined machine, valid value (1..6), downward or equal |
| 42 | * by rank only: trust, changed_sequence. Unknown |
| 43 | * machines get no entry; upward moves are ignored. |
| 44 | * MACHINE_REMOVED tombstone: joined_sequence 0, trust preserved. |
| 45 | * PROVIDER_DISCOVERED new provider: LIVE; rediscovery never clears quarantine. |
| 46 | * PROVIDER_QUARANTINED provider: REVOKED (quarantined), sequence. |
| 47 | * WORLD_COMMITTED per store (object_id = store id, v1.1): adopted only |
| 48 | * when the store has no commit yet or world_generation == |
| 49 | * its shadow + 1; world_generation 0 is a no-op. |
| 50 | * POLICY_CHANGED / RUNTIME_BUILD_CHANGED expected digest = evidence_digest. |
| 51 | * |
| 52 | * The corpora also follow the validate rules of argus_abi.h: class never |
| 53 | * weaker than the per-kind minimum (stub_min_class below, a local copy of |
| 54 | * lane B's argus_event_min_class table until integration), no CONSUMER flag, |
| 55 | * cap_id < ARGUS_CAP_MAX, EXTERNAL_EFFECT_* with effect_class EXTERNAL. |
| 56 | */ |
| 57 | #include "stub_state.h" |
| 58 | #include "../argus_detect.h" |
| 59 | |
| 60 | #include <string.h> |
| 61 | |
| 62 | /* ---- lookups ---------------------------------------------------------------- */ |
| 63 | |
| 64 | static const StubState *S(const ArgusStateView *v) { return (const StubState *)(const void *)v; } |
| 65 | |
| 66 | static int op_cap(const ArgusStateView *v, uint32_t id, ArgusCapShadow *out) |
| 67 | { |
| 68 | const StubState *s = S(v); |
| 69 | for (size_t i = 0; i < s->n_caps; i++) |
| 70 | if (s->caps[i].cap_id == id) { *out = s->caps[i]; return ARGUS_OK; } |
| 71 | return ARGUS_ERR_STATE; |
| 72 | } |
| 73 | |
| 74 | static int op_machine(const ArgusStateView *v, const uint8_t id[ARGUS_MACHINE_ID_LEN], ArgusMachineShadow *out) |
| 75 | { |
| 76 | const StubState *s = S(v); |
| 77 | for (size_t i = 0; i < s->n_machines; i++) |
| 78 | if (memcmp(s->machines[i].machine_id, id, ARGUS_MACHINE_ID_LEN) == 0) { *out = s->machines[i]; return ARGUS_OK; } |
| 79 | return ARGUS_ERR_STATE; |
| 80 | } |
| 81 | |
| 82 | static int op_artifact(const ArgusStateView *v, const uint8_t d[ARGUS_DIGEST_LEN], ArgusArtifactShadow *out) |
| 83 | { |
| 84 | const StubState *s = S(v); |
| 85 | for (size_t i = 0; i < s->n_artifacts; i++) |
| 86 | if (memcmp(s->artifacts[i].digest, d, ARGUS_DIGEST_LEN) == 0) { *out = s->artifacts[i]; return ARGUS_OK; } |
| 87 | return ARGUS_ERR_STATE; |
| 88 | } |
| 89 | |
| 90 | static int op_lease(const ArgusStateView *v, uint32_t id, ArgusLeaseShadow *out) |
| 91 | { |
| 92 | const StubState *s = S(v); |
| 93 | for (size_t i = 0; i < s->n_leases; i++) |
| 94 | if (s->leases[i].lease_id == id) { *out = s->leases[i]; return ARGUS_OK; } |
| 95 | return ARGUS_ERR_STATE; |
| 96 | } |
| 97 | |
| 98 | static int op_provider(const ArgusStateView *v, const uint8_t id[ARGUS_DIGEST_LEN], ArgusProviderShadow *out) |
| 99 | { |
| 100 | const StubState *s = S(v); |
| 101 | for (size_t i = 0; i < s->n_providers; i++) |
| 102 | if (memcmp(s->providers[i].provider_id, id, ARGUS_DIGEST_LEN) == 0) { *out = s->providers[i]; return ARGUS_OK; } |
| 103 | return ARGUS_ERR_STATE; |
| 104 | } |
| 105 | |
| 106 | static int op_world(const ArgusStateView *v, uint32_t store_id, ArgusWorldShadow *out) |
| 107 | { |
| 108 | const StubState *s = S(v); |
| 109 | for (size_t i = 0; i < s->n_worlds; i++) |
| 110 | if (s->worlds[i].store_id == store_id) { |
| 111 | if (s->worlds[i].generation == 0 && s->worlds[i].sequence == 0) |
| 112 | return ARGUS_ERR_STATE; |
| 113 | *out = s->worlds[i]; |
| 114 | return ARGUS_OK; |
| 115 | } |
| 116 | return ARGUS_ERR_STATE; |
| 117 | } |
| 118 | |
| 119 | static int op_policy(const ArgusStateView *v, uint8_t out[ARGUS_DIGEST_LEN]) |
| 120 | { |
| 121 | memcpy(out, S(v)->policy, ARGUS_DIGEST_LEN); |
| 122 | return ARGUS_OK; |
| 123 | } |
| 124 | |
| 125 | static int op_runtime(const ArgusStateView *v, uint8_t out[ARGUS_DIGEST_LEN]) |
| 126 | { |
| 127 | memcpy(out, S(v)->runtime, ARGUS_DIGEST_LEN); |
| 128 | return ARGUS_OK; |
| 129 | } |
| 130 | |
| 131 | static const ArgusStateOps stub_ops = { |
| 132 | op_cap, op_machine, op_artifact, op_lease, op_provider, op_world, op_policy, op_runtime, |
| 133 | }; |
| 134 | |
| 135 | void stub_state_init(StubState *s) { memset(s, 0, sizeof *s); } |
| 136 | const ArgusStateOps *stub_state_ops(void) { return &stub_ops; } |
| 137 | const ArgusStateView *stub_state_view(const StubState *s) { return (const ArgusStateView *)(const void *)s; } |
| 138 | |
| 139 | /* ---- population ------------------------------------------------------------- */ |
| 140 | |
| 141 | int stub_put_cap(StubState *s, const ArgusCapShadow *c) |
| 142 | { |
| 143 | for (size_t i = 0; i < s->n_caps; i++) |
| 144 | if (s->caps[i].cap_id == c->cap_id) { s->caps[i] = *c; return ARGUS_OK; } |
| 145 | if (s->n_caps >= STUB_MAX_CAPS) return ARGUS_ERR_FULL; |
| 146 | s->caps[s->n_caps++] = *c; |
| 147 | return ARGUS_OK; |
| 148 | } |
| 149 | |
| 150 | int stub_put_machine(StubState *s, const ArgusMachineShadow *m) |
| 151 | { |
| 152 | for (size_t i = 0; i < s->n_machines; i++) |
| 153 | if (memcmp(s->machines[i].machine_id, m->machine_id, ARGUS_MACHINE_ID_LEN) == 0) { s->machines[i] = *m; return ARGUS_OK; } |
| 154 | if (s->n_machines >= STUB_MAX_MACHINES) return ARGUS_ERR_FULL; |
| 155 | s->machines[s->n_machines++] = *m; |
| 156 | return ARGUS_OK; |
| 157 | } |
| 158 | |
| 159 | int stub_put_artifact(StubState *s, const ArgusArtifactShadow *a) |
| 160 | { |
| 161 | for (size_t i = 0; i < s->n_artifacts; i++) |
| 162 | if (memcmp(s->artifacts[i].digest, a->digest, ARGUS_DIGEST_LEN) == 0) { s->artifacts[i] = *a; return ARGUS_OK; } |
| 163 | if (s->n_artifacts >= STUB_MAX_ARTIFACTS) return ARGUS_ERR_FULL; |
| 164 | s->artifacts[s->n_artifacts++] = *a; |
| 165 | return ARGUS_OK; |
| 166 | } |
| 167 | |
| 168 | int stub_put_lease(StubState *s, const ArgusLeaseShadow *l) |
| 169 | { |
| 170 | for (size_t i = 0; i < s->n_leases; i++) |
| 171 | if (s->leases[i].lease_id == l->lease_id) { s->leases[i] = *l; return ARGUS_OK; } |
| 172 | if (s->n_leases >= STUB_MAX_LEASES) return ARGUS_ERR_FULL; |
| 173 | s->leases[s->n_leases++] = *l; |
| 174 | return ARGUS_OK; |
| 175 | } |
| 176 | |
| 177 | int stub_put_provider(StubState *s, const ArgusProviderShadow *p) |
| 178 | { |
| 179 | for (size_t i = 0; i < s->n_providers; i++) |
| 180 | if (memcmp(s->providers[i].provider_id, p->provider_id, ARGUS_DIGEST_LEN) == 0) { s->providers[i] = *p; return ARGUS_OK; } |
| 181 | if (s->n_providers >= STUB_MAX_PROVIDERS) return ARGUS_ERR_FULL; |
| 182 | s->providers[s->n_providers++] = *p; |
| 183 | return ARGUS_OK; |
| 184 | } |
| 185 | |
| 186 | int stub_set_world(StubState *s, uint32_t store_id, uint64_t generation, const uint8_t digest[ARGUS_DIGEST_LEN], uint64_t sequence) |
| 187 | { |
| 188 | size_t i = 0; |
| 189 | while (i < s->n_worlds && s->worlds[i].store_id != store_id) |
| 190 | i++; |
| 191 | if (i == s->n_worlds) { |
| 192 | if (s->n_worlds >= ARGUS_WORLD_STORES) return ARGUS_ERR_FULL; |
| 193 | s->n_worlds++; |
| 194 | } |
| 195 | s->worlds[i].store_id = store_id; |
| 196 | s->worlds[i].generation = generation; |
| 197 | memcpy(s->worlds[i].digest, digest, ARGUS_DIGEST_LEN); |
| 198 | s->worlds[i].sequence = sequence; |
| 199 | return ARGUS_OK; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /* ---- per-kind minimum class (local copy of lane B's table) ----------------- * |
| 204 | * CRITICAL: 4, 12, 21, 31, 32, 42, 52, 60-63, 71, 72, 80 |
| 205 | * SECURITY: 1, 3, 10, 20, 22, 30, 40, 41, 50, 51, 70 |
| 206 | * AUDIT: 2, 11, 43, 81 |
| 207 | * Anything else (not a kind in ABI v1): 0, as in lane B's table. */ |
| 208 | uint8_t stub_min_class(uint16_t kind) |
| 209 | { |
| 210 | switch (kind) { |
| 211 | case ARGUS_EV_CAPABILITY_REVOKED: |
| 212 | case ARGUS_EV_CREDENTIAL_LEASE_REVOKED: |
| 213 | case ARGUS_EV_ARTIFACT_REJECTED: |
| 214 | case ARGUS_EV_MACHINE_TRUST_CHANGED: |
| 215 | case ARGUS_EV_MACHINE_REMOVED: |
| 216 | case ARGUS_EV_PROVIDER_QUARANTINED: |
| 217 | case ARGUS_EV_EXTERNAL_EFFECT_COMMITTED: |
| 218 | case ARGUS_EV_INTEGRITY_VIOLATION: |
| 219 | case ARGUS_EV_SIGNATURE_FAILURE: |
| 220 | case ARGUS_EV_STALE_GENERATION: |
| 221 | case ARGUS_EV_FORGED_CAPABILITY: |
| 222 | case ARGUS_EV_POLICY_CHANGED: |
| 223 | case ARGUS_EV_RUNTIME_BUILD_CHANGED: |
| 224 | case ARGUS_EV_TELEMETRY_DROPPED: |
| 225 | return ARGUS_CLASS_CRITICAL; |
| 226 | case ARGUS_EV_CAPABILITY_GRANTED: |
| 227 | case ARGUS_EV_CAPABILITY_DENIED: |
| 228 | case ARGUS_EV_CREDENTIAL_LEASE_CREATED: |
| 229 | case ARGUS_EV_ARTIFACT_ADMITTED: |
| 230 | case ARGUS_EV_ARTIFACT_ACTIVATED: |
| 231 | case ARGUS_EV_MACHINE_JOINED: |
| 232 | case ARGUS_EV_PROVIDER_DISCOVERED: |
| 233 | case ARGUS_EV_PROVIDER_CHANGED: |
| 234 | case ARGUS_EV_EXTERNAL_EFFECT_REQUESTED: |
| 235 | case ARGUS_EV_EXTERNAL_EFFECT_DENIED: |
| 236 | case ARGUS_EV_WORLD_COMMITTED: |
| 237 | return ARGUS_CLASS_SECURITY; |
| 238 | case ARGUS_EV_CAPABILITY_USED: |
| 239 | case ARGUS_EV_CREDENTIAL_LEASE_USED: |
| 240 | case ARGUS_EV_PROVIDER_USED: |
| 241 | case ARGUS_EV_CAPABILITY_USE_SUMMARY: |
| 242 | return ARGUS_CLASS_AUDIT; |
| 243 | default: |
| 244 | return 0; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static int rank_or(uint32_t trust, int dflt) |
| 249 | { |
| 250 | int r = argus_trust_rank(trust); |
| 251 | return r < 0 ? dflt : r; |
| 252 | } |
| 253 | |
| 254 | int stub_state_apply(StubState *s, const ArgusEvent *ev) |
| 255 | { |
| 256 | const StubState *cs = s; |
| 257 | const ArgusStateView *v = stub_state_view(cs); |
| 258 | if (ev->kind == ARGUS_EV_ARTIFACT_REJECTED) { |
| 259 | ArgusArtifactShadow a = {0}; |
| 260 | memcpy(a.digest, ev->evidence_digest, ARGUS_DIGEST_LEN); |
| 261 | a.state = ARGUS_SHADOW_REJECTED; |
| 262 | a.sequence = ev->sequence; |
| 263 | return stub_put_artifact(s, &a); |
| 264 | } |
| 265 | if (ev->outcome != ARGUS_OUTCOME_OK) |
| 266 | return ARGUS_OK; |
| 267 | switch (ev->kind) { |
| 268 | case ARGUS_EV_CAPABILITY_GRANTED: { |
| 269 | if (ev->cap_id == ARGUS_CAP_NONE) return ARGUS_OK; /* no capability: no entry */ |
| 270 | ArgusCapShadow c = {0}; |
| 271 | int seen = op_cap(v, ev->cap_id, &c) == ARGUS_OK && c.state != ARGUS_SHADOW_UNSEEN; |
| 272 | if (seen && ev->cap_generation <= c.generation) return ARGUS_OK; /* replay: not applied */ |
| 273 | memset(&c, 0, sizeof c); |
| 274 | c.cap_id = ev->cap_id; |
| 275 | c.generation = ev->cap_generation; |
| 276 | c.state = ARGUS_SHADOW_LIVE; |
| 277 | c.subject = ev->principal; |
| 278 | c.rights = (uint32_t)ev->object_id; |
| 279 | c.resource = ev->resource; |
| 280 | c.granted_sequence = ev->sequence; |
| 281 | return stub_put_cap(s, &c); |
| 282 | } |
| 283 | case ARGUS_EV_CAPABILITY_REVOKED: { |
| 284 | ArgusCapShadow c; |
| 285 | if (op_cap(v, ev->cap_id, &c) != ARGUS_OK || c.state == ARGUS_SHADOW_UNSEEN) return ARGUS_OK; |
| 286 | c.state = ARGUS_SHADOW_REVOKED; |
| 287 | c.revoked_sequence = ev->sequence; |
| 288 | return stub_put_cap(s, &c); |
| 289 | } |
| 290 | case ARGUS_EV_CREDENTIAL_LEASE_CREATED: { |
| 291 | ArgusLeaseShadow l = {0}; |
| 292 | if (op_lease(v, ev->object_id, &l) == ARGUS_OK && l.state != ARGUS_SHADOW_UNSEEN) return ARGUS_OK; |
| 293 | memset(&l, 0, sizeof l); |
| 294 | l.lease_id = ev->object_id; l.subject = ev->principal; l.scope = ev->resource; |
| 295 | l.state = ARGUS_SHADOW_LIVE; l.sequence = ev->sequence; |
| 296 | return stub_put_lease(s, &l); |
| 297 | } |
| 298 | case ARGUS_EV_CREDENTIAL_LEASE_REVOKED: { |
| 299 | ArgusLeaseShadow l; |
| 300 | if (op_lease(v, ev->object_id, &l) != ARGUS_OK) return ARGUS_OK; |
| 301 | l.state = ARGUS_SHADOW_REVOKED; l.sequence = ev->sequence; |
| 302 | return stub_put_lease(s, &l); |
| 303 | } |
| 304 | case ARGUS_EV_ARTIFACT_ADMITTED: { |
| 305 | if (ev->code != 0) return ARGUS_OK; |
| 306 | ArgusArtifactShadow a = {0}; |
| 307 | if (op_artifact(v, ev->evidence_digest, &a) == ARGUS_OK && a.state == ARGUS_SHADOW_REJECTED) return ARGUS_OK; |
| 308 | memset(&a, 0, sizeof a); |
| 309 | memcpy(a.digest, ev->evidence_digest, ARGUS_DIGEST_LEN); |
| 310 | a.state = ARGUS_SHADOW_LIVE; a.sequence = ev->sequence; |
| 311 | return stub_put_artifact(s, &a); |
| 312 | } |
| 313 | case ARGUS_EV_MACHINE_JOINED: { |
| 314 | ArgusMachineShadow m = {0}; |
| 315 | int found = op_machine(v, ev->machine_id, &m) == ARGUS_OK; |
| 316 | if (found && m.joined_sequence != 0) return ARGUS_OK; /* already joined */ |
| 317 | int obs = argus_trust_rank(ARGUS_TRUST_OBSERVED); |
| 318 | uint32_t trust = ARGUS_TRUST_OBSERVED; |
| 319 | if (rank_or(ev->object_id, obs) > obs) trust = ev->object_id; /* capped at OBSERVED */ |
| 320 | if (found && rank_or(m.trust, obs) > rank_or(trust, obs)) trust = m.trust; /* tombstone keeps worse */ |
| 321 | memcpy(m.machine_id, ev->machine_id, ARGUS_MACHINE_ID_LEN); |
| 322 | m.trust = trust; |
| 323 | m.joined_sequence = m.changed_sequence = ev->sequence; |
| 324 | return stub_put_machine(s, &m); |
| 325 | } |
| 326 | case ARGUS_EV_MACHINE_TRUST_CHANGED: { |
| 327 | ArgusMachineShadow m; |
| 328 | if (op_machine(v, ev->machine_id, &m) != ARGUS_OK || m.joined_sequence == 0) return ARGUS_OK; |
| 329 | int to = argus_trust_rank(ev->object_id); |
| 330 | if (to < 0 || to < rank_or(m.trust, argus_trust_rank(ARGUS_TRUST_OBSERVED))) return ARGUS_OK; /* upward: ignored */ |
| 331 | m.trust = ev->object_id; m.changed_sequence = ev->sequence; |
| 332 | return stub_put_machine(s, &m); |
| 333 | } |
| 334 | case ARGUS_EV_MACHINE_REMOVED: { |
| 335 | ArgusMachineShadow m; |
| 336 | if (op_machine(v, ev->machine_id, &m) != ARGUS_OK) return ARGUS_OK; |
| 337 | m.joined_sequence = 0; /* tombstone, trust kept */ |
| 338 | return stub_put_machine(s, &m); |
| 339 | } |
| 340 | case ARGUS_EV_PROVIDER_DISCOVERED: |
| 341 | case ARGUS_EV_PROVIDER_QUARANTINED: { |
| 342 | ArgusProviderShadow p = {0}; |
| 343 | int found = op_provider(v, ev->evidence_digest, &p) == ARGUS_OK; |
| 344 | if (ev->kind == ARGUS_EV_PROVIDER_DISCOVERED && found) return ARGUS_OK; /* rediscovery changes nothing */ |
| 345 | memcpy(p.provider_id, ev->evidence_digest, ARGUS_DIGEST_LEN); |
| 346 | p.state = ev->kind == ARGUS_EV_PROVIDER_DISCOVERED ? ARGUS_SHADOW_LIVE : ARGUS_SHADOW_REVOKED; |
| 347 | p.sequence = ev->sequence; |
| 348 | return stub_put_provider(s, &p); |
| 349 | } |
| 350 | case ARGUS_EV_WORLD_COMMITTED: { /* per store: object_id = store id (v1.1) */ |
| 351 | if (ev->world_generation == 0) return ARGUS_OK; |
| 352 | ArgusWorldShadow w = {0}; |
| 353 | int known = op_world(v, ev->object_id, &w) == ARGUS_OK; |
| 354 | if (!known || w.generation == 0 || |
| 355 | (w.generation != UINT64_MAX && ev->world_generation == w.generation + 1u)) |
| 356 | return stub_set_world(s, ev->object_id, ev->world_generation, ev->evidence_digest, ev->sequence); |
| 357 | return ARGUS_OK; |
| 358 | } |
| 359 | case ARGUS_EV_POLICY_CHANGED: |
| 360 | memcpy(s->policy, ev->evidence_digest, ARGUS_DIGEST_LEN); |
| 361 | return ARGUS_OK; |
| 362 | case ARGUS_EV_RUNTIME_BUILD_CHANGED: |
| 363 | memcpy(s->runtime, ev->evidence_digest, ARGUS_DIGEST_LEN); |
| 364 | return ARGUS_OK; |
| 365 | default: |
| 366 | return ARGUS_OK; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* ---- corpus generator ------------------------------------------------------- * |
| 371 | * One generator keeps its own model of the legal state and only emits events |
| 372 | * that are legal against it (and against the argus_abi.h apply/validate rules). |
| 373 | * The hostile variant injects one violation every ARGUS_CORPUS_HOSTILE_EVERY |
| 374 | * events; each injection is built so exactly one detector fires. An injection |
| 375 | * that the core does not apply (world skip, trust escalation, lease re-create) |
| 376 | * leaves the model unchanged; one that it applies (a capped JOINED) is |
| 377 | * followed by the model, so later legal events stay legal. |
| 378 | * Cap/right/result numbers below mirror native/capability (see argus_detect.h). */ |
| 379 | |
| 380 | #define G_SLOTS 48 /* cap ids 0..47 (v1.1: 0 = the office slot, a real cap); ids 200..255 are never granted */ |
| 381 | #define G_UNSEEN_CAP 200u |
| 382 | #define G_LEASES 12 /* concurrent lease slots */ |
| 383 | #define G_LEASE_IDS 56 /* distinct lease ids per stream, below the core's 64-entry table */ |
| 384 | #define G_MACHINES 5 /* M0..M3 always joined; M4 cycles join/remove */ |
| 385 | #define G_PROVIDERS 4 |
| 386 | #define G_ARTIFACTS 96 /* ruling B: core artifact table is 256 */ |
| 387 | #define G_QM 3 /* machine quarantined from 2/5 of the day on (no legal release in v1) */ |
| 388 | #define G_QP 2 /* provider quarantined in the second half */ |
| 389 | #define G_DOWN 1 /* machine whose trust steps down during the day */ |
| 390 | #define G_STREAMS 3 /* v1.1: producer stream ids 0..2, every machine uses all of them */ |
| 391 | #define G_STORES 2 /* v1.1: World stores committed interleaved */ |
| 392 | static const uint32_t g_store_id[G_STORES] = { 0, 5 }; |
| 393 | |
| 394 | #define R_READ 0x1u |
| 395 | #define R_WRITE 0x2u |
| 396 | #define R_EFFECT 0x4u |
| 397 | #define C_ERR_BOUNDS (-1) |
| 398 | #define C_ERR_STALE_GEN (-2) |
| 399 | #define C_ERR_REVOKED (-3) |
| 400 | #define C_ERR_SUBJECT (-5) |
| 401 | #define C_ERR_RIGHTS (-7) |
| 402 | #define C_ERR_UNAUTHORIZED (-15) |
| 403 | |
| 404 | typedef struct { |
| 405 | uint64_t rng; |
| 406 | ArgusEvent *out; |
| 407 | size_t max, n; |
| 408 | struct { uint64_t gen; uint32_t state; uint32_t subject; uint32_t rights; uint64_t resource; } slot[G_SLOTS]; |
| 409 | struct { uint32_t id; uint32_t state; uint32_t subject; uint64_t scope; } lease[G_LEASES]; |
| 410 | uint32_t next_lease_id; |
| 411 | uint8_t machine[G_MACHINES][ARGUS_MACHINE_ID_LEN]; |
| 412 | int m_joined[G_MACHINES]; |
| 413 | uint32_t m_trust[G_MACHINES]; |
| 414 | uint8_t provider[G_PROVIDERS][ARGUS_DIGEST_LEN]; |
| 415 | int p_quar[G_PROVIDERS]; |
| 416 | uint8_t artifact[G_ARTIFACTS][ARGUS_DIGEST_LEN]; |
| 417 | uint32_t a_state[G_ARTIFACTS]; |
| 418 | size_t n_art; |
| 419 | uint64_t world_gen[G_STORES]; |
| 420 | uint8_t world_digest[G_STORES][ARGUS_DIGEST_LEN]; |
| 421 | uint8_t policy[ARGUS_DIGEST_LEN]; |
| 422 | uint8_t runtime[ARGUS_DIGEST_LEN]; |
| 423 | int policy_tampered; |
| 424 | uint64_t digest_ctr; |
| 425 | int m3_quarantined_done, p2_quarantined_done; |
| 426 | /* hostile */ |
| 427 | int hostile; |
| 428 | ArgusExpectedFinding *expect; |
| 429 | size_t expect_max, n_expect; |
| 430 | size_t next_inject; |
| 431 | unsigned inject_type; |
| 432 | } Gen; |
| 433 | |
| 434 | static uint64_t rnd(Gen *g) |
| 435 | { |
| 436 | uint64_t z = (g->rng += 0x9E3779B97F4A7C15ull); |
| 437 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 438 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 439 | return z ^ (z >> 31); |
| 440 | } |
| 441 | |
| 442 | static uint32_t rn(Gen *g, uint32_t n) { return (uint32_t)(rnd(g) % n); } |
| 443 | |
| 444 | /* Deterministic, unique 32-byte value (tag keeps namespaces apart). */ |
| 445 | static void fresh_digest(Gen *g, uint8_t tag, uint8_t out[ARGUS_DIGEST_LEN]) |
| 446 | { |
| 447 | uint64_t c = ++g->digest_ctr; |
| 448 | for (size_t i = 0; i < ARGUS_DIGEST_LEN; i++) { |
| 449 | uint64_t z = c * 0x9E3779B97F4A7C15ull + i * 0xD1B54A32D192ED03ull + tag; |
| 450 | z = (z ^ (z >> 29)) * 0xBF58476D1CE4E5B9ull; |
| 451 | out[i] = (uint8_t)(z >> 32); |
| 452 | } |
| 453 | out[0] = tag; |
| 454 | out[1] = (uint8_t)c; |
| 455 | out[2] = (uint8_t)(c >> 8); |
| 456 | } |
| 457 | |
| 458 | static ArgusEvent *emit_ev(Gen *g, uint16_t kind, uint8_t outcome, int32_t code) |
| 459 | { |
| 460 | if (g->n >= g->max) |
| 461 | return NULL; |
| 462 | ArgusEvent *e = &g->out[g->n++]; |
| 463 | memset(e, 0, sizeof *e); |
| 464 | e->version = ARGUS_ABI_VERSION; |
| 465 | e->class_ = stub_min_class(kind); |
| 466 | e->kind = kind; |
| 467 | e->outcome = outcome; |
| 468 | e->code = code; |
| 469 | e->cap_id = ARGUS_CAP_NONE; /* v1.1: no capability unless fill_cap sets one (0 is a real slot) */ |
| 470 | /* v1.1: stream id in flag bits 2-15. The sequence stays the global index, so it is |
| 471 | * strictly increasing within every (machine, stream) as well (gaps are legal). */ |
| 472 | e->flags = (uint16_t)(ARGUS_FLAG_SYNTHETIC | ((g->n % G_STREAMS) << ARGUS_FLAG_STREAM_SHIFT)); |
| 473 | e->sequence = (uint64_t)g->n; /* 1-based, strictly increasing */ |
| 474 | /* hot-path rule: USED and summaries carry tick 0 (no clock read on validate) */ |
| 475 | e->tick = (kind == ARGUS_EV_CAPABILITY_USED || kind == ARGUS_EV_CAPABILITY_USE_SUMMARY) ? 0 : (uint64_t)g->n; |
| 476 | return e; |
| 477 | } |
| 478 | |
| 479 | /* Attribute to a joined, non-quarantined machine. Every corpus event carries a |
| 480 | * machine_id (M0..M2 are always joined and never quarantined). */ |
| 481 | static void attach_ok(Gen *g, ArgusEvent *e) |
| 482 | { |
| 483 | uint32_t start = rn(g, G_MACHINES); |
| 484 | for (uint32_t k = 0; k < G_MACHINES; k++) { |
| 485 | uint32_t i = (start + k) % G_MACHINES; |
| 486 | if (g->m_joined[i] && g->m_trust[i] != ARGUS_TRUST_QUARANTINED) { |
| 487 | memcpy(e->machine_id, g->machine[i], ARGUS_MACHINE_ID_LEN); |
| 488 | return; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static int find_slot(Gen *g, uint32_t state, uint32_t need_rights, uint32_t lack_rights, uint64_t min_gen) |
| 494 | { |
| 495 | uint32_t start = rn(g, G_SLOTS); |
| 496 | for (uint32_t k = 0; k < G_SLOTS; k++) { |
| 497 | uint32_t i = (start + k) % G_SLOTS; |
| 498 | if (g->slot[i].state == state && (g->slot[i].rights & need_rights) == need_rights && |
| 499 | (g->slot[i].rights & lack_rights) == 0 && g->slot[i].gen >= min_gen) |
| 500 | return (int)i; |
| 501 | } |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | static void fill_cap(ArgusEvent *e, Gen *g, int i, uint64_t gen) |
| 506 | { |
| 507 | e->cap_id = (uint32_t)i; /* slot 0 = cap 0, the office capability */ |
| 508 | e->cap_generation = gen; |
| 509 | e->principal = g->slot[i].subject; |
| 510 | e->resource = g->slot[i].resource; |
| 511 | } |
| 512 | |
| 513 | /* ---- legal operations ---- */ |
| 514 | |
| 515 | static void grant_slot(Gen *g, uint32_t i) |
| 516 | { |
| 517 | ArgusEvent *e = emit_ev(g, ARGUS_EV_CAPABILITY_GRANTED, ARGUS_OUTCOME_OK, 0); |
| 518 | if (!e) return; |
| 519 | g->slot[i].gen += 1; /* strictly above the last grant of this slot */ |
| 520 | g->slot[i].state = ARGUS_SHADOW_LIVE; |
| 521 | g->slot[i].subject = 1u + rn(g, 8); |
| 522 | g->slot[i].rights = R_READ | (rn(g, 4) ? R_WRITE : 0u) | (rn(g, 2) ? R_EFFECT : 0u); |
| 523 | g->slot[i].resource = 0x1000u + i; |
| 524 | fill_cap(e, g, (int)i, g->slot[i].gen); |
| 525 | e->object_id = g->slot[i].rights; |
| 526 | attach_ok(g, e); |
| 527 | } |
| 528 | |
| 529 | static void gop_grant(Gen *g) |
| 530 | { |
| 531 | uint32_t start = rn(g, G_SLOTS); |
| 532 | for (uint32_t k = 0; k < G_SLOTS; k++) { |
| 533 | uint32_t i = (start + k) % G_SLOTS; |
| 534 | if (g->slot[i].state == ARGUS_SHADOW_LIVE) |
| 535 | continue; |
| 536 | grant_slot(g, i); |
| 537 | return; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static void gop_use(Gen *g) |
| 542 | { |
| 543 | int i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0); |
| 544 | if (i < 0) { gop_grant(g); return; } |
| 545 | ArgusEvent *e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0); |
| 546 | if (!e) return; |
| 547 | fill_cap(e, g, i, g->slot[i].gen); |
| 548 | if ((g->slot[i].rights & R_EFFECT) && rn(g, 3) == 0) |
| 549 | e->effect_class = ARGUS_EFFECT_EXTERNAL; |
| 550 | else if ((g->slot[i].rights & R_WRITE) && rn(g, 4) == 0) |
| 551 | e->effect_class = ARGUS_EFFECT_EVIDENCE; |
| 552 | else |
| 553 | e->effect_class = ARGUS_EFFECT_EPHEMERAL; |
| 554 | attach_ok(g, e); |
| 555 | } |
| 556 | |
| 557 | /* v1.1 hot path: a producer thread's flushed use table, one summary per touched |
| 558 | * slot. Legal only for a LIVE slot by its subject, with min == max == the current |
| 559 | * generation: the producer flushes a slot's pending summary BEFORE it emits a |
| 560 | * transition (REVOKED/GRANTED) for that slot, so a summary never spans a |
| 561 | * revocation or a re-grant (see the header proposal in the lane report). */ |
| 562 | static void gop_summary(Gen *g) |
| 563 | { |
| 564 | int i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0); |
| 565 | if (i < 0) { gop_grant(g); return; } |
| 566 | ArgusEvent *e = emit_ev(g, ARGUS_EV_CAPABILITY_USE_SUMMARY, ARGUS_OUTCOME_OK, 0); |
| 567 | if (!e) return; |
| 568 | fill_cap(e, g, i, g->slot[i].gen); |
| 569 | e->world_generation = g->slot[i].gen; /* MIN generation seen (v1.1: 64-bit); object_id 0 */ |
| 570 | e->resource = 1u + rn(g, 4096); /* count */ |
| 571 | e->effect_class = ARGUS_EFFECT_NONE; |
| 572 | attach_ok(g, e); |
| 573 | } |
| 574 | |
| 575 | static void gop_denied(Gen *g) |
| 576 | { |
| 577 | int i; |
| 578 | ArgusEvent *e; |
| 579 | switch (rn(g, 6)) { |
| 580 | case 0: /* correct refusal of a revoked reference */ |
| 581 | if ((i = find_slot(g, ARGUS_SHADOW_REVOKED, 0, 0, 1)) >= 0) { |
| 582 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, C_ERR_REVOKED))) return; |
| 583 | fill_cap(e, g, i, g->slot[i].gen); |
| 584 | attach_ok(g, e); |
| 585 | return; |
| 586 | } |
| 587 | break; |
| 588 | case 1: /* correct refusal of an old generation */ |
| 589 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 2)) >= 0) { |
| 590 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, C_ERR_STALE_GEN))) return; |
| 591 | fill_cap(e, g, i, g->slot[i].gen - 1); |
| 592 | attach_ok(g, e); |
| 593 | return; |
| 594 | } |
| 595 | break; |
| 596 | case 3: /* wrong subject refused */ |
| 597 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) >= 0) { |
| 598 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_DENIED, ARGUS_OUTCOME_DENIED, C_ERR_SUBJECT))) return; |
| 599 | fill_cap(e, g, i, g->slot[i].gen); |
| 600 | e->principal += 100u; |
| 601 | attach_ok(g, e); |
| 602 | return; |
| 603 | } |
| 604 | break; |
| 605 | case 4: /* bogus (never granted, in range) reference refused by bounds */ |
| 606 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, C_ERR_BOUNDS))) return; |
| 607 | e->cap_id = G_UNSEEN_CAP + rn(g, ARGUS_CAP_MAX - G_UNSEEN_CAP); |
| 608 | e->cap_generation = 1; |
| 609 | e->principal = 1u + rn(g, 8); |
| 610 | attach_ok(g, e); |
| 611 | return; |
| 612 | case 5: /* quarantined machine tries and is refused */ |
| 613 | if (g->m_trust[G_QM] == ARGUS_TRUST_QUARANTINED && (i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) >= 0) { |
| 614 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_DENIED, ARGUS_OUTCOME_DENIED, C_ERR_UNAUTHORIZED))) return; |
| 615 | fill_cap(e, g, i, g->slot[i].gen); |
| 616 | memcpy(e->machine_id, g->machine[G_QM], ARGUS_MACHINE_ID_LEN); |
| 617 | return; |
| 618 | } |
| 619 | break; |
| 620 | default: |
| 621 | break; |
| 622 | } |
| 623 | /* rights refused on a live reference */ |
| 624 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) < 0) { gop_grant(g); return; } |
| 625 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_DENIED, C_ERR_RIGHTS))) return; |
| 626 | fill_cap(e, g, i, g->slot[i].gen); |
| 627 | attach_ok(g, e); |
| 628 | } |
| 629 | |
| 630 | static void gop_revoke(Gen *g) |
| 631 | { |
| 632 | int i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0); |
| 633 | if (i < 0) { gop_grant(g); return; } |
| 634 | ArgusEvent *e = emit_ev(g, ARGUS_EV_CAPABILITY_REVOKED, ARGUS_OUTCOME_OK, 0); |
| 635 | if (!e) return; |
| 636 | fill_cap(e, g, i, g->slot[i].gen); /* at the granted generation */ |
| 637 | attach_ok(g, e); |
| 638 | g->slot[i].state = ARGUS_SHADOW_REVOKED; |
| 639 | } |
| 640 | |
| 641 | static void gop_effect(Gen *g) |
| 642 | { |
| 643 | ArgusEvent *e; |
| 644 | int i = find_slot(g, ARGUS_SHADOW_LIVE, R_EFFECT, 0, 0); |
| 645 | if (i >= 0 && rn(g, 3) != 0) { |
| 646 | if (!(e = emit_ev(g, ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_OUTCOME_OK, 0))) return; |
| 647 | fill_cap(e, g, i, g->slot[i].gen); |
| 648 | e->effect_class = ARGUS_EFFECT_EXTERNAL; |
| 649 | attach_ok(g, e); |
| 650 | if (!(e = emit_ev(g, ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0))) return; |
| 651 | fill_cap(e, g, i, g->slot[i].gen); |
| 652 | e->effect_class = ARGUS_EFFECT_EXTERNAL; |
| 653 | attach_ok(g, e); |
| 654 | return; |
| 655 | } |
| 656 | /* a reference without the effect right asks and is refused */ |
| 657 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, R_EFFECT, 0)) < 0) { gop_use(g); return; } |
| 658 | uint16_t kind = rn(g, 2) ? ARGUS_EV_EXTERNAL_EFFECT_REQUESTED : ARGUS_EV_EXTERNAL_EFFECT_DENIED; |
| 659 | if (!(e = emit_ev(g, kind, ARGUS_OUTCOME_DENIED, C_ERR_RIGHTS))) return; |
| 660 | fill_cap(e, g, i, g->slot[i].gen); |
| 661 | e->effect_class = ARGUS_EFFECT_EXTERNAL; |
| 662 | attach_ok(g, e); |
| 663 | } |
| 664 | |
| 665 | /* World generations advance by exactly one per store; no repeats (header apply |
| 666 | * rule). v1.1: two stores (object_id = store id) interleave. */ |
| 667 | static void world_commit(Gen *g, uint32_t k) |
| 668 | { |
| 669 | ArgusEvent *e = emit_ev(g, ARGUS_EV_WORLD_COMMITTED, ARGUS_OUTCOME_OK, 0); |
| 670 | if (!e) return; |
| 671 | g->world_gen[k] += 1; |
| 672 | fresh_digest(g, 0x57, g->world_digest[k]); |
| 673 | e->object_id = g_store_id[k]; |
| 674 | e->world_generation = g->world_gen[k]; |
| 675 | memcpy(e->evidence_digest, g->world_digest[k], ARGUS_DIGEST_LEN); |
| 676 | attach_ok(g, e); |
| 677 | } |
| 678 | |
| 679 | static void gop_world(Gen *g) { world_commit(g, rn(g, G_STORES)); } |
| 680 | |
| 681 | static void lease_fill(ArgusEvent *e, Gen *g, uint32_t i) |
| 682 | { |
| 683 | e->object_id = g->lease[i].id; |
| 684 | e->principal = g->lease[i].subject; |
| 685 | e->resource = g->lease[i].scope; |
| 686 | } |
| 687 | |
| 688 | /* Lease ids are never reused (ruling A): a slot whose lease was revoked gets a |
| 689 | * fresh id on its next creation; once G_LEASE_IDS ids are used, revoked slots |
| 690 | * stay revoked and only see refused uses. */ |
| 691 | static void gop_lease(Gen *g) |
| 692 | { |
| 693 | uint32_t i = rn(g, G_LEASES); |
| 694 | ArgusEvent *e; |
| 695 | if (g->lease[i].state != ARGUS_SHADOW_LIVE) { |
| 696 | int exhausted = g->next_lease_id > G_LEASE_IDS; |
| 697 | if (g->lease[i].state == ARGUS_SHADOW_REVOKED && (exhausted || rn(g, 2))) { |
| 698 | /* use of a revoked lease, correctly refused */ |
| 699 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_DENIED, C_ERR_REVOKED))) return; |
| 700 | lease_fill(e, g, i); |
| 701 | attach_ok(g, e); |
| 702 | return; |
| 703 | } |
| 704 | if (exhausted) { gop_use(g); return; } |
| 705 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0))) return; |
| 706 | g->lease[i].id = g->next_lease_id++; |
| 707 | g->lease[i].state = ARGUS_SHADOW_LIVE; |
| 708 | g->lease[i].subject = 1u + rn(g, 8); |
| 709 | g->lease[i].scope = (rnd(g) & 0xFFFFu) | 0x1u; |
| 710 | lease_fill(e, g, i); |
| 711 | attach_ok(g, e); |
| 712 | return; |
| 713 | } |
| 714 | switch (rn(g, 5)) { |
| 715 | case 0: /* revoke */ |
| 716 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_REVOKED, ARGUS_OUTCOME_OK, 0))) return; |
| 717 | e->object_id = g->lease[i].id; e->principal = g->lease[i].subject; |
| 718 | attach_ok(g, e); |
| 719 | g->lease[i].state = ARGUS_SHADOW_REVOKED; |
| 720 | return; |
| 721 | case 1: /* out of scope, correctly refused */ |
| 722 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_DENIED, C_ERR_RIGHTS))) return; |
| 723 | lease_fill(e, g, i); |
| 724 | e->resource = g->lease[i].scope | (1ull << 40); |
| 725 | attach_ok(g, e); |
| 726 | return; |
| 727 | default: /* in scope, by its subject */ |
| 728 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0))) return; |
| 729 | lease_fill(e, g, i); |
| 730 | e->resource = g->lease[i].scope & rnd(g); |
| 731 | attach_ok(g, e); |
| 732 | return; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | static void gop_artifact(Gen *g) |
| 737 | { |
| 738 | ArgusEvent *e; |
| 739 | uint32_t r = rn(g, 4); |
| 740 | if ((r == 0 || g->n_art == 0) && g->n_art < G_ARTIFACTS) { |
| 741 | size_t a = g->n_art++; |
| 742 | fresh_digest(g, 0xA7, g->artifact[a]); |
| 743 | int reject = rn(g, 4) == 0; |
| 744 | if (!(e = emit_ev(g, reject ? ARGUS_EV_ARTIFACT_REJECTED : ARGUS_EV_ARTIFACT_ADMITTED, |
| 745 | reject ? ARGUS_OUTCOME_DENIED : ARGUS_OUTCOME_OK, reject ? C_ERR_RIGHTS : 0))) return; |
| 746 | memcpy(e->evidence_digest, g->artifact[a], ARGUS_DIGEST_LEN); |
| 747 | attach_ok(g, e); |
| 748 | g->a_state[a] = reject ? ARGUS_SHADOW_REJECTED : ARGUS_SHADOW_LIVE; |
| 749 | return; |
| 750 | } |
| 751 | if (g->n_art == 0) return; |
| 752 | size_t a = rn(g, (uint32_t)g->n_art); |
| 753 | int live = g->a_state[a] == ARGUS_SHADOW_LIVE; |
| 754 | if (!(e = emit_ev(g, ARGUS_EV_ARTIFACT_ACTIVATED, live ? ARGUS_OUTCOME_OK : ARGUS_OUTCOME_DENIED, |
| 755 | live ? 0 : C_ERR_UNAUTHORIZED))) return; |
| 756 | memcpy(e->evidence_digest, g->artifact[a], ARGUS_DIGEST_LEN); |
| 757 | attach_ok(g, e); |
| 758 | } |
| 759 | |
| 760 | static void gop_provider(Gen *g) |
| 761 | { |
| 762 | uint32_t p = rn(g, G_PROVIDERS); |
| 763 | ArgusEvent *e; |
| 764 | if (g->p_quar[p]) { |
| 765 | if (!(e = emit_ev(g, ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_DENIED, C_ERR_UNAUTHORIZED))) return; |
| 766 | } else if (rn(g, 8) == 0) { |
| 767 | if (!(e = emit_ev(g, ARGUS_EV_PROVIDER_CHANGED, ARGUS_OUTCOME_OK, 0))) return; |
| 768 | } else { |
| 769 | if (!(e = emit_ev(g, ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0))) return; |
| 770 | } |
| 771 | memcpy(e->evidence_digest, g->provider[p], ARGUS_DIGEST_LEN); |
| 772 | e->principal = 1u + rn(g, 8); |
| 773 | attach_ok(g, e); |
| 774 | } |
| 775 | |
| 776 | /* Trust only ever moves down (header machine lifecycle). */ |
| 777 | static void set_trust(Gen *g, uint32_t m, uint32_t trust) |
| 778 | { |
| 779 | ArgusEvent *e = emit_ev(g, ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_OUTCOME_OK, 0); |
| 780 | if (!e) return; |
| 781 | memcpy(e->machine_id, g->machine[m], ARGUS_MACHINE_ID_LEN); |
| 782 | e->object_id = trust; |
| 783 | g->m_trust[m] = trust; |
| 784 | } |
| 785 | |
| 786 | /* JOINED with object_id 0: the core records OBSERVED (a joiner never declares trust). */ |
| 787 | static void join_machine(Gen *g, uint32_t m) |
| 788 | { |
| 789 | ArgusEvent *e = emit_ev(g, ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0); |
| 790 | if (!e) return; |
| 791 | memcpy(e->machine_id, g->machine[m], ARGUS_MACHINE_ID_LEN); |
| 792 | g->m_joined[m] = 1; |
| 793 | g->m_trust[m] = ARGUS_TRUST_OBSERVED; |
| 794 | } |
| 795 | |
| 796 | static void gop_machine(Gen *g) |
| 797 | { |
| 798 | if (rn(g, 2) == 0) { |
| 799 | /* M1 steps down: OBSERVED -> REATTESTATION_REQUIRED -> RESTRICTED, then stays */ |
| 800 | if (g->m_trust[G_DOWN] == ARGUS_TRUST_OBSERVED) { set_trust(g, G_DOWN, ARGUS_TRUST_REATTESTATION_REQUIRED); return; } |
| 801 | if (g->m_trust[G_DOWN] == ARGUS_TRUST_REATTESTATION_REQUIRED) { set_trust(g, G_DOWN, ARGUS_TRUST_RESTRICTED); return; } |
| 802 | } |
| 803 | if (g->m_joined[4]) { |
| 804 | ArgusEvent *e = emit_ev(g, ARGUS_EV_MACHINE_REMOVED, ARGUS_OUTCOME_OK, 0); |
| 805 | if (!e) return; |
| 806 | memcpy(e->machine_id, g->machine[4], ARGUS_MACHINE_ID_LEN); |
| 807 | g->m_joined[4] = 0; /* tombstone keeps OBSERVED */ |
| 808 | } else { |
| 809 | join_machine(g, 4); /* re-join after removal is legal */ |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* Policy / runtime digests are announced once at start of day; later events |
| 814 | * re-announce the stored digest (a changed digest is a code 5 finding). */ |
| 815 | static void gop_misc(Gen *g) |
| 816 | { |
| 817 | int policy = !g->policy_tampered && rn(g, 2); |
| 818 | ArgusEvent *e = emit_ev(g, policy ? ARGUS_EV_POLICY_CHANGED : ARGUS_EV_RUNTIME_BUILD_CHANGED, ARGUS_OUTCOME_OK, 0); |
| 819 | if (!e) return; |
| 820 | memcpy(e->evidence_digest, policy ? g->policy : g->runtime, ARGUS_DIGEST_LEN); |
| 821 | attach_ok(g, e); |
| 822 | } |
| 823 | |
| 824 | /* ---- hostile injections: each triggers exactly one detector ---- */ |
| 825 | |
| 826 | static void expect_code(Gen *g, uint64_t seq, uint16_t code) |
| 827 | { |
| 828 | if (g->n_expect < g->expect_max) { |
| 829 | g->expect[g->n_expect].sequence = seq; |
| 830 | g->expect[g->n_expect].code = code; |
| 831 | } |
| 832 | g->n_expect++; |
| 833 | } |
| 834 | |
| 835 | static int find_lease(Gen *g, int live_only) |
| 836 | { |
| 837 | uint32_t start = rn(g, G_LEASES); |
| 838 | for (uint32_t k = 0; k < G_LEASES; k++) { |
| 839 | uint32_t i = (start + k) % G_LEASES; |
| 840 | if (g->lease[i].state == ARGUS_SHADOW_LIVE || |
| 841 | (!live_only && g->lease[i].state == ARGUS_SHADOW_REVOKED)) |
| 842 | return (int)i; |
| 843 | } |
| 844 | return -1; |
| 845 | } |
| 846 | |
| 847 | /* Returns 1 if an injection of this type was emitted. */ |
| 848 | static int inject(Gen *g, unsigned type) |
| 849 | { |
| 850 | ArgusEvent *e; |
| 851 | int i; |
| 852 | switch (type) { |
| 853 | case 0: /* 1: use of a reference ARGUS never saw granted, outcome OK */ |
| 854 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 855 | e->cap_id = G_UNSEEN_CAP + rn(g, ARGUS_CAP_MAX - G_UNSEEN_CAP); e->cap_generation = 1; e->principal = 1u + rn(g, 8); |
| 856 | attach_ok(g, e); |
| 857 | expect_code(g, e->sequence, ARGUS_F_FORGED_CAPABILITY); |
| 858 | return 1; |
| 859 | case 1: /* 1: producer reports a forgery attempt (self-report: sync 0) */ |
| 860 | if (!(e = emit_ev(g, ARGUS_EV_FORGED_CAPABILITY, ARGUS_OUTCOME_DENIED, C_ERR_BOUNDS))) return 0; |
| 861 | e->principal = 1u + rn(g, 8); |
| 862 | attach_ok(g, e); |
| 863 | expect_code(g, e->sequence, ARGUS_F_FORGED_CAPABILITY); |
| 864 | return 1; |
| 865 | case 2: /* 2: old generation accepted */ |
| 866 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 2)) < 0) return 0; |
| 867 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 868 | fill_cap(e, g, i, g->slot[i].gen - 1); |
| 869 | attach_ok(g, e); |
| 870 | expect_code(g, e->sequence, ARGUS_F_STALE_GENERATION); |
| 871 | return 1; |
| 872 | case 3: /* 3: revoked reference accepted (current generation) */ |
| 873 | if ((i = find_slot(g, ARGUS_SHADOW_REVOKED, 0, 0, 1)) < 0) return 0; |
| 874 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 875 | fill_cap(e, g, i, g->slot[i].gen); |
| 876 | attach_ok(g, e); |
| 877 | expect_code(g, e->sequence, ARGUS_F_REVOKED_CAPABILITY_USED); |
| 878 | return 1; |
| 879 | case 4: /* 4: never-admitted artifact activated */ |
| 880 | if (!(e = emit_ev(g, ARGUS_EV_ARTIFACT_ACTIVATED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 881 | fresh_digest(g, 0xBD, e->evidence_digest); |
| 882 | attach_ok(g, e); |
| 883 | expect_code(g, e->sequence, ARGUS_F_ARTIFACT_DIGEST_UNEXPECTED); |
| 884 | return 1; |
| 885 | case 5: /* 5: signature failure on the trusted path */ |
| 886 | if (!(e = emit_ev(g, ARGUS_EV_SIGNATURE_FAILURE, ARGUS_OUTCOME_DENIED, 0))) return 0; |
| 887 | fresh_digest(g, 0x5F, e->evidence_digest); |
| 888 | attach_ok(g, e); |
| 889 | expect_code(g, e->sequence, ARGUS_F_SIGNATURE_INVALID); |
| 890 | return 1; |
| 891 | case 6: /* 6: lease used by a different subject */ |
| 892 | if ((i = find_lease(g, 1)) < 0) return 0; |
| 893 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 894 | lease_fill(e, g, (uint32_t)i); |
| 895 | e->principal = g->lease[i].subject + 1000u; e->resource = g->lease[i].scope & 1u; |
| 896 | attach_ok(g, e); |
| 897 | expect_code(g, e->sequence, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION); |
| 898 | return 1; |
| 899 | case 7: /* 6: lease used outside its scope */ |
| 900 | if ((i = find_lease(g, 1)) < 0) return 0; |
| 901 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 902 | lease_fill(e, g, (uint32_t)i); |
| 903 | e->resource = g->lease[i].scope | (1ull << 41); |
| 904 | attach_ok(g, e); |
| 905 | expect_code(g, e->sequence, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION); |
| 906 | return 1; |
| 907 | case 8: /* 7: event from a machine that never joined (refused, so only 7 fires) */ |
| 908 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_DENIED, ARGUS_OUTCOME_DENIED, C_ERR_RIGHTS))) return 0; |
| 909 | e->principal = 1u + rn(g, 8); |
| 910 | fresh_digest(g, 0xEE, e->machine_id); |
| 911 | expect_code(g, e->sequence, ARGUS_F_MACHINE_IDENTITY_MISMATCH); |
| 912 | return 1; |
| 913 | case 9: /* 7: joined machine joins again without removal (core: unchanged) */ |
| 914 | if (!(e = emit_ev(g, ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 915 | memcpy(e->machine_id, g->machine[0], ARGUS_MACHINE_ID_LEN); |
| 916 | expect_code(g, e->sequence, ARGUS_F_MACHINE_IDENTITY_MISMATCH); |
| 917 | return 1; |
| 918 | case 10: /* 8: external effect committed on a reference without the effect right */ |
| 919 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, R_EFFECT, 0)) < 0) return 0; |
| 920 | if (!(e = emit_ev(g, ARGUS_EV_EXTERNAL_EFFECT_COMMITTED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 921 | fill_cap(e, g, i, g->slot[i].gen); |
| 922 | e->effect_class = ARGUS_EFFECT_EXTERNAL; |
| 923 | attach_ok(g, e); |
| 924 | expect_code(g, e->sequence, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED); |
| 925 | return 1; |
| 926 | case 11: { /* 9: World generation skipped on one store; the core does not adopt it, nor does the model */ |
| 927 | uint32_t k = rn(g, G_STORES); |
| 928 | if (g->world_gen[k] == 0) return 0; |
| 929 | if (!(e = emit_ev(g, ARGUS_EV_WORLD_COMMITTED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 930 | e->object_id = g_store_id[k]; |
| 931 | e->world_generation = g->world_gen[k] + 2; |
| 932 | fresh_digest(g, 0x58, e->evidence_digest); |
| 933 | attach_ok(g, e); |
| 934 | expect_code(g, e->sequence, ARGUS_F_WORLD_PROVENANCE_INCONSISTENT); |
| 935 | return 1; |
| 936 | } |
| 937 | case 12: /* 10: quarantined provider used with outcome OK */ |
| 938 | if (!g->p_quar[G_QP]) return 0; |
| 939 | if (!(e = emit_ev(g, ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 940 | memcpy(e->evidence_digest, g->provider[G_QP], ARGUS_DIGEST_LEN); |
| 941 | e->principal = 1u + rn(g, 8); |
| 942 | attach_ok(g, e); |
| 943 | expect_code(g, e->sequence, ARGUS_F_QUARANTINED_USE); |
| 944 | return 1; |
| 945 | case 13: /* 10: quarantined machine acts with outcome OK */ |
| 946 | if (g->m_trust[G_QM] != ARGUS_TRUST_QUARANTINED) return 0; |
| 947 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) < 0) return 0; |
| 948 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 949 | fill_cap(e, g, i, g->slot[i].gen); |
| 950 | memcpy(e->machine_id, g->machine[G_QM], ARGUS_MACHINE_ID_LEN); |
| 951 | expect_code(g, e->sequence, ARGUS_F_QUARANTINED_USE); |
| 952 | return 1; |
| 953 | case 14: /* 1: use at a generation above the shadow's (never minted), G-4 */ |
| 954 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) < 0) return 0; |
| 955 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 956 | fill_cap(e, g, i, g->slot[i].gen + 1); |
| 957 | attach_ok(g, e); |
| 958 | expect_code(g, e->sequence, ARGUS_F_FORGED_CAPABILITY); |
| 959 | return 1; |
| 960 | case 15: /* 14: live reference used OK by a principal other than its subject, G-3 */ |
| 961 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 0)) < 0) return 0; |
| 962 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 963 | fill_cap(e, g, i, g->slot[i].gen); |
| 964 | e->principal = g->slot[i].subject + 1000u; |
| 965 | attach_ok(g, e); |
| 966 | expect_code(g, e->sequence, ARGUS_F_SUBJECT_MISMATCH); |
| 967 | return 1; |
| 968 | case 16: { /* 15: joined machine announces an upward trust move (core ignores it) */ |
| 969 | uint32_t m = g->m_trust[G_QM] == ARGUS_TRUST_QUARANTINED ? G_QM : G_DOWN; |
| 970 | if (!(e = emit_ev(g, ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 971 | memcpy(e->machine_id, g->machine[m], ARGUS_MACHINE_ID_LEN); |
| 972 | e->object_id = ARGUS_TRUST_TRUSTED; |
| 973 | expect_code(g, e->sequence, ARGUS_F_TRUST_ESCALATION); |
| 974 | return 1; |
| 975 | } |
| 976 | case 17: /* 15: removed machine re-joins declaring itself TRUSTED (core caps at OBSERVED) */ |
| 977 | if (g->m_joined[4]) return 0; |
| 978 | if (!(e = emit_ev(g, ARGUS_EV_MACHINE_JOINED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 979 | memcpy(e->machine_id, g->machine[4], ARGUS_MACHINE_ID_LEN); |
| 980 | e->object_id = ARGUS_TRUST_TRUSTED; |
| 981 | g->m_joined[4] = 1; |
| 982 | g->m_trust[4] = ARGUS_TRUST_OBSERVED; |
| 983 | expect_code(g, e->sequence, ARGUS_F_TRUST_ESCALATION); |
| 984 | return 1; |
| 985 | case 18: /* 6: CREATED re-uses an existing lease id for another subject, G-26 (not applied) */ |
| 986 | if ((i = find_lease(g, 0)) < 0) return 0; |
| 987 | if (!(e = emit_ev(g, ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 988 | e->object_id = g->lease[i].id; e->principal = g->lease[i].subject + 1000u; e->resource = UINT64_MAX; |
| 989 | attach_ok(g, e); |
| 990 | expect_code(g, e->sequence, ARGUS_F_CREDENTIAL_SCOPE_VIOLATION); |
| 991 | return 1; |
| 992 | case 19: /* 10: provider never discovered used with outcome OK, G-9 */ |
| 993 | if (!(e = emit_ev(g, ARGUS_EV_PROVIDER_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 994 | fresh_digest(g, 0xDF, e->evidence_digest); |
| 995 | e->principal = 1u + rn(g, 8); |
| 996 | attach_ok(g, e); |
| 997 | expect_code(g, e->sequence, ARGUS_F_QUARANTINED_USE); |
| 998 | return 1; |
| 999 | case 20: /* 8: durable EVIDENCE write under a reference without WRITE, G-24 */ |
| 1000 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, R_WRITE, 0)) < 0) return 0; |
| 1001 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1002 | fill_cap(e, g, i, g->slot[i].gen); |
| 1003 | e->effect_class = ARGUS_EFFECT_EVIDENCE; |
| 1004 | attach_ok(g, e); |
| 1005 | expect_code(g, e->sequence, ARGUS_F_EFFECT_CLASS_UNAUTHORIZED); |
| 1006 | return 1; |
| 1007 | case 21: /* 5: producer reports an integrity violation, G-14 */ |
| 1008 | if (!(e = emit_ev(g, ARGUS_EV_INTEGRITY_VIOLATION, ARGUS_OUTCOME_ERROR, 0))) return 0; |
| 1009 | fresh_digest(g, 0x60, e->evidence_digest); |
| 1010 | attach_ok(g, e); |
| 1011 | expect_code(g, e->sequence, ARGUS_F_SIGNATURE_INVALID); |
| 1012 | return 1; |
| 1013 | case 22: /* 5: stored policy digest replaced, G-14; once per stream, no policy events after */ |
| 1014 | if (g->policy_tampered) return 0; |
| 1015 | if (!(e = emit_ev(g, ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1016 | fresh_digest(g, 0x9D, e->evidence_digest); |
| 1017 | attach_ok(g, e); |
| 1018 | g->policy_tampered = 1; |
| 1019 | expect_code(g, e->sequence, ARGUS_F_SIGNATURE_INVALID); |
| 1020 | return 1; |
| 1021 | case 23: /* 7: TRUST_CHANGED for a machine that never joined (core creates no entry), G-10 */ |
| 1022 | if (!(e = emit_ev(g, ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1023 | fresh_digest(g, 0xEF, e->machine_id); |
| 1024 | e->object_id = ARGUS_TRUST_RESTRICTED; |
| 1025 | expect_code(g, e->sequence, ARGUS_F_MACHINE_IDENTITY_MISMATCH); |
| 1026 | return 1; |
| 1027 | case 24: /* 3: use summary whose MAX generation hits a revoked slot (v1.1) */ |
| 1028 | if ((i = find_slot(g, ARGUS_SHADOW_REVOKED, 0, 0, 1)) < 0) return 0; |
| 1029 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USE_SUMMARY, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1030 | fill_cap(e, g, i, g->slot[i].gen); |
| 1031 | e->world_generation = g->slot[i].gen; e->resource = 1u + rn(g, 64); |
| 1032 | attach_ok(g, e); |
| 1033 | expect_code(g, e->sequence, ARGUS_F_REVOKED_CAPABILITY_USED); |
| 1034 | return 1; |
| 1035 | case 25: /* 2: use summary whose MIN generation is stale (v1.1) */ |
| 1036 | if ((i = find_slot(g, ARGUS_SHADOW_LIVE, 0, 0, 2)) < 0) return 0; |
| 1037 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USE_SUMMARY, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1038 | fill_cap(e, g, i, g->slot[i].gen); |
| 1039 | e->world_generation = g->slot[i].gen - 1; e->resource = 2u + rn(g, 64); |
| 1040 | attach_ok(g, e); |
| 1041 | expect_code(g, e->sequence, ARGUS_F_STALE_GENERATION); |
| 1042 | return 1; |
| 1043 | case 26: /* 14: cap 0 (the office capability) used OK by the wrong subject (v1.1) */ |
| 1044 | if (g->slot[0].state != ARGUS_SHADOW_LIVE) return 0; |
| 1045 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USED, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1046 | fill_cap(e, g, 0, g->slot[0].gen); |
| 1047 | e->principal = g->slot[0].subject + 1000u; |
| 1048 | e->effect_class = ARGUS_EFFECT_EPHEMERAL; |
| 1049 | attach_ok(g, e); |
| 1050 | expect_code(g, e->sequence, ARGUS_F_SUBJECT_MISMATCH); |
| 1051 | return 1; |
| 1052 | case 27: /* 1 only: summary ABOVE a revoked generation (never minted; detector 3 stays quiet, v1.1) */ |
| 1053 | if ((i = find_slot(g, ARGUS_SHADOW_REVOKED, 0, 0, 1)) < 0) return 0; |
| 1054 | if (!(e = emit_ev(g, ARGUS_EV_CAPABILITY_USE_SUMMARY, ARGUS_OUTCOME_OK, 0))) return 0; |
| 1055 | fill_cap(e, g, i, g->slot[i].gen + 1); |
| 1056 | e->world_generation = g->slot[i].gen + 1; e->resource = 1; |
| 1057 | attach_ok(g, e); |
| 1058 | expect_code(g, e->sequence, ARGUS_F_FORGED_CAPABILITY); |
| 1059 | return 1; |
| 1060 | default: |
| 1061 | return 0; |
| 1062 | } |
| 1063 | } |
| 1064 | #define G_INJECT_TYPES 28u |
| 1065 | |
| 1066 | static void maybe_inject(Gen *g) |
| 1067 | { |
| 1068 | if (!g->hostile || g->n < g->next_inject) |
| 1069 | return; |
| 1070 | g->next_inject += ARGUS_CORPUS_HOSTILE_EVERY; |
| 1071 | for (unsigned k = 0; k < G_INJECT_TYPES; k++) { |
| 1072 | unsigned t = (g->inject_type + k) % G_INJECT_TYPES; |
| 1073 | if (inject(g, t)) { |
| 1074 | g->inject_type = t + 1; |
| 1075 | return; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | static size_t generate(Gen *g, uint64_t seed) |
| 1081 | { |
| 1082 | g->rng = seed ^ 0xA5A5A5A55A5A5A5Aull; |
| 1083 | g->next_lease_id = 1; |
| 1084 | for (uint32_t m = 0; m < G_MACHINES; m++) |
| 1085 | fresh_digest(g, (uint8_t)(0xC0 + m), g->machine[m]); |
| 1086 | for (uint32_t p = 0; p < G_PROVIDERS; p++) |
| 1087 | fresh_digest(g, (uint8_t)(0xD0 + p), g->provider[p]); |
| 1088 | fresh_digest(g, 0x9C, g->policy); |
| 1089 | fresh_digest(g, 0x9B, g->runtime); |
| 1090 | |
| 1091 | /* start of day: machines join, providers are discovered, policy and |
| 1092 | * runtime announced, first World */ |
| 1093 | for (uint32_t m = 0; m < G_MACHINES; m++) |
| 1094 | join_machine(g, m); |
| 1095 | for (uint32_t p = 0; p < G_PROVIDERS; p++) { |
| 1096 | ArgusEvent *e = emit_ev(g, ARGUS_EV_PROVIDER_DISCOVERED, ARGUS_OUTCOME_OK, 0); |
| 1097 | if (!e) break; |
| 1098 | memcpy(e->evidence_digest, g->provider[p], ARGUS_DIGEST_LEN); |
| 1099 | attach_ok(g, e); |
| 1100 | } |
| 1101 | for (int k = 0; k < 2; k++) { |
| 1102 | ArgusEvent *e = emit_ev(g, k ? ARGUS_EV_RUNTIME_BUILD_CHANGED : ARGUS_EV_POLICY_CHANGED, ARGUS_OUTCOME_OK, 0); |
| 1103 | if (!e) break; |
| 1104 | memcpy(e->evidence_digest, k ? g->runtime : g->policy, ARGUS_DIGEST_LEN); |
| 1105 | attach_ok(g, e); |
| 1106 | } |
| 1107 | for (uint32_t k = 0; k < G_STORES; k++) |
| 1108 | world_commit(g, k); |
| 1109 | grant_slot(g, 0); /* the office capability, cap 0 (v1.1) */ |
| 1110 | g->next_inject = g->n + ARGUS_CORPUS_HOSTILE_EVERY / 2; |
| 1111 | |
| 1112 | while (g->n < g->max) { |
| 1113 | maybe_inject(g); |
| 1114 | if (!g->m3_quarantined_done && g->n >= g->max * 2 / 5) { |
| 1115 | g->m3_quarantined_done = 1; |
| 1116 | set_trust(g, G_QM, ARGUS_TRUST_QUARANTINED); |
| 1117 | continue; |
| 1118 | } |
| 1119 | if (!g->p2_quarantined_done && g->n >= g->max / 2) { |
| 1120 | g->p2_quarantined_done = 1; |
| 1121 | ArgusEvent *e = emit_ev(g, ARGUS_EV_PROVIDER_QUARANTINED, ARGUS_OUTCOME_OK, 0); |
| 1122 | if (e) { |
| 1123 | memcpy(e->evidence_digest, g->provider[G_QP], ARGUS_DIGEST_LEN); |
| 1124 | attach_ok(g, e); |
| 1125 | g->p_quar[G_QP] = 1; |
| 1126 | } |
| 1127 | continue; |
| 1128 | } |
| 1129 | uint32_t r = rn(g, 100); |
| 1130 | if (r < 15) gop_grant(g); |
| 1131 | else if (r < 33) gop_use(g); |
| 1132 | else if (r < 40) gop_summary(g); |
| 1133 | else if (r < 50) gop_denied(g); |
| 1134 | else if (r < 55) gop_revoke(g); |
| 1135 | else if (r < 62) gop_effect(g); |
| 1136 | else if (r < 67) gop_world(g); |
| 1137 | else if (r < 76) gop_lease(g); |
| 1138 | else if (r < 84) gop_artifact(g); |
| 1139 | else if (r < 91) gop_provider(g); |
| 1140 | else if (r < 96) gop_machine(g); |
| 1141 | else gop_misc(g); |
| 1142 | } |
| 1143 | return g->n; |
| 1144 | } |
| 1145 | |
| 1146 | size_t argus_corpus_benign(ArgusEvent *out, size_t max, uint64_t seed) |
| 1147 | { |
| 1148 | static Gen g; /* test-only helper; not reentrant */ |
| 1149 | if (out == NULL || max == 0) |
| 1150 | return 0; |
| 1151 | memset(&g, 0, sizeof g); |
| 1152 | g.out = out; |
| 1153 | g.max = max; |
| 1154 | return generate(&g, seed); |
| 1155 | } |
| 1156 | |
| 1157 | size_t argus_corpus_hostile(ArgusEvent *out, size_t max, uint64_t seed, |
| 1158 | ArgusExpectedFinding *expect, size_t expect_max, size_t *n_expect) |
| 1159 | { |
| 1160 | static Gen g; /* test-only helper; not reentrant */ |
| 1161 | if (n_expect) *n_expect = 0; |
| 1162 | if (out == NULL || max == 0 || n_expect == NULL || (expect == NULL && expect_max != 0)) |
| 1163 | return 0; |
| 1164 | memset(&g, 0, sizeof g); |
| 1165 | g.out = out; |
| 1166 | g.max = max; |
| 1167 | g.hostile = 1; |
| 1168 | g.expect = expect; |
| 1169 | g.expect_max = expect_max; |
| 1170 | size_t n = generate(&g, seed); |
| 1171 | *n_expect = g.n_expect; |
| 1172 | return n; |
| 1173 | } |
| 1174 | |
| 1175 | /* ---- ARGUS-1 containment corpora ----------------------------------------- */ |
| 1176 | static void contain_corpus_identity(uint8_t out[ARGUS_MACHINE_ID_LEN], uint64_t seed, |
| 1177 | uint32_t index) |
| 1178 | { |
| 1179 | uint64_t x = seed ^ (0x9E3779B97F4A7C15ull * (uint64_t)(index + 1)); |
| 1180 | for (size_t i = 0; i < ARGUS_MACHINE_ID_LEN; i++) { |
| 1181 | x ^= x >> 12; x ^= x << 25; x ^= x >> 27; |
| 1182 | out[i] = (uint8_t)(x * 0x2545F4914F6CDD1Dull >> 56); |
| 1183 | } |
| 1184 | uint8_t any = 0; |
| 1185 | for (size_t i = 0; i < ARGUS_MACHINE_ID_LEN; i++) any |= out[i]; |
| 1186 | if (any == 0) out[0] = 1; |
| 1187 | } |
| 1188 | |
| 1189 | static ArgusEvent contain_corpus_event(uint16_t kind, uint64_t sequence, |
| 1190 | const uint8_t machine[ARGUS_MACHINE_ID_LEN]) |
| 1191 | { |
| 1192 | ArgusEvent e = {0}; |
| 1193 | e.version = ARGUS_ABI_VERSION; |
| 1194 | e.class_ = stub_min_class(kind); |
| 1195 | e.kind = kind; |
| 1196 | e.outcome = ARGUS_OUTCOME_OK; |
| 1197 | e.flags = (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT); |
| 1198 | e.sequence = sequence; |
| 1199 | e.cap_id = ARGUS_CAP_NONE; |
| 1200 | memcpy(e.machine_id, machine, ARGUS_MACHINE_ID_LEN); |
| 1201 | return e; |
| 1202 | } |
| 1203 | |
| 1204 | size_t argus_corpus_containment_storm(ArgusEvent *out, size_t capacity, |
| 1205 | size_t stale_uses, uint64_t seed) |
| 1206 | { |
| 1207 | size_t need = 1u + ARGUS_CAP_MAX + stale_uses; |
| 1208 | if (!out || need < stale_uses || capacity < need) return 0; |
| 1209 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; contain_corpus_identity(machine, seed, 0); |
| 1210 | size_t n = 0; |
| 1211 | ArgusEvent e = contain_corpus_event(ARGUS_EV_MACHINE_JOINED, ++n, machine); |
| 1212 | e.object_id = ARGUS_TRUST_OBSERVED; out[n - 1] = e; |
| 1213 | for (uint32_t id = 0; id < ARGUS_CAP_MAX; id++) { |
| 1214 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_GRANTED, n + 1, machine); |
| 1215 | e.cap_id = id; e.cap_generation = 1; e.principal = id == 0 ? 0 : 1000u + id; |
| 1216 | e.object_id = id == 0 ? ARGUS_CAP_RIGHT_PRIVILEGED : 1u; |
| 1217 | e.resource = 0xA000u + id; out[n++] = e; |
| 1218 | } |
| 1219 | for (size_t i = 0; i < stale_uses; i++) { |
| 1220 | uint32_t id = (uint32_t)(i % ARGUS_CAP_MAX); |
| 1221 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_USED, n + 1, machine); |
| 1222 | e.cap_id = id; e.cap_generation = 0; e.principal = id == 0 ? 0 : 1000u + id; |
| 1223 | e.code = -4; /* authority's stale-generation result */ |
| 1224 | e.effect_class = ARGUS_EFFECT_NONE; out[n++] = e; |
| 1225 | } |
| 1226 | return n; |
| 1227 | } |
| 1228 | |
| 1229 | size_t argus_corpus_cap0_framing(ArgusEvent *out, size_t capacity, |
| 1230 | size_t stale_uses, uint64_t seed) |
| 1231 | { |
| 1232 | size_t need = 2u + stale_uses; |
| 1233 | if (!out || need < stale_uses || capacity < need) return 0; |
| 1234 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; contain_corpus_identity(machine, seed, 0); |
| 1235 | size_t n = 0; |
| 1236 | ArgusEvent e = contain_corpus_event(ARGUS_EV_MACHINE_JOINED, ++n, machine); |
| 1237 | e.object_id = ARGUS_TRUST_OBSERVED; out[n - 1] = e; |
| 1238 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_GRANTED, n + 1, machine); |
| 1239 | e.cap_id = 0; e.cap_generation = 1; e.principal = 0; |
| 1240 | e.object_id = ARGUS_CAP_RIGHT_PRIVILEGED; e.resource = 0xA000u; out[n++] = e; |
| 1241 | for (size_t i = 0; i < stale_uses; i++) { |
| 1242 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_USED, n + 1, machine); |
| 1243 | e.cap_id = 0; e.cap_generation = 0; e.principal = 7; e.code = -4; |
| 1244 | e.effect_class = ARGUS_EFFECT_NONE; out[n++] = e; |
| 1245 | } |
| 1246 | return n; |
| 1247 | } |
| 1248 | |
| 1249 | size_t argus_corpus_table_saturation(ArgusEvent *out, size_t capacity, |
| 1250 | uint16_t table_kind, size_t slots, |
| 1251 | size_t overflows, uint64_t seed) |
| 1252 | { |
| 1253 | if (!out || slots > SIZE_MAX - overflows - 1) return 0; |
| 1254 | if (table_kind != ARGUS_EV_MACHINE_JOINED && |
| 1255 | table_kind != ARGUS_EV_CREDENTIAL_LEASE_CREATED && |
| 1256 | table_kind != ARGUS_EV_PROVIDER_DISCOVERED && |
| 1257 | table_kind != ARGUS_EV_ARTIFACT_ADMITTED) return 0; |
| 1258 | size_t n = 0; |
| 1259 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; contain_corpus_identity(machine, seed, 0); |
| 1260 | if (table_kind != ARGUS_EV_MACHINE_JOINED) { |
| 1261 | ArgusEvent join = contain_corpus_event(ARGUS_EV_MACHINE_JOINED, 1, machine); |
| 1262 | join.object_id = ARGUS_TRUST_OBSERVED; |
| 1263 | if (capacity == 0) return 0; |
| 1264 | out[n++] = join; |
| 1265 | } |
| 1266 | for (size_t i = 0; i < slots + overflows; i++) { |
| 1267 | uint8_t event_machine[ARGUS_MACHINE_ID_LEN]; |
| 1268 | if (table_kind == ARGUS_EV_MACHINE_JOINED) |
| 1269 | contain_corpus_identity(event_machine, seed, (uint32_t)i + 1); |
| 1270 | else memcpy(event_machine, machine, ARGUS_MACHINE_ID_LEN); |
| 1271 | uint64_t sequence = table_kind == ARGUS_EV_MACHINE_JOINED ? 1 : n + 1; |
| 1272 | ArgusEvent e = contain_corpus_event(table_kind, sequence, event_machine); |
| 1273 | switch (table_kind) { |
| 1274 | case ARGUS_EV_MACHINE_JOINED: |
| 1275 | e.object_id = ARGUS_TRUST_OBSERVED; |
| 1276 | break; |
| 1277 | case ARGUS_EV_CREDENTIAL_LEASE_CREATED: |
| 1278 | e.object_id = (uint32_t)i + 1; e.principal = (uint32_t)i + 1; e.resource = 1; |
| 1279 | break; |
| 1280 | case ARGUS_EV_PROVIDER_DISCOVERED: |
| 1281 | case ARGUS_EV_ARTIFACT_ADMITTED: |
| 1282 | contain_corpus_identity(e.evidence_digest, seed ^ 0xD1B54A32D192ED03ull, |
| 1283 | (uint32_t)i + 1); |
| 1284 | break; |
| 1285 | default: return 0; |
| 1286 | } |
| 1287 | if (n >= capacity) return 0; |
| 1288 | out[n++] = e; |
| 1289 | } |
| 1290 | return n; |
| 1291 | } |
| 1292 | |
| 1293 | size_t argus_corpus_authority_churn(ArgusEvent *out, size_t capacity, |
| 1294 | size_t cycles, uint64_t seed) |
| 1295 | { |
| 1296 | size_t need = 1u + cycles * 3u; |
| 1297 | if (!out || cycles > (SIZE_MAX - 1u) / 3u || need < cycles || capacity < need) return 0; |
| 1298 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; contain_corpus_identity(machine, seed, 0); |
| 1299 | size_t n = 0; |
| 1300 | ArgusEvent e = contain_corpus_event(ARGUS_EV_MACHINE_JOINED, ++n, machine); |
| 1301 | e.object_id = ARGUS_TRUST_OBSERVED; out[n - 1] = e; |
| 1302 | uint64_t generation[16] = {0}; |
| 1303 | for (size_t i = 0; i < cycles; i++) { |
| 1304 | uint32_t id = 1u + (uint32_t)(i % 16u); |
| 1305 | uint32_t subject = 100u + id; |
| 1306 | generation[id - 1]++; |
| 1307 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_GRANTED, n + 1, machine); |
| 1308 | e.cap_id = id; e.cap_generation = generation[id - 1]; e.principal = subject; |
| 1309 | e.object_id = 1u; e.resource = 0xB000u + id; out[n++] = e; |
| 1310 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_USED, n + 1, machine); |
| 1311 | e.cap_id = id; e.cap_generation = generation[id - 1]; e.principal = subject; |
| 1312 | e.resource = 0; e.effect_class = ARGUS_EFFECT_NONE; out[n++] = e; |
| 1313 | e = contain_corpus_event(ARGUS_EV_CAPABILITY_REVOKED, n + 1, machine); |
| 1314 | e.cap_id = id; e.cap_generation = generation[id - 1]; e.principal = subject; |
| 1315 | e.object_id = 1u; e.resource = 0xB000u + id; out[n++] = e; |
| 1316 | } |
| 1317 | return n; |
| 1318 | } |