All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Roy Hopkins <roy.hopkins@suse.com>
Cc: qemu-devel@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Sergio Lopez" <slp@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Alistair Francis" <alistair@alistair23.me>,
	"Peter Xu" <peterx@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Tom Lendacky" <thomas.lendacky@amd.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Jörg Roedel" <jroedel@suse.com>
Subject: Re: [PATCH v3 14/15] i386/sev: Add implementation of CGS set_guest_policy()
Date: Mon, 24 Jun 2024 15:53:56 +0100	[thread overview]
Message-ID: <ZnmIhC5qF5il2NwW@redhat.com> (raw)
In-Reply-To: <bcf161124214aa768f05eb99e174705741dda352.1718979106.git.roy.hopkins@suse.com>

On Fri, Jun 21, 2024 at 03:29:17PM +0100, Roy Hopkins wrote:
> The new cgs_set_guest_policy() function is provided to receive the guest
> policy flags, SNP ID block and SNP ID authentication from guest
> configuration such as an IGVM file and apply it to the platform prior to
> launching the guest.
> 
> The policy is used to populate values for the existing 'policy',
> 'id_block' and 'id_auth' parameters. When provided, the guest policy is
> applied and the ID block configuration is used to verify the launch
> measurement and signatures. The guest is only successfully started if
> the expected launch measurements match the actual measurements and the
> signatures are valid.
> 
> Signed-off-by: Roy Hopkins <roy.hopkins@suse.com>
> ---
>  target/i386/sev.h | 12 +++++++
>  target/i386/sev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 95 insertions(+)
> 
> diff --git a/target/i386/sev.h b/target/i386/sev.h
> index 2ccd6fe1e8..7b92102bd0 100644
> --- a/target/i386/sev.h
> +++ b/target/i386/sev.h
> @@ -157,6 +157,18 @@ struct QEMU_PACKED sev_es_save_area {
>      uint8_t fpreg_ymm[256];
>  };
>  
> +struct QEMU_PACKED sev_snp_id_authentication {
> +    uint32_t id_key_alg;
> +    uint32_t auth_key_algo;
> +    uint8_t reserved[56];
> +    uint8_t id_block_sig[512];
> +    uint8_t id_key[1028];
> +    uint8_t reserved2[60];
> +    uint8_t id_key_sig[512];
> +    uint8_t author_key[1028];
> +    uint8_t reserved3[892];
> +};
> +
>  #ifdef CONFIG_SEV
>  bool sev_enabled(void);
>  bool sev_es_enabled(void);
> diff --git a/target/i386/sev.c b/target/i386/sev.c
> index 5d9ef745bb..688b378c42 100644
> --- a/target/i386/sev.c
> +++ b/target/i386/sev.c
> @@ -2446,6 +2446,88 @@ static int cgs_get_mem_map_entry(int index,
>      return 0;
>  }
>  
> +static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type,
> +                                uint64_t policy, void *policy_data1,
> +                                uint32_t policy_data1_size, void *policy_data2,
> +                                uint32_t policy_data2_size, Error **errp)
> +{
> +    if (policy_type != GUEST_POLICY_SEV) {
> +        error_setg(errp, "%s: Invalid guest policy type provided for SEV: %d",
> +        __func__, policy_type);
> +        return -1;
> +    }
> +    /*
> +     * SEV-SNP handles policy differently. The policy flags are defined in
> +     * kvm_start_conf.policy and an ID block and ID auth can be provided.
> +     */
> +    if (sev_snp_enabled()) {
> +        SevSnpGuestState *sev_snp_guest =
> +            SEV_SNP_GUEST(MACHINE(qdev_get_machine())->cgs);
> +        struct kvm_sev_snp_launch_finish *finish =
> +            &sev_snp_guest->kvm_finish_conf;
> +
> +        /*
> +         * The policy consists of flags in 'policy' and optionally an ID block
> +         * and ID auth in policy_data1 and policy_data2 respectively. The ID
> +         * block and auth are optional so clear any previous ID block and auth
> +         * and set them if provided, but always set the policy flags.
> +         */
> +        g_free(sev_snp_guest->id_block);
> +        g_free((guchar *)finish->id_block_uaddr);
> +        g_free(sev_snp_guest->id_auth);
> +        g_free((guchar *)finish->id_auth_uaddr);
> +        sev_snp_guest->id_block = NULL;
> +        finish->id_block_uaddr = 0;
> +        sev_snp_guest->id_auth = NULL;
> +        finish->id_auth_uaddr = 0;
> +
> +        if (policy_data1_size > 0) {
> +            struct sev_snp_id_authentication *id_auth =
> +                (struct sev_snp_id_authentication *)policy_data2;
> +
> +            if (policy_data1_size != KVM_SEV_SNP_ID_BLOCK_SIZE) {
> +                error_setg(errp, "%s: Invalid SEV-SNP ID block: incorrect size",
> +                           __func__);
> +                return -1;
> +            }
> +            if (policy_data2_size != KVM_SEV_SNP_ID_AUTH_SIZE) {
> +                error_setg(errp,
> +                           "%s: Invalid SEV-SNP ID auth block: incorrect size",
> +                           __func__);
> +                return -1;
> +            }

Perhaps add

  assert(policy_data1 != NULL);
  assert(policy_data2 != NULL);


> +            finish->id_block_uaddr =
> +                (__u64)g_malloc0(KVM_SEV_SNP_ID_BLOCK_SIZE);
> +            finish->id_authu_addr = (__u64)g_malloc0(KVM_SEV_SNP_ID_AUTH_SIZE);
> +            memcpy((void *)finish->id_block_uaddr, policy_data1,
> +                   KVM_SEV_SNP_ID_BLOCK_SIZE);
> +            memcpy((void *)finish->id_auth_uaddr, policy_data2,
> +                   KVM_SEV_SNP_ID_AUTH_SIZE);

How about using g_memdup2 ?

   finish->id_block_uaddr = (__u64)g_memdup2(policy_data1, KVM_SEV_SNP_ID_BLOCK_SIZE);
   finish->id_auth_uaddr = (__u64)g_memdup2(policy_data2, KVM_SEV_SNP_ID_AUTH_SIZE);

> +
> +            /*
> +             * Check if an author key has been provided and use that to flag
> +             * whether the author key is enabled. The first of the author key
> +             * must be non-zero to indicate the key type, which will currently
> +             * always be 2.
> +             */
> +            sev_snp_guest->kvm_finish_conf.auth_key_en =
> +                id_auth->author_key[0] ? 1 : 0;
> +            finish->id_block_en = 1;
> +        }
> +        sev_snp_guest->kvm_start_conf.policy = policy;
> +    } else {
> +        SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
> +        /* Only the policy flags are supported for SEV and SEV-ES */
> +        if ((policy_data1_size > 0) || (policy_data2_size > 0) || !sev_guest) {
> +            error_setg(errp, "%s: An ID block/ID auth block has been provided "
> +                             "but SEV-SNP is not supported", __func__);

Slightly more accurate to say  s/is not supported/is not enabled/

> +            return -1;
> +        }
> +        sev_guest->policy = policy;
> +    }
> +    return 0;
> +}
> +
>  static void
>  sev_common_class_init(ObjectClass *oc, void *data)
>  {
> @@ -2484,6 +2566,7 @@ sev_common_instance_init(Object *obj)
>      cgs->check_support = cgs_check_support;
>      cgs->set_guest_state = cgs_set_guest_state;
>      cgs->get_mem_map_entry = cgs_get_mem_map_entry;
> +    cgs->set_guest_policy = cgs_set_guest_policy;
>  
>      QTAILQ_INIT(&sev_common->launch_vmsa);
>  }
> -- 
> 2.43.0
> 

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 :|



  reply	other threads:[~2024-06-24 14:54 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21 14:29 [PATCH v3 00/15] Introduce support for IGVM files Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 01/15] meson: Add optional dependency on IGVM library Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 02/15] backends/confidential-guest-support: Add functions to support IGVM Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 03/15] backends/igvm: Add IGVM loader and configuration Roy Hopkins
