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 / test_argus_contain_hostile.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_abi.h" |
| 18 | #include "../argus_contain.h" |
| 19 | #include "../bridge/argus_aegis_bridge.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | static int checks, failures; |
| 26 | #define CHECK(x) do { checks++; if (!(x)) { failures++; fprintf(stderr,"FAIL %s:%d: %s\n",__FILE__,__LINE__,#x); } } while(0) |
| 27 | |
| 28 | typedef struct { |
| 29 | AienosCapAdmin *admin; |
| 30 | AienosCapView *view; |
| 31 | AienosCapRef office; |
| 32 | AienosContain *gate; |
| 33 | AienosContainTablePolicy policy; |
| 34 | ArgusCore *core; |
| 35 | ArgusContain *contain; |
| 36 | ArgusAegisBridge *bridge; |
| 37 | void *core_mem, *contain_mem, *gate_mem, *bridge_mem; |
| 38 | uint8_t machine[ARGUS_MACHINE_ID_LEN]; |
| 39 | } Rig; |
| 40 | |
| 41 | static void setup(Rig *r) { |
| 42 | memset(r,0,sizeof(*r)); |
| 43 | CHECK(aienos_cap_start(&r->admin,&r->view)==AIENOS_CAP_OK); |
| 44 | CHECK(aienos_cap_office(r->admin,&r->office)==AIENOS_CAP_OK); |
| 45 | r->core_mem=calloc(1,argus_core_footprint()); |
| 46 | r->contain_mem=calloc(1,argus_contain_footprint()); |
| 47 | r->gate_mem=calloc(1,aienos_contain_footprint()); |
| 48 | r->bridge_mem=calloc(1,argus_aegis_bridge_footprint()); |
| 49 | CHECK(r->core_mem&&r->contain_mem&&r->gate_mem&&r->bridge_mem); |
| 50 | CHECK(argus_core_init(&r->core,r->core_mem,argus_core_footprint())==ARGUS_OK); |
| 51 | CHECK(argus_contain_init(&r->contain,r->contain_mem,argus_contain_footprint())==ARGUS_OK); |
| 52 | r->machine[0]=0xA1; |
| 53 | CHECK(argus_aegis_bridge_init(&r->bridge,r->bridge_mem,argus_aegis_bridge_footprint(), |
| 54 | r->core,r->contain,&r->gate,r->view,r->machine)==ARGUS_OK); |
| 55 | ArgusEvent joined={0}; joined.version=ARGUS_ABI_VERSION; joined.class_=ARGUS_CLASS_SECURITY; |
| 56 | joined.kind=ARGUS_EV_MACHINE_JOINED; joined.outcome=ARGUS_OUTCOME_OK; |
| 57 | joined.flags=(uint16_t)((1u<<ARGUS_FLAG_STREAM_SHIFT)|ARGUS_FLAG_SYNTHETIC); |
| 58 | joined.sequence=1; joined.object_id=ARGUS_TRUST_OBSERVED; joined.cap_id=ARGUS_CAP_NONE; |
| 59 | memcpy(joined.machine_id,r->machine,sizeof joined.machine_id); |
| 60 | CHECK(argus_aegis_bridge_ingest(r->bridge,&joined)==ARGUS_OK); |
| 61 | r->policy.verdict[AIENOS_CONTAIN_REVOKE_CAPABILITY]=AIENOS_CONTAIN_GRANT; |
| 62 | AienosContainAuthorizer az={aienos_contain_table_decide,&r->policy}; |
| 63 | CHECK(aienos_cap_set_observer(r->admin,argus_aegis_bridge_observer,r->bridge)==AIENOS_CAP_OK); |
| 64 | CHECK(aienos_contain_create(&r->gate,r->gate_mem,aienos_contain_footprint(),r->admin,r->view,r->office,&az)==AIENOS_CONTAIN_OK); |
| 65 | CHECK(argus_aegis_bridge_attach_gate(r->bridge)==ARGUS_OK); |
| 66 | } |
| 67 | |
| 68 | static void teardown(Rig *r) { |
| 69 | aienos_cap_set_observer(r->admin,NULL,NULL); |
| 70 | aienos_contain_destroy(r->gate); |
| 71 | argus_aegis_bridge_destroy(r->bridge); |
| 72 | aienos_cap_stop(r->admin,r->view); |
| 73 | free(r->bridge_mem); free(r->gate_mem); free(r->contain_mem); free(r->core_mem); |
| 74 | } |
| 75 | |
| 76 | static AienosCapRef mint(Rig *r,uint32_t subject,uint32_t rights) { |
| 77 | AienosCapMint m={0,subject,0x9000u+subject,rights,0,{AIENOS_CAP_PARENT_NONE,0},r->office}; |
| 78 | AienosCapRef out={AIENOS_CAP_PARENT_NONE,0}; |
| 79 | CHECK(aienos_cap_mint(r->admin,&m,&out)==AIENOS_CAP_OK); |
| 80 | return out; |
| 81 | } |
| 82 | |
| 83 | static AienosContainRequest request(uint64_t id,uint32_t subject,AienosCapRef target) { |
| 84 | AienosContainRequest q={0}; |
| 85 | q.containment=AIENOS_CONTAIN_REVOKE_CAPABILITY; q.severity=ARGUS_SEV_HIGH; |
| 86 | q.finding_code=ARGUS_F_STALE_GENERATION; q.principal=subject; q.target=target; |
| 87 | q.request_id=id; q.finding_sequence=id; q.version=AIENOS_CONTAIN_REQUEST_VERSION; |
| 88 | q.incident_id=id; |
| 89 | q.finding_digest[0]=(uint8_t)id; |
| 90 | return q; |
| 91 | } |
| 92 | |
| 93 | static int live(Rig *r,AienosCapRef c) { |
| 94 | AienosCapEntry e; |
| 95 | return aienos_cap_inspect(r->view,c,&e)==AIENOS_CAP_OK && e.state==AIENOS_CAP_STATE_LIVE; |
| 96 | } |
| 97 | |
| 98 | static ArgusEvent receipt_event(Rig *r,uint16_t kind,uint64_t seq, |
| 99 | const ArgusContainmentRequest *q,uint8_t status) { |
| 100 | ArgusEvent e={0}; e.version=ARGUS_ABI_VERSION; e.class_=ARGUS_CLASS_CRITICAL; |
| 101 | e.kind=kind; e.outcome=ARGUS_OUTCOME_OK; e.flags=(uint16_t)(2u<<ARGUS_FLAG_STREAM_SHIFT); |
| 102 | e.sequence=seq; e.cap_id=q?q->target.cap_id:ARGUS_CAP_NONE; |
| 103 | e.cap_generation=q?q->target.generation:0; e.principal=q?q->principal:0; |
| 104 | e.world_generation=q?q->request_id:999; |
| 105 | e.object_id=ARGUS_CONTAIN_PACK(q?q->containment:ARGUS_CONTAIN_REVOKE_CAPABILITY, |
| 106 | status,q?q->finding_code:ARGUS_F_STALE_GENERATION); |
| 107 | e.resource=kind==ARGUS_EV_CONTAINMENT_DECIDED?77:1; |
| 108 | memcpy(e.machine_id,r->machine,sizeof e.machine_id); |
| 109 | if(q) argus_contain_request_digest(q,e.evidence_digest); |
| 110 | return e; |
| 111 | } |
| 112 | |
| 113 | static int take_code(Rig *r,uint16_t kind,uint64_t seq, |
| 114 | const ArgusContainmentRequest *q,uint8_t status,uint16_t wanted) { |
| 115 | ArgusEvent e=receipt_event(r,kind,seq,q,status); |
| 116 | CHECK(argus_aegis_bridge_ingest(r->bridge,&e)==ARGUS_OK); |
| 117 | ArgusFinding f[16]; size_t n=argus_aegis_bridge_take_findings(r->bridge,f,16); |
| 118 | for(size_t i=0;i<n;i++) if(f[i].code==wanted) return 1; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static ArgusContainmentRequest make_proposal(Rig *r,AienosCapRef target) { |
| 123 | ArgusFinding f={0}; f.code=ARGUS_F_STALE_GENERATION; f.severity=ARGUS_SEV_CRITICAL; |
| 124 | f.confidence=ARGUS_CONF_DETERMINISTIC; f.containment=ARGUS_CONTAIN_REVOKE_CAPABILITY; |
| 125 | f.sequence=2; f.principal=7; f.cap_id=target.cap_id; f.cap_generation=target.generation-1; |
| 126 | memcpy(f.machine_id,r->machine,sizeof f.machine_id); |
| 127 | ArgusEvent trigger={0}; trigger.sequence=2; trigger.kind=ARGUS_EV_STALE_GENERATION; |
| 128 | trigger.cap_id=target.cap_id; trigger.cap_generation=target.generation-1; |
| 129 | memcpy(trigger.machine_id,r->machine,sizeof trigger.machine_id); |
| 130 | ArgusContainmentRequest q={0}; ArgusEvent prop={0}; size_t n=0; |
| 131 | CHECK(argus_contain_propose(r->contain,r->core,&f,1,&trigger,&q,1,&prop,1,&n)==ARGUS_OK && n==1); |
| 132 | CHECK(argus_aegis_bridge_ingest(r->bridge,&prop)==ARGUS_OK); |
| 133 | return q; |
| 134 | } |
| 135 | |
| 136 | static void hostile_receipt_transitions(void) { |
| 137 | Rig r; setup(&r); AienosCapRef c=mint(&r,7,AIENOS_CAP_RIGHT_READ); |
| 138 | CHECK(take_code(&r,ARGUS_EV_CONTAINMENT_DECIDED,1,NULL,ARGUS_CSTATUS_GRANT, |
| 139 | ARGUS_F_CONTAINMENT_DECISION_UNMATCHED)); |
| 140 | teardown(&r); |
| 141 | |
| 142 | setup(&r); c=mint(&r,7,AIENOS_CAP_RIGHT_READ); |
| 143 | ArgusContainmentRequest q=make_proposal(&r,c); |
| 144 | CHECK(take_code(&r,ARGUS_EV_CONTAINMENT_EXECUTED,1,&q,ARGUS_CSTATUS_DONE, |
| 145 | ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED)); |
| 146 | teardown(&r); |
| 147 | |
| 148 | setup(&r); c=mint(&r,7,AIENOS_CAP_RIGHT_READ); |
| 149 | q=make_proposal(&r,c); |
| 150 | ArgusEvent grant=receipt_event(&r,ARGUS_EV_CONTAINMENT_DECIDED,1,&q,ARGUS_CSTATUS_GRANT); |
| 151 | CHECK(argus_aegis_bridge_ingest(r.bridge,&grant)==ARGUS_OK); |
| 152 | ArgusFinding scratch[8]; size_t nf=argus_aegis_bridge_take_findings(r.bridge,scratch,8); |
| 153 | CHECK(nf==0); |
| 154 | CHECK(take_code(&r,ARGUS_EV_CONTAINMENT_EXECUTED,2,&q,ARGUS_CSTATUS_DONE, |
| 155 | ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED)); |
| 156 | CHECK(live(&r,c)); |
| 157 | teardown(&r); |
| 158 | } |
| 159 | |
| 160 | static void hostile_digest_and_scope(void) { |
| 161 | Rig r; setup(&r); |
| 162 | AienosCapRef c=mint(&r,7,AIENOS_CAP_RIGHT_READ); |
| 163 | AienosContainRequest q=request(1,7,c); uint8_t d[AIENOS_CONTAIN_DIGEST_LEN]; |
| 164 | AienosContainDecision dec={0}; aienos_contain_request_digest(&q,d); d[0]^=0x80; |
| 165 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK); |
| 166 | CHECK(dec.status==AIENOS_CONTAIN_DENY && live(&r,c)); |
| 167 | |
| 168 | /* Mint while the observer is detached: the gate cannot trust this target's lineage. */ |
| 169 | CHECK(aienos_cap_set_observer(r.admin,NULL,NULL)==AIENOS_CAP_OK); |
| 170 | AienosCapRef pre=mint(&r,8,AIENOS_CAP_RIGHT_READ); |
| 171 | CHECK(aienos_cap_set_observer(r.admin,argus_aegis_bridge_observer,r.bridge)==AIENOS_CAP_OK); |
| 172 | q=request(2,8,pre); aienos_contain_request_digest(&q,d); |
| 173 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK); |
| 174 | CHECK(dec.status==AIENOS_CONTAIN_ESCALATE && !aienos_contain_inert(r.gate)); |
| 175 | AienosContainResult result={0}; |
| 176 | CHECK(aienos_contain_execute(r.gate,dec.decision_id,&result)!=AIENOS_CONTAIN_OK); |
| 177 | CHECK(live(&r,pre)); |
| 178 | printf("HOSTILE bad_digest_denied %s\n",live(&r,c)?"PASS":"FAIL"); |
| 179 | printf("HOSTILE unobserved_target_cannot_auto_revoke %s\n",live(&r,pre)?"PASS":"FAIL"); |
| 180 | teardown(&r); |
| 181 | } |
| 182 | |
| 183 | static void hostile_single_execution_and_replay(void) { |
| 184 | Rig r; setup(&r); |
| 185 | AienosCapRef c=mint(&r,11,AIENOS_CAP_RIGHT_READ); |
| 186 | AienosContainRequest q=request(10,11,c); uint8_t d[AIENOS_CONTAIN_DIGEST_LEN]; |
| 187 | AienosContainDecision dec={0}; AienosContainResult result={0}; |
| 188 | aienos_contain_request_digest(&q,d); |
| 189 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK); |
| 190 | CHECK(dec.status==AIENOS_CONTAIN_GRANT); |
| 191 | CHECK(aienos_contain_execute(r.gate,dec.decision_id,&result)==AIENOS_CONTAIN_OK); |
| 192 | CHECK(result.status==AIENOS_CONTAIN_DONE && !live(&r,c)); |
| 193 | CHECK(aienos_contain_execute(r.gate,dec.decision_id,&result)!=AIENOS_CONTAIN_OK); |
| 194 | CHECK(result.status==AIENOS_CONTAIN_FAILED); |
| 195 | |
| 196 | AienosCapRef next=mint(&r,12,AIENOS_CAP_RIGHT_READ); |
| 197 | q=request(11,12,next); aienos_contain_request_digest(&q,d); |
| 198 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK && dec.status==AIENOS_CONTAIN_GRANT); |
| 199 | CHECK(aienos_contain_submit(r.gate,&q,d,&result)==AIENOS_CONTAIN_OK); |
| 200 | CHECK(result.status==AIENOS_CONTAIN_DENY && live(&r,next)); |
| 201 | printf("HOSTILE execute_once_and_replay_denied %s\n",live(&r,next)?"PASS":"FAIL"); |
| 202 | teardown(&r); |
| 203 | } |
| 204 | |
| 205 | static void hostile_protected_and_synthetic(void) { |
| 206 | Rig r; setup(&r); |
| 207 | AienosCapRef office=r.office; |
| 208 | AienosContainRequest q=request(20,0,office); uint8_t d[AIENOS_CONTAIN_DIGEST_LEN]; |
| 209 | AienosContainDecision dec={0}; aienos_contain_request_digest(&q,d); |
| 210 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK); |
| 211 | CHECK(dec.status==AIENOS_CONTAIN_DENY && live(&r,office)); |
| 212 | |
| 213 | q=request(21,31,(AienosCapRef){AIENOS_CONTAIN_CAP_NONE,0}); |
| 214 | q.containment=AIENOS_CONTAIN_QUARANTINE_MACHINE; q.flags=AIENOS_CREQ_SYNTHETIC; |
| 215 | aienos_contain_request_digest(&q,d); |
| 216 | CHECK(aienos_contain_submit(r.gate,&q,d,&dec)==AIENOS_CONTAIN_OK); |
| 217 | CHECK(dec.status==AIENOS_CONTAIN_ESCALATE); |
| 218 | AienosContainResult result={0}; |
| 219 | CHECK(aienos_contain_execute(r.gate,dec.decision_id,&result)!=AIENOS_CONTAIN_OK); |
| 220 | CHECK(result.status==AIENOS_CONTAIN_FAILED); |
| 221 | printf("HOSTILE protected_office_and_synthetic_scope %s\n",dec.status==AIENOS_CONTAIN_ESCALATE && result.status==AIENOS_CONTAIN_FAILED?"PASS":"FAIL"); |
| 222 | teardown(&r); |
| 223 | } |
| 224 | |
| 225 | int main(void) { |
| 226 | hostile_digest_and_scope(); |
| 227 | hostile_single_execution_and_replay(); |
| 228 | hostile_protected_and_synthetic(); |
| 229 | hostile_receipt_transitions(); |
| 230 | printf("ARGUS-1 hostile containment: %d checks, %d failures\n",checks,failures); |
| 231 | return failures?1:0; |
| 232 | } |