Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 158 additions & 20 deletions vm/devices/tpm/tpm_device/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use thiserror::Error;
use tpm_protocol::TPM_NV_INDEX_AIK_CERT;
use tpm_protocol::TPM_NV_INDEX_ATTESTATION_REPORT;
use tpm_protocol::TPM_NV_INDEX_GUEST_ATTESTATION_INPUT;
use tpm_protocol::TPM_NV_INDEX_MITIGATED;
use tpm_protocol::tpm20proto;
use tpm_protocol::tpm20proto::CommandCodeEnum;
use tpm_protocol::tpm20proto::TPM20_RH_PLATFORM;
Expand Down Expand Up @@ -631,6 +632,7 @@ impl Tpm {
force_ak_regen: bool,
large_vtpm_blob: bool,
fixup_16k_ak_cert: bool,
recovered: bool,
}

let quirks = {
Expand All @@ -642,7 +644,7 @@ impl Tpm {
.map_err(TpmErrorKind::ReadNvramState)?;

if let Some(mut blob) = existing_nvmem_blob {
if matches!(
let recovered = if matches!(
self.tpm_engine_helper.tpm_engine.version(),
TpmVersion::V138
) {
Expand All @@ -651,8 +653,10 @@ impl Tpm {
// that the TPM nvram is consistent - this code can be removed
// once the fix for reporting the NVRAM size correctly is
// everywhere.
recover::recover_blob(&mut blob);
}
recover::recover_blob(&mut blob)
} else {
false
};

if let Err(e) = self.tpm_engine_helper.tpm_engine.reset(Some(&blob)) {
if e.is_mismatched_blob_size() {
Expand All @@ -676,6 +680,9 @@ impl Tpm {
fixup_16k_ak_cert: blob.len() == LEGACY_VTPM_SIZE,

large_vtpm_blob: blob.len() >= STANDARD_VTPM_SIZE,

// Whether the TPM blob was recovered.
recovered,
}
} else {
TpmQuirks {
Expand All @@ -686,6 +693,8 @@ impl Tpm {
// This is a brand-new vTPM and will get provisioned with at
// least 32kB of storage.
large_vtpm_blob: true,
// No recovery happened.
recovered: false,
}
}
};
Expand All @@ -694,12 +703,31 @@ impl Tpm {
force_ak_regen,
fixup_16k_ak_cert,
large_vtpm_blob,
recovered,
} = quirks;

self.tpm_engine_helper
.initialize_tpm_engine()
.map_err(TpmErrorKind::InitializeTpmEngine)?;

// Read the mitigation marker before refreshing the TPM seeds. Create a
// missing AKCert index if the TPM has a large NVRAM, or if this is a
// refresh scenario (where OpenHCL was requested to refresh the TPM
// seeds) and there is no mitigation marker.
let create_if_missing = large_vtpm_blob
|| (self.refresh_tpm_seeds
Comment thread
stunes-ms marked this conversation as resolved.
Resolved
&& match self.tpm_engine_helper.find_nv_index(TPM_NV_INDEX_MITIGATED) {
Ok(index) => index.is_none(),
Comment on lines +717 to +720

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Preserve the recovery mitigation across seed refresh.

For an unmarked corrupt 16 KiB blob, recover_blob can return recovered = true, but this condition still sets create_if_missing = true when seeds are refreshed. ChangePPS clears platform-owned NV indices, including the old AKCert, before allocate_guest_attestation_nv_indices runs. The allocator therefore takes AkCertType::None and attempts a fresh 4 KiB allocation; its only mitigation-marker write is in AkCertType::PlatformOwned, so the recovered TPM is left unmarked. The explicit AKCert deletion is not the only cause: seed refresh already removes the platform index.

Please include current-boot recovery in the creation decision and preserve the recovery mitigation after seed refresh. A regression test combining corrupt-blob recovery with seed refresh, followed by persist/reload, would cover the missing path. The new matrix currently exercises healthy blobs and pre-existing markers only.

Err(e) => {
tracing::error!(
CVM_ALLOWED,
err = &e as &dyn std::error::Error,
"error reading TPM mitigation marker"
);
false
}
});

// If necessary, recreate EPS & PPS.
// The host indicates this when VM identity changes.
if self.refresh_tpm_seeds {
Expand All @@ -711,6 +739,18 @@ impl Tpm {
return Err(TpmErrorKind::RefreshTpmSeeds(e).into());
}

// Delete any existing AKCert after refreshing seeds.
if let Err(e) = self
.tpm_engine_helper
.nv_undefine_space(TPM20_RH_PLATFORM, TPM_NV_INDEX_AIK_CERT)
Comment on lines +742 to +745
{
tracing::error!(
CVM_ALLOWED,
err = &e as &dyn std::error::Error,
"error deleting AKCert after refreshing seeds"
);
}

tracing::info!("TPM seeds have been refreshed");
}

Expand Down Expand Up @@ -855,7 +895,8 @@ impl Tpm {
preserve_ak_cert: !self.refresh_tpm_seeds, // Preserve AK cert if TPM seeds are not refreshed
support_attestation_report: self.ak_cert_type.attested(),
mitigate_legacy_akcert: fixup_16k_ak_cert,
create_if_missing: large_vtpm_blob,
create_if_missing,
recovered,
},
)
{
Expand All @@ -875,9 +916,8 @@ impl Tpm {
}
TpmAkCertType::Trusted(_, _) => {
// Otherwise, if the existing AKCert index is platform-
// defined and this appears to be an HCL-provisioned
// vTPM, then handle AKCert renewal from OpenHCL.
self.tpm_engine_helper.has_platform_akcert_index() && large_vtpm_blob
// defined, then handle AKCert renewal from OpenHCL.
self.tpm_engine_helper.has_platform_akcert_index()
}
// If there's no AKCert, then don't handle renewal.
TpmAkCertType::None => false,
Expand Down Expand Up @@ -2375,7 +2415,9 @@ mod tests {
}

for version in [TpmVersion::V138, TpmVersion::V185] {
let mut tpm = new_test_tpm(version, None, None).await.unwrap();
let mut tpm = new_test_tpm(version, None, None, TpmAkCertType::None, false)
.await
.unwrap();
let key = TpmRsa2kPublic {
modulus: [0; 256],
exponent: [0; 3],
Expand Down Expand Up @@ -2431,7 +2473,9 @@ mod tests {
use tpm_protocol::tpm20proto::TPM20_RH_PLATFORM;

for version in [TpmVersion::V138, TpmVersion::V185] {
let mut tpm = new_test_tpm(version, None, None).await.unwrap();
let mut tpm = new_test_tpm(version, None, None, TpmAkCertType::None, false)
.await
.unwrap();
assert!(tpm.read_ak_cert().unwrap().is_none());

let old_time = std::time::UNIX_EPOCH;
Expand Down Expand Up @@ -2513,7 +2557,9 @@ mod tests {
#[async_test]
async fn test_ak_cert_write_failure_preserves_renewal_time() {
for version in [TpmVersion::V138, TpmVersion::V185] {
let mut tpm = new_test_tpm(version, None, None).await.unwrap();
let mut tpm = new_test_tpm(version, None, None, TpmAkCertType::None, false)
.await
.unwrap();
let logger = Arc::new(TestTpmLogger::default());
tpm.logger = Some(logger.clone());
tpm.tpm_engine_helper
Expand Down Expand Up @@ -2566,7 +2612,15 @@ mod tests {
/// Each library validates NVRAM against its compiled-in size.
#[async_test]
async fn test_legacy_nvram_size_rejected_for_v185() {
let Err(err) = new_test_tpm(TpmVersion::V185, Some(LEGACY_VTPM_SIZE), None).await else {
let Err(err) = new_test_tpm(
TpmVersion::V185,
Some(LEGACY_VTPM_SIZE),
None,
TpmAkCertType::None,
false,
)
.await
else {
panic!("legacy nvram size must be rejected");
};

Expand All @@ -2584,6 +2638,8 @@ mod tests {
TpmVersion::V185,
Some(default_vtpm_size(TpmVersion::V185)),
Some(vec![0; LEGACY_VTPM_SIZE]),
TpmAkCertType::None,
false,
)
.await
else {
Expand All @@ -2599,12 +2655,82 @@ mod tests {
/// The 1.38 implementation must keep accepting legacy state.
#[async_test]
async fn test_legacy_nvram_size_allowed_for_v138() {
assert!(
new_test_tpm(TpmVersion::V138, Some(LEGACY_VTPM_SIZE), None)
for refresh_tpm_seeds in [false, true] {
let mut tpm = new_test_tpm(
TpmVersion::V138,
Some(LEGACY_VTPM_SIZE),
Some(vec![0; LEGACY_VTPM_SIZE]),
TpmAkCertType::Trusted(Arc::new(TestRequestAkCertHelper::default()), None),
refresh_tpm_seeds,
)
.await
.expect("legacy nvram size is supported by 1.38");

let result = tpm
.tpm_engine_helper
.find_nv_index(TPM_NV_INDEX_AIK_CERT)
.expect("find_nv_index should succeed");
assert_eq!(result.is_some(), refresh_tpm_seeds);
if let Some(result) = result {
let nv_bits = TpmaNvBits::from(result.nv_public.nv_public.attributes.0.get());
assert!(nv_bits.nv_platformcreate());
}
assert_eq!(tpm.handle_ak_cert_renewal, refresh_tpm_seeds);
}
}

#[async_test]
async fn test_ak_cert_creation_from_existing_nvram() {
for nvram_size in [LEGACY_VTPM_SIZE, STANDARD_VTPM_SIZE] {
for has_mitigation_marker in [false, true] {
let mut tpm = new_test_tpm(
TpmVersion::V138,
Some(nvram_size),
Some(vec![0; nvram_size]),
TpmAkCertType::None,
false,
)
.await
.is_ok(),
"legacy nvram size is supported by 1.38"
);
.unwrap();
if has_mitigation_marker {
tpm.tpm_engine_helper
.nv_define_space(TPM20_RH_PLATFORM, 0, TPM_NV_INDEX_MITIGATED, 1)
.unwrap();
}
tpm.flush_pending_nvram().await.unwrap();
let blob = tpm.idle_stores().nvram.restore().await.unwrap().unwrap();
assert_eq!(blob.len(), nvram_size);

// Drop the TPM instance to uninitialize the underlying
// platform. Otherwise the next new_test_tpm will fail.
drop(tpm);

for refresh_tpm_seeds in [false, true] {
let mut tpm = new_test_tpm(
TpmVersion::V138,
Some(nvram_size),
Some(blob.clone()),
TpmAkCertType::Trusted(Arc::new(TestRequestAkCertHelper::default()), None),
refresh_tpm_seeds,
)
.await
.unwrap();

let expected = nvram_size != LEGACY_VTPM_SIZE
|| (refresh_tpm_seeds && !has_mitigation_marker);
let index = tpm
.tpm_engine_helper
.find_nv_index(TPM_NV_INDEX_AIK_CERT)
.unwrap();
assert_eq!(
index.is_some(),
expected,
"size={nvram_size}, refresh={refresh_tpm_seeds}, marker={has_mitigation_marker}"
);
assert_eq!(tpm.handle_ak_cert_renewal, expected);
}
}
}
}

/// Each library is compiled for a fixed NVRAM size and cannot be handed a
Expand All @@ -2613,7 +2739,9 @@ mod tests {
async fn test_default_nvram_size_initializes_both_versions() {
for version in [TpmVersion::V138, TpmVersion::V185] {
assert!(
new_test_tpm(version, None, None).await.is_ok(),
new_test_tpm(version, None, None, TpmAkCertType::None, false)
.await
.is_ok(),
"default nvram size must initialize {version:?}"
);
}
Expand All @@ -2623,7 +2751,15 @@ mod tests {
/// cleanly inside the library, so it must be rejected rather than used.
#[async_test]
async fn test_standard_nvram_size_rejected_for_v185() {
let Err(err) = new_test_tpm(TpmVersion::V185, Some(STANDARD_VTPM_SIZE), None).await else {
let Err(err) = new_test_tpm(
TpmVersion::V185,
Some(STANDARD_VTPM_SIZE),
None,
TpmAkCertType::None,
false,
)
.await
else {
panic!("32kB nvram size must be rejected for 1.85");
};

Expand All @@ -2637,6 +2773,8 @@ mod tests {
version: TpmVersion,
nvram_size: Option<usize>,
nvram_blob: Option<Vec<u8>>,
cert_type: TpmAkCertType,
refresh_tpm_seeds: bool,
) -> Result<Tpm, TpmError> {
let mut nvram_store = EphemeralNonVolatileStore::new_boxed();
if let Some(blob) = nvram_blob {
Expand All @@ -2651,9 +2789,9 @@ mod tests {
nvram_store,
nvram_size,
Box::new(|| std::time::Duration::new(0, 0)),
refresh_tpm_seeds,
false,
false,
TpmAkCertType::None,
cert_type,
None,
None,
false,
Expand Down
9 changes: 6 additions & 3 deletions vm/devices/tpm/tpm_device/src/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ fn check_blob(blob: &[u8]) -> Result<(), usize> {
}

/// Fixup an NVRAM blob that has data past the end, by zeroing out the last
/// header that refers to data past the end.
pub fn recover_blob(blob: &mut [u8]) {
/// header that refers to data past the end. Returns true if any changes were
/// made.
pub fn recover_blob(blob: &mut [u8]) -> bool {
let original_size = blob.len();
if original_size != LEGACY_VTPM_SIZE {
tracing::debug!("TPM NVRAM size is not legacy size, skipping recovery");
return;
return false;
}

match check_blob(blob) {
Ok(()) => {
tracing::info!("TPM NVRAM is already good, skipping recovery");
false
}
Err(bad_offset) => {
tracing::warn!("TPM NVRAM blob is corrupt, truncating at offset {bad_offset}");
blob[bad_offset..].fill(0);
true
}
}
}
4 changes: 3 additions & 1 deletion vm/devices/tpm/tpm_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ pub struct AllocateNvIndicesParams {
pub mitigate_legacy_akcert: bool,
/// Create the AKCert index if it is not present.
pub create_if_missing: bool,
/// Whether the TPM blob was recovered from a corrupted state.
pub recovered: bool,
}

impl<E: TpmEngine> TpmEngineHelper<E> {
Expand Down Expand Up @@ -808,7 +810,7 @@ impl<E: TpmEngine> TpmEngineHelper<E> {
let will_mitigate_cert =
params.mitigate_legacy_akcert && cert.len() == TPM_DEFAULT_AKCERT_SIZE;

if will_mitigate_cert {
if params.recovered && will_mitigate_cert {
Comment thread
stunes-ms marked this conversation as resolved.
Resolved
self.write_mitigation_marker(auth_value);
}

Expand Down
Loading
Loading