runtime-rs: extend SEV/SEV-SNP detection by including a details struct

This matches the existing TDX handling where additional details are
retrieved right away after TDX is detected.  Note that the actual details
(cbitpos) acquisition is NOT included at this time.

This change might seem bigger than it is.  The change itself is just in
protection.rs, the rest are corresponding adjustments.

Signed-off-by: Pavel Mores <pmores@redhat.com>
This commit is contained in:
Pavel Mores 2025-01-30 14:06:22 +01:00 committed by Pavel Mores
parent c549d12da7
commit a3f973db3b
4 changed files with 41 additions and 20 deletions

View File

@ -31,14 +31,19 @@ pub struct TDXDetails {
pub minor_version: u32, pub minor_version: u32,
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SevSnpDetails {
pub cbitpos: u32,
}
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub enum GuestProtection { pub enum GuestProtection {
#[default] #[default]
NoProtection, NoProtection,
Tdx(TDXDetails), Tdx(TDXDetails),
Sev, Sev(SevSnpDetails),
Snp, Snp(SevSnpDetails),
Pef, Pef,
Se, Se,
} }
@ -51,8 +56,8 @@ impl fmt::Display for GuestProtection {
"tdx (major_version: {}, minor_version: {})", "tdx (major_version: {}, minor_version: {})",
details.major_version, details.minor_version details.major_version, details.minor_version
), ),
GuestProtection::Sev => write!(f, "sev"), GuestProtection::Sev(details) => write!(f, "sev (cbitpos: {}", details.cbitpos),
GuestProtection::Snp => write!(f, "snp"), GuestProtection::Snp(details) => write!(f, "snp (cbitpos: {}", details.cbitpos),
GuestProtection::Pef => write!(f, "pef"), GuestProtection::Pef => write!(f, "pef"),
GuestProtection::Se => write!(f, "se"), GuestProtection::Se => write!(f, "se"),
GuestProtection::NoProtection => write!(f, "none"), GuestProtection::NoProtection => write!(f, "none"),
@ -190,12 +195,22 @@ pub fn arch_guest_protection(
Ok(false) Ok(false)
}; };
if check_contents(snp_path)? { let retrieve_sev_cbitpos = || -> Result<u32, ProtectionError> {
return Ok(GuestProtection::Snp); Err(ProtectionError::CheckFailed(
} "cbitpos retrieval NOT IMPLEMENTED YET".to_owned(),
))
};
if check_contents(sev_path)? { let is_snp_available = check_contents(snp_path)?;
return Ok(GuestProtection::Sev); let is_sev_available = is_snp_available || check_contents(sev_path)?;
if is_snp_available || is_sev_available {
let cbitpos = retrieve_sev_cbitpos()?;
let sev_snp_details = SevSnpDetails { cbitpos };
return Ok(if is_snp_available {
GuestProtection::Snp(sev_snp_details)
} else {
GuestProtection::Sev(sev_snp_details)
});
} }
Ok(GuestProtection::NoProtection) Ok(GuestProtection::NoProtection)

View File

@ -549,7 +549,7 @@ fn get_platform_cfg(guest_protection_to_use: GuestProtection) -> Option<Platform
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use kata_sys_util::protection::TDXDetails; use kata_sys_util::protection::{SevSnpDetails, TDXDetails};
use kata_types::config::hypervisor::{ use kata_types::config::hypervisor::{
BlockDeviceInfo, Hypervisor as HypervisorConfig, SecurityInfo, BlockDeviceInfo, Hypervisor as HypervisorConfig, SecurityInfo,
}; };
@ -2180,6 +2180,8 @@ mod tests {
minor_version: 0, minor_version: 0,
}; };
let sev_snp_details = SevSnpDetails { cbitpos: 42 };
#[derive(Debug)] #[derive(Debug)]
struct TestData<'a> { struct TestData<'a> {
use_image: bool, use_image: bool,
@ -2202,14 +2204,14 @@ mod tests {
use_image: true, use_image: true,
container_rootfs_driver: "container", container_rootfs_driver: "container",
vm_rootfs_driver: "vm", vm_rootfs_driver: "vm",
guest_protection_to_use: GuestProtection::Sev, guest_protection_to_use: GuestProtection::Sev(sev_snp_details.clone()),
result: Ok(()), result: Ok(()),
}, },
TestData { TestData {
use_image: true, use_image: true,
container_rootfs_driver: "container", container_rootfs_driver: "container",
vm_rootfs_driver: "vm", vm_rootfs_driver: "vm",
guest_protection_to_use: GuestProtection::Snp, guest_protection_to_use: GuestProtection::Snp(sev_snp_details.clone()),
result: Ok(()), result: Ok(()),
}, },
TestData { TestData {

View File

@ -508,7 +508,7 @@ pub fn guest_protection_is_tdx(guest_protection_to_use: GuestProtection) -> bool
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use kata_sys_util::protection::TDXDetails; use kata_sys_util::protection::{SevSnpDetails, TDXDetails};
#[test] #[test]
fn test_guest_protection_is_tdx() { fn test_guest_protection_is_tdx() {
@ -517,6 +517,8 @@ mod tests {
minor_version: 0, minor_version: 0,
}; };
let sev_snp_details = SevSnpDetails { cbitpos: 42 };
#[derive(Debug)] #[derive(Debug)]
struct TestData { struct TestData {
protection: GuestProtection, protection: GuestProtection,
@ -537,11 +539,11 @@ mod tests {
result: false, result: false,
}, },
TestData { TestData {
protection: GuestProtection::Sev, protection: GuestProtection::Sev(sev_snp_details.clone()),
result: false, result: false,
}, },
TestData { TestData {
protection: GuestProtection::Snp, protection: GuestProtection::Snp(sev_snp_details.clone()),
result: false, result: false,
}, },
TestData { TestData {

View File

@ -956,7 +956,7 @@ fn get_ch_vcpu_tids(proc_path: &str) -> Result<HashMap<u32, u32>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use kata_sys_util::protection::TDXDetails; use kata_sys_util::protection::{SevSnpDetails, TDXDetails};
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use kata_sys_util::protection::TDX_SYS_FIRMWARE_DIR; use kata_sys_util::protection::TDX_SYS_FIRMWARE_DIR;
@ -990,6 +990,8 @@ mod tests {
minor_version: 0, minor_version: 0,
}; };
let sev_snp_details = SevSnpDetails { cbitpos: 42 };
#[derive(Debug)] #[derive(Debug)]
struct TestData { struct TestData {
value: Option<GuestProtection>, value: Option<GuestProtection>,
@ -1010,12 +1012,12 @@ mod tests {
result: Ok(GuestProtection::Se), result: Ok(GuestProtection::Se),
}, },
TestData { TestData {
value: Some(GuestProtection::Sev), value: Some(GuestProtection::Sev(sev_snp_details.clone())),
result: Ok(GuestProtection::Sev), result: Ok(GuestProtection::Sev(sev_snp_details.clone())),
}, },
TestData { TestData {
value: Some(GuestProtection::Snp), value: Some(GuestProtection::Snp(sev_snp_details.clone())),
result: Ok(GuestProtection::Snp), result: Ok(GuestProtection::Snp(sev_snp_details.clone())),
}, },
TestData { TestData {
value: Some(GuestProtection::Tdx(tdx_details.clone())), value: Some(GuestProtection::Tdx(tdx_details.clone())),