Skip to content

Commit 1d3c09b

Browse files
XanClicbonzini
authored andcommitted
Bytes: Fix read() and write()
read() and write() must not ignore the `count` parameter: The mappings passed into the `try_access()` closure are only valid for up to `count` bytes, not more. (Note: We cannot really have a test case for this, as right now, memory fragmentation will only happen exactly at memory region boundaries. In this case, `region.write()`/`region.read()` will only access the region up until its end, even if the passed slice is longer, and so silently ignore the length mismatch. This change is necessary for when page boundaries result in different mappings within a single region, i.e. the region does not end at the fragmentation point, and calling `region.write()`/`region.read()` would write/read across the boundary. Because we don’t have IOMMU support yet, this can’t be tested.) Signed-off-by: Hanna Czenczek <[email protected]>
1 parent 5b6bf8d commit 1d3c09b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
The `mmap(2)` syscall itself already validates that offset and length are valid, and trying to replicate this check
3535
in userspace ended up being imperfect.
3636

37+
### Fixed
38+
39+
- \[[#339](https://github.com/rust-vmm/vm-memory/pull/339)\] Fix `Bytes::read()` and `Bytes::write()` not to ignore `try_access()`'s `count` parameter
40+
3741
## \[v0.16.1\]
3842

3943
### Added

src/guest_memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
558558
self.try_access(
559559
buf.len(),
560560
addr,
561-
|offset, _count, caddr, region| -> Result<usize> {
562-
region.write(&buf[offset..], caddr)
561+
|offset, count, caddr, region| -> Result<usize> {
562+
region.write(&buf[offset..(offset + count)], caddr)
563563
},
564564
)
565565
}
@@ -568,8 +568,8 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
568568
self.try_access(
569569
buf.len(),
570570
addr,
571-
|offset, _count, caddr, region| -> Result<usize> {
572-
region.read(&mut buf[offset..], caddr)
571+
|offset, count, caddr, region| -> Result<usize> {
572+
region.read(&mut buf[offset..(offset + count)], caddr)
573573
},
574574
)
575575
}

0 commit comments

Comments
 (0)