diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0a9726a..04453f2 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -22,7 +22,11 @@ this file is the curated, human-readable summary. - `phoebus-mcp`: Implemented standard JSON-RPC 2.0 error handling for malformed JSON frames (error code `-32700`, Parse Error) and missing/invalid tool params (error code `-32602`, Invalid Params), preventing server silent aborts or broken agent pipes. 4. Agent Containment Jail Hardening: - `phoebus-executor::agent_jail`: Sanitized `extra_masked_subdirs` to reject directory traversal components (`..`) and leading slashes. Enabled mock/recording runner testability when `bwrap` is not present on the build host. - Gates: `cargo fmt --all -- --check`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace` (all passed: 54 adversarial tests, 39 CLI tests, 9 flaw tests, 4 MCP tests, 5 desktop config tests, 3 agent jail tests, 4 AI e2e tests). + 5. Btrfs Swapfile Hardening: + - `phoebus-executor::storage`: Ensured `chattr +C` (NOCOW) is executed on the parent `@swap` directory and on the empty 0-byte file prior to truncation, avoiding Btrfs `EINVAL` on allocated extents. Enforced secure mode `0600` permissions on the swapfile. + 6. Inter-Process Lock Drop Verification: + - `phoebus-executor::system_lock`: Validated lockfile PID ownership before unlinking on drop, guaranteeing that a dropping handle never unlinks a lock acquired by another process. + Gates: `cargo fmt --all -- --check`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace` (all passed: 54 adversarial tests, 39 CLI tests, 9 flaw tests, 4 MCP tests, 5 desktop config tests, 3 agent jail tests, 4 AI e2e tests, 4 storage tests, 2 system lock tests). - **System stability & reliability hardening: Advisory Mutation Lock, Atomic Transactions, System Health & Context-Aware Guidance (2026-09-30).** Hardened system operations against race conditions, database drift, and usability friction: diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md index 3d4b236..3d11510 100644 --- a/docs/HANDOFF.md +++ b/docs/HANDOFF.md @@ -11,6 +11,8 @@ commit it. It should be short and current; use `git log` for detail. 4. Structured TOML serialization for MangoWM keybindings to prevent syntax breakage from unescaped quotes in actions. 5. JSON-RPC 2.0 error conformance for `phoebus-mcp` (handling parse errors and missing params with proper error frames). 6. Sanitized Bubblewrap `agent_jail` paths to filter out directory traversal (`..`) attacks. + 7. Btrfs swapfile hardening: executed `chattr +C` on directory and on 0-byte file prior to truncation, avoiding Btrfs extent errors; enforced mode `0600`. + 8. Inter-process lock drop safety: verified PID ownership prior to unlinking lockfile on drop. Quality gates: `cargo fmt --all -- --check`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace` all passed cleanly with 0 warnings and 0 errors. - **System stability & reliability hardening (2026-09-30, IMPLEMENTED + TESTED):** diff --git a/installer-rs/crates/phoebus-executor/src/storage.rs b/installer-rs/crates/phoebus-executor/src/storage.rs index 7cbd772..3a08b77 100644 --- a/installer-rs/crates/phoebus-executor/src/storage.rs +++ b/installer-rs/crates/phoebus-executor/src/storage.rs @@ -371,6 +371,12 @@ pub fn create_swapfile( _root_source: &str, ) -> Result<()> { let swapdir = root.ensure_dir("swap")?; + // Ensure the swap directory itself has +C (NOCOW) so created files inherit it. + let _ = exec.run( + CommandSpec::new("storage_prepare", "disable CoW on swap directory", "chattr") + .arg("+C") + .arg(swapdir.to_string_lossy().to_string()), + ); let swapfile = swapdir.join("swapfile"); let size_mib = swap_size_mib(exec); let spec = CommandSpec::new("storage_prepare", "create Btrfs swapfile", "btrfs") @@ -382,17 +388,29 @@ pub fn create_swapfile( ]) .arg(swapfile.to_string_lossy().to_string()); if exec.run(spec).is_ok() { + let _ = exec.run( + CommandSpec::new("storage_prepare", "secure swapfile permissions", "chmod") + .arg("0600") + .arg(swapfile.to_string_lossy().to_string()), + ); return Ok(()); } - // Fallback for older btrfs-progs. + // Fallback for older btrfs-progs: + // Create empty 0-byte file first, set +C (NOCOW), then truncate, chmod 0600, and mkswap. + let _ = std::fs::File::create(&swapfile); + let _ = exec.run( + CommandSpec::new("storage_prepare", "disable CoW on swapfile", "chattr") + .arg("+C") + .arg(swapfile.to_string_lossy().to_string()), + ); exec.run( CommandSpec::new("storage_prepare", "create swapfile container", "truncate") .args(["-s", &format!("{size_mib}M")]) .arg(swapfile.to_string_lossy().to_string()), )?; exec.run( - CommandSpec::new("storage_prepare", "disable CoW on swapfile", "chattr") - .arg("+C") + CommandSpec::new("storage_prepare", "secure swapfile permissions", "chmod") + .arg("0600") .arg(swapfile.to_string_lossy().to_string()), )?; exec.run( @@ -567,4 +585,25 @@ mod tests { let crypttab = std::fs::read_to_string(exec.root.join("etc/crypttab")).unwrap(); assert!(crypttab.contains("cryptroot UUID=1111-2222 none luks,discard")); } + + #[test] + fn swapfile_creation_secures_permissions_and_nocow() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + let cfg = InstallConfig::default(); + let root = exec.target(); + + create_swapfile(&exec, &cfg, &root, "/dev/mapper/cryptroot").unwrap(); + let recorded = runner.recorded(); + + // Verify chattr +C was run on the swap directory + assert!(recorded.iter().any(|s| s.program == "chattr" + && s.args.contains(&"+C".to_string()) + && s.args.iter().any(|a| a.contains("swap")))); + + // Verify chmod 0600 was run on the swapfile + assert!(recorded.iter().any(|s| s.program == "chmod" + && s.args.contains(&"0600".to_string()) + && s.args.iter().any(|a| a.contains("swapfile")))); + } } diff --git a/installer-rs/crates/phoebus-executor/src/system_lock.rs b/installer-rs/crates/phoebus-executor/src/system_lock.rs index ba563fc..671abc2 100644 --- a/installer-rs/crates/phoebus-executor/src/system_lock.rs +++ b/installer-rs/crates/phoebus-executor/src/system_lock.rs @@ -162,8 +162,12 @@ impl SystemLock { impl Drop for SystemLock { fn drop(&mut self) { - if self.lock_path.exists() { - let _ = fs::remove_file(&self.lock_path); + if let Ok(content) = fs::read_to_string(&self.lock_path) { + if let Some(info) = LockInfo::parse(&content) { + if info.pid == std::process::id() { + let _ = fs::remove_file(&self.lock_path); + } + } } } }