[3/5] KVM: x86/mmu: Harden TDP MMU iteration against root w/o shadow page

Message ID 20230722012350.2371049-4-seanjc@google.com
State New
Headers
Series KVM: x86/mmu: Don't synthesize triple fault on bad root |

Commit Message

Sean Christopherson July 22, 2023, 1:23 a.m. UTC
  Explicitly check that tdp_iter_start() is handed a valid shadow page
to harden KVM against bugs where

Opportunistically stop the TDP MMU iteration instead of continuing on
with garbage if the incoming root is bogus.  Attempting to walk a garbage
root is more likely to caused major problems than doing nothing.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/mmu/tdp_iter.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
  

Comments

Yu Zhang July 25, 2023, 10:39 a.m. UTC | #1
On Fri, Jul 21, 2023 at 06:23:48PM -0700, Sean Christopherson wrote:
> Explicitly check that tdp_iter_start() is handed a valid shadow page
> to harden KVM against bugs where

Sorry, where? 

It's not about guest using an invisible GFN, it's about a KVM bug, right?

> 
> Opportunistically stop the TDP MMU iteration instead of continuing on
> with garbage if the incoming root is bogus.  Attempting to walk a garbage
> root is more likely to caused major problems than doing nothing.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>  arch/x86/kvm/mmu/tdp_iter.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c
> index d2eb0d4f8710..bd30ebfb2f2c 100644
> --- a/arch/x86/kvm/mmu/tdp_iter.c
> +++ b/arch/x86/kvm/mmu/tdp_iter.c
> @@ -39,13 +39,14 @@ void tdp_iter_restart(struct tdp_iter *iter)
>  void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
>  		    int min_level, gfn_t next_last_level_gfn)
>  {
> -	int root_level = root->role.level;
> -
> -	WARN_ON(root_level < 1);
> -	WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
> +	if (WARN_ON_ONCE(!root || (root->role.level < 1) ||
> +			 (root->role.level > PT64_ROOT_MAX_LEVEL))) {
> +		iter->valid = false;
> +		return;
> +	}
>  

I saw many usages of WARN_ON_ONCE() and WARN_ON() in KVM. And just wonder,
is there any criteria for KVM when to use which?

B.R.
Yu
  
Sean Christopherson July 25, 2023, 3:56 p.m. UTC | #2
On Tue, Jul 25, 2023, Yu Zhang wrote:
> On Fri, Jul 21, 2023 at 06:23:48PM -0700, Sean Christopherson wrote:
> > Explicitly check that tdp_iter_start() is handed a valid shadow page
> > to harden KVM against bugs where
> 
> Sorry, where? 

Gah, I must have seen something shiny when writing the changelog.

> It's not about guest using an invisible GFN, it's about a KVM bug, right?

Yes, the intent is to guard against a KVM bug, e.g. if KVM managed to get into
the TDP MMU with an invalid root, or a root belonging to a shadow MMU.  I'll fix
the changelog in v2.

> > Opportunistically stop the TDP MMU iteration instead of continuing on
> > with garbage if the incoming root is bogus.  Attempting to walk a garbage
> > root is more likely to caused major problems than doing nothing.
> > 
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> >  arch/x86/kvm/mmu/tdp_iter.c | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c
> > index d2eb0d4f8710..bd30ebfb2f2c 100644
> > --- a/arch/x86/kvm/mmu/tdp_iter.c
> > +++ b/arch/x86/kvm/mmu/tdp_iter.c
> > @@ -39,13 +39,14 @@ void tdp_iter_restart(struct tdp_iter *iter)
> >  void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
> >  		    int min_level, gfn_t next_last_level_gfn)
> >  {
> > -	int root_level = root->role.level;
> > -
> > -	WARN_ON(root_level < 1);
> > -	WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
> > +	if (WARN_ON_ONCE(!root || (root->role.level < 1) ||
> > +			 (root->role.level > PT64_ROOT_MAX_LEVEL))) {
> > +		iter->valid = false;
> > +		return;
> > +	}
> >  
> 
> I saw many usages of WARN_ON_ONCE() and WARN_ON() in KVM. And just wonder,
> is there any criteria for KVM when to use which?

Heh, already a step ahead of you :-)

