rust-vmm / vm-memory

Virtual machine's guest memory crate
Apache License 2.0
305 stars 98 forks source link

mmap: get file size with seek instead of metadata #196

Closed slp closed 2 years ago

slp commented 2 years ago

Currently mmap::check_file_offset attempts to validate the backing object by checking, among other things, its size to confirm the mapping will fit on it. This check is implemented by using std::fs::metadata to obtain the file size, but this approach doesn't work when the backing object is a block or char device, as the returned value is always zero.

In this commit, we replace the use of std::fs:metadata with std::io::Seek::seek(SeekEnd(0)), which works fine even with special files such as block and char devices. As a reference, this is the method that QEMU uses for the same goal.

Fixes: #195 Signed-off-by: Sergio Lopez slp@redhat.com