Skip to content

Commit de67f48

Browse files
committed
fix(vmm): Add vendor ID normalization
The custom CPU template feature allows users to modify vendor ID. This results in a situation where the vendor ID mismatch the brand string, because the normalization is enforced on the brand string based on the host value. Adds additional normalization on the vendor ID to prevent the above situation. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent f419083 commit de67f48

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/vmm/src/cpu_config/x86_64/cpuid/normalize.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ pub enum NormalizeCpuidError {
2727
/// Failed to set extended cache features leaf.
2828
#[error("Failed to set extended cache features leaf: {0}")]
2929
ExtendedCacheFeatures(#[from] ExtendedCacheFeaturesError),
30+
/// Failed to set vendor ID in leaf 0x0.
31+
#[error("Failed to set vendor ID in leaf 0x0: {0}")]
32+
VendorId(#[from] VendorIdError),
33+
}
34+
35+
/// Error type for setting leaf 0 section.
36+
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
37+
pub enum VendorIdError {
38+
/// Leaf 0x0 is missing from CPUID.
39+
#[error("Leaf 0x0 is missing from CPUID.")]
40+
MissingLeaf0,
3041
}
3142

3243
/// Error type for setting leaf 1 section of `IntelCpuid::normalize`.
@@ -193,6 +204,7 @@ impl super::Cpuid {
193204
let cpus_per_core = 1u8
194205
.checked_shl(u32::from(cpu_bits))
195206
.ok_or(NormalizeCpuidError::CpuBits(cpu_bits))?;
207+
self.update_vendor_id()?;
196208
self.update_feature_info_entry(cpu_index, cpu_count)
197209
.map_err(NormalizeCpuidError::FeatureInformation)?;
198210
self.update_extended_topology_entry(cpu_index, cpu_count, cpu_bits, cpus_per_core)
@@ -213,6 +225,22 @@ impl super::Cpuid {
213225
}
214226
}
215227

228+
/// Pass-through the vendor ID from the host. This is used to prevent modification of the vendor
229+
/// ID via custom CPU templates.
230+
fn update_vendor_id(&mut self) -> Result<(), VendorIdError> {
231+
let leaf_0 = self
232+
.get_mut(&CpuidKey::leaf(0x0))
233+
.ok_or(VendorIdError::MissingLeaf0)?;
234+
235+
let host_leaf_0 = cpuid(0x0);
236+
237+
leaf_0.result.ebx = host_leaf_0.ebx;
238+
leaf_0.result.ecx = host_leaf_0.ecx;
239+
leaf_0.result.edx = host_leaf_0.edx;
240+
241+
Ok(())
242+
}
243+
216244
// Update feature information entry
217245
fn update_feature_info_entry(
218246
&mut self,

0 commit comments

Comments
 (0)