jfs: fix lbmDONE races in lbmIODone() (was "loop bug saga")
Subsystems (5)
Christian Brauner
maintainercover jfs: fix lbmDONE races in lbmIODone() (was "loop bug saga")
This is the root cause of syzbot's "general protection fault in lo_rw_aio" (cf. [1]) which got a lot of loop patches over the last months (cf. [2]). It's not a loop bug. jfs releases the block device while its own log superblock write is still queued and the loop device gets torn down underneath it. Bart already pointed at b15e4310633f ("jfs: Set the lbmDone flag at the end of lbmIODone") in [3].
Since b15e4310633f lbmIODone() sets lbmDONE as the last thing it does. lbmRead() doesn't take LCACHE_LOCK and returns as soon as lbmREAD is gone. So in lmLogShutdown() the late lbmDONE of the read completion can land on the superblock write that lbmDirectWrite() just submitted. lbmIOWait() returns right away, jfs frees the buffers and the unmount finishes. The first patch makes lbmRead() wait for lbmDONE under LCACHE_LOCK.
On PREEMPT_RT the woken umount task preempts the completion that holds LCACHE_LOCK on the same CPU, so this happens all the time. With the reproducer from [4] every RT VM I booted hit the lbmIODone() UAF within a minute and the lo_rw_aio() NULL deref a few seconds later. With some instrumentation that's an early lbmIOWait() return in about one out of 70 unmounts. Without PREEMPT_RT I've seen it once in 26400 unmounts. With the first patch applied it's zero in 19200 unmounts and the reproducer doesn't trigger anything anymore.
The lockless wait in lbmRead() isn't new. Before b15e4310633f the early return let lbmDirectWrite()'s flags get lost in the read-modify-write that clears lbmREAD instead. That's the much rarer pre-7.1 version syzbot hit in [4]. With b15e4310633f reverted I've seen it once in 244600 unmounts.
The second patch is the group commit side of b15e4310633f. lbmIODone() now sets lbmDONE after lmPostGC() ran with the lock dropped, which may have freed or requeued the buffer already. That's syzbot's "kernel BUG in lbmIOWait" (cf. [5]) and it's a lot harder to hit. On PREEMPT_RT the late lbmDONE lands on a new I/O of the buffer in about one out of ten group commit completions but I haven't seen it land on lmLogShutdown()'s final write on its own in about a million unmounts. With a delay that holds the completion after lmPostGC() until lmLogShutdown() has submitted that write every VM hits syzbot's BUG within the first few unmounts, down to the 0x2e in R12. With the second patch the same delay is harmless. The details are in the patch.
None of the loop changes that were proposed are needed for this.
/*
* I'm also appending some deets on the nilfs detour that was in the
* original patch series so we don't have to rehash that discussion:
*/
Tetsuo's loop debug check (cf. [6]) also fired for nilfs2 on syzbot ci (cf. [7]). His "block: wait for inflight requests in bdev_release()" (cf. [8]) cites that report as evidence that reads leak past the release of the block device via readahead. I looked into it and it's a false positive of the check.
When nilfs2 searches for the super root at mount it reads ahead through buffer heads in the page cache of the block device. If the mount fails it doesn't wait for that readahead. That's fine because the last close waits for it:
bdev_release()
-> blkdev_put_whole()
-> blkdev_flush_mapping()
-> kill_bdev()
-> truncate_inode_pages()
-> discard_buffer()
-> lock_buffer()The reports are printed by the loop worker while it processes exactly the reads that the closing task waits for. lo_release() and __loop_clr_fd() haven't run yet and lo->lo_backing_file is still valid. Both logs go on for more than ten seconds after the reports without a splat.
The check fires because loop_clr_fd() already switches the device to Lo_rundown when the caller is the only opener and blkdev_put_whole() drops bd_openers to zero before it flushes the page cache and calls ->release(). So "lo_state != Lo_bound && disk_openers() == 0" is true for every command a final close drains after such a LOOP_CLR_FD. syzkaller closes the loop device before mount(2) and opens it again afterwards to issue LOOP_CLR_FD. That only reports if another opener, probably udevd's blkid probe, kept the device open across the mount and closes it in between. That happened about once in 600 to 700 mounts of those images.
There's no hole in the block layer or in mm that would let these reads escape. The bios belong to locked buffer heads in the page cache of the block device and nothing gets rid of those without waiting for them. All 48 crash reports of [1] I looked at are on the write side as well. A read would fault on file->f_op instead of in file_inode() and I couldn't find a syzbot report like that.
So a check that wants to catch I/O after the teardown should test whether __loop_clr_fd() has started, e.g., by looking at lo->lo_backing_file. lo_state and the number of openers can't tell a final close that is still draining from the teardown. The comment in the debug patch says the state "should remain unchanged unless __loop_clr_fd() by error starts" but loop_clr_fd() changes it.
jfs is different. It writes its log from pages it allocated itself, so kill_bdev() never sees that I/O and nothing in the block layer can wait for it.
[1]: https://syzkaller.appspot.com/bug?extid=cd8a9a308e879a4e2c28
[2]: https://lore.kernel.org/[email protected]
[3]: https://lore.kernel.org/fb1e0f16346994e32b479e4be596436b2a614b03.1791234123.git.bvanassche@acm.org
[4]: https://syzkaller.appspot.com/bug?extid=ecf51a7ccb6b1394e90c
[5]: https://syzkaller.appspot.com/bug?extid=2e5cf2352e9f69ebf172
[6]: https://lore.kernel.org/[email protected]
[7]: https://lore.kernel.org/[email protected]
[8]: https://lore.kernel.org/[email protected]
---
Christian Brauner (2):
jfs: wait for lbmDONE under LCACHE_LOCK in lbmRead()
jfs: set lbmDONE before dropping LCACHE_LOCK for lmPostGC()
fs/jfs/jfs_logmgr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
---
base-commit: 2dc60bd9975fc8666a13d58410499e06e6799ba9
change-id: 20261123-work-loop-backing-file-c1d1030c3d64
Christian Brauner
maintainerPATCH 1/2 jfs: wait for lbmDONE under LCACHE_LOCK in lbmRead()
lbmRead() waits for the read without taking LCACHE_LOCK and returns as soon as bp->l_flag is no longer lbmREAD. Since commit b15e4310633f ("jfs: Set the lbmDone flag at the end of lbmIODone") lbmIODone() clears lbmREAD and wakes the reader first and only sets lbmDONE right before it drops LCACHE_LOCK. So the reader can already be using the buffer again when lbmIODone() ORs lbmDONE into bp->l_flag.
lmLogShutdown() reads the log superblock, updates it and writes it back via lbmDirectWrite(). lbmDirectWrite() sets bp->l_flag without taking LCACHE_LOCK and submits the write. If that store happens before the late lbmDONE from the read completion then the write is marked as done before it has even been issued:
umount read completion
====== ===============
lmLogShutdown()
-> lbmRead(log, 1, &bpsuper)
lbmIODone()
-> LCACHE_LOCK()
bp->l_flag &= ~lbmREAD
wake_up(&bp->l_ioevent)
// bp->l_flag != lbmREAD
-> lbmDirectWrite(log, bpsuper, ...)
bp->l_flag = lbmWRITE | lbmRELEASE | lbmSYNC | lbmDIRECT
submit_bio()
-> lbmIOWait(bpsuper, lbmFREE)
-> LCACHE_LOCK()
bp->l_flag |= lbmDONE
LCACHE_UNLOCK()
// lbmDONE is set, don't wait
-> lbmLogShutdown()
// free all lbufs and their pageslmLogShutdown() returns, the unmount finishes and the block device is released while the superblock write is still queued. What happens next depends on who gets there first. If the write is still sitting in the loop device when the device is torn down then __loop_clr_fd() has already cleared lo->lo_backing_file and the loop worker dereferences NULL:
KASAN: null-ptr-deref in range [0x00000000000000a0-0x00000000000000a7] Workqueue: loop6 loop_workfn RIP: 0010:file_inode include/linux/fs.h:1351 [inline] RIP: 0010:kiocb_start_write include/linux/fs.h:2797 [inline] RIP: 0010:lo_rw_aio+0xc26/0x1100 drivers/block/loop.c:398 Call Trace: do_req_filebacked drivers/block/loop.c:430 [inline] loop_handle_cmd drivers/block/loop.c:1847 [inline] loop_process_work+0x638/0x11d0 drivers/block/loop.c:1882 process_one_work kernel/workqueue.c:3495 [inline] process_scheduled_works+0xc3d/0x1630 kernel/workqueue.c:3578 worker_thread+0xa47/0xfb0 kernel/workqueue.c:3659 kthread+0x38b/0x470 kernel/kthread.c:436
If the loop worker gets to it first the freed page is written to the backing file and lbmIODone() then runs on the freed lbuf:
BUG: KASAN: slab-use-after-free in lbmIODone+0x130e/0x16b0 fs/jfs/jfs_logmgr.c:2192 Read of size 4 by task ksoftirqd/0/16 Call Trace: lbmIODone+0x130e/0x16b0 fs/jfs/jfs_logmgr.c:2192 blk_update_request+0x57b/0xe50 block/blk-mq.c:1036 blk_mq_end_request+0x3e/0x70 block/blk-mq.c:1167 blk_complete_reqs block/blk-mq.c:1242 [inline] blk_done_softirq+0x10a/0x160 block/blk-mq.c:1247 handle_softirqs+0x1d4/0x6c0 kernel/softirq.c:669 run_ksoftirqd+0x53/0x190 kernel/softirq.c:1133 Freed by task 5632: kfree+0x1c7/0x6d0 mm/slub.c:6930 lbmLogShutdown fs/jfs/jfs_logmgr.c:1862 [inline] lmLogShutdown+0x456/0x850 fs/jfs/jfs_logmgr.c:1681 lmLogClose+0x28c/0x530 fs/jfs/jfs_logmgr.c:1457 jfs_umount+0x2da/0x3b0 fs/jfs/jfs_umount.c:124 jfs_put_super+0x8c/0x190 fs/jfs/super.c:194 generic_shutdown_super+0x140/0x2d0 fs/super.c:723 kill_block_super+0x44/0xa0 fs/super.c:1852 cleanup_mnt+0x204/0x2a0 fs/namespace.c:1396
If the loop worker got hold of lo->lo_backing_file right before __loop_clr_fd() cleared it then it writes through a file that is being released instead. That's syzbot's "WARNING in file_remove_privs_flags". I haven't seen that one in my runs but in all four logs of it that syzbot kept the process that owns the loop device had mounted a jfs image a few seconds earlier.
On PREEMPT_RT LCACHE_LOCK doesn't disable preemption. The woken umount task preempts the completion on the same CPU, gets all the way to lbmIOWait() and only then lets the completion finish. With syzbot's reproducer that happens in about one out of 70 unmounts. Without PREEMPT_RT the umount task has to be running on another CPU already and I've only seen it once in 26400 unmounts. lmLogFileSystem() does the same read, update and direct write of the log superblock for external logs.
The lockless wait isn't new. Before commit b15e4310633f ("jfs: Set the lbmDone flag at the end of lbmIODone") lbmIODone() set lbmDONE first and that ended the reader's wait early as well. lbmDirectWrite()'s store could then get lost in lbmIODone()'s read-modify-write of bp->l_flag when it cleared lbmREAD, leaving nothing but lbmDONE behind. That window was a lot smaller but syzbot hit it too.
So wait for the read via lbmIOWait() which waits for lbmDONE under LCACHE_LOCK. lbmIODone() sets lbmDONE and drops the lock as the last thing it does so once lbmIOWait() returns the completion is done with the buffer. Note that lbmRead() keeps ignoring lbmERROR like it did before. That's a separate problem.
--- fs/jfs/jfs_logmgr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jfs/jfs_logmgr.c +1 -1
@@ -1984,7 +1984,7 @@ static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)submit_bio (bio); } -wait_event (bp->l_ioevent, (bp->l_flag != lbmREAD)); +lbmIOWait (bp,0 );return 0 ; }
Signature · 1 line
2.53.0
Re: [PATCH 1/2] jfs: wait for lbmDONE under LCACHE_LOCK in lbmRead()
fs/jfs/jfs_logmgr.c static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp) · line 1990
Quoted 13 lines · Christian Brauner
fs/jfs/jfs_logmgr.c +1
-1@@ -1984,7 +1984,7 @@ static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)submit_bio (bio); } -wait_event (bp->l_ioevent, (bp->l_flag != lbmREAD)); +lbmIOWait (bp,0 );return 0 ; }
Christian Brauner
maintainerPATCH 2/2 jfs: set lbmDONE before dropping LCACHE_LOCK for lmPostGC()
In reply to Christian BraunerSince commit b15e4310633f ("jfs: Set the lbmDone flag at the end of lbmIODone") lbmIODone() sets lbmDONE as the very last thing. For a group commit write that means after it dropped LCACHE_LOCK, called lmPostGC() and took the lock again. But lmPostGC() can hand the buffer back to the freelist via lbmFree() or queue it for the next write via lbmWrite(), and the buffer of the current log page is written by group commit over and over. So the lbmDONE set afterwards can land on the next I/O of the buffer.
At unmount lmLogShutdown() writes the current log page via lbmWrite() and waits for it via lbmIOWait(log->bp, lbmFREE). If the completion of an earlier group commit write of that page sets its lbmDONE in between then lbmIOWait() returns while the page is still in flight and on the write queue and lbmfree() asserts:
BUG at fs/jfs/jfs_logmgr.c:1919 assert(bp->l_wqnext == NULL) kernel BUG at fs/jfs/jfs_logmgr.c:1919!
RIP: 0010:lbmfree fs/jfs/jfs_logmgr.c:1919 [inline] RIP: 0010:lbmIOWait+0x5ab/0x5b0 fs/jfs/jfs_logmgr.c:2154 Call Trace: lmLogShutdown+0x1ff/0x850 fs/jfs/jfs_logmgr.c:1657 lmLogClose+0x28c/0x530 fs/jfs/jfs_logmgr.c:1457 jfs_umount+0x2da/0x3b0 fs/jfs/jfs_umount.c:124 jfs_put_super+0x8c/0x190 fs/jfs/super.c:194 generic_shutdown_super+0x13d/0x2d0 fs/super.c:751 kill_block_super+0x44/0xa0 fs/super.c:1903 deactivate_locked_super+0xbe/0x110 fs/super.c:586 cleanup_mnt+0x204/0x2a0 fs/namespace.c:1396
In my build of the same config R12 holds bp->l_flag at that point and in the report it is 0x2e, i.e., lbmWRITE | lbmRELEASE | lbmSYNC | lbmDONE. So the completion of the write lmLogShutdown() waits for hasn't run yet and lbmDONE came from somewhere else. A completion that only gets to the buffer after lbmWrite() stored those flags takes them for its own, clears lbmWRITE and takes the buffer off the write queue. A read completion clears lbmREAD and sets lbmDONE under the same LCACHE_LOCK. The group commit branch is the only place in lbmIODone() that drops LCACHE_LOCK before it sets lbmDONE.
With a workload that writes and fsyncs a few files before every unmount on PREEMPT_RT the late lbmDONE lands on a new I/O of the buffer in about one out of ten group commit completions. Mostly that's the next write lmPostGC() itself just queued. But in about one out of 1800 completions another task reinitialized the buffer while the lock was dropped. Preempted completions kept it dropped for up to 14 ms. I haven't seen the late lbmDONE land on lmLogShutdown()'s final write on its own in about a million unmounts. But with a debug patch that holds the completion right after lmPostGC() until lmLogShutdown() has submitted that write every VM hits exactly the BUG above within the first few unmounts, with the same 0x2e in R12. With this patch the same delay leaves the completion nothing to do after lmPostGC() and 15400 unmounts run clean.
So set lbmDONE before dropping the lock for lmPostGC() like lbmIODone() did before commit b15e4310633f ("jfs: Set the lbmDone flag at the end of lbmIODone") and don't touch the buffer after lmPostGC(). That doesn't bring back what that commit fixed. That was lbmRead() returning while lbmIODone() still used the buffer. Nobody waits for lbmDONE on a group commit write. The transactions wait for their commit records via tblkGC_COMMITTED instead.
--- fs/jfs/jfs_logmgr.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/jfs/jfs_logmgr.c +3 -1
@@ -2279,9 +2279,11 @@ static void lbmIODone(struct bio *bio)* Group Commit pageout: */ else if (bp->l_flag & lbmGC) { +/* lmPostGC() may free or requeue @bp. */ + bp->l_flag |= lbmDONE;LCACHE_UNLOCK (flags);lmPostGC (bp); -LCACHE_LOCK (flags);/* disable+lock */ +return ; }/*
Signature · 1 line
2.53.0
Re: [PATCH 2/2] jfs: set lbmDONE before dropping LCACHE_LOCK for lmPostGC()
Quoted 5 lines · Christian Brauner
fs/jfs/jfs_logmgr.c +0
-0
fs/jfs/jfs_logmgr.c static void lbmIODone(struct bio *bio) · line 2287
Quoted 10 lines
fs/jfs/jfs_logmgr.c +3
-1@@ -2279,9 +2279,11 @@ static void lbmIODone(struct bio *bio)* Group Commit pageout: */ else if (bp->l_flag & lbmGC) { +/* lmPostGC() may free or requeue @bp. */ + bp->l_flag |= lbmDONE;LCACHE_UNLOCK (flags);lmPostGC (bp); -LCACHE_LOCK (flags);/* disable+lock */ +return ; }