From patchwork Sat Jul 27 05:56:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Edward Adam Davis X-Patchwork-Id: 13743618 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 lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 329D7C3DA4A for ; Sat, 27 Jul 2024 05:57:31 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1sXaQr-0000JI-0Q; Sat, 27 Jul 2024 05:57:29 +0000 Received: from [172.30.29.67] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sXaQo-0000J0-MZ for linux-f2fs-devel@lists.sourceforge.net; Sat, 27 Jul 2024 05:57:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Date:Subject:Cc:To:From:Message-ID:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=zbSS+ajYypXg92Z/q7nSeFt+R/QQz5CJOPsbYcOtdj0=; b=JstkTxG+p/1nwVJyNmjxUzBMbb pKtNeZmvkFsqEKnJviaMPK8L9h3P7LKkxuE6aqj12I0dxgy0jy5RsZm/CAqxwBnwE3JSOxmLOAdCy b+XPf1MJXD6EmBqAoJdHMWdIvi7KGjFS/3DImx/Ev8iUWrXRhyqenqsWr4ZV5zyO6bAI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Date: Subject:Cc:To:From:Message-ID:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=zbSS+ajYypXg92Z/q7nSeFt+R/QQz5CJOPsbYcOtdj0=; b=and59Lm8VOizOsoAi9BRUbTlsW XBi5aVN66hcBuz6hJ+C0btIwOi8p/2vxPihwkv9F28zPv7BvNpqg8gfvhYazRN+udEr0yaKnGlCkv /Bb1gbFuPAF0ti1Ztjpia+UswOOq5nRbaWNzXvrvwLaO5vLkIcpoccQfrO3/oZI8AVzA=; Received: from out162-62-57-210.mail.qq.com ([162.62.57.210]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1sXaQn-00064I-9c for linux-f2fs-devel@lists.sourceforge.net; Sat, 27 Jul 2024 05:57:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1722059814; bh=zbSS+ajYypXg92Z/q7nSeFt+R/QQz5CJOPsbYcOtdj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YKiiuvPIrulqAnZpFnppaTaafXLQ5eVYno+R2a1aalQCyOcUsUFyKZqI992+qqbPD 1ZqSzA3XswROgSicqQqXr3nZ4yx9di2UD8qbAhExjJvvAhgamphgMRt1ZARzzbWqio u08TjVJDnyaM1KFqC8zL54my+TCWZUteOFZ8LnL0= Received: from pek-lxu-l1.wrs.com ([111.198.228.103]) by newxmesmtplogicsvrszb16-1.qq.com (NewEsmtp) with SMTP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oeAVDbp3OJHYyFg= To: syzbot+1a8e2b31f2ac9bd3d148@syzkaller.appspotmail.com Date: Sat, 27 Jul 2024 13:56:53 +0800 X-OQ-MSGID: <20240727055652.2700649-2-eadavis@qq.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <0000000000005c7ccb061e032b9b@google.com> References: <0000000000005c7ccb061e032b9b@google.com> MIME-Version: 1.0 X-Headers-End: 1sXaQn-00064I-9c Subject: [f2fs-dev] [PATCH] f2fs: Add mutex to prevent gc task from being accessed before initialization X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Edward Adam Davis via Linux-f2fs-devel From: Edward Adam Davis Reply-To: Edward Adam Davis Cc: daehojeong@google.com, syzkaller-bugs@googlegroups.com, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, jaegeuk@kernel.org Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net syzbot report null-ptr-deref Write in f2fs_stop_gc_thread, because shutdown and remount can cause multi-path entry into f2fs_stop_gc_thread, resulting in incorrect access to f2fs_gc_task before it is properly initialized. Reported-and-tested-by: syzbot+1a8e2b31f2ac9bd3d148@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=1a8e2b31f2ac9bd3d148 Signed-off-by: Edward Adam Davis --- fs/f2fs/gc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index 6066c6eecf41..a624e8271fbc 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -27,6 +27,7 @@ static struct kmem_cache *victim_entry_slab; static unsigned int count_bits(const unsigned long *addr, unsigned int offset, unsigned int len); +static DEFINE_MUTEX(gc_lock); static int gc_thread_func(void *data) { @@ -203,10 +204,13 @@ int f2fs_start_gc_thread(struct f2fs_sb_info *sbi) void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi) { - struct f2fs_gc_kthread *gc_th = sbi->gc_thread; + struct f2fs_gc_kthread *gc_th; - if (!gc_th) + guard(mutex)(&gc_lock); + gc_th = sbi->gc_thread; + if (!gc_th || gc_th->f2fs_gc_task < PAGE_OFFSET) return; + kthread_stop(gc_th->f2fs_gc_task); wake_up_all(&gc_th->fggc_wq); kfree(gc_th);