Re: [PATCH v3] KVM: SEV: Add KVM_SEV_SNP_HV_REPORT_REQ command

From: xen

Date: Sat Jan 24 2026 - 15:29:33 EST



On 24-01-2026 17:27, Tom Lendacky wrote:
> On 1/24/26 08:40, Thomas Courrege wrote:
>> Sorry, i didn't saw the response, i changed the email i use.
>>
>> On 21-01-2026 00:45, Tom Lendacky wrote:
>>> On 12/15/25 08:14, Thomas Courrege wrote:
>>>
>>>> + size_t rsp_size = sizeof(*report_rsp);
>>>> + int ret;
>>> The declarations above should be in reverse fir tree order.
>>     
>> Like that ?
>>     struct sev_data_snp_msg_report_rsp *report_rsp;
>>     struct sev_data_snp_hv_report_req data;
>>     struct kvm_sev_snp_hv_report_req params;
>>     struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
>>     size_t rsp_size = sizeof(*report_rsp);
>>     void __user *u_report;
>>     void __user *u_params;
>>     int ret;
> struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
> struct sev_data_snp_msg_report_rsp *report_rsp;
> struct kvm_sev_snp_hv_report_req params;
> struct sev_data_snp_hv_report_req data;
> size_t rsp_size = sizeof(*report_rsp);
> void __user *u_report;
> void __user *u_params;
> int ret;
>
>>>> + if (ret)
>>>> + goto e_free_rsp;
>>>> +
>>>> + if (!report_rsp->status)
>>>> + rsp_size += report_rsp->report_size;
>>>> +
>>>> + if (params.report_len < rsp_size) {
>>>> + rsp_size = sizeof(*report_rsp);
>>>> + ret = -ENOSPC;
>>>> + }
>>> This can be contained within the if above it, right?
>>>
>>> if (!report_rsp->status) {
>>> if (params.report_len < (rsp_size + report_rsp->report_size))
>>> ret = -ENOSPC;
>>> else
>>> rsp_size += report_rsp->report_size;
>>> }
>> This leads to an error in case the user wants to query the report size.
>>
>>
>> Using params.report_len = 32, the nested if is true and thus the user get
>>
>> back the default rsp_size (= 32), not increased with report_size (= 1184).
> But isn't params.report_len set below to the proper value since it wasn't
> using rsp_size? The rsp_size variable is only used for the copy_to_user()
> for the report itself. Assuming you want to copy what's in 'rsp' no matter
> the return code you get, then can't you just do:
>
> if (!report_rsp->status) {
> if (params.report_len < (rsp_size + report_rsp->report_size))
> ret = -ENOSPC;
> else
> rsp_size += report_rsp->report_size;
>
> params.report_len = sizeof(*report_rsp) + report_rsp->report_size;
> }
>
> if (copy_to_user(u_report, report_rsp, rsp_size))
> ret = -EFAULT;
>
> Thanks,
> Tom
That's a good solution, thank you.

I'll also add a patch note for the next one.

Thanks,
Thomas
>>>> +
>>>> + if (copy_to_user(u_report, report_rsp, rsp_size))
>>>> + ret = -EFAULT;
>>>> +
>>>> + params.report_len = sizeof(*report_rsp) + report_rsp->report_size;
>>> I'm not sure if we can rely on report_rsp->report_size being valid if
>>> resport_rsp->status is not zero. So maybe just set this to rsp_size.
>>>
>>> Thanks,
>>> Tom
>> maybe something like this ? to avoid copying on ENOSPC, where this issue come from
>>
>>     if (!report_rsp->status)
>>         rsp_size += report_rsp->report_size;
>>
>>     if (params.report_len < rsp_size) {
>>         ret = -ENOSPC;
>>     } else {
>>         if (copy_to_user(u_report, report_rsp, rsp_size))
>>             ret = -EFAULT;
>>     }
>>
>>     params.report_len = rsp_size;
>>
>>
>> To test this specific case : 
>>     https://github.com/Th0rOnDoR/test-length-sev/blob/main/sev_test.c
>>
>> Thanks, 
>> Thomas
>