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_attacks_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 part C: adversarial tests of the approved-write path, |
| 17 | //! each driven over the daemon's real control socket (AienRuntimeServer with a |
| 18 | //! mock backend; the approved proposal text is the proposer, no model). |
| 19 | //! |
| 20 | //! Every test asserts the SECURE outcome: it passes when the attack is refused |
| 21 | //! and fails ("ATTACK SUCCEEDED") when it is not. Tests share one process-wide |
| 22 | //! AIEN_COMPOSE_DIR, so they take turns. |
| 23 | //! |
| 24 | //! Needs librx_compose.a (AIEN_OMEGA_COMPOSE_LIB); in a stub build every test |
| 25 | //! returns early. The two crash tests need `--features fault-hold` (the |
| 26 | //! production build compiles the crash points to nothing). |
| 27 | #[path = "support/home_guard.rs"] |
| 28 | mod home_guard; |
| 29 | |
| 30 | use aien_inference_abi::MockInferenceBackend; |
| 31 | use aien_kv_cache::create_shared_kv_manager; |
| 32 | use aien_omega_compose::hex; |
| 33 | use aien_runtime::approved::{approved_proposal_sha256, ApprovedProposal}; |
| 34 | use aien_runtime::approved_auth::{desk_key_path, DeskKey}; |
| 35 | use aien_runtime::client::AienRuntimeClient; |
| 36 | use aien_runtime::control::{ControlCommand, ControlEnvelope, ControlResponse}; |
| 37 | use aien_runtime::effects; |
| 38 | use aien_runtime::server::AienRuntimeServer; |
| 39 | use aien_runtime::spine::AienRuntimeSpine; |
| 40 | use aien_scheduler::SchedulerConfig; |
| 41 | use serde_json::json; |
| 42 | use sha2::{Digest, Sha256}; |
| 43 | use std::path::{Path, PathBuf}; |
| 44 | use std::time::Duration; |
| 45 | |
| 46 | const PATH: &str = "NOTES.md"; |
| 47 | const CONTENT: &str = "approved through the daemon\n"; |
| 48 | const APPROVER: &str = "interplane-host"; |
| 49 | |
| 50 | /// One daemon at a time: AIEN_COMPOSE_DIR is process-wide. |
| 51 | static TURN: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); |
| 52 | |
| 53 | fn sha(b: &[u8]) -> String { |
| 54 | hex(&Sha256::digest(b)) |
| 55 | } |
| 56 | |
| 57 | fn home(tmp: &Path) -> PathBuf { |
| 58 | tmp.join("compose") |
| 59 | } |
| 60 | |
| 61 | fn ws(tmp: &Path) -> PathBuf { |
| 62 | let w = tmp.join("ws"); |
| 63 | std::fs::create_dir_all(&w).unwrap(); |
| 64 | w |
| 65 | } |
| 66 | |
| 67 | /// A compose home with a desk key, and a workspace beside it. |
| 68 | fn fresh() -> tempfile::TempDir { |
| 69 | let tmp = tempfile::tempdir().unwrap(); |
| 70 | DeskKey::create(&desk_key_path(&home(tmp.path()))).unwrap(); |
| 71 | ws(tmp.path()); |
| 72 | tmp |
| 73 | } |
| 74 | |
| 75 | fn unsigned(request: &str, approval: &str, path: &str, content: &str) -> ApprovedProposal { |
| 76 | ApprovedProposal { |
| 77 | request_id: request.into(), |
| 78 | trace_id: format!("trace-{request}"), |
| 79 | approval_id: approval.into(), |
| 80 | approver: APPROVER.into(), |
| 81 | path: path.into(), |
| 82 | content: content.into(), |
| 83 | approved_proposal_sha256: approved_proposal_sha256(path, content), |
| 84 | content_sha256: sha(content.as_bytes()), |
| 85 | approval_mac: String::new(), |
| 86 | requirements: Some(String::new()), |
| 87 | requirements_mac: String::new(), |
| 88 | requirements_base: None, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Signed by the real desk key of `tmp`'s compose home. |
| 93 | fn signed(tmp: &Path, request: &str, approval: &str) -> ApprovedProposal { |
| 94 | sign(tmp, unsigned(request, approval, PATH, CONTENT)) |
| 95 | } |
| 96 | |
| 97 | fn sign(tmp: &Path, mut p: ApprovedProposal) -> ApprovedProposal { |
| 98 | DeskKey::load(&desk_key_path(&home(tmp))) |
| 99 | .unwrap() |
| 100 | .seal(&mut p, &ws(tmp)); |
| 101 | p |
| 102 | } |
| 103 | |
| 104 | struct Daemon { |
| 105 | c: AienRuntimeClient, |
| 106 | socket: PathBuf, |
| 107 | h: tokio::task::JoinHandle<Result<(), String>>, |
| 108 | } |
| 109 | |
| 110 | /// Start a daemon over `tmp`'s compose home (one at a time per compose home). |
| 111 | async fn up(tmp: &Path, name: &str) -> Daemon { |
| 112 | std::env::set_var("AIEN_COMPOSE_DIR", home(tmp)); |
| 113 | let socket = tmp.join(name); |
| 114 | let cfg = SchedulerConfig { |
| 115 | max_batch_size: 8, |
| 116 | max_batch_tokens: 1024, |
| 117 | prefill_chunk_size: 64, |
| 118 | watermark_blocks: 4, |
| 119 | chunk_prefill: true, |
| 120 | max_prefill_tokens: 1024, |
| 121 | }; |
| 122 | let spine = AienRuntimeSpine::new(64, cfg, create_shared_kv_manager(256, 16)); |
| 123 | let server = AienRuntimeServer::new(spine, &socket); |
| 124 | let h = tokio::spawn(async move { server.run(MockInferenceBackend::new(1)).await }); |
| 125 | let c = AienRuntimeClient::new(&socket); |
| 126 | for _ in 0..250 { |
| 127 | if c.is_alive().await { |
| 128 | return Daemon { c, socket, h }; |
| 129 | } |
| 130 | tokio::time::sleep(Duration::from_millis(20)).await; |
| 131 | } |
| 132 | panic!("daemon did not come up"); |
| 133 | } |
| 134 | |
| 135 | async fn down(d: Daemon) { |
| 136 | let _ = d.c.send_command(ControlCommand::Shutdown).await; |
| 137 | let _ = tokio::time::timeout(Duration::from_secs(5), d.h).await; |
| 138 | } |
| 139 | |
| 140 | async fn submit(c: &AienRuntimeClient, p: &ApprovedProposal, ws: &Path) -> ControlResponse { |
| 141 | c.send_command(ControlCommand::ComposeApprovedProposal { |
| 142 | proposal: p.clone(), |
| 143 | workspace: ws.display().to_string(), |
| 144 | }) |
| 145 | .await |
| 146 | .unwrap() |
| 147 | } |
| 148 | |
| 149 | /// (records_total, every host record's text). |
| 150 | async fn journal(c: &AienRuntimeClient) -> (u64, Vec<String>) { |
| 151 | match c |
| 152 | .send_command(ControlCommand::ComposeRecall { |
| 153 | ids: vec![], |
| 154 | prefix: None, |
| 155 | }) |
| 156 | .await |
| 157 | .unwrap() |
| 158 | { |
| 159 | ControlResponse::ComposeRecalled(r) => ( |
| 160 | r.records_total, |
| 161 | r.host.iter().filter_map(|h| h.text.clone()).collect(), |
| 162 | ), |
| 163 | other => panic!("recall: {other:?}"), |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn phase_count(texts: &[String], phase: &str) -> usize { |
| 168 | let needle = format!("\"approved_submission\":\"{phase}\""); |
| 169 | texts.iter().filter(|t| t.contains(&needle)).count() |
| 170 | } |
| 171 | |
| 172 | /// The refusal (refused_by, name), or panic naming the attack as successful. |
| 173 | fn refused(r: ControlResponse, attack: &str) -> (String, String) { |
| 174 | match r { |
| 175 | ControlResponse::ComposeApprovedRefused(e) => (e.refused_by.clone(), e.name.clone()), |
| 176 | other => panic!("ATTACK SUCCEEDED ({attack}): {other:?}"), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn committed(r: ControlResponse) -> aien_runtime::approved::ApprovedComposeReport { |
| 181 | match r { |
| 182 | ControlResponse::ComposeApprovedResult(r) => *r, |
| 183 | other => panic!("honest approval did not commit: {other:?}"), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// One field of an honestly signed approval changed after signing (hashes |
| 188 | /// recomputed where the field feeds them, so only the MAC can catch it). |
| 189 | async fn tampered(attack: &str, edit: impl FnOnce(&Path, &mut ApprovedProposal)) { |
| 190 | let _t = TURN.lock().await; |
| 191 | let tmp = fresh(); |
| 192 | let d = up(tmp.path(), "s.sock").await; |
| 193 | let good = signed(tmp.path(), "req-t", "appr-t"); |
| 194 | let mut p = good.clone(); |
| 195 | edit(tmp.path(), &mut p); |
| 196 | let (n0, _) = journal(&d.c).await; |
| 197 | let (by, name) = refused(submit(&d.c, &p, &ws(tmp.path())).await, attack); |
| 198 | assert_eq!( |
| 199 | (by.as_str(), name.as_str()), |
| 200 | ("PROPOSAL_REFUSED", "Unauthenticated") |
| 201 | ); |
| 202 | assert_eq!( |
| 203 | journal(&d.c).await.0, |
| 204 | n0, |
| 205 | "{attack}: refusal appended records" |
| 206 | ); |
| 207 | // The forgery did not consume the honest approval. |
| 208 | assert_eq!( |
| 209 | committed(submit(&d.c, &good, &ws(tmp.path())).await).state, |
| 210 | "COMMITTED" |
| 211 | ); |
| 212 | down(d).await; |
| 213 | } |
| 214 | |
| 215 | fn rehash(p: &mut ApprovedProposal) { |
| 216 | p.approved_proposal_sha256 = approved_proposal_sha256(&p.path, &p.content); |
| 217 | p.content_sha256 = sha(p.content.as_bytes()); |
| 218 | } |
| 219 | |
| 220 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 221 | #[cfg_attr( |
| 222 | not(compose_linked), |
| 223 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 224 | )] |
| 225 | async fn c01_forged_approver() { |
| 226 | let _home = home_guard::home_slot(); |
| 227 | tampered("forged approver", |_, p| p.approver = "drake".into()).await; |
| 228 | } |
| 229 | |
| 230 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 231 | #[cfg_attr( |
| 232 | not(compose_linked), |
| 233 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 234 | )] |
| 235 | async fn c02a_invalid_mac_random() { |
| 236 | let _home = home_guard::home_slot(); |
| 237 | tampered("random MAC", |_, p| p.approval_mac = "ab".repeat(32)).await; |
| 238 | } |
| 239 | |
| 240 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 241 | #[cfg_attr( |
| 242 | not(compose_linked), |
| 243 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 244 | )] |
| 245 | async fn c02b_invalid_mac_empty_and_short() { |
| 246 | let _home = home_guard::home_slot(); |
| 247 | tampered("empty MAC", |_, p| p.approval_mac.clear()).await; |
| 248 | tampered("short MAC", |_, p| p.approval_mac.truncate(63)).await; |
| 249 | } |
| 250 | |
| 251 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 252 | #[cfg_attr( |
| 253 | not(compose_linked), |
| 254 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 255 | )] |
| 256 | async fn c02c_invalid_mac_uppercase_of_the_real_one() { |
| 257 | let _home = home_guard::home_slot(); |
| 258 | tampered("uppercase MAC", |_, p| { |
| 259 | p.approval_mac = p.approval_mac.to_uppercase() |
| 260 | }) |
| 261 | .await; |
| 262 | } |
| 263 | |
| 264 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 265 | #[cfg_attr( |
| 266 | not(compose_linked), |
| 267 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 268 | )] |
| 269 | async fn c02d_mac_from_another_desk_key() { |
| 270 | let _home = home_guard::home_slot(); |
| 271 | tampered("MAC under another desk key", |tmp, p| { |
| 272 | let other = DeskKey::create(&tmp.join("other-desk").join("k")).unwrap(); |
| 273 | p.approval_mac = other.sign(p, &ws(tmp)); |
| 274 | }) |
| 275 | .await; |
| 276 | } |
| 277 | |
| 278 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 279 | #[cfg_attr( |
| 280 | not(compose_linked), |
| 281 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 282 | )] |
| 283 | async fn c03_changed_trace_id() { |
| 284 | let _home = home_guard::home_slot(); |
| 285 | tampered("changed trace_id", |_, p| p.trace_id = "trace-other".into()).await; |
| 286 | } |
| 287 | |
| 288 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 289 | #[cfg_attr( |
| 290 | not(compose_linked), |
| 291 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 292 | )] |
| 293 | async fn c04_changed_request_id() { |
| 294 | let _home = home_guard::home_slot(); |
| 295 | tampered("changed request_id", |_, p| { |
| 296 | p.request_id = "req-other".into() |
| 297 | }) |
| 298 | .await; |
| 299 | } |
| 300 | |
| 301 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 302 | #[cfg_attr( |
| 303 | not(compose_linked), |
| 304 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 305 | )] |
| 306 | async fn c05_changed_approval_id() { |
| 307 | let _home = home_guard::home_slot(); |
| 308 | tampered("changed approval_id", |_, p| { |
| 309 | p.approval_id = "appr-other".into() |
| 310 | }) |
| 311 | .await; |
| 312 | } |
| 313 | |
| 314 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 315 | #[cfg_attr( |
| 316 | not(compose_linked), |
| 317 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 318 | )] |
| 319 | async fn c06_changed_path() { |
| 320 | let _home = home_guard::home_slot(); |
| 321 | tampered("changed path", |_, p| { |
| 322 | p.path = "OTHER.md".into(); |
| 323 | rehash(p); |
| 324 | }) |
| 325 | .await; |
| 326 | } |
| 327 | |
| 328 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 329 | #[cfg_attr( |
| 330 | not(compose_linked), |
| 331 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 332 | )] |
| 333 | async fn c07_changed_content() { |
| 334 | let _home = home_guard::home_slot(); |
| 335 | tampered("changed content", |_, p| { |
| 336 | p.content = "attacker content\n".into(); |
| 337 | rehash(p); |
| 338 | }) |
| 339 | .await; |
| 340 | } |
| 341 | |
| 342 | /// Invalid authentication evidence on the daemon side: a desk key file other |
| 343 | /// users can read, or no desk key, refuses everything (NoDesk), nothing runs. |
| 344 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 345 | #[cfg_attr( |
| 346 | not(compose_linked), |
| 347 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 348 | )] |
| 349 | async fn c08_desk_key_open_to_others_or_missing() { |
| 350 | let _home = home_guard::home_slot(); |
| 351 | use std::os::unix::fs::PermissionsExt; |
| 352 | let _t = TURN.lock().await; |
| 353 | let tmp = fresh(); |
| 354 | let d = up(tmp.path(), "s.sock").await; |
| 355 | let p = signed(tmp.path(), "req-k", "appr-k"); |
| 356 | let key = desk_key_path(&home(tmp.path())); |
| 357 | let (n0, _) = journal(&d.c).await; |
| 358 | std::fs::set_permissions(&key, std::fs::Permissions::from_mode(0o644)).unwrap(); |
| 359 | let (_, name) = refused(submit(&d.c, &p, &ws(tmp.path())).await, "desk key 0644"); |
| 360 | assert_eq!(name, "NoDesk"); |
| 361 | let saved = std::fs::read(&key).unwrap(); |
| 362 | std::fs::remove_file(&key).unwrap(); |
| 363 | let (_, name) = refused(submit(&d.c, &p, &ws(tmp.path())).await, "no desk key"); |
| 364 | assert_eq!(name, "NoDesk"); |
| 365 | assert_eq!( |
| 366 | journal(&d.c).await.0, |
| 367 | n0, |
| 368 | "a NoDesk refusal appended records" |
| 369 | ); |
| 370 | // Restored: the same approval was not consumed by the refusals. |
| 371 | std::fs::write(&key, saved).unwrap(); |
| 372 | std::fs::set_permissions(&key, std::fs::Permissions::from_mode(0o600)).unwrap(); |
| 373 | assert_eq!( |
| 374 | committed(submit(&d.c, &p, &ws(tmp.path())).await).state, |
| 375 | "COMMITTED" |
| 376 | ); |
| 377 | down(d).await; |
| 378 | } |
| 379 | |
| 380 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 381 | #[cfg_attr( |
| 382 | not(compose_linked), |
| 383 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 384 | )] |
| 385 | async fn c09_same_process_replay() { |
| 386 | let _home = home_guard::home_slot(); |
| 387 | let _t = TURN.lock().await; |
| 388 | let tmp = fresh(); |
| 389 | let d = up(tmp.path(), "s.sock").await; |
| 390 | let w = ws(tmp.path()); |
| 391 | let p = signed(tmp.path(), "req-r", "appr-r"); |
| 392 | let first = committed(submit(&d.c, &p, &w).await); |
| 393 | assert_eq!(first.state, "COMMITTED"); |
| 394 | let (n1, _) = journal(&d.c).await; |
| 395 | // The same approval again: the original result, no second run. |
| 396 | let again = committed(submit(&d.c, &p, &w).await); |
| 397 | assert_eq!( |
| 398 | again.state, "ALREADY_COMMITTED", |
| 399 | "ATTACK SUCCEEDED: ran twice" |
| 400 | ); |
| 401 | assert!(again.task.is_none()); |
| 402 | assert_eq!(again.compose_proposal_sha256, first.compose_proposal_sha256); |
| 403 | // Either id reused under a fresh, validly signed approval: refused. |
| 404 | for q in [ |
| 405 | signed(tmp.path(), "req-r", "appr-new"), |
| 406 | signed(tmp.path(), "req-new", "appr-r"), |
| 407 | ] { |
| 408 | let (by, _) = refused(submit(&d.c, &q, &w).await, "id reuse"); |
| 409 | assert_eq!(by, "REPLAY_REFUSED"); |
| 410 | } |
| 411 | let (n2, texts) = journal(&d.c).await; |
| 412 | assert_eq!(n2, n1, "a replay appended records"); |
| 413 | assert_eq!(phase_count(&texts, "committed"), 1); |
| 414 | down(d).await; |
| 415 | } |
| 416 | |
| 417 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 418 | #[cfg_attr( |
| 419 | not(compose_linked), |
| 420 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 421 | )] |
| 422 | async fn c10_replay_after_daemon_restart() { |
| 423 | let _home = home_guard::home_slot(); |
| 424 | let _t = TURN.lock().await; |
| 425 | let tmp = fresh(); |
| 426 | let w = ws(tmp.path()); |
| 427 | let p = signed(tmp.path(), "req-a", "appr-a"); |
| 428 | let d = up(tmp.path(), "s1.sock").await; |
| 429 | let first = committed(submit(&d.c, &p, &w).await); |
| 430 | down(d).await; |
| 431 | let d = up(tmp.path(), "s2.sock").await; |
| 432 | let (n0, _) = journal(&d.c).await; |
| 433 | let again = committed(submit(&d.c, &p, &w).await); |
| 434 | assert_eq!( |
| 435 | again.state, "ALREADY_COMMITTED", |
| 436 | "ATTACK SUCCEEDED: ran after restart" |
| 437 | ); |
| 438 | assert_eq!(again.grant_links, first.grant_links); |
| 439 | let (by, _) = refused( |
| 440 | submit(&d.c, &signed(tmp.path(), "req-b", "appr-a"), &w).await, |
| 441 | "approval_id reuse after restart", |
| 442 | ); |
| 443 | assert_eq!(by, "REPLAY_REFUSED"); |
| 444 | let (n1, texts) = journal(&d.c).await; |
| 445 | assert_eq!(n1, n0, "replay after restart appended records"); |
| 446 | assert_eq!(phase_count(&texts, "committed"), 1); |
| 447 | down(d).await; |
| 448 | } |
| 449 | |
| 450 | #[tokio::test(flavor = "multi_thread", worker_threads = 8)] |
| 451 | #[cfg_attr( |
| 452 | not(compose_linked), |
| 453 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 454 | )] |
| 455 | async fn c11_concurrent_duplicate_submissions() { |
| 456 | let _home = home_guard::home_slot(); |
| 457 | let _t = TURN.lock().await; |
| 458 | let tmp = fresh(); |
| 459 | let d = up(tmp.path(), "s.sock").await; |
| 460 | let w = ws(tmp.path()); |
| 461 | let p = signed(tmp.path(), "req-c", "appr-c"); |
| 462 | let mut tasks = Vec::new(); |
| 463 | for _ in 0..12 { |
| 464 | let (s, p, w) = (d.socket.clone(), p.clone(), w.clone()); |
| 465 | tasks.push(tokio::spawn(async move { |
| 466 | submit(&AienRuntimeClient::new(&s), &p, &w).await |
| 467 | })); |
| 468 | } |
| 469 | let mut ran = 0; |
| 470 | for t in tasks { |
| 471 | match t.await.unwrap() { |
| 472 | ControlResponse::ComposeApprovedResult(r) if r.task.is_some() => ran += 1, |
| 473 | ControlResponse::ComposeApprovedResult(r) => assert_eq!(r.state, "ALREADY_COMMITTED"), |
| 474 | ControlResponse::ComposeApprovedRefused(r) => { |
| 475 | assert_eq!(r.refused_by, "REPLAY_REFUSED") |
| 476 | } |
| 477 | other => panic!("{other:?}"), |
| 478 | } |
| 479 | } |
| 480 | let (_, texts) = journal(&d.c).await; |
| 481 | assert_eq!( |
| 482 | ran, 1, |
| 483 | "ATTACK SUCCEEDED: {ran} compose runs for one approval" |
| 484 | ); |
| 485 | assert_eq!(phase_count(&texts, "accepted"), 1); |
| 486 | assert_eq!(phase_count(&texts, "committed"), 1); |
| 487 | down(d).await; |
| 488 | } |
| 489 | |
| 490 | /// The response is lost: the caller hangs up after the daemon committed the |
| 491 | /// approval, without reading the answer. The retry gets the original result |
| 492 | /// back (ALREADY_COMMITTED); the approval ran exactly once. |
| 493 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 494 | #[cfg_attr( |
| 495 | not(compose_linked), |
| 496 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 497 | )] |
| 498 | async fn c14a_retry_after_response_loss() { |
| 499 | let _home = home_guard::home_slot(); |
| 500 | lost_response(true).await; |
| 501 | } |
| 502 | |
| 503 | /// The caller hangs up at once, before any answer. Whatever the daemon did |
| 504 | /// with the abandoned request, the retry runs the approval at most once. |
| 505 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 506 | #[cfg_attr( |
| 507 | not(compose_linked), |
| 508 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 509 | )] |
| 510 | async fn c14b_retry_after_immediate_hangup() { |
| 511 | let _home = home_guard::home_slot(); |
| 512 | lost_response(false).await; |
| 513 | } |
| 514 | |
| 515 | async fn lost_response(wait: bool) { |
| 516 | use tokio::io::AsyncWriteExt; |
| 517 | let _t = TURN.lock().await; |
| 518 | let tmp = fresh(); |
| 519 | let d = up(tmp.path(), "s.sock").await; |
| 520 | let w = ws(tmp.path()); |
| 521 | let p = signed(tmp.path(), "req-l", "appr-l"); |
| 522 | let env = ControlEnvelope { |
| 523 | protocol_version: 1, |
| 524 | request_id: 7, |
| 525 | operation_id: 7, |
| 526 | operator_session: 1, |
| 527 | command: ControlCommand::ComposeApprovedProposal { |
| 528 | proposal: p.clone(), |
| 529 | workspace: w.display().to_string(), |
| 530 | }, |
| 531 | }; |
| 532 | let mut s = tokio::net::UnixStream::connect(&d.socket).await.unwrap(); |
| 533 | s.write_all(format!("{}\n", serde_json::to_string(&env).unwrap()).as_bytes()) |
| 534 | .await |
| 535 | .unwrap(); |
| 536 | // Read nothing; hang up only once the daemon has committed the approval, |
| 537 | // so the response (not the request) is what is lost. |
| 538 | let mut ran = !wait; |
| 539 | for _ in 0..(if wait { 400 } else { 0 }) { |
| 540 | if phase_count(&journal(&d.c).await.1, "committed") == 1 { |
| 541 | ran = true; |
| 542 | break; |
| 543 | } |
| 544 | tokio::time::sleep(Duration::from_millis(25)).await; |
| 545 | } |
| 546 | drop(s); // response lost |
| 547 | assert!(ran, "the first submission never committed"); |
| 548 | let mut last = None; |
| 549 | for _ in 0..200 { |
| 550 | match submit(&d.c, &p, &w).await { |
| 551 | ControlResponse::ComposeApprovedResult(r) => { |
| 552 | last = Some(r.state.clone()); |
| 553 | break; |
| 554 | } |
| 555 | ControlResponse::ComposeApprovedRefused(r) if r.name == "InFlight" => { |
| 556 | tokio::time::sleep(Duration::from_millis(25)).await |
| 557 | } |
| 558 | other => panic!("retry after a lost response: {other:?}"), |
| 559 | } |
| 560 | } |
| 561 | let (_, texts) = journal(&d.c).await; |
| 562 | if wait { |
| 563 | assert_eq!( |
| 564 | last.as_deref(), |
| 565 | Some("ALREADY_COMMITTED"), |
| 566 | "retry must get the original result" |
| 567 | ); |
| 568 | } |
| 569 | assert!(last.is_some(), "retry never got a result"); |
| 570 | assert_eq!(phase_count(&texts, "accepted"), 1); |
| 571 | assert_eq!( |
| 572 | phase_count(&texts, "committed"), |
| 573 | 1, |
| 574 | "ATTACK SUCCEEDED: ran twice" |
| 575 | ); |
| 576 | eprintln!("c14 (wait {wait}): retry state = {last:?}"); |
| 577 | down(d).await; |
| 578 | } |
| 579 | |
| 580 | /// Replay records cannot be written by a caller (a forged claim would consume, |
| 581 | /// a forged commit would validate an approval). |
| 582 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 583 | #[cfg_attr( |
| 584 | not(compose_linked), |
| 585 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 586 | )] |
| 587 | async fn c15_forged_replay_records_via_compose_note() { |
| 588 | let _home = home_guard::home_slot(); |
| 589 | let _t = TURN.lock().await; |
| 590 | let tmp = fresh(); |
| 591 | let d = up(tmp.path(), "s.sock").await; |
| 592 | for phase in [ |
| 593 | "accepted", |
| 594 | "in_flight", |
| 595 | "committed", |
| 596 | "failed", |
| 597 | "not_executed", |
| 598 | "uncertain", |
| 599 | "declared", |
| 600 | ] { |
| 601 | let text = json!({"approved_submission": phase, "claim": 1, "request_id": "req-f", |
| 602 | "approval_id": "appr-f", "approval_key": "0".repeat(64)}); |
| 603 | match d |
| 604 | .c |
| 605 | .send_command(ControlCommand::ComposeNote { |
| 606 | kind: "effect".into(), |
| 607 | text: text.to_string(), |
| 608 | links: vec![], |
| 609 | }) |
| 610 | .await |
| 611 | .unwrap() |
| 612 | { |
| 613 | ControlResponse::Error(_) => {} |
| 614 | other => panic!("ATTACK SUCCEEDED: forged {phase} record written: {other:?}"), |
| 615 | } |
| 616 | } |
| 617 | let w = ws(tmp.path()); |
| 618 | assert_eq!( |
| 619 | committed(submit(&d.c, &signed(tmp.path(), "req-f", "appr-f"), &w).await).state, |
| 620 | "COMMITTED" |
| 621 | ); |
| 622 | down(d).await; |
| 623 | } |
| 624 | |
| 625 | /// Even a desk-signed approval may not name a path outside the workspace. |
| 626 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 627 | #[cfg_attr( |
| 628 | not(compose_linked), |
| 629 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 630 | )] |
| 631 | async fn c16_desk_signed_path_escaping_the_workspace() { |
| 632 | let _home = home_guard::home_slot(); |
| 633 | let _t = TURN.lock().await; |
| 634 | let tmp = fresh(); |
| 635 | let d = up(tmp.path(), "s.sock").await; |
| 636 | let w = ws(tmp.path()); |
| 637 | let (n0, _) = journal(&d.c).await; |
| 638 | for (i, path) in ["../escape.md", "/tmp/escape.md", "sub/../../escape.md"] |
| 639 | .iter() |
| 640 | .enumerate() |
| 641 | { |
| 642 | let p = sign( |
| 643 | tmp.path(), |
| 644 | unsigned(&format!("req-p{i}"), &format!("appr-p{i}"), path, CONTENT), |
| 645 | ); |
| 646 | let r = submit(&d.c, &p, &w).await; |
| 647 | let (by, name) = refused(r, &format!("path {path}")); |
| 648 | eprintln!("c16: {path} -> {by} {name}"); |
| 649 | } |
| 650 | assert_eq!(journal(&d.c).await.0, n0); |
| 651 | down(d).await; |
| 652 | } |
| 653 | |
| 654 | /// c17 / c17b: an approval signed for workspace A presented with workspace B |
| 655 | /// is Unauthenticated, appends nothing, and does not consume the approval: |
| 656 | /// presented with A afterwards it commits, and its grant targets A. |
| 657 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 658 | #[cfg_attr( |
| 659 | not(compose_linked), |
| 660 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 661 | )] |
| 662 | async fn c17_approval_redirected_to_another_workspace() { |
| 663 | let _home = home_guard::home_slot(); |
| 664 | let _t = TURN.lock().await; |
| 665 | let tmp = fresh(); |
| 666 | let d = up(tmp.path(), "s.sock").await; |
| 667 | let other = tmp.path().join("other-ws"); |
| 668 | std::fs::create_dir_all(&other).unwrap(); |
| 669 | let p = signed(tmp.path(), "req-w", "appr-w"); |
| 670 | let (n0, _) = journal(&d.c).await; |
| 671 | let (by, name) = refused( |
| 672 | submit(&d.c, &p, &other).await, |
| 673 | "approval for one workspace used in another", |
| 674 | ); |
| 675 | assert_eq!( |
| 676 | (by.as_str(), name.as_str()), |
| 677 | ("PROPOSAL_REFUSED", "Unauthenticated") |
| 678 | ); |
| 679 | assert_eq!( |
| 680 | journal(&d.c).await.0, |
| 681 | n0, |
| 682 | "c17b: the refusal appended records" |
| 683 | ); |
| 684 | let r = committed(submit(&d.c, &p, &ws(tmp.path())).await); |
| 685 | assert_eq!( |
| 686 | r.state, "COMMITTED", |
| 687 | "c17b: the refusal consumed the approval" |
| 688 | ); |
| 689 | let want = canon(&ws(tmp.path())).join(PATH).display().to_string(); |
| 690 | assert_eq!(r.target.as_deref(), Some(want.as_str())); |
| 691 | down(d).await; |
| 692 | } |
| 693 | |
| 694 | /// c17c: the bound workspace presented through a symlink to it canonicalises |
| 695 | /// to the same directory: accepted, and the grant targets the real directory |
| 696 | /// (never the link). A symlink to a DIFFERENT directory is Unauthenticated. |
| 697 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 698 | #[cfg_attr( |
| 699 | not(compose_linked), |
| 700 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 701 | )] |
| 702 | async fn c17c_workspace_through_a_symlink() { |
| 703 | let _home = home_guard::home_slot(); |
| 704 | let _t = TURN.lock().await; |
| 705 | let tmp = fresh(); |
| 706 | let d = up(tmp.path(), "s.sock").await; |
| 707 | let other = tmp.path().join("other-ws"); |
| 708 | std::fs::create_dir_all(&other).unwrap(); |
| 709 | let to_other = tmp.path().join("link-other"); |
| 710 | std::os::unix::fs::symlink(&other, &to_other).unwrap(); |
| 711 | let p = signed(tmp.path(), "req-y", "appr-y"); |
| 712 | let (_, name) = refused( |
| 713 | submit(&d.c, &p, &to_other).await, |
| 714 | "symlink to another workspace", |
| 715 | ); |
| 716 | assert_eq!(name, "Unauthenticated"); |
| 717 | let to_ws = tmp.path().join("link-ws"); |
| 718 | std::os::unix::fs::symlink(ws(tmp.path()), &to_ws).unwrap(); |
| 719 | let r = committed(submit(&d.c, &p, &to_ws).await); |
| 720 | let want = canon(&ws(tmp.path())).join(PATH).display().to_string(); |
| 721 | assert_eq!( |
| 722 | r.target.as_deref(), |
| 723 | Some(want.as_str()), |
| 724 | "grant target through the link" |
| 725 | ); |
| 726 | down(d).await; |
| 727 | } |
| 728 | |
| 729 | fn canon(p: &Path) -> PathBuf { |
| 730 | std::fs::canonicalize(p).unwrap() |
| 731 | } |
| 732 | |
| 733 | /// A compose home inside the workspace (the desk key within the model's reach). |
| 734 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 735 | #[cfg_attr( |
| 736 | not(compose_linked), |
| 737 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 738 | )] |
| 739 | async fn c20_workspace_containing_the_compose_home() { |
| 740 | let _t = TURN.lock().await; |
| 741 | let tmp = fresh(); |
| 742 | let d = up(tmp.path(), "s.sock").await; |
| 743 | // Signed for the overlapping workspace itself: only confinement can refuse. |
| 744 | let mut p = unsigned("req-o", "appr-o", PATH, CONTENT); |
| 745 | DeskKey::load(&desk_key_path(&home(tmp.path()))) |
| 746 | .unwrap() |
| 747 | .seal(&mut p, tmp.path()); |
| 748 | let (_, name) = refused(submit(&d.c, &p, tmp.path()).await, "overlapping workspace"); |
| 749 | assert_eq!(name, "Confinement"); |
| 750 | down(d).await; |
| 751 | } |
| 752 | |
| 753 | /// A1: a caller writes its own `authorization` note (workspace, path and a |
| 754 | /// confined target, as #262 requires) for a proposal digest no approved |
| 755 | /// compose produced, then opens an effect intent on it. |
| 756 | /// |
| 757 | /// sovereign-core #261: the daemon refuses a raw `authorization` note on every |
| 758 | /// path, so the forged grant is never written and no intent can open. |
| 759 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 760 | #[cfg_attr( |
| 761 | not(compose_linked), |
| 762 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 763 | )] |
| 764 | async fn c18_a1_raw_forged_authorization() { |
| 765 | let opened = forged_authorization(|tmp| { |
| 766 | let w = canon(&ws(tmp)); |
| 767 | (w.clone(), PATH.into(), w.join(PATH)) |
| 768 | }) |
| 769 | .await; |
| 770 | assert_eq!( |
| 771 | opened, "note refused", |
| 772 | "ATTACK SUCCEEDED (sc#261 A1): a caller-written authorization opened an effect intent" |
| 773 | ); |
| 774 | } |
| 775 | |
| 776 | /// A1b: the same forgery naming a target that is not workspace/path: refused |
| 777 | /// at the note (#262 confinement). |
| 778 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 779 | #[cfg_attr( |
| 780 | not(compose_linked), |
| 781 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 782 | )] |
| 783 | async fn c19_a1b_forged_authorization_outside_workspace() { |
| 784 | let opened = forged_authorization(|tmp| { |
| 785 | let w = canon(&ws(tmp)); |
| 786 | (w, PATH.into(), canon(tmp).join("outside.txt")) |
| 787 | }) |
| 788 | .await; |
| 789 | assert_eq!( |
| 790 | opened, "note refused", |
| 791 | "ATTACK SUCCEEDED: target outside its workspace" |
| 792 | ); |
| 793 | } |
| 794 | |
| 795 | /// A1c: the note names its OWN workspace: the parent of the real workspace, |
| 796 | /// which also holds the compose home. Refused like c18 (sc#261). |
| 797 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 798 | #[cfg_attr( |
| 799 | not(compose_linked), |
| 800 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 801 | )] |
| 802 | async fn c19c_a1c_forged_authorization_names_its_own_workspace() { |
| 803 | let opened = forged_authorization(|tmp| { |
| 804 | let w = canon(tmp); |
| 805 | (w.clone(), "outside.txt".into(), w.join("outside.txt")) |
| 806 | }) |
| 807 | .await; |
| 808 | assert_eq!( |
| 809 | opened, "note refused", |
| 810 | "ATTACK SUCCEEDED (sc#261 A1c): a note naming its own workspace opened an effect intent" |
| 811 | ); |
| 812 | } |
| 813 | |
| 814 | /// "opened" = an effect intent opened on the caller-written authorization, |
| 815 | /// "note refused" = ComposeNote refused it, otherwise the intent's refusal. |
| 816 | async fn forged_authorization(at: impl FnOnce(&Path) -> (PathBuf, String, PathBuf)) -> String { |
| 817 | let _t = TURN.lock().await; |
| 818 | let tmp = fresh(); |
| 819 | let d = up(tmp.path(), "s.sock").await; |
| 820 | let (w, path, target) = at(tmp.path()); |
| 821 | let target = target.display().to_string(); |
| 822 | let psha = sha(b"never proposed by any approved compose"); |
| 823 | let csha = sha(b"forged\n"); |
| 824 | let note = json!({"proposal_sha256": psha, "path": path, "content_sha256": csha, |
| 825 | "approver": "attacker", "workspace": w.display().to_string(), "target": target, |
| 826 | "prior_sha256": null}); |
| 827 | let auth = match d |
| 828 | .c |
| 829 | .send_command(ControlCommand::ComposeNote { |
| 830 | kind: "authorization".into(), |
| 831 | text: note.to_string(), |
| 832 | links: vec![], |
| 833 | }) |
| 834 | .await |
| 835 | .unwrap() |
| 836 | { |
| 837 | ControlResponse::ComposeNoted(n) => n.id, |
| 838 | ControlResponse::Error(e) => { |
| 839 | eprintln!("forged authorization: note refused: {e}"); |
| 840 | down(d).await; |
| 841 | return "note refused".into(); |
| 842 | } |
| 843 | other => panic!("{other:?}"), |
| 844 | }; |
| 845 | let r = intent(&d.c, auth, &psha, &path, &target, &csha).await; |
| 846 | eprintln!("forged authorization: intent: {r:?}"); |
| 847 | down(d).await; |
| 848 | match r { |
| 849 | ControlResponse::ComposeNoted(_) => "opened".into(), |
| 850 | ControlResponse::Error(e) => e, |
| 851 | other => panic!("{other:?}"), |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | async fn intent( |
| 856 | c: &AienRuntimeClient, |
| 857 | authorization: u64, |
| 858 | proposal_sha256: &str, |
| 859 | path: &str, |
| 860 | target: &str, |
| 861 | content_sha256: &str, |
| 862 | ) -> ControlResponse { |
| 863 | let (pid, start) = effects::self_executor(); |
| 864 | c.send_command(ControlCommand::ComposeEffectIntent { |
| 865 | authorization, |
| 866 | proposal_sha256: proposal_sha256.into(), |
| 867 | path: path.into(), |
| 868 | target: target.into(), |
| 869 | content_sha256: content_sha256.into(), |
| 870 | executor_pid: pid, |
| 871 | executor_start: start, |
| 872 | }) |
| 873 | .await |
| 874 | .unwrap() |
| 875 | } |
| 876 | |
| 877 | /// The effect intent for an approved compose's grant. |
| 878 | async fn grant_intent( |
| 879 | c: &AienRuntimeClient, |
| 880 | r: &aien_runtime::approved::ApprovedComposeReport, |
| 881 | grant: u64, |
| 882 | ) -> ControlResponse { |
| 883 | let target = r.target.clone().expect("grant target"); |
| 884 | intent( |
| 885 | c, |
| 886 | grant, |
| 887 | &r.compose_proposal_sha256, |
| 888 | &r.path, |
| 889 | &target, |
| 890 | &r.content_sha256, |
| 891 | ) |
| 892 | .await |
| 893 | } |
| 894 | |
| 895 | fn opened(r: ControlResponse, what: &str) -> u64 { |
| 896 | match r { |
| 897 | ControlResponse::ComposeNoted(n) => n.id, |
| 898 | other => panic!("{what}: intent did not open: {other:?}"), |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | fn not_opened(r: ControlResponse, attack: &str) -> String { |
| 903 | match r { |
| 904 | ControlResponse::Error(e) => e, |
| 905 | other => panic!("ATTACK SUCCEEDED ({attack}): {other:?}"), |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /// The `state` the ack record for `intent` holds. |
| 910 | async fn ack_state(c: &AienRuntimeClient, intent: u64) -> String { |
| 911 | match c |
| 912 | .send_command(ControlCommand::ComposeEffectAck { |
| 913 | intent, |
| 914 | reported: json!({"executor": "attack-test"}), |
| 915 | }) |
| 916 | .await |
| 917 | .unwrap() |
| 918 | { |
| 919 | ControlResponse::ComposeNoted(_) => {} |
| 920 | other => panic!("ack: {other:?}"), |
| 921 | } |
| 922 | let needle = format!("\"intent\":{intent},"); |
| 923 | let (_, texts) = journal(c).await; |
| 924 | let t = texts |
| 925 | .iter() |
| 926 | .rev() |
| 927 | .find(|t| t.contains("\"phase\":\"ack\"") && t.contains(&needle)) |
| 928 | .expect("ack record"); |
| 929 | let v: serde_json::Value = serde_json::from_str(t).unwrap(); |
| 930 | v["state"].as_str().unwrap().to_string() |
| 931 | } |
| 932 | |
| 933 | /// An approved compose, committed, with its grant id. |
| 934 | async fn approved_grant( |
| 935 | d: &Daemon, |
| 936 | tmp: &Path, |
| 937 | ) -> (aien_runtime::approved::ApprovedComposeReport, u64) { |
| 938 | let r = committed(submit(&d.c, &signed(tmp, "req-g", "appr-g"), &ws(tmp)).await); |
| 939 | let g = r.approved_grant.expect("COMMITTED report names its grant"); |
| 940 | (r, g) |
| 941 | } |
| 942 | |
| 943 | /// The host text and links of record `id`. |
| 944 | async fn text_of(c: &AienRuntimeClient, id: u64) -> (String, Vec<u64>) { |
| 945 | match c |
| 946 | .send_command(ControlCommand::ComposeRecall { |
| 947 | ids: vec![id], |
| 948 | prefix: None, |
| 949 | }) |
| 950 | .await |
| 951 | .unwrap() |
| 952 | { |
| 953 | ControlResponse::ComposeRecalled(r) => r |
| 954 | .host |
| 955 | .iter() |
| 956 | .find(|h| h.id == id) |
| 957 | .map(|h| (h.text.clone().expect("record text"), h.links.clone())) |
| 958 | .expect("record"), |
| 959 | other => panic!("recall: {other:?}"), |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /// c26: the honest path. Approved compose -> grant -> intent -> write -> ack |
| 964 | /// DONE; a second intent on the same grant is AlreadySpent; the approval |
| 965 | /// presented again is ALREADY_COMMITTED and carries no new grant. |
| 966 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 967 | #[cfg_attr( |
| 968 | not(compose_linked), |
| 969 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 970 | )] |
| 971 | async fn c26_happy_path_one_approval_one_effect() { |
| 972 | let _home = home_guard::home_slot(); |
| 973 | let _t = TURN.lock().await; |
| 974 | let tmp = fresh(); |
| 975 | let d = up(tmp.path(), "s.sock").await; |
| 976 | let (r, g) = approved_grant(&d, tmp.path()).await; |
| 977 | let i = opened(grant_intent(&d.c, &r, g).await, "honest grant"); |
| 978 | std::fs::write(r.target.as_deref().unwrap(), CONTENT).unwrap(); |
| 979 | assert_eq!(ack_state(&d.c, i).await, "DONE"); |
| 980 | let e = not_opened( |
| 981 | grant_intent(&d.c, &r, g).await, |
| 982 | "second effect on one grant", |
| 983 | ); |
| 984 | assert!(e.contains("AlreadySpent"), "{e}"); |
| 985 | let again = committed( |
| 986 | submit( |
| 987 | &d.c, |
| 988 | &signed(tmp.path(), "req-g", "appr-g"), |
| 989 | &ws(tmp.path()), |
| 990 | ) |
| 991 | .await, |
| 992 | ); |
| 993 | assert_eq!(again.state, "ALREADY_COMMITTED"); |
| 994 | assert_eq!(again.approved_grant, None); |
| 995 | down(d).await; |
| 996 | } |
| 997 | |
| 998 | /// c21: after the intent opens, the target is swapped for a symlink to a file |
| 999 | /// outside the workspace holding the approved bytes. The ack must not record |
| 1000 | /// DONE. A symlink placed BEFORE the intent: the intent is refused. |
| 1001 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1002 | #[cfg_attr( |
| 1003 | not(compose_linked), |
| 1004 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1005 | )] |
| 1006 | async fn c21_symlink_swap_of_the_target() { |
| 1007 | let _home = home_guard::home_slot(); |
| 1008 | let _t = TURN.lock().await; |
| 1009 | let tmp = fresh(); |
| 1010 | let d = up(tmp.path(), "s.sock").await; |
| 1011 | let (r, g) = approved_grant(&d, tmp.path()).await; |
| 1012 | let target = PathBuf::from(r.target.clone().unwrap()); |
| 1013 | let outside = tmp.path().join("outside.txt"); |
| 1014 | std::fs::write(&outside, CONTENT).unwrap(); |
| 1015 | // Before the intent: refused. |
| 1016 | std::os::unix::fs::symlink(&outside, &target).unwrap(); |
| 1017 | let e = not_opened( |
| 1018 | grant_intent(&d.c, &r, g).await, |
| 1019 | "intent on a symlinked target", |
| 1020 | ); |
| 1021 | eprintln!("c21 before: {e}"); |
| 1022 | std::fs::remove_file(&target).unwrap(); |
| 1023 | // After the intent: the ack reads the world and must not say DONE. |
| 1024 | let i = opened(grant_intent(&d.c, &r, g).await, "honest grant"); |
| 1025 | std::os::unix::fs::symlink(&outside, &target).unwrap(); |
| 1026 | let st = ack_state(&d.c, i).await; |
| 1027 | assert_ne!( |
| 1028 | st, "DONE", |
| 1029 | "ATTACK SUCCEEDED: DONE recorded through a symlink" |
| 1030 | ); |
| 1031 | eprintln!("c21 after: ack state {st}"); |
| 1032 | down(d).await; |
| 1033 | } |
| 1034 | |
| 1035 | /// c28: the approved path is in a subdirectory. After the intent opens, the |
| 1036 | /// subdirectory is swapped for a symlink to a directory outside the |
| 1037 | /// workspace, and the approved bytes are written there. The ack must not |
| 1038 | /// record DONE (the bytes are outside the workspace). |
| 1039 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1040 | #[cfg_attr( |
| 1041 | not(compose_linked), |
| 1042 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1043 | )] |
| 1044 | async fn c28_parent_directory_swap_after_the_intent() { |
| 1045 | let _home = home_guard::home_slot(); |
| 1046 | let _t = TURN.lock().await; |
| 1047 | let tmp = fresh(); |
| 1048 | let w = ws(tmp.path()); |
| 1049 | std::fs::create_dir_all(w.join("sub")).unwrap(); |
| 1050 | let d = up(tmp.path(), "s.sock").await; |
| 1051 | let p = sign( |
| 1052 | tmp.path(), |
| 1053 | unsigned("req-p", "appr-p", "sub/NOTES.md", CONTENT), |
| 1054 | ); |
| 1055 | let r = committed(submit(&d.c, &p, &w).await); |
| 1056 | let g = r.approved_grant.expect("grant"); |
| 1057 | let i = opened(grant_intent(&d.c, &r, g).await, "honest grant"); |
| 1058 | let outdir = tmp.path().join("outdir"); |
| 1059 | std::fs::create_dir_all(&outdir).unwrap(); |
| 1060 | std::fs::rename(w.join("sub"), w.join("sub-moved")).unwrap(); |
| 1061 | std::os::unix::fs::symlink(&outdir, w.join("sub")).unwrap(); |
| 1062 | std::fs::write(outdir.join("NOTES.md"), CONTENT).unwrap(); |
| 1063 | let st = ack_state(&d.c, i).await; |
| 1064 | eprintln!("c28: ack state {st}"); |
| 1065 | down(d).await; |
| 1066 | assert_ne!( |
| 1067 | st, "DONE", |
| 1068 | "ATTACK SUCCEEDED: DONE recorded for bytes outside the workspace (parent symlink)" |
| 1069 | ); |
| 1070 | } |
| 1071 | |
| 1072 | /// c28b: the same parent swap, settled by reconcile instead of the ack. The |
| 1073 | /// intent names an executor that has already exited, so ComposeReconcile |
| 1074 | /// decides it from the world: it must not record DONE. |
| 1075 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1076 | #[cfg_attr( |
| 1077 | not(compose_linked), |
| 1078 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1079 | )] |
| 1080 | async fn c28b_parent_directory_swap_settled_by_reconcile() { |
| 1081 | let _home = home_guard::home_slot(); |
| 1082 | let _t = TURN.lock().await; |
| 1083 | let tmp = fresh(); |
| 1084 | let w = ws(tmp.path()); |
| 1085 | std::fs::create_dir_all(w.join("sub")).unwrap(); |
| 1086 | let d = up(tmp.path(), "s.sock").await; |
| 1087 | let p = sign( |
| 1088 | tmp.path(), |
| 1089 | unsigned("req-q", "appr-q", "sub/NOTES.md", CONTENT), |
| 1090 | ); |
| 1091 | let r = committed(submit(&d.c, &p, &w).await); |
| 1092 | let g = r.approved_grant.expect("grant"); |
| 1093 | // An executor that is gone: a child that has exited. A dead pid reads as |
| 1094 | // not alive whatever start ticks the intent names. |
| 1095 | let mut child = std::process::Command::new("true").spawn().unwrap(); |
| 1096 | let pid = child.id(); |
| 1097 | child.wait().unwrap(); |
| 1098 | let (_, start) = effects::self_executor(); |
| 1099 | let i = match d |
| 1100 | .c |
| 1101 | .send_command(ControlCommand::ComposeEffectIntent { |
| 1102 | authorization: g, |
| 1103 | proposal_sha256: r.compose_proposal_sha256.clone(), |
| 1104 | path: r.path.clone(), |
| 1105 | target: r.target.clone().unwrap(), |
| 1106 | content_sha256: r.content_sha256.clone(), |
| 1107 | executor_pid: pid, |
| 1108 | executor_start: start, |
| 1109 | }) |
| 1110 | .await |
| 1111 | .unwrap() |
| 1112 | { |
| 1113 | ControlResponse::ComposeNoted(n) => n.id, |
| 1114 | other => panic!("intent did not open: {other:?}"), |
| 1115 | }; |
| 1116 | let outdir = tmp.path().join("outdir"); |
| 1117 | std::fs::create_dir_all(&outdir).unwrap(); |
| 1118 | std::fs::rename(w.join("sub"), w.join("sub-moved")).unwrap(); |
| 1119 | std::os::unix::fs::symlink(&outdir, w.join("sub")).unwrap(); |
| 1120 | std::fs::write(outdir.join("NOTES.md"), CONTENT).unwrap(); |
| 1121 | let out = |
| 1122 | d.c.send_command(ControlCommand::ComposeReconcile { declare: None }) |
| 1123 | .await |
| 1124 | .unwrap(); |
| 1125 | down(d).await; |
| 1126 | let ControlResponse::ComposeReconciled(rep) = out else { |
| 1127 | panic!("reconcile: {out:?}"); |
| 1128 | }; |
| 1129 | let o = rep |
| 1130 | .outcomes |
| 1131 | .iter() |
| 1132 | .find(|o| o.intent == i) |
| 1133 | .expect("the intent was looked at"); |
| 1134 | eprintln!("c28b: reconcile {} ({})", o.state, o.note); |
| 1135 | assert_ne!( |
| 1136 | o.state, "DONE", |
| 1137 | "ATTACK SUCCEEDED: reconcile settled DONE for bytes outside the workspace" |
| 1138 | ); |
| 1139 | } |
| 1140 | |
| 1141 | /// c22: an `approved_grant` record sent through the socket's ComposeNote |
| 1142 | /// (a copy of the daemon's own grant, same links) is refused and appends |
| 1143 | /// nothing. |
| 1144 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1145 | #[cfg_attr( |
| 1146 | not(compose_linked), |
| 1147 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1148 | )] |
| 1149 | async fn c22_forged_approved_grant_via_socket() { |
| 1150 | let _home = home_guard::home_slot(); |
| 1151 | let _t = TURN.lock().await; |
| 1152 | let tmp = fresh(); |
| 1153 | let d = up(tmp.path(), "s.sock").await; |
| 1154 | let (_, g) = approved_grant(&d, tmp.path()).await; |
| 1155 | let (text, links) = text_of(&d.c, g).await; |
| 1156 | let (n0, _) = journal(&d.c).await; |
| 1157 | let out = |
| 1158 | d.c.send_command(ControlCommand::ComposeNote { |
| 1159 | kind: "authorization".into(), |
| 1160 | text, |
| 1161 | links, |
| 1162 | }) |
| 1163 | .await |
| 1164 | .unwrap(); |
| 1165 | assert!( |
| 1166 | matches!(out, ControlResponse::Error(_)), |
| 1167 | "ATTACK SUCCEEDED: second approved_grant noted: {out:?}" |
| 1168 | ); |
| 1169 | assert_eq!(journal(&d.c).await.0, n0); |
| 1170 | down(d).await; |
| 1171 | } |
| 1172 | |
| 1173 | /// Journal level: a ComposeBridge on the compose home with the daemon down |
| 1174 | /// writes records directly (no ComposeNote reserved-record check). This is |
| 1175 | /// an attacker who can already write the compose home; the tests check the |
| 1176 | /// ledger's own consistency rules, not the socket boundary. |
| 1177 | fn bridge(tmp: &Path) -> aien_runtime::compose::ComposeBridge { |
| 1178 | let never: aien_runtime::compose::ComposeProposer = |
| 1179 | std::sync::Arc::new(|_: &str, _: Duration| Err("no model".to_string())); |
| 1180 | aien_runtime::compose::ComposeBridge::new(home(tmp), never, "attack") |
| 1181 | } |
| 1182 | |
| 1183 | fn raw_note( |
| 1184 | b: &aien_runtime::compose::ComposeBridge, |
| 1185 | kind: &str, |
| 1186 | text: &str, |
| 1187 | links: &[u64], |
| 1188 | ) -> u64 { |
| 1189 | match b.note_unchecked(kind, text, links) { |
| 1190 | ControlResponse::ComposeNoted(n) => n.id, |
| 1191 | other => panic!("raw note: {other:?}"), |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /// A forged approved grant backed by a claim left in `end` (failed, |
| 1196 | /// uncertain, uncertain-declared-committed): open_intent must refuse it. |
| 1197 | async fn grant_on_unfinished_claim(end: &str) { |
| 1198 | use aien_runtime::approved_replay as rp; |
| 1199 | let _t = TURN.lock().await; |
| 1200 | let tmp = fresh(); |
| 1201 | std::env::set_var("AIEN_COMPOSE_DIR", home(tmp.path())); |
| 1202 | let (g, csha, psha, target) = { |
| 1203 | let b = bridge(tmp.path()); |
| 1204 | let w = canon(&ws(tmp.path())); |
| 1205 | let csha = sha(CONTENT.as_bytes()); |
| 1206 | // A complete bound identity: the grant parses and its identity hashes |
| 1207 | // to the claim's key, so only the claim's state can refuse it. |
| 1208 | let ident = aien_runtime::approved_auth::ApprovalIdentity { |
| 1209 | trace_id: "t".into(), |
| 1210 | request_id: format!("req-{end}"), |
| 1211 | approval_id: format!("appr-{end}"), |
| 1212 | approver: "attacker".into(), |
| 1213 | path: PATH.into(), |
| 1214 | content_sha256: csha.clone(), |
| 1215 | approved_proposal_sha256: approved_proposal_sha256(PATH, CONTENT), |
| 1216 | desk_key_id: "forged".into(), |
| 1217 | workspace: w.display().to_string(), |
| 1218 | }; |
| 1219 | let keys = rp::ClaimKeys { |
| 1220 | approval_key: aien_runtime::approved_auth::approval_key(&ident), |
| 1221 | request_id: ident.request_id.clone(), |
| 1222 | approval_id: ident.approval_id.clone(), |
| 1223 | trace_id: ident.trace_id.clone(), |
| 1224 | }; |
| 1225 | let c = rp::claim(&b, &keys).unwrap(); |
| 1226 | rp::mark_in_flight(&b, &c).unwrap(); |
| 1227 | match end { |
| 1228 | "failed" => rp::fail(&b, &c, "attack").unwrap(), |
| 1229 | _ => rp::uncertain(&b, &c, "attack").unwrap(), |
| 1230 | } |
| 1231 | if end == "declared" { |
| 1232 | rp::declare(&b, c.id, true, "attacker", "attack").unwrap(); |
| 1233 | } |
| 1234 | let target = w.join(PATH).display().to_string(); |
| 1235 | let psha = sha(b"forged compose proposal"); |
| 1236 | // Promotion and evidence: any two existing records, linked. |
| 1237 | let pr = raw_note(&b, "constraint", "{\"fake\":\"promotion\"}", &[]); |
| 1238 | let ev = raw_note(&b, "constraint", "{\"fake\":\"evidence\"}", &[]); |
| 1239 | let text = json!({ |
| 1240 | "approved_grant": 1, "proposal_sha256": psha, "path": PATH, |
| 1241 | "content_sha256": csha, "approver": "attacker", "target": target, |
| 1242 | "workspace": w.display().to_string(), "prior_sha256": null, |
| 1243 | "approval_key": keys.approval_key, "replay_claim": c.id, |
| 1244 | "cx_promotion": pr, "cx_evidence": ev, |
| 1245 | "trace_id": ident.trace_id, "request_id": ident.request_id, |
| 1246 | "approval_id": ident.approval_id, |
| 1247 | "approved_proposal_sha256": ident.approved_proposal_sha256, |
| 1248 | "desk_key_id": ident.desk_key_id, |
| 1249 | }); |
| 1250 | let g = raw_note(&b, "authorization", &text.to_string(), &[pr, ev, c.id]); |
| 1251 | (g, csha, psha, target) |
| 1252 | }; |
| 1253 | let d = up(tmp.path(), "s.sock").await; |
| 1254 | let e = not_opened( |
| 1255 | intent(&d.c, g, &psha, PATH, &target, &csha).await, |
| 1256 | &format!("approved grant on a {end} claim"), |
| 1257 | ); |
| 1258 | assert!(e.contains("NotAuthorized"), "{end}: {e}"); |
| 1259 | down(d).await; |
| 1260 | } |
| 1261 | |
| 1262 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1263 | #[cfg_attr( |
| 1264 | not(compose_linked), |
| 1265 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1266 | )] |
| 1267 | async fn c23_forged_grant_on_failed_claim() { |
| 1268 | let _home = home_guard::home_slot(); |
| 1269 | grant_on_unfinished_claim("failed").await; |
| 1270 | } |
| 1271 | |
| 1272 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1273 | #[cfg_attr( |
| 1274 | not(compose_linked), |
| 1275 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1276 | )] |
| 1277 | async fn c24a_forged_grant_on_uncertain_claim() { |
| 1278 | let _home = home_guard::home_slot(); |
| 1279 | grant_on_unfinished_claim("uncertain").await; |
| 1280 | } |
| 1281 | |
| 1282 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1283 | #[cfg_attr( |
| 1284 | not(compose_linked), |
| 1285 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1286 | )] |
| 1287 | async fn c24b_forged_grant_on_uncertain_claim_declared_committed() { |
| 1288 | let _home = home_guard::home_slot(); |
| 1289 | grant_on_unfinished_claim("declared").await; |
| 1290 | } |
| 1291 | |
| 1292 | /// c25 (journal level): a byte-for-byte copy of the daemon's real grant, |
| 1293 | /// same links, appended directly. One approval must never open two effect |
| 1294 | /// intents. |
| 1295 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1296 | #[cfg_attr( |
| 1297 | not(compose_linked), |
| 1298 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1299 | )] |
| 1300 | async fn c25_duplicate_approved_grant_for_one_committed_claim() { |
| 1301 | let _home = home_guard::home_slot(); |
| 1302 | let _t = TURN.lock().await; |
| 1303 | let tmp = fresh(); |
| 1304 | let d = up(tmp.path(), "s.sock").await; |
| 1305 | let (r, g) = approved_grant(&d, tmp.path()).await; |
| 1306 | let (text, links) = text_of(&d.c, g).await; |
| 1307 | down(d).await; |
| 1308 | let g2 = raw_note(&bridge(tmp.path()), "authorization", &text, &links); |
| 1309 | let d = up(tmp.path(), "s2.sock").await; |
| 1310 | let first = grant_intent(&d.c, &r, g).await; |
| 1311 | let second = grant_intent(&d.c, &r, g2).await; |
| 1312 | let both = matches!( |
| 1313 | (&first, &second), |
| 1314 | ( |
| 1315 | ControlResponse::ComposeNoted(_), |
| 1316 | ControlResponse::ComposeNoted(_) |
| 1317 | ) |
| 1318 | ); |
| 1319 | eprintln!("c25: real grant #{g}: {first:?}\nc25: copy #{g2}: {second:?}"); |
| 1320 | down(d).await; |
| 1321 | assert!( |
| 1322 | !both, |
| 1323 | "ATTACK SUCCEEDED (journal level): two effect intents from one approval" |
| 1324 | ); |
| 1325 | } |
| 1326 | |
| 1327 | /// #306: the daemon must close its compose home before `run` returns, even |
| 1328 | /// while a client connection is still open. Connection tasks hold their own |
| 1329 | /// handle on the bridge and outlive `run`; if the close is left to them, a |
| 1330 | /// successor that opens the same home right after shutdown races the close and |
| 1331 | /// is refused with E_REPLAY (journal behind its J-Space anchor). |
| 1332 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1333 | #[cfg_attr( |
| 1334 | not(compose_linked), |
| 1335 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1336 | )] |
| 1337 | async fn c25b_shutdown_closes_the_compose_home_while_a_connection_is_open() { |
| 1338 | let _home = home_guard::home_slot(); |
| 1339 | let _t = TURN.lock().await; |
| 1340 | let tmp = fresh(); |
| 1341 | let d = up(tmp.path(), "s.sock").await; |
| 1342 | let (_r, _g) = approved_grant(&d, tmp.path()).await; |
| 1343 | // An idle client connection that outlives the daemon's run(). |
| 1344 | let idle = tokio::net::UnixStream::connect(&d.socket).await.unwrap(); |
| 1345 | down(d).await; |
| 1346 | // Another opener on the same home must get in at once, every time. |
| 1347 | for i in 0..5 { |
| 1348 | let b = bridge(tmp.path()); |
| 1349 | let n = raw_note(&b, "constraint", &format!("after shutdown {i}"), &[]); |
| 1350 | assert!(n > 0); |
| 1351 | drop(b); |
| 1352 | } |
| 1353 | drop(idle); |
| 1354 | } |
| 1355 | |
| 1356 | /// #306: once `close` has run, NO path may open the home again: a compose task, |
| 1357 | /// a record_digest, a note and a recover all refuse, and the journal gets no |
| 1358 | /// new bytes. (Every lazy open goes through one check.) |
| 1359 | #[test] |
| 1360 | #[cfg_attr( |
| 1361 | not(compose_linked), |
| 1362 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1363 | )] |
| 1364 | fn c25c_a_closed_bridge_never_reopens_the_home() { |
| 1365 | let _home = home_guard::home_slot(); |
| 1366 | let tmp = fresh(); |
| 1367 | let b = bridge(tmp.path()); |
| 1368 | let first = raw_note(&b, "constraint", "before close", &[]); |
| 1369 | assert!(first > 0); |
| 1370 | let journal = home(tmp.path()).join("cortex.cx"); |
| 1371 | let before = std::fs::read(&journal).expect("journal exists after the first note"); |
| 1372 | b.close(); |
| 1373 | assert!(b.is_closed()); |
| 1374 | let refused = |what: &str, r: String| { |
| 1375 | assert!(r.contains("compose home closed"), "{what} not refused: {r}"); |
| 1376 | }; |
| 1377 | refused("record_digest", b.record_digest(first).unwrap_err()); |
| 1378 | let ws = ws(tmp.path()); |
| 1379 | match b.run_task("write NOTES.md", &ws.display().to_string()) { |
| 1380 | ControlResponse::Error(e) => refused("run_task", e), |
| 1381 | other => panic!("run_task after close: {other:?}"), |
| 1382 | } |
| 1383 | match b.note_unchecked("constraint", "after close", &[]) { |
| 1384 | ControlResponse::Error(e) => refused("note", e), |
| 1385 | other => panic!("note after close: {other:?}"), |
| 1386 | } |
| 1387 | match b.recover() { |
| 1388 | ControlResponse::Error(e) => refused("recover", e), |
| 1389 | other => panic!("recover after close: {other:?}"), |
| 1390 | } |
| 1391 | assert_eq!( |
| 1392 | std::fs::read(&journal).unwrap(), |
| 1393 | before, |
| 1394 | "the journal changed after close" |
| 1395 | ); |
| 1396 | } |
| 1397 | |
| 1398 | /// Crash tests: a child process runs a whole daemon, submits one approval |
| 1399 | /// over its socket and is aborted at a crash point inside the daemon. The |
| 1400 | /// parent restarts a daemon over the same compose home and retries. |
| 1401 | #[cfg(feature = "fault-hold")] |
| 1402 | mod crash { |
| 1403 | use super::*; |
| 1404 | |
| 1405 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1406 | #[ignore] |
| 1407 | async fn crash_child_daemon() { |
| 1408 | let Ok(tmp) = std::env::var("ATTACK_CRASH_TMP") else { |
| 1409 | return; |
| 1410 | }; |
| 1411 | let tmp = PathBuf::from(tmp); |
| 1412 | let d = up(&tmp, "child.sock").await; |
| 1413 | let p = signed(&tmp, "req-x", "appr-x"); |
| 1414 | let out = submit(&d.c, &p, &ws(&tmp)).await; |
| 1415 | panic!("child was not stopped at its crash point: {out:?}"); |
| 1416 | } |
| 1417 | |
| 1418 | fn run_child(tmp: &Path, hold: &str) { |
| 1419 | use std::os::unix::process::ExitStatusExt; |
| 1420 | let st = std::process::Command::new(std::env::current_exe().unwrap()) |
| 1421 | .args([ |
| 1422 | "--exact", |
| 1423 | "crash::crash_child_daemon", |
| 1424 | "--ignored", |
| 1425 | "--nocapture", |
| 1426 | "--test-threads=1", |
| 1427 | ]) |
| 1428 | .env("AIEN_FAULT_HOLD", hold) |
| 1429 | .env("ATTACK_CRASH_TMP", tmp) |
| 1430 | .stdout(std::process::Stdio::null()) |
| 1431 | .stderr(std::process::Stdio::null()) |
| 1432 | .status() |
| 1433 | .unwrap(); |
| 1434 | assert_eq!(st.signal(), Some(6), "child must abort at {hold}: {st:?}"); |
| 1435 | } |
| 1436 | |
| 1437 | async fn after_crash(hold: &str, want_phase: &str, want_refusal: &str) { |
| 1438 | let _t = TURN.lock().await; |
| 1439 | let tmp = fresh(); |
| 1440 | run_child(tmp.path(), hold); |
| 1441 | let d = up(tmp.path(), "after.sock").await; |
| 1442 | let (n0, texts) = journal(&d.c).await; |
| 1443 | assert_eq!(phase_count(&texts, "accepted"), 1, "the child claimed once"); |
| 1444 | assert_eq!( |
| 1445 | phase_count(&texts, want_phase), |
| 1446 | 1, |
| 1447 | "restart reconcile: {texts:?}" |
| 1448 | ); |
| 1449 | let w = ws(tmp.path()); |
| 1450 | let p = signed(tmp.path(), "req-x", "appr-x"); |
| 1451 | let r = submit(&d.c, &p, &w).await; |
| 1452 | let (by, name) = refused(r, &format!("retry after crash at {hold}")); |
| 1453 | assert_eq!( |
| 1454 | (by.as_str(), name.as_str()), |
| 1455 | ("REPLAY_REFUSED", want_refusal) |
| 1456 | ); |
| 1457 | let (n1, texts) = journal(&d.c).await; |
| 1458 | assert_eq!(n1, n0, "a refused retry appended records"); |
| 1459 | assert_eq!(phase_count(&texts, "committed"), 0); |
| 1460 | assert!(!w.join(PATH).exists()); |
| 1461 | down(d).await; |
| 1462 | } |
| 1463 | |
| 1464 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1465 | #[cfg_attr( |
| 1466 | not(compose_linked), |
| 1467 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1468 | )] |
| 1469 | async fn c12_crash_before_execution() { |
| 1470 | after_crash("approved_after_claim", "not_executed", "AlreadyConsumed").await; |
| 1471 | } |
| 1472 | |
| 1473 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1474 | #[cfg_attr( |
| 1475 | not(compose_linked), |
| 1476 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1477 | )] |
| 1478 | async fn c13_crash_after_execution_started() { |
| 1479 | after_crash("approved_after_compose", "uncertain", "Uncertain").await; |
| 1480 | } |
| 1481 | |
| 1482 | /// c27: crash after the claim is COMMITTED, before the grant is written. |
| 1483 | /// The approval stays spent and no grant ever appears: no effect possible. |
| 1484 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 1485 | #[cfg_attr( |
| 1486 | not(compose_linked), |
| 1487 | ignore = "needs the linked librx_compose.a (AIEN_OMEGA_COMPOSE_LIB)" |
| 1488 | )] |
| 1489 | async fn c27_crash_after_commit_before_grant() { |
| 1490 | let _t = TURN.lock().await; |
| 1491 | let tmp = fresh(); |
| 1492 | run_child(tmp.path(), "approved_after_commit"); |
| 1493 | let d = up(tmp.path(), "after.sock").await; |
| 1494 | let (n0, texts) = journal(&d.c).await; |
| 1495 | assert_eq!(phase_count(&texts, "committed"), 1, "{texts:?}"); |
| 1496 | let grants = |t: &[String]| { |
| 1497 | t.iter() |
| 1498 | .filter(|x| x.contains("\"approved_grant\"")) |
| 1499 | .count() |
| 1500 | }; |
| 1501 | assert_eq!(grants(&texts), 0, "a grant was written before the crash"); |
| 1502 | let w = ws(tmp.path()); |
| 1503 | let r = submit(&d.c, &signed(tmp.path(), "req-x", "appr-x"), &w).await; |
| 1504 | match &r { |
| 1505 | ControlResponse::ComposeApprovedResult(rep) => { |
| 1506 | assert_eq!(rep.state, "ALREADY_COMMITTED"); |
| 1507 | assert_eq!( |
| 1508 | rep.approved_grant, None, |
| 1509 | "ATTACK SUCCEEDED: retry minted a grant" |
| 1510 | ); |
| 1511 | } |
| 1512 | ControlResponse::ComposeApprovedRefused(_) => {} |
| 1513 | other => panic!("retry: {other:?}"), |
| 1514 | } |
| 1515 | let (n1, texts) = journal(&d.c).await; |
| 1516 | assert_eq!( |
| 1517 | grants(&texts), |
| 1518 | 0, |
| 1519 | "ATTACK SUCCEEDED: a grant appeared after restart" |
| 1520 | ); |
| 1521 | assert_eq!(n1, n0, "the retry appended records"); |
| 1522 | assert!(!w.join(PATH).exists()); |
| 1523 | eprintln!("c27: retry -> {r:?}"); |
| 1524 | down(d).await; |
| 1525 | } |
| 1526 | } |