Skip to contentSkip to conversation

KVM: x86: Fix allocation sizeof argument

PATCH In review Colin King 4 participants 7 messages Oct 1, 2021 – Oct 6, 2021

Subsystems (4)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 KVM: x86: Fix allocation sizeof argument

From: Colin Ian King <[email protected]>

The allocation for *gfn_track should be for a slot->npages lot of short integers, however the current allocation is using sizeof(*gfn_track) and that is the size of a pointer, which is too large. Fix this by using sizeof(**gfn_track) instead.

Addresses-Coverity: ("Wrong sizeof argument")

Fixes 35b330bba6a7 ("KVM: x86: only allocate gfn_track when necessary")
---
 arch/x86/kvm/mmu/page_track.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/page_track.c +1 -1
index bb5d60bd4dbf..5b785a5f7dc9 100644
@@ -92,7 +92,7 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
 		slots = __kvm_memslots(kvm, i);
 		kvm_for_each_memslot(slot, slots) {
 			gfn_track = slot->arch.gfn_track + KVM_PAGE_TRACK_WRITE;
-			*gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
+			*gfn_track = kvcalloc(slot->npages, sizeof(**gfn_track),
 					      GFP_KERNEL_ACCOUNT);
 			if (*gfn_track == NULL) {
 				mutex_unlock(&kvm->slots_arch_lock);
Signature · 1 line
2.32.0
#1 lore raw
Quoted 19 lines · Colin King

From: Colin Ian King <[email protected]>

The allocation for *gfn_track should be for a slot->npages lot of short integers, however the current allocation is using sizeof(*gfn_track) and that is the size of a pointer, which is too large. Fix this by using sizeof(**gfn_track) instead.

Addresses-Coverity: ("Wrong sizeof argument")

Fixes 35b330bba6a7 ("KVM: x86: only allocate gfn_track when necessary")
---
 arch/x86/kvm/mmu/page_track.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/page_track.c +0 -0
index bb5d60bd4dbf..5b785a5f7dc9 100644

arch/x86/kvm/mmu/page_track.c int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm) · line 96

Quoted 6 lines
arch/x86/kvm/mmu/page_track.c +1 -1
@@ -92,7 +92,7 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
 		slots = __kvm_memslots(kvm, i);
 		kvm_for_each_memslot(slot, slots) {
 			gfn_track = slot->arch.gfn_track + KVM_PAGE_TRACK_WRITE;
-			*gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
+			*gfn_track = kvcalloc(slot->npages, sizeof(**gfn_track),
 					      GFP_KERNEL_ACCOUNT);

Eww (not your patch, the original code). IMO the double indirection is completely unnecessary, e.g. I find this far easier to follow

arch/x86/kvm/mmu/page_track.c +5 -5
index bb5d60bd4dbf..8cae41b831dd 100644
@@ -75,7 +75,7 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
 {
        struct kvm_memslots *slots;
        struct kvm_memory_slot *slot;
-       unsigned short **gfn_track;
+       unsigned short *gfn_track;
        int i;
 
        if (write_tracking_enabled(kvm))
@@ -91,13 +91,13 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
        for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
                slots = __kvm_memslots(kvm, i);
                kvm_for_each_memslot(slot, slots) {
-                       gfn_track = slot->arch.gfn_track + KVM_PAGE_TRACK_WRITE;
-                       *gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
-                                             GFP_KERNEL_ACCOUNT);
-                       if (*gfn_track == NULL) {
+                       gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
+                                            GFP_KERNEL_ACCOUNT);
+                       if (gfn_track == NULL) {
                                mutex_unlock(&kvm->slots_arch_lock);
                                return -ENOMEM;
                        }
+                       slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE] = gfn_track;
                }
        }
 

arch/x86/kvm/mmu/page_track.c int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm) · line 98

