[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for cert
From: |
Daniel P . Berrangé |
Subject: |
Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching |
Date: |
Wed, 18 Dec 2024 17:50:52 +0000 |
User-agent: |
Mutt/2.2.13 (2024-03-09) |
On Wed, Dec 18, 2024 at 09:49:39AM -0600, Michael Roth wrote:
> The GHCB specification[1] defines a VMGEXIT-based Guest Request
> hypercall to allow an SNP guest to issue encrypted requests directly to
> SNP firmware to do things like query the attestation report for the
> guest. These are generally handled purely in the kernel.
>
> In some some cases, it's useful for the host to be able to additionally
> supply the certificate chain for the signing key that SNP firmware uses
> to sign these attestation reports. To allow for this, the GHCB
> specification defines an Extended Guest Request where this certificate
> data can be provided in a special format described in the GHCB spec.
> This certificate data may be global or guest-specific depending on how
> the guest was configured. Rather than providing interfaces to manage
> these within the kernel, KVM provides a new KVM_EXIT_SNP_REQ_CERTS exit
> to request the certificate contents from userspace. Implement support
> for that here.
>
> To synchronize delivery of the certificates to the guest in a way where
> they will not be rendered invalid by updates to SNP firmware or
> attestation singing/endorsement keys by management tools outside the
> purview of QEMU, it is expected by users of KVM_EXIT_SNP_REQ_CERTS to
> obtain a shared/read lock on the certificate file prior to delivering
> them back to KVM. Only after this will the attestation report be
> retrieved from firmware and bundled with the certificate data, so QEMU
> must continue to hold the file lock until KVM confirms that the
> attestation report has been retrieved/bundled. This confirmation is done
> by way of the kvm_immediate_exit callback infrastructure that was
> introduced in a previous patch.
>
> [1] "Guest Hypervisor Communication Block (GHCB) Standardization",
> https://www.amd.com/en/developer/sev.html
>
> Signed-off-by: Michael Roth <michael.roth@amd.com>
> ---
> qapi/qom.json | 23 +++-
> target/i386/kvm/kvm.c | 10 ++
> target/i386/sev-sysemu-stub.c | 5 +
> target/i386/sev.c | 249 ++++++++++++++++++++++++++++++++++
> target/i386/sev.h | 2 +
> 5 files changed, 288 insertions(+), 1 deletion(-)
>
> diff --git a/qapi/qom.json b/qapi/qom.json
> index 28ce24cd8d..6eaf0e7721 100644
> --- a/qapi/qom.json
> +++ b/qapi/qom.json
> @@ -1034,6 +1034,25 @@
> # firmware. Set this to true to disable the use of VCEK.
> # (default: false) (since: 9.1)
> #
> +# @certs-path: Path to certificate data that can be passed to guests via
> +# SNP Extended Guest Requests. File should be in the format
> +# described in the GHCB specification. (default: none)
> +# (since: 10.0)
Can we document the required format here explicitly, rather than expecting
users to go searching for specs which are often practically impossible
to find, and even harder to read & interpret ?
> diff --git a/target/i386/sev.c b/target/i386/sev.c
> index 1a4eb1ada6..2c41bdbccf 100644
> --- a/target/i386/sev.c
> +++ b/target/i386/sev.c
> @@ -157,6 +157,9 @@ struct SevSnpGuestState {
> char *id_auth_base64;
> uint8_t *id_auth;
> char *host_data;
> + char *certs_path;
> + int certs_fd;
> + uint32_t certs_timeout;
>
> struct kvm_sev_snp_launch_start kvm_start_conf;
> struct kvm_sev_snp_launch_finish kvm_finish_conf;
> @@ -1355,6 +1358,215 @@ sev_snp_launch_finish(SevCommonState *sev_common)
> }
> }
>
> +static int open_certs_locked(SevSnpGuestState *sev_snp_guest)
> +{
> + int fd, ret;
> +
> + if (sev_snp_guest->certs_fd != -1) {
> + return 0;
> + }
> +
> + fd = qemu_open(sev_snp_guest->certs_path, O_RDONLY, NULL);
> + if (fd == -1) {
> + error_report("Unable to open certificate blob at path %s, ret %d",
> + sev_snp_guest->certs_path, fd);
> + return fd;
> + }
> +
> + ret = qemu_lock_fd(fd, 0, 0, false);
> + if (ret == -EAGAIN || ret == -EACCES) {
> + ret = -EAGAIN;
> + goto out_close;
> + } else if (ret) {
> + goto out_close;
> + }
This locking scheme is likely unsafe. Consider this sequence
* QEMU runs qemu_open(path)
* External mgmt app runs unlink(path)
* External mgmt app runs open(path)
* External mgmt app runs lock(fd)
* QEMU runs qemu_lock_fd(fd)
QEMU has successfully acquired a lock on an FD that corresponds to a
deleted file, not the current existing file.
Avoiding this problem requires either that the external mgmt app agrees
to *NEVER* unlink() the files under any circumstance, or for QEMU to
run its open + lock logic in a loop, checking 'stat' and 'fstat' before
opening and after locking, in order to detect a replaced file from its
changed inode.
I'm not inclined to rely on mgmt apps never unlink()ing as that's to
easy to mess up IMHO.
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
- [PATCH RFC v1 0/3] SEV-SNP: Add support for SNP certificate fetching, Michael Roth, 2024/12/18
- [PATCH v1 1/3] linux-headers: Update for 6.12 and SNP certificate support, Michael Roth, 2024/12/18
- [PATCH v1 2/3] accel/kvm: Add kvm_immediate_exit callback infrastructure, Michael Roth, 2024/12/18
- [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Michael Roth, 2024/12/18
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Markus Armbruster, 2024/12/18
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching,
Daniel P . Berrangé <=
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Michael Roth, 2024/12/18
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Daniel P . Berrangé, 2024/12/19
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Michael Roth, 2024/12/19
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Daniel P . Berrangé, 2024/12/19
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Michael Roth, 2024/12/19
- Re: [PATCH v1 3/3] i386/sev: Add KVM_EXIT_SNP_REQ_CERTS support for certificate-fetching, Daniel P . Berrangé, 2024/12/19