From ab6b0340f83eafa62fcbcc48e2006c36fb3cbc28 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 24 Nov 2026 03:03:15 -0800 Subject: [PATCH 1/8] feat(vmm): launch AMD SEV-SNP guests with Secure TSC Without Secure TSC the hypervisor can intercept RDTSC and change the TSC offset and scale, so guest time is host-controlled between NTS syncs. Add cvm.snp_secure_tsc (off by default, needs QEMU >= 11.1 and host kernel >= 6.18). It adds secure-tsc=on to the sev-snp-guest object and sets SEV_FEATURES bit 9 in the measurement document; dstack-mr now accepts that bit. Signed-off-by: Kevin Wang --- CHANGELOG.md | 8 ++++++ docs/design-and-hardening-decisions.md | 2 ++ dstack/dstack-mr/src/sev.rs | 34 ++++++++++++++------------ dstack/vmm/src/app.rs | 6 ++++- dstack/vmm/src/app/qemu.rs | 6 +++-- dstack/vmm/src/config.rs | 6 +++++ dstack/vmm/vmm.toml | 4 +++ 7 files changed, 47 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17f6f1ca1f..2ce184b2ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **Secure TSC for AMD SEV-SNP guests.** `snp_secure_tsc = true` under `[cvm]` + in `vmm.toml` launches SNP guests with Secure TSC, so the host cannot skew + the guest clock. Off by default: it needs QEMU >= 11.1, host kernel >= 6.18 + and a CPU with the `snp_secure_tsc` flag. A KMS or verifier older than this + release rejects such a guest. + ### Changed - **Go SDK module moves to `github.com/Dstack-TEE/dstack/sdk/go/dstack`.** diff --git a/docs/design-and-hardening-decisions.md b/docs/design-and-hardening-decisions.md index b6ffbb9cd2..5bd2222ed2 100644 --- a/docs/design-and-hardening-decisions.md +++ b/docs/design-and-hardening-decisions.md @@ -57,6 +57,8 @@ See [here](https://intel.github.io/ccc-linux-guest-hardening-docs/security-spec. **Implementation**: dstack OS enforces the guest kernel uses TSC as the only timer source by appending `tsc=reliable no-kvmclock` to the kernel cmdline. It also enforces the use of NTS with built-in [trusted servers](../os/yocto/layers/meta-dstack/recipes-core/chrony/files/chrony.conf) to synchronize system time. +On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. Set `snp_secure_tsc = true` under `[cvm]` in `vmm.toml` to enable it (QEMU >= 11.1, host kernel >= 6.18, a CPU with the `snp_secure_tsc` flag). The launch measurement covers the setting, but nothing requires it yet, so a host may still launch without it. + **Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. If `secure_time` is disabled, time synchronization is not enforced before application launch. **Rationale**: Time synchronization is provided as an optional feature because the process typically requires tens of seconds to complete. Applications can function without the `secure_time` option enabled and may implement their own time synchronization mechanisms if required. diff --git a/dstack/dstack-mr/src/sev.rs b/dstack/dstack-mr/src/sev.rs index b413e8b430..007a49c0b7 100644 --- a/dstack/dstack-mr/src/sev.rs +++ b/dstack/dstack-mr/src/sev.rs @@ -39,6 +39,9 @@ const VMSA_GPA: u64 = 0x0000_FFFF_FFFF_F000; /// the same bit layout the SEV-SNP ABI's `GUEST_FEATURES` field in /// `SNP_LAUNCH_START` carries). Set on every SNP guest. const SEV_FEATURE_SNP_ACTIVE: u64 = 1 << 0; +/// `SecureTSC`, bit 9: the PSP fixes the guest TSC scale and offset, so the +/// hypervisor can no longer intercept or skew the guest's TSC. +pub const SEV_FEATURE_SECURE_TSC: u64 = 1 << 9; /// `SEV_FEATURES` bits a dstack launch may carry. /// @@ -55,16 +58,13 @@ const SEV_FEATURE_SNP_ACTIVE: u64 = 1 << 0; /// SecureTSC (bit 9) or VmsaRegProt (bit 14) likewise removes a guest-side /// protection the operator may believe is on. /// -/// dstack has exactly one launch path, and it sets no feature property: -/// `sev-snp-guest,id=sev0,policy=0x30000,...` in `dstack/vmm/src/app/qemu.rs`, -/// which leaves KVM to start the guest with `SNPActive` alone, and the VMM -/// writes the matching `guest_features: 1` into the measurement document -/// (`dstack/vmm/src/app.rs`). `1` is therefore the only value dstack has ever -/// produced — the captured real-hardware vector in this module's tests carries -/// it too — so it is the only value accepted. Widening this is a one-line -/// change once a launch path actually sets another bit; accepting bits nothing -/// emits would only ever admit a guest dstack did not configure. -const ALLOWED_GUEST_FEATURES: u64 = SEV_FEATURE_SNP_ACTIVE; +/// dstack has exactly one launch path, `sev-snp-guest,id=sev0,policy=0x30000,...` +/// in `dstack/vmm/src/app/qemu.rs`. It starts the guest with `SNPActive`, plus +/// `SecureTSC` when the host sets `cvm.snp_secure_tsc`, and the VMM writes the +/// matching `guest_features` into the measurement document +/// (`dstack/vmm/src/app.rs`). Those are the only bits accepted; accepting bits +/// nothing emits would only ever admit a guest dstack did not configure. +const ALLOWED_GUEST_FEATURES: u64 = SEV_FEATURE_SNP_ACTIVE | SEV_FEATURE_SECURE_TSC; #[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] #[serde(deny_unknown_fields)] @@ -150,7 +150,7 @@ pub fn validate_measurement_input(input: &MeasurementInput) -> Result<()> { if unexpected_features != 0 { bail!( "guest_features {:#x} carries unsupported SEV_FEATURES bits {unexpected_features:#x}; \ - dstack launches amd sev-snp guests with SNPActive only ({ALLOWED_GUEST_FEATURES:#x})", + dstack launches amd sev-snp guests with at most {ALLOWED_GUEST_FEATURES:#x}", input.guest_features ); } @@ -1657,7 +1657,7 @@ mod tests { let cases: [(&str, u64); 4] = [ ("RestrictedInjection", SNP_ACTIVE | (1 << 3)), ("DebugSwap", SNP_ACTIVE | (1 << 5)), - ("SecureTSC", SNP_ACTIVE | (1 << 9)), + ("VmsaRegProt", SNP_ACTIVE | (1 << 14)), ("reserved bit 63", SNP_ACTIVE | (1 << 63)), ]; for (name, features) in cases { @@ -1683,10 +1683,12 @@ mod tests { "unexpected error: {err}" ); - // The one value dstack's launch path produces stays accepted. - let mut input = valid_input(); - input.guest_features = SNP_ACTIVE; - validate_measurement_input(&input).expect("SNPActive-only launch is accepted"); + // The values dstack's launch path produces stay accepted. + for features in [SNP_ACTIVE, SNP_ACTIVE | SEV_FEATURE_SECURE_TSC] { + let mut input = valid_input(); + input.guest_features = features; + validate_measurement_input(&input).expect("dstack launch features are accepted"); + } } #[test] diff --git a/dstack/vmm/src/app.rs b/dstack/vmm/src/app.rs index df023d778f..0e2e8d5346 100644 --- a/dstack/vmm/src/app.rs +++ b/dstack/vmm/src/app.rs @@ -2447,7 +2447,11 @@ fn make_vm_config( measurement: image_measurement.measurement.clone(), vcpus: effective_vcpus, vcpu_type: Some("EPYC-v4".to_string()), - guest_features: 1, + guest_features: if cfg.cvm.snp_secure_tsc { + 1 | dstack_mr::sev::SEV_FEATURE_SECURE_TSC + } else { + 1 + }, }; config["sev_snp_measurement"] = serde_json::Value::String( serde_json::to_string(&measurement) diff --git a/dstack/vmm/src/app/qemu.rs b/dstack/vmm/src/app/qemu.rs index 58403bd121..9161ab7972 100644 --- a/dstack/vmm/src/app/qemu.rs +++ b/dstack/vmm/src/app/qemu.rs @@ -1164,8 +1164,10 @@ impl VmConfig { .arg("-object") .arg(amd_sev_snp_memory_backend_arg(mem)); command.arg("-object").arg(format!( - "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data={host_data},cbitpos={},reduced-phys-bits={}", - snp_params.cbitpos, snp_params.reduced_phys_bits + "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data={host_data},cbitpos={},reduced-phys-bits={}{}", + snp_params.cbitpos, + snp_params.reduced_phys_bits, + if cfg.snp_secure_tsc { ",secure-tsc=on" } else { "" } )); command.arg("-machine").arg( "q35,kernel-irqchip=split,confidential-guest-support=sev0,memory-backend=ram1,hpet=off", diff --git a/dstack/vmm/src/config.rs b/dstack/vmm/src/config.rs index 4f76e742a5..e8fa274db0 100644 --- a/dstack/vmm/src/config.rs +++ b/dstack/vmm/src/config.rs @@ -543,6 +543,12 @@ pub struct CvmConfig { /// Default is None (disabled), common value is 4050. pub qgs_port: Option, + /// Launch AMD SEV-SNP guests with Secure TSC, so the host cannot skew the + /// guest clock. Needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with + /// the `snp_secure_tsc` flag. + #[serde(default)] + pub snp_secure_tsc: bool, + /// SMBIOS product information for cloud environment detection #[serde(default)] pub product: ProductConfig, diff --git a/dstack/vmm/vmm.toml b/dstack/vmm/vmm.toml index da6ae15e7e..7cd492c77b 100644 --- a/dstack/vmm/vmm.toml +++ b/dstack/vmm/vmm.toml @@ -139,6 +139,10 @@ disk_prealloc = "off" # The guest kernel will use this vsock port to communicate with the host QGS. # Default is None (disabled), common value is 4050. qgs_port = 4050 +# Launch AMD SEV-SNP guests with Secure TSC, so the host cannot skew the guest +# clock. Needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with the +# snp_secure_tsc flag. +snp_secure_tsc = false # SMBIOS product information for cloud environment detection. # Field names correspond to /sys/class/dmi/id/ entries in guest. From 6c92b71d6f9a53ad41b543afd44133ca342b5f10 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 24 Nov 2026 03:07:43 -0800 Subject: [PATCH 2/8] feat(dstack-util): refuse secure_time when the host controls the clock secure_time waits for an NTS sync, but between syncs the clock follows the TSC. Before waiting, require the tsc clocksource, require Secure TSC on AMD SEV-SNP, and reject AWS. The guest kernel has no userspace interface for the Secure TSC status, so add a kernel patch exposing it as /sys/devices/system/cpu/sev/secure_tsc. Signed-off-by: Kevin Wang --- CHANGELOG.md | 7 ++++ docs/design-and-hardening-decisions.md | 4 +-- dstack/dstack-util/src/system_setup.rs | 29 ++++++++++++++++ os/mkosi/components/kernel/kernel-build.sh | 3 +- os/mkosi/components/kernel/kernel.sh | 3 +- ...ev-expose-secure-tsc-status-in-sysfs.patch | 34 +++++++++++++++++++ 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce184b2ee..c01eaa245c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Upgrade notes + +- **`secure_time` refuses a clock the host controls.** An app with + `secure_time` enabled no longer boots on AWS, nor on an AMD SEV-SNP host + that does not launch guests with Secure TSC (`snp_secure_tsc = true` in + `vmm.toml`), nor when the clocksource is not `tsc`. + ### Added - **Secure TSC for AMD SEV-SNP guests.** `snp_secure_tsc = true` under `[cvm]` diff --git a/docs/design-and-hardening-decisions.md b/docs/design-and-hardening-decisions.md index 5bd2222ed2..b13ba7e0cb 100644 --- a/docs/design-and-hardening-decisions.md +++ b/docs/design-and-hardening-decisions.md @@ -57,9 +57,9 @@ See [here](https://intel.github.io/ccc-linux-guest-hardening-docs/security-spec. **Implementation**: dstack OS enforces the guest kernel uses TSC as the only timer source by appending `tsc=reliable no-kvmclock` to the kernel cmdline. It also enforces the use of NTS with built-in [trusted servers](../os/yocto/layers/meta-dstack/recipes-core/chrony/files/chrony.conf) to synchronize system time. -On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. Set `snp_secure_tsc = true` under `[cvm]` in `vmm.toml` to enable it (QEMU >= 11.1, host kernel >= 6.18, a CPU with the `snp_secure_tsc` flag). The launch measurement covers the setting, but nothing requires it yet, so a host may still launch without it. +On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. Set `snp_secure_tsc = true` under `[cvm]` in `vmm.toml` to enable it (QEMU >= 11.1, host kernel >= 6.18, a CPU with the `snp_secure_tsc` flag). -**Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. If `secure_time` is disabled, time synchronization is not enforced before application launch. +**Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. It first checks that the host cannot move the clock between syncs and refuses to boot otherwise: the clocksource must be `tsc`, an AMD SEV-SNP guest must run with Secure TSC, and AWS is not supported. If `secure_time` is disabled, time synchronization is not enforced before application launch. **Rationale**: Time synchronization is provided as an optional feature because the process typically requires tens of seconds to complete. Applications can function without the `secure_time` option enabled and may implement their own time synchronization mechanisms if required. diff --git a/dstack/dstack-util/src/system_setup.rs b/dstack/dstack-util/src/system_setup.rs index 51ba3436a7..934e77b937 100644 --- a/dstack/dstack-util/src/system_setup.rs +++ b/dstack/dstack-util/src/system_setup.rs @@ -1377,6 +1377,34 @@ fn verify_health_check_requirement(app_compose: &AppCompose) -> Result<()> { Ok(()) } +/// NTS fixes the absolute time; only a TSC the host cannot move keeps it +/// between syncs. +fn verify_secure_clock(platform: TeeVariant) -> Result<()> { + match platform { + TeeVariant::DstackTdx | TeeVariant::DstackGcpTdx => {} + TeeVariant::DstackAmdSevSnp => { + let secure_tsc = fs::read_to_string("/sys/devices/system/cpu/sev/secure_tsc") + .context("failed to read amd sev-snp Secure TSC status")?; + if secure_tsc.trim() != "1" { + bail!("secure_time requires amd sev-snp Secure TSC, which the host did not enable"); + } + } + TeeVariant::DstackAwsNitroTpm | TeeVariant::DstackNitroEnclave => { + bail!("secure_time is not supported on AWS") + } + } + let clocksource = + fs::read_to_string("/sys/devices/system/clocksource/clocksource0/current_clocksource") + .context("failed to read the current clocksource")?; + if clocksource.trim() != "tsc" { + bail!( + "secure_time requires the tsc clocksource, found {}", + clocksource.trim() + ); + } + Ok(()) +} + fn verify_platform_requirements( app_compose: &AppCompose, current_platform: TeeVariant, @@ -1515,6 +1543,7 @@ pub async fn cmd_sys_setup(args: SetupArgs) -> Result<()> { async fn do_sys_setup(stage0: Stage0<'_>) -> Result<()> { verify_app_compose_policy(&stage0.shared).context("Failed to verify app-compose policy")?; if stage0.shared.app_compose.secure_time { + verify_secure_clock(detect_tee_variant()?)?; info!("Waiting for the system time to be synchronized"); cmd! { chronyc waitsync 30 0.1 0 5; diff --git a/os/mkosi/components/kernel/kernel-build.sh b/os/mkosi/components/kernel/kernel-build.sh index d21c16442c..bda294fbd0 100755 --- a/os/mkosi/components/kernel/kernel-build.sh +++ b/os/mkosi/components/kernel/kernel-build.sh @@ -67,7 +67,8 @@ for patch in \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0013-ovl-handle-idmapped-mounts-in-setattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0014-ovl-handle-idmapped-mounts-in-getattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0015-ovl-handle-idmapped-mounts-in-set-acl.patch" \ - "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch"; do + "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch" \ + "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch"; do patch -d "$src" -p1 --fuzz=0 < "$patch" done diff --git a/os/mkosi/components/kernel/kernel.sh b/os/mkosi/components/kernel/kernel.sh index 98ebe592c8..e9fc414a2c 100644 --- a/os/mkosi/components/kernel/kernel.sh +++ b/os/mkosi/components/kernel/kernel.sh @@ -23,7 +23,8 @@ component_cache_key() { "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0013-ovl-handle-idmapped-mounts-in-setattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0014-ovl-handle-idmapped-mounts-in-getattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0015-ovl-handle-idmapped-mounts-in-set-acl.patch" \ - "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch" + "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch" \ + "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch" key_tools gcc ld make pahole key_packages binutils dwarves bc bison flex libssl-dev libelf-dev } diff --git a/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch b/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch new file mode 100644 index 0000000000..c9da6d8940 --- /dev/null +++ b/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch @@ -0,0 +1,34 @@ +From: Kevin Wang +Subject: [PATCH] x86/sev: expose Secure TSC status in sysfs + +Userspace has no way to tell whether an SEV-SNP guest runs with Secure +TSC: CPUID only says the CPU supports it, and the SEV_STATUS MSR needs +the msr driver. Add /sys/devices/system/cpu/sev/secure_tsc next to +vmpl, so dstack can refuse secure_time when the hypervisor still +controls the guest TSC. + +Upstream-Status: Inappropriate [dstack specific] +Signed-off-by: Kevin Wang +--- +--- a/arch/x86/coco/sev/core.c ++++ b/arch/x86/coco/sev/core.c +@@ -1431,10 +1431,19 @@ static ssize_t vmpl_show(struct kobject + return sysfs_emit(buf, "%d\n", snp_vmpl); + } + ++static ssize_t secure_tsc_show(struct kobject *kobj, ++ struct kobj_attribute *attr, char *buf) ++{ ++ return sysfs_emit(buf, "%d\n", ++ cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)); ++} ++ + static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl); ++static struct kobj_attribute secure_tsc_attr = __ATTR_RO(secure_tsc); + + static struct attribute *vmpl_attrs[] = { + &vmpl_attr.attr, ++ &secure_tsc_attr.attr, + NULL + }; + From eef89adc96a7042de86b3163763549b11496692f Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 24 Nov 2026 03:22:32 -0800 Subject: [PATCH 3/8] refactor(vmm): derive Secure TSC from the compose secure_time Drop cvm.snp_secure_tsc. A host option could break every SNP guest on a QEMU without secure-tsc, including apps that never asked for a secure clock. Only an app whose compose sets secure_time now gets secure-tsc=on, so every other app launches exactly as before, with the same launch digest. Signed-off-by: Kevin Wang --- CHANGELOG.md | 14 ++-- docs/design-and-hardening-decisions.md | 2 +- dstack/dstack-mr/src/sev.rs | 2 +- dstack/vmm/src/app.rs | 103 +++++++++++++++++++++---- dstack/vmm/src/app/qemu.rs | 15 +++- dstack/vmm/src/config.rs | 6 -- dstack/vmm/src/one_shot.rs | 1 + dstack/vmm/vmm.toml | 4 - 8 files changed, 112 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c01eaa245c..f05df07247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,16 +11,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`secure_time` refuses a clock the host controls.** An app with `secure_time` enabled no longer boots on AWS, nor on an AMD SEV-SNP host - that does not launch guests with Secure TSC (`snp_secure_tsc = true` in - `vmm.toml`), nor when the clocksource is not `tsc`. + that cannot launch guests with Secure TSC (QEMU >= 11.1, host kernel >= 6.18, + a CPU with the `snp_secure_tsc` flag), nor when the clocksource is not `tsc`. + Such a guest also needs a KMS and verifier of this release. Apps without + `secure_time` launch as before. ### Added -- **Secure TSC for AMD SEV-SNP guests.** `snp_secure_tsc = true` under `[cvm]` - in `vmm.toml` launches SNP guests with Secure TSC, so the host cannot skew - the guest clock. Off by default: it needs QEMU >= 11.1, host kernel >= 6.18 - and a CPU with the `snp_secure_tsc` flag. A KMS or verifier older than this - release rejects such a guest. +- **Secure TSC for AMD SEV-SNP guests.** The VMM launches an SNP guest with + Secure TSC when its app compose sets `secure_time`, so the host cannot skew + the guest clock. ### Changed diff --git a/docs/design-and-hardening-decisions.md b/docs/design-and-hardening-decisions.md index b13ba7e0cb..117354735b 100644 --- a/docs/design-and-hardening-decisions.md +++ b/docs/design-and-hardening-decisions.md @@ -57,7 +57,7 @@ See [here](https://intel.github.io/ccc-linux-guest-hardening-docs/security-spec. **Implementation**: dstack OS enforces the guest kernel uses TSC as the only timer source by appending `tsc=reliable no-kvmclock` to the kernel cmdline. It also enforces the use of NTS with built-in [trusted servers](../os/yocto/layers/meta-dstack/recipes-core/chrony/files/chrony.conf) to synchronize system time. -On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. Set `snp_secure_tsc = true` under `[cvm]` in `vmm.toml` to enable it (QEMU >= 11.1, host kernel >= 6.18, a CPU with the `snp_secure_tsc` flag). +On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. The VMM enables it for an app whose app-compose.json sets `secure_time`, which needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with the `snp_secure_tsc` flag. **Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. It first checks that the host cannot move the clock between syncs and refuses to boot otherwise: the clocksource must be `tsc`, an AMD SEV-SNP guest must run with Secure TSC, and AWS is not supported. If `secure_time` is disabled, time synchronization is not enforced before application launch. diff --git a/dstack/dstack-mr/src/sev.rs b/dstack/dstack-mr/src/sev.rs index 007a49c0b7..e63de6703f 100644 --- a/dstack/dstack-mr/src/sev.rs +++ b/dstack/dstack-mr/src/sev.rs @@ -60,7 +60,7 @@ pub const SEV_FEATURE_SECURE_TSC: u64 = 1 << 9; /// /// dstack has exactly one launch path, `sev-snp-guest,id=sev0,policy=0x30000,...` /// in `dstack/vmm/src/app/qemu.rs`. It starts the guest with `SNPActive`, plus -/// `SecureTSC` when the host sets `cvm.snp_secure_tsc`, and the VMM writes the +/// `SecureTSC` when the app compose sets `secure_time`, and the VMM writes the /// matching `guest_features` into the measurement document /// (`dstack/vmm/src/app.rs`). Those are the only bits accepted; accepting bits /// nothing emits would only ever admit a guest dstack did not configure. diff --git a/dstack/vmm/src/app.rs b/dstack/vmm/src/app.rs index 0e2e8d5346..e3cd03e77d 100644 --- a/dstack/vmm/src/app.rs +++ b/dstack/vmm/src/app.rs @@ -1789,6 +1789,7 @@ impl App { &hex::encode(compose_hash), mr_config, app_compose.requirements.as_ref(), + app_compose.secure_time, )?; fs::write(shared_dir.join(SYS_CONFIG), &sys_config_str) .context("Failed to write vm config")?; @@ -2147,6 +2148,7 @@ pub(crate) fn make_sys_config( compose_hash: &str, mr_config: Option, requirements: Option<&dstack_types::Requirements>, + secure_time: bool, ) -> Result { let image_path = cfg.image.path.join(&manifest.image); let image = Image::load(image_path).context("Failed to load image info")?; @@ -2188,6 +2190,7 @@ pub(crate) fn make_sys_config( compose_hash, mr_config.clone(), requirements, + secure_time, )?; let mut sys_config = json!({ "kms_urls": kms_urls, @@ -2275,6 +2278,7 @@ fn make_vm_config( _compose_hash: &str, mr_config: Option, requirements: Option<&dstack_types::Requirements>, + secure_time: bool, ) -> Result { let platform = cfg.cvm.resolved_platform(); let is_amd_sev_snp = platform == crate::config::CvmPlatform::AmdSevSnp && !manifest.no_tee; @@ -2447,7 +2451,7 @@ fn make_vm_config( measurement: image_measurement.measurement.clone(), vcpus: effective_vcpus, vcpu_type: Some("EPYC-v4".to_string()), - guest_features: if cfg.cvm.snp_secure_tsc { + guest_features: if secure_time { 1 | dstack_mr::sev::SEV_FEATURE_SECURE_TSC } else { 1 @@ -4266,10 +4270,24 @@ mod tests { let image = test_tdx_image(true); let compose_hash = hex_of(0x22, 32); - let bridge_config = - make_vm_config(&config, &bridge_manifest, &image, &compose_hash, None, None)?; - let user_config = - make_vm_config(&config, &user_manifest, &image, &compose_hash, None, None)?; + let bridge_config = make_vm_config( + &config, + &bridge_manifest, + &image, + &compose_hash, + None, + None, + false, + )?; + let user_config = make_vm_config( + &config, + &user_manifest, + &image, + &compose_hash, + None, + None, + false, + )?; assert_eq!(bridge_config, user_config); Ok(()) @@ -4294,6 +4312,7 @@ mod tests { &hex_of(0x22, 32), None, None, + false, )?; assert_eq!(vm_config["num_verity_volumes"], 2); @@ -4313,6 +4332,7 @@ mod tests { &hex_of(0x22, 32), None, None, + false, )?; assert_eq!(vm_config["swtpm"], true); @@ -4327,20 +4347,43 @@ mod tests { manifest.simulated_tee = Some(dstack_types::TeeVariant::DstackTdx); for platform in [CvmPlatform::Tdx, CvmPlatform::AmdSevSnp] { config.cvm.platform = Some(platform); - let vm_config = - make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &test_tdx_image(true), + "", + None, + None, + false, + )?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); } config.cvm.platform = Some(CvmPlatform::Tdx); manifest.swtpm = true; - let vm_config = make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &test_tdx_image(true), + "", + None, + None, + false, + )?; assert!(vm_config.get("tdx_measurement").is_none()); manifest.swtpm = false; manifest.simulated_tee = None; - let vm_config = make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &test_tdx_image(true), + "", + None, + None, + false, + )?; assert!(vm_config.get("tdx_measurement").is_none()); Ok(()) } @@ -4353,7 +4396,15 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(1024); let image = test_tdx_image(true); - let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &image, + &hex_of(0x22, 32), + None, + None, + false, + )?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); @@ -4371,7 +4422,15 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(2048); let image = test_tdx_image(true); - let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &image, + &hex_of(0x22, 32), + None, + None, + false, + )?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); @@ -4389,7 +4448,15 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(3072); let image = test_tdx_image(false); - let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; + let vm_config = make_vm_config( + &config, + &manifest, + &image, + &hex_of(0x22, 32), + None, + None, + false, + )?; assert!(vm_config.get("tdx_attestation_variant").is_none()); assert!(vm_config.get("tdx_measurement").is_none()); @@ -4419,6 +4486,7 @@ mod tests { &hex_of(0x22, 32), None, Some(&requirements), + false, )?; assert!(vm_config.get("tdx_attestation_variant").is_none()); @@ -4445,6 +4513,7 @@ mod tests { &hex_of(0x22, 32), None, Some(&requirements), + false, )?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); @@ -4545,8 +4614,14 @@ mod tests { let build_hash = Sha256::digest(sha256sum.as_bytes()).to_vec(); fs::write(image_dir.join("digest.txt"), hex::encode(&build_hash))?; - let sys_config_document = - make_sys_config(&config, &manifest, &compose_hash, Some(mr_config), None)?; + let sys_config_document = make_sys_config( + &config, + &manifest, + &compose_hash, + Some(mr_config), + None, + false, + )?; let sys_config: serde_json::Value = serde_json::from_str(&sys_config_document)?; assert!(sys_config.get("tee_simulator").is_none()); // A host must never nominate the trust anchor that authenticates its diff --git a/dstack/vmm/src/app/qemu.rs b/dstack/vmm/src/app/qemu.rs index 9161ab7972..a9e1280430 100644 --- a/dstack/vmm/src/app/qemu.rs +++ b/dstack/vmm/src/app/qemu.rs @@ -265,6 +265,7 @@ struct PreparedQemuLaunch { tdx_mr_config_id: Option, snp_host_data: Option, snp_launch_params: Option, + snp_secure_tsc: bool, } impl PreparedQemuLaunch { @@ -367,6 +368,7 @@ impl PreparedQemuLaunch { tdx_mr_config_id, snp_host_data, snp_launch_params, + snp_secure_tsc: app_compose.secure_time, }) } } @@ -1100,7 +1102,14 @@ impl VmConfig { let launch_params = prepared.snp_launch_params.context( "snp launch parameters should be detected during launch preparation", )?; - self.configure_amd_sev_snp_guest(command, cfg, mem, host_data, launch_params); + self.configure_amd_sev_snp_guest( + command, + cfg, + mem, + host_data, + launch_params, + prepared.snp_secure_tsc, + ); } } Ok(()) @@ -1159,6 +1168,7 @@ impl VmConfig { mem: u32, host_data: &str, snp_params: AmdSevSnpLaunchParams, + secure_tsc: bool, ) { command .arg("-object") @@ -1167,7 +1177,7 @@ impl VmConfig { "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data={host_data},cbitpos={},reduced-phys-bits={}{}", snp_params.cbitpos, snp_params.reduced_phys_bits, - if cfg.snp_secure_tsc { ",secure-tsc=on" } else { "" } + if secure_tsc { ",secure-tsc=on" } else { "" } )); command.arg("-machine").arg( "q35,kernel-irqchip=split,confidential-guest-support=sev0,memory-backend=ram1,hpet=off", @@ -1538,6 +1548,7 @@ mod tests { tdx_mr_config_id: None, snp_host_data: None, snp_launch_params: None, + snp_secure_tsc: false, }; (config, vm, prepared) } diff --git a/dstack/vmm/src/config.rs b/dstack/vmm/src/config.rs index e8fa274db0..4f76e742a5 100644 --- a/dstack/vmm/src/config.rs +++ b/dstack/vmm/src/config.rs @@ -543,12 +543,6 @@ pub struct CvmConfig { /// Default is None (disabled), common value is 4050. pub qgs_port: Option, - /// Launch AMD SEV-SNP guests with Secure TSC, so the host cannot skew the - /// guest clock. Needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with - /// the `snp_secure_tsc` flag. - #[serde(default)] - pub snp_secure_tsc: bool, - /// SMBIOS product information for cloud environment detection #[serde(default)] pub product: ProductConfig, diff --git a/dstack/vmm/src/one_shot.rs b/dstack/vmm/src/one_shot.rs index 6eafeeab63..a825b523ba 100644 --- a/dstack/vmm/src/one_shot.rs +++ b/dstack/vmm/src/one_shot.rs @@ -250,6 +250,7 @@ Compose file content (first 200 chars): &compose_hash, mr_config, app_compose.requirements.as_ref(), + app_compose.secure_time, )?; let sys_config_path = vm_work_dir.shared_dir().join(".sys-config.json"); fs_err::write(&sys_config_path, &sys_config_str).context("Failed to write sys config")?; diff --git a/dstack/vmm/vmm.toml b/dstack/vmm/vmm.toml index 7cd492c77b..da6ae15e7e 100644 --- a/dstack/vmm/vmm.toml +++ b/dstack/vmm/vmm.toml @@ -139,10 +139,6 @@ disk_prealloc = "off" # The guest kernel will use this vsock port to communicate with the host QGS. # Default is None (disabled), common value is 4050. qgs_port = 4050 -# Launch AMD SEV-SNP guests with Secure TSC, so the host cannot skew the guest -# clock. Needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with the -# snp_secure_tsc flag. -snp_secure_tsc = false # SMBIOS product information for cloud environment detection. # Field names correspond to /sys/class/dmi/id/ entries in guest. From 0c6923bbe61b257fdfad7efa21a95ee1a6d07094 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 24 Nov 2026 03:49:43 -0800 Subject: [PATCH 4/8] refactor(vmm): share the SNP launch feature mapping and test Secure TSC Move the secure_tsc -> SEV_FEATURES mapping into dstack-mr, build the sev-snp-guest object in one function, and cover the secure_time launch on both sides: guest_features in the measurement document and secure-tsc=on on the QEMU object. Name secure_time when platform detection fails. Signed-off-by: Kevin Wang --- dstack/dstack-mr/src/sev.rs | 15 ++++++++-- dstack/dstack-util/src/system_setup.rs | 3 +- dstack/vmm/src/app.rs | 15 ++++++---- dstack/vmm/src/app/qemu.rs | 41 ++++++++++++++++++++------ 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/dstack/dstack-mr/src/sev.rs b/dstack/dstack-mr/src/sev.rs index e63de6703f..1e023b7466 100644 --- a/dstack/dstack-mr/src/sev.rs +++ b/dstack/dstack-mr/src/sev.rs @@ -41,7 +41,16 @@ const VMSA_GPA: u64 = 0x0000_FFFF_FFFF_F000; const SEV_FEATURE_SNP_ACTIVE: u64 = 1 << 0; /// `SecureTSC`, bit 9: the PSP fixes the guest TSC scale and offset, so the /// hypervisor can no longer intercept or skew the guest's TSC. -pub const SEV_FEATURE_SECURE_TSC: u64 = 1 << 9; +const SEV_FEATURE_SECURE_TSC: u64 = 1 << 9; + +/// `SEV_FEATURES` dstack launches an amd sev-snp guest with. +pub fn launch_guest_features(secure_tsc: bool) -> u64 { + if secure_tsc { + SEV_FEATURE_SNP_ACTIVE | SEV_FEATURE_SECURE_TSC + } else { + SEV_FEATURE_SNP_ACTIVE + } +} /// `SEV_FEATURES` bits a dstack launch may carry. /// @@ -1684,9 +1693,9 @@ mod tests { ); // The values dstack's launch path produces stay accepted. - for features in [SNP_ACTIVE, SNP_ACTIVE | SEV_FEATURE_SECURE_TSC] { + for secure_tsc in [false, true] { let mut input = valid_input(); - input.guest_features = features; + input.guest_features = launch_guest_features(secure_tsc); validate_measurement_input(&input).expect("dstack launch features are accepted"); } } diff --git a/dstack/dstack-util/src/system_setup.rs b/dstack/dstack-util/src/system_setup.rs index 934e77b937..8ce704313d 100644 --- a/dstack/dstack-util/src/system_setup.rs +++ b/dstack/dstack-util/src/system_setup.rs @@ -1543,7 +1543,8 @@ pub async fn cmd_sys_setup(args: SetupArgs) -> Result<()> { async fn do_sys_setup(stage0: Stage0<'_>) -> Result<()> { verify_app_compose_policy(&stage0.shared).context("Failed to verify app-compose policy")?; if stage0.shared.app_compose.secure_time { - verify_secure_clock(detect_tee_variant()?)?; + let platform = detect_tee_variant().context("secure_time requires a TEE platform")?; + verify_secure_clock(platform)?; info!("Waiting for the system time to be synchronized"); cmd! { chronyc waitsync 30 0.1 0 5; diff --git a/dstack/vmm/src/app.rs b/dstack/vmm/src/app.rs index e3cd03e77d..b9bf9eb1dc 100644 --- a/dstack/vmm/src/app.rs +++ b/dstack/vmm/src/app.rs @@ -2451,11 +2451,7 @@ fn make_vm_config( measurement: image_measurement.measurement.clone(), vcpus: effective_vcpus, vcpu_type: Some("EPYC-v4".to_string()), - guest_features: if secure_time { - 1 | dstack_mr::sev::SEV_FEATURE_SECURE_TSC - } else { - 1 - }, + guest_features: dstack_mr::sev::launch_guest_features(secure_time), }; config["sev_snp_measurement"] = serde_json::Value::String( serde_json::to_string(&measurement) @@ -4614,6 +4610,15 @@ mod tests { let build_hash = Sha256::digest(sha256sum.as_bytes()).to_vec(); fs::write(image_dir.join("digest.txt"), hex::encode(&build_hash))?; + let image = Image::load(&image_dir)?; + let secure_time_vm_config = + make_vm_config(&config, &manifest, &image, &compose_hash, None, None, true)?; + let secure_time_measurement: dstack_mr::sev::SnpMeasurementDocument = serde_json::from_str( + secure_time_vm_config["sev_snp_measurement"] + .as_str() + .context("sev_snp_measurement must be a string")?, + )?; + assert_eq!(secure_time_measurement.guest_features, 0x201); let sys_config_document = make_sys_config( &config, &manifest, diff --git a/dstack/vmm/src/app/qemu.rs b/dstack/vmm/src/app/qemu.rs index a9e1280430..c620e1a70b 100644 --- a/dstack/vmm/src/app/qemu.rs +++ b/dstack/vmm/src/app/qemu.rs @@ -45,6 +45,19 @@ struct AmdSevSnpLaunchParams { reduced_phys_bits: u32, } +fn amd_sev_snp_guest_arg( + host_data: &str, + params: AmdSevSnpLaunchParams, + secure_tsc: bool, +) -> String { + format!( + "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data={host_data},cbitpos={},reduced-phys-bits={}{}", + params.cbitpos, + params.reduced_phys_bits, + if secure_tsc { ",secure-tsc=on" } else { "" } + ) +} + fn parse_amd_sev_snp_qmp_capabilities(stdout: &[u8]) -> Result { let stdout = std::str::from_utf8(stdout).context("QMP output is not valid UTF-8")?; let mut qmp_error = None; @@ -1173,12 +1186,9 @@ impl VmConfig { command .arg("-object") .arg(amd_sev_snp_memory_backend_arg(mem)); - command.arg("-object").arg(format!( - "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data={host_data},cbitpos={},reduced-phys-bits={}{}", - snp_params.cbitpos, - snp_params.reduced_phys_bits, - if secure_tsc { ",secure-tsc=on" } else { "" } - )); + command + .arg("-object") + .arg(amd_sev_snp_guest_arg(host_data, snp_params, secure_tsc)); command.arg("-machine").arg( "q35,kernel-irqchip=split,confidential-guest-support=sev0,memory-backend=ram1,hpet=off", ); @@ -1264,10 +1274,10 @@ mod tests { use crate::vm_launcher::SidecarChannel; use super::{ - amd_sev_snp_memory_backend_arg, create_hd, fs, nic_fd_layout, + amd_sev_snp_guest_arg, amd_sev_snp_memory_backend_arg, create_hd, fs, nic_fd_layout, parse_amd_sev_snp_qmp_capabilities, prepare_data_disk, qemu_img_create_args, - virtio_pci_device, Command, Path, PreparedQemuLaunch, PreparedVolume, QemuCommandBuilder, - VmConfig, + virtio_pci_device, AmdSevSnpLaunchParams, Command, Path, PreparedQemuLaunch, + PreparedVolume, QemuCommandBuilder, VmConfig, }; use crate::app::image::{Image, ImageInfo}; use crate::app::{needs_swtpm, GpuConfig, GpuSpec, Manifest, PortMapping, VmVolume, VmWorkDir}; @@ -1301,6 +1311,19 @@ mod tests { ); } + #[test] + fn amd_sev_snp_guest_arg_enables_secure_tsc_on_request() { + let params = AmdSevSnpLaunchParams { + cbitpos: 51, + reduced_phys_bits: 1, + }; + assert_eq!( + amd_sev_snp_guest_arg("aa", params, false), + "sev-snp-guest,id=sev0,policy=0x30000,sev-device=/dev/sev,kernel-hashes=on,host-data=aa,cbitpos=51,reduced-phys-bits=1" + ); + assert!(amd_sev_snp_guest_arg("aa", params, true).ends_with(",secure-tsc=on")); + } + #[test] fn amd_sev_snp_qmp_capabilities_extracts_launch_params() { let stdout = br#"{"QMP":{"version":{"qemu":{"major":10,"minor":0,"micro":2}}}} From 2f78450fe65322a940d190ab316dfd4ad4ac9b21 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 24 Nov 2026 04:38:03 -0800 Subject: [PATCH 5/8] docs: Secure TSC needs QEMU 11.2 QEMU documents secure-tsc as "since 11.1", but v11.1.3 does not have the property; v11.2.0-rc1 does. /proc/cpuinfo has no snp_secure_tsc flag either: the kernel does not list that feature there. Signed-off-by: Kevin Wang --- CHANGELOG.md | 4 ++-- docs/design-and-hardening-decisions.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f05df07247..0c0d3067b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`secure_time` refuses a clock the host controls.** An app with `secure_time` enabled no longer boots on AWS, nor on an AMD SEV-SNP host - that cannot launch guests with Secure TSC (QEMU >= 11.1, host kernel >= 6.18, - a CPU with the `snp_secure_tsc` flag), nor when the clocksource is not `tsc`. + that cannot launch guests with Secure TSC (QEMU >= 11.2, host kernel >= 6.18, + a CPU that supports it), nor when the clocksource is not `tsc`. Such a guest also needs a KMS and verifier of this release. Apps without `secure_time` launch as before. diff --git a/docs/design-and-hardening-decisions.md b/docs/design-and-hardening-decisions.md index 117354735b..9276fdec53 100644 --- a/docs/design-and-hardening-decisions.md +++ b/docs/design-and-hardening-decisions.md @@ -57,7 +57,7 @@ See [here](https://intel.github.io/ccc-linux-guest-hardening-docs/security-spec. **Implementation**: dstack OS enforces the guest kernel uses TSC as the only timer source by appending `tsc=reliable no-kvmclock` to the kernel cmdline. It also enforces the use of NTS with built-in [trusted servers](../os/yocto/layers/meta-dstack/recipes-core/chrony/files/chrony.conf) to synchronize system time. -On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. The VMM enables it for an app whose app-compose.json sets `secure_time`, which needs QEMU >= 11.1, host kernel >= 6.18 and a CPU with the `snp_secure_tsc` flag. +On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. The VMM enables it for an app whose app-compose.json sets `secure_time`, which needs QEMU >= 11.2, host kernel >= 6.18 and a CPU that supports Secure TSC. **Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. It first checks that the host cannot move the clock between syncs and refuses to boot otherwise: the clocksource must be `tsc`, an AMD SEV-SNP guest must run with Secure TSC, and AWS is not supported. If `secure_time` is disabled, time synchronization is not enforced before application launch. From b16524e5484f1cc70835fbb092beceba868b65a0 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 25 Nov 2026 21:33:28 -0800 Subject: [PATCH 6/8] build(mkosi): keep the Secure TSC kernel patch out of yocto The yocto backend is going away, so the new patch lives with the mkosi kernel component instead. Signed-off-by: Kevin Wang --- os/mkosi/components/kernel/kernel-build.sh | 2 +- os/mkosi/components/kernel/kernel.sh | 2 +- .../0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename os/{yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch => mkosi/components/kernel/patches/0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch} (100%) diff --git a/os/mkosi/components/kernel/kernel-build.sh b/os/mkosi/components/kernel/kernel-build.sh index bda294fbd0..a32263b3d4 100755 --- a/os/mkosi/components/kernel/kernel-build.sh +++ b/os/mkosi/components/kernel/kernel-build.sh @@ -68,7 +68,7 @@ for patch in \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0014-ovl-handle-idmapped-mounts-in-getattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0015-ovl-handle-idmapped-mounts-in-set-acl.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch" \ - "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch"; do + "$MKOSI_DIR/components/kernel/patches/0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch"; do patch -d "$src" -p1 --fuzz=0 < "$patch" done diff --git a/os/mkosi/components/kernel/kernel.sh b/os/mkosi/components/kernel/kernel.sh index e9fc414a2c..a5e8de77ec 100644 --- a/os/mkosi/components/kernel/kernel.sh +++ b/os/mkosi/components/kernel/kernel.sh @@ -24,7 +24,7 @@ component_cache_key() { "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0014-ovl-handle-idmapped-mounts-in-getattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0015-ovl-handle-idmapped-mounts-in-set-acl.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch" \ - "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch" + "$COMPONENT_PATH/patches/0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch" key_tools gcc ld make pahole key_packages binutils dwarves bc bison flex libssl-dev libelf-dev } diff --git a/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch b/os/mkosi/components/kernel/patches/0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch similarity index 100% rename from os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0017-x86-sev-expose-secure-tsc-status-in-sysfs.patch rename to os/mkosi/components/kernel/patches/0001-x86-sev-expose-secure-tsc-status-in-sysfs.patch From ea065c4b1bee69232b9df9a1e595c294b0691826 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 25 Nov 2026 21:41:05 -0800 Subject: [PATCH 7/8] refactor(vmm): pass the app compose to make_vm_config requirements and secure_time both come from the app compose, so take the compose itself instead of one argument per field. Signed-off-by: Kevin Wang --- dstack/vmm/src/app.rs | 156 ++++++++++++------------------------- dstack/vmm/src/one_shot.rs | 3 +- 2 files changed, 52 insertions(+), 107 deletions(-) diff --git a/dstack/vmm/src/app.rs b/dstack/vmm/src/app.rs index b9bf9eb1dc..3e3dd0046e 100644 --- a/dstack/vmm/src/app.rs +++ b/dstack/vmm/src/app.rs @@ -20,6 +20,7 @@ use dstack_types::shared_filenames::{ USER_CONFIG, }; use dstack_types::version::Version; +use dstack_types::AppCompose; use dstack_vmm_rpc::{ self as pb, GpuInfo, ReloadVmsResponse, StatusRequest, StatusResponse, VmConfiguration, }; @@ -1788,8 +1789,7 @@ impl App { &manifest, &hex::encode(compose_hash), mr_config, - app_compose.requirements.as_ref(), - app_compose.secure_time, + Some(&app_compose), )?; fs::write(shared_dir.join(SYS_CONFIG), &sys_config_str) .context("Failed to write vm config")?; @@ -2147,8 +2147,7 @@ pub(crate) fn make_sys_config( manifest: &Manifest, compose_hash: &str, mr_config: Option, - requirements: Option<&dstack_types::Requirements>, - secure_time: bool, + app_compose: Option<&AppCompose>, ) -> Result { let image_path = cfg.image.path.join(&manifest.image); let image = Image::load(image_path).context("Failed to load image info")?; @@ -2189,8 +2188,7 @@ pub(crate) fn make_sys_config( &image, compose_hash, mr_config.clone(), - requirements, - secure_time, + app_compose, )?; let mut sys_config = json!({ "kms_urls": kms_urls, @@ -2277,9 +2275,9 @@ fn make_vm_config( image: &Image, _compose_hash: &str, mr_config: Option, - requirements: Option<&dstack_types::Requirements>, - secure_time: bool, + app_compose: Option<&AppCompose>, ) -> Result { + let requirements = app_compose.and_then(|compose| compose.requirements.as_ref()); let platform = cfg.cvm.resolved_platform(); let is_amd_sev_snp = platform == crate::config::CvmPlatform::AmdSevSnp && !manifest.no_tee; // A simulated TDX guest gets the vm_config of a TDX guest, so the simulator @@ -2451,7 +2449,9 @@ fn make_vm_config( measurement: image_measurement.measurement.clone(), vcpus: effective_vcpus, vcpu_type: Some("EPYC-v4".to_string()), - guest_features: dstack_mr::sev::launch_guest_features(secure_time), + guest_features: dstack_mr::sev::launch_guest_features( + app_compose.is_some_and(|compose| compose.secure_time), + ), }; config["sev_snp_measurement"] = serde_json::Value::String( serde_json::to_string(&measurement) @@ -4266,24 +4266,10 @@ mod tests { let image = test_tdx_image(true); let compose_hash = hex_of(0x22, 32); - let bridge_config = make_vm_config( - &config, - &bridge_manifest, - &image, - &compose_hash, - None, - None, - false, - )?; - let user_config = make_vm_config( - &config, - &user_manifest, - &image, - &compose_hash, - None, - None, - false, - )?; + let bridge_config = + make_vm_config(&config, &bridge_manifest, &image, &compose_hash, None, None)?; + let user_config = + make_vm_config(&config, &user_manifest, &image, &compose_hash, None, None)?; assert_eq!(bridge_config, user_config); Ok(()) @@ -4308,7 +4294,6 @@ mod tests { &hex_of(0x22, 32), None, None, - false, )?; assert_eq!(vm_config["num_verity_volumes"], 2); @@ -4328,7 +4313,6 @@ mod tests { &hex_of(0x22, 32), None, None, - false, )?; assert_eq!(vm_config["swtpm"], true); @@ -4343,43 +4327,20 @@ mod tests { manifest.simulated_tee = Some(dstack_types::TeeVariant::DstackTdx); for platform in [CvmPlatform::Tdx, CvmPlatform::AmdSevSnp] { config.cvm.platform = Some(platform); - let vm_config = make_vm_config( - &config, - &manifest, - &test_tdx_image(true), - "", - None, - None, - false, - )?; + let vm_config = + make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); } config.cvm.platform = Some(CvmPlatform::Tdx); manifest.swtpm = true; - let vm_config = make_vm_config( - &config, - &manifest, - &test_tdx_image(true), - "", - None, - None, - false, - )?; + let vm_config = make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; assert!(vm_config.get("tdx_measurement").is_none()); manifest.swtpm = false; manifest.simulated_tee = None; - let vm_config = make_vm_config( - &config, - &manifest, - &test_tdx_image(true), - "", - None, - None, - false, - )?; + let vm_config = make_vm_config(&config, &manifest, &test_tdx_image(true), "", None, None)?; assert!(vm_config.get("tdx_measurement").is_none()); Ok(()) } @@ -4392,15 +4353,7 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(1024); let image = test_tdx_image(true); - let vm_config = make_vm_config( - &config, - &manifest, - &image, - &hex_of(0x22, 32), - None, - None, - false, - )?; + let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); @@ -4418,15 +4371,7 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(2048); let image = test_tdx_image(true); - let vm_config = make_vm_config( - &config, - &manifest, - &image, - &hex_of(0x22, 32), - None, - None, - false, - )?; + let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); assert!(vm_config.get("tdx_measurement").is_some()); @@ -4444,15 +4389,7 @@ mod tests { let config = test_tdx_config()?; let manifest = test_manifest(3072); let image = test_tdx_image(false); - let vm_config = make_vm_config( - &config, - &manifest, - &image, - &hex_of(0x22, 32), - None, - None, - false, - )?; + let vm_config = make_vm_config(&config, &manifest, &image, &hex_of(0x22, 32), None, None)?; assert!(vm_config.get("tdx_attestation_variant").is_none()); assert!(vm_config.get("tdx_measurement").is_none()); @@ -4465,24 +4402,34 @@ mod tests { Ok(()) } + fn test_app_compose(extra: serde_json::Value) -> AppCompose { + let mut compose = json!({ + "manifest_version": 2, + "name": "test", + "runner": "docker-compose" + }); + if let (Some(compose), Some(extra)) = (compose.as_object_mut(), extra.as_object()) { + compose.extend(extra.clone()); + } + serde_json::from_value(compose).expect("valid app compose") + } + #[test] fn tdx_requirements_measure_acpi_tables_overrides_lite_to_legacy() -> Result<()> { let mut config = test_tdx_config()?; config.cvm.tdx_attestation_variant = TdxAttestationVariantConfig::Lite; let manifest = test_manifest(2048); let image = test_tdx_image(true); - let requirements = dstack_types::Requirements { - tdx_measure_acpi_tables: Some(true), - ..Default::default() - }; + let app_compose = test_app_compose(json!({ + "requirements": { "tdx_measure_acpi_tables": true } + })); let vm_config = make_vm_config( &config, &manifest, &image, &hex_of(0x22, 32), None, - Some(&requirements), - false, + Some(&app_compose), )?; assert!(vm_config.get("tdx_attestation_variant").is_none()); @@ -4498,18 +4445,16 @@ mod tests { config.cvm.tdx_attestation_variant = TdxAttestationVariantConfig::Legacy; let manifest = test_manifest(2048); let image = test_tdx_image(true); - let requirements = dstack_types::Requirements { - tdx_measure_acpi_tables: Some(false), - ..Default::default() - }; + let app_compose = test_app_compose(json!({ + "requirements": { "tdx_measure_acpi_tables": false } + })); let vm_config = make_vm_config( &config, &manifest, &image, &hex_of(0x22, 32), None, - Some(&requirements), - false, + Some(&app_compose), )?; assert_eq!(vm_config["tdx_attestation_variant"], "lite"); @@ -4610,23 +4555,24 @@ mod tests { let build_hash = Sha256::digest(sha256sum.as_bytes()).to_vec(); fs::write(image_dir.join("digest.txt"), hex::encode(&build_hash))?; + let secure_time = test_app_compose(json!({ "secure_time": true })); let image = Image::load(&image_dir)?; - let secure_time_vm_config = - make_vm_config(&config, &manifest, &image, &compose_hash, None, None, true)?; + let secure_time_vm_config = make_vm_config( + &config, + &manifest, + &image, + &compose_hash, + None, + Some(&secure_time), + )?; let secure_time_measurement: dstack_mr::sev::SnpMeasurementDocument = serde_json::from_str( secure_time_vm_config["sev_snp_measurement"] .as_str() .context("sev_snp_measurement must be a string")?, )?; assert_eq!(secure_time_measurement.guest_features, 0x201); - let sys_config_document = make_sys_config( - &config, - &manifest, - &compose_hash, - Some(mr_config), - None, - false, - )?; + let sys_config_document = + make_sys_config(&config, &manifest, &compose_hash, Some(mr_config), None)?; let sys_config: serde_json::Value = serde_json::from_str(&sys_config_document)?; assert!(sys_config.get("tee_simulator").is_none()); // A host must never nominate the trust anchor that authenticates its diff --git a/dstack/vmm/src/one_shot.rs b/dstack/vmm/src/one_shot.rs index a825b523ba..9b494d092f 100644 --- a/dstack/vmm/src/one_shot.rs +++ b/dstack/vmm/src/one_shot.rs @@ -249,8 +249,7 @@ Compose file content (first 200 chars): &manifest, &compose_hash, mr_config, - app_compose.requirements.as_ref(), - app_compose.secure_time, + Some(&app_compose), )?; let sys_config_path = vm_work_dir.shared_dir().join(".sys-config.json"); fs_err::write(&sys_config_path, &sys_config_str).context("Failed to write sys config")?; From 90eb91c2e7cb43d1ac7a549ad62ae952fc6897ff Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Thu, 26 Nov 2026 17:30:09 -0800 Subject: [PATCH 8/8] docs: secure_time is on by default, so Secure TSC applies to every SNP app The upgrade note said apps without secure_time launch as before, but secure_time defaults to true when the app compose omits it. Only apps with secure_time: false are unaffected. Signed-off-by: Kevin Wang --- CHANGELOG.md | 18 ++++++++++-------- docs/design-and-hardening-decisions.md | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cdad2b2a7..a657fe435e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,18 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Upgrade notes -- **`secure_time` refuses a clock the host controls.** An app with - `secure_time` enabled no longer boots on AWS, nor on an AMD SEV-SNP host - that cannot launch guests with Secure TSC (QEMU >= 11.2, host kernel >= 6.18, - a CPU that supports it), nor when the clocksource is not `tsc`. - Such a guest also needs a KMS and verifier of this release. Apps without - `secure_time` launch as before. +- **`secure_time` refuses a clock the host controls.** `secure_time` is on + unless the app compose sets it to `false`, so this applies to every app + that leaves it out. Such an app no longer boots on AWS, nor when the + clocksource is not `tsc`. On AMD SEV-SNP the VMM launches it with Secure + TSC, which needs QEMU >= 11.2 (not released yet), host kernel >= 6.18 and a + CPU that supports it; an older QEMU exits at startup on the unknown + `secure-tsc` property. Such a guest also needs a KMS and verifier of this + release. Apps with `secure_time: false` launch as before. ### Added - **Secure TSC for AMD SEV-SNP guests.** The VMM launches an SNP guest with - Secure TSC when its app compose sets `secure_time`, so the host cannot skew - the guest clock. + Secure TSC when `secure_time` is on, so the host cannot skew the guest + clock. ### Changed diff --git a/docs/design-and-hardening-decisions.md b/docs/design-and-hardening-decisions.md index 9276fdec53..cfb883a1e8 100644 --- a/docs/design-and-hardening-decisions.md +++ b/docs/design-and-hardening-decisions.md @@ -57,7 +57,7 @@ See [here](https://intel.github.io/ccc-linux-guest-hardening-docs/security-spec. **Implementation**: dstack OS enforces the guest kernel uses TSC as the only timer source by appending `tsc=reliable no-kvmclock` to the kernel cmdline. It also enforces the use of NTS with built-in [trusted servers](../os/yocto/layers/meta-dstack/recipes-core/chrony/files/chrony.conf) to synchronize system time. -On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. The VMM enables it for an app whose app-compose.json sets `secure_time`, which needs QEMU >= 11.2, host kernel >= 6.18 and a CPU that supports Secure TSC. +On AMD SEV-SNP the hypervisor controls the guest TSC unless the guest is launched with Secure TSC. The VMM enables it whenever `secure_time` is on in app-compose.json (the default), which needs QEMU >= 11.2, host kernel >= 6.18 and a CPU that supports Secure TSC. **Behavior**: When `secure_time` is enabled in the app-compose.json configuration, the system ensures time synchronization is completed before requesting application keys. It first checks that the host cannot move the clock between syncs and refuses to boot otherwise: the clocksource must be `tsc`, an AMD SEV-SNP guest must run with Secure TSC, and AWS is not supported. If `secure_time` is disabled, time synchronization is not enforced before application launch.