Skip to content

feat: measure the app key epoch in RTMR3, MrConfigV3 and KMS certificates - #1694

Merged
kvinwang merged 1 commit into
nextfrom
feat/measure-key-epochs
Dec 1, 2026
Merged

kvinwang merged 1 commit into
nextfrom
feat/measure-key-epochs

Conversation

@kvinwang

@kvinwang kvinwang commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A verifier cannot tell which app epoch a CVM's keys or certificates come from. Neither the quote nor KMS-issued certificates record it, and a certificate signed before an app epoch rotation stays valid until it expires. The root epoch needs no measurement of its own: the KMS root key in key-provider, or the issuing root CA, already identifies it.

Changes

  • RTMR3: for KMS keys, the guest emits an app-epoch runtime event right after key-provider. The payload is the app epoch as a 4-byte big-endian integer.
  • MrConfigV3: a new optional field, app_epoch, pins the app epoch.
    • The guest honors the pin only when TDX MR_CONFIG_ID or SEV-SNP HOST_DATA binds the document. It requests that epoch and refuses to boot if the KMS serves another.
    • SEV-SNP has no runtime register, so there the document always carries the epoch for KMS keys: without a pin, the VMM asks the KMS for the current epoch before every cold start, automatic restart and one-shot launch, and a new guest refuses to boot without it. Operator impact: a new SEV-SNP image does not boot under an older VMM, and SEV-SNP launches need the KMS reachable from the host. The VMM does this only for images whose metadata.json sets app_epoch_pin, so older images launch as before.
    • The VMM takes the pin from VmConfiguration.app_epoch and from UpdateVmRequest.update_app_epoch/app_epoch. At deployment it refuses the pin unless the key provider is KMS and the launch binds MrConfigV3.
    • vmm-cli adds deploy --app-epoch, update --app-epoch and update --no-app-epoch. The web UI adds an App Epoch field to its deploy and update dialogs, and keeps the pin when cloning. ListImages reports app_epoch_pin, and both offer the pin only for an image it marks, so an older VMM is never sent a pin it would silently drop.
    • The field is omitted when unset, so existing documents hash as before. Older guests reject a document that contains it instead of ignoring it, and on SEV-SNP so do older KMS and verifiers.
  • Sealed env: GetSealedEnvPubKey now takes SealedEnvPubKeyRequest, which is wire-compatible with AppId and adds an optional app_epoch. The KMS serves the key of any app epoch up to the current one. vmm-cli and the web UI pass the pin, so the env of a pinned VM is sealed at its epoch.
  • Certificates: SignCert signs under the app epoch the attestation records, through the new Attestation::decode_app_epoch: the boot-time app-epoch event on TDX, GCP TDX and AWS NitroTPM, or the MrConfigV3 pin on SEV-SNP. The requester cannot choose it, and the request format is unchanged. Where nothing records it (older guests, Nitro Enclave) the current epoch is used as before. A running CVM therefore renews certificates under its boot epoch's policy row; once that row is revoked, renewal is denied until it restarts. The KMS writes the epoch into a new PHALA_RATLS_APP_EPOCH extension (OID 1.3.6.1.4.1.62397.1.10), which CertExt::get_app_epoch reads. This replaces feat(kms): stamp key epochs in issued certificates #1645.
  • Verifier: dstack-verifier reports the measured app epoch as app_epoch, in /verify and in --verify-cert mode.
  • Lean models: the boot trace and MrConfig models are updated to match.

Verification

  • cargo test -p dstack-types -p ra-tls -p cert-client -p dstack-vmm -p dstack-util -p dstack-kms --all-features
  • cargo clippy on the changed crates, and cargo check --workspace --all-features --tests
  • pytest dstack/vmm/src/tests/test_vmm_cli.py, npm test in dstack/vmm/ui, and prek
  • lake build in formal/

@kvinwang
kvinwang force-pushed the feat/measure-key-epochs branch from aacc8c3 to 668f85b Compare December 1, 2026 08:40
@kvinwang kvinwang changed the title feat: measure key epochs in RTMR3, MrConfigV3 and KMS certificates feat: measure the app key epoch in RTMR3, MrConfigV3 and KMS certificates Dec 1, 2026
@kvinwang
kvinwang force-pushed the feat/measure-key-epochs branch 9 times, most recently from f7a25ba to 2ec1527 Compare December 1, 2026 10:15
@Leechael

Leechael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Reviewed at 2ec1527. I found no security bypass. The epoch SignCert signs under is always the one the attestation records, and every downgrade path I traced (LUKS header, sealed env, KMS GetAppKey/SignCert/GetSealedEnvPubKey) fails closed. Unpinned MrConfigV3 documents hash as before, and SealedEnvPubKeyRequest is wire-compatible with AppId.

