Skip to content

Commit d2dc8a4

Browse files
committed
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 274535d commit d2dc8a4

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
and `GuestRegionMmap::from_range` to be separate from the error type returned by `GuestRegionCollection` functions.
2424
Change return type of `GuestRegionMmap::new` from `Result` to `Option`.
2525
- \[#324](https:////github.com/rust-vmm/vm-memory/pull/324)\] `GuestMemoryRegion::bitmap()` now returns a `BitmapSlice`. Accessing the full bitmap is now possible only if the type of the memory region is know, for example with `MmapRegion::bitmap()`.
26+
- \[[#339](https://github.com/rust-vmm/vm-memory/pull/339)\] Fix `Bytes::read()` and `Bytes::write()` not to ignore `try_access()`'s `count` parameter
2627

2728
### Removed
2829

src/guest_memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
550550
self.try_access(
551551
buf.len(),
552552
addr,
553-
|offset, _count, caddr, region| -> Result<usize> {
554-
region.write(&buf[offset..], caddr)
553+
|offset, count, caddr, region| -> Result<usize> {
554+
region.write(&buf[offset..(offset + count)], caddr)
555555
},
556556
)
557557
}
@@ -560,8 +560,8 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
560560
self.try_access(
561561
buf.len(),
562562
addr,
563-
|offset, _count, caddr, region| -> Result<usize> {
564-
region.read(&mut buf[offset..], caddr)
563+
|offset, count, caddr, region| -> Result<usize> {
564+
region.read(&mut buf[offset..(offset + count)], caddr)
565565
},
566566
)
567567
}

0 commit comments

Comments
 (0)