diff --git a/CHANGELOG.md b/CHANGELOG.md index d254f54c7..8f5cc0dca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 requirement in the policy bound by `compose_hash` instead of the `gpu-attestation` event. SEV-SNP in public clouds stays unsupported. +### Fixed + +- The guest agent answers legacy `Info` without a quote on TDX guests whose + kernel exposes the TSM measurement sysfs, so `Info` no longer queues behind + quote callers. Callers that wait more than 10 seconds for the attestation + queue receive HTTP 503 and can retry. + ## [0.6.6] - 2026-11-20 ### Upgrade notes diff --git a/dstack/guest-agent/Cargo.toml b/dstack/guest-agent/Cargo.toml index 4d81c6172..2f7d739af 100644 --- a/dstack/guest-agent/Cargo.toml +++ b/dstack/guest-agent/Cargo.toml @@ -67,3 +67,4 @@ lspci.workspace = true # the synthetic-attestation helpers. Declared here so the production build of # this crate does not carry them. dstack-attest = { workspace = true, features = ["simulator"] } +tokio = { workspace = true, features = ["test-util"] } diff --git a/dstack/guest-agent/src/backend.rs b/dstack/guest-agent/src/backend.rs index fcdf471bb..c671a3432 100644 --- a/dstack/guest-agent/src/backend.rs +++ b/dstack/guest-agent/src/backend.rs @@ -3,11 +3,39 @@ // SPDX-License-Identifier: Apache-2.0 use anyhow::{Context, Result}; +use cc_eventlog::TdxEvent; use dstack_guest_agent_rpc::v0::GetQuoteResponse; use ra_tls::attestation::Attestation; -use ra_tls::attestation::{QuoteContentType, VersionedAttestation}; +use ra_tls::attestation::{ + AttestationV1, QuoteContentType, TdxAttestationExt, VersionedAttestation, +}; +use tdx_attest::TdxMeasurements; + +/// The TDX measurements and event log legacy `Info` reports. +pub struct TcbInfo { + pub measurements: Option, + pub event_log: Vec, +} + +impl From for TcbInfo { + fn from(attestation: AttestationV1) -> Self { + Self { + measurements: attestation.td10_report().map(|report| TdxMeasurements { + mrtd: report.mr_td, + rtmrs: [report.rt_mr0, report.rt_mr1, report.rt_mr2, report.rt_mr3], + }), + event_log: attestation.tdx_event_log().unwrap_or_default().to_vec(), + } + } +} pub trait PlatformBackend: Send + Sync { + /// Read the TCB info without a quote, or `None` to fall back to + /// [`Self::attestation_for_info`]. + fn local_tcb_info(&self) -> Result> { + Ok(None) + } + fn attestation_for_info(&self) -> Result; fn certificate_attestation(&self, pubkey: &[u8]) -> Result; fn quote_response(&self, report_data: [u8; 64], vm_config: &str) -> Result; @@ -20,6 +48,21 @@ pub trait PlatformBackend: Send + Sync { pub struct RealPlatform; impl PlatformBackend for RealPlatform { + fn local_tcb_info(&self) -> Result> { + let Some(measurements) = + tdx_attest::read_measurements().context("failed to read TDX measurements")? + else { + return Ok(None); + }; + let mut event_log = + cc_eventlog::tdx::read_event_log().context("failed to read event log")?; + cc_eventlog::tdx::fill_v2_preimages(&mut event_log); + Ok(Some(TcbInfo { + measurements: Some(measurements), + event_log, + })) + } + fn attestation_for_info(&self) -> Result { Ok(Attestation::local() .context("Failed to get local attestation")? diff --git a/dstack/guest-agent/src/rpc_service.rs b/dstack/guest-agent/src/rpc_service.rs index 234e95f63..2ba67bc46 100644 --- a/dstack/guest-agent/src/rpc_service.rs +++ b/dstack/guest-agent/src/rpc_service.rs @@ -25,12 +25,10 @@ use ed25519_dalek::{Signer as Ed25519Signer, SigningKey as Ed25519SigningKey, Ve use fs_err as fs; use k256::ecdsa::SigningKey; use or_panic::ResultOrPanic; -use ra_rpc::{CallContext, RpcCall}; +use ra_rpc::{CallContext, ResultExt as _, RpcCall}; use ra_tls::{ api_v1::sign_recoverable_keccak256, - attestation::{ - QuoteContentType, TdxAttestationExt, VersionedAttestation, DEFAULT_HASH_ALGORITHM, - }, + attestation::{QuoteContentType, VersionedAttestation, DEFAULT_HASH_ALGORITHM}, cert::{CertConfigV2, CertSigningRequestV2, Csr}, kdf::{derive_key, derive_p256_key_pair_from_bytes}, }; @@ -41,7 +39,7 @@ use serde_json::json; use tracing::error; use crate::{ - backend::{PlatformBackend, RealPlatform}, + backend::{PlatformBackend, RealPlatform, TcbInfo}, config::Config, }; @@ -62,6 +60,13 @@ const IDENTITY_RETRY_INTERVAL: Duration = Duration::from_secs(30); /// every `Info` may start: a quote plus an RA-TLS round trip to the KMS. const DEMO_CERT_RETRY_INTERVAL: Duration = Duration::from_secs(30); +/// How long a caller may wait for the attestation queue. +/// +/// Callers keep running after their client disconnects, so without a bound a +/// slow platform lets abandoned requests take every quote slot from live ones. +/// A timeout counts as a failure for the identity and demo-cert throttles. +const ATTEST_QUEUE_TIMEOUT: Duration = Duration::from_secs(10); + /// Decode the immutable identity fields out of a boot attestation. /// /// Costs a quote and an event-log replay, which is why its result is cached for @@ -183,13 +188,18 @@ impl AppStateInner { /// running one on the executor stalls every connection the agent serves, /// including the systemd watchdog heartbeat. Callers queue here rather than /// on `dstack-attest`'s quote lock, so a flood of requests holds no threads. - /// The guard moves into the task, so a cancelled caller cannot let the - /// next call in while its own is still running. + /// The wait is bounded by [`ATTEST_QUEUE_TIMEOUT`]. The guard moves into + /// the task, so a cancelled caller cannot let the next call in while its + /// own is still running. async fn attest( &self, work: impl FnOnce(&dyn PlatformBackend) -> Result + Send + 'static, ) -> Result { - let guard = self.attest_lock.clone().lock_owned().await; + let guard = + tokio::time::timeout(ATTEST_QUEUE_TIMEOUT, self.attest_lock.clone().lock_owned()) + .await + .context("the attestation queue is busy, retry later") + .with_code(503)?; let platform = self.platform.clone(); tokio::task::spawn_blocking(move || { let _guard = guard; @@ -199,6 +209,23 @@ impl AppStateInner { .context("the attestation task panicked")? } + /// The TCB info for legacy `Info`, read without a quote where the platform + /// allows, so `Info` does not queue behind quote callers. + async fn tcb_info(&self) -> Result { + let platform = self.platform.clone(); + let local = tokio::task::spawn_blocking(move || platform.local_tcb_info()) + .await + .context("the TCB info task panicked")??; + match local { + Some(tcb) => Ok(tcb), + None => Ok(self + .attest(|platform| platform.attestation_for_info()) + .await? + .into_v1() + .into()), + } + } + async fn issue_cert( &self, key: &KeyPair, @@ -667,32 +694,19 @@ pub async fn get_info(state: &AppState, external: bool) -> Result { let (tcb_info, vm_config) = if hide_tcb_info { Default::default() } else { - let attestation = state - .inner - .attest(|platform| platform.attestation_for_info()) - .await? - .into_v1(); - let td_report = match attestation.td10_report() { - Some(report) => json!({ - "mrtd": hex::encode(report.mr_td), - "rtmr0": hex::encode(report.rt_mr0), - "rtmr1": hex::encode(report.rt_mr1), - "rtmr2": hex::encode(report.rt_mr2), - "rtmr3": hex::encode(report.rt_mr3), - }), - None => json!({}), - }; + let tcb = state.inner.tcb_info().await?; + let mr = tcb.measurements.as_ref(); let tcb_info = serde_json::to_string_pretty(&json!({ - "mrtd": td_report["mrtd"], - "rtmr0": td_report["rtmr0"], - "rtmr1": td_report["rtmr1"], - "rtmr2": td_report["rtmr2"], - "rtmr3": td_report["rtmr3"], + "mrtd": mr.map(|m| hex::encode(m.mrtd)), + "rtmr0": mr.map(|m| hex::encode(m.rtmrs[0])), + "rtmr1": mr.map(|m| hex::encode(m.rtmrs[1])), + "rtmr2": mr.map(|m| hex::encode(m.rtmrs[2])), + "rtmr3": mr.map(|m| hex::encode(m.rtmrs[3])), "mr_aggregated": hex::encode(&identity.mr_aggregated), "os_image_hash": hex::encode(&identity.os_image_hash), "compose_hash": hex::encode(&identity.compose_hash), "device_id": hex::encode(&identity.device_id), - "event_log": attestation.tdx_event_log().unwrap_or_default(), + "event_log": tcb.event_log, "app_compose": state.config().app_compose.raw, })) .unwrap_or_default(); @@ -1221,7 +1235,9 @@ pub(crate) mod tests { Signature as Ed25519Signature, Verifier, VerifyingKey as Ed25519VerifyingKey, }; use k256::ecdsa::{Signature as K256Signature, VerifyingKey}; - use ra_tls::attestation::{AttestationV1, PlatformEvidence, VersionedAttestation}; + use ra_tls::attestation::{ + AttestationV1, PlatformEvidence, TdxAttestationExt, VersionedAttestation, + }; use sha2::{Digest as _, Sha256}; use std::collections::HashSet; use std::convert::TryFrom; @@ -1527,6 +1543,14 @@ pNs85uhOZE8z2jr8Pg== } impl PlatformBackend for TestSimulatorPlatform { + fn local_tcb_info(&self) -> Result> { + let attestation = self.attestation.clone().into_v1(); + if !matches!(attestation.platform, PlatformEvidence::Tdx { .. }) { + return Ok(None); + } + Ok(Some(attestation.into())) + } + fn attestation_for_info(&self) -> Result { self.probe.calls.fetch_add(1, Ordering::Relaxed); if self.probe.failing.load(Ordering::Relaxed) { @@ -2274,6 +2298,84 @@ pNs85uhOZE8z2jr8Pg== quote.await.unwrap().unwrap(); } + /// Legacy `Info` reads the TCB info without a quote, so it answers while a + /// quote holds the queue, with the same document the quote would give. + #[tokio::test] + async fn legacy_info_does_not_queue_behind_a_quote() { + let probe = Arc::new(InfoAttestationProbe::default()); + let (state, _guard) = setup_test_state_with_probe(probe.clone()).await; + let identity = state.identity().await.unwrap(); + state.inner.demo_cert.set(String::new()).unwrap(); + let (entered_rx, release_tx) = probe.arm_gate(); + let quote = tokio::spawn({ + let state = state.clone(); + async move { state.quote_response([0; 64]).await } + }); + entered_rx.await.unwrap(); + + let info = tokio::time::timeout(Duration::from_secs(2), get_info(&state, false)) + .await + .expect("Info answers while the quote is held") + .unwrap(); + let attestation = state + .inner + .platform + .attestation_for_info() + .unwrap() + .into_v1(); + let report = attestation.td10_report().unwrap(); + let expected = json!({ + "mrtd": hex::encode(report.mr_td), + "rtmr0": hex::encode(report.rt_mr0), + "rtmr1": hex::encode(report.rt_mr1), + "rtmr2": hex::encode(report.rt_mr2), + "rtmr3": hex::encode(report.rt_mr3), + "mr_aggregated": hex::encode(&identity.mr_aggregated), + "os_image_hash": hex::encode(&identity.os_image_hash), + "compose_hash": hex::encode(&identity.compose_hash), + "device_id": hex::encode(&identity.device_id), + "event_log": attestation.tdx_event_log().unwrap(), + "app_compose": state.config().app_compose.raw, + }); + assert_eq!( + info.tcb_info, + serde_json::to_string_pretty(&expected).unwrap() + ); + + release_tx.send(()).unwrap(); + quote.await.unwrap().unwrap(); + } + + #[tokio::test(start_paused = true)] + async fn a_platform_call_gives_up_on_a_held_queue_with_503() { + let (state, _guard) = setup_test_state_with_probe(Arc::default()).await; + let held = state.inner.attest_lock.lock().await; + + let err = state + .quote_response([0; 64]) + .await + .expect_err("the queue is held"); + assert_eq!(ra_rpc::code_of(&err), Some(503)); + + drop(held); + state.quote_response([0; 64]).await.unwrap(); + } + + #[tokio::test(start_paused = true)] + async fn identity_callers_wait_at_most_one_queue_timeout() { + let probe = Arc::new(InfoAttestationProbe::default()); + let (state, _guard) = setup_test_state_with_probe(probe.clone()).await; + let _held = state.inner.attest_lock.lock().await; + + let start = tokio::time::Instant::now(); + let (first, second) = tokio::join!(state.identity(), state.identity()); + assert_eq!(start.elapsed(), ATTEST_QUEUE_TIMEOUT); + assert_eq!(ra_rpc::code_of(&first.unwrap_err()), Some(503)); + let throttled = second.unwrap_err().to_string(); + assert!(throttled.contains("next attempt"), "{throttled}"); + assert_eq!(probe.calls(), 0); + } + /// A failed decode must be as cheap to repeat as a cached success is. /// `identity()` is reached from the anonymous `/prpc/v1/Info`, and each /// attempt is a hardware quote plus an RTMR replay under the global quote diff --git a/dstack/tdx-attest/src/dummy.rs b/dstack/tdx-attest/src/dummy.rs index b153474de..9dd9ede0e 100644 --- a/dstack/tdx-attest/src/dummy.rs +++ b/dstack/tdx-attest/src/dummy.rs @@ -8,7 +8,7 @@ use thiserror::Error; -use crate::{Result, TdxReport, TdxReportData}; +use crate::{Result, TdxMeasurements, TdxReport, TdxReportData}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] pub enum TdxAttestError { @@ -50,6 +50,10 @@ pub fn extend_rtmr(_index: u32, _event_type: u32, _digest: [u8; 48]) -> Result<( Err(TdxAttestError::NotSupported) } +pub fn read_measurements() -> Result> { + Ok(None) +} + pub fn is_tdx_available() -> bool { false } diff --git a/dstack/tdx-attest/src/lib.rs b/dstack/tdx-attest/src/lib.rs index 4c73fb02c..f8aa96699 100644 --- a/dstack/tdx-attest/src/lib.rs +++ b/dstack/tdx-attest/src/lib.rs @@ -22,3 +22,10 @@ pub type TdxReportData = [u8; 64]; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TdxReport(pub [u8; 1024]); + +/// MRTD and RTMR0-3 of the running TD. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TdxMeasurements { + pub mrtd: [u8; 48], + pub rtmrs: [[u8; 48]; 4], +} diff --git a/dstack/tdx-attest/src/linux.rs b/dstack/tdx-attest/src/linux.rs index bde47ce34..66d4d1a94 100644 --- a/dstack/tdx-attest/src/linux.rs +++ b/dstack/tdx-attest/src/linux.rs @@ -23,7 +23,7 @@ use std::time::{Duration, Instant}; use thiserror::Error; -use crate::{Result, TdxReport, TdxReportData}; +use crate::{Result, TdxMeasurements, TdxReport, TdxReportData}; // ============================================================================ // Constants @@ -309,6 +309,35 @@ fn rtmr_sysfs_base() -> Result> { .then(|| PathBuf::from(RTMR_SYSFS_BASE))) } +/// Read MRTD and RTMR0-3 from the kernel's TSM measurement sysfs, or `None` on +/// kernels without it. +/// +/// Unlike [`extend_rtmr`], this ignores `DCAP_TDX_RTMR_SYSFS_PATH`: the TEE +/// simulator points it at a directory without `mrtd`, and callers there can +/// read the measurements from a quote instead. +pub fn read_measurements() -> Result> { + let base = Path::new(RTMR_SYSFS_BASE); + if !base.is_dir() { + return Ok(None); + } + let read = |name: &str| -> Result<[u8; 48]> { + let path = base.join(format!("{name}:sha384")); + fs::read(&path) + .map_err(|e| TdxAttestError::DeviceFailure(format!("read {}: {e}", path.display())))? + .try_into() + .map_err(|_| TdxAttestError::DeviceFailure(format!("{}: not 48 bytes", path.display()))) + }; + Ok(Some(TdxMeasurements { + mrtd: read("mrtd")?, + rtmrs: [ + read("rtmr0")?, + read("rtmr1")?, + read("rtmr2")?, + read("rtmr3")?, + ], + })) +} + fn extend_rtmr_via_sysfs(base: &Path, index: u32, digest: &[u8; 48]) -> Result<()> { let path = base.join(format!("rtmr{index}:sha384"));