Re: [PATCH v3 01/11] KVM: SVM: Add KVM_SEV SEND_START command

From: Brijesh Singh
Date: Tue Nov 12 2019 - 17:27:31 EST



On 11/12/19 12:35 PM, Peter Gonda wrote:
> On Wed, Jul 10, 2019 at 1:13 PM Singh, Brijesh <brijesh.singh@xxxxxxx> wrote:
>> +static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
>> +{
>> + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
>> + void *amd_cert = NULL, *session_data = NULL;
>> + void *pdh_cert = NULL, *plat_cert = NULL;
>> + struct sev_data_send_start *data = NULL;
>> + struct kvm_sev_send_start params;
>> + int ret;
>> +
>> + if (!sev_guest(kvm))
>> + return -ENOTTY;
>> +
>> + if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
>> + sizeof(struct kvm_sev_send_start)))
>> + return -EFAULT;
>> +
>> + data = kzalloc(sizeof(*data), GFP_KERNEL);
>> + if (!data)
>> + return -ENOMEM;
>> +
>> + /* userspace wants to query the session length */
>> + if (!params.session_len)
>> + goto cmd;
>> +
>> + if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
>> + !params.session_uaddr)
>> + return -EINVAL;
> I think pdh_cert is only required if the guest policy SEV bit is set.
> Can pdh_cert be optional?


We don't cache the policy information in kernel, having said so we can
try caching it during the LAUNCH_START to optimize this case. I have to
check with FW folks but I believe all those fields are required. IIRC,
When I passed NULL then SEND_START failed for me. But I double check it
and update you on this.


>
>> +
>> + /* copy the certificate blobs from userspace */
>> + pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr, params.pdh_cert_len);
>> + if (IS_ERR(pdh_cert)) {
>> + ret = PTR_ERR(pdh_cert);
>> + goto e_free;
>> + }
>> +
>> + data->pdh_cert_address = __psp_pa(pdh_cert);
>> + data->pdh_cert_len = params.pdh_cert_len;
>> +
>> + plat_cert = psp_copy_user_blob(params.plat_cert_uaddr, params.plat_cert_len);
>> + if (IS_ERR(plat_cert)) {
>> + ret = PTR_ERR(plat_cert);
>> + goto e_free_pdh;
>> + }
> I think plat_cert is also only required if the guest policy SEV bit is
> set. Can plat_cert also be optional?


Same as above, I believe its required.


>
>> +
>> + data->plat_cert_address = __psp_pa(plat_cert);
>> + data->plat_cert_len = params.plat_cert_len;
>> +
>> + amd_cert = psp_copy_user_blob(params.amd_cert_uaddr, params.amd_cert_len);
>> + if (IS_ERR(amd_cert)) {
>> + ret = PTR_ERR(amd_cert);
>> + goto e_free_plat_cert;
>> + }
> I think amd_cert is also only required if the guest policy SEV bit is
> set. Can amd_cert also be optional?


Same as above, I believe its required. I will double check it.


>> +
>> + data->amd_cert_address = __psp_pa(amd_cert);
>> + data->amd_cert_len = params.amd_cert_len;
>> +
>> + ret = -EINVAL;
>> + if (params.session_len > SEV_FW_BLOB_MAX_SIZE)
>> + goto e_free_amd_cert;
>> +
>> + ret = -ENOMEM;
>> + session_data = kmalloc(params.session_len, GFP_KERNEL);
>> + if (!session_data)
>> + goto e_free_amd_cert;
> This pattern of returning -EINVAL if a length is greater than
> SEV_FW_BLOB_MAX_SIZE and -ENOMEM if kmalloc fails is used at
> sev_launch_measure. And I think in your later patches you do similar,
> did you consider factoring this out into a helper function similar to
> psp_copy_user_blob?


Yes, we could factor out this check into a separate function. Let me see
what I can do in next iteration. thanks for the feedbacks.

-Brijesh