Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1004521iob; Fri, 13 May 2022 19:19:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnTD8zTIjK+KXLo7ltaM5OeBoorC6oqOHLsPaAsFa0S8z+tCQRpcDU0ZO7RrN/s5tAgaso X-Received: by 2002:a5d:5984:0:b0:20c:7de2:5416 with SMTP id n4-20020a5d5984000000b0020c7de25416mr5935527wri.30.1652494775792; Fri, 13 May 2022 19:19:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652494775; cv=none; d=google.com; s=arc-20160816; b=FSsgC0i+SnGqEAgipk3kee9/MtBh2nOL718YIr9o/o+e4dMic6kH75+tOiE9utxWkW i+kKe4uuqdZqWHVIz5fEam/qZc6mXp5ISt1yS190sfz1zCzaCSFe/XPv/JSotTzsOiKI R135oPzpTqqC0k2s7PG+dH4ITDRJzzJk7I49h+eeScuD1ZJJ61CN7eeAm+i0+fOT0Sv5 A4/kkIkCUM3sNLXRt1hiZTfn7tP4pmFAi+xsMLmzElA3Sxue5NbDZtfajQNP7+iZwS3w 7fOaJIS+Z22yjmBskFce5ogGUUYczPKYcnqJ7dNaTa42XqA5UJLlyNx1IcwsC8QWYn45 wX7A== 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:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=f7yzhfisk2h8SeZqMk+S4T8cT4lhDdnwCiG3Dpdd8fo=; b=P+zOi+AUwqr2/wkLmAuuA21EPZY3gdWpaqDhUwgqjacZqK2zOYS1FHz4XKOU6ClUrH AyNqz2fP+IvLNISlCFuG19kdNWIGduyFd8Tm9i7VZs804HCOmlKrtHoYW1pEoLn9nyH1 iMU9ZNWCT53ak3xKJOfJ1PDsVPcxEQTzDhT+oP9daHRGL9MssIjiaTWB2o1t7x8RVcXd KMmeU/2ct7w2cZQs14GaNRnqHdmrf7mH+hByJ5DNSJ5/H0p56XN4MVt9k3bOh+yU1Grx L0g97Q/QCsj5bysQsFALOoiHHjeVKQYkTSFHW6y9A4EYwWqvUUIU4NH6T8BQySx97nT3 IRVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pDK8cgIt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q11-20020adff50b000000b0020c5edaa539si3268131wro.794.2022.05.13.19.19.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:19:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pDK8cgIt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B6AE04B5F44; Fri, 13 May 2022 17:36:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383930AbiEMTuK (ORCPT + 99 others); Fri, 13 May 2022 15:50:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383915AbiEMTuH (ORCPT ); Fri, 13 May 2022 15:50:07 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 843622DC6 for ; Fri, 13 May 2022 12:50:05 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id q3-20020a17090a304300b001dd2130b435so4819857pjl.0 for ; Fri, 13 May 2022 12:50:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=f7yzhfisk2h8SeZqMk+S4T8cT4lhDdnwCiG3Dpdd8fo=; b=pDK8cgIt+nRNbSGx8CFEQTj3ejYbBQrgiRGoeUNz3MJMNd1xbE9rZnQrT9/55ZuyRU ZXWBpxSXylg+TutIljZ3z4P6vfOS8tHOdWC9H20Rqxni7xPlBMrvE1e3khOO0biZrPMA 6KuRRwULLCi5v3071KvZdVd/ieraSJ5BpCHv8clwiWSXw5BQ1QJTl6WJFEKrnKdna8os zGN+3NPeY4ROds4c5WuGUIXxSxShCDiHwk0TC9FtijtVaE5cPHrpUfvNRsPj83cPMtxE gCSNU9hV5KwLmm+QJmh1ZrBSOKIM9Roc3qXbV9z++ME/AQCBSPiD/0OF/8gw+/7rg2Fn vFXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=f7yzhfisk2h8SeZqMk+S4T8cT4lhDdnwCiG3Dpdd8fo=; b=INrVcSJyXP0GYySqi6QlXmYPemn2wTILBls4ZGxUcvwn325k1RU8V5qOQTTetkoUjq BKKiQnHmO6Nrn9SgVA7yE0Sr9uDmIkoN2TKtwS8NRxKe9DCwuR5DN810NIIcFNhe9TBu NR9BBkfEqS3817KWdC6Fdb97NyPqTY1o+UaPVvKlSd5ob08gIQod/BNtOVE4W1idRYSq rYe/fd+EECDFjOZA9LP9KX2PuX0+qYFs/EajKftSNCaFhZ/uVlebRb11pwBh9wtfUDUJ UrBTrCqJmfL3YzT/7J9AtcAgk2+1J8KScYji1CBRPhHHsuVGs4I6A6s/qMNRY8dqJd0Y hJ2Q== X-Gm-Message-State: AOAM532449Nu8+sTBugp7esmA1EjsFIf/2aREZFGHVI4PcdMS0Fuo3QD HsnQTErt913ypztMiDt4PDHU2SlNP4k= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:902:b703:b0:15e:ea16:2c6e with SMTP id d3-20020a170902b70300b0015eea162c6emr6043987pls.100.1652471405059; Fri, 13 May 2022 12:50:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 13 May 2022 19:49:59 +0000 In-Reply-To: <20220513195000.99371-1-seanjc@google.com> Message-Id: <20220513195000.99371-2-seanjc@google.com> Mime-Version: 1.0 References: <20220513195000.99371-1-seanjc@google.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog Subject: [PATCH 1/2] KVM: x86/mmu: Drop RWX=0 SPTEs during ept_sync_page() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Ben Gardon Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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: X-Mailing-List: linux-kernel@vger.kernel.org Drop SPTEs whose new protections will yield a RWX=0 SPTE, i.e. a SPTE that is marked shadow-present but is not-present in the page tables. If EPT with execute-only support is in use by L1, KVM can create a RWX=0 SPTE can be created for an EPTE if the upper level combined permissions are R (or RW) and the leaf EPTE is changed from R (or RW) to X. Because the EPTE is considered present when viewed in isolation, and no reserved bits are set, FNAME(prefetch_invalid_gpte) will consider the GPTE valid. Creating a not-present SPTE isn't fatal as the SPTE is "correct" in the sense that the guest translation is inaccesible (the combined protections of all levels yield RWX=0), i.e. the guest won't get stuck in an infinite loop. If EPT A/D bits are disabled, KVM can mistake the SPTE for an access-tracked SPTE. But again, such confusion isn't fatal as the "saved" protections are also RWX=0. Add a WARN in make_spte() to detect creation of SPTEs that will result in RWX=0 protections, which is the real motivation for fixing ept_sync_page(). Creating a useless SPTE means KVM messed up _something_, even if whatever goof occurred doesn't manifest as a functional bug. Fixes: d95c55687e11 ("kvm: mmu: track read permission explicitly for shadow EPT page tables") Cc: David Matlack Cc: Ben Gardon Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/paging_tmpl.h | 9 ++++++++- arch/x86/kvm/mmu/spte.c | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index b025decf610d..d9f98f9ed4a0 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -1052,7 +1052,14 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) if (sync_mmio_spte(vcpu, &sp->spt[i], gfn, pte_access)) continue; - if (gfn != sp->gfns[i]) { + /* + * Drop the SPTE if the new protections would result in a RWX=0 + * SPTE or if the gfn is changing. The RWX=0 case only affects + * EPT with execute-only support, i.e. EPT without an effective + * "present" bit, as all other paging modes will create a + * read-only SPTE if pte_access is zero. + */ + if ((!pte_access && !shadow_present_mask) || gfn != sp->gfns[i]) { drop_spte(vcpu->kvm, &sp->spt[i]); flush = true; continue; diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 75c9e87d446a..9ad60662beac 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -101,6 +101,8 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 spte = SPTE_MMU_PRESENT_MASK; bool wrprot = false; + WARN_ON_ONCE(!pte_access && !shadow_present_mask); + if (sp->role.ad_disabled) spte |= SPTE_TDP_AD_DISABLED_MASK; else if (kvm_mmu_page_ad_need_write_protect(sp)) -- 2.36.0.550.gb090851708-goog