Skip to content

Commit ff2bd9f

Browse files
Dan Carpenterbonzini
authored andcommitted
KVM: SVM: Fix sev_pin_memory() error handling
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: a8d908b ("KVM: x86: report sev_pin_memory errors with PTR_ERR") Signed-off-by: Dan Carpenter <[email protected]> Message-Id: <20200714142351.GA315374@mwanda> Reviewed-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent a445fc4 commit ff2bd9f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
318318
unsigned long locked, lock_limit;
319319
struct page **pages;
320320
unsigned long first, last;
321+
int ret;
321322

322323
if (ulen == 0 || uaddr + ulen < uaddr)
323324
return ERR_PTR(-EINVAL);
@@ -351,6 +352,7 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
351352
npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
352353
if (npinned != npages) {
353354
pr_err("SEV: Failure locking %lu pages.\n", npages);
355+
ret = -ENOMEM;
354356
goto err;
355357
}
356358

@@ -360,13 +362,11 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
360362
return pages;
361363

362364
err:
363-
if (npinned > 0) {
365+
if (npinned > 0)
364366
unpin_user_pages(pages, npinned);
365-
npinned = -ENOMEM;
366-
}
367367

368368
kvfree(pages);
369-
return ERR_PTR(npinned);
369+
return ERR_PTR(ret);
370370
}
371371

372372
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)
440440

441441
/* Lock the user memory. */
442442
inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
443-
if (!inpages) {
444-
ret = -ENOMEM;
443+
if (IS_ERR(inpages)) {
444+
ret = PTR_ERR(inpages);
445445
goto e_free;
446446
}
447447

@@ -795,13 +795,13 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
795795

796796
/* lock userspace source and destination page */
797797
src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
798-
if (!src_p)
799-
return -EFAULT;
798+
if (IS_ERR(src_p))
799+
return PTR_ERR(src_p);
800800

801801
dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
802-
if (!dst_p) {
802+
if (IS_ERR(dst_p)) {
803803
sev_unpin_memory(kvm, src_p, n);
804-
return -EFAULT;
804+
return PTR_ERR(dst_p);
805805
}
806806

807807
/*

0 commit comments

Comments
 (0)