From 1e1c34cdb0c91662f604d7f16e91eb64adec00ce Mon Sep 17 00:00:00 2001 From: Lucas Clemente Vella Date: Wed, 21 Jun 2023 13:02:57 +0100 Subject: [PATCH] Returning error if offset doesn't fit into libc::off_t. --- src/unix.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/unix.rs b/src/unix.rs index 6e3fd4e8..c9c8df34 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,5 +1,6 @@ extern crate libc; +use std::convert::TryInto; use std::fs::File; use std::mem::ManuallyDrop; use std::os::unix::io::{FromRawFd, RawFd}; @@ -45,7 +46,15 @@ impl MmapInner { offset: u64, ) -> io::Result { let alignment = offset % page_size() as u64; - let aligned_offset = offset - alignment; + let aligned_offset: libc::off_t = (offset - alignment).try_into().map_err(|_| { + // On some platforms, libc exposed by rust uses 32 bit for file + // offset. In these cases, we can't honor requests for mappings + // starting from beyond the initial 4 GB. + io::Error::new( + io::ErrorKind::InvalidData, + "memory map offset overflows libc::off_t", + ) + })?; let (map_len, map_offset) = Self::adjust_mmap_params(len as usize, alignment as usize)?; @@ -56,7 +65,7 @@ impl MmapInner { prot, flags, file, - aligned_offset as libc::off_t, + aligned_offset, ); if ptr == libc::MAP_FAILED {