linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory
@ 2022-06-27 16:11 Peter Gonda
  2022-06-27 18:10 ` Sean Christopherson
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Gonda @ 2022-06-27 16:11 UTC (permalink / raw)
  To: kvm
  Cc: Peter Gonda, Greg Thelen, Paolo Bonzini, Sean Christopherson,
	Tom Lendacky, linux-kernel

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

Signed-off-by: Peter Gonda <pgonda@google.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 arch/x86/kvm/svm/sev.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 309bcdb2f929..485ad86c01c6 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -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)
-- 
2.37.0.rc0.161.g10f37bed90-goog


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

* Re: [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory
  2022-06-27 16:11 [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory Peter Gonda
@ 2022-06-27 18:10 ` Sean Christopherson
  2022-06-27 18:39   ` Peter Gonda
  2022-06-27 20:57   ` Mingwei Zhang
  0 siblings, 2 replies; 4+ messages in thread
From: Sean Christopherson @ 2022-06-27 18:10 UTC (permalink / raw)
  To: Peter Gonda; +Cc: kvm, Greg Thelen, Paolo Bonzini, Tom Lendacky, linux-kernel

On Mon, Jun 27, 2022, Peter Gonda wrote:
> Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a
> dangling pointer to invalid memory.
> 
> Signed-off-by: Peter Gonda <pgonda@google.com>
> Cc: Greg Thelen <gthelen@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Tom Lendacky <thomas.lendacky@amd.com>
> Cc: kvm@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  arch/x86/kvm/svm/sev.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index 309bcdb2f929..485ad86c01c6 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -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.

>  }
>  
>  static void sev_clflush_pages(struct page *pages[], unsigned long npages)
> -- 
> 2.37.0.rc0.161.g10f37bed90-goog
> 

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

* Re: [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory
  2022-06-27 18:10 ` Sean Christopherson
@ 2022-06-27 18:39   ` Peter Gonda
  2022-06-27 20:57   ` Mingwei Zhang
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Gonda @ 2022-06-27 18:39 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: kvm list, Greg Thelen, Paolo Bonzini, Tom Lendacky, LKML

On Mon, Jun 27, 2022 at 12:10 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Mon, Jun 27, 2022, Peter Gonda wrote:
> > Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a
> > dangling pointer to invalid memory.
> >
> > Signed-off-by: Peter Gonda <pgonda@google.com>
> > Cc: Greg Thelen <gthelen@google.com>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Sean Christopherson <seanjc@google.com>
> > Cc: Tom Lendacky <thomas.lendacky@amd.com>
> > Cc: kvm@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > ---
> >  arch/x86/kvm/svm/sev.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > index 309bcdb2f929..485ad86c01c6 100644
> > --- a/arch/x86/kvm/svm/sev.c
> > +++ b/arch/x86/kvm/svm/sev.c
> > @@ -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. =]

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

* Re: [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory
  2022-06-27 18:10 ` Sean Christopherson
  2022-06-27 18:39   ` Peter Gonda
@ 2022-06-27 20:57   ` Mingwei Zhang
  1 sibling, 0 replies; 4+ messages in thread
From: Mingwei Zhang @ 2022-06-27 20:57 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Peter Gonda, kvm, Greg Thelen, Paolo Bonzini, Tom Lendacky, LKML

On Mon, Jun 27, 2022 at 1:37 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Mon, Jun 27, 2022, Peter Gonda wrote:
> > Clear to the @pages array pointer in sev_unpin_memory to avoid leaving a
> > dangling pointer to invalid memory.
> >
> > Signed-off-by: Peter Gonda <pgonda@google.com>
> > Cc: Greg Thelen <gthelen@google.com>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Sean Christopherson <seanjc@google.com>
> > Cc: Tom Lendacky <thomas.lendacky@amd.com>
> > Cc: kvm@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > ---
> >  arch/x86/kvm/svm/sev.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > index 309bcdb2f929..485ad86c01c6 100644
> > --- a/arch/x86/kvm/svm/sev.c
> > +++ b/arch/x86/kvm/svm/sev.c
> > @@ -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

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

end of thread, other threads:[~2022-06-27 20:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-27 16:11 [PATCH] KVM: SEV: Clear the pages pointer in sev_unpin_memory Peter Gonda
2022-06-27 18:10 ` Sean Christopherson
2022-06-27 18:39   ` Peter Gonda
2022-06-27 20:57   ` Mingwei Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).