From patchwork Sun Sep 22 08:08:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeongjun Park X-Patchwork-Id: 13809018 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 01090CF9C68 for ; Sun, 22 Sep 2024 08:08:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62B5F6B007B; Sun, 22 Sep 2024 04:08:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DBBA6B0082; Sun, 22 Sep 2024 04:08:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A2EB6B0085; Sun, 22 Sep 2024 04:08:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2C5796B007B for ; Sun, 22 Sep 2024 04:08:49 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AE38D80C6F for ; Sun, 22 Sep 2024 08:08:48 +0000 (UTC) X-FDA: 82591647936.22.928F3C5 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf04.hostedemail.com (Postfix) with ESMTP id E24704000B for ; Sun, 22 Sep 2024 08:08:46 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mh6TeLjJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of aha310510@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=aha310510@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726992398; 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:references:dkim-signature; bh=J3FsihmhCrs+dPbCmmFqmu8S8SyW6nS0IMPoboRbafg=; b=DYrAhEHfUXRwzs6h0bdcq1/sXi9pZUQ5OsdYEm73DaKTlfCUcTMnS+5GxDQFyS4X5T3KVN wCRBNM4RqeD8SwD7zNdYqcu61K9my86fOkeY7jLsD7cx5SmQDExUEzAuX2f3/VBlIUXh24 z/tqVPgylp2Wx5WiE4PKnXz8DeKw4+c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726992398; a=rsa-sha256; cv=none; b=Jo29lwrvzzYy/rgJNM5LGne5LNfJ1su0aRHb3eF5Xwo5jjqt4zEBuxYXfBUkQu4+veQt/Z 6TfozYXbRTs/5Mq5LOsoeJov0oXJ7ef8FlwKo2Lnia24K+Ig3vDyVvqD6+X9bv61gv4RDt eN8xaKvPgwaTqmwQ1lR+iev3AuphfjA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mh6TeLjJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of aha310510@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=aha310510@gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-718d91eef2eso2105602b3a.1 for ; Sun, 22 Sep 2024 01:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726992525; x=1727597325; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=J3FsihmhCrs+dPbCmmFqmu8S8SyW6nS0IMPoboRbafg=; b=mh6TeLjJyN9TmEQG8sUhVtq7lPw89ck0lRBDerpFK5lcbUJIhvzmuqvFyjJSH/tc9N zksNaeQ7WnuhW86uVhZvlgIjex8aFad8jKZ7Ws+xMlZz3qzQECKWGKU4+jrSes/JEJld ZpgXQoh184d41T/kv14V5duCGm+FoPA4Y9NUATzY8Cu3NLCEeG3tvaSMUCl3bPGPl4mO +vwNV7SjXdlXBqh1zV0DYMqThKeX/Y9vDUFwVGrHcG/TDS4Kz4sC2MBXX89mU8DBDkXz Sm2O9xCoyJBHD0B2BpqsGtRQa6ae42KylnittfuuIaWlvOJ8XRSki6e3wogZ1s/Cqzrw mXOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726992525; x=1727597325; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=J3FsihmhCrs+dPbCmmFqmu8S8SyW6nS0IMPoboRbafg=; b=Hm4aROLdWvRuVq6p1OPrALM5jU7n18DaHXfI0JzsfP7B3c9kTHwaN0SYkQ+WHIPQxC g6amwmYCywfXnE0SIKCMjNwepHB3YKNGe8/ZkuX1gzNdLL43nRAuxGJu5BLY6EkG0kzo TB2lNGglDxEdKZnYckYM2NYvi8wfs1phFP51J/KK9eFtht/mx48FA+doNo+++B5j2A1b 0NjjQJBuPCkWxKNZ/51mFE9ZmbUfQoCgHgepQ713oHM6Dmm5VAje/AOpAXxLk2JpWZi5 sxgZgYqcGN/k+fOuhMCv6MUv8O+KJ/iocYFfHy3Cj1RyB7/dMTrCm1UK4VhLmSqxtSlT k1mQ== X-Forwarded-Encrypted: i=1; AJvYcCXUkcEBJCb4DSeAZXlYOcl+eKJu6NcDQMiyus/kGFebt9IHOQI3DzxcYmlbQw5wzi9vvF94o/E05g==@kvack.org X-Gm-Message-State: AOJu0YxV+2QFyRHG0mSjoUjg/mqo1s6Ccg6lXAAJrg8czFP4oz0PYSCd M06aOI73J+IV2zD6Qy3fkiUI8O4jrp1mRG1mnLwvVauoVQIXl5B8 X-Google-Smtp-Source: AGHT+IHpk0Bc6lWPro4iLJbr05T92LGyxhPyen4ubXkfG4rEh2xJJ1L+/TSCfeFZkozx8z949PNa7A== X-Received: by 2002:a05:6a00:3e08:b0:718:eeab:97ca with SMTP id d2e1a72fcca58-7198e243aebmr20456882b3a.2.1726992525347; Sun, 22 Sep 2024 01:08:45 -0700 (PDT) Received: from kernelexploit-virtual-machine.localdomain ([121.185.186.233]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944b7afc5sm12214321b3a.122.2024.09.22.01.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Sep 2024 01:08:44 -0700 (PDT) From: Jeongjun Park To: akpm@linux-foundation.org, hughd@google.com Cc: yuzhao@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, syzbot , Jeongjun Park Subject: [PATCH RESEND] mm: shmem: fix data-race in shmem_getattr() Date: Sun, 22 Sep 2024 17:08:38 +0900 Message-Id: <20240922080838.15184-1-aha310510@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E24704000B X-Stat-Signature: amgzipxfssf6obby5otdjbbxmwpo9bky X-Rspam-User: X-HE-Tag: 1726992526-896745 X-HE-Meta: U2FsdGVkX18jKJ2oQQyGuMF/T51gbtGbxnbxpAivYIMuyk5THJHV1BFssMHQQy+56MZhO67hvYZwTfOZKAXNYYDr5pXAhAoiKVCfSe+qRcl37tmJc5aNGTThUTnXqYaLoM7MFaD4xoV+ClR5iSasEhlCu0j/zwUB2fUIzlibkQ0Lxc/B7mj5jp27QFkdfDgHGxlVVSVoNRW0W4OdxGNvo8nb1HNBGGYXjEMWEZ/9Lux5U8oELnTPTpPz531LWZZcogNN/dxJSIhFA8DxfwqBxxXnRx+oropAYXr89PpvkoG1YJeAZ5KvUQ4JEGylgiW+/1o7lREti5EbSlPRwWDCEJ8iBXVs3rh/+OPJ7HAc7487VRWnzlB7paStZQS4J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G1ov6v7xW6osx0YvMlUdE/Qz/h+Y2lz X-Bogosity: Ham, tests=bogofilter, spamicity=0.000059, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: I got the following KCSAN report during syzbot testing: ================================================================== BUG: KCSAN: data-race in generic_fillattr / inode_set_ctime_current write to 0xffff888102eb3260 of 4 bytes by task 6565 on cpu 1: inode_set_ctime_to_ts include/linux/fs.h:1638 [inline] inode_set_ctime_current+0x169/0x1d0 fs/inode.c:2626 shmem_mknod+0x117/0x180 mm/shmem.c:3443 shmem_create+0x34/0x40 mm/shmem.c:3497 lookup_open fs/namei.c:3578 [inline] open_last_lookups fs/namei.c:3647 [inline] path_openat+0xdbc/0x1f00 fs/namei.c:3883 do_filp_open+0xf7/0x200 fs/namei.c:3913 do_sys_openat2+0xab/0x120 fs/open.c:1416 do_sys_open fs/open.c:1431 [inline] __do_sys_openat fs/open.c:1447 [inline] __se_sys_openat fs/open.c:1442 [inline] __x64_sys_openat+0xf3/0x120 fs/open.c:1442 x64_sys_call+0x1025/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:258 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x76/0x7e read to 0xffff888102eb3260 of 4 bytes by task 3498 on cpu 0: inode_get_ctime_nsec include/linux/fs.h:1623 [inline] inode_get_ctime include/linux/fs.h:1629 [inline] generic_fillattr+0x1dd/0x2f0 fs/stat.c:62 shmem_getattr+0x17b/0x200 mm/shmem.c:1157 vfs_getattr_nosec fs/stat.c:166 [inline] vfs_getattr+0x19b/0x1e0 fs/stat.c:207 vfs_statx_path fs/stat.c:251 [inline] vfs_statx+0x134/0x2f0 fs/stat.c:315 vfs_fstatat+0xec/0x110 fs/stat.c:341 __do_sys_newfstatat fs/stat.c:505 [inline] __se_sys_newfstatat+0x58/0x260 fs/stat.c:499 __x64_sys_newfstatat+0x55/0x70 fs/stat.c:499 x64_sys_call+0x141f/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:263 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x76/0x7e value changed: 0x2755ae53 -> 0x27ee44d3 Since there is no special protection when shmem_getattr() calls generic_fillattr(), data-race occurs by functions such as shmem_unlink() or shmem_mknod(). This can cause unexpected results, so commenting it out is not enough. Therefore, when calling generic_fillattr() from shmem_getattr(), it is appropriate to protect the inode using inode_lock_shared() and inode_unlock_shared() to prevent data-race. Reported-by: syzbot Fixes: 44a30220bc0a ("shmem: recalculate file inode when fstat") Signed-off-by: Jeongjun Park --- mm/shmem.c | 2 ++ 1 file changed, 2 insertions(+) -- diff --git a/mm/shmem.c b/mm/shmem.c index 5a77acf6ac6a..9beeb47c3743 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1154,7 +1154,9 @@ static int shmem_getattr(struct mnt_idmap *idmap, stat->attributes_mask |= (STATX_ATTR_APPEND | STATX_ATTR_IMMUTABLE | STATX_ATTR_NODUMP); + inode_lock_shared(inode); generic_fillattr(idmap, request_mask, inode, stat); + inode_unlock_shared(inode); if (shmem_is_huge(inode, 0, false, NULL, 0)) stat->blksize = HPAGE_PMD_SIZE;