Findings, most important first:

Should fix before merge

  1. resize_vm skips validate_app_epoch_image (dstack/vmm/src/main_service.rs:1395). create_vm and update_vm both check it, but ResizeVm can move a pinned VM to an image without app_epoch_pin. The pin is then only rejected at the next start, in launch_app_epoch. Add the check after apply_resource_updates.

  2. Web UI: showDeployDialog() does not reset app_epoch (dstack/vmm/ui/src/composables/useVmManager.ts:1185). It clears the other fields a clone leaves behind (networks, app_id, disk_prealloc, annotations) but not this one. If you clone a pinned VM, cancel, and then click Deploy, the new VM is silently pinned to the clone's epoch. Add vmForm.value.app_epoch = ''.

  3. The PR description contradicts the code on SEV-SNP. It says the current epoch is used for "SEV-SNP without a pin". In the code, a new guest refuses to boot on SEV-SNP with KMS keys when no pin is set (dstack-util/src/system_setup.rs:2619). So the VMM asks the KMS for the current epoch before every cold start, auto-restart and one-shot launch (vmm/src/app/mr_config.rs:139). The commit message and docs/app-key-epochs.md are right. The description should state the operator impact: a new SEV-SNP image does not boot under an older VMM, and SEV-SNP restarts now need the KMS reachable from the host.

  4. docs/sealed-env.md:40-41 overstates the web UI. It says both vmm-cli and the web UI accept the historical prefix "only for an app epoch the user chose, never one the host reports". The UI does not verify the signature at all; it relies on the same-origin VMM channel. The update dialog also prefills app_epoch from the VMM's config.app_epoch (useVmManager.ts:1242), so Reset Secrets without editing that field seals at the host-reported epoch. vmm-cli refuses exactly this case (sealing_app_epoch). This is not a bypass, since the UI already trusts the key the VMM returns, but the doc should describe the UI's actual trust model.

Tests

  1. The core SignCert claim has no test. sign_cert_issues_from_the_current_epochs_after_rotation uses a Nitro Enclave quote, so decode_app_epoch() returns None and only the fallback to the current epoch is covered. Suggested cases:
    • A measured epoch older than the current one (TDX app-epoch event, or SEV-SNP MrConfigV3.app_epoch). It should send requestedAppEpoch to /authorizeApp, derive that epoch's app CA, and stamp PHALA_RATLS_APP_EPOCH.
    • A measured epoch newer than the current one, which should be rejected.
    • Rejection when that epoch's policy rows are revoked.
  2. Attestation::decode_app_epoch is only tested on the TDX event path. The SEV-SNP branch (with and without a pin, malformed mr_config) and the Nitro branch have no tests.

Behaviour change worth a CHANGELOG line

  1. SignCert now authorizes against the booted epoch's policy rows (kms/src/main_service.rs:1159; DstackApp.sol:365). Suppose a TDX CVM on a new image boots at epoch N, and the owner then advances to N+1 and clears N's rows as the docs advise. Runtime certificate renewal is then denied until the CVM reboots; before this PR it succeeded under N+1. The reverse also holds: while N's rows remain, the CVM keeps getting epoch-N certificates. This is consistent with the design, but it is visible to operators.

Minor

  1. Adopting a running VM can rewrite its mr_config (vmm/src/app.rs:557, mr_config.rs:52). run_start keeps the launched app_epoch, but launch_mr_config_version picks V1 or V3 from the on-disk manifest.app_epoch, which a pending update --app-epoch / --no-app-epoch may have changed. If the VMM restarts in that window, it either drops the V3 document from the host-side sys-config or writes a V3 document with app_epoch: None. The running guest is unaffected, because it reads its embedded copy, and the next cold start rebuilds the document. But the code does not match the new comment ("a running guest keeps the document it was launched with"). Reusing the existing sys_config.mr_config while the VM is running would fix it. Rebuilding on adopt was already the behaviour before this PR; only the comment is new.
  2. vmm-cli update --app-epoch checks the pin too late. ensure_app_epoch runs only after UpgradeApp has applied, so against an older VMM the resize and compose changes land before the "ignored the app epoch pin" error. deploy avoids this by creating the VM stopped.
  3. The web UI update dialog never reads back whether the VMM kept the pin. vmm-cli does. An older VMM drops the pin silently.
  4. Nit: verify_sealed_env_pubkey accepts a v1-signed key for any app_epoch even when a pin is passed. The only caller (encrypt_env_payload) checks the epoch afterwards, but moving that check into verify_sealed_env_pubkey would protect future callers.

…ates

A verifier could not tell which app epoch a CVM's keys or certificates
came from. The root epoch needs no measurement: the KMS root key in
`key-provider`, or the issuing root CA, already identifies it.

