Skip to content

Commit afd05d6

Browse files
00xcShadowCurse
authored andcommitted
virtio: block: fix integer overflow
When computing the end of the destination slice for a configuration write in Block::write_config(), an addition might overflow. This will immediately cause a panic in debug builds, and panic on slice indexing a few lines below for release builds. Fix this by using `usize::checked_add()` and `slice::get_mut()`. Signed-off-by: Carlos López <[email protected]>
1 parent 06256c3 commit afd05d6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/vmm/src/devices/virtio/block/device.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,15 +578,18 @@ impl VirtioDevice for Block {
578578
}
579579

580580
fn write_config(&mut self, offset: u64, data: &[u8]) {
581-
let data_len = data.len() as u64;
582-
let config_len = self.config_space.len() as u64;
583-
if offset + data_len > config_len {
581+
let start = usize::try_from(offset).ok();
582+
let end = start.and_then(|s| s.checked_add(data.len()));
583+
let Some(dst) = start
584+
.zip(end)
585+
.and_then(|(start, end)| self.config_space.get_mut(start..end)) else
586+
{
584587
error!("Failed to write config space");
585588
METRICS.block.cfg_fails.inc();
586589
return;
587-
}
590+
};
588591

589-
self.config_space[offset as usize..(offset + data_len) as usize].copy_from_slice(data);
592+
dst.copy_from_slice(data);
590593
}
591594

592595
fn activate(&mut self, mem: GuestMemoryMmap) -> ActivateResult {

0 commit comments

Comments
 (0)