Skip to content

Commit 20c3c41

Browse files
mdrothsean-jc
authored andcommitted
KVM: SEV: Add KVM_SEV_SNP_ENABLE_REQ_CERTS command
Introduce a new command for KVM_MEMORY_ENCRYPT_OP ioctl that can be used to enable fetching of endorsement key certificates from userspace via the new KVM_EXIT_SNP_REQ_CERTS exit type. Also introduce a new KVM_X86_SEV_SNP_REQ_CERTS KVM device attribute so that userspace can query whether the kernel supports the new command/exit. Suggested-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Liam Merwick <liam.merwick@oracle.com> Tested-by: Liam Merwick <liam.merwick@oracle.com> Signed-off-by: Michael Roth <michael.roth@amd.com> Link: https://patch.msgid.link/20260109231732.1160759-3-michael.roth@amd.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent fa9893f commit 20c3c41

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

Documentation/virt/kvm/x86/amd-memory-encryption.rst

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,68 @@ Returns: 0 on success, -negative on error
572572
See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for further
573573
details on the input parameters in ``struct kvm_sev_snp_launch_finish``.
574574

575+
21. KVM_SEV_SNP_ENABLE_REQ_CERTS
576+
--------------------------------
577+
578+
The KVM_SEV_SNP_ENABLE_REQ_CERTS command will configure KVM to exit to
579+
userspace with a ``KVM_EXIT_SNP_REQ_CERTS`` exit type as part of handling
580+
a guest attestation report, which will to allow userspace to provide a
581+
certificate corresponding to the endorsement key used by firmware to sign
582+
that attestation report.
583+
584+
Returns: 0 on success, -negative on error
585+
586+
NOTE: The endorsement key used by firmware may change as a result of
587+
management activities like updating SEV-SNP firmware or loading new
588+
endorsement keys, so some care should be taken to keep the returned
589+
certificate data in sync with the actual endorsement key in use by
590+
firmware at the time the attestation request is sent to SNP firmware. The
591+
recommended scheme to do this is to use file locking (e.g. via fcntl()'s
592+
F_OFD_SETLK) in the following manner:
593+
594+
- Prior to obtaining/providing certificate data as part of servicing an
595+
exit type of ``KVM_EXIT_SNP_REQ_CERTS``, the VMM should obtain a
596+
shared/read or exclusive/write lock on the certificate blob file before
597+
reading it and returning it to KVM, and continue to hold the lock until
598+
the attestation request is actually sent to firmware. To facilitate
599+
this, the VMM can set the ``immediate_exit`` flag of kvm_run just after
600+
supplying the certificate data, and just before resuming the vCPU.
601+
This will ensure the vCPU will exit again to userspace with ``-EINTR``
602+
after it finishes fetching the attestation request from firmware, at
603+
which point the VMM can safely drop the file lock.
604+
605+
- Tools/libraries that perform updates to SNP firmware TCB values or
606+
endorsement keys (e.g. via /dev/sev interfaces such as ``SNP_COMMIT``,
607+
``SNP_SET_CONFIG``, or ``SNP_VLEK_LOAD``, see
608+
Documentation/virt/coco/sev-guest.rst for more details) in such a way
609+
that the certificate blob needs to be updated, should similarly take an
610+
exclusive lock on the certificate blob for the duration of any updates
611+
to endorsement keys or the certificate blob contents to ensure that
612+
VMMs using the above scheme will not return certificate blob data that
613+
is out of sync with the endorsement key used by firmware at the time
614+
the attestation request is actually issued.
615+
616+
This scheme is recommended so that tools can use a fairly generic/natural
617+
approach to synchronizing firmware/certificate updates via file-locking,
618+
which should make it easier to maintain interoperability across
619+
tools/VMMs/vendors.
620+
575621
Device attribute API
576622
====================
577623

578624
Attributes of the SEV implementation can be retrieved through the
579625
``KVM_HAS_DEVICE_ATTR`` and ``KVM_GET_DEVICE_ATTR`` ioctls on the ``/dev/kvm``
580626
device node, using group ``KVM_X86_GRP_SEV``.
581627

582-
Currently only one attribute is implemented:
628+
The following attributes are currently implemented:
583629

584630
* ``KVM_X86_SEV_VMSA_FEATURES``: return the set of all bits that
585631
are accepted in the ``vmsa_features`` of ``KVM_SEV_INIT2``.
586632

633+
* ``KVM_X86_SEV_SNP_REQ_CERTS``: return a value of 1 if the kernel supports the
634+
``KVM_EXIT_SNP_REQ_CERTS`` exit, which allows for fetching endorsement key
635+
certificates from userspace for each SNP attestation request the guest issues.
636+
587637
Firmware Management
588638
===================
589639

arch/x86/include/uapi/asm/kvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ struct kvm_sync_regs {
503503
#define KVM_X86_GRP_SEV 1
504504
# define KVM_X86_SEV_VMSA_FEATURES 0
505505
# define KVM_X86_SNP_POLICY_BITS 1
506+
# define KVM_X86_SEV_SNP_REQ_CERTS 2
506507

507508
struct kvm_vmx_nested_state_data {
508509
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
@@ -743,6 +744,7 @@ enum sev_cmd_id {
743744
KVM_SEV_SNP_LAUNCH_START = 100,
744745
KVM_SEV_SNP_LAUNCH_UPDATE,
745746
KVM_SEV_SNP_LAUNCH_FINISH,
747+
KVM_SEV_SNP_ENABLE_REQ_CERTS,
746748

747749
KVM_SEV_NR_MAX,
748750
};

arch/x86/kvm/svm/sev.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,9 @@ int sev_dev_get_attr(u32 group, u64 attr, u64 *val)
21582158
*val = snp_supported_policy_bits;
21592159
return 0;
21602160

2161+
case KVM_X86_SEV_SNP_REQ_CERTS:
2162+
*val = sev_snp_enabled ? 1 : 0;
2163+
return 0;
21612164
default:
21622165
return -ENXIO;
21632166
}
@@ -2574,6 +2577,16 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
25742577
return ret;
25752578
}
25762579

2580+
static int snp_enable_certs(struct kvm *kvm)
2581+
{
2582+
if (kvm->created_vcpus || !sev_snp_guest(kvm))
2583+
return -EINVAL;
2584+
2585+
to_kvm_sev_info(kvm)->snp_certs_enabled = true;
2586+
2587+
return 0;
2588+
}
2589+
25772590
int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
25782591
{
25792592
struct kvm_sev_cmd sev_cmd;
@@ -2679,6 +2692,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
26792692
case KVM_SEV_SNP_LAUNCH_FINISH:
26802693
r = snp_launch_finish(kvm, &sev_cmd);
26812694
break;
2695+
case KVM_SEV_SNP_ENABLE_REQ_CERTS:
2696+
r = snp_enable_certs(kvm);
2697+
break;
26822698
default:
26832699
r = -EINVAL;
26842700
goto out;

0 commit comments

Comments
 (0)