KVM Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] KVM: SVM: Fix sev_pin_memory() error handling
@ 2020-07-14 14:23 Dan Carpenter
  2020-07-31 20:18 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2020-07-14 14:23 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm, kernel-janitors

The sev_pin_memory() function was modified to return error pointers
instead of NULL but there are two problems.  The first problem is that
if "npages" is zero then it still returns NULL.  Secondly, several of
the callers were not updated to check for error pointers instead of
NULL.

Either one of these issues will lead to an Oops.

Fixes: a8d908b5873c ("KVM: x86: report sev_pin_memory errors with PTR_ERR")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 arch/x86/kvm/svm/sev.c   |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index f7f1f4ecf08e..402dc4234e39 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -318,6 +318,7 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 	unsigned long locked, lock_limit;
 	struct page **pages;
 	unsigned long first, last;
+	int ret;
 
 	if (ulen == 0 || uaddr + ulen < uaddr)
 		return ERR_PTR(-EINVAL);
@@ -351,6 +352,7 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 	npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
 	if (npinned != npages) {
 		pr_err("SEV: Failure locking %lu pages.\n", npages);
+		ret = -ENOMEM;
 		goto err;
 	}
 
@@ -360,13 +362,11 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 	return pages;
 
 err:
-	if (npinned > 0) {
+	if (npinned > 0)
 		unpin_user_pages(pages, npinned);
-		npinned = -ENOMEM;
-	}
 
 	kvfree(pages);
-	return ERR_PTR(npinned);
+	return ERR_PTR(ret);
 }
 
 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
@@ -440,8 +440,8 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	/* Lock the user memory. */
 	inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
-	if (!inpages) {
-		ret = -ENOMEM;
+	if (IS_ERR(inpages)) {
+		ret = PTR_ERR(inpages);
 		goto e_free;
 	}
 
@@ -795,13 +795,13 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
 
 		/* lock userspace source and destination page */
 		src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
-		if (!src_p)
-			return -EFAULT;
+		if (IS_ERR(src_p))
+			return PTR_ERR(src_p);
 
 		dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
-		if (!dst_p) {
+		if (IS_ERR(dst_p)) {
 			sev_unpin_memory(kvm, src_p, n);
-			return -EFAULT;
+			return PTR_ERR(dst_p);
 		}
 
 		/*

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

* Re: [PATCH] KVM: SVM: Fix sev_pin_memory() error handling
  2020-07-14 14:23 [PATCH] KVM: SVM: Fix sev_pin_memory() error handling Dan Carpenter
@ 2020-07-31 20:18 ` Sean Christopherson
  2020-08-03 10:27   ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2020-07-31 20:18 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm, kernel-janitors

On Tue, Jul 14, 2020 at 05:23:51PM +0300, Dan Carpenter wrote:
> The sev_pin_memory() function was modified to return error pointers
> instead of NULL but there are two problems.  The first problem is that
> if "npages" is zero then it still returns NULL.  Secondly, several of
> the callers were not updated to check for error pointers instead of
> NULL.
> 
> Either one of these issues will lead to an Oops.
> 
> Fixes: a8d908b5873c ("KVM: x86: report sev_pin_memory errors with PTR_ERR")

Explicit Cc: to stable needed for KVM patches.

> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Sean Christopherson <sean.j.christopherson@intel.com>

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

* Re: [PATCH] KVM: SVM: Fix sev_pin_memory() error handling
  2020-07-31 20:18 ` Sean Christopherson
@ 2020-08-03 10:27   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2020-08-03 10:27 UTC (permalink / raw)
  To: Sean Christopherson, Dan Carpenter
  Cc: Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm, kernel-janitors

On 31/07/20 22:18, Sean Christopherson wrote:
> On Tue, Jul 14, 2020 at 05:23:51PM +0300, Dan Carpenter wrote:
>> The sev_pin_memory() function was modified to return error pointers
>> instead of NULL but there are two problems.  The first problem is that
>> if "npages" is zero then it still returns NULL.  Secondly, several of
>> the callers were not updated to check for error pointers instead of
>> NULL.
>>
>> Either one of these issues will lead to an Oops.
>>
>> Fixes: a8d908b5873c ("KVM: x86: report sev_pin_memory errors with PTR_ERR")
> 
> Explicit Cc: to stable needed for KVM patches.
> 
>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> Reviewed-by: Sean Christopherson <sean.j.christopherson@intel.com>
> 

Fortunately it's only broken in 5.9.  Queued, thanks.

Paolo


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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-14 14:23 [PATCH] KVM: SVM: Fix sev_pin_memory() error handling Dan Carpenter
2020-07-31 20:18 ` Sean Christopherson
2020-08-03 10:27   ` Paolo Bonzini

KVM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kvm/0 kvm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kvm kvm/ https://lore.kernel.org/kvm \
		kvm@vger.kernel.org
	public-inbox-index kvm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.kvm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git