Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp190503pxv; Thu, 24 Jun 2021 06:00:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycxkS3qd0jZUov6pNO733oQ+8sdf2aX28HkBOT2rBhiKf+ihpA8fZzbCqpKAxlIkjUbGG4 X-Received: by 2002:a5e:930d:: with SMTP id k13mr3980937iom.61.1624539614141; Thu, 24 Jun 2021 06:00:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624539614; cv=none; d=google.com; s=arc-20160816; b=u8pVqJ6gYoegR3y+LQ7inVarRrQd50ML1pr44APmkowxXhuQbqBrLfAtJDrJXNnYlT ViHcxaTIBrG7Dhv6JkQrGkSmNxiTnA2XYyp30DYQkpw63Eh6PXAqj04AeiI9QgNQPPfY tAe0Y0LBShnc6rxvPNdAty7tvb9pcaU5vPt+IajLObt5eKLlHVqPChVQkpZgnSL8618Z d55D+OCSw3Oep/Y8RewMZKZb1+kSHOTVy9QLlyIDZKJW2AJTiAvLRhxNFMO+rzAyS89T MM1eMA7hfZ7aCwlgQ5uQuP4lanG3wRulGjBwW4xpmwnXH03uJZN/gf5DNP0wlzrhyIZ0 wxiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:message-id :mime-version:in-reply-to:references:cc:to:subject:from:date :dkim-signature; bh=uZuciq1P1LLIv86omk3p15CWzxu9H3VjtHemSC+vWOI=; b=obQu3hVTF9R+aYOcyhtXWg7+tgDfyNO9MwoCnODZ6+cFL5CmFBRt6xTwJpSV/dO51d o7MkjafFbHM3BCHIeBA+U1ZE74TxvkuIfXLyXPAg3Nq2HUcBwi3HTE3epcDhBZ/Ou4JX FCfMBccqzHBUwWj5Su1eBK4w6EU9DtTLi+IJF662jb9H9QKRalq+LJzzQOHnuyls7yvV z8b5/3DrJ0PkV2sAoZkNcFXuIHku3zCqno7sOE1wzLWUvF3DMcY1KzGqxXK+SdEvpfz9 P4zwm3Sk+omPU0NNAWxK+bc624hTJ45I33Ml2LuSaMPGRDYcQaifrh/ko+BqNAfvfLPJ +lwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rznw7O6D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h19si3327246jaj.37.2021.06.24.06.00.01; Thu, 24 Jun 2021 06:00:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rznw7O6D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231880AbhFXNAW (ORCPT + 99 others); Thu, 24 Jun 2021 09:00:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231635AbhFXNAU (ORCPT ); Thu, 24 Jun 2021 09:00:20 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C425AC061574; Thu, 24 Jun 2021 05:58:00 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id t32so5109756pfg.2; Thu, 24 Jun 2021 05:58:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:subject:to:cc:references:in-reply-to:mime-version :message-id:content-transfer-encoding; bh=uZuciq1P1LLIv86omk3p15CWzxu9H3VjtHemSC+vWOI=; b=rznw7O6D2Zt35p7Og+3iCwDgw2dkNPfwzD7rlUMP0y2Ej6O0tM+y3kSEcf8SWyT54d ublbOGuxe9Umj5fFSx3kLcve+5qwIiX31Bd1k9qJoijVaEMTrXxDg0VCRopT0Otp624H vQfbCt9QT5NQGNwfAmJ+KqkgxcPuOAOPjB6i02zSgoYEerKJgc/pbPT1wHARr0++dvZS Cel9tp2O0yGrWOTbrGUMXfBkZ8Eb/NzV2+WsHAOwG6gVAXQkKI9dWxURNSfgfHu0j01+ sMqgBmAGdH9hfo97BHqdnz3MvquYkt7SnDT6LlXF4LTAyCM7dcMF+YevSRJYr4EeGBsv ThKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:to:cc:references:in-reply-to :mime-version:message-id:content-transfer-encoding; bh=uZuciq1P1LLIv86omk3p15CWzxu9H3VjtHemSC+vWOI=; b=Rjh9Om0dq8H/D0FO1lZ43yL+7aCSVS/X9jpOD5wNaflreBubimlvLvH1Xo35Cpjq+9 2OHhmtNHJwqVm2uBcl5sWbwjDZp6nbdjuvhmR1QOKnjOKnHOK2X4k4eM9gO9tgZnpZSg 2ACA6zeKf6pJjkXZdeuL5/WDSvtzDLSk8vIF1He/xWKTSqoLVu16Q96mjspw/gxyhP9+ Hypw9vCkAHbXByxnc3fDIKOytvJFdqTIgC2EyvyFN0jXX2/yJqU/TP+Zt1xUbFierH7H KaiNAR7dSh02gAYpibe7gdXUTtocCfGLiLtVw9KjnaCX7qnneyUhDTopI4bnFZYw6Ata nG8Q== X-Gm-Message-State: AOAM5334kxLKFAYNT7r1MSLXzP99EsOab3UVDbDcB/oUpHd9HFYEscz9 qrqL7xqODolIdZPFJPHp4ws= X-Received: by 2002:a63:530a:: with SMTP id h10mr4685438pgb.98.1624539480323; Thu, 24 Jun 2021 05:58:00 -0700 (PDT) Received: from localhost (60-242-147-73.tpgi.com.au. [60.242.147.73]) by smtp.gmail.com with ESMTPSA id h22sm2909006pfc.21.2021.06.24.05.57.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 05:58:00 -0700 (PDT) Date: Thu, 24 Jun 2021 22:57:54 +1000 From: Nicholas Piggin Subject: Re: [PATCH 0/6] KVM: Remove uses of struct page from x86 and arm64 MMU To: Aleksandar Markovic , Huacai Chen , Marc Zyngier , Paul Mackerras , Paolo Bonzini , David Stevens , Zhenyu Wang , Zhi Wang Cc: Alexandru Elisei , dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, James Morse , Jim Mattson , Joerg Roedel , kvmarm@lists.cs.columbia.edu, kvm-ppc@vger.kernel.org, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Sean Christopherson , David Stevens , Suzuki K Poulose , Vitaly Kuznetsov , Wanpeng Li , Will Deacon References: <20210624035749.4054934-1-stevensd@google.com> <1624530624.8jff1f4u11.astroid@bobo.none> <1624534759.nj0ylor2eh.astroid@bobo.none> <0d3a699a-15eb-9f1b-0735-79d14736f38c@redhat.com> In-Reply-To: <0d3a699a-15eb-9f1b-0735-79d14736f38c@redhat.com> MIME-Version: 1.0 Message-Id: <1624539354.6zggpdrdbw.astroid@bobo.none> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Excerpts from Paolo Bonzini's message of June 24, 2021 10:41 pm: > On 24/06/21 13:42, Nicholas Piggin wrote: >> Excerpts from Nicholas Piggin's message of June 24, 2021 8:34 pm: >>> Excerpts from David Stevens's message of June 24, 2021 1:57 pm: >>>> KVM supports mapping VM_IO and VM_PFNMAP memory into the guest by usin= g >>>> follow_pte in gfn_to_pfn. However, the resolved pfns may not have >>>> assoicated struct pages, so they should not be passed to pfn_to_page. >>>> This series removes such calls from the x86 and arm64 secondary MMU. T= o >>>> do this, this series modifies gfn_to_pfn to return a struct page in >>>> addition to a pfn, if the hva was resolved by gup. This allows the >>>> caller to call put_page only when necessated by gup. >>>> >>>> This series provides a helper function that unwraps the new return typ= e >>>> of gfn_to_pfn to provide behavior identical to the old behavior. As I >>>> have no hardware to test powerpc/mips changes, the function is used >>>> there for minimally invasive changes. Additionally, as gfn_to_page and >>>> gfn_to_pfn_cache are not integrated with mmu notifier, they cannot be >>>> easily changed over to only use pfns. >>>> >>>> This addresses CVE-2021-22543 on x86 and arm64. >>> >>> Does this fix the problem? (untested I don't have a POC setup at hand, >>> but at least in concept) >>=20 >> This one actually compiles at least. Unfortunately I don't have much >> time in the near future to test, and I only just found out about this >> CVE a few hours ago. >=20 > And it also works (the reproducer gets an infinite stream of userspace=20 > exits and especially does not crash). We can still go for David's=20 > solution later since MMU notifiers are able to deal with this pages, but=20 > it's a very nice patch for stable kernels. Oh nice, thanks for testing. How's this? Thanks, Nick --- KVM: Fix page ref underflow for regions with valid but non-refcounted pages It's possible to create a region which maps valid but non-refcounted pages (e.g., tail pages of non-compound higher order allocations). These host pages can then be returned by gfn_to_page, gfn_to_pfn, etc., family of APIs, which take a reference to the page, which takes it from 0 to 1. When the reference is dropped, this will free the page incorrectly. Fix this by only taking a reference on the page if it was non-zero, which indicates it is participating in normal refcounting (and can be released with put_page). Signed-off-by: Nicholas Piggin --- virt/kvm/kvm_main.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6a6bc7af0e28..46fb042837d2 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2055,6 +2055,13 @@ static bool vma_is_valid(struct vm_area_struct *vma,= bool write_fault) return true; } =20 +static int kvm_try_get_pfn(kvm_pfn_t pfn) +{ + if (kvm_is_reserved_pfn(pfn)) + return 1; + return get_page_unless_zero(pfn_to_page(pfn)); +} + static int hva_to_pfn_remapped(struct vm_area_struct *vma, unsigned long addr, bool *async, bool write_fault, bool *writable, @@ -2104,13 +2111,21 @@ static int hva_to_pfn_remapped(struct vm_area_struc= t *vma, * Whoever called remap_pfn_range is also going to call e.g. * unmap_mapping_range before the underlying pages are freed, * causing a call to our MMU notifier. + * + * Certain IO or PFNMAP mappings can be backed with valid + * struct pages, but be allocated without refcounting e.g., + * tail pages of non-compound higher order allocations, which + * would then underflow the refcount when the caller does the + * required put_page. Don't allow those pages here. */=20 - kvm_get_pfn(pfn); + if (!kvm_try_get_pfn(pfn)) + r =3D -EFAULT; =20 out: pte_unmap_unlock(ptep, ptl); *p_pfn =3D pfn; - return 0; + + return r; } =20 /* --=20 2.23.0