Skip to contentSkip to conversation

KVM: SEV: Clear the pages pointer in sev_unpin_memory

PATCH In review Peter Gonda 3 participants 4 messages Jun 27, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: SEV: Clear the pages pointer in sev_unpin_memory

Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a dangling pointer to invalid memory.

Cc Greg Thelen <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kvm/svm/sev.c +1 -0
index 309bcdb2f929..485ad86c01c6 100644
@@ -452,6 +452,7 @@ static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
 	unpin_user_pages(pages, npages);
 	kvfree(pages);
 	sev->pages_locked -= npages;
+	*pages = NULL;
 }
 
 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#1 lore raw
Quoted 19 lines · Peter Gonda

Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a dangling pointer to invalid memory.

Cc Greg Thelen <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kvm/svm/sev.c +0 -0
index 309bcdb2f929..485ad86c01c6 100644

arch/x86/kvm/svm/sev.c static void sev_unpin_memory(struct kvm *kvm, struct page **pages, · line 455

Quoted 4 lines
arch/x86/kvm/svm/sev.c +1 -0
@@ -452,6 +452,7 @@ static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
 	unpin_user_pages(pages, npages);
 	kvfree(pages);
 	sev->pages_locked -= npages;
+	*pages = NULL;

Would this have helped detect a real bug? I generally like cleaning up, but this leaves things in a somewhat inconsistent state, e.g. when unpinning a kvm_enc_region, pages will be NULL but npages will be non-zero. It's somewhat moot because the region is immediately freed in that case, but that begs the question of what real benefit this provides. sev_dbg_crypt() is the only flow where there's much danger of a use-after-free.

Quoted 6 lines
arch/x86/kvm/svm/sev.c +0 -0
 }
 
 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#2 lore raw
Quoted 16 lines · Sean Christopherson
Quoted 14 lines · Peter Gonda

Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a dangling pointer to invalid memory.

Cc Greg Thelen <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 17 lines

arch/x86/kvm/svm/sev.c static void sev_unpin_memory(struct kvm *kvm, struct page **pages, · line 455

Quoted 9 lines
arch/x86/kvm/svm/sev.c +1 -0
index 309bcdb2f929..485ad86c01c6 100644
@@ -452,6 +452,7 @@ static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
      unpin_user_pages(pages, npages);
      kvfree(pages);
      sev->pages_locked -= npages;
+     *pages = NULL;

Would this have helped detect a real bug? I generally like cleaning up, but this leaves things in a somewhat inconsistent state, e.g. when unpinning a kvm_enc_region, pages will be NULL but npages will be non-zero. It's somewhat moot because the region is immediately freed in that case, but that begs the question of what real benefit this provides. sev_dbg_crypt() is the only flow where there's much danger of a use-after-free.

No strong opinion here, I just thought since this is a helper that takes a 'struct page **pages" we may as well clear this. While there are no bugs caught now if someone were to introduce something wrong this would make it more clear.

We could update sev_unpin_memory() to take a int *npages so it can be cleared as well. Since kvm_enc_region describes a region with u64 instead of pointers that seemed "safer" give you'd have to cast them to dereference.

Totally fine with the NACK of course. =]

#3 lore raw
In reply to Sean Christopherson
Quoted 16 lines · Sean Christopherson
Quoted 14 lines · Peter Gonda

Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a dangling pointer to invalid memory.

Cc Greg Thelen <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

arch/x86/kvm/svm/sev.c static void sev_unpin_memory(struct kvm *kvm, struct page **pages, · line 455

Quoted 9 lines
arch/x86/kvm/svm/sev.c +1 -0
index 309bcdb2f929..485ad86c01c6 100644
@@ -452,6 +452,7 @@ static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
      unpin_user_pages(pages, npages);
      kvfree(pages);
      sev->pages_locked -= npages;
+     *pages = NULL;

Would this have helped detect a real bug? I generally like cleaning up, but this leaves things in a somewhat inconsistent state, e.g. when unpinning a kvm_enc_region, pages will be NULL but npages will be non-zero. It's somewhat moot because the region is immediately freed in that case, but that begs the question of what real benefit this provides. sev_dbg_crypt() is the only flow where there's much danger of a use-after-free.

+1

We could send thousands of patches like this. But I think none of them would be directly helpful. Sometimes, things could be even worse, i.e., imagine when those NULL pointers got deferenced, they become BUG_ON(true).

If there are bugs related with those pointers, better to fix those bugs instead.

Thanks.
-Mingwei