From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 540EBC49EA7 for ; Thu, 24 Jun 2021 12:58:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CB3C6128D for ; Thu, 24 Jun 2021 12:58:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231896AbhFXNAX (ORCPT ); Thu, 24 Jun 2021 09:00:23 -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-Google-Smtp-Source: ABdhPJyRyvJn5C7VYeX4UgUoQTwK2Wl40+EfEX+5KjgwzBTc5sLi33BY4sgc5lVA8ilsgegR5j26Eg== 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A463C48BDF for ; Thu, 24 Jun 2021 12:58:32 +0000 (UTC) Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3CD3B6128D for ; Thu, 24 Jun 2021 12:58:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3CD3B6128D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4G9gDQ4Xkfz3bxy for ; Thu, 24 Jun 2021 22:58:30 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=rznw7O6D; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::42c; helo=mail-pf1-x42c.google.com; envelope-from=npiggin@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=rznw7O6D; dkim-atps=neutral Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4G9gCw3zL6z305v for ; Thu, 24 Jun 2021 22:58:03 +1000 (AEST) Received: by mail-pf1-x42c.google.com with SMTP id 21so5118367pfp.3 for ; Thu, 24 Jun 2021 05:58:02 -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=ZIahQnJig/33M9YgWfeqemI7Oc49kWNhZqgeC+YkMqlYB7uTbmU269set98B7RWvqK QUhnPT0hzVyK7fcG+iOpU+i6nl6KYNch1HB5GQ7tuRYjdP5cvOrliZ7nxpM8DlX+O0Wk WLCnQqVzq/VzMCNh+3YjkV7cRZ5pHIfnR2qchMbihn8g+fJ+8b+2wZj0KTYMWmM4OLLc G65n0RYT+Z6P2qk4mrHAqLB3N69cDds5fMHrB+FMpcb1ro1jFZQeTe/qTXpR6HLerc5M 0ol9T6jYcQWCKHmlVmDhXEQSDTOYwNSBSW+hYA4wwkS5oUltu3jG3aZHYs4UDXWscI5h tAtg== X-Gm-Message-State: AOAM532pit8XgpuoyWmIm0xCZwobvUqc3dVPYQ1XdKnmpChfAaoPzmGj R8frFVkd2Eb/lQLrbthX0jY= X-Google-Smtp-Source: ABdhPJyRyvJn5C7VYeX4UgUoQTwK2Wl40+EfEX+5KjgwzBTc5sLi33BY4sgc5lVA8ilsgegR5j26Eg== 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 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 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Wanpeng Li , kvm@vger.kernel.org, David Stevens , Alexandru Elisei , intel-gfx@lists.freedesktop.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, kvmarm@lists.cs.columbia.edu, Will Deacon , Suzuki K Poulose , James Morse , kvm-ppc@vger.kernel.org, Sean Christopherson , Vitaly Kuznetsov , linux-mips@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Jim Mattson Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CB57C49EA7 for ; Thu, 24 Jun 2021 14:04:03 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id F084F6140A for ; Thu, 24 Jun 2021 14:04:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F084F6140A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 9BD124B1A7; Thu, 24 Jun 2021 10:04:02 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@gmail.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8YaRw0V0KI+m; Thu, 24 Jun 2021 10:04:01 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id CD5324B1BA; Thu, 24 Jun 2021 10:03:55 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 8851B4B195 for ; Thu, 24 Jun 2021 08:58:03 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ukTUqt6v45No for ; Thu, 24 Jun 2021 08:58:01 -0400 (EDT) Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 866374B178 for ; Thu, 24 Jun 2021 08:58:01 -0400 (EDT) Received: by mail-pf1-f175.google.com with SMTP id c8so5103793pfp.5 for ; Thu, 24 Jun 2021 05:58:01 -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=nJwenFudGP6lUa8maIP7WbxF8KOeeSPML/cQBD/rbBISSGDXiGdVWKrQ25UN04JjVs VlXj4d/oPR0ELqJ/zuLnjDczJR06xGWSTuctGQLhdOU7Ue/hfHZXpaWz9o8G14tmWVPV Ydd+0nfgmHrKYuDC+1VVbG7fJ6yw3V9BdPIcrfsTZRvgWI4zk9q3d/HNcbSHJX7PPLw7 8CN85Hhj9Fz4ghUOfIhp1i6neWnmh8DuFErT7UwRkGY2GGR/SnSG5+5zoF6FNqChPJq/ OQjwB/DHNZkE2lZjsS2JfN9EXdVzys9oS4KmZ/JgCpfaMjFXDJ95JWhT2le6cPOLPpfE QtTg== X-Gm-Message-State: AOAM531Pv1GObIoK+pPREv/7sbmaIJ/6CgSimiXzZHAGHRnRdwc/ntf7 9/5ZGT6rlpC7GIzaUy8HMI8= X-Google-Smtp-Source: ABdhPJyRyvJn5C7VYeX4UgUoQTwK2Wl40+EfEX+5KjgwzBTc5sLi33BY4sgc5lVA8ilsgegR5j26Eg== 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 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> X-Mailman-Approved-At: Thu, 24 Jun 2021 10:03:53 -0400 Cc: Wanpeng Li , kvm@vger.kernel.org, David Stevens , intel-gfx@lists.freedesktop.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, kvmarm@lists.cs.columbia.edu, Will Deacon , kvm-ppc@vger.kernel.org, Sean Christopherson , Vitaly Kuznetsov , linux-mips@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Jim Mattson X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu 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 using >>>> 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. To >>>> 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 type >>>> 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) >> >> 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. > > And it also works (the reproducer gets an infinite stream of userspace > exits and especially does not crash). We can still go for David's > solution later since MMU notifiers are able to deal with this pages, but > 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; } +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_struct *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. */ - kvm_get_pfn(pfn); + if (!kvm_try_get_pfn(pfn)) + r = -EFAULT; out: pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; - return 0; + + return r; } /* -- 2.23.0 _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2265C48BDF for ; Thu, 24 Jun 2021 12:59:40 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9D626613BE for ; Thu, 24 Jun 2021 12:59:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9D626613BE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:In-Reply-To: References:Cc:To:Subject:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HlCsi8W++UkUjP/ZSiGum+zl93fWfOykMtFVjbuyRI0=; b=rfzVdR5FtmgIbt VDlug3giR1b6ZxqpkDElG17lW0AZm+q3g3ZvUXS/IvnmE6CJQo5q17yTyaA3S6+qF5zY0Cx7m384k xiK5Py6iG2RbEOjXhCRHgSNNW+wMMfxWqzky5r0v8OlVFoa2/Iwfdcj1cf0faOmkBAZTyKkMqfH0o lSAmmpTWCRI2WWzi/VAFQ1JPfrviw2l3S65qyaOFx/rsL32Py5UEkTia/WDHxeoVA8epYRq10hZ7D niLSPTL2BLD+4vck4+2/UivR32y/fnwZ/vj2HSYKm+iecCtHwwDgGCN1rR6LTlVSyvSdYlimTUkKo WxYNmvSY5Sda+kgADBCQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lwOvk-00EiVD-ER; Thu, 24 Jun 2021 12:58:04 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lwOvh-00EiTo-5V for linux-arm-kernel@lists.infradead.org; Thu, 24 Jun 2021 12:58:02 +0000 Received: by mail-pf1-x42c.google.com with SMTP id 21so5118368pfp.3 for ; 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=lTLLLTALC5PzU8KrtnniuirI2oZA2stF4zUKqPh8CHc5bhUmcAMhlFr7YgToHWF/Pf fLx3s/chewgL/ew0Xm/goNXvMcVdNWbRkJb+QPG7g7IzXGypdo2QcZSt5tgogxXjbio+ cMSsqfSNv6asN7LOtelUlAxZOjvKn5Iwnj6ai47Yg3LxpW5LVeeXXpiYQ9vpeOylSsPh 5vHagTxwFtjQe3SXeOmtebChPmBwGTE4v/1q0LcCB3oF48Ck4QBuYnuFQOUJdmn5LbTP G2m9YRfha5edFO2Ic3mmlynlo6Bqa2BQcrpRZ44nNT3VpBexP90OUwQvP0pH79TSy2dF rtlA== X-Gm-Message-State: AOAM533mN8zvLKF1c+y3NstqKDZDJNzf8Pgkhkgbc0GkzmSA1x2tlaYz k7qJbl5BbWBkPgMKOfCNiZ0= X-Google-Smtp-Source: ABdhPJyRyvJn5C7VYeX4UgUoQTwK2Wl40+EfEX+5KjgwzBTc5sLi33BY4sgc5lVA8ilsgegR5j26Eg== 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210624_055801_248586_F65D2432 X-CRM114-Status: GOOD ( 24.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 using >>>> 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. To >>>> 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 type >>>> 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) >> >> 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. > > And it also works (the reproducer gets an infinite stream of userspace > exits and especially does not crash). We can still go for David's > solution later since MMU notifiers are able to deal with this pages, but > 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; } +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_struct *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. */ - kvm_get_pfn(pfn); + if (!kvm_try_get_pfn(pfn)) + r = -EFAULT; out: pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; - return 0; + + return r; } /* -- 2.23.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D11C5C49EA5 for ; Thu, 24 Jun 2021 12:58:01 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9D7EF613C3 for ; Thu, 24 Jun 2021 12:58:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9D7EF613C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 471AE6EB2F; Thu, 24 Jun 2021 12:58:01 +0000 (UTC) Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by gabe.freedesktop.org (Postfix) with ESMTPS id BB44B6EB2F; Thu, 24 Jun 2021 12:58:00 +0000 (UTC) Received: by mail-pf1-x42e.google.com with SMTP id q192so5092642pfc.7; 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=Ocs4AtefsbgeLgOLTC4u8v9kb1f0/cTGV5CGGenCnLMkkV7m7+nRW3LUlj4nKrzz0s IbjR3loDFxw+ukEyuWlTP5j0uC7nYazBJ2LWKmWlOEPKxW6A+Y/rZ9k/Zkw52Sos9gA2 WTojPYmmRtprXZ5KDk81x7QJr1ao4lMJGLbb1IgTQCc+3ohFOKBfXhmkH8shngK19T8I QRdA7j2/O1On5lE7gIxzqHEZLYUxI+aP/bgRF6rh+0qJknZLFdpdBz60GS6yfAkZ/Pcp avyl3vS1MTfXgDGnaxCo03JbCLSFzYws/1VcZicwPBZnq3BFTr2DnwCrNlBVmML+xtXu i7+A== X-Gm-Message-State: AOAM533yJkuUiYpE3rR44pxgy3Bk0N8kTwYdE2hvUP2lZie8RLJEGiua Tk51EqXfGvyMqLZ4YMub4YY= X-Google-Smtp-Source: ABdhPJyRyvJn5C7VYeX4UgUoQTwK2Wl40+EfEX+5KjgwzBTc5sLi33BY4sgc5lVA8ilsgegR5j26Eg== 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 To: Aleksandar Markovic , Huacai Chen , Marc Zyngier , Paul Mackerras , Paolo Bonzini , David Stevens , Zhenyu Wang , Zhi Wang 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> Subject: Re: [Intel-gfx] [PATCH 0/6] KVM: Remove uses of struct page from x86 and arm64 MMU X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Wanpeng Li , kvm@vger.kernel.org, David Stevens , Alexandru Elisei , intel-gfx@lists.freedesktop.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, kvmarm@lists.cs.columbia.edu, Will Deacon , Suzuki K Poulose , James Morse , kvm-ppc@vger.kernel.org, Sean Christopherson , Vitaly Kuznetsov , linux-mips@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Jim Mattson Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" 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 using >>>> 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. To >>>> 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 type >>>> 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) >> >> 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. > > And it also works (the reproducer gets an infinite stream of userspace > exits and especially does not crash). We can still go for David's > solution later since MMU notifiers are able to deal with this pages, but > 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; } +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_struct *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. */ - kvm_get_pfn(pfn); + if (!kvm_try_get_pfn(pfn)) + r = -EFAULT; out: pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; - return 0; + + return r; } /* -- 2.23.0 _______________________________________________ Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicholas Piggin Date: Thu, 24 Jun 2021 12:57:54 +0000 Subject: Re: [PATCH 0/6] KVM: Remove uses of struct page from x86 and arm64 MMU Message-Id: <1624539354.6zggpdrdbw.astroid@bobo.none> List-Id: 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 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 using >>>> 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. To >>>> 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 type >>>> 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) >> >> 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. > > And it also works (the reproducer gets an infinite stream of userspace > exits and especially does not crash). We can still go for David's > solution later since MMU notifiers are able to deal with this pages, but > 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; } +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_struct *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. */ - kvm_get_pfn(pfn); + if (!kvm_try_get_pfn(pfn)) + r = -EFAULT; out: pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; - return 0; + + return r; } /* -- 2.23.0