Skip to content

Commit ca58afd

Browse files
00xcShadowCurse
authored andcommitted
virtio: balloon: fix integer overflow
Fix a potential integer overflow when computing the end of the destination slice during a configuration write in Balloon::write_config(). This bug leads to a panic when adding with overflow or indexing the slice a few lines below, depending on the build. Fix this by using `usize::checked_add()` and `slice::get_mut()`. Signed-off-by: Carlos López <[email protected]>
1 parent afd05d6 commit ca58afd

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,18 @@ impl VirtioDevice for Balloon {
553553
}
554554

555555
fn write_config(&mut self, offset: u64, data: &[u8]) {
556-
let data_len = data.len() as u64;
557556
let config_space_bytes = self.config_space.as_mut_slice();
558-
let config_len = config_space_bytes.len() as u64;
559-
if offset + data_len > config_len {
557+
let start = usize::try_from(offset).ok();
558+
let end = start.and_then(|s| s.checked_add(data.len()));
559+
let Some(dst) = start
560+
.zip(end)
561+
.and_then(|(start, end)| config_space_bytes.get_mut(start..end)) else
562+
{
560563
error!("Failed to write config space");
561564
return;
562-
}
563-
config_space_bytes[offset as usize..(offset + data_len) as usize].copy_from_slice(data);
565+
};
566+
567+
dst.copy_from_slice(data);
564568
}
565569

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

0 commit comments

Comments
 (0)