From patchwork Fri Apr 21 17:40:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13220545 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 B1801C7618E for ; Fri, 21 Apr 2023 17:41:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 465E96B0072; Fri, 21 Apr 2023 13:41:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4147D6B0074; Fri, 21 Apr 2023 13:41:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 302F66B0075; Fri, 21 Apr 2023 13:41:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1B2AF6B0072 for ; Fri, 21 Apr 2023 13:41:35 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DE041A04F7 for ; Fri, 21 Apr 2023 17:41:34 +0000 (UTC) X-FDA: 80706115308.28.F6172D4 Received: from out-60.mta1.migadu.com (out-60.mta1.migadu.com [95.215.58.60]) by imf30.hostedemail.com (Postfix) with ESMTP id E760080019 for ; Fri, 21 Apr 2023 17:41:32 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="PFr/EZar"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.60 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682098893; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8lsVSCZFASjGj882L2SUq8X57TGcITsPyPXAfEkTLgs=; b=7ZNKxNIodNh8h+APAZ4PfJ4vi4bLzwWUWKbkhVlpFSfYdq2P4LXNV54FYcit5jDn+n1BUO lk1JXC1+x7bEjbgLbCIjLfhzX/milP9t6cdcvLFLWYNIOH3dNWcHPBNkUWee2zqOAmhleE jC0Ik62NytLVfgUTL7Nj+pJqDdGmYVo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="PFr/EZar"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.60 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682098893; a=rsa-sha256; cv=none; b=aZAIs5PxXRQGSdDMNHEAtTYuhpFW6k69PyvGE5UHM1Z5TtDtHEZmmzx/7eCJdcon0JvE4x dIsAIcllh8IYeHaJ1yRogzz9547GTxS6y9X9pKKgd/gA4UAiMBPow/hys7tPnKO8PesNcD 4YPoxmEI+xGwrDV7lZGWhvFWY2TLRuI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1682098890; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8lsVSCZFASjGj882L2SUq8X57TGcITsPyPXAfEkTLgs=; b=PFr/EZaro7ssLUyyiZMxEP8YjsXfPwxeIaTBtv49tB58yI+pA+FCiq9xPo1wG91VUqogAG FyfKBvQ9Z2R68a2BgzhattI724YLKBAbj5PronSuPVAPPPW8Z0PtgStuMgWLu5uwlluwGl HlqkPYGxLE4udEvViO3aTzpvtlubx/U= From: Roman Gushchin To: linux-mm@kvack.org, Andrew Morton Cc: Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , linux-kernel@vger.kernel.org, Roman Gushchin , syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com, Dmitry Vyukov Subject: [PATCH] mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required() Date: Fri, 21 Apr 2023 10:40:54 -0700 Message-Id: <20230421174054.3434533-1-roman.gushchin@linux.dev> In-Reply-To: <00000000000058b63f05f9d98811@google.com> References: <00000000000058b63f05f9d98811@google.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E760080019 X-Stat-Signature: awb1j6aoceizh6cni46ma4d3i4u6yrua X-HE-Tag: 1682098892-542790 X-HE-Meta: U2FsdGVkX18rcXk4fURKykrCfakWTXHV6jx/MEzuHAdS6kSmRrVvs3TLLxwBXTKWuRw2Qk39fAyAWxZlAkplCNckw+aki8LR+gdEjhQR7KoSDik9B21Z/1nDtT5hUo2/RiOPzoPm1o1jJXuvsigKOh3kS4xnV1k+NhTb8sbLVOub9ki+TbtBshdxktnEb/6yZwJOSwGjCoU4DDPv7FLxEk+PVpmXXFe+d/sytXAp+oESgB/KifgrXOV8gehfPBgJC7sz1Jx/XVZz/36VzoODm4XcM/tosRNcPUbg2QNe2Y/7jReGqZNenEu7oZXpbOBnuZwZMpyveHSo98oDvIWn+jcEM5gXbm1zmexj+HIifD0XHC1k6hgYLbc+LUfCs8tBwOOv+n9kJHm5Vq2n3yYOFId5gu4/jd8t+X88P86povC7gOoCM3Yqw/l/QX67sHX4oAtPs/KO387jYca/UrmXrmtVuHyGL/paXb4dN3PPxoZ2jnN3CqmcbH6nECS60gMBlggFyc92gyZw/HBM8SIR8ONeyIRrsLVWqB9RkjxCHymmreuQ3QmWb76eCqsfst9LmLau61sHElNrjZo6ZYg2Pz0LSP6jWPCKEV1kygmcFAbP2vZu2Pvlo6QyimPXrW8BrY5ZENjfl0DrEhNGCBJdoHynsCspKBvw6xsVdLcwWzFlA3WXQzJrXQg0iPnjdMv98r7CYBhRoVbqqB0Mz8NCPxNcQB6XtqXdfwcqMWlXZcYdr1S7WzpnbY8TL7+C1huk1rNViCWdCKvSsVfk8dv1sQsBjSDmSnA3/KYTrGqpFegpO+r5oYPINDC/DxMTiZhr8I3wk7+lA/MsqzkclS2EUSbq76nocvcoY1iM99p0OdfXTBj4gFFoezm7E9lDzvqHL2qI1+DskoiyQvGdSEM/5CeCzNcSj8FsCgFGGqFQdhuudGXPdouTeUt4h8vDxakZezrqt5Fr7QnM9HUf5hP fzzbNqLm 8+ZeEFQATLxjRymL3DivCuXI9CXg9uZ+d+uiuW5v46MJdPmsC50pvTUG3gI+7O3TYrdLT8v5a6XJapS6kOqA6rjcjsWgV6rVi03rZXeBWQZPNTWU5b7GN6ud/dvbYbOxyqmoWyI7TlBYkVznDYKzQe01ISpVvr2i9v5MLSeGEDNTD44VMkfEbyaj9jampUkKs43kwivfxguDWeQo9NJy35akO0LQuM9G0OyBaqQRpuMzruAC9H9P9eO4S2HV8w1U/xxvx3Ulj7K40R3FH2jKTLO7WSz70cyZeg+/k36sO/cAKLAFoQD6Hcp1w885i1g1Zz1kyVtM7Fc7WuCXfkaFmblwLTIouCQLHchPUUcxvwiAp/I6qUcLFZaY4QlC3mBQ4L0+QXZOWEcDVgaE= 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: KCSAN found an issue in obj_stock_flush_required(): stock->cached_objcg can be reset between the check and dereference: ================================================================== BUG: KCSAN: data-race in drain_all_stock / drain_obj_stock write to 0xffff888237c2a2f8 of 8 bytes by task 19625 on cpu 0: drain_obj_stock+0x408/0x4e0 mm/memcontrol.c:3306 refill_obj_stock+0x9c/0x1e0 mm/memcontrol.c:3340 obj_cgroup_uncharge+0xe/0x10 mm/memcontrol.c:3408 memcg_slab_free_hook mm/slab.h:587 [inline] __cache_free mm/slab.c:3373 [inline] __do_kmem_cache_free mm/slab.c:3577 [inline] kmem_cache_free+0x105/0x280 mm/slab.c:3602 __d_free fs/dcache.c:298 [inline] dentry_free fs/dcache.c:375 [inline] __dentry_kill+0x422/0x4a0 fs/dcache.c:621 dentry_kill+0x8d/0x1e0 dput+0x118/0x1f0 fs/dcache.c:913 __fput+0x3bf/0x570 fs/file_table.c:329 ____fput+0x15/0x20 fs/file_table.c:349 task_work_run+0x123/0x160 kernel/task_work.c:179 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] exit_to_user_mode_loop+0xcf/0xe0 kernel/entry/common.c:171 exit_to_user_mode_prepare+0x6a/0xa0 kernel/entry/common.c:203 __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline] syscall_exit_to_user_mode+0x26/0x140 kernel/entry/common.c:296 do_syscall_64+0x4d/0xc0 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x63/0xcd read to 0xffff888237c2a2f8 of 8 bytes by task 19632 on cpu 1: obj_stock_flush_required mm/memcontrol.c:3319 [inline] drain_all_stock+0x174/0x2a0 mm/memcontrol.c:2361 try_charge_memcg+0x6d0/0xd10 mm/memcontrol.c:2703 try_charge mm/memcontrol.c:2837 [inline] mem_cgroup_charge_skmem+0x51/0x140 mm/memcontrol.c:7290 sock_reserve_memory+0xb1/0x390 net/core/sock.c:1025 sk_setsockopt+0x800/0x1e70 net/core/sock.c:1525 udp_lib_setsockopt+0x99/0x6c0 net/ipv4/udp.c:2692 udp_setsockopt+0x73/0xa0 net/ipv4/udp.c:2817 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3668 __sys_setsockopt+0x1c3/0x230 net/socket.c:2271 __do_sys_setsockopt net/socket.c:2282 [inline] __se_sys_setsockopt net/socket.c:2279 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2279 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd value changed: 0xffff8881382d52c0 -> 0xffff888138893740 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 19632 Comm: syz-executor.0 Not tainted 6.3.0-rc2-syzkaller-00387-g534293368afa #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/02/2023 Fix it by reading the cached_objcg with READ_ONCE(). Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Reported-by: syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com Reported-by: Dmitry Vyukov Link: https://lore.kernel.org/linux-mm/CACT4Y+ZfucZhM60YPphWiCLJr6+SGFhT+jjm8k1P-a_8Kkxsjg@mail.gmail.com/T/#t Signed-off-by: Roman Gushchin Reviewed-by: Yosry Ahmed Acked-by: Muchun Song --- mm/memcontrol.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5abffe6f8389..9426a1ddc190 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3314,10 +3314,11 @@ static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock) static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, struct mem_cgroup *root_memcg) { + struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg); struct mem_cgroup *memcg; - if (stock->cached_objcg) { - memcg = obj_cgroup_memcg(stock->cached_objcg); + if (objcg) { + memcg = obj_cgroup_memcg(objcg); if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) return true; }