From patchwork Fri Jul 21 23:00:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 124161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp544436vqg; Fri, 21 Jul 2023 17:34:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlH2H4hmIXuxhleabNOmKHsKEYRmLdeskHwhfhp4PjhxFSfGdX7dkSm+ejgRWzGPI6dky2N3 X-Received: by 2002:a17:90b:4b07:b0:261:cce:d1c2 with SMTP id lx7-20020a17090b4b0700b002610cced1c2mr5534622pjb.11.1689986045022; Fri, 21 Jul 2023 17:34:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689986045; cv=none; d=google.com; s=arc-20160816; b=HeBfnV1DmN2jZYtI+dtWLszcaknrAveL8sghNuLRBIyafwYEV4NIHi6JJpeJ2iaAKl bf8X2B+s25Q9nkW3n5FpiAHs2K7fAi+BIZYJXvvmonmzpeNnOXfysfJ38Xs7mfrpxohu FaCWr+i+aE/6vY6W5FgCncOvX9eIuf8ad2pk3TWmMtgHXXVDbNlv6S/lFKOibxBd2/T2 iYUbFul1uLr3QKvLa+2uFDNlHT6kiegoEl/aMOJXR1W/Enw0dfilz/UjzNvLL9ld6ZuO A+RnD5AV5ON6uubU1rr5Z0mwL68RfAE/oUun2dd28uBOcX1Kl1mHcbJ6P0Ku/sOyhwwE W7pg== 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=Hf66Tg9z6eVGNOq7lITDvOc5Z8kagkKjdF0cIF19+es=; fh=sOnAx2GuCQaI5IJu7Mo1xgLCDrPH4l5HM1pSkvn/pIo=; b=m3XzC8qw6tfBwmoxslMmo+VlC/LAGmh7cZroya8Y370WgmS432aG9fren1XUDIuvDQ s/ZAq5vgRTLM+NwuqiC5jTLfVHXMNma4FinWEFQrwoUBMu6hRF84/srTUNW9Vpa4cFYs hWHYWkDpVSqYXKT645sn4Swqg0IVg0uBk4cSceThfb5pFEH4OILpsCaZ9dafgSzz3t3t cNbaOslwHlxeKWyPcIXSfw9qsTZOfZeZpKQ+6A3l6N0weq7NCmWpw5aaDusXUt1FkOzp RxZMEekV8e+xcnyWG67AyjK881OgWF+HfWKzThmBjwreCtleH6aiBmakW27xC9mIjwI4 uuFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gWGRlaTk; 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 fu8-20020a17090ad18800b00263bf1c6dc0si6495004pjb.102.2023.07.21.17.33.52; Fri, 21 Jul 2023 17:34:05 -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=gWGRlaTk; 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 S231256AbjGUXA2 (ORCPT + 99 others); Fri, 21 Jul 2023 19:00:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231190AbjGUXAV (ORCPT ); Fri, 21 Jul 2023 19:00:21 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFF5E3AA3 for ; Fri, 21 Jul 2023 16:00:19 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b8a7734734so14247045ad.2 for ; Fri, 21 Jul 2023 16:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689980419; x=1690585219; 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=Hf66Tg9z6eVGNOq7lITDvOc5Z8kagkKjdF0cIF19+es=; b=gWGRlaTkn5NiQLzTFkJJKM2s4Vv0wJGMneYaEHOeICGvTQoBJES+ZxeOaxEYUZZ8H/ aLgB8W2eVwNHUj+L5gE16Bl9H4JCMo1jGVfETz9hdyOEhIOdqK4qUaEnHiyFgT3I5xvw 5T05h3Lp3sUtvhTNneKrxTx1I6X497FTCSBHT4zPNmNGobPrsW5GniH4Q83Wpsg84klB BoNg3UF4Ix1V+9TO5Vo7EA8b8WvxG0sO4gwmvOZvacxW6B3glGXlVviCggBB5PUN470T AxanZ4SsftqURjvz+fPuPucWNINaeTK/YOLjg38HJr+MNi6DcmrliLE9v0nUEQxlvldR Td2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689980419; x=1690585219; 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=Hf66Tg9z6eVGNOq7lITDvOc5Z8kagkKjdF0cIF19+es=; b=hB86obYvpZ89OkgucrOSIPYnojPS8f9iFUTyqaN6+kdSSnFh8NnzF13OVMoDqrf357 QPX/KkhN+ylJplzbixXX1XN3xgiAFhOATpgggvn+FLp4pot6O0sQtEHrdJldxoyHiXu5 7jQjwM+AV4fl5KdIPJ3MGx298ZL3tOGJ348ozLluuIobjwgtW8zUNbYqo8XWId4Zqhs/ dufimSB5xFlEANUzxlGg8qDAAHohHzduhgOiSw5Py635inX4Y5BEFTwDwRWKLuOGdSwR ji5P6iZ585Sgq/lha5+tM1UOsqWgzuF9tgQhOcJ1p7mO/p2ubQ/kSWDYxUD/N6M4hTHH k/0A== X-Gm-Message-State: ABy/qLbO4ZC9dn2fg3lsfbEsWI5zgm8IwynsgbqJf1n2cUlz4PHk/hu7 EKLkinW8AB706Ubzp5+HZZPp0KJDWLc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:dad2:b0:1b8:5541:9d4d with SMTP id q18-20020a170902dad200b001b855419d4dmr13969plx.6.1689980419181; Fri, 21 Jul 2023 16:00:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 21 Jul 2023 16:00:02 -0700 In-Reply-To: <20230721230006.2337941-1-seanjc@google.com> Mime-Version: 1.0 References: <20230721230006.2337941-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230721230006.2337941-6-seanjc@google.com> Subject: [PATCH v2 5/9] KVM: x86/mmu: Bug the VM if a vCPU ends up in long mode without PAE enabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , David Matlack , Jim Mattson 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_BLOCKED,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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772078807206041060 X-GMAIL-MSGID: 1772078807206041060 Promote the ASSERT(), which is quite dead code in KVM, into a KVM_BUG_ON() for KVM's sanity check that CR4.PAE=1 if the vCPU is in long mode when performing a walk of guest page tables. The sanity is quite cheap since neither EFER nor CR4.PAE requires a VMREAD, especially relative to the cost of walking the guest page tables. More importantly, the sanity check would have prevented the true badness fixed by commit 112e66017bff ("KVM: nVMX: add missing consistency checks for CR0 and CR4"). The missed consistency check resulted in some versions of KVM corrupting the on-stack guest_walker structure due to KVM thinking there are 4/5 levels of page tables, but wiring up the MMU hooks to point at the paging32 implementation, which only allocates space for two levels of page tables in "struct guest_walker32". Queue a page fault for injection if the assertion fails, as both callers, FNAME(gva_to_gpa) and FNAME(walk_addr_generic), assume that walker.fault contains sane info on a walk failure. E.g. not populating the fault info could result in KVM consuming and/or exposing uninitialized stack data before the vCPU is kicked out to userspace, which doesn't happen until KVM checks for KVM_REQ_VM_DEAD on the next enter. Move the check below the initialization of "pte_access" so that the aforementioned to-be-injected page fault doesn't consume uninitialized stack data. The information _shouldn't_ reach the guest or userspace, but there's zero downside to being paranoid in this case. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/paging_tmpl.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index a3fc7c1a7f8d..f8d358226ac6 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -338,7 +338,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, } #endif walker->max_level = walker->level; - ASSERT(!(is_long_mode(vcpu) && !is_pae(vcpu))); /* * FIXME: on Intel processors, loads of the PDPTE registers for PAE paging @@ -348,6 +347,17 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, nested_access = (have_ad ? PFERR_WRITE_MASK : 0) | PFERR_USER_MASK; pte_access = ~0; + + /* + * Queue a page fault for injection if this assertion fails, as callers + * assume that walker.fault contains sane info on a walk failure. I.e. + * avoid making the situation worse by inducing even worse badness + * between when the assertion fails and when KVM kicks the vCPU out to + * userspace (because the VM is bugged). + */ + if (KVM_BUG_ON(is_long_mode(vcpu) && !is_pae(vcpu), vcpu->kvm)) + goto error; + ++walker->level; do {