Root Cause Analysis
The slab use-after-free occurs because the error-cleanup path in __ext4_fill_super() (fs/ext4/super.c) tears down mballoc (ext4_mb_release()) before shutting down the JBD2 journal (ext4_journal_destroy()), allowing a subsequent journal commit callback to re-queue sbi->s_discard_work after ext4_mb_release() has already flushed it and before ext4_free_sbi() frees sbi.
Detailed Mechanism
-
Initialization during mount (fs/ext4/super.c):
-
Mount failure triggers a journaled superblock update:
ext4_init_orphan_info(sb) (fs/ext4/super.c:5751) fails on a corrupted orphan file block checksum at fs/ext4/orphan.c:625 and calls ext4_error().
ext4_handle_error() (fs/ext4/super.c:734) schedules sbi->s_sb_upd_work (update_super_work), which starts and stops a JBD2 handle (jbd2_journal_start() / jbd2_journal_dirty_metadata() / jbd2_journal_stop() in fs/ext4/super.c:783-806) to record the error in the superblock.
ext4_init_orphan_info() returns -EIO, jumping to failed_mount7 in __ext4_fill_super().
-
Inverted teardown order in __ext4_fill_super():
- At
failed_mount6, __ext4_fill_super() calls ext4_mb_release(sb) (fs/ext4/super.c:5823). Inside ext4_mb_release() (fs/ext4/mballoc.c:3898), flush_work(&sbi->s_discard_work) runs and drains any currently queued discard work, then mballoc structures are freed.
- However,
sbi->s_journal is still running with j_commit_callback still pointing to ext4_journal_commit_callback.
- Later, at
failed_mount_wq, __ext4_fill_super() calls ext4_journal_destroy(sbi, sbi->s_journal) (fs/ext4/super.c:5846, defined in fs/ext4/ext4_jbd2.h:437), which calls ext4_force_commit(), flush_work(&sbi->s_sb_upd_work), and jbd2_journal_destroy().
- When
kjournald2 commits the transaction in jbd2_journal_commit_transaction(), it invokes ext4_journal_commit_callback() (fs/ext4/super.c:526) -> ext4_process_freed_data() (fs/ext4/mballoc.c:4018).
- In
ext4_process_freed_data() (fs/ext4/mballoc.c:4031-4037), when test_opt(sb, DISCARD) is set and list_empty(&sbi->s_discard_list) is true, it unconditionally calls queue_work(system_dfl_wq, &sbi->s_discard_work)βeven when freed_data_list is empty. (Note also that EXT4_MOUNT_DISCARD has not yet been cleared for non-discard devices because that check at fs/ext4/super.c:5787 comes after ext4_init_orphan_info().)
-
Use-after-free:
- After
ext4_journal_destroy() finishes, __ext4_fill_super() returns the error without flushing sbi->s_discard_work again.
ext4_fill_super() frees sbi via ext4_free_sbi(sbi) (fs/ext4/super.c:5925 -> 4418).
- When the worker thread subsequently runs
ext4_discard_work() (fs/ext4/mballoc.c:3645), reading sbi->s_sb (at offset 1008 bytes in struct ext4_sb_info) accesses the freed kmalloc-8k slab object.
Note that normal unmount in ext4_put_super() (fs/ext4/super.c:1333-1345) avoids this by calling ext4_journal_destroy() before ext4_mb_release(sb). In __ext4_fill_super(), either the journal must be destroyed (or j_commit_callback / s_discard_work handled) before ext4_mb_release(), or s_discard_work must not be queued once teardown/mballoc release begins and must be flushed after ext4_journal_destroy(). Additionally, ext4_process_freed_data() does not need to queue s_discard_work if freed_data_list is empty.