From patchwork Thu Feb 22 03:02:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gong Ruiqi X-Patchwork-Id: 13566670 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 DA22DC48BEB for ; Thu, 22 Feb 2024 02:56:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39E996B0071; Wed, 21 Feb 2024 21:56:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 34EF56B0074; Wed, 21 Feb 2024 21:56:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 216046B0075; Wed, 21 Feb 2024 21:56:29 -0500 (EST) 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 0FA2A6B0071 for ; Wed, 21 Feb 2024 21:56:29 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A15751403F9 for ; Thu, 22 Feb 2024 02:56:28 +0000 (UTC) X-FDA: 81817926456.19.8773FB6 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf25.hostedemail.com (Postfix) with ESMTP id 7240DA0002 for ; Thu, 22 Feb 2024 02:56:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of gongruiqi1@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=gongruiqi1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708570587; 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=3TAR7++Af3dtiN6aUMWvBvnyXnQiIbi7KgFq13HGn+4=; b=uY6fO/JFnRc9R4Ao+dmt3dLynkG7XzhTv1PwPQQwujHJqswGSlxwtP7j+/5+/GFoIlN4qj KGZbp6jJoBg7QaXyR1reSpMJQkoUdyCMxt2NxkoUAdX5fVJH5Z7JmYg3Vxz4kcsdmmsG3Z u825NSz/2yrKKGktoLH35Pe65bc8Pqc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of gongruiqi1@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=gongruiqi1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708570587; a=rsa-sha256; cv=none; b=kYpffA/+YjXLq6SOnvbq/GZLjbNrcVvhHzngfu3XuxZ/mA8juW1Owa0DRaU/UJxUhm5x4A 6RtSnSFOtgL0ldIqaQz8+JTK9NQNHnolgSkQzOxOQBBudiVb5RleQpnFW0aDT2D4OBos8u wGZ+aJRDln/FoVGkAJy6jGmqa8/j0qY= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4TgHm92VknzvVwW; Thu, 22 Feb 2024 10:54:17 +0800 (CST) Received: from dggpemd100002.china.huawei.com (unknown [7.185.36.164]) by mail.maildlp.com (Postfix) with ESMTPS id 7BA5418005D; Thu, 22 Feb 2024 10:56:20 +0800 (CST) Received: from huawei.com (10.67.174.33) by dggpemd100002.china.huawei.com (7.185.36.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.28; Thu, 22 Feb 2024 10:56:20 +0800 From: "GONG, Ruiqi" To: , , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song CC: , , Wang Weiyang , Xiu Jianfeng Subject: [PATCH v2 stable] memcg: add refcnt for pcpu stock to avoid UAF problem in drain_all_stock() Date: Thu, 22 Feb 2024 11:02:37 +0800 Message-ID: <20240222030237.82486-1-gongruiqi1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.67.174.33] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemd100002.china.huawei.com (7.185.36.164) X-Rspamd-Queue-Id: 7240DA0002 X-Rspam-User: X-Stat-Signature: qozs3ycpwbk6n1c4ewsqzynagsua9uym X-Rspamd-Server: rspam01 X-HE-Tag: 1708570585-625310 X-HE-Meta: U2FsdGVkX1/h4hSg3kIb3PStzw29W4hq+pLCDS9Y/CiexADlhzLq+3TtazLAG3KC6qGKDDNCeVUpk9/B2Eh5M7r810pkdmb3QtCYakbca4yFZxKchLRe/xGf5PP1LugBw8katY4V2qmdrdw4H5KSdnb+5ZTkDpVnNjdhuU+vh1VJHuj6TehanHgpAQrnvyEEZ9Rba37ZLNjIc72IBbGRkYNqZnhBzfFd4M4UcegkdLHzmvglnnd956agRpbeF7Jyo400qRK/9ORbeaxXWQy+u7/JwhJh+Jrca+7tOZfpvIw0A01EYZSzEp4wK3xjsuKhZ2PyIqL9ZsO4vSYz8REYw4O84RpHZxFoWUOPCOANH3bwLEgsvpRi2rNM54cUUL7TTmHJvhr0ucMSBWb/Ssk3JYxM8jlqiIMQMpUzFbTqPxGqqKXpmd+rF1mzBJZyTHSuMqvRbf6pJmdbAyCHcESNriev8nhkfSCzlKoTlle44GeNtfUOIJe5Jieiiy9blyf2Ui96N/yqjuD7bKg5Fzz4ol3QeYem7awmV/m2iEcTlhYknrie3ITDbPtSXTKEfCz/B+DKjPJvmo5ZlFn4catO6joAmu2nRRjz6d2gGFbHEQbSe6yavbiLYpxzZ2ryLAFnoNt+PBWPLQXURUN9K+DoNdqz2cqSFDoGIr3XfGG1Qaxdz2LPh0+kYwMCzsBYX2S/Mrhf2bB4hweXqRp+iunYRTVcdD988ji08xKeAWEWyWP3jynKMB1ENrSaXqkzItXCQOfxp3UHnZ5I1dZfJtuzGG2o6Pq1Nay3hqAZakd4xZ2RynV8L8AbSONyGqlmYnM3tM1fmzIxlbOv5eOJRc1ebLSHfpyet1WSCOJRb65HHg0lKn+vCI0C0hz92Ty4h+H1KsIEZMNyPS4= 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: List-Subscribe: List-Unsubscribe: commit 1a3e1f40962c445b997151a542314f3c6097f8c3 upstream. NOTE: This is a partial backport since we only need the refcnt between memcg and stock to fix the problem stated below, and in this way multiple versions use the same code and align with each other. --- There was a kernel panic happened on an in-house environment running 3.10, and the same problem was reproduced on 4.19: general protection fault: 0000 [#1] SMP PTI CPU: 1 PID: 2085 Comm: bash Kdump: loaded Tainted: G L 4.19.90+ #7 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/01/2014 RIP: 0010 drain_all_stock+0xad/0x140 Code: 00 00 4d 85 ff 74 2c 45 85 c9 74 27 4d 39 fc 74 42 41 80 bc 24 28 04 00 00 00 74 17 49 8b 04 24 49 8b 17 48 8b 88 90 02 00 00 <48> 39 8a 90 02 00 00 74 02 eb 86 48 63 88 3c 01 00 00 39 8a 3c 01 RSP: 0018:ffffa7efc5813d70 EFLAGS: 00010202 RAX: ffff8cb185548800 RBX: ffff8cb89f420160 RCX: ffff8cb1867b6000 RDX: babababababababa RSI: 0000000000000001 RDI: 0000000000231876 RBP: 0000000000000000 R08: 0000000000000415 R09: 0000000000000002 R10: 0000000000000000 R11: 0000000000000001 R12: ffff8cb186f89040 R13: 0000000000020160 R14: 0000000000000001 R15: ffff8cb186b27040 FS: 00007f4a308d3740(0000) GS:ffff8cb89f440000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007ffe4d634a68 CR3: 000000010b022000 CR4: 00000000000006e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: mem_cgroup_force_empty_write+0x31/0xb0 cgroup_file_write+0x60/0x140 ? __check_object_size+0x136/0x147 kernfs_fop_write+0x10e/0x190 __vfs_write+0x37/0x1b0 ? selinux_file_permission+0xe8/0x130 ? security_file_permission+0x2e/0xb0 vfs_write+0xb6/0x1a0 ksys_write+0x57/0xd0 do_syscall_64+0x63/0x250 ? async_page_fault+0x8/0x30 entry_SYSCALL_64_after_hwframe+0x5c/0xc1 Modules linked in: ... It is found that in case of stock->nr_pages == 0, the memcg on stock->cached could be freed due to its refcnt decreased to 0, which made stock->cached become a dangling pointer. It could cause a UAF problem in drain_all_stock() in the following concurrent scenario. Note that drain_all_stock() doesn't disable irq but only preemption. CPU1 CPU2 ============================================================================== stock->cached = memcgA (freed) drain_all_stock(memcgB) rcu_read_lock() memcg = CPU1's stock->cached (memcgA) (interrupted) refill_stock(memcgC) drain_stock(memcgA) stock->cached = memcgC stock->nr_pages += xxx (> 0) stock->nr_pages > 0 mem_cgroup_is_descendant(memcgA, memcgB) [UAF] rcu_read_unlock() This problem is, unintentionally, fixed at 5.9, where commit 1a3e1f40962c ("mm: memcontrol: decouple reference counting from page accounting") adds memcg refcnt for stock. Therefore affected LTS versions include 4.19 and 5.4. For 4.19, memcg's css offline process doesn't call drain_all_stock(). so it's easier for the released memcg to be left on the stock. For 5.4, although mem_cgroup_css_offline() does call drain_all_stock(), but the flushing could be skipped when stock->nr_pages happens to be 0, and besides the async draining could be delayed and take place after the UAF problem has happened. Fix this problem by adding (and decreasing) memcg's refcnt when memcg is put onto (and removed from) stock, just like how commit 1a3e1f40962c ("mm: memcontrol: decouple reference counting from page accounting") does. After all, "being on the stock" is a kind of reference with regards to memcg. As such, it's guaranteed that a css on stock would not be freed. It's good to mention that refill_stock() is executed in an irq-disabled context, so the drain_stock() patched with css_put() would not actually free memcgA until the end of refill_stock(), since css_put() is an RCU free and it's still in grace period. For CPU2, the access to CPU1's stock->cached is protected by rcu_read_lock(), so in this case it gets either NULL from stock->cached or a memcgA that is still good. Cc: stable@vger.kernel.org # 4.19 5.4 Fixes: cdec2e4265df ("memcg: coalesce charging via percpu storage") Signed-off-by: GONG, Ruiqi Acked-by: Michal Hocko --- v2: - Add a statement of this patch being a partial backport - Add a paragraph to mention the grace period in refill_stock() mm/memcontrol.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5a366cf79821..8c04296df1c7 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2015,6 +2015,9 @@ static void drain_stock(struct memcg_stock_pcp *stock) { struct mem_cgroup *old = stock->cached; + if (!old) + return; + if (stock->nr_pages) { page_counter_uncharge(&old->memory, stock->nr_pages); if (do_memsw_account()) @@ -2022,6 +2025,8 @@ static void drain_stock(struct memcg_stock_pcp *stock) css_put_many(&old->css, stock->nr_pages); stock->nr_pages = 0; } + + css_put(&old->css); stock->cached = NULL; } @@ -2057,6 +2062,7 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) stock = this_cpu_ptr(&memcg_stock); if (stock->cached != memcg) { /* reset if necessary */ drain_stock(stock); + css_get(&memcg->css); stock->cached = memcg; } stock->nr_pages += nr_pages;