From nobody Sun Apr 19 05:32:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2ADF4C43334 for ; Wed, 6 Jul 2022 02:48:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231151AbiGFCsK (ORCPT ); Tue, 5 Jul 2022 22:48:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229648AbiGFCsJ (ORCPT ); Tue, 5 Jul 2022 22:48:09 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B2971A05C; Tue, 5 Jul 2022 19:48:07 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Ld3nl48DhzcftB; Wed, 6 Jul 2022 10:46:03 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 6 Jul 2022 10:48:04 +0800 Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 6 Jul 2022 10:48:04 +0800 From: Liu Shixin To: Greg Kroah-Hartman , Andrew Morton , Matthew Wilcox , Jan Kara , William Kucharski , "Christoph Hellwig" CC: , , Liu Shixin Subject: [PATCH 5.15] mm/filemap: fix UAF in find_lock_entries Date: Wed, 6 Jul 2022 11:24:34 +0800 Message-ID: <20220706032434.579610-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Release refcount after xas_set to fix UAF which may cause panic like this: page:ffffea000491fa40 refcount:1 mapcount:0 mapping:0000000000000000 index= :0x1 pfn:0x1247e9 head:ffffea000491fa00 order:3 compound_mapcount:0 compound_pincount:0 memcg:ffff888104f91091 flags: 0x2fffff80010200(slab|head|node=3D0|zone=3D2|lastcpupid=3D0x1fffff) ... page dumped because: VM_BUG_ON_PAGE(PageTail(page)) ------------[ cut here ]------------ kernel BUG at include/linux/page-flags.h:632! invalid opcode: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN CPU: 1 PID: 7642 Comm: sh Not tainted 5.15.51-dirty #26 ... Call Trace: __invalidate_mapping_pages+0xe7/0x540 drop_pagecache_sb+0x159/0x320 iterate_supers+0x120/0x240 drop_caches_sysctl_handler+0xaa/0xe0 proc_sys_call_handler+0x2b4/0x480 new_sync_write+0x3d6/0x5c0 vfs_write+0x446/0x7a0 ksys_write+0x105/0x210 do_syscall_64+0x35/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7f52b5733130 ... This problem has been fixed on mainline by patch 6b24ca4a1a8d ("mm: Use multi-index entries in the page cache") since it deletes the related code. Fixes: 5c211ba29deb ("mm: add and use find_lock_entries") Signed-off-by: Liu Shixin --- mm/filemap.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 00e391e75880..24b5d7ebdc29 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2087,16 +2087,18 @@ unsigned find_lock_entries(struct address_space *ma= pping, pgoff_t start, { XA_STATE(xas, &mapping->i_pages, start); struct page *page; + bool putpage; =20 rcu_read_lock(); while ((page =3D find_get_entry(&xas, end, XA_PRESENT))) { + putpage =3D true; if (!xa_is_value(page)) { if (page->index < start) - goto put; + goto next; if (page->index + thp_nr_pages(page) - 1 > end) - goto put; + goto next; if (!trylock_page(page)) - goto put; + goto next; if (page->mapping !=3D mapping || PageWriteback(page)) goto unlock; VM_BUG_ON_PAGE(!thp_contains(page, xas.xa_index), @@ -2105,20 +2107,24 @@ unsigned find_lock_entries(struct address_space *ma= pping, pgoff_t start, indices[pvec->nr] =3D xas.xa_index; if (!pagevec_add(pvec, page)) break; + putpage =3D false; goto next; unlock: unlock_page(page); -put: - put_page(page); next: if (!xa_is_value(page) && PageTransHuge(page)) { unsigned int nr_pages =3D thp_nr_pages(page); =20 /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */ xas_set(&xas, page->index + nr_pages); - if (xas.xa_index < nr_pages) + if (xas.xa_index < nr_pages) { + if (putpage) + put_page(page); break; + } } + if (putpage) + put_page(page); } rcu_read_unlock(); =20 --=20 2.25.1