- The guest emits an `app-epoch` runtime event after `key-provider` for
  KMS keys, with the app epoch as a big-endian u32.
- MrConfigV3 gains an optional `app_epoch` pin. The guest honors it only
  when TDX MR_CONFIG_ID or SEV-SNP HOST_DATA binds the document,
  requests that app epoch from the KMS, and refuses to boot when the
  served epoch differs. SEV-SNP has no runtime register, so there the
  VMM always binds the app epoch for KMS keys, asking the KMS for the
  current one when no pin is set, on images whose metadata.json sets
  `app_epoch_pin`, and the guest refuses to boot without it.
- The VMM takes the pin as `VmConfiguration.app_epoch` and
  `UpdateVmRequest.update_app_epoch`, and refuses it at deployment
  unless the key provider is KMS and the launch binds MrConfigV3.
  `ListImages` reports `app_epoch_pin`, and vmm-cli (`deploy
  --app-epoch`, `update --app-epoch/--no-app-epoch`) and the web UI (an
  App Epoch field) offer the pin only for an image it marks, so an older
  VMM is never sent a pin it would drop. The VMM writes launch inputs
  only when it launches a guest, not when it adopts a running one.
- `GetSealedEnvPubKey` takes `SealedEnvPubKeyRequest` with an optional
  `app_epoch` up to the current one, so vmm-cli and the web UI seal the
  env of a pinned VM at its epoch, never at one only the host reports.
  The key of an older epoch is signed under
  `dstack-sealed-env-pubkey-historical-v1`, so verifiers expecting the
  current key reject it.
- `SignCert` signs under the app epoch the attestation records
  (`Attestation::decode_app_epoch`: the boot-time `app-epoch` event where
  a register replays it, the MrConfigV3 pin on SEV-SNP), falling back to
  the current one where nothing records it. It stamps that epoch into the
  `PHALA_RATLS_APP_EPOCH` certificate extension (OID
  1.3.6.1.4.1.62397.1.10), read by `CertExt::get_app_epoch`.
- dstack-verifier reports the measured app epoch as `app_epoch`, in
  `/verify` and in the certificate mode.
- The Lean boot trace, MrConfig and KMS signature models follow these
  changes.

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang force-pushed the feat/measure-key-epochs branch from 2ec1527 to 050dc7a Compare December 1, 2026 12:28
@kvinwang

kvinwang commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough review. All addressed in 050dc7a:

  1. resize_vm now runs validate_app_epoch_image after apply_resource_updates, like create_vm and update_vm.
  2. showDeployDialog() resets app_epoch.
  3. The PR description now matches the code on SEV-SNP and states the operator impact: a new SEV-SNP image does not boot under an older VMM, and SEV-SNP launches need the KMS reachable from the host.
  4. docs/sealed-env.md now describes the web UI's actual trust model: it verifies no signature, trusts the key the VMM returns over its own origin, and its update dialog prefills the pin the VMM reports. Only vmm-cli refuses to seal at a host-reported pin.
  5. SignCert is split into quote verification and sign_attested_csr, so a new test drives the latter with an SEV-SNP caller whose MrConfigV3 binds the epoch. It covers an older measured epoch (requestedAppEpoch, that epoch's app CA, and the PHALA_RATLS_APP_EPOCH extension), a revoked row, and an epoch beyond the current one.
  6. decode_app_epoch is now tested on SEV-SNP (pinned, unpinned, malformed document, and a runtime event that must not stand in for the document) and on Nitro Enclave.
  7. Added to the CHANGELOG upgrade note: once the boot epoch's row is revoked, certificate renewal is denied until the CVM restarts.
  8. Fixed at the root. The VMM writes launch inputs only when it launches a guest; adopting a running guest leaves them alone, since the guest copied its own at boot and never reads the host's again. launched_app_epoch() is gone. This also stops the pre-existing rebuild-on-adopt behaviour for compose changes.
  9. and 10. ListImages now reports app_epoch_pin. vmm-cli refuses --app-epoch before sending anything unless the target image reports it, and the web UI shows the App Epoch field only for such images. An older VMM never reports the flag, so it is never sent a pin it would drop. This replaces the readback in vmm-cli (ensure_app_epoch and the stopped-create dance), which is now removed.
  10. verify_sealed_env_pubkey rejects a key of another epoch whenever a pin is passed.

@Leechael Leechael left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 050dc7a: all 11 points are addressed. Adopting a running guest no longer rewrites its launch inputs, which is safe since the guest copies host-shared at boot and unmounts it. The ListImages capability gate is a cleaner fix than the readback. LGTM.

@kvinwang
kvinwang merged commit edff019 into next Dec 1, 2026
29 checks passed
@kvinwang
kvinwang deleted the feat/measure-key-epochs branch December 1, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants