syzbot report a null-ptr-deref in gpiolib_seq_stop. [1] syzbot uses "echo 2 > /proc/thread-self/fail-nth", and in the current thread, the second memory allocation will trigger a failure. That is to say, the memory allocation for priv in gpiolib_seq_start() fails, so m->private is not initialized, which eventually leads to a null pointer dereference to m->private in gpiolib_seq_stop(). Because this type of problem is recurring, it is best to handle it in the upper layer of the gpio driver, that is, in fs/seq_file.c, and judge the pointer returned after m->op->start() returns. If its value is null, exit the subsequent process.This failure does not affect the next execution of traverse(), so 0 is returned here. [1] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 0 UID: 0 PID: 5829 Comm: syz-executor520 Not tainted 6.13.0-rc2-syzkaller-00362-g2d8308bf5b67 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/25/2024 RIP: 0010:gpiolib_seq_stop+0x4c/0xe0 drivers/gpio/gpiolib.c:5067 Code: 48 c1 ea 03 80 3c 02 00 0f 85 98 00 00 00 48 8b 9b e0 00 00 00 48 b8 00 00 00 00 00 fc ff df 48 8d 7b 04 48 89 fa 48 c1 ea 03 <0f> b6 14 02 48 89 f8 83 e0 07 83 c0 03 38 d0 7c 04 84 d2 75 60 8b RSP: 0018:ffffc90003e1fa58 EFLAGS: 00010247 RAX: dffffc0000000000 RBX: 0000000000000000 RCX: ffff88802463a018 RDX: 0000000000000000 RSI: ffffffff84cc96be RDI: 0000000000000004 RBP: 0000000000000000 R08: 0000000000000dc0 R09: 00000000ffffffff R10: ffffffff8df7c5d3 R11: 0000000000000001 R12: ffffffff8bb596a0 R13: 0000000000000000 R14: 0000000000000000 R15: ffffc90003e1fc40 FS: 0000555557ff9380(0000) GS:ffff8880b8600000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000055e43c381608 CR3: 0000000076408000 CR4: 00000000003526f0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: <TASK> traverse.part.0.constprop.0+0x2bd/0x640 fs/seq_file.c:131 traverse fs/seq_file.c:98 [inline] seq_read_iter+0x934/0x12b0 fs/seq_file.c:195 seq_read+0x39f/0x4e0 fs/seq_file.c:162 full_proxy_read+0xfb/0x1b0 fs/debugfs/file.c:351 vfs_read+0x1df/0xbe0 fs/read_write.c:563 ksys_pread64 fs/read_write.c:756 [inline] __do_sys_pread64 fs/read_write.c:764 [inline] __se_sys_pread64 fs/read_write.c:761 [inline] __x64_sys_pread64+0x1f6/0x250 fs/read_write.c:761 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f Reported-by: syzbot+b95d0c98f01e7a95da72@xxxxxxxxxxxxxxxxxxxxxxxxx Closes: https://syzkaller.appspot.com/bug?extid=b95d0c98f01e7a95da72 Tested-by: syzbot+b95d0c98f01e7a95da72@xxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Edward Adam Davis <eadavis@xxxxxx> --- fs/seq_file.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/seq_file.c b/fs/seq_file.c index 8bbb1ad46335..130424bbcf7d 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -104,6 +104,9 @@ static int traverse(struct seq_file *m, loff_t offset) return -ENOMEM; } p = m->op->start(m, &m->index); + if (!p && !m->private) + return 0; + while (p) { error = PTR_ERR(p); if (IS_ERR(p)) -- 2.47.0