From 83b7b3b7d602bca28b45649e2da2aa8fb57fbfc5 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 19:41:53 +0100 Subject: [PATCH] [edge] Epoch floor: an unreadable or corrupt floor refuses share links instead of resetting to 0; directory fsync'ed after the write --- crates/edge/src/shared.rs | 42 ++++++++++++++++++++++++++------ crates/edge/tests/share_links.rs | 25 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/crates/edge/src/shared.rs b/crates/edge/src/shared.rs index ce05feb..7839e2f 100644 --- a/crates/edge/src/shared.rs +++ b/crates/edge/src/shared.rs @@ -104,6 +104,8 @@ pub struct Shared { revocations: ArcSwap, /// The highest revocation epoch seen since start; a file with a lower one is refused. epoch_seen: std::sync::atomic::AtomicU32, + /// Set when the saved epoch floor could not be read: share links stay refused. + floor_error: Option, pub throttle: crate::share::Throttle, cert_states: Mutex>, pub acme_wake: tokio::sync::Notify, @@ -122,7 +124,13 @@ fn same_listeners(a: &Runtime, b: &Runtime) -> Result<(), EdgeError> { impl Shared { pub fn new(rt: Runtime, paths: Paths, credentials_dir: Option) -> Arc { - let floor = read_epoch_floor(&rt); + let (floor, floor_error) = match read_epoch_floor(&rt) { + Ok(f) => (f, None), + Err(e) => { + log::error!(target: "ianitor::share", "{e}"); + (u32::MAX, Some(e)) + } + }; let certs = Arc::new(CertStore::default()); certs.replace_files(rt.file_certs.clone()); let (changed, _) = tokio::sync::watch::channel(1); @@ -138,6 +146,7 @@ impl Shared { credentials_dir, revocations: ArcSwap::from_pointee(RevocationState::default()), epoch_seen: std::sync::atomic::AtomicU32::new(floor), + floor_error, throttle: crate::share::Throttle::default(), cert_states: Mutex::new(HashMap::new()), acme_wake: tokio::sync::Notify::new(), @@ -360,6 +369,9 @@ impl Shared { .ok_or_else(|| fail("a link id is not 32 hex digits".into()))?, ); } + if let Some(e) = &self.floor_error { + return Err(fail(e.clone())); + } // The epoch only goes up: a lower one would bring stopped links back. let seen = self.epoch_seen.load(Ordering::SeqCst); if file.epoch < seen { @@ -427,11 +439,23 @@ fn epoch_floor_path(rt: &Runtime) -> PathBuf { Path::new(&rt.cfg.state_dir).join("share-epoch-floor") } -fn read_epoch_floor(rt: &Runtime) -> u32 { - std::fs::read_to_string(epoch_floor_path(rt)) - .ok() - .and_then(|s| s.trim().parse().ok()) - .unwrap_or(0) +/// Missing: 0. Unreadable or not a number: an error, and share links stay refused (failing +/// open would forget which links were stopped). +fn read_epoch_floor(rt: &Runtime) -> Result { + let path = epoch_floor_path(rt); + match std::fs::read_to_string(&path) { + Ok(s) => s.trim().parse().map_err(|_| { + format!( + "{} is not a number; share links are refused until it is fixed", + path.display() + ) + }), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(0), + Err(e) => Err(format!( + "cannot read {} ({e}); share links are refused until it is fixed", + path.display() + )), + } } fn write_epoch_floor(rt: &Runtime, epoch: u32) -> std::io::Result<()> { @@ -446,7 +470,11 @@ fn write_epoch_floor(rt: &Runtime, epoch: u32) -> std::io::Result<()> { f.write_all(format!("{epoch}\n").as_bytes())?; f.sync_all()?; } - std::fs::rename(&tmp, &path) + std::fs::rename(&tmp, &path)?; + match path.parent() { + Some(dir) => std::fs::File::open(dir)?.sync_all(), + None => Ok(()), + } } fn poisoned() -> AdminError { diff --git a/crates/edge/tests/share_links.rs b/crates/edge/tests/share_links.rs index 56148a3..cd94938 100644 --- a/crates/edge/tests/share_links.rs +++ b/crates/edge/tests/share_links.rs @@ -531,4 +531,29 @@ mod unix { .await; assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 302); } + + #[tokio::test(flavor = "multi_thread")] + async fn an_unreadable_epoch_floor_refuses_share_links() { + let mut e = env().await; + let t = link(HOST, 15, "/", now() + 3600, None); + let cookie = share_cookie(&e.get(HOST, &format!("/?share={t}"), None).await); + assert_eq!(e.get(HOST, "/", Some(&cookie)).await.status, 200); + std::fs::write(e.edge.dir.join("state/share-epoch-floor"), "garbage").unwrap(); + e.edge.restart(); + assert_eq!( + e.get(HOST, "/", Some(&cookie)).await.status, + 302, + "fails closed" + ); + let r = e + .edge + .admin("POST", "/v1/share-revocations/reload", None) + .await; + assert_eq!(r.status, 422, "{}", r.text); + assert!(e.edge.stderr().contains("share-epoch-floor")); + // A missing floor is simply 0. + std::fs::remove_file(e.edge.dir.join("state/share-epoch-floor")).unwrap(); + e.edge.restart(); + assert_eq!(e.get(HOST, "/", Some(&cookie)).await.status, 200); + } }