From patchwork Thu May 25 03:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13254709 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41683C77B7C for ; Thu, 25 May 2023 02:28:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A20F6B0078; Wed, 24 May 2023 22:28:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8529A6B007B; Wed, 24 May 2023 22:28:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74087900002; Wed, 24 May 2023 22:28:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6373C6B0078 for ; Wed, 24 May 2023 22:28:18 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 297441C737D for ; Thu, 25 May 2023 02:28:18 +0000 (UTC) X-FDA: 80827193076.22.B54E8C1 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf24.hostedemail.com (Postfix) with ESMTP id D2F2518000E for ; Thu, 25 May 2023 02:28:14 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684981695; h=from:from:sender: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: references; bh=UA9HU4fTiTYyVcdOPFborzHaAxg3hV8b0nmBsG1yH+A=; b=TbwH085nQMd3DHlOmPlj5bMOFEC5UmgtA8icCr+lvonB2cWPR8ClzY9F9C2AQUmE4BBgO8 Sn21i7O8T5aqN0wBm4eYb+1/mHrCROJhyTNpG/puCncOeJ5G7L8lgGoip60sjgeLIWzfTm w5F8BjYWsEVK05j5hj3zE8n5M5bS/BI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684981695; a=rsa-sha256; cv=none; b=S8SdqNfJzKblpi97sNZeKwpWLfQLpqAryCr6xLl1JMoiUL3gZe3f6RxONvWKVD0Pc9vdxd 6b+hvkqgYqehtF3tWKJbqkIjHbgkAQ8bhioySdFMZ4GG0tr8n5H+dpINgQVMuhrurYu4SR iGTIyJrPzQdWAaaxWAYFm8TsoCddat0= Received: from dggpeml500011.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QRX0G3qb3zTktT; Thu, 25 May 2023 10:23:10 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 25 May 2023 10:28:09 +0800 From: Tu Jinjiang To: , , , CC: , Subject: [PATCH] mm: shmem: Fix UAF bug in shmem_show_options() Date: Thu, 25 May 2023 11:16:40 +0800 Message-ID: <20230525031640.593733-1-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500011.china.huawei.com (7.185.36.84) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: D2F2518000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 164jpwnpawowhj37rdpxwjkwsymr13nm X-HE-Tag: 1684981694-226088 X-HE-Meta: U2FsdGVkX1/JRfqnLMxnvO6Q12klN7SHAQ493Io6mXXKgRtsgwNHfbPq4YycaAyzNvDO/Tz1su78YV6SOmp/8UZDa+0USwN8SzXnyt0MZf28Y1H9ZLupe5utNcKalpZQ4CN+UO4TgvK3anAhfOsfSjKZPRFIB3yBPn6FQlj87w2tb6QKW9s6ZJz9LcoTzp7qPvzKc96e/Qmu2wKqRLzUl+sC2DDBR5wITURBJ+RnG85qb2hIxzr8ZBJQqZ/f3K5r7qlcPae+lzsEHyfl3584H/o0lyG4WgvK4bs/U16P1rEsv1Yxm2PUrQUAo3d0a0p9f+h+evV1ag5CSxfjNgne0hFpCIvazvyyAV6QB5K7SyHDKRXGBvqvbJiCBHpnNXEr2bRpoVikEp+Z+JeZKDgaq3TjNpbJXonpg2DdZwbvmi0SlzJDUYGdsEbgnmvTrfEsFXIrr2oMz4JEIo4zF3QkNNJnQD/svgnVwzvMAt66RIK4m3v0eftQRMChyhSP8W0WuDssKnkRWNQACDyNFTNvR/1SI5d9lGJiKsyjsUfzv4uKTMjexgr62+CRXy1PftNZlCrIpiiETxtL+85TCAKrE8DpQtn/eX5AdZ+PmCsC218RUgA8XKLLtrcOdOWZyEUN+9Q9ebUEyy6oWPMN4715UknOG+sksEDf1GdIp2qR6A5Wq2w4GSyiPIyX2PSAID1V5i8NH8WZvZt7QUT+aRbkKDsriLVIjNSxgMDPLlunfpsKFOpO1P1UYCIysmonE3IWVgdmK8N9HvjvCO7waycFEr6U+UbWXQ7zd7mHQACU6O1InYe6055WMhTY/bWnYtqQf3JZpWYWar59+BHIKqTMbSIWMXzMgXFkDo6+3PVMGvGTbRRdciYZKM2yQOJ8apSbMIx4LAZG6sSetBnrNHJY32YhQtyVJyDOpoXV83gZE+xPpRRkFgaWN9LCFsnihTVDQB5vGfbo+3vHI6B3kAb OzLHs0F8 LoXxk0kXTTsajbKeRJc2TYceR/TK+e96AJy0N+Os8SqKJjVTkXM0I6+66exZ62pjU7UQu4shrrPXE8/X2dIFbiRhffIXUVsp2pDwZdpo/lZT7++YafH0XGm1/cElwzj2HWQdGy7GQJ0NaHtov4FSTe/ydi61tUFks9uXvCczAlrobXAZvOcstJfrLcFnK1TRQGpAj 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: shmem_show_options() uses sbinfo->mpol without adding it's refcnt. This may lead to race with replacement of the mpol by remount. The execution sequence is as follows. CPU0 CPU1 shmem_show_options() shmem_reconfigure() shmem_show_mpol(seq, sbinfo->mpol) mpol = sbinfo->mpol mpol_put(mpol) mpol->mode The KASAN report is as follows. BUG: KASAN: slab-use-after-free in shmem_show_options+0x21b/0x340 Read of size 2 at addr ffff888124324004 by task mount/2388 CPU: 2 PID: 2388 Comm: mount Not tainted 6.4.0-rc3-00017-g9d646009f65d-dirty #8 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: dump_stack_lvl+0x37/0x50 print_report+0xd0/0x620 ? shmem_show_options+0x21b/0x340 ? __virt_addr_valid+0xf4/0x180 ? shmem_show_options+0x21b/0x340 kasan_report+0xb8/0xe0 ? shmem_show_options+0x21b/0x340 shmem_show_options+0x21b/0x340 ? __pfx_shmem_show_options+0x10/0x10 ? strchr+0x2c/0x50 ? strlen+0x23/0x40 ? seq_puts+0x7d/0x90 show_vfsmnt+0x1e6/0x260 ? __pfx_show_vfsmnt+0x10/0x10 ? __kasan_kmalloc+0x7f/0x90 seq_read_iter+0x57a/0x740 vfs_read+0x2e2/0x4a0 ? __pfx_vfs_read+0x10/0x10 ? down_write_killable+0xb8/0x140 ? __pfx_down_write_killable+0x10/0x10 ? __fget_light+0xa9/0x1e0 ? up_write+0x3f/0x80 ksys_read+0xb8/0x150 ? __pfx_ksys_read+0x10/0x10 ? fpregs_assert_state_consistent+0x55/0x60 ? exit_to_user_mode_prepare+0x2d/0x120 do_syscall_64+0x3c/0x90 entry_SYSCALL_64_after_hwframe+0x72/0xdc Allocated by task 2387: kasan_save_stack+0x22/0x50 kasan_set_track+0x25/0x30 __kasan_slab_alloc+0x59/0x70 kmem_cache_alloc+0xdd/0x220 mpol_new+0x83/0x150 mpol_parse_str+0x280/0x4a0 shmem_parse_one+0x364/0x520 vfs_parse_fs_param+0xf8/0x1a0 vfs_parse_fs_string+0xc9/0x130 shmem_parse_options+0xb2/0x110 path_mount+0x597/0xdf0 do_mount+0xcd/0xf0 __x64_sys_mount+0xbd/0x100 do_syscall_64+0x3c/0x90 entry_SYSCALL_64_after_hwframe+0x72/0xdc Freed by task 2389: kasan_save_stack+0x22/0x50 kasan_set_track+0x25/0x30 kasan_save_free_info+0x2e/0x50 __kasan_slab_free+0x10e/0x1a0 kmem_cache_free+0x9c/0x350 shmem_reconfigure+0x278/0x370 reconfigure_super+0x383/0x450 path_mount+0xcc5/0xdf0 do_mount+0xcd/0xf0 __x64_sys_mount+0xbd/0x100 do_syscall_64+0x3c/0x90 entry_SYSCALL_64_after_hwframe+0x72/0xdc The buggy address belongs to the object at ffff888124324000 which belongs to the cache numa_policy of size 32 The buggy address is located 4 bytes inside of freed 32-byte region [ffff888124324000, ffff888124324020) ================================================================== To fix the bug, shmem_get_sbmpol() / mpol_put() needs to be called before / after shmem_show_mpol() call. Signed-off-by: Tu Jinjiang Reviewed-by: Kefeng Wang Acked-by: Hugh Dickins --- mm/shmem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index e40a08c5c6d7..5e54ab5f61f2 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3726,6 +3726,7 @@ static int shmem_reconfigure(struct fs_context *fc) static int shmem_show_options(struct seq_file *seq, struct dentry *root) { struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); + struct mempolicy *mpol; if (sbinfo->max_blocks != shmem_default_max_blocks()) seq_printf(seq, ",size=%luk", @@ -3768,7 +3769,9 @@ static int shmem_show_options(struct seq_file *seq, struct dentry *root) if (sbinfo->huge) seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); #endif - shmem_show_mpol(seq, sbinfo->mpol); + mpol = shmem_get_sbmpol(sbinfo); + shmem_show_mpol(seq, mpol); + mpol_put(mpol); if (sbinfo->noswap) seq_printf(seq, ",noswap"); return 0;