Takashi Iwai 547aad
From f0a0ccda18d6fd826d7c7e7ad48a6ed61c20f8b4 Mon Sep 17 00:00:00 2001
Takashi Iwai 547aad
From: ZhangPeng <zhangpeng362@huawei.com>
Takashi Iwai 547aad
Date: Sat, 19 Nov 2022 21:05:42 +0900
Takashi Iwai 547aad
Subject: [PATCH] nilfs2: fix NULL pointer dereference in nilfs_palloc_commit_free_entry()
Takashi Iwai 547aad
Git-commit: f0a0ccda18d6fd826d7c7e7ad48a6ed61c20f8b4
Takashi Iwai 547aad
Patch-mainline: v6.1-rc8
Takashi Iwai 547aad
References: git-fixes
Takashi Iwai 547aad
Takashi Iwai 547aad
Syzbot reported a null-ptr-deref bug:
Takashi Iwai 547aad
Takashi Iwai 547aad
 NILFS (loop0): segctord starting. Construction interval = 5 seconds, CP
Takashi Iwai 547aad
 frequency < 30 seconds
Takashi Iwai 547aad
 general protection fault, probably for non-canonical address
Takashi Iwai 547aad
 0xdffffc0000000002: 0000 [#1] PREEMPT SMP KASAN
Takashi Iwai 547aad
 KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]
Takashi Iwai 547aad
 CPU: 1 PID: 3603 Comm: segctord Not tainted
Takashi Iwai 547aad
 6.1.0-rc2-syzkaller-00105-gb229b6ca5abb #0
Takashi Iwai 547aad
 Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google
Takashi Iwai 547aad
 10/11/2022
Takashi Iwai 547aad
 RIP: 0010:nilfs_palloc_commit_free_entry+0xe5/0x6b0
Takashi Iwai 547aad
 fs/nilfs2/alloc.c:608
Takashi Iwai 547aad
 Code: 00 00 00 00 fc ff df 80 3c 02 00 0f 85 cd 05 00 00 48 b8 00 00 00
Takashi Iwai 547aad
 00 00 fc ff df 4c 8b 73 08 49 8d 7e 10 48 89 fa 48 c1 ea 03 <80> 3c 02
Takashi Iwai 547aad
 00 0f 85 26 05 00 00 49 8b 46 10 be a6 00 00 00 48 c7 c7
Takashi Iwai 547aad
 RSP: 0018:ffffc90003dff830 EFLAGS: 00010212
Takashi Iwai 547aad
 RAX: dffffc0000000000 RBX: ffff88802594e218 RCX: 000000000000000d
Takashi Iwai 547aad
 RDX: 0000000000000002 RSI: 0000000000002000 RDI: 0000000000000010
Takashi Iwai 547aad
 RBP: ffff888071880222 R08: 0000000000000005 R09: 000000000000003f
Takashi Iwai 547aad
 R10: 000000000000000d R11: 0000000000000000 R12: ffff888071880158
Takashi Iwai 547aad
 R13: ffff88802594e220 R14: 0000000000000000 R15: 0000000000000004
Takashi Iwai 547aad
 FS:  0000000000000000(0000) GS:ffff8880b9b00000(0000)
Takashi Iwai 547aad
 knlGS:0000000000000000
Takashi Iwai 547aad
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
Takashi Iwai 547aad
 CR2: 00007fb1c08316a8 CR3: 0000000018560000 CR4: 0000000000350ee0
Takashi Iwai 547aad
 Call Trace:
Takashi Iwai 547aad
  <TASK>
Takashi Iwai 547aad
  nilfs_dat_commit_free fs/nilfs2/dat.c:114 [inline]
Takashi Iwai 547aad
  nilfs_dat_commit_end+0x464/0x5f0 fs/nilfs2/dat.c:193
Takashi Iwai 547aad
  nilfs_dat_commit_update+0x26/0x40 fs/nilfs2/dat.c:236
Takashi Iwai 547aad
  nilfs_btree_commit_update_v+0x87/0x4a0 fs/nilfs2/btree.c:1940
Takashi Iwai 547aad
  nilfs_btree_commit_propagate_v fs/nilfs2/btree.c:2016 [inline]
Takashi Iwai 547aad
  nilfs_btree_propagate_v fs/nilfs2/btree.c:2046 [inline]
Takashi Iwai 547aad
  nilfs_btree_propagate+0xa00/0xd60 fs/nilfs2/btree.c:2088
Takashi Iwai 547aad
  nilfs_bmap_propagate+0x73/0x170 fs/nilfs2/bmap.c:337
Takashi Iwai 547aad
  nilfs_collect_file_data+0x45/0xd0 fs/nilfs2/segment.c:568
Takashi Iwai 547aad
  nilfs_segctor_apply_buffers+0x14a/0x470 fs/nilfs2/segment.c:1018
Takashi Iwai 547aad
  nilfs_segctor_scan_file+0x3f4/0x6f0 fs/nilfs2/segment.c:1067
Takashi Iwai 547aad
  nilfs_segctor_collect_blocks fs/nilfs2/segment.c:1197 [inline]
