vmm: config: Add DiskConfig check for device serial length

Signed-off-by: Jinrong Liang <cloudliang@tencent.com>
This commit is contained in:
Jinrong Liang 2025-06-12 17:35:53 +08:00 committed by Rob Bradford
parent a336533389
commit bb6ca56fb0
3 changed files with 21 additions and 0 deletions

1
Cargo.lock generated
View file

@ -2472,6 +2472,7 @@ dependencies = [
"uuid",
"vfio-ioctls",
"vfio_user",
"virtio-bindings",
"virtio-devices",
"virtio-queue",
"vm-allocator",

View file

@ -71,6 +71,7 @@ tracer = { path = "../tracer" }
uuid = "1.12.1"
vfio-ioctls = { workspace = true, default-features = false }
vfio_user = { workspace = true }
virtio-bindings = { workspace = true }
virtio-devices = { path = "../virtio-devices" }
virtio-queue = { workspace = true }
vm-allocator = { path = "../vm-allocator" }

View file

@ -14,6 +14,7 @@ use option_parser::{
};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use virtio_bindings::virtio_blk::VIRTIO_BLK_ID_BYTES;
use virtio_devices::block::MINIMUM_BLOCK_QUEUE_SIZE;
use virtio_devices::{RateLimiterConfig, TokenBucketConfig};
@ -221,6 +222,8 @@ pub enum ValidationError {
LandlockPathDoesNotExist(PathBuf),
/// Access provided in landlock-rules in invalid
InvalidLandlockAccess(String),
/// Invalid block device serial length
InvalidSerialLength(usize, usize),
}
type ValidationResult<T> = std::result::Result<T, ValidationError>;
@ -391,6 +394,12 @@ impl fmt::Display for ValidationError {
InvalidLandlockAccess(s) => {
write!(f, "{s}")
}
InvalidSerialLength(actual, max) => {
write!(
f,
"Block device serial length ({actual}) exceeds maximum allowed length ({max})"
)
}
}
}
}
@ -1358,6 +1367,16 @@ impl DiskConfig {
return Err(ValidationError::InvalidRateLimiterGroup);
}
// Check Block device serial length
if let Some(ref serial) = self.serial {
if serial.len() > VIRTIO_BLK_ID_BYTES as usize {
return Err(ValidationError::InvalidSerialLength(
serial.len(),
VIRTIO_BLK_ID_BYTES as usize,
));
}
}
Ok(())
}
}