diff --git a/CLAUDE.md b/CLAUDE.md index 95b479e4..1ae33c22 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -199,7 +199,9 @@ transport-adjacent. The rest, imperatively: - **`.sc/local-key` and the per-checkout protected caches (`.sc/protected-cache`, `.sc/ws/cache-`) are never committed and never transferred.** Cache saves are best-effort and ordered after ref moves; a - cache failure may cause a spurious re-seal but never incorrectness. + cache failure may cause a spurious re-seal but never incorrectness. The key + is owner-only on unix only; non-unix keeps default permissions and warns once + (#214, ACL deferred). - **Revoke is standing-only.** A revoked recipient keeps decrypting ciphertext sealed before the revoke (they hold the old wraps); cryptographic cutover is `sc rewrap --rotate` at the tip (fresh key per protected blob; a plain diff --git a/ROADMAP.md b/ROADMAP.md index 6523c057..b7d2f61c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -785,10 +785,12 @@ scale-&-reach horizon): `sc resolve --theirs` protected path keeps their ciphertext instead of re-sealing; their wrap list must be in the unioned protection, and a lookup gap on a partial clone just seals. -- **Non-unix local-key fallback (P33 final-review follow-on).** `.sc/local-key` - is minted 0600-from-first-byte via a unix-only `OpenOptions::mode` call; - there is no non-unix fallback today, so the key file is left world-readable - on a non-unix target. A non-unix permission-hardening path is deferred. +- **Non-unix local-key permissions (#214, P33 follow-on).** `.sc/local-key` is + minted 0600-from-first-byte on unix only. On a non-unix target it keeps default + permissions and `sc` warns once when a cache holds a protected entry (the + exposure is bounded; see THREAT-MODEL.md). Deferred: an owner-only Windows ACL + (`windows-sys` is already in `Cargo.lock`) and a manual check on a real Windows + host. - **fd secret injection — shipped (ADR-0052).** `sc run --secrets-fd N` hands the secrets to the command on a descriptor (NUL-terminated `NAME=VALUE` records) instead of its environment. Not built: the same mode for `sc ws run` and diff --git a/crates/repo/src/cache.rs b/crates/repo/src/cache.rs index 8a33f612..88034aea 100644 --- a/crates/repo/src/cache.rs +++ b/crates/repo/src/cache.rs @@ -9,6 +9,9 @@ //! nothing — it does not reintroduce the equality oracle this phase closes. //! A lost/stale/corrupt cache degrades to spurious re-seals, never //! incorrectness and never a hard error. +//! Where the key file cannot be made owner-only (non-unix; #214) it is +//! still written with default permissions and a one-time warning is printed +//! once the cache holds a protected entry: see [`can_restrict_permissions`]. //! //! **The keyed tag is the sole authority for a hit.** `mtime_ns`/`size` are //! recorded for documentation (when the entry was observed) and as a @@ -31,6 +34,53 @@ use scl_core::ObjectId; use crate::error::{Error, Result}; use crate::layout::Layout; +/// Whether `.sc/local-key` can be created readable by its owner only. Only +/// unix can (`mode(0o600)` from the first byte); there is no Windows ACL path +/// yet (#214), so elsewhere the key keeps default permissions and a warning is +/// printed. Tests force `false` via `force_unrestricted_for_test`. +fn can_restrict_permissions() -> bool { + #[cfg(test)] + if FORCE_UNRESTRICTED.with(|f| f.get()) { + return false; + } + cfg!(unix) +} + +#[cfg(test)] +thread_local! { + static FORCE_UNRESTRICTED: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Test seam: make this thread behave like a non-unix target. +#[cfg(test)] +pub(crate) fn force_unrestricted_for_test(on: bool) { + FORCE_UNRESTRICTED.with(|f| f.set(on)); +} + +/// Print the unrestricted-key warning the first time `flag` is unset; `true` +/// iff it printed. The flag is a parameter so a test can use a private one. +fn warn_unrestricted_once(flag: &std::sync::atomic::AtomicBool) -> bool { + if flag.swap(true, std::sync::atomic::Ordering::Relaxed) { + return false; + } + eprintln!( + "warning: this platform cannot restrict .sc/local-key to its owner; other \ + local users may be able to read it and test guesses against the \ + protected-file cache tags (the protected plaintext checked out in the \ + working tree is equally readable by default)" + ); + true +} + +/// Warn (once per process) when a cache that holds protected entries is +/// backed by a key whose permissions could not be restricted. +fn warn_if_unrestricted(has_entries: bool) { + static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + if has_entries && !can_restrict_permissions() { + warn_unrestricted_once(&WARNED); + } +} + /// Load (minting if absent) the per-repo random cache key. The key file is /// the only hard-error surface here: if it can't be created/read, the cache /// cannot be safely keyed, so surface it rather than running unkeyed. @@ -77,6 +127,7 @@ fn write_key_0600(path: &Path, bytes: &[u8]) -> std::io::Result<()> { f.write_all(bytes) } +/// Non-unix: default (inherited) permissions; see [`can_restrict_permissions`]. #[cfg(not(unix))] fn write_key_0600(path: &Path, bytes: &[u8]) -> std::io::Result<()> { use std::io::Write; @@ -186,6 +237,9 @@ impl ProtectedCache { } } } + if path.is_some() { + warn_if_unrestricted(!entries.is_empty()); + } ProtectedCache { key, root, @@ -253,6 +307,7 @@ impl ProtectedCache { let Some(path) = &self.path else { return Ok(()); }; + warn_if_unrestricted(!self.entries.is_empty()); let mut out = String::new(); for (p, e) in &self.entries { if !is_plain_file(&self.root, p) { @@ -322,6 +377,42 @@ mod tests { cleanup(&layout); } + #[test] + fn unrestricted_key_still_persists_and_hits() { + force_unrestricted_for_test(true); + let layout = tmp("unrestricted"); + let key = local_key(&layout).unwrap(); + assert_eq!(key, local_key(&layout).unwrap(), "key persisted"); + assert!(layout.local_key_path().exists()); + + let abs = layout.root.join("secret.txt"); + std::fs::write(&abs, b"v1").unwrap(); + let id = ObjectId::of(b"cipher-of-v1"); + let mut c = ProtectedCache::open( + key, + layout.root.clone(), + Some(layout.protected_cache_path()), + ); + c.record("secret.txt", b"v1", id); + c.save().unwrap(); + assert!(layout.protected_cache_path().exists(), "cache persisted"); + let c = ProtectedCache::open( + key, + layout.root.clone(), + Some(layout.protected_cache_path()), + ); + assert_eq!(c.unchanged("secret.txt", b"v1"), Some(id), "hit survives"); + force_unrestricted_for_test(false); + cleanup(&layout); + } + + #[test] + fn unrestricted_warning_prints_once() { + let flag = std::sync::atomic::AtomicBool::new(false); + assert!(warn_unrestricted_once(&flag)); + assert!(!warn_unrestricted_once(&flag)); + } + #[test] fn tag_is_authoritative_for_hits_and_misses() { let layout = tmp("hits"); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index ba8d9ed0..eaff8843 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -81,6 +81,19 @@ too, not just the src-control-side metadata. protected blob under a fresh key (ADR-0051), whereas a plain `sc rewrap` only replaces wrap lists and leaves the key a revoked holder already has. History keeps the old wraps and ciphertext regardless. +- **Local cache key is owner-only on unix only (P33, #214):** the + unchanged-detection cache is keyed by `.sc/local-key`, created `0600` from its + first byte. Only unix can do that; on a non-unix target the key and the cache + files are written with inherited default permissions, and `sc` prints a + one-time stderr warning once a cache holds a protected entry. The exposure is + small: checkout already writes protected plaintext into the working tree with + default permissions on every platform, and the cache holds tags only for files + whose plaintext is on disk, so a reader of the key could read that plaintext + directly. What remains is the tags of the last-committed version of a file + edited since, and stale entries until the next save. The real fix is an + owner-only Windows ACL (`windows-sys` is already a transitive dependency); it is + unverified without a Windows host and tracked in ROADMAP.md. A unix filesystem + that ignores mode bits (vfat, exFAT, some network mounts) is not detected. ## Private branches (`sc branch --private` / `sc branch publish`) — ADR-0044 diff --git a/docs/adr/0043-randomized-protected-encryption.md b/docs/adr/0043-randomized-protected-encryption.md index b3bedabc..1818cea8 100644 --- a/docs/adr/0043-randomized-protected-encryption.md +++ b/docs/adr/0043-randomized-protected-encryption.md @@ -101,7 +101,10 @@ file changed. Detection is a per-checkout stat cache (`crates/repo/src/cache.rs` - `.sc/local-key`: 32 random bytes, mode **0600 from first byte** (via `OpenOptions::create_new` + `mode(0o600)`, so the file is never briefly world-readable between creation and permission-setting), created lazily, - **never committed, never transferred**. The per-checkout cache file itself + **never committed, never transferred**. Non-unix targets cannot restrict it + (#214): it keeps default permissions with a one-time warning, because checkout + already writes the same plaintext to the working tree with default + permissions (see THREAT-MODEL.md). The per-checkout cache file itself (below) is written via `atomic_write_durable`. - Per-checkout cache files: `.sc/protected-cache` for the main working tree; `.sc/ws/cache-` per ws workspace (see below). Each entry is