From a77160dca4273467653ca5e2c050e776f6d5fce0 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 14 Sep 2021 17:02:20 +0100 Subject: [PATCH] devices: acpi: Increase robustness of bus devices Check the size of data buffer for reading on the ApciPmTimer device to avoid a potential panic if the guest uses non-DWORD access. Simplify the zeroring of the buffer for AcpiShutdownDevice. Signed-off-by: Rob Bradford --- devices/src/acpi.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/devices/src/acpi.rs b/devices/src/acpi.rs index b37e7cbea..ece070049 100644 --- a/devices/src/acpi.rs +++ b/devices/src/acpi.rs @@ -34,9 +34,7 @@ impl AcpiShutdownDevice { impl BusDevice for AcpiShutdownDevice { // Spec has all fields as zero fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { - for i in data.iter_mut() { - *i = 0; - } + data.fill(0) } fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option> { @@ -191,6 +189,10 @@ impl Default for AcpiPmTimerDevice { impl BusDevice for AcpiPmTimerDevice { fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) { + if data.len() != std::mem::size_of::() { + warn!("Invalid sized read of PM timer: {}", data.len()); + return; + } let now = Instant::now(); let since = now.duration_since(self.start); let nanos = since.as_nanos();