From 677c8831afc79d3638c2d551b697e94f07153cd9 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 29 Apr 2022 09:58:22 +0200 Subject: [PATCH] vmm: Ensure uniqueness of generated identifiers The device identifiers generated from the DeviceManager were not guaranteed to be unique since they were not taking the list of identifiers provided through the configuration. By returning the list of unique identifiers from the configuration, and by providing it to the DeviceManager, the generation of new identifiers can rely both on the DeviceTree and the list of IDs from the configuration. Signed-off-by: Sebastien Boeuf --- vmm/src/config.rs | 6 ++++-- vmm/src/device_manager.rs | 11 +++++++++-- vmm/src/vm.rs | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index ae1b1b345..19b2b12c0 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -2329,7 +2329,9 @@ pub struct VmConfig { impl VmConfig { // Also enables virtio-iommu if the config needs it - pub fn validate(&mut self) -> ValidationResult<()> { + // Returns the list of unique identifiers provided through the + // configuration. + pub fn validate(&mut self) -> ValidationResult> { let mut id_list = BTreeSet::new(); #[cfg(not(feature = "tdx"))] @@ -2578,7 +2580,7 @@ impl VmConfig { .map(|p| p.iommu_segments.is_some()) .unwrap_or_default(); - Ok(()) + Ok(id_list) } pub fn parse(vm_params: VmParams) -> Result { diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 28a8b8fee..380b26584 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -72,7 +72,7 @@ use pci::{ VfioUserPciDevice, VfioUserPciDeviceError, }; use seccompiler::SeccompAction; -use std::collections::HashMap; +use std::collections::{BTreeSet, HashMap}; use std::convert::TryInto; use std::fs::{read_link, File, OpenOptions}; use std::io::{self, stdout, Seek, SeekFrom}; @@ -934,6 +934,9 @@ pub struct DeviceManager { // io_uring availability if detected io_uring_supported: Option, + + // List of unique identifiers provided at boot through the configuration. + boot_id_list: BTreeSet, } impl DeviceManager { @@ -949,6 +952,7 @@ impl DeviceManager { activate_evt: &EventFd, force_iommu: bool, restoring: bool, + boot_id_list: BTreeSet, ) -> DeviceManagerResult>> { let device_tree = Arc::new(Mutex::new(DeviceTree::new())); @@ -1071,6 +1075,7 @@ impl DeviceManager { force_iommu, restoring, io_uring_supported: None, + boot_id_list, }; let device_manager = Arc::new(Mutex::new(device_manager)); @@ -3002,7 +3007,9 @@ impl DeviceManager { // Increment the counter. self.device_id_cnt += Wrapping(1); // Check if the name is already in use. - if !self.device_tree.lock().unwrap().contains_key(&name) { + if !self.boot_id_list.contains(&name) + && !self.device_tree.lock().unwrap().contains_key(&name) + { return Ok(name); } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 11c3171eb..f8646306f 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -570,7 +570,7 @@ impl Vm { None }; - config + let boot_id_list = config .lock() .unwrap() .validate() @@ -603,6 +603,7 @@ impl Vm { &activate_evt, force_iommu, restoring, + boot_id_list, ) .map_err(Error::DeviceManager)?;