From a6177bd18b2fd4c32e7fff3dbe78ac442fa0334c Mon Sep 17 00:00:00 2001 From: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:03:50 +0000 Subject: [PATCH 1/3] fix(vmm): prefer host kms_url when querying SEV-SNP app epoch On SEV-SNP launches with a 0.6.7+ guest image (app_epoch_pin: true), the VMM queries the KMS from the host to resolve the current app epoch before starting QEMU. Under single-node user-mode networking (as configured by dstackup), cvm.kms_urls holds the guest-only 10.0.2.2 alias, which is not routable from the host. When the VM manifest does not override kms_urls, try the host-side top-level kms_url first and fall back to cvm.kms_urls (deduplicated). Signed-off-by: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> --- dstack/vmm/src/app/mr_config.rs | 71 ++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/dstack/vmm/src/app/mr_config.rs b/dstack/vmm/src/app/mr_config.rs index 76094891a..8b2a6206b 100644 --- a/dstack/vmm/src/app/mr_config.rs +++ b/dstack/vmm/src/app/mr_config.rs @@ -133,6 +133,28 @@ fn is_snp_launch(manifest: &Manifest, platform: CvmPlatform) -> bool { || (!manifest.no_tee && platform == CvmPlatform::AmdSevSnp) } +/// KMS URLs the VMM tries from the host to resolve the current app epoch. +/// +/// When the VM manifest does not override `kms_urls`, prefer the host-side +/// top-level `config.kms_url` ahead of `config.cvm.kms_urls`: under QEMU +/// user-mode networking `cvm.kms_urls` carries the guest-only `10.0.2.2` +/// alias, which is not routable from the host. +fn app_epoch_kms_urls<'a>(manifest: &'a Manifest, config: &'a Config) -> Vec<&'a str> { + if !manifest.kms_urls.is_empty() { + return manifest.kms_urls.iter().map(String::as_str).collect(); + } + let mut urls = Vec::new(); + if !config.kms_url.is_empty() { + urls.push(config.kms_url.as_str()); + } + for url in &config.cvm.kms_urls { + if !urls.contains(&url.as_str()) { + urls.push(url.as_str()); + } + } + urls +} + /// The app epoch a launch binds in MrConfigV3: the pin, or on SEV-SNP with KMS /// keys the KMS's current app epoch, since SEV-SNP has no runtime register to /// measure the epoch the guest is served. Images predating the field get none. @@ -154,11 +176,7 @@ pub(crate) async fn launch_app_epoch( { return Ok(manifest.app_epoch); } - let kms_urls = if manifest.kms_urls.is_empty() { - &config.cvm.kms_urls - } else { - &manifest.kms_urls - }; + let kms_urls = app_epoch_kms_urls(manifest, config); if kms_urls.is_empty() { bail!("an amd sev-snp launch with KMS keys needs a KMS URL to read the app epoch"); } @@ -372,4 +390,47 @@ mod tests { let document: serde_json::Value = serde_json::from_str(&document).unwrap(); assert_eq!(document["init_script_hashes"], serde_json::json!([])); } + + #[test] + fn app_epoch_kms_urls_prefers_host_kms_url() { + let mut config: Config = crate::Figment::from(crate::config::load_config_figment(None)) + .extract() + .unwrap(); + config.kms_url = "https://127.0.0.1:13443".into(); + config.cvm.kms_urls = vec![ + "https://10.0.2.2:13443".into(), + "https://127.0.0.1:13443".into(), + ]; + + let mut manifest = Manifest::from_json(serde_json::json!({ + "id": "vm-1", + "name": "vm-1", + "app_id": "11".repeat(20), + "vcpu": 2, + "memory": 2048, + "disk_size": 1024, + "image": "dstack-0.6.7", + "port_map": [], + "created_at_ms": 0 + })) + .unwrap(); + + assert_eq!( + app_epoch_kms_urls(&manifest, &config), + vec!["https://127.0.0.1:13443", "https://10.0.2.2:13443"] + ); + + manifest.kms_urls = vec!["https://override.example".into()]; + assert_eq!( + app_epoch_kms_urls(&manifest, &config), + vec!["https://override.example"] + ); + + manifest.kms_urls.clear(); + config.kms_url.clear(); + assert_eq!( + app_epoch_kms_urls(&manifest, &config), + vec!["https://10.0.2.2:13443", "https://127.0.0.1:13443"] + ); + } } From 1b7aaf8eed638f503ac4cf162887ab22c4646321 Mon Sep 17 00:00:00 2001 From: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:04:19 +0000 Subject: [PATCH 2/3] fix(dstackup): set host kms_url in vmm.toml dstackup install writes cvm.kms_urls with the QEMU user-mode guest alias (https://10.0.2.2:), which is not routable from the host, and left the top-level host kms_url empty. Set kms_url = https://127.0.0.1: when KMS is enabled so host-side VMM RPCs (app-epoch resolution on 0.6.7 SEV-SNP launches and env-key lookups) can reach the local KMS. Signed-off-by: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> --- dstack/crates/dstack-cli-core/src/config.rs | 8 +++++++- dstack/crates/dstackup/src/install.rs | 10 +++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/dstack/crates/dstack-cli-core/src/config.rs b/dstack/crates/dstack-cli-core/src/config.rs index d2f832bce..80d0e32aa 100644 --- a/dstack/crates/dstack-cli-core/src/config.rs +++ b/dstack/crates/dstack-cli-core/src/config.rs @@ -283,6 +283,8 @@ pub struct VmmRender { pub key_provider_addr: String, /// local key-provider port. pub key_provider_port: u32, + /// Host-reachable KMS URL used by the VMM itself (e.g. `https://127.0.0.1:`). + pub kms_url: String, /// KMS URLs injected into app CVMs (the guest-visible KMS address). pub kms_urls: Vec, /// confidential-computing platform (selects qemu/share-mode for the CVMs). @@ -308,6 +310,7 @@ impl Default for VmmRender { host_api_port: 10000, key_provider_addr: "127.0.0.1".to_string(), key_provider_port: 3443, + kms_url: String::new(), kms_urls: Vec::new(), platform: Platform::Tdx, auth_enabled: false, @@ -332,7 +335,7 @@ keep_alive = 10 log_level = "info" address = "{dashboard_addr}" reuse = true -kms_url = "" +kms_url = "{kms_url}" event_buffer_size = 20 node_name = "" run_path = "{vm_path}" @@ -436,6 +439,7 @@ port = {kp_port} Platform::AmdSevSnp => "vhd", Platform::Tdx => "9p", }, + kms_url = r.kms_url, kms_urls = r .kms_urls .iter() @@ -469,12 +473,14 @@ mod tests { dashboard_addr: "tcp:127.0.0.1:19080".into(), cid_start: 2000, host_api_port: 10001, + kms_url: "https://127.0.0.1:13443".into(), auth_enabled: true, auth_token: "deadbeef".into(), ..Default::default() }; let rendered = vmm_toml(&r); assert!(rendered.contains(r#"address = "tcp:127.0.0.1:19080""#)); + assert!(rendered.contains(r#"kms_url = "https://127.0.0.1:13443""#)); assert!(rendered.contains("cid_start = 2000")); assert!(rendered.contains("port = 10001")); assert!(rendered.contains("enabled = true")); diff --git a/dstack/crates/dstackup/src/install.rs b/dstack/crates/dstackup/src/install.rs index 1dc6fd0b9..46794e79e 100644 --- a/dstack/crates/dstackup/src/install.rs +++ b/dstack/crates/dstackup/src/install.rs @@ -156,10 +156,13 @@ pub(crate) async fn cmd_install(mut o: InstallOpts, release_api_base_url: &str) // KMS host port + URL, resolved during preflight so conflicts are caught // before image download, builds, or systemd writes. - let kms_urls = if o.no_kms { - vec![] + let (kms_url, kms_urls) = if o.no_kms { + (String::new(), vec![]) } else { - vec![format!("https://10.0.2.2:{kms_port}")] + ( + format!("https://127.0.0.1:{kms_port}"), + vec![format!("https://10.0.2.2:{kms_port}")], + ) }; // 9. render configs. @@ -174,6 +177,7 @@ pub(crate) async fn cmd_install(mut o: InstallOpts, release_api_base_url: &str) host_api_port: o.host_api_port, key_provider_addr: kp_addr, key_provider_port: kp_port as u32, + kms_url, kms_urls: kms_urls.clone(), platform, auth_enabled: true, From 2ad3c4f62d2a8232cf9d858ff760b47fcb477b2d Mon Sep 17 00:00:00 2001 From: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:18:51 +0000 Subject: [PATCH 3/3] fix(vmm-cli): allow --env-file with --kms-encrypt-url alone create_vm rejected --env-file when --kms-url was omitted even if --kms-encrypt-url was given (unlike update_vm, which already allowed it). Allowing --kms-encrypt-url without --kms-url lets single-node hosts encrypt envs against the host-side KMS without overriding cvm.kms_urls on the VM. Signed-off-by: Pierre LE GUEN <26087574+PierreLeGuen@users.noreply.github.com> --- dstack/vmm/src/tests/test_vmm_cli.py | 14 ++++++++++++++ dstack/vmm/src/vmm-cli.py | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/dstack/vmm/src/tests/test_vmm_cli.py b/dstack/vmm/src/tests/test_vmm_cli.py index 397004003..630a304f5 100644 --- a/dstack/vmm/src/tests/test_vmm_cli.py +++ b/dstack/vmm/src/tests/test_vmm_cli.py @@ -700,6 +700,20 @@ def rpc(method, params=None): methods = [c.args[0] for c in self.cli.rpc_call.call_args_list] self.assertNotIn("CreateVm", methods) + # --env-file with --kms-encrypt-url works without overriding manifest kms_urls. + capability = {"sealed_env": True} + args.env_file = "env" + args.app_epoch = None + args.kms_url = None + args.kms_encrypt_url = "https://127.0.0.1:13444" + client.return_value.request.return_value = (200, self.response) + self.cli.rpc_call.reset_mock() + self.cli.create_vm(args) + method, payload = self.cli.rpc_call.call_args.args + self.assertEqual(method, "CreateVm") + self.assertNotIn("kms_urls", payload) + self.decrypt(payload) + if __name__ == "__main__": unittest.main() diff --git a/dstack/vmm/src/vmm-cli.py b/dstack/vmm/src/vmm-cli.py index b273dcf3b..79f65550b 100755 --- a/dstack/vmm/src/vmm-cli.py +++ b/dstack/vmm/src/vmm-cli.py @@ -1165,11 +1165,11 @@ def create_vm(self, args) -> None: envs = parse_env_file(args.env_file) - # Validate: --env-file requires --kms-url and kms_enabled in compose + # Validate: --env-file requires --kms-url or --kms-encrypt-url, and kms_enabled in compose if envs: - if not args.kms_url: + if not args.kms_url and not args.kms_encrypt_url: raise Exception( - "--env-file requires --kms-url to encrypt environment variables" + "--env-file requires --kms-url or --kms-encrypt-url to encrypt environment variables" ) try: compose_json = json.loads(compose_content)