2024-06-24 13:29   ` Daniel P. Berrangé
2024-06-28 10:59     ` Roy Hopkins
2024-06-27  9:06   ` Stefano Garzarella
2024-06-27  9:14     ` Daniel P. Berrangé
2024-06-28 11:00       ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 04/15] hw/core/machine: Add igvm-cfg object and processing for IGVM files Roy Hopkins
2024-06-24 14:00   ` Daniel P. Berrangé
2024-06-28 11:09     ` Roy Hopkins
2024-06-28 11:23       ` Daniel P. Berrangé
2024-07-01 11:59         ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 05/15] i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with IGVM Roy Hopkins
2024-06-27 12:38   ` Stefano Garzarella
2024-06-28 11:10     ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 06/15] sev: Update launch_update_data functions to use Error handling Roy Hopkins
2024-06-27 12:48   ` Stefano Garzarella
2024-06-28 11:20     ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 07/15] i386/sev: Refactor setting of reset vector and initial CPU state Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 08/15] i386/sev: Implement ConfidentialGuestSupport functions for SEV Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 09/15] docs/system: Add documentation on support for IGVM Roy Hopkins
2024-06-24 14:09   ` Daniel P. Berrangé
2024-07-01 14:28     ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 10/15] docs/interop/firmware.json: Add igvm to FirmwareDevice Roy Hopkins
2024-06-27 12:53   ` Stefano Garzarella
2024-07-02 10:36     ` Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 11/15] backends/confidential-guest-support: Add set_guest_policy() function Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 12/15] backends/igvm: Process initialization sections in IGVM file Roy Hopkins
2024-06-21 14:29 ` [PATCH v3 13/15] backends/igvm: Handle policy for SEV guests Roy Hopkins
2024-06-24 14:56   ` Daniel P. Berrangé
2024-06-21 14:29 ` [PATCH v3 14/15] i386/sev: Add implementation of CGS set_guest_policy() Roy Hopkins
2024-06-24 14:53   ` Daniel P. Berrangé [this message]
2024-06-21 14:29 ` [PATCH v3 15/15] sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2 Roy Hopkins
2024-06-24 14:14   ` Daniel P. Berrangé
2024-07-01 13:50     ` Roy Hopkins
2024-06-24 13:50 ` [PATCH v3 00/15] Introduce support for IGVM files Daniel P. Berrangé
2024-06-28 10:56   ` Roy Hopkins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZnmIhC5qF5il2NwW@redhat.com \
    --to=berrange@redhat.com \
    --cc=alistair@alistair23.me \
    --cc=anisinha@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=imammedo@redhat.com \
    --cc=jroedel@suse.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=michael.roth@amd.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=roy.hopkins@suse.com \
    --cc=sgarzare@redhat.com \
    --cc=slp@redhat.com \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.