From patchwork Wed Oct 25 08:54:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kazunori Kobayashi X-Patchwork-Id: 13435769 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 DE95CC25B47 for ; Wed, 25 Oct 2023 09:13:15 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1qvZww-0003Av-63; Wed, 25 Oct 2023 09:13:14 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1qvZwv-0003Ap-J4 for linux-f2fs-devel@lists.sourceforge.net; Wed, 25 Oct 2023 09:13:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QQ/17KLqALE4B8widhP681PaNMIrFY8nfETZa+CkAqw=; b=QPgDZJSeYMry86oq8y1vH/Cvtf s3v6AZUa4ZhTdCj6TDP5K4NtLv+i9LvQhA1ThUg5bVrX2D5tQbkAc/WK+o5hyH5Jy3m7gezNUjEqd wfqFDZhC4ZfI2fWeq2B2sOi6s0sL1OCNk1WGeE6k+xF2cT5dvJUi1T0ME6P+to9UBH0E=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version: Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=QQ/17KLqALE4B8widhP681PaNMIrFY8nfETZa+CkAqw=; b=aZlG6MVkl5b3/h/kn6Fsu1aGXc Q28cBYu1najl9GSf+u5DlKaPikIs0n27HpBd47+1ntsxDxKHRwppRe8tyJbXQDVpqU++gvBdKpBv9 G7S/Tzic5ltv42a2h/OCsWmDTEYYGhxuH0eECTFhYTw58CkJUSupLsy4sCu8hjziYJu8=; Received: from 202x210x215x66.ap202.ftth.ucom.ne.jp ([202.210.215.66] helo=smtp.priv.miraclelinux.com) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1qvZwu-008geg-Pn for linux-f2fs-devel@lists.sourceforge.net; Wed, 25 Oct 2023 09:13:13 +0000 Received: from cip-lava-a.miraclelinux.com (cip-lava-a.miraclelinux.com [10.2.1.116]) by smtp.priv.miraclelinux.com (Postfix) with ESMTP id 77053140036; Wed, 25 Oct 2023 17:54:38 +0900 (JST) From: Kazunori Kobayashi To: linux-f2fs-devel@lists.sourceforge.net Date: Wed, 25 Oct 2023 08:54:32 +0000 Message-Id: <20231025085432.11639-1-kazunori.kobayashi@miraclelinux.com> X-Mailer: git-send-email 2.17.1 X-Headers-End: 1qvZwu-008geg-Pn Subject: [f2fs-dev] [PATCH 4.19] f2fs: fix to do sanity check on inode type during garbage collection 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: , Cc: hiraku.toyooka@miraclelinux.com, stable@vger.kernel.org, Jaegeuk Kim MIME-Version: 1.0 Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net From: Chao Yu commit 9056d6489f5a41cfbb67f719d2c0ce61ead72d9f upstream. As report by Wenqing Liu in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215231 - Overview kernel NULL pointer dereference triggered in folio_mark_dirty() when mount and operate on a crafted f2fs image - Reproduce tested on kernel 5.16-rc3, 5.15.X under root 1. mkdir mnt 2. mount -t f2fs tmp1.img mnt 3. touch tmp 4. cp tmp mnt F2FS-fs (loop0): sanity_check_inode: inode (ino=49) extent info [5942, 4294180864, 4] is incorrect, run fsck to fix F2FS-fs (loop0): f2fs_check_nid_range: out-of-range nid=31340049, run fsck to fix. BUG: kernel NULL pointer dereference, address: 0000000000000000 folio_mark_dirty+0x33/0x50 move_data_page+0x2dd/0x460 [f2fs] do_garbage_collect+0xc18/0x16a0 [f2fs] f2fs_gc+0x1d3/0xd90 [f2fs] f2fs_balance_fs+0x13a/0x570 [f2fs] f2fs_create+0x285/0x840 [f2fs] path_openat+0xe6d/0x1040 do_filp_open+0xc5/0x140 do_sys_openat2+0x23a/0x310 do_sys_open+0x57/0x80 The root cause is for special file: e.g. character, block, fifo or socket file, f2fs doesn't assign address space operations pointer array for mapping->a_ops field, so, in a fuzzed image, SSA table indicates a data block belong to special file, when f2fs tries to migrate that block, it causes NULL pointer access once move_data_page() calls a_ops->set_dirty_page(). Cc: stable@vger.kernel.org Reported-by: Wenqing Liu Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Kazunori Kobayashi --- fs/f2fs/gc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index ff447bbb5248..fb4494c54484 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -958,7 +958,8 @@ static void gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, if (phase == 3) { inode = f2fs_iget(sb, dni.ino); - if (IS_ERR(inode) || is_bad_inode(inode)) + if (IS_ERR(inode) || is_bad_inode(inode) || + special_file(inode->i_mode)) continue; if (!down_write_trylock(