-
Notifications
You must be signed in to change notification settings - Fork 32
MigTD: add retry for quote generation #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haitaohuang
wants to merge
3
commits into
intel:main
Choose a base branch
from
haitaohuang:quote_retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // Copyright (c) Microsoft Corporation | ||
| // | ||
| // SPDX-License-Identifier: BSD-2-Clause-Patent | ||
|
|
||
| //! Quote generation with retry logic for handling security updates | ||
| //! | ||
| //! This module provides a resilient GetQuote flow that can handle impactless security | ||
| //! updates. If an update happens after the REPORT is retrieved but before the QUOTE | ||
| //! is generated, the Quoting Enclave may reject the REPORT. This module handles | ||
| //! such scenarios with simple exponential backoff retry. | ||
|
|
||
| #![cfg(feature = "attestation")] | ||
|
|
||
| use alloc::vec::Vec; | ||
|
|
||
| #[cfg(not(feature = "AzCVMEmu"))] | ||
| use tdx_tdcall::tdreport::tdcall_report; | ||
|
|
||
| #[cfg(feature = "AzCVMEmu")] | ||
| use tdx_tdcall_emu::tdreport::tdcall_report; | ||
|
|
||
| /// Initial retry delay in milliseconds (5 seconds) | ||
| #[cfg(not(feature = "AzCVMEmu"))] | ||
| const INITIAL_DELAY_MS: u64 = 5000; | ||
|
|
||
| //shorter for testing | ||
| #[cfg(feature = "AzCVMEmu")] | ||
| const INITIAL_DELAY_MS: u64 = 20; | ||
|
|
||
| /// Maximum number of attempts before giving up | ||
| const MAX_ATTEMPTS: u32 = 6; // Total wait time up to ~2.5 minutes with 5s initial delay | ||
|
|
||
| /// Error type for quote generation with retry | ||
| #[derive(Debug)] | ||
| pub enum QuoteError { | ||
| /// Failed to generate TD report | ||
| ReportGenerationFailed, | ||
| /// Quote generation failed after all retry attempts | ||
| QuoteGenerationFailed, | ||
| } | ||
|
|
||
| /// Get a quote with retry logic to handle potential security updates | ||
| /// | ||
| /// On quote failure, fetches a new TD REPORT and retries with exponential backoff. | ||
| /// | ||
| /// # Arguments | ||
| /// * `additional_data` - The 64-byte additional data to include in the TD REPORT | ||
| /// | ||
| /// # Returns | ||
| /// * `Ok((quote, report))` - The generated quote and the TD REPORT used | ||
| /// * `Err(QuoteError)` - If TD report/quote generation fails | ||
| pub fn get_quote_with_retry(additional_data: &[u8; 64]) -> Result<(Vec<u8>, Vec<u8>), QuoteError> { | ||
| let mut delay_ms = INITIAL_DELAY_MS; | ||
|
|
||
| for attempt in 1..=MAX_ATTEMPTS { | ||
| // Get TD REPORT | ||
| let current_report = tdcall_report(additional_data).map_err(|e| { | ||
| log::error!("Failed to get TD report: {:?}\n", e); | ||
| QuoteError::ReportGenerationFailed | ||
| })?; | ||
|
|
||
| let report_bytes = current_report.as_bytes(); | ||
|
|
||
| // Attempt to get quote | ||
| match attestation::get_quote(report_bytes) { | ||
| Ok(quote) => { | ||
| log::info!("Quote generated successfully\n"); | ||
| return Ok((quote, report_bytes.to_vec())); | ||
| } | ||
| Err(e) => { | ||
| if attempt < MAX_ATTEMPTS { | ||
| log::warn!( | ||
| "GetQuote failed (attempt {}/{}): {:?}, retrying with delay of {}ms\n", | ||
| attempt, | ||
| MAX_ATTEMPTS, | ||
| e, | ||
| delay_ms | ||
| ); | ||
| delay_milliseconds(delay_ms); | ||
| delay_ms *= 2; | ||
| } else { | ||
| log::error!("GetQuote failed after {} attempts: {:?}\n", MAX_ATTEMPTS, e); | ||
| return Err(QuoteError::QuoteGenerationFailed); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Should be unreachable because the final attempt returns above on failure. | ||
| Err(QuoteError::QuoteGenerationFailed) | ||
| } | ||
|
|
||
| /// Delay for the specified number of milliseconds | ||
| #[cfg(feature = "AzCVMEmu")] | ||
| fn delay_milliseconds(ms: u64) { | ||
| std::thread::sleep(std::time::Duration::from_millis(ms)); | ||
| } | ||
|
|
||
| #[cfg(not(feature = "AzCVMEmu"))] | ||
| fn delay_milliseconds(ms: u64) { | ||
| use crate::driver::ticks::Timer; | ||
| use core::future::Future; | ||
| use core::pin::Pin; | ||
| use core::task::{Context, Poll, Waker}; | ||
| use core::time::Duration; | ||
| use td_payload::arch::apic::{disable, enable_and_hlt}; | ||
|
|
||
| let mut timer = Timer::after(Duration::from_millis(ms)); | ||
| let waker = Waker::noop(); | ||
| let mut cx = Context::from_waker(&waker); | ||
|
|
||
| loop { | ||
| if let Poll::Ready(()) = Pin::new(&mut timer).poll(&mut cx) { | ||
| break; | ||
| } | ||
| enable_and_hlt(); | ||
| disable(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.