From 26d1a76ad9740ba2a20400889733273b5e27c297 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 9 Feb 2022 11:14:49 +0000 Subject: [PATCH] vmm: config: Validate balloon size is less than RAM size Signed-off-by: Rob Bradford --- vmm/src/config.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index d7224ed7e..833ce90ea 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -155,6 +155,8 @@ pub enum ValidationError { InvalidNumPciSegments(u16), /// Invalid PCI segment id InvalidPciSegment(u16), + /// Balloon too big + BalloonLargerThanRam(u64, u64), } type ValidationResult = std::result::Result; @@ -218,6 +220,13 @@ impl fmt::Display for ValidationError { InvalidPciSegment(pci_segment) => { write!(f, "Invalid PCI segment id{}", pci_segment) } + BalloonLargerThanRam(balloon_size, ram_size) => { + write!( + f, + "Ballon size ({}) greater than RAM ({})", + balloon_size, ram_size + ) + } } } } @@ -2165,6 +2174,23 @@ impl VmConfig { } } + if let Some(balloon) = &self.balloon { + let mut ram_size = self.memory.size; + + if let Some(zones) = &self.memory.zones { + for zone in zones { + ram_size += zone.size; + } + } + + if balloon.size >= ram_size { + return Err(ValidationError::BalloonLargerThanRam( + balloon.size, + ram_size, + )); + } + } + if let Some(devices) = &self.devices { for device in devices { device.validate(self)?;