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=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 0A822C433E9 for ; Fri, 26 Feb 2021 16:22:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7C3E464EF0 for ; Fri, 26 Feb 2021 16:22:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7C3E464EF0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D55A18D0002; Fri, 26 Feb 2021 11:22:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D2B9A8D0001; Fri, 26 Feb 2021 11:22:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C698F8D0002; Fri, 26 Feb 2021 11:22:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id B05BE8D0001 for ; Fri, 26 Feb 2021 11:22:41 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 6EAF58248068 for ; Fri, 26 Feb 2021 16:22:41 +0000 (UTC) X-FDA: 77860937322.01.685B5C7 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf23.hostedemail.com (Postfix) with ESMTP id B52B4A0004C4 for ; Fri, 26 Feb 2021 16:22:36 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 44B4CAB8C; Fri, 26 Feb 2021 16:22:28 +0000 (UTC) From: Vlastimil Babka To: stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrea Arcangeli , "Kirill A. Shutemov" , Jann Horn , Linus Torvalds , Michal Hocko , Hugh Dickins , Vlastimil Babka , Nicolai Stange Subject: [PATCH 4.9 STABLE] mm, thp: make do_huge_pmd_wp_page() lock page for testing mapcount Date: Fri, 26 Feb 2021 17:22:00 +0100 Message-Id: <20210226162200.20548-1-vbabka@suse.cz> X-Mailer: git-send-email 2.30.1 In-Reply-To: <26569718-050f-fc90-e3ac-79edfaae9ac7@suse.cz> References: <26569718-050f-fc90-e3ac-79edfaae9ac7@suse.cz> MIME-Version: 1.0 X-Stat-Signature: 5n78rmfr3aodkzost7khs17gx918818q X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B52B4A0004C4 Received-SPF: none (suse.cz>: No applicable sender policy available) receiver=imf23; identity=mailfrom; envelope-from=""; helo=mx2.suse.de; client-ip=195.135.220.15 X-HE-DKIM-Result: none/none X-HE-Tag: 1614356556-753996 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Jann reported [1] a race between __split_huge_pmd_locked() and page_trans_huge_map_swapcount() which can result in a page to be reused instead of COWed. This was later assigned CVE-2020-29368. This was fixed by commit c444eb564fb1 ("mm: thp: make the THP mapcount at= omic against __split_huge_pmd_locked()") by doing the split under the page loc= k, while all users of page_trans_huge_map_swapcount() were already also unde= r page lock. The fix was backported also to 4.9 stable series. When testing the backport on a 4.12 based kernel, Nicolai noticed the POC= from [1] still reproduces after backporting c444eb564fb1 and identified a miss= ing page lock in do_huge_pmd_wp_page() around the call to page_trans_huge_mapcount(). The page lock was only added in ba3c4ce6def4 = ("mm, THP, swap: make reuse_swap_page() works for THP swapped out") in 4.14. Th= e commit also wrapped page_trans_huge_mapcount() into page_trans_huge_map_swapcount() for the purposes of COW decisions. I have verified that 4.9.y indeed also reproduces with the POC. Backporti= ng ba3c4ce6def4 alone however is not possible as it's part of a larger effor= t of optimizing THP swapping, which would be risky to backport fully. Therefore this 4.9-stable-only patch just wraps page_trans_huge_mapcount(= ) in page_trans_huge_mapcount() under page lock the same way as ba3c4ce6def= 4 does, but without the page_trans_huge_map_swapcount() part. Other callers of page_trans_huge_mapcount() are all under page lock already. I have ver= ified the POC no longer reproduces afterwards. [1] https://bugs.chromium.org/p/project-zero/issues/detail?id=3D2045 Reported-by: Nicolai Stange Signed-off-by: Vlastimil Babka --- mm/huge_memory.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 05ca01ef97f7..14cd0ef33b62 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1022,6 +1022,19 @@ int do_huge_pmd_wp_page(struct fault_env *fe, pmd_= t orig_pmd) * We can only reuse the page if nobody else maps the huge page or it's * part. */ + if (!trylock_page(page)) { + get_page(page); + spin_unlock(fe->ptl); + lock_page(page); + spin_lock(fe->ptl); + if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) { + unlock_page(page); + put_page(page); + goto out_unlock; + } + put_page(page); + } + if (page_trans_huge_mapcount(page, NULL) =3D=3D 1) { pmd_t entry; entry =3D pmd_mkyoung(orig_pmd); @@ -1029,8 +1042,10 @@ int do_huge_pmd_wp_page(struct fault_env *fe, pmd_= t orig_pmd) if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1)) update_mmu_cache_pmd(vma, fe->address, fe->pmd); ret |=3D VM_FAULT_WRITE; + unlock_page(page); goto out_unlock; } + unlock_page(page); get_page(page); spin_unlock(fe->ptl); alloc: --=20 2.30.1