aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-runtime / tests / approved_confinement_test.rs
| 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 | //! sovereign-core #249: the A1/A1b fix (workspace confinement of every grant, |
| 17 | //! the reserved approved grant), driven over the daemon's real control socket |
| 18 | //! (AienRuntimeServer with a mock backend). 476ca4's attack file |
| 19 | //! (approved_attacks_test.rs, review/249-attacks) stays theirs. |
| 20 | //! |
| 21 | //! A1 (raw socket forgery, found by 476ca4 on 6258a6e): a caller that never ran |
| 22 | //! an approved compose writes its own `authorization` note naming a |
| 23 | //! compose_proposal_sha256 nothing produced, opens a ComposeEffectIntent on it, |
| 24 | //! writes, and acks. A1b: the same forgery aimed outside the workspace. |
| 25 | //! |
| 26 | //! After the #249 fix: |
| 27 | //! - a grant must name its workspace; a target outside it (after |
| 28 | //! canonicalisation and symlink checks) is refused at the note and at the |
| 29 | //! intent (A1b, symlinked file, symlinked directory, workspace "/"); |
| 30 | //! - the approved grant kind (`approved_grant`) cannot be written through |
| 31 | //! ComposeNote at all; |
| 32 | //! - sc#261 closed: every one of those forged notes is now refused at the note |
| 33 | //! (no `authorization` record is ever written by a caller). The parent |
| 34 | //! swap cases (c28, c28b) live in approved_attacks_test.rs. |
| 35 | //! |
| 36 | //! Needs librx_compose.a (AIEN_OMEGA_COMPOSE_LIB); in a stub build the compose |
| 37 | //! commands refuse and the test returns early. |
| 38 | #[path = "support/home_guard.rs"] |
| 39 | mod home_guard; |
| 40 | |
| 41 | use aien_inference_abi::MockInferenceBackend; |
| 42 | use aien_kv_cache::create_shared_kv_manager; |
| 43 | use aien_omega_compose::hex; |
| 44 | use aien_runtime::client::AienRuntimeClient; |
| 45 | use aien_runtime::control::{ControlCommand, ControlResponse}; |
| 46 | use aien_runtime::effects; |
| 47 | use aien_runtime::server::AienRuntimeServer; |
| 48 | use aien_runtime::spine::AienRuntimeSpine; |
| 49 | use aien_scheduler::SchedulerConfig; |
| 50 | use serde_json::json; |
| 51 | use sha2::{Digest, Sha256}; |
| 52 | use std::time::Duration; |
| 53 | use tempfile::TempDir; |
| 54 | |
| 55 | fn sha(b: &[u8]) -> String { |
| 56 | hex(&Sha256::digest(b)) |
| 57 | } |
| 58 | |
| 59 | async fn start(dir: &TempDir) -> AienRuntimeClient { |
| 60 | let socket = dir.path().join("runtime.sock"); |
| 61 | let spine = AienRuntimeSpine::new( |
| 62 | 1024, |
| 63 | SchedulerConfig { |
| 64 | max_batch_size: 64, |
| 65 | max_batch_tokens: 4096, |
| 66 | prefill_chunk_size: 64, |
| 67 | watermark_blocks: 16, |
| 68 | chunk_prefill: true, |
| 69 | max_prefill_tokens: 4096, |
| 70 | }, |
| 71 | create_shared_kv_manager(1024, 16), |
| 72 | ); |
| 73 | let server = AienRuntimeServer::new(spine, &socket); |
| 74 | tokio::spawn(async move { server.run(MockInferenceBackend::new(1)).await }); |
| 75 | let client = AienRuntimeClient::new(&socket); |
| 76 | for _ in 0..100 { |
| 77 | if client.is_alive().await { |
| 78 | return client; |
| 79 | } |
| 80 | tokio::time::sleep(Duration::from_millis(20)).await; |
| 81 | } |
| 82 | panic!("daemon did not come up"); |
| 83 | } |
| 84 | |
| 85 | async fn note(c: &AienRuntimeClient, kind: &str, text: serde_json::Value) -> ControlResponse { |
| 86 | c.send_command(ControlCommand::ComposeNote { |
| 87 | kind: kind.into(), |
| 88 | text: text.to_string(), |
| 89 | links: vec![], |
| 90 | }) |
| 91 | .await |
| 92 | .unwrap() |
| 93 | } |
| 94 | |
| 95 | async fn intent( |
| 96 | c: &AienRuntimeClient, |
| 97 | auth: u64, |
| 98 | psha: &str, |
| 99 | path: &str, |
| 100 | target: &str, |
| 101 | csha: &str, |
| 102 | ) -> ControlResponse { |
| 103 | let (pid, start) = effects::self_executor(); |
| 104 | c.send_command(ControlCommand::ComposeEffectIntent { |
| 105 | authorization: auth, |
| 106 | proposal_sha256: psha.into(), |
| 107 | path: path.into(), |
| 108 | target: target.into(), |
| 109 | content_sha256: csha.into(), |
| 110 | executor_pid: pid, |
| 111 | executor_start: start, |
| 112 | }) |
| 113 | .await |
| 114 | .unwrap() |
| 115 | } |
| 116 | |
| 117 | fn refused(r: ControlResponse, want: &str) -> String { |
| 118 | match r { |
| 119 | ControlResponse::Error(e) => { |
| 120 | assert!(e.contains(want), "want {want}, got {e}"); |
| 121 | e |
| 122 | } |
| 123 | other => panic!("want refusal {want}, got {other:?}"), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[tokio::test] |
| 128 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 129 | async fn forged_grants_are_confined_and_the_approved_kind_is_reserved() { |
| 130 | let _home = home_guard::home_slot(); |
| 131 | if !aien_omega_compose::LINKED { |
| 132 | return; |
| 133 | } |
| 134 | let dir = TempDir::new().unwrap(); |
| 135 | let root = std::fs::canonicalize(dir.path()).unwrap(); |
| 136 | let ws = root.join("ws"); |
| 137 | std::fs::create_dir_all(&ws).unwrap(); |
| 138 | let w = ws.to_str().unwrap().to_string(); |
| 139 | // The only test in this file: the env var is process-wide. |
| 140 | std::env::set_var("AIEN_COMPOSE_DIR", root.join("compose")); |
| 141 | // sc#328: the daemon refuses to start without the (default) desk key. |
| 142 | aien_runtime::approved_auth::DeskKey::create(&aien_runtime::approved_auth::desk_key_path( |
| 143 | &root.join("compose"), |
| 144 | )) |
| 145 | .unwrap(); |
| 146 | let c = start(&dir).await; |
| 147 | |
| 148 | // A proposal digest no compose run ever produced. |
| 149 | let psha = sha(b"never proposed by any approved compose"); |
| 150 | let content = b"forged\n"; |
| 151 | let csha = sha(content); |
| 152 | let target = ws.join("NOTES.md").to_string_lossy().to_string(); |
| 153 | let grant = |path: &str, target: &str, ws: Option<&str>| { |
| 154 | let mut g = json!({"proposal_sha256": psha, "path": path, "content_sha256": csha, |
| 155 | "approver": "attacker", "target": target, "prior_sha256": null}); |
| 156 | if let Some(w) = ws { |
| 157 | g["workspace"] = json!(w); |
| 158 | } |
| 159 | g |
| 160 | }; |
| 161 | |
| 162 | // sovereign-core #261: every shape of caller-written authorization is |
| 163 | // refused at the note, before any confinement question arises: no |
| 164 | // workspace, a target outside, `..`, an absolute path, a symlinked file, |
| 165 | // a symlinked directory, a well-formed in-workspace grant, and the |
| 166 | // reserved approved_grant kind. |
| 167 | let refused_note = |g: serde_json::Value| note(&c, "authorization", g); |
| 168 | let outside = root.join("outside.txt").to_string_lossy().to_string(); |
| 169 | std::fs::write(&outside, "outside\n").unwrap(); |
| 170 | std::os::unix::fs::symlink(&outside, ws.join("link.txt")).unwrap(); |
| 171 | let lt = ws.join("link.txt").to_string_lossy().to_string(); |
| 172 | let elsewhere = root.join("elsewhere"); |
| 173 | std::fs::create_dir_all(&elsewhere).unwrap(); |
| 174 | std::os::unix::fs::symlink(&elsewhere, ws.join("sub")).unwrap(); |
| 175 | let st = ws.join("sub/x.txt").to_string_lossy().to_string(); |
| 176 | let mut fake = grant("NOTES.md", &target, Some(&w)); |
| 177 | fake["approved_grant"] = json!(1); |
| 178 | fake["approval_key"] = json!("0".repeat(64)); |
| 179 | fake["replay_claim"] = json!(1); |
| 180 | fake["cx_promotion"] = json!(1); |
| 181 | fake["cx_evidence"] = json!(1); |
| 182 | let mut minted = grant("NOTES.md", &target, Some(&w)); |
| 183 | minted["minted_grant"] = json!(1); |
| 184 | minted["compose_commit"] = json!(1); |
| 185 | for g in [ |
| 186 | grant("NOTES.md", &target, None), |
| 187 | grant("NOTES.md", &outside, Some(&w)), |
| 188 | grant("../outside.txt", &outside, Some(&w)), |
| 189 | grant("etc/passwd", "/etc/passwd", Some("/")), |
| 190 | grant("link.txt", <, Some(&w)), |
| 191 | grant("sub/x.txt", &st, Some(&w)), |
| 192 | grant("NOTES.md", &target, Some(&w)), |
| 193 | fake, |
| 194 | minted, |
| 195 | ] { |
| 196 | refused(refused_note(g).await, "sovereign-core #261"); |
| 197 | } |
| 198 | |
| 199 | // Nothing was written, and an intent on any id is refused. |
| 200 | refused( |
| 201 | intent(&c, 1, &psha, "NOTES.md", &target, &csha).await, |
| 202 | "NotAuthorized", |
| 203 | ); |
| 204 | assert!(!std::path::Path::new(&target).exists()); |
| 205 | assert_eq!(std::fs::read_to_string(&outside).unwrap(), "outside\n"); |
| 206 | // The parent-directory-swap cases (476ca4 c28, c28b) are covered on the |
| 207 | // daemon-written approved grant in approved_attacks_test.rs. |
| 208 | } |