Takashi Iwai 547aad
  nilfs_segctor_collect fs/nilfs2/segment.c:1503 [inline]
Takashi Iwai 547aad
  nilfs_segctor_do_construct+0x12fc/0x6af0 fs/nilfs2/segment.c:2045
Takashi Iwai 547aad
  nilfs_segctor_construct+0x8e3/0xb30 fs/nilfs2/segment.c:2379
Takashi Iwai 547aad
  nilfs_segctor_thread_construct fs/nilfs2/segment.c:2487 [inline]
Takashi Iwai 547aad
  nilfs_segctor_thread+0x3c3/0xf30 fs/nilfs2/segment.c:2570
Takashi Iwai 547aad
  kthread+0x2e4/0x3a0 kernel/kthread.c:376
Takashi Iwai 547aad
  ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:306
Takashi Iwai 547aad
  </TASK>
Takashi Iwai 547aad
 ...
Takashi Iwai 547aad
Takashi Iwai 547aad
If DAT metadata file is corrupted on disk, there is a case where
Takashi Iwai 547aad
req->pr_desc_bh is NULL and blocknr is 0 at nilfs_dat_commit_end() during
Takashi Iwai 547aad
a b-tree operation that cascadingly updates ancestor nodes of the b-tree,
Takashi Iwai 547aad
because nilfs_dat_commit_alloc() for a lower level block can initialize
Takashi Iwai 547aad
the blocknr on the same DAT entry between nilfs_dat_prepare_end() and
Takashi Iwai 547aad
nilfs_dat_commit_end().
Takashi Iwai 547aad
Takashi Iwai 547aad
If this happens, nilfs_dat_commit_end() calls nilfs_dat_commit_free()
Takashi Iwai 547aad
without valid buffer heads in req->pr_desc_bh and req->pr_bitmap_bh, and
Takashi Iwai 547aad
causes the NULL pointer dereference above in
Takashi Iwai 547aad
nilfs_palloc_commit_free_entry() function, which leads to a crash.
Takashi Iwai 547aad
Takashi Iwai 547aad
Fix this by adding a NULL check on req->pr_desc_bh and req->pr_bitmap_bh
Takashi Iwai 547aad
before nilfs_palloc_commit_free_entry() in nilfs_dat_commit_free().
Takashi Iwai 547aad
Takashi Iwai 547aad
This also calls nilfs_error() in that case to notify that there is a fatal
Takashi Iwai 547aad
flaw in the filesystem metadata and prevent further operations.
Takashi Iwai 547aad
Takashi Iwai 547aad
Link: https://lkml.kernel.org/r/00000000000097c20205ebaea3d6@google.com
Takashi Iwai 547aad
Link: https://lkml.kernel.org/r/20221114040441.1649940-1-zhangpeng362@huawei.com
Takashi Iwai 547aad
Link: https://lkml.kernel.org/r/20221119120542.17204-1-konishi.ryusuke@gmail.com
Takashi Iwai 547aad
Signed-off-by: ZhangPeng <zhangpeng362@huawei.com>
Takashi Iwai 547aad
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Takashi Iwai 547aad
Reported-by: syzbot+ebe05ee8e98f755f61d0@syzkaller.appspotmail.com
Takashi Iwai 547aad
Tested-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Takashi Iwai 547aad
Cc: <stable@vger.kernel.org>
Takashi Iwai 547aad
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Takashi Iwai 547aad
Acked-by: Takashi Iwai <tiwai@suse.de>
Takashi Iwai 547aad
Takashi Iwai 547aad
---
Takashi Iwai 547aad
 fs/nilfs2/dat.c | 7 +++++++
Takashi Iwai 547aad
 1 file changed, 7 insertions(+)
Takashi Iwai 547aad
Takashi Iwai 547aad
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
Takashi Iwai 547aad
index 3b55e239705f..9930fa901039 100644
Takashi Iwai 547aad
--- a/fs/nilfs2/dat.c
Takashi Iwai 547aad
+++ b/fs/nilfs2/dat.c
Takashi Iwai 547aad
@@ -111,6 +111,13 @@ static void nilfs_dat_commit_free(struct inode *dat,
Takashi Iwai 547aad
 	kunmap_atomic(kaddr);
Takashi Iwai 547aad
 
Takashi Iwai 547aad
 	nilfs_dat_commit_entry(dat, req);
Takashi Iwai 547aad
+
Takashi Iwai 547aad
+	if (unlikely(req->pr_desc_bh == NULL || req->pr_bitmap_bh == NULL)) {
Takashi Iwai 547aad
+		nilfs_error(dat->i_sb,
Takashi Iwai 547aad
+			    "state inconsistency probably due to duplicate use of vblocknr = %llu",
Takashi Iwai 547aad
+			    (unsigned long long)req->pr_entry_nr);
Takashi Iwai 547aad
+		return;
Takashi Iwai 547aad
+	}
Takashi Iwai 547aad
 	nilfs_palloc_commit_free_entry(dat, req);
Takashi Iwai 547aad
 }
Takashi Iwai 547aad
 
Takashi Iwai 547aad
-- 
Takashi Iwai 547aad
2.35.3
Takashi Iwai 547aad