Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasmtime-cache: Reject overflowing duration values (#14060)

Rui JingAna2401847bde6
diff --git a/crates/cache/src/config.rs b/crates/cache/src/config.rs
index beef58d282..bc39f87a78 100644
--- a/crates/cache/src/config.rs
+++ b/crates/cache/src/config.rs
@@ -262,9 +262,9 @@ macro_rules! generate_deserializer {
generate_deserializer!(deserialize_duration(num: u64, unit: &str) -> Duration {
match unit {
"s" => Some(Duration::from_secs(num)),
- "m" => Some(Duration::from_secs(num * 60)),
- "h" => Some(Duration::from_secs(num * 60 * 60)),
- "d" => Some(Duration::from_secs(num * 60 * 60 * 24)),
+ "m" => num.checked_mul(60).map(Duration::from_secs),
+ "h" => num.checked_mul(60 * 60).map(Duration::from_secs),
+ "d" => num.checked_mul(60 * 60 * 24).map(Duration::from_secs),
_ => None,
}
});
diff --git a/crates/cache/src/config/tests.rs b/crates/cache/src/config/tests.rs
index a605c9e216..c88ae84c87 100644
--- a/crates/cache/src/config/tests.rs
+++ b/crates/cache/src/config/tests.rs
@@ -400,6 +400,27 @@ fn test_duration_settings() {
);
}
+#[test]
+fn test_duration_overflow() {
+ let (_td, cd, cp) = test_prolog();
+
+ for (num, unit) in [
+ (u64::MAX / 60 + 1, "m"),
+ (u64::MAX / (60 * 60) + 1, "h"),
+ (u64::MAX / (60 * 60 * 24) + 1, "d"),
+ ] {
+ let content = format!(
+ "[cache]\ndirectory = '{}'\ncleanup-interval = '{num}{unit}'",
+ cd.display()
+ );
+ fs::write(&cp, content).expect("Failed to write test config file");
+ assert!(
+ CacheConfig::from_file(Some(&cp)).is_err(),
+ "duration {num}{unit} should overflow"
+ );
+ }
+}
+
#[test]
fn test_percent_settings() {
let (_td, cd, cp) = test_prolog();