kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Hoist input checks in kvm_add_msr_filter()
@ 2021-05-03 12:21 Siddharth Chandrasekaran
  2021-05-03 20:53 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Siddharth Chandrasekaran @ 2021-05-03 12:21 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, x86, H. Peter Anvin
  Cc: Siddharth Chandrasekaran, kvm, linux-kernel

In ioctl KVM_X86_SET_MSR_FILTER, input from user space is validated
after a memdup_user(). For invalid inputs we'd memdup and then call
kfree unnecessarily. Hoist input validation to avoid kfree altogether.

Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
---
 arch/x86/kvm/x86.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ee0dc58ac3a5..15c20b31cc91 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5393,11 +5393,16 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
 	struct msr_bitmap_range range;
 	unsigned long *bitmap = NULL;
 	size_t bitmap_size;
-	int r;
 
 	if (!user_range->nmsrs)
 		return 0;
 
+	if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
+		return -EINVAL;
+
+	if (!user_range->flags)
+		return -EINVAL;
+
 	bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
 	if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
 		return -EINVAL;
@@ -5413,24 +5418,10 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
 		.bitmap = bitmap,
 	};
 
-	if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) {
-		r = -EINVAL;
-		goto err;
-	}
-
-	if (!range.flags) {
-		r = -EINVAL;
-		goto err;
-	}
-
-	/* Everything ok, add this range identifier. */
 	msr_filter->ranges[msr_filter->count] = range;
 	msr_filter->count++;
 
 	return 0;
-err:
-	kfree(bitmap);
-	return r;
 }
 
 static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: x86: Hoist input checks in kvm_add_msr_filter()
  2021-05-03 12:21 [PATCH] KVM: x86: Hoist input checks in kvm_add_msr_filter() Siddharth Chandrasekaran
@ 2021-05-03 20:53 ` Sean Christopherson
  2021-05-03 21:17   ` Siddharth Chandrasekaran
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2021-05-03 20:53 UTC (permalink / raw)
  To: Siddharth Chandrasekaran
  Cc: Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm, linux-kernel

On Mon, May 03, 2021, Siddharth Chandrasekaran wrote:
> In ioctl KVM_X86_SET_MSR_FILTER, input from user space is validated
> after a memdup_user(). For invalid inputs we'd memdup and then call
> kfree unnecessarily. Hoist input validation to avoid kfree altogether.
> 
> Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
> ---
>  arch/x86/kvm/x86.c | 21 ++++++---------------
>  1 file changed, 6 insertions(+), 15 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index ee0dc58ac3a5..15c20b31cc91 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -5393,11 +5393,16 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
>  	struct msr_bitmap_range range;
>  	unsigned long *bitmap = NULL;
>  	size_t bitmap_size;
> -	int r;
>  
>  	if (!user_range->nmsrs)
>  		return 0;
>  
> +	if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
> +		return -EINVAL;
> +
> +	if (!user_range->flags)
> +		return -EINVAL;
> +
>  	bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
>  	if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
>  		return -EINVAL;
> @@ -5413,24 +5418,10 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
>  		.bitmap = bitmap,
>  	};
>  
> -	if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) {
> -		r = -EINVAL;
> -		goto err;
> -	}
> -
> -	if (!range.flags) {
> -		r = -EINVAL;
> -		goto err;
> -	}
> -
> -	/* Everything ok, add this range identifier. */
>  	msr_filter->ranges[msr_filter->count] = range;

Might be worth elminating the intermediate "range", too.  Doesn't affect output,
but it would make it a little more obvious that the new range is mostly coming
straight from userspace input.  E.g.

	msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) {
		.flags = user_range->flags,
		.base = user_range->base,
		.nmsrs = user_range->nmsrs,
		.bitmap = bitmap,
	};

Either way:

Reviewed-by: Sean Christopherson <seanjc@google.com>

>  	msr_filter->count++;
>  
>  	return 0;
> -err:
> -	kfree(bitmap);
> -	return r;
>  }
>  
>  static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
> -- 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: x86: Hoist input checks in kvm_add_msr_filter()
  2021-05-03 20:53 ` Sean Christopherson
@ 2021-05-03 21:17   ` Siddharth Chandrasekaran
  0 siblings, 0 replies; 3+ messages in thread
From: Siddharth Chandrasekaran @ 2021-05-03 21:17 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm, linux-kernel

On Mon, May 03, 2021 at 08:53:09PM +0000, Sean Christopherson wrote:
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
> 
> 
> 
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
> 
> On Mon, May 03, 2021, Siddharth Chandrasekaran wrote:
> > In ioctl KVM_X86_SET_MSR_FILTER, input from user space is validated
> > after a memdup_user(). For invalid inputs we'd memdup and then call
> > kfree unnecessarily. Hoist input validation to avoid kfree altogether.
> >
> > Signed-off-by: Siddharth Chandrasekaran <sidcha@amazon.de>
> > ---
> >  arch/x86/kvm/x86.c | 21 ++++++---------------
> >  1 file changed, 6 insertions(+), 15 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index ee0dc58ac3a5..15c20b31cc91 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -5393,11 +5393,16 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
> >       struct msr_bitmap_range range;
> >       unsigned long *bitmap = NULL;
> >       size_t bitmap_size;
> > -     int r;
> >
> >       if (!user_range->nmsrs)
> >               return 0;
> >
> > +     if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE))
> > +             return -EINVAL;
> > +
> > +     if (!user_range->flags)
> > +             return -EINVAL;
> > +
> >       bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long);
> >       if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE)
> >               return -EINVAL;
> > @@ -5413,24 +5418,10 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,
> >               .bitmap = bitmap,
> >       };
> >
> > -     if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) {
> > -             r = -EINVAL;
> > -             goto err;
> > -     }
> > -
> > -     if (!range.flags) {
> > -             r = -EINVAL;
> > -             goto err;
> > -     }
> > -
> > -     /* Everything ok, add this range identifier. */
> >       msr_filter->ranges[msr_filter->count] = range;
> 
> Might be worth elminating the intermediate "range", too.  Doesn't affect output,
> but it would make it a little more obvious that the new range is mostly coming
> straight from userspace input.  E.g.
> 
>         msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) {
>                 .flags = user_range->flags,
>                 .base = user_range->base,
>                 .nmsrs = user_range->nmsrs,
>                 .bitmap = bitmap,
>         };
> 
> Either way:
> 
> Reviewed-by: Sean Christopherson <seanjc@google.com>

Thanks for the review. Changed as suggested.

~ Sid.



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-03 21:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-03 12:21 [PATCH] KVM: x86: Hoist input checks in kvm_add_msr_filter() Siddharth Chandrasekaran
2021-05-03 20:53 ` Sean Christopherson
2021-05-03 21:17   ` Siddharth Chandrasekaran

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).