alloc_super() biases s_pending_errors for every superblock but generic_shutdown_super() only drops the bias and waits for pending error reports if sb->s_root is set. fserror_alloc_event() accepts reports whenever SB_ACTIVE is set. But xfs sets SB_ACTIVE around log recovery and f2fs around gc for checkpoint=disable, both while filling the superblock. If such a mount fails s_root stays NULL, nothing waits for the queued reports and destroy_super_work() frees the superblock while fserror_worker() is still pending. The worker holds no reference to the superblock. It reads s_flags and s_op, puts the inode it pinned and decrements s_pending_errors: BUG: KASAN: slab-use-after-free in fserror_worker (fs/fserror.c:54) Read of size 8 at addr ff11000107366060 by task kworker/1:0/25 ... Allocated by task 120: __kmalloc_cache_noprof+0x27a/0x650 sget_fc (fs/super.c:326 [alloc_super()] fs/super.c:874) get_tree_bdev_flags (fs/super.c:1479 fs/super.c:1866) vfs_get_tree+0x8b/0x2f0 Freed by task 83: kfree+0x1e8/0x5e0 process_one_work+0x953/0x1700 Nobody can listen for errors of a superblock that isn't born yet. fanotify marks need a path in the mounted filesystem and the xfs health monitor is attached through an ioctl on a mounted filesystem as well. So don't accept reports before SB_BORN is set. A born superblock always has s_root set when it's shut down so every accepted report is waited for. Errors during the mount are still logged as lost reports and xfs still records them in its health state. Waiting for the reports of a failed mount in generic_shutdown_super() instead wouldn't be enough. That runs from ->kill_sb() after the error path of ->fill_super() and xfs and f2fs have already freed state there that putting a pinned inode needs. Reproducer, xfs with a dirty log and a bad crc in the refcount btree root of AG 0. Log recovery reports the corruption while xfs has SB_ACTIVE set and then the mount fails. The work item normally runs right away, so a kprobe on queue_work_on() points it at a wrapper that sleeps until destroy_super_work() has freed the superblock: mkfs.xfs -f /dev/vda mount /dev/vda /mnt && echo foo > /mnt/foo && sync xfs_io -x -c "shutdown -f" /mnt # flush the log, then shut down umount /mnt # the log stays dirty xfs_db -x -c "agf 0" -c "addr refcntroot" -c "write -c numrecs 7" /dev/vda insmod r6_fshold.ko fserror_worker=0x$(awk '$3 == "fserror_worker" { print $1 }' /proc/kallsyms) echo 1 > /sys/module/r6_fshold/parameters/armed mount /dev/vda /mnt XFS (vda): Metadata CRC error detected at xfs_refcountbt_read_verify+0x1e/0x240, xfs_refcountbt block 0x30 XFS (vda): Failed to recover leftover CoW staging extents, err -117. XFS (vda): Error -5 reserving per-AG metadata reserve pool. BUG: KASAN: slab-use-after-free in fserror_worker+0x2da/0x310 Not a selftest since the work item has to be held back with a kprobe. Fixes: 21945e6cb516 ("fs: report filesystem and file I/O errors to fsnotify") Cc: stable@vger.kernel.org Signed-off-by: Christian Brauner (Amutable) --- fs/fserror.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/fserror.c b/fs/fserror.c index d8a800580012..90ce04d58514 100644 --- a/fs/fserror.c +++ b/fs/fserror.c @@ -96,6 +96,13 @@ static inline struct fserror_event *fserror_alloc_event(struct super_block *sb, return NULL; if (!(sb->s_flags & SB_ACTIVE)) goto out_pending; + /* + * xfs and f2fs set SB_ACTIVE while filling the superblock. Nobody can + * listen before the superblock is born and if the mount fails nothing + * waits for the event. + */ + if (!(sb->s_flags & SB_BORN)) + goto out_pending; event = mempool_alloc(&fserror_events_pool, gfp_flags); if (!event) -- 2.53.0