Quoted 2 lines
arch/x86/kvm/mmu/page_track.c +0 -0
 			if (*gfn_track == NULL) {
 				mutex_unlock(&kvm->slots_arch_lock);

Hrm, this fails to free the gfn_track allocations for previous memslots. The on-demand rmaps code has the exact same bug (it frees rmaps for previous lpages in the _current_ slot, but does not free previous slots).

And having two separate flows (and flags) for rmaps vs. gfn_track is pointless, and means we have to maintain two near-identical copies of non-obvious code.

Paolo, is it too late to just drop the original deae4a10f166 ("KVM: x86: only allocate gfn_track when necessary")?

Quoted 3 lines
Signature · 1 line
2.32.0
maintainer
#2 lore raw
Quoted 5 lines · Sean Christopherson
Quoted 2 lines
  			if (*gfn_track == NULL) {
  				mutex_unlock(&kvm->slots_arch_lock);

Hrm, this fails to free the gfn_track allocations for previous memslots. The on-demand rmaps code has the exact same bug (it frees rmaps for previous lpages in the_current_ slot, but does not free previous slots).

That's not a huge deal because the syscall is failing. So as long as it's not leaked forever, it's okay. The problem is the WARN_ON(slot->arch.rmap[i]), or the missing check in kvm_page_track_enable_mmu_write_tracking, but that's easily fixed. I'd even remove the call to memslot_rmaps_free.

Quoted 2 lines

And having two separate flows (and flags) for rmaps vs. gfn_track is pointless, and means we have to maintain two near-identical copies of non-obvious code.

I was thinking the separate flow (not so much the flag) is needed because, if KVMGT is enabled, gfn_track is allocated unconditionally. rmaps are added on top of that if shadow paging is enabled; but kvm_page_track_create_memslot will have already created the counter, including the one for KVM_PAGE_TRACK_WRITE.

But looking at the code again, I guess you could call kvm_page_track_enable_mmu_write_tracking inside alloc_all_memslots_rmaps (with a little bit of renaming), and with that the flag would go away.

I'll take a look tomorrow, but I'd rather avoid reverting the patch.

Thanks,

Paolo

Quoted 3 lines

Paolo, is it too late to just drop the original deae4a10f166 ("KVM: x86: only allocate gfn_track when necessary")?

#3 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
Quoted 2 lines
  			if (*gfn_track == NULL) {
  				mutex_unlock(&kvm->slots_arch_lock);

Hrm, this fails to free the gfn_track allocations for previous memslots. The on-demand rmaps code has the exact same bug (it frees rmaps for previous lpages in the_current_ slot, but does not free previous slots).

Quoted 5 lines

That's not a huge deal because the syscall is failing. So as long as it's not leaked forever, it's okay. The problem is the WARN_ON(slot->arch.rmap[i]), or the missing check in kvm_page_track_enable_mmu_write_tracking, but that's easily fixed. I'd even remove the call to memslot_rmaps_free.

It can be leaked forever though, e.g. if userspace invokes KVM_RUN over and over on -ENOMEM. That would trigger the WARN_ON(slot->arch.rmap[i]) and leak the previous allocation. I think it would be safe to change that WARN_ON to a check-and-continue, i.e. to preserve the previous allocation

Quoted 9 lines
Quoted 2 lines

And having two separate flows (and flags) for rmaps vs. gfn_track is pointless, and means we have to maintain two near-identical copies of non-obvious code.

I was thinking the separate flow (not so much the flag) is needed because, if KVMGT is enabled, gfn_track is allocated unconditionally. rmaps are added on top of that if shadow paging is enabled; but kvm_page_track_create_memslot will have already created the counter, including the one for KVM_PAGE_TRACK_WRITE.

Quoted 3 lines

But looking at the code again, I guess you could call kvm_page_track_enable_mmu_write_tracking inside alloc_all_memslots_rmaps (with a little bit of renaming), and with that the flag would go away.

Yes, and reuse the control flow, which is what I really care about since that's the part that both features get wrong.

Quoted 1 line

I'll take a look tomorrow, but I'd rather avoid reverting the patch.

I can poke at it too if you don't have time. I wasn't suggesting a full revert, rather a "drop and pretend it never got applied", with a plan to apply a new version instead of fixing up the current code.

maintainer
#4 lore raw
Quoted 3 lines · Sean Christopherson

I wasn't suggesting a full revert,
rather a "drop and pretend it never got applied", with a plan to apply a new version instead of fixing up the current code.

Considering that there are issues in the rmaps as well, I'd rather fix both the right way.

Paolo

#5 lore raw
In reply to Sean Christopherson
Quoted 67 lines · Sean Christopherson
Quoted 19 lines · Colin King

From: Colin Ian King <[email protected]>

The allocation for *gfn_track should be for a slot->npages lot of short integers, however the current allocation is using sizeof(*gfn_track) and that is the size of a pointer, which is too large. Fix this by using sizeof(**gfn_track) instead.

Addresses-Coverity: ("Wrong sizeof argument")

Fixes 35b330bba6a7 ("KVM: x86: only allocate gfn_track when necessary")
---
 arch/x86/kvm/mmu/page_track.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/page_track.c +0 -0
index bb5d60bd4dbf..5b785a5f7dc9 100644

arch/x86/kvm/mmu/page_track.c int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm) · line 96

Quoted 6 lines
arch/x86/kvm/mmu/page_track.c +1 -1
@@ -92,7 +92,7 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
              slots = __kvm_memslots(kvm, i);
              kvm_for_each_memslot(slot, slots) {
                      gfn_track = slot->arch.gfn_track + KVM_PAGE_TRACK_WRITE;
-                     *gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
+                     *gfn_track = kvcalloc(slot->npages, sizeof(**gfn_track),
                                            GFP_KERNEL_ACCOUNT);

Eww (not your patch, the original code). IMO the double indirection is completely unnecessary, e.g. I find this far easier to follow

arch/x86/kvm/mmu/page_track.c +5 -5
index bb5d60bd4dbf..8cae41b831dd 100644
@@ -75,7 +75,7 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
 {
        struct kvm_memslots *slots;
        struct kvm_memory_slot *slot;
-       unsigned short **gfn_track;
+       unsigned short *gfn_track;
        int i;
 
        if (write_tracking_enabled(kvm))
@@ -91,13 +91,13 @@ int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm)
        for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
                slots = __kvm_memslots(kvm, i);
                kvm_for_each_memslot(slot, slots) {
-                       gfn_track = slot->arch.gfn_track + KVM_PAGE_TRACK_WRITE;
-                       *gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
-                                             GFP_KERNEL_ACCOUNT);
-                       if (*gfn_track == NULL) {
+                       gfn_track = kvcalloc(slot->npages, sizeof(*gfn_track),
+                                            GFP_KERNEL_ACCOUNT);
+                       if (gfn_track == NULL) {
                                mutex_unlock(&kvm->slots_arch_lock);
                                return -ENOMEM;
                        }
+                       slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE] = gfn_track;
                }
        }
 

arch/x86/kvm/mmu/page_track.c int kvm_page_track_enable_mmu_write_tracking(struct kvm *kvm) · line 98

Quoted 4 lines
arch/x86/kvm/mmu/page_track.c +0 -0
                      if (*gfn_track == NULL) {
                              mutex_unlock(&kvm->slots_arch_lock);
Quoted 6 lines

Hrm, this fails to free the gfn_track allocations for previous memslots. The on-demand rmaps code has the exact same bug (it frees rmaps for previous lpages in the _current_ slot, but does not free previous slots).

And having two separate flows (and flags) for rmaps vs. gfn_track is pointless, and means we have to maintain two near-identical copies of non-obvious code.

I agree that's better than my patch. I can put together a new patch once it's decided whether or not my patch should be dropped.

-David

#6 lore raw
Quoted 10 lines · David Stevens
Quoted 6 lines · Sean Christopherson

Hrm, this fails to free the gfn_track allocations for previous memslots. The on-demand rmaps code has the exact same bug (it frees rmaps for previous lpages in the _current_ slot, but does not free previous slots).

And having two separate flows (and flags) for rmaps vs. gfn_track is pointless, and means we have to maintain two near-identical copies of non-obvious code.

I agree that's better than my patch. I can put together a new patch once it's decided whether or not my patch should be dropped.

All yours, unless Paolo wants to fight you for it :-) I'm totally ok doing cleanup/fixes on top.