Skip to content

Commit 23e139f

Browse files
committed
refactor: extract TDX quote report_data offset as a named constant
Replace the duplicated magic offset `568..632` with a named constant `TDX_QUOTE_REPORT_DATA_RANGE` and add a warning log when the quote is too short to patch report_data, which would indicate data corruption. Follow-up to PR #541.
1 parent 00d3a9f commit 23e139f

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

dstack-attest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ serde_json.workspace = true
2626
sha2.workspace = true
2727
sha3.workspace = true
2828
tdx-attest.workspace = true
29+
tracing.workspace = true
2930
insta.workspace = true
3031
errify.workspace = true
3132

dstack-attest/src/attestation.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
//! Attestation functions
66
7+
/// Byte range of the REPORT_DATA field within a TDX quote.
8+
/// In Intel TDX ECDSA quote format, the TD Report body starts at offset 568
9+
/// and REPORT_DATA occupies bytes 568..632 (64 bytes).
10+
pub const TDX_QUOTE_REPORT_DATA_RANGE: std::ops::Range<usize> = 568..632;
11+
712
use std::{borrow::Cow, time::SystemTime};
813

914
use anyhow::{anyhow, bail, Context, Result};
@@ -274,8 +279,14 @@ impl VersionedAttestation {
274279
let VersionedAttestation::V0 { attestation } = self;
275280
attestation.report_data = report_data;
276281
if let Some(tdx_quote) = attestation.tdx_quote_mut() {
277-
if tdx_quote.quote.len() >= 632 {
278-
tdx_quote.quote[568..632].copy_from_slice(&report_data);
282+
if tdx_quote.quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
283+
tdx_quote.quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
284+
} else {
285+
tracing::warn!(
286+
"TDX quote too short to patch report_data ({} < {})",
287+
tdx_quote.quote.len(),
288+
TDX_QUOTE_REPORT_DATA_RANGE.end
289+
);
279290
}
280291
}
281292
}

guest-agent/src/rpc_service.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use k256::ecdsa::SigningKey;
2828
use or_panic::ResultOrPanic;
2929
use ra_rpc::{Attestation, CallContext, RpcCall};
3030
use ra_tls::{
31-
attestation::{QuoteContentType, VersionedAttestation, DEFAULT_HASH_ALGORITHM},
31+
attestation::{
32+
QuoteContentType, VersionedAttestation, DEFAULT_HASH_ALGORITHM,
33+
TDX_QUOTE_REPORT_DATA_RANGE,
34+
},
3235
cert::CertConfigV2,
3336
kdf::{derive_key, derive_p256_key_pair_from_bytes},
3437
};
@@ -486,7 +489,7 @@ fn simulate_quote(
486489
return Err(anyhow::anyhow!("Quote not found"));
487490
};
488491

489-
quote.quote[568..632].copy_from_slice(&report_data);
492+
quote.quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
490493
Ok(GetQuoteResponse {
491494
quote: quote.quote.to_vec(),
492495
event_log: serde_json::to_string(&quote.event_log)

0 commit comments

Comments
 (0)