Message ID | 20230722012350.2371049-4-seanjc@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp562796vqg; Fri, 21 Jul 2023 18:30:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlET7qSbnX2s2XMMSlPnmsmcoyUsihXCiNPobWmKwLOzphUpqdMLRhI3cGFG2CAgUCtPHcOW X-Received: by 2002:a05:6402:1397:b0:51d:9e0c:1396 with SMTP id b23-20020a056402139700b0051d9e0c1396mr2439738edv.35.1689989446701; Fri, 21 Jul 2023 18:30:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689989446; cv=none; d=google.com; s=arc-20160816; b=ytGbtecu/iH3bHBPZ6dc0yWsyCRx5bzVfLJlo9axRY/lcL9PdTu6AGkziC1LgvEZlK j59yF0N5OYb/QNqy+RH+VWu8Y0jfcv2+WeuQVk4jC4QG5YcPCyvJYgxMTgrrZdoFFqni lF83qZuGohl+S174bb8IHRE3LviqvHe2fPNrQEcm6KJIjjynGW4vyqKw0o6fvoa/do3X qvXv3UVEdeCiwQTnJap5aqCdSo8uJGMUbFWor9p4ojMztn3ubYamqGcUmupiuEJUvnmt jUKckVfS4vFtmtEXAyjIozm/tVPQ0Abr6vpj7og335aZ4FEQUCRjl6nWr3Rp2MqQI0Dq D/KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=K8g8ylIBioHABO4UYnvq+eRAgeeK2kAPLMh05XoHYU4=; fh=+wFND6X2PweTsJwZ99iP2DHiwxyX/zx7C8kEbHFvmqg=; b=tPTMqCwF511eyyOWLrmMokM8QETL0/uAv0REfH7W6Li8VLkF/skmzdstI4DUOHn9Ad +25GX5CklFSGRsXgdPoPQzcPyQiNnxPusDWO7TywHH+ma4tv3QTkJxGtdisUFvlLLIPD SL3LVCBkMjG3ZSeiEikvBj021azS+RaQ7+RA28UAd1uR2x7lihWwWiOluYph3ywW7Oft fU6EeVu0K4RUZYzDBpORtccJq/+SHnLwGU21mrQiZzX6/IWMcKPEXt1yEWJ/F/d4Lq2I XvWt262Xi11PLkEjHjO93kExraBnIeDUfBj5/80VD+IQFlrx4hRxXcqgVZ+SJzAU97J4 Utpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=d9n714xK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r15-20020a056402034f00b0051e4c4ac51esi3010920edw.467.2023.07.21.18.30.23; Fri, 21 Jul 2023 18:30:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=d9n714xK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230073AbjGVBYH (ORCPT <rfc822;assdfgzxcv4@gmail.com> + 99 others); Fri, 21 Jul 2023 21:24:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231228AbjGVBX7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 21 Jul 2023 21:23:59 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3840B35AD for <linux-kernel@vger.kernel.org>; Fri, 21 Jul 2023 18:23:58 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-cab7304dcccso2457603276.3 for <linux-kernel@vger.kernel.org>; Fri, 21 Jul 2023 18:23:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689989037; x=1690593837; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=K8g8ylIBioHABO4UYnvq+eRAgeeK2kAPLMh05XoHYU4=; b=d9n714xKJqMwcsAZMI0NHTCtMswGC8ES6vvzsYorFNt0M9xXUeBRyv2a9UkMH2SDBb 1ScznqmzIh4kjUyE1rSGMt/RSAjUOgBLjmGeokgwc60pdNX308+hKXWH1xXe2eJ1E7UE 05Os0QeMBsYL+KYrO61vzAUcL8pSdwb+96Z18F1kECGsA94JjKWTeYHXZFmAJjnJ1zha vpdW1oLjoY/H5htKcV2jS7OfuDX/CAAY8O0JZDKKDltJ//M2/x0m9xTroHtPNR5sFrHa 9EfyrhBnD0vt7qK+t8Y0oFu8yMInPvL09a53JrtWtYnKRNKlsMgf/sCN6zvwRwW95iyM scGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689989037; x=1690593837; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=K8g8ylIBioHABO4UYnvq+eRAgeeK2kAPLMh05XoHYU4=; b=Kqk9lh2PGzgwMwRtswRqNhkDxZ8DqHm2LcE4xo0eDQUKUEZgCFg4aZGYqkiRWH9vUy /ki1HMmjV87ge/7zUuhB754iyluz60bFoMjRgQihl+Qqv2TqQq6CFN62ZNtYH1Jp/S6J tORReMJPInOvhHNpm0F7cGNlYQjVNyUklD+XiN0tLHUFOpL6H0KZIhXQAOFKcW8ha1K2 hi8K5YIpH5gyetH9E0CM2rNZC3LRzJ33OCRbeDHp6CAVgpOQoyxr5uI5R1+AXGpDhSnU s2DRQXVnN1Rwbu+JhAOblxqrzT9USig8TTusg+vdLOHp9OdBzNkNaDP9FG7uBRhOP8fp puWA== X-Gm-Message-State: ABy/qLYPqmtqMIxn2T5ixtZxCWyT4Jh6RVM74dDNIop/LgBIP4ZmEI0V ykquYx6yQlU3FynPfrAkAFGEYAeVX3o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:245:0:b0:d06:cbd:1f3e with SMTP id 66-20020a250245000000b00d060cbd1f3emr13920ybc.3.1689989037474; Fri, 21 Jul 2023 18:23:57 -0700 (PDT) Reply-To: Sean Christopherson <seanjc@google.com> Date: Fri, 21 Jul 2023 18:23:48 -0700 In-Reply-To: <20230722012350.2371049-1-seanjc@google.com> Mime-Version: 1.0 References: <20230722012350.2371049-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230722012350.2371049-4-seanjc@google.com> Subject: [PATCH 3/5] KVM: x86/mmu: Harden TDP MMU iteration against root w/o shadow page From: Sean Christopherson <seanjc@google.com> To: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reima Ishii <ishiir@g.ecc.u-tokyo.ac.jp> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772082374281015058 X-GMAIL-MSGID: 1772082374281015058 |
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
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
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
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
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);