Re: [PATCH v2] KVM: nSVM: Check full nested state size before writing to user buffer

From: Shivansh Dhiman

Date: Tue Nov 17 2026 - 02:25:43 EST


On 13-11-26 10:31, Priyanka Mani wrote:
> svm_get_nested_state() compares the userspace-provided buffer size against
> kvm_state.size before adding KVM_STATE_NESTED_SVM_VMCB_SIZE for a vCPU that
> is in guest mode. When L2 is active and userspace passes a size that is
> large enough for the header but too small for the vmcb12 payload, KVM thus
> skips the early exit, copies out the header, clears and fills the full 4KiB
> vmcb12 area past the end of the buffer described by userspace, and only
> then does the caller notice that the returned size is larger than the
> provided one and fail with -E2BIG.
>
> As a result, KVM writes up to 4KiB beyond the buffer userspace said it
> has, and if that memory isn't mapped, the ioctl fails with -EFAULT instead
> of the documented -E2BIG without reporting the required size.
>
> Move the size check to immediately before the first copy_to_user(), after
> the header, including kvm_state.size, has been fully filled in, so that a
> too-small buffer is rejected without writing anything to it, and so that
> any future additions to the size are automatically covered by the check.

Tested the patch by passing various values in user_data_size (0, 127,
128, 129, 4223 and 4224) while L2 is active. Now it only updates the
size field and returns -E2BIG for every too-small buffer, and succeeds
with the full 4224 bytes.

Tested-by: Shivansh Dhiman <shivansh.dhiman@xxxxxxx>

Thanks,
Shivansh