From 26ea43cab18983d92a5ea07cbd059f7d040b05ef Mon Sep 17 00:00:00 2001 From: Jiaqi Gao Date: Tue, 12 Sep 2023 03:53:31 -0400 Subject: [PATCH 1/2] devices/pci: return error if type of bar is not correct Return error instead of panic. Signed-off-by: Jiaqi Gao --- src/devices/pci/src/config.rs | 4 ++-- src/devices/pci/src/lib.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/devices/pci/src/config.rs b/src/devices/pci/src/config.rs index 44c1e45d..18d8066b 100644 --- a/src/devices/pci/src/config.rs +++ b/src/devices/pci/src/config.rs @@ -6,7 +6,7 @@ use bitflags::bitflags; use core::convert::From; use crate::mmio::{alloc_mmio32, alloc_mmio64}; -use crate::{PciCommand, Result}; +use crate::{PciCommand, PciError, Result}; pub const PCI_CONFIGURATION_ADDRESS_PORT: u16 = 0xCF8; pub const PCI_CONFIGURATION_DATA_PORT: u16 = 0xCFC; @@ -453,7 +453,7 @@ impl PciDevice { self.bars[current_bar].address = addr & PCI_MEM64_BASE_ADDRESS_MASK; current_bar_offset += 4; } - _ => panic!("Unsupported BAR type"), + _ => return Err(PciError::InvalidBarType), } } diff --git a/src/devices/pci/src/lib.rs b/src/devices/pci/src/lib.rs index ed6be3b2..bc9d6e8f 100644 --- a/src/devices/pci/src/lib.rs +++ b/src/devices/pci/src/lib.rs @@ -25,4 +25,5 @@ pub type Result = core::result::Result; pub enum PciError { InvalidParameter, MmioOutofResource, + InvalidBarType, } From fa956d0753bade4b3ddb630bd795273b19617750 Mon Sep 17 00:00:00 2001 From: Jiaqi Gao Date: Tue, 12 Sep 2023 03:56:08 -0400 Subject: [PATCH 2/2] pci/devices: fix the potential integer overflow Change the return type to an option and add check for the input parameter to prevent the potential integer overflow. Signed-off-by: Jiaqi Gao --- src/devices/pci/src/mmio.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/devices/pci/src/mmio.rs b/src/devices/pci/src/mmio.rs index ee191df2..9f322bac 100644 --- a/src/devices/pci/src/mmio.rs +++ b/src/devices/pci/src/mmio.rs @@ -35,10 +35,10 @@ pub fn alloc_mmio32(size: u32) -> Result { #[cfg(not(feature = "fuzz"))] pub fn alloc_mmio32(size: u32) -> Result { - let addr = *MMIO32.lock(); - let addr = align_up(addr as usize, size as usize); + let cur = *MMIO32.lock(); + let addr = align_up(cur as u64, size as u64).ok_or(PciError::InvalidParameter)?; - if size > MMIO32_SIZE || addr > (MMIO32_START + MMIO32_SIZE - size) as usize { + if size > MMIO32_SIZE || addr > (MMIO32_START + MMIO32_SIZE - size) as u64 { return Err(PciError::MmioOutofResource); } @@ -54,14 +54,16 @@ pub fn alloc_mmio64(size: u64) -> Result { #[cfg(not(feature = "fuzz"))] pub fn alloc_mmio64(size: u64) -> Result { - let addr = *MMIO64.lock(); - addr.checked_add(size).ok_or(PciError::InvalidParameter)?; + let cur = *MMIO64.lock(); + let addr = align_up(cur, size).ok_or(PciError::InvalidParameter)? as u64; - let addr = align_up(addr as usize, size as usize) as u64; - *MMIO64.lock() = addr + size; + *MMIO64.lock() = addr.checked_add(size).ok_or(PciError::InvalidParameter)?; Ok(addr) } -fn align_up(addr: usize, align: usize) -> usize { - (addr + align - 1) & !(align - 1) +fn align_up(addr: u64, align: u64) -> Option { + if align == 0 { + return None; + } + Some((addr.checked_add(align)? - 1) & !(align - 1)) }