https://lore.kernel.org/all/20230721230006.2337941-5-seanjc@google.com
  
Yu Zhang July 26, 2023, 3:55 a.m. UTC | #3
On Tue, Jul 25, 2023 at 08:56:32AM -0700, Sean Christopherson wrote:
> On Tue, Jul 25, 2023, Yu Zhang wrote:
> > On Fri, Jul 21, 2023 at 06:23:48PM -0700, Sean Christopherson wrote:
> > > Explicitly check that tdp_iter_start() is handed a valid shadow page
> > > to harden KVM against bugs where
> > 
> > Sorry, where? 
> 
> Gah, I must have seen something shiny when writing the changelog.
> 
> > It's not about guest using an invisible GFN, it's about a KVM bug, right?
> 
> Yes, the intent is to guard against a KVM bug, e.g. if KVM managed to get into
> the TDP MMU with an invalid root, or a root belonging to a shadow MMU.  I'll fix
> the changelog in v2.
> 
> > > Opportunistically stop the TDP MMU iteration instead of continuing on
> > > with garbage if the incoming root is bogus.  Attempting to walk a garbage
> > > root is more likely to caused major problems than doing nothing.
> > > 
> > > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > > ---
> > >  arch/x86/kvm/mmu/tdp_iter.c | 11 ++++++-----
> > >  1 file changed, 6 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c
> > > index d2eb0d4f8710..bd30ebfb2f2c 100644
> > > --- a/arch/x86/kvm/mmu/tdp_iter.c
> > > +++ b/arch/x86/kvm/mmu/tdp_iter.c
> > > @@ -39,13 +39,14 @@ void tdp_iter_restart(struct tdp_iter *iter)
> > >  void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
> > >  		    int min_level, gfn_t next_last_level_gfn)
> > >  {
> > > -	int root_level = root->role.level;
> > > -
> > > -	WARN_ON(root_level < 1);
> > > -	WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
> > > +	if (WARN_ON_ONCE(!root || (root->role.level < 1) ||
> > > +			 (root->role.level > PT64_ROOT_MAX_LEVEL))) {
> > > +		iter->valid = false;
> > > +		return;
> > > +	}
> > >  
> > 
> > I saw many usages of WARN_ON_ONCE() and WARN_ON() in KVM. And just wonder,
> > is there any criteria for KVM when to use which?
> 
> Heh, already a step ahead of you :-)
> 
> https://lore.kernel.org/all/20230721230006.2337941-5-seanjc@google.com

Haha! That patch lies just above this series, and the explanation is very
convincing. :) Thanks! 

B.R.
Yu
  

Patch

diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c
index d2eb0d4f8710..bd30ebfb2f2c 100644
--- a/arch/x86/kvm/mmu/tdp_iter.c
+++ b/arch/x86/kvm/mmu/tdp_iter.c
@@ -39,13 +39,14 @@  void tdp_iter_restart(struct tdp_iter *iter)
 void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
 		    int min_level, gfn_t next_last_level_gfn)
 {
-	int root_level = root->role.level;
-
-	WARN_ON(root_level < 1);
-	WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
+	if (WARN_ON_ONCE(!root || (root->role.level < 1) ||
+			 (root->role.level > PT64_ROOT_MAX_LEVEL))) {
+		iter->valid = false;
+		return;
+	}
 
 	iter->next_last_level_gfn = next_last_level_gfn;
-	iter->root_level = root_level;
+	iter->root_level = root->role.level;
 	iter->min_level = min_level;
 	iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root->spt;
 	iter->as_id = kvm_mmu_page_as_id(root);