Skip to content

Commit f070aa4

Browse files
committed
Add mutex lock for TDX quote generation
The TDX driver does not support concurrent access. Add global mutex locks at both tdx-attest and dstack-attest layers to prevent race conditions: - tdx-attest: Lock in get_quote() to protect low-level TDX driver calls - dstack-attest: Lock in quote_with_app_id() for future TEE environments
1 parent 6f14cb3 commit f070aa4

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

dstack-attest/src/attestation.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const DSTACK_NITRO_ENCLAVE: &str = "dstack-nitro-enclave";
2828
#[cfg(feature = "quote")]
2929
const SYS_CONFIG_PATH: &str = "/dstack/.host-shared/.sys-config.json";
3030

31+
/// Global lock for quote generation. The underlying TDX driver does not support concurrent access.
32+
#[cfg(feature = "quote")]
33+
static QUOTE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
34+
3135
/// Read vm_config from sys-config.json
3236
#[cfg(feature = "quote")]
3337
fn read_vm_config() -> Result<String> {
@@ -574,6 +578,11 @@ impl Attestation {
574578
}
575579

576580
pub fn quote_with_app_id(report_data: &[u8; 64], app_id: Option<[u8; 20]>) -> Result<Self> {
581+
// Lock to prevent concurrent quote generation (TDX driver doesn't support it)
582+
let _guard = QUOTE_LOCK
583+
.lock()
584+
.map_err(|_| anyhow!("Quote lock poisoned"))?;
585+
577586
let mode = AttestationMode::detect()?;
578587
let runtime_events = if mode.is_composable() {
579588
RuntimeEvent::read_all().context("Failed to read runtime events")?

tdx-attest/src/linux.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ use tdx_attest_sys as sys;
66

77
use std::ptr;
88
use std::slice;
9+
use std::sync::Mutex;
910

1011
use sys::*;
1112

13+
/// Global lock for TDX attestation operations. The TDX driver does not support concurrent access.
14+
static TDX_LOCK: Mutex<()> = Mutex::new(());
15+
1216
use num_enum::FromPrimitive;
1317
use thiserror::Error;
1418

@@ -49,6 +53,9 @@ pub enum TdxAttestError {
4953
}
5054

5155
pub fn get_quote(report_data: &TdxReportData) -> Result<Vec<u8>> {
56+
// Lock to prevent concurrent access - TDX driver doesn't support it
57+
let _guard = TDX_LOCK.lock().map_err(|_| TdxAttestError::Busy)?;
58+
5259
let mut att_key_id = TdxUuid([0; TDX_UUID_SIZE as usize]);
5360
let mut quote_ptr = ptr::null_mut();
5461
let mut quote_size = 0;

0 commit comments

Comments
 (0)