From: Paolo Bonzini <pbonzini@redhat.com>
To: Sean Christopherson <seanjc@google.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, yang.zhong@intel.com
Subject: Re: [PATCH 2/3] KVM: x86: add system attribute to retrieve full set of supported xsave states
Date: Fri, 28 Jan 2022 13:33:55 +0100 [thread overview]
Message-ID: <abfd32df-b882-a1eb-b2a0-389fd6a68fac@redhat.com> (raw)
In-Reply-To: <YfK71pSnmtpnSJQ8@google.com>
On 1/27/22 16:35, Sean Christopherson wrote:
> On Wed, Jan 26, 2022, Paolo Bonzini wrote:
>> +static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr)
>> +{
>> + if (attr->group)
>> + return -ENXIO;
>> +
>> + switch (attr->attr) {
>> + case KVM_X86_XCOMP_GUEST_SUPP:
>> + if (put_user(supported_xcr0, (u64 __user *)attr->addr))
>
> Deja vu[*].
>
> arch/x86/kvm/x86.c: In function ‘kvm_x86_dev_get_attr’:
> arch/x86/kvm/x86.c:4345:46: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> | ^
> arch/x86/include/asm/uaccess.h:221:31: note: in definition of macro ‘do_put_user_call’
> 221 | register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX); \
> | ^~~
> arch/x86/kvm/x86.c:4345:21: note: in expansion of macro ‘put_user’
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> | ^~~~~~~~
> arch/x86/kvm/x86.c:4345:46: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> | ^
> arch/x86/include/asm/uaccess.h:223:21: note: in definition of macro ‘do_put_user_call’
> 223 | __ptr_pu = (ptr); \
> | ^~~
> arch/x86/kvm/x86.c:4345:21: note: in expansion of macro ‘put_user’
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> | ^~~~~~~~
> arch/x86/kvm/x86.c:4345:46: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> | ^
> arch/x86/include/asm/uaccess.h:230:45: note: in definition of macro ‘do_put_user_call’
> 230 | [size] "i" (sizeof(*(ptr))) \
> | ^~~
> arch/x86/kvm/x86.c:4345:21: note: in expansion of macro ‘put_user’
> 4345 | if (put_user(supported_xcr0, (u64 __user *)attr->addr))
>
> Given that we're collectively 2 for 2 in mishandling {g,s}et_attr(), what about
> a prep pacth like so? Compile tested only...
>
> From: Sean Christopherson <seanjc@google.com>
> Date: Thu, 27 Jan 2022 07:31:53 -0800
> Subject: [PATCH] KVM: x86: Add a helper to retrieve userspace address from
> kvm_device_attr
>
> Add a helper to handle converting the u64 userspace address embedded in
> struct kvm_device_attr into a userspace pointer, it's all too easy to
> forget the intermediate "unsigned long" cast as well as the truncation
> check.
>
> No functional change intended.
>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> arch/x86/kvm/x86.c | 28 +++++++++++++++++++++-------
> 1 file changed, 21 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 8033eca6f3a1..67836f7c71f5 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4335,14 +4335,28 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
> return r;
> }
>
> +static inline void __user *kvm_get_attr_addr(struct kvm_device_attr *attr)
> +{
> + void __user *uaddr = (void __user*)(unsigned long)attr->addr;
> +
> + if ((u64)(unsigned long)uaddr != attr->addr)
> + return ERR_PTR(-EFAULT);
> + return uaddr;
> +}
> +
> static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr)
> {
> + u64 __user *uaddr = kvm_get_attr_addr(attr);
> +
> if (attr->group)
> return -ENXIO;
>
> + if (IS_ERR(uaddr))
> + return PTR_ERR(uaddr);
> +
> switch (attr->attr) {
> case KVM_X86_XCOMP_GUEST_SUPP:
> - if (put_user(supported_xcr0, (u64 __user *)attr->addr))
> + if (put_user(supported_xcr0, uaddr))
> return -EFAULT;
> return 0;
> default:
> @@ -5070,11 +5084,11 @@ static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu,
> static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
> struct kvm_device_attr *attr)
> {
> - u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
> + u64 __user *uaddr = kvm_get_attr_addr(attr);
> int r;
>
> - if ((u64)(unsigned long)uaddr != attr->addr)
> - return -EFAULT;
> + if (IS_ERR(uaddr))
> + return PTR_ERR(uaddr);
>
> switch (attr->attr) {
> case KVM_VCPU_TSC_OFFSET:
> @@ -5093,12 +5107,12 @@ static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
> static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
> struct kvm_device_attr *attr)
> {
> - u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
> + u64 __user *uaddr = kvm_get_attr_addr(attr);
> struct kvm *kvm = vcpu->kvm;
> int r;
>
> - if ((u64)(unsigned long)uaddr != attr->addr)
> - return -EFAULT;
> + if (IS_ERR(uaddr))
> + return PTR_ERR(uaddr);
>
> switch (attr->attr) {
> case KVM_VCPU_TSC_OFFSET: {
> --
>
Nice, I applied it.
Paolo
next prev parent reply other threads:[~2022-01-28 12:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-26 15:22 [PATCH 0/3] KVM: x86: export supported_xcr0 via UAPI Paolo Bonzini
2022-01-26 15:22 ` [PATCH 1/3] selftests: kvm: move vm_xsave_req_perm call to amx_test Paolo Bonzini
2022-01-26 15:22 ` [PATCH 2/3] KVM: x86: add system attribute to retrieve full set of supported xsave states Paolo Bonzini
2022-01-27 15:35 ` Sean Christopherson
2022-01-28 2:41 ` Yang Zhong
2022-01-28 6:07 ` Yang Zhong
2022-01-28 12:33 ` Paolo Bonzini [this message]
2022-01-26 15:22 ` [PATCH 3/3] selftests: kvm: check dynamic bits against KVM_X86_XCOMP_GUEST_SUPP Paolo Bonzini
2022-01-27 5:39 ` [PATCH 0/3] KVM: x86: export supported_xcr0 via UAPI Yang Zhong
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=abfd32df-b882-a1eb-b2a0-389fd6a68fac@redhat.com \
--to=pbonzini@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=seanjc@google.com \
--cc=yang.zhong@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).