Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp180576pxv; Thu, 24 Jun 2021 05:44:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1t7XPeglNgmX5trtxoVkU/0QbZK1ga6LSQfZLPns2XYR23zK3vhYBN5XVAZDV1a8of8YA X-Received: by 2002:a17:906:144b:: with SMTP id q11mr5073999ejc.478.1624538681655; Thu, 24 Jun 2021 05:44:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624538681; cv=none; d=google.com; s=arc-20160816; b=nI0RPyGnYseRQ0uDiu0pTbnHY/eyTygaL6oFTNEkzLLjYJKonWlfgFqNlpu3FkLw7G 3DNip4+fJj6n+M4RaZznLjPlZUI5gmbWJ8Og8r0FCc4BpFHc1jCDMfXoXlYFABFosk96 4e9z/qKqdWfQR5ztN8U05BOk6oGpDLxT3lfNUdZ3QUsVevjalH3vezYFt20QraM/M/WZ UImlPC170bghPQ/vDi/noRqpjPn894hxp6ZkYuybi7DsUhI8P8MYEWhByZ1f+6hDBzph 5C7m119BmZ7kA1u2LOM50gA39AztSO9lOqayc7p8Ekc2Lht/9TLPwgnMDHeX2N7macWZ FGeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=Ao/aik86DETd7ceeMOwWvzTcaJv+rZayOURU+D31sn8=; b=pIIpqe5f2hmtCqUaSjoEf8LIvaqaV+aXKbmQLtI2ztcx2lEVm1mTn6sdKncArbHv95 xz6mxEEL0eKtO2fujXwF0JHgxvMG2a+HGcjN/wMAiCVWjKVB/Fqojal88eCBSLDME7m1 PUmMJO9q8vVVk7sSfnL4PgDjRW0jEuB78A5R4gxDwquminfpzr5RpVGEgp3KXeaFGBbd ytTVdOjzbWD2/uQE00bitEncqnKgnWTr/rGjSXKRnJ0/D/JXfOaBw1S0d0DAwLI0Fy+b BoYHK5ra3QgtoiHVLLmEJdlR8wAJep3Zz5zvdMPAJYmVkmUnA6yhz6F8Gv8FqSjv/WNZ AY+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GjvelJK9; 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=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k19si3586016ejv.176.2021.06.24.05.44.17; Thu, 24 Jun 2021 05:44:41 -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=@redhat.com header.s=mimecast20190719 header.b=GjvelJK9; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231848AbhFXMnq (ORCPT + 99 others); Thu, 24 Jun 2021 08:43:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:36618 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231817AbhFXMnp (ORCPT ); Thu, 24 Jun 2021 08:43:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624538486; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ao/aik86DETd7ceeMOwWvzTcaJv+rZayOURU+D31sn8=; b=GjvelJK9NqzwPH+wQt4p2rrMHQ+NmPcPSNVXrH2f0EGBGBkRWu28p8uhXdnkGg+jIxxi35 K18CZYpwAQXIXJe/qoc0AAtBS6Xqn7n7gbbZtzOsuIr22LchymDudqsIDKQkzI+6dF6VBX PM/RTYHUvoAjjDy3wrUPGHfG0iT5m+I= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-148-POEPgkm2O36_LcSmJJd9Fg-1; Thu, 24 Jun 2021 08:41:25 -0400 X-MC-Unique: POEPgkm2O36_LcSmJJd9Fg-1 Received: by mail-wr1-f72.google.com with SMTP id w10-20020a5d608a0000b0290124b2be1b59so521682wrt.20 for ; Thu, 24 Jun 2021 05:41:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Ao/aik86DETd7ceeMOwWvzTcaJv+rZayOURU+D31sn8=; b=Vj11swe7uZyHJnsNKr/eqnJTF5ndbp3ckGxuAUJml1EGk+PCW9tjnpi6ed5PwxS7uc rgJVPzxj1x5fE7f2mxtN7imY1QQsew01hmslZBCJRysgqCzVTkWIVEfj0y3THRAUqFFU otMvgiEqTTJibNQ8NFoOaLtWKzRffNz/YOj/58wEIx5TvtkXCo9Ng/qaK0UR7jaAbuHh LhobXnRViihu7iIyn7VCjlvwbqccOTvzzUNbHzKKvRf1fHKUKfmieIjxRk3GQDJg3fDe Q2GT5raq1GkRPLt+z4uhMWmWhBqsVKbozudI19F/9jMDxzrV+UkvEOmhI30sYIVASvZJ PK2A== X-Gm-Message-State: AOAM532YAdjw/TmdhSUIMhCLp5sDHMaGOUmvsuZdbubPBDuhopkyuj2o 4evJIDm30hRf/vQ2RdsNKQG94d8N/Tpv5aXJkVBjith17zvQEBaU6BRW1ClP0894ZmE97G+E/Ks YSK9pL8yMxy4kF7jceR8GsXZI X-Received: by 2002:adf:f88e:: with SMTP id u14mr4211984wrp.391.1624538484097; Thu, 24 Jun 2021 05:41:24 -0700 (PDT) X-Received: by 2002:adf:f88e:: with SMTP id u14mr4211952wrp.391.1624538483934; Thu, 24 Jun 2021 05:41:23 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id 22sm2818691wmi.4.2021.06.24.05.41.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Jun 2021 05:41:23 -0700 (PDT) Subject: Re: [PATCH 0/6] KVM: Remove uses of struct page from x86 and arm64 MMU To: Nicholas Piggin , Aleksandar Markovic , Huacai Chen , Marc Zyngier , Paul Mackerras , 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> From: Paolo Bonzini Message-ID: <0d3a699a-15eb-9f1b-0735-79d14736f38c@redhat.com> Date: Thu, 24 Jun 2021 14:41:21 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <1624534759.nj0ylor2eh.astroid@bobo.none> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. If you provide a Signed-off-by, I can integrate it. Paolo > --- > > > 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). > > --- > 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; > } > > /* >