Parfait changes
cover Parfait changes
The following patch(s) are bugs found by the static compiler 'Parfait'. Care was taken to make sure false positive results were removed from this patchset.
Parfait Overview
================
https://labs.oracle.com/pls/apex/f?p=labs:49:::::P49_PROJECT_ID:13
v1:
Initial release
Joe Moriarty (1):
KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476)
problem
arch/x86/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 1 line
2.15.0
PATCH 1/1 KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
The Parfait (version 2.1.0) static code analysis tool found the following NULL pointer dereference problem.
- arch/x86/kvm/mmu.c
There is a possibility that the call to __gfn_to_rmap() can happen with a NULL pointer given for the slot argument. This can happen if the slot information cannot be determined from a previous call to __gfn_to_memslot(). The base_gfn will be passed in as 0 to gfn_to_index if no valid slot information can be obtained from a call to __gfn_to_memslot().
--- arch/x86/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu.c +1 -1
@@ -1239,7 +1239,7 @@ static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, {unsigned long idx; - idx =gfn_to_index (gfn, slot->base_gfn, level); + idx =gfn_to_index (gfn, slot ? slot->base_gfn :0 , level);return &slot->arch.rmap[level -PT_PAGE_TABLE_LEVEL ][idx]; }
Signature · 1 line
2.15.0
Paolo Bonzini
maintainerRe: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
Quoted 10 lines · Joe Moriarty
The Parfait (version 2.1.0) static code analysis tool found the following NULL pointer dereference problem.
-
arch/x86/kvm/mmu.c
There is a possibility that the call to__gfn_to_rmap()can happen with a NULL pointer given for the slot argument. This can happen if the slot information cannot be determined from a previous call to__gfn_to_memslot(). Thebase_gfnwill be passed in as 0 togfn_to_indexif no valid slot information can be obtained from a call to__gfn_to_memslot().
How does it justify the possible NULL pointer dereference?
The fix below is incorrect anyway, since it would just get a random page out of guest memory; please report the issue without providing a patch if you do not understand the code well.
I can't exclude there is a race condition here that can lead to a NULL pointer dereference. However, it should be handled by passing the struct kvm_memory_slot all the way down to __gfn_to_rmap, i.e. adding a new argument to gfn_to_rmap, rmap_add, mmu_set_spte, __direct_map, try_async_pf and many more functions. That's not as easy as the patch you posted. :)
Thanks,
Paolo
Quoted 19 lines
Signed-off-by Joe Moriarty <[email protected]>--- arch/x86/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/mmu.c +1
-1@@ -1239,7 +1239,7 @@ static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, {unsigned long idx; - idx =gfn_to_index (gfn, slot->base_gfn, level); + idx =gfn_to_index (gfn, slot ? slot->base_gfn :0 , level);return &slot->arch.rmap[level -PT_PAGE_TABLE_LEVEL ][idx]; }
Re: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
Quoted 42 lines · Paolo Bonzini
Quoted 10 lines · Joe Moriarty
The Parfait (version 2.1.0) static code analysis tool found the following NULL pointer dereference problem.
-
arch/x86/kvm/mmu.c
There is a possibility that the call to__gfn_to_rmap()can happen with a NULL pointer given for the slot argument. This can happen if the slot information cannot be determined from a previous call to__gfn_to_memslot(). Thebase_gfnwill be passed in as 0 togfn_to_indexif no valid slot information can be obtained from a call to__gfn_to_memslot().How does it justify the possible NULL pointer dereference?
The fix below is incorrect anyway, since it would just get a random page out of guest memory; please report the issue without providing a patch if you do not understand the code well.
I can't exclude there is a race condition here that can lead to a NULL pointer dereference. However, it should be handled by passing the struct
kvm_memory_slotall the way down to__gfn_to_rmap, i.e. adding a new argument togfn_to_rmap,rmap_add,mmu_set_spte,__direct_map,try_async_pfand many more functions. That's not as easy as the patch you posted. :)Thanks,
Paolo
Quoted 13 lines
Signed-off-by Joe Moriarty <[email protected]>--- arch/x86/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/mmu.c +0
-0@@ -1239,7 +1239,7 @@ static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, {unsigned long idx;
Quoted 7 lines
arch/x86/kvm/mmu.c static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, · line 1245
Quoted 6 lines
arch/x86/kvm/mmu.c +1
-1- idx =gfn_to_index (gfn, slot->base_gfn, level); + idx =gfn_to_index (gfn, slot ? slot->base_gfn :0 , level);return &slot->arch.rmap[level -PT_PAGE_TABLE_LEVEL ][idx]; }
Hi Paolo,
Thank you for the review. I wasn't sure if the change I proposed was correct or not. I will take your suggestion of posting to the mailing list instead of as a patch the next time I encounter a situation like this again. In the meantime, I will look at doing your suggestion of passing kvm_memory_slot down to gfn_to_rmap() and the other functions you pointed out above for the next version of the patch.
Joe
Paolo Bonzini
maintainerRe: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
Quoted 9 lines · Joe Moriarty
Quoted 1 line
Hi Paolo,
Thank you for the review. I wasn't sure if the change I proposed was correct or not. I will take your suggestion of posting to the mailing list instead of as a patch the next time I encounter a situation like this again. In the meantime, I will look at doing your suggestion of passing
kvm_memory_slotdown togfn_to_rmap()and the other functions you pointed out above for the next version of the patch.
It's not easy, but I can send you a box of beers if you get round to it.
Note that I'm still not sure how the NULL pointer dereference can
happen, and you didn't include more output from your tool, so you might be wasting your time after all...
Anyway, I would start basically by mapping the paths from try_async_pf's callers to mmu_set_spte and from there to rmap_add.
On the other hand, in the rmap_remove path, you probably should just exit immediately if slot is NULL. (Guangrong, do you have any idea why we don't zap SPTEs in kvm_arch_free_memslot?)
Paolo
Re: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
Quoted 16 lines · Paolo Bonzini
Quoted 9 lines · Joe Moriarty
Quoted 1 line
Hi Paolo,
Thank you for the review. I wasn't sure if the change I proposed was correct or not. I will take your suggestion of posting to the mailing list instead of as a patch the next time I encounter a situation like this again. In the meantime, I will look at doing your suggestion of passing
kvm_memory_slotdown togfn_to_rmap()and the other functions you pointed out above for the next version of the patch.It's not easy, but I can send you a box of beers if you get round to it.
Note that I'm still not sure how the NULL pointer dereference can
happen, and you didn't include more output from your tool, so you might be wasting your time after all...
Quoted 9 lines
Anyway, I would start basically by mapping the paths from try_async_pf's callers to
mmu_set_spteand from there tormap_add.On the other hand, in the
rmap_removepath, you probably should just exit immediately if slot is NULL. (Guangrong, do you have any idea why we don't zap SPTEs inkvm_arch_free_memslot?)Paolo
The crux of the problem is the possible return of NULL from search_memslots() located at line 950 in include/linux/kvm_host.h. This is called by __gfn_to_memslot(). I would assume this is a case of this is never going to happen. I say this because the kernel would panic today with a NULL pointer dereference at line 1254 of arch/x86/kvm/mmu.c if it did happen.
I was thinking a BUG_ON() would be more appropriate at the end of search_memslots() before the "return NULL". That is if my aforementioned assumption is correct about the code path. If not, then continue on with the suggested fix of passing down kvm_memory_slot to gfn_to_rmap().
Let me know which solution you would like.
Joe
Paolo Bonzini
maintainerRe: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem
----- Original Message -----
Quoted 30 lines
From: "Joe Moriarty" <[email protected]>
To: "Paolo Bonzini" <[email protected]>, [email protected], [email protected], "Xiao Guangrong" <[email protected]>
Sent: Wednesday, February 14, 2018 7:14:09 PM
Subject: Re: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problemQuoted 16 lines · Paolo Bonzini
Quoted 9 lines · Joe Moriarty
Quoted 1 line
Hi Paolo,
Thank you for the review. I wasn't sure if the change I proposed was correct or not. I will take your suggestion of posting to the mailing list instead of as a patch the next time I encounter a situation like this again. In the meantime, I will look at doing your suggestion of passing
kvm_memory_slotdown togfn_to_rmap()and the other functions you pointed out above for the next version of the patch.It's not easy, but I can send you a box of beers if you get round to it.
Note that I'm still not sure how the NULL pointer dereference can
happen, and you didn't include more output from your tool, so you might be wasting your time after all...Quoted 6 lines
Anyway, I would start basically by mapping the paths from try_async_pf's callers to
mmu_set_spteand from there tormap_add.On the other hand, in the
rmap_removepath, you probably should just exit immediately if slot is NULL. (Guangrong, do you have any idea why we don't zap SPTEs inkvm_arch_free_memslot?)
Quoted 6 lines
The crux of the problem is the possible return of NULL from
search_memslots()located at line 950 ininclude/linux/kvm_host.h. This is called by__gfn_to_memslot(). I would assume this is a case of this is never going to happen. I say this because the kernel would panic today with a NULL pointer dereference at line 1254 ofarch/x86/kvm/mmu.cif it did happen.
I cannot really rule out that it won't happen. I didn't have in mind that NULL-pointer dereference, but it's always looked racy to me---I've never gotten round to fix it because I wasn't sure about the right thing to do on the rmap_remove path.
Quoted 2 lines
I was thinking a
BUG_ON()would be more appropriate at the end ofsearch_memslots()before the "return NULL".
No, this is probably too much. The right fix is to pass down the memslot, thus avoiding gfn_to_rmap in favor of __gfn_to_rmap.
Paolo