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 / argus_contain.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 | #include "argus_contain.h" |
| 18 | #include "argus_core.h" |
| 19 | #include "sha256.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | enum { ST_FREE=0, ST_PROPOSED, ST_GRANTED, ST_ESCALATED, ST_CONFIRMED, |
| 24 | ST_DENIED, ST_FAILED, ST_FAILED_SCOPE, ST_EXPIRED }; |
| 25 | |
| 26 | typedef struct { |
| 27 | ArgusContainmentRequest req; |
| 28 | uint8_t digest[ARGUS_DIGEST_LEN]; |
| 29 | uint8_t state, kind4_count, retries; |
| 30 | uint64_t entered, decision_id, closed_at; |
| 31 | } Pending; |
| 32 | |
| 33 | typedef struct { |
| 34 | uint8_t used, type, severity, state, retries; |
| 35 | uint32_t principal, cap_id, target_object; |
| 36 | uint64_t generation, last_event, request_id; |
| 37 | uint16_t finding_code; |
| 38 | uint8_t digest[ARGUS_DIGEST_LEN]; |
| 39 | } Recent; |
| 40 | |
| 41 | struct ArgusContain { |
| 42 | uint64_t events, next_request_id, next_proposal_sequence, window_start; |
| 43 | uint32_t window_count; |
| 44 | uint32_t executor_cap_id; |
| 45 | uint8_t saturation_mask, overflow_reported, overflow_pending; |
| 46 | uint8_t overflow_machine_id[ARGUS_MACHINE_ID_LEN]; |
| 47 | Pending pending[ARGUS_CONTAIN_PENDING]; |
| 48 | Recent recent[ARGUS_CONTAIN_RECENT]; |
| 49 | ArgusContainHealth health; |
| 50 | }; |
| 51 | |
| 52 | static void wr32(uint8_t *p, uint32_t v) |
| 53 | { p[0]=(uint8_t)v; p[1]=(uint8_t)(v>>8); p[2]=(uint8_t)(v>>16); p[3]=(uint8_t)(v>>24); } |
| 54 | static void wr16(uint8_t *p, uint16_t v) { p[0]=(uint8_t)v; p[1]=(uint8_t)(v>>8); } |
| 55 | static void wr64(uint8_t *p, uint64_t v) |
| 56 | { wr32(p,(uint32_t)v); wr32(p+4,(uint32_t)(v>>32)); } |
| 57 | |
| 58 | size_t argus_contain_footprint(void) { return sizeof(ArgusContain); } |
| 59 | int argus_contain_init(ArgusContain **state, void *memory, size_t bytes) |
| 60 | { |
| 61 | if (!state || !memory || bytes < sizeof(ArgusContain)) return ARGUS_ERR_ARG; |
| 62 | memset(memory,0,sizeof(ArgusContain)); |
| 63 | *state=(ArgusContain *)memory; |
| 64 | (*state)->next_request_id=1; |
| 65 | (*state)->next_proposal_sequence=1; |
| 66 | return ARGUS_OK; |
| 67 | } |
| 68 | |
| 69 | static int is_open(uint8_t s) |
| 70 | { return s==ST_PROPOSED || s==ST_GRANTED || s==ST_ESCALATED; } |
| 71 | |
| 72 | static int same_key_req(const ArgusContainmentRequest *a, const ArgusContainmentRequest *b) |
| 73 | { |
| 74 | return a->containment==b->containment && a->principal==b->principal && |
| 75 | a->target.cap_id==b->target.cap_id && a->target.generation==b->target.generation && |
| 76 | a->target_object==b->target_object && a->finding_code==b->finding_code; |
| 77 | } |
| 78 | static int same_key_recent(const Recent *a, const ArgusContainmentRequest *b) |
| 79 | { |
| 80 | return a->used && a->type==b->containment && a->principal==b->principal && |
| 81 | a->cap_id==b->target.cap_id && a->generation==b->target.generation && |
| 82 | a->target_object==b->target_object && a->finding_code==b->finding_code; |
| 83 | } |
| 84 | static int same_key_recent_recent(const Recent *a, const Recent *b) |
| 85 | { |
| 86 | return a->used && a->type==b->type && a->principal==b->principal && |
| 87 | a->cap_id==b->cap_id && a->generation==b->generation && |
| 88 | a->target_object==b->target_object && a->finding_code==b->finding_code; |
| 89 | } |
| 90 | static int find_pending(const ArgusContain *s, uint64_t id) |
| 91 | { |
| 92 | for (size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) |
| 93 | if (s->pending[i].state!=ST_FREE && s->pending[i].req.request_id==id) return (int)i; |
| 94 | return -1; |
| 95 | } |
| 96 | static const Recent *find_recent_request(const ArgusContain *s, uint64_t id) |
| 97 | { |
| 98 | for(size_t i=0;i<ARGUS_CONTAIN_RECENT;i++) |
| 99 | if(s->recent[i].used && s->recent[i].request_id==id) return &s->recent[i]; |
| 100 | return NULL; |
| 101 | } |
| 102 | static int pending_slot(ArgusContain *s) |
| 103 | { |
| 104 | for (size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) if (s->pending[i].state==ST_FREE) return (int)i; |
| 105 | return -1; |
| 106 | } |
| 107 | static void remember(ArgusContain *s, Pending *p) |
| 108 | { |
| 109 | Recent r={0}; const ArgusContainmentRequest *q=&p->req; |
| 110 | r.used=1; r.type=q->containment; r.severity=q->severity; r.state=p->state; |
| 111 | r.retries=p->retries; r.principal=q->principal; r.cap_id=q->target.cap_id; |
| 112 | r.generation=q->target.generation; r.target_object=q->target_object; r.request_id=q->request_id; |
| 113 | r.finding_code=q->finding_code; r.last_event=s->events; |
| 114 | memcpy(r.digest,p->digest,ARGUS_DIGEST_LEN); |
| 115 | size_t slot=0; |
| 116 | for (size_t i=0;i<ARGUS_CONTAIN_RECENT;i++) { |
| 117 | if (!s->recent[i].used) { slot=i; goto chosen; } |
| 118 | if (same_key_recent_recent(&s->recent[i],&r)) { slot=i; goto chosen; } |
| 119 | if (s->recent[i].last_event<s->recent[slot].last_event) slot=i; |
| 120 | } |
| 121 | chosen: |
| 122 | s->recent[slot]=r; |
| 123 | p->closed_at=s->events; |
| 124 | } |
| 125 | static void close_pending(ArgusContain *s, Pending *p, uint8_t state) |
| 126 | { |
| 127 | p->state=state; remember(s,p); memset(p,0,sizeof *p); |
| 128 | } |
| 129 | |
| 130 | static void emit_contain_finding(ArgusFinding *f, uint16_t code, uint8_t severity, |
| 131 | const ArgusEvent *ev, uint32_t principal, |
| 132 | uint32_t cap_id, uint64_t generation) |
| 133 | { |
| 134 | memset(f,0,sizeof *f); f->code=code; f->severity=severity; |
| 135 | f->confidence=ARGUS_CONF_DETERMINISTIC; f->detector=code; |
| 136 | f->sequence=ev->sequence; f->principal=principal; f->cap_id=cap_id; |
| 137 | f->cap_generation=generation; memcpy(f->machine_id,ev->machine_id,ARGUS_MACHINE_ID_LEN); |
| 138 | argus_event_digest(ev,f->event_digest); |
| 139 | } |
| 140 | static int push_finding(ArgusContain *s, ArgusFinding *out, size_t cap, size_t *n, |
| 141 | uint16_t code, uint8_t severity, const ArgusEvent *ev, |
| 142 | uint32_t principal, uint32_t cap_id, uint64_t generation) |
| 143 | { |
| 144 | if (*n>=cap) return ARGUS_ERR_OVERFLOW; |
| 145 | emit_contain_finding(&out[(*n)++],code,severity,ev,principal,cap_id,generation); |
| 146 | if (code==ARGUS_F_CONTAINMENT_DECISION_UNMATCHED) s->health.decisions_unmatched++; |
| 147 | else if (code==ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED) s->health.executions_unauthorized++; |
| 148 | else if (code==ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED) s->health.executions_unconfirmed++; |
| 149 | return ARGUS_OK; |
| 150 | } |
| 151 | |
| 152 | static int echo_matches(const Pending *p, const ArgusEvent *ev) |
| 153 | { |
| 154 | const ArgusContainmentRequest *q=&p->req; |
| 155 | return ARGUS_CONTAIN_TYPE_OF(ev->object_id)==q->containment && |
| 156 | ARGUS_CONTAIN_CODE_OF(ev->object_id)==q->finding_code && ev->principal==q->principal && |
| 157 | ev->cap_id==q->target.cap_id && ev->cap_generation==q->target.generation && |
| 158 | ev->world_generation==q->request_id && memcmp(ev->machine_id,q->machine_id,ARGUS_MACHINE_ID_LEN)==0 && |
| 159 | memcmp(ev->evidence_digest,p->digest,ARGUS_DIGEST_LEN)==0; |
| 160 | } |
| 161 | |
| 162 | static void apply_decision(ArgusContain *s, Pending *p, const ArgusEvent *ev) |
| 163 | { |
| 164 | uint8_t status=ARGUS_CONTAIN_STATUS_OF(ev->object_id); |
| 165 | p->decision_id=ev->resource; p->entered=s->events; |
| 166 | if (status==ARGUS_CSTATUS_GRANT) { p->state=ST_GRANTED; s->health.granted++; } |
| 167 | else if (status==ARGUS_CSTATUS_DENY) { close_pending(s,p,ST_DENIED); s->health.denied++; } |
| 168 | else if (status==ARGUS_CSTATUS_ESCALATE) { p->state=ST_ESCALATED; s->health.escalated++; } |
| 169 | } |
| 170 | |
| 171 | int argus_contain_observe(ArgusContain *s, const ArgusCore *core, const ArgusEvent *ev, |
| 172 | ArgusFinding *out, size_t cap, size_t *n_out) |
| 173 | { |
| 174 | (void)core; |
| 175 | if (!s || !ev || !n_out || !out || cap==0) return ARGUS_ERR_ARG; |
| 176 | size_t n=0; s->events++; |
| 177 | if (s->events>s->window_start+ARGUS_CONTAIN_WINDOW_EVENTS) { |
| 178 | s->window_start=((s->events-1)/ARGUS_CONTAIN_WINDOW_EVENTS)*ARGUS_CONTAIN_WINDOW_EVENTS; |
| 179 | s->window_count=0; |
| 180 | } |
| 181 | if (ev->kind==ARGUS_EV_CONTAINMENT_DECIDED || ev->kind==ARGUS_EV_CONTAINMENT_EXECUTED) { |
| 182 | uint64_t id=ev->world_generation; int ix=find_pending(s,id); |
| 183 | if (ix<0) { |
| 184 | const Recent *r=find_recent_request(s,id); |
| 185 | int scope_close=ev->kind==ARGUS_EV_CONTAINMENT_EXECUTED && r && r->state==ST_FAILED_SCOPE && |
| 186 | ARGUS_CONTAIN_STATUS_OF(ev->object_id)==ARGUS_CSTATUS_FAILED_SCOPE && |
| 187 | ARGUS_CONTAIN_TYPE_OF(ev->object_id)==r->type && ARGUS_CONTAIN_CODE_OF(ev->object_id)==r->finding_code && |
| 188 | ev->principal==r->principal && ev->cap_id==r->cap_id && ev->cap_generation==r->generation && |
| 189 | ev->world_generation==r->request_id && memcmp(ev->evidence_digest,r->digest,ARGUS_DIGEST_LEN)==0; |
| 190 | if (!scope_close && cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc)return rc; } |
| 191 | } else if (!is_open(s->pending[ix].state) || !echo_matches(&s->pending[ix],ev)) { |
| 192 | if (cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc) return rc; } |
| 193 | } else { |
| 194 | Pending *p=&s->pending[ix]; uint8_t status=ARGUS_CONTAIN_STATUS_OF(ev->object_id); |
| 195 | if (ev->kind==ARGUS_EV_CONTAINMENT_DECIDED) { |
| 196 | if ((p->state==ST_PROPOSED && (status==ARGUS_CSTATUS_GRANT || status==ARGUS_CSTATUS_DENY || status==ARGUS_CSTATUS_ESCALATE)) || |
| 197 | (p->state==ST_ESCALATED && (status==ARGUS_CSTATUS_GRANT || status==ARGUS_CSTATUS_DENY))) apply_decision(s,p,ev); |
| 198 | else { if (cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc) return rc; } } |
| 199 | } else { |
| 200 | if (p->state!=ST_GRANTED) { |
| 201 | if (status==ARGUS_CSTATUS_DONE || status==ARGUS_CSTATUS_PARTIAL) { |
| 202 | if (cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED,ARGUS_SEV_CRITICAL,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc) return rc; } |
| 203 | } else { if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc)return rc; } } |
| 204 | } else if (status==ARGUS_CSTATUS_FAILED_SCOPE || |
| 205 | (status==ARGUS_CSTATUS_DONE && (uint32_t)ev->resource!=1u) || |
| 206 | (status==ARGUS_CSTATUS_PARTIAL && (uint32_t)ev->resource>1u)) { |
| 207 | close_pending(s,p,ST_FAILED_SCOPE); s->health.failed_scope++; |
| 208 | if (cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED,ARGUS_SEV_CRITICAL,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc) return rc; } |
| 209 | } else if (status==ARGUS_CSTATUS_DONE) { |
| 210 | if (p->kind4_count==1) { close_pending(s,p,ST_CONFIRMED); s->health.confirmed++; } |
| 211 | else { if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc)return rc; } close_pending(s,p,ST_FAILED); s->health.failed++; } |
| 212 | } else if (status==ARGUS_CSTATUS_FAILED || status==ARGUS_CSTATUS_UNAVAILABLE) { |
| 213 | close_pending(s,p,ST_FAILED); if(status==ARGUS_CSTATUS_FAILED) s->health.failed++; else s->health.unavailable++; |
| 214 | } else { |
| 215 | if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED,ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); if(rc)return rc; } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } else if (ev->kind==ARGUS_EV_CONTAINMENT_PROPOSED) { |
| 220 | int ix=find_pending(s,ev->world_generation); |
| 221 | if (ix>=0 && s->pending[ix].state==ST_PROPOSED && echo_matches(&s->pending[ix],ev)) |
| 222 | s->pending[ix].entered=s->events; |
| 223 | else if(cap) { |
| 224 | int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_DECISION_UNMATCHED, |
| 225 | ARGUS_SEV_HIGH,ev,ev->principal,ev->cap_id,ev->cap_generation); |
| 226 | if(rc)return rc; |
| 227 | } |
| 228 | } else if (ev->kind==ARGUS_EV_CAPABILITY_GRANTED && ev->principal==43u) { |
| 229 | s->executor_cap_id=ev->cap_id; |
| 230 | } else if (ev->kind==ARGUS_EV_CAPABILITY_REVOKED) { |
| 231 | int matched=0; |
| 232 | for (size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) { |
| 233 | Pending *p=&s->pending[i]; |
| 234 | if (p->state!=ST_FREE && p->req.target.cap_id==ev->cap_id && p->req.target.generation==ev->cap_generation) { |
| 235 | matched=1; |
| 236 | if (p->state!=ST_GRANTED) { |
| 237 | if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED,ARGUS_SEV_CRITICAL,ev,p->req.principal,p->req.target.cap_id,p->req.target.generation); if(rc)return rc; } |
| 238 | continue; |
| 239 | } |
| 240 | p->kind4_count++; p->entered=s->events; |
| 241 | if (p->kind4_count>1) { |
| 242 | close_pending(s,p,ST_FAILED_SCOPE); s->health.failed_scope++; |
| 243 | if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED,ARGUS_SEV_CRITICAL,ev,p->req.principal,p->req.target.cap_id,p->req.target.generation); if(rc)return rc; } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | if (!matched) for (size_t i=0;i<ARGUS_CONTAIN_RECENT;i++) { |
| 248 | const Recent *r=&s->recent[i]; |
| 249 | if(r->used && r->cap_id==ev->cap_id && r->generation==ev->cap_generation) { |
| 250 | if(cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED,ARGUS_SEV_CRITICAL,ev,r->principal,r->cap_id,r->generation); if(rc)return rc; } |
| 251 | matched=1; break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | /* Timeout is measured from the latest valid state transition, including 90 -> 91 |
| 256 | * and GRANT -> observer revoke. Expire only after the full interval has elapsed. */ |
| 257 | for (size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) { |
| 258 | Pending *p=&s->pending[i]; |
| 259 | if (is_open(p->state) && s->events-p->entered>ARGUS_CONTAIN_TIMEOUT_EVENTS) { |
| 260 | close_pending(s,p,ST_EXPIRED); s->health.expired++; |
| 261 | if (cap) { int rc=push_finding(s,out,cap,&n,ARGUS_F_CONTAINMENT_UNANSWERED,ARGUS_SEV_MEDIUM,ev,p->req.principal,p->req.target.cap_id,p->req.target.generation); if(rc)return rc; } |
| 262 | } |
| 263 | } |
| 264 | *n_out=n; return ARGUS_OK; |
| 265 | } |
| 266 | |
| 267 | static int protected_target(const ArgusContain *s, const ArgusStateOps *ops, const ArgusStateView *view, |
| 268 | const ArgusFinding *f, const ArgusCapShadow *c, int have_cap) |
| 269 | { |
| 270 | if (f->cap_id==0 || f->cap_id==ARGUS_CAP_NONE || f->cap_id==s->executor_cap_id) return 1; |
| 271 | if (!have_cap) return 0; |
| 272 | if (c->subject==ARGUS_SUBJ_AEGIS || c->subject==ARGUS_SUBJ_AEGIS_ROOT || c->subject==43u || |
| 273 | (c->rights&ARGUS_CAP_RIGHT_PRIVILEGED)!=0) return 1; |
| 274 | for (uint32_t id=0;id<ARGUS_CAP_MAX;id++) { |
| 275 | ArgusCapShadow other={0}; |
| 276 | if (ops->cap(view,id,&other)==ARGUS_OK && other.state==ARGUS_SHADOW_LIVE && |
| 277 | other.subject==c->subject && (other.rights&ARGUS_CAP_RIGHT_PRIVILEGED)!=0) return 1; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | static int never_propose(uint16_t code) |
| 282 | { return code==11 || code==12 || code==13 || code==16 || (code>=17 && code<=21); } |
| 283 | |
| 284 | static uint8_t saturation_request_type(uint16_t kind, uint8_t *bit) |
| 285 | { |
| 286 | switch(kind) { |
| 287 | case ARGUS_EV_MACHINE_JOINED: *bit=0x1u; return ARGUS_CONTAIN_REQUIRE_REATTESTATION; |
| 288 | case ARGUS_EV_CREDENTIAL_LEASE_CREATED: *bit=0x2u; return ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE; |
| 289 | case ARGUS_EV_PROVIDER_DISCOVERED: *bit=0x4u; return ARGUS_CONTAIN_QUARANTINE_PROVIDER; |
| 290 | case ARGUS_EV_ARTIFACT_ADMITTED: *bit=0x8u; return ARGUS_CONTAIN_REJECT_ARTIFACT; |
| 291 | default: *bit=0; return ARGUS_CONTAIN_NONE; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | int argus_contain_propose(ArgusContain *s, const ArgusCore *core, |
| 296 | const ArgusFinding *findings, size_t nf, const ArgusEvent *trigger, |
| 297 | ArgusContainmentRequest *requests, size_t rcap, |
| 298 | ArgusEvent *events, size_t ecap, size_t *n_out) |
| 299 | { |
| 300 | if(!s || !core || (nf && (!findings || !trigger)) || !n_out || (rcap && !requests) || (ecap && !events)) return ARGUS_ERR_ARG; |
| 301 | size_t n=0; const ArgusStateOps *ops=argus_core_ops(); const ArgusStateView *view=argus_core_view(core); |
| 302 | for(size_t j=0;j<nf;j++) { |
| 303 | const ArgusFinding *f=&findings[j]; |
| 304 | uint8_t sat_bit=0; |
| 305 | uint8_t sat_type=(f->code==ARGUS_F_TELEMETRY_LOSS && trigger && f->severity==ARGUS_SEV_CRITICAL)? |
| 306 | saturation_request_type(trigger->kind,&sat_bit):ARGUS_CONTAIN_NONE; |
| 307 | int saturation=sat_type!=ARGUS_CONTAIN_NONE; |
| 308 | uint8_t request_type=saturation?sat_type:f->containment; |
| 309 | if(f->confidence!=ARGUS_CONF_DETERMINISTIC || f->severity<ARGUS_SEV_HIGH || |
| 310 | request_type==ARGUS_CONTAIN_NONE || request_type>ARGUS_CONTAIN_MAX || |
| 311 | (never_propose(f->code) && !saturation)) continue; |
| 312 | if(saturation && (s->saturation_mask&sat_bit)) { s->health.suppressed_dedup++; continue; } |
| 313 | if(s->next_request_id==0 || s->next_request_id==UINT64_MAX || |
| 314 | s->next_proposal_sequence==0 || s->next_proposal_sequence==UINT64_MAX) return ARGUS_ERR_FULL; |
| 315 | ArgusCapShadow c={0}; int has=ops->cap(view,f->cap_id,&c)==ARGUS_OK && c.state==ARGUS_SHADOW_LIVE; |
| 316 | if(request_type==ARGUS_CONTAIN_REVOKE_CAPABILITY && protected_target(s,ops,view,f,&c,has)) { s->health.suppressed_protected++; continue; } |
| 317 | if(request_type==ARGUS_CONTAIN_REVOKE_CAPABILITY && (!has || c.generation==0)) continue; |
| 318 | ArgusContainmentRequest q={0}; |
| 319 | uint64_t incident=0; (void)argus_core_incident(core,f->principal,f->code,&incident); |
| 320 | q.incident_id=incident; q.containment=request_type; q.severity=f->severity; q.finding_code=f->code; |
| 321 | q.principal=(request_type==ARGUS_CONTAIN_REVOKE_CAPABILITY)?c.subject:f->principal; |
| 322 | q.target.cap_id=(request_type==ARGUS_CONTAIN_REVOKE_CAPABILITY)?f->cap_id:ARGUS_CAP_NONE; |
| 323 | q.target.generation=(request_type==ARGUS_CONTAIN_REVOKE_CAPABILITY)?c.generation:0; |
| 324 | if (q.containment!=ARGUS_CONTAIN_LIVE_TYPE) q.flags=ARGUS_CREQ_SYNTHETIC; |
| 325 | if (saturation) q.flags|=ARGUS_CREQ_SATURATION; |
| 326 | if (trigger && q.containment==ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE) q.target_object=trigger->object_id; |
| 327 | if (trigger && (q.containment==ARGUS_CONTAIN_REJECT_ARTIFACT || q.containment==ARGUS_CONTAIN_QUARANTINE_PROVIDER)) |
| 328 | memcpy(q.target_digest,trigger->evidence_digest,ARGUS_DIGEST_LEN); |
| 329 | memcpy(q.machine_id,f->machine_id,ARGUS_MACHINE_ID_LEN); argus_finding_digest(f,q.finding_digest); |
| 330 | q.request_id=s->next_request_id; q.finding_sequence=f->sequence; q.version=ARGUS_CONTAIN_REQUEST_VERSION; |
| 331 | int duplicate=0; |
| 332 | for(size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) if(is_open(s->pending[i].state)&&same_key_req(&s->pending[i].req,&q)) {duplicate=1;break;} |
| 333 | if(duplicate) {s->health.suppressed_dedup++;continue;} |
| 334 | uint8_t retries=0; |
| 335 | for(size_t i=0;i<ARGUS_CONTAIN_RECENT;i++) if(same_key_recent(&s->recent[i],&q)) { |
| 336 | Recent *r=&s->recent[i]; |
| 337 | if(r->state==ST_CONFIRMED || r->retries>=ARGUS_CONTAIN_RETRIES || |
| 338 | (f->severity<=r->severity && s->events-r->last_event<ARGUS_CONTAIN_COOLDOWN_EVENTS)) {s->health.suppressed_cooldown++;duplicate=1;break;} |
| 339 | retries=(uint8_t)(r->retries+1u); r->retries=retries; |
| 340 | } |
| 341 | if(duplicate) continue; |
| 342 | if(s->window_count>=ARGUS_CONTAIN_WINDOW_MAX) {s->health.suppressed_budget++;continue;} |
| 343 | int ix=pending_slot(s); |
| 344 | if(ix<0) { |
| 345 | s->health.pending_full++; |
| 346 | if(!s->overflow_reported) { |
| 347 | s->overflow_reported=1; s->overflow_pending=1; |
| 348 | memcpy(s->overflow_machine_id,f->machine_id,ARGUS_MACHINE_ID_LEN); |
| 349 | } |
| 350 | continue; |
| 351 | } |
| 352 | if(n>=rcap || n>=ecap) return ARGUS_ERR_OVERFLOW; |
| 353 | Pending *p=&s->pending[ix]; memset(p,0,sizeof *p); p->req=q; p->state=ST_PROPOSED; p->entered=s->events; p->retries=retries; |
| 354 | argus_contain_request_digest(&q,p->digest); requests[n]=q; |
| 355 | ArgusEvent *e=&events[n]; memset(e,0,sizeof *e); e->version=ARGUS_ABI_VERSION; e->class_=ARGUS_CLASS_CRITICAL; |
| 356 | e->kind=ARGUS_EV_CONTAINMENT_PROPOSED; e->outcome=ARGUS_OUTCOME_OK; e->flags=ARGUS_FLAG_CONSUMER; |
| 357 | e->sequence=s->next_proposal_sequence++; e->principal=q.principal; e->cap_id=q.target.cap_id; e->cap_generation=q.target.generation; |
| 358 | e->world_generation=q.request_id; e->object_id=ARGUS_CONTAIN_PACK(q.containment,ARGUS_CSTATUS_PROPOSED,q.finding_code); |
| 359 | e->resource=q.finding_sequence; memcpy(e->machine_id,q.machine_id,ARGUS_MACHINE_ID_LEN); memcpy(e->evidence_digest,p->digest,ARGUS_DIGEST_LEN); |
| 360 | n++; s->next_request_id++; s->window_count++; s->health.requested++; |
| 361 | if (saturation) s->saturation_mask|=sat_bit; |
| 362 | } |
| 363 | *n_out=n; return ARGUS_OK; |
| 364 | } |
| 365 | |
| 366 | void argus_contain_health(const ArgusContain *s, ArgusContainHealth *out) |
| 367 | { if(s&&out) *out=s->health; } |
| 368 | |
| 369 | int argus_contain_take_overflow_event(ArgusContain *s, ArgusEvent *out) |
| 370 | { |
| 371 | if(!s || !out) return ARGUS_ERR_ARG; |
| 372 | if(!s->overflow_pending) return ARGUS_ERR_STATE; |
| 373 | if(s->next_proposal_sequence==0 || s->next_proposal_sequence==UINT64_MAX) return ARGUS_ERR_FULL; |
| 374 | memset(out,0,sizeof *out); out->version=ARGUS_ABI_VERSION; out->class_=ARGUS_CLASS_CRITICAL; |
| 375 | out->kind=ARGUS_EV_TELEMETRY_DROPPED; out->effect_class=ARGUS_EFFECT_NONE; out->outcome=ARGUS_OUTCOME_ERROR; |
| 376 | out->flags=ARGUS_FLAG_CONSUMER; out->sequence=s->next_proposal_sequence++; |
| 377 | out->code=ARGUS_ERR_FULL; out->cap_id=ARGUS_CAP_NONE; out->object_id=ARGUS_CLASS_CRITICAL; out->resource=1; |
| 378 | memcpy(out->machine_id,s->overflow_machine_id,ARGUS_MACHINE_ID_LEN); s->overflow_pending=0; |
| 379 | return ARGUS_OK; |
| 380 | } |
| 381 | |
| 382 | void argus_contain_state_digest(const ArgusContain *s, uint8_t out[ARGUS_DIGEST_LEN]) |
| 383 | { |
| 384 | if(!out) return; |
| 385 | if(!s) {memset(out,0,ARGUS_DIGEST_LEN);return;} |
| 386 | uint8_t b[ARGUS_CONTAIN_PENDING*(ARGUS_CONTAIN_REQUEST_SIZE+56)+ARGUS_CONTAIN_RECENT*80+256]; size_t n=0; |
| 387 | static const uint8_t domain[]="AIENOS-ARGUS-CONTAIN-STATE-V1\0"; memcpy(b+n,domain,sizeof domain);n+=sizeof domain; |
| 388 | wr64(b+n,s->events);n+=8; wr64(b+n,s->next_request_id);n+=8; wr64(b+n,s->next_proposal_sequence);n+=8; wr64(b+n,s->window_start);n+=8; wr32(b+n,s->window_count);n+=4; wr32(b+n,s->executor_cap_id);n+=4; b[n++]=s->saturation_mask; b[n++]=s->overflow_reported; b[n++]=s->overflow_pending; memcpy(b+n,s->overflow_machine_id,ARGUS_MACHINE_ID_LEN);n+=ARGUS_MACHINE_ID_LEN; |
| 389 | for(size_t i=0;i<ARGUS_CONTAIN_PENDING;i++) { const Pending *p=&s->pending[i]; b[n++]=p->state; if(p->state==ST_FREE) continue; |
| 390 | uint8_t enc[ARGUS_CONTAIN_REQUEST_SIZE]; (void)argus_contain_request_encode(&p->req,enc); memcpy(b+n,enc,sizeof enc);n+=sizeof enc; |
| 391 | memcpy(b+n,p->digest,ARGUS_DIGEST_LEN);n+=ARGUS_DIGEST_LEN; b[n++]=p->kind4_count; wr64(b+n,p->entered);n+=8;wr64(b+n,p->decision_id);n+=8; } |
| 392 | for(size_t i=0;i<ARGUS_CONTAIN_RECENT;i++) { const Recent *r=&s->recent[i]; b[n++]=r->used; if(!r->used)continue; |
| 393 | b[n++]=r->type;b[n++]=r->severity;b[n++]=r->state;b[n++]=r->retries;wr32(b+n,r->principal);n+=4;wr32(b+n,r->cap_id);n+=4;wr64(b+n,r->generation);n+=8;wr32(b+n,r->target_object);n+=4;wr16(b+n,r->finding_code);n+=2;wr64(b+n,r->last_event);n+=8;wr64(b+n,r->request_id);n+=8;memcpy(b+n,r->digest,ARGUS_DIGEST_LEN);n+=ARGUS_DIGEST_LEN; } |
| 394 | const uint64_t health[]={s->health.requested,s->health.granted,s->health.denied,s->health.escalated, |
| 395 | s->health.confirmed,s->health.failed,s->health.failed_scope,s->health.unavailable,s->health.expired, |
| 396 | s->health.suppressed_dedup,s->health.suppressed_cooldown,s->health.suppressed_budget, |
| 397 | s->health.suppressed_protected,s->health.pending_full,s->health.decisions_unmatched, |
| 398 | s->health.executions_unauthorized,s->health.executions_unconfirmed}; |
| 399 | for(size_t i=0;i<sizeof health/sizeof health[0];i++) { wr64(b+n,health[i]); n+=8; } |
| 400 | sha256_hash(b,n,out); |
| 401 | } |