From 73bb7bd6736dfe5da1c0bc627cf315db272ebcef Mon Sep 17 00:00:00 2001 From: Sebastian <106036+schoeller@users.noreply.github.com> Date: Mon, 17 Aug 2026 18:25:15 +0200 Subject: [PATCH] 1GiB guard for mmap Adding a MAX_GUARD for mmap to file --- crates/ocs_plugin_api/src/shm.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ocs_plugin_api/src/shm.rs b/crates/ocs_plugin_api/src/shm.rs index 0e889cdf..f91a3881 100644 --- a/crates/ocs_plugin_api/src/shm.rs +++ b/crates/ocs_plugin_api/src/shm.rs @@ -185,7 +185,25 @@ impl SharedDocumentReader { /// Open the file at `path` read-only and map it. The mapping may initially /// contain no valid snapshot; the caller should `refresh()` before use. pub fn open(path: &Path) -> io::Result { + const MAX_FILE_SIZE: u64 = 1024 * 1024 * 1024; // 1 GiB guard let file = OpenOptions::new().read(true).open(path)?; + let metadata = file.metadata()?; + if !metadata.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("snapshot path is not a regular file: {}", path.display()), + )); + } + if metadata.len() > MAX_FILE_SIZE { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!( + "snapshot file size {} exceeds maximum {} MiB; refusing to mmap", + metadata.len(), + MAX_FILE_SIZE / 1024 / 1024 + ), + )); + } let mmap = unsafe { Mmap::map(&file)? }; let file_len = mmap.len(); let segment_size = if file_len > CONTROL_SIZE {