Skip to content

Commit 1ba4642

Browse files
committed
mmap: drop file-offset checking
When mmap-ing a file-like object, vm-memory was trying to validate the the range [offset, offset+len) was valid in the file. However, our homegrown check had lots of edge cases where it gave false-positives (e.g. it rejected things that should really be working, such as vfio devices, or fds that cannot be seeked like guest_memfd), and trying to implement a check that works for all of these is in the end wasted effort anyway, because the kernel validates all this as part of the mmap syscall anyway. So just drop these checks in favor of failing at mmap-time. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 3a0b31d commit 1ba4642

File tree

4 files changed

+4
-46
lines changed

4 files changed

+4
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Added
66

77
- \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations
8+
- \[[#319](https://github.com/rust-vmm/vm-memory/pull/319)\] Allow constructing `MmapRegion`s
9+
mapping file descriptors that cannot be seeked, such as `guest_memfd`.
810

911
### Changed
1012

src/mmap.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
//! This implementation is mmap-ing the memory of the guest into the current process.
1414
1515
use std::borrow::Borrow;
16-
#[cfg(unix)]
17-
use std::io::{Seek, SeekFrom};
1816
use std::ops::Deref;
1917
use std::result;
2018
use std::sync::atomic::Ordering;
@@ -70,34 +68,6 @@ pub enum Error {
7068
UnsortedMemoryRegions,
7169
}
7270

73-
// TODO: use this for Windows as well after we redefine the Error type there.
74-
#[cfg(unix)]
75-
/// Checks if a mapping of `size` bytes fits at the provided `file_offset`.
76-
///
77-
/// For a borrowed `FileOffset` and size, this function checks whether the mapping does not
78-
/// extend past EOF, and that adding the size to the file offset does not lead to overflow.
79-
pub fn check_file_offset(
80-
file_offset: &FileOffset,
81-
size: usize,
82-
) -> result::Result<(), MmapRegionError> {
83-
let mut file = file_offset.file();
84-
let start = file_offset.start();
85-
86-
if let Some(end) = start.checked_add(size as u64) {
87-
let filesize = file
88-
.seek(SeekFrom::End(0))
89-
.map_err(MmapRegionError::SeekEnd)?;
90-
file.rewind().map_err(MmapRegionError::SeekStart)?;
91-
if filesize < end {
92-
return Err(MmapRegionError::MappingPastEof);
93-
}
94-
} else {
95-
return Err(MmapRegionError::InvalidOffsetLength);
96-
}
97-
98-
Ok(())
99-
}
100-
10171
/// [`GuestMemoryRegion`](trait.GuestMemoryRegion.html) implementation that mmaps the guest's
10272
/// memory region in the current process.
10373
///

src/mmap_unix.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::result;
1717

1818
use crate::bitmap::{Bitmap, BS};
1919
use crate::guest_memory::FileOffset;
20-
use crate::mmap::{check_file_offset, NewBitmap};
20+
use crate::mmap::NewBitmap;
2121
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
2222

2323
/// Error conditions that may arise when creating a new `MmapRegion` object.
@@ -41,12 +41,6 @@ pub enum Error {
4141
/// The `mmap` call returned an error.
4242
#[error("{0}")]
4343
Mmap(io::Error),
44-
/// Seeking the end of the file returned an error.
45-
#[error("Error seeking the end of the file: {0}")]
46-
SeekEnd(io::Error),
47-
/// Seeking the start of the file returned an error.
48-
#[error("Error seeking the start of the file: {0}")]
49-
SeekStart(io::Error),
5044
}
5145

5246
pub type Result<T> = result::Result<T, Error>;
@@ -137,7 +131,6 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
137131
}
138132

139133
let (fd, offset) = if let Some(ref f_off) = self.file_offset {
140-
check_file_offset(f_off, self.size)?;
141134
(f_off.file().as_raw_fd(), f_off.start())
142135
} else {
143136
(-1, 0)

src/mmap_xen.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use tests::ioctl_with_ref;
2626

2727
use crate::bitmap::{Bitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
29-
use crate::mmap::{check_file_offset, NewBitmap};
29+
use crate::mmap::NewBitmap;
3030
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
3131

3232
/// Error conditions that may arise when creating a new `MmapRegion` object.
@@ -44,12 +44,6 @@ pub enum Error {
4444
/// The `mmap` call returned an error.
4545
#[error("{0}")]
4646
Mmap(io::Error),
47-
/// Seeking the end of the file returned an error.
48-
#[error("Error seeking the end of the file: {0}")]
49-
SeekEnd(io::Error),
50-
/// Seeking the start of the file returned an error.
51-
#[error("Error seeking the start of the file: {0}")]
52-
SeekStart(io::Error),
5347
/// Invalid file offset.
5448
#[error("Invalid file offset")]
5549
InvalidFileOffset,
@@ -524,7 +518,6 @@ struct MmapXenUnix(MmapUnix);
524518
impl MmapXenUnix {
525519
fn new(range: &MmapRange) -> Result<Self> {
526520
let (fd, offset) = if let Some(ref f_off) = range.file_offset {
527-
check_file_offset(f_off, range.size)?;
528521
(f_off.file().as_raw_fd(), f_off.start())
529522
} else {
530523
(-1, 0)

0 commit comments

Comments
 (0)