Skip to content

Commit e3931a5

Browse files
committed
vhost: fix panic on update_config call when device is not active
There is a window in time when the vhost device is not yet initialized by the guest, but the user already sends the request to update its config. Though this is an unlikely scenario, fix it by actually checking the `active_state` instead of asserting on it. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent 59141ff commit e3931a5

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎src/vmm/src/devices/virtio/block/vhost_user/device.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,11 @@ impl<T: VhostUserHandleBackend> VhostUserBlockImpl<T> {
267267

268268
pub fn update_config(&mut self) -> Result<(), VhostUserBlockError> {
269269
let start_time = get_time_us(ClockType::Monotonic);
270-
let interrupt = self
271-
.device_state
272-
.active_state()
273-
.expect("Device is not initialized")
274-
.interrupt
275-
.clone();
270+
271+
let Some(device_state) = self.device_state.active_state() else {
272+
return Err(VhostUserBlockError::DeviceNotActive);
273+
};
274+
let interrupt = device_state.interrupt.clone();
276275

277276
// This buffer is used for config size check in vhost crate.
278277
let buffer = [0u8; BLOCK_CONFIG_SPACE_SIZE as usize];
@@ -732,6 +731,9 @@ mod tests {
732731
vhost_block.write_config(0x69, &[0]);
733732
assert_eq!(vhost_block.config_space, vec![0x69, 0x69, 0x69]);
734733

734+
// Before device is active, update_config should error out
735+
vhost_block.update_config().unwrap_err();
736+
735737
// Testing [`config_update`]
736738
vhost_block.device_state = DeviceState::Activated(ActiveState {
737739
mem: default_mem(),

‎src/vmm/src/devices/virtio/block/vhost_user/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub const QUEUE_SIZE: u16 = 256;
2020
pub enum VhostUserBlockError {
2121
/// Cannot create config
2222
Config,
23+
/// Device is not active
24+
DeviceNotActive,
2325
/// Snapshotting of vhost-user-blk devices is not supported
2426
SnapshottingNotSupported,
2527
/// Vhost-user error: {0}

0 commit comments

Comments
 (0)