Skip to content

Commit e07fa35

Browse files
00xcShadowCurse
authored andcommitted
virtio: net: fix integer overflow
Fix a potential integer overflow when computing the end of the destination slice during a configuration write in Net::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 ca58afd commit e07fa35

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,16 +790,19 @@ impl VirtioDevice for Net {
790790
}
791791

792792
fn write_config(&mut self, offset: u64, data: &[u8]) {
793-
let data_len = data.len() as u64;
794793
let config_space_bytes = self.config_space.as_mut_slice();
795-
let config_len = config_space_bytes.len() as u64;
796-
if offset + data_len > config_len {
794+
let start = usize::try_from(offset).ok();
795+
let end = start.and_then(|s| s.checked_add(data.len()));
796+
let Some(dst) = start
797+
.zip(end)
798+
.and_then(|(start, end)| config_space_bytes.get_mut(start..end)) else
799+
{
797800
error!("Failed to write config space");
798801
METRICS.net.cfg_fails.inc();
799802
return;
800-
}
803+
};
801804

802-
config_space_bytes[offset as usize..(offset + data_len) as usize].copy_from_slice(data);
805+
dst.copy_from_slice(data);
803806
self.guest_mac = Some(self.config_space.guest_mac);
804807
METRICS.net.mac_address_updates.inc();
805808
}

0 commit comments

Comments
 (0)