Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ext4: fix use-after-free in update_super_work when racing with umount

Commit b98535d09179 ("ext4: fix bug_on in start_this_handle during umount
filesystem") moved ext4_unregister_sysfs() before flushing s_sb_upd_work
to prevent new error work from being queued via /proc/fs/ext4/xx/mb_groups
reads during unmount. However, this introduced a use-after-free because
update_super_work calls ext4_notify_error_sysfs() -> sysfs_notify() which
accesses the kobject's kernfs_node after it has been freed by kobject_del()
in ext4_unregister_sysfs():

update_super_work ext4_put_super
----------------- --------------
ext4_unregister_sysfs(sb)
kobject_del(&sbi->s_kobj)
__kobject_del()
sysfs_remove_dir()
kobj->sd = NULL
sysfs_put(sd)
kernfs_put() // RCU free
ext4_notify_error_sysfs(sbi)
sysfs_notify(&sbi->s_kobj)
kn = kobj->sd // stale pointer
kernfs_get(kn) // UAF on freed kernfs_node
ext4_journal_destroy()
flush_work(&sbi->s_sb_upd_work)

Instead of reordering the teardown sequence, fix this by making
ext4_notify_error_sysfs() detect that sysfs has already been torn down
by checking s_kobj.state_in_sysfs, and skipping the sysfs_notify() call
in that case. A dedicated mutex (s_error_notify_mutex) serializes
ext4_notify_error_sysfs() against kobject_del() in ext4_unregister_sysfs()
to prevent TOCTOU races where the kobject could be deleted between the
state_in_sysfs check and the sysfs_notify() call.

Fixes: b98535d09179 ("ext4: fix bug_on in start_this_handle during umount filesystem")
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Jiayuan Chen <jiayuan.chen@shopee.com>
Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20260319120336.157873-1-jiayuan.chen@linux.dev
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@kernel.org

authored by

Jiayuan Chen and committed by
Theodore Ts'o
d15e4b0a 496bb99b

+11 -1
+1
fs/ext4/ext4.h
··· 1570 1570 struct proc_dir_entry *s_proc; 1571 1571 struct kobject s_kobj; 1572 1572 struct completion s_kobj_unregister; 1573 + struct mutex s_error_notify_mutex; /* protects sysfs_notify vs kobject_del */ 1573 1574 struct super_block *s_sb; 1574 1575 struct buffer_head *s_mmp_bh; 1575 1576
+1
fs/ext4/super.c
··· 5406 5406 5407 5407 timer_setup(&sbi->s_err_report, print_daily_error_info, 0); 5408 5408 spin_lock_init(&sbi->s_error_lock); 5409 + mutex_init(&sbi->s_error_notify_mutex); 5409 5410 INIT_WORK(&sbi->s_sb_upd_work, update_super_work); 5410 5411 5411 5412 err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
+9 -1
fs/ext4/sysfs.c
··· 597 597 598 598 void ext4_notify_error_sysfs(struct ext4_sb_info *sbi) 599 599 { 600 - sysfs_notify(&sbi->s_kobj, NULL, "errors_count"); 600 + mutex_lock(&sbi->s_error_notify_mutex); 601 + if (sbi->s_kobj.state_in_sysfs) 602 + sysfs_notify(&sbi->s_kobj, NULL, "errors_count"); 603 + mutex_unlock(&sbi->s_error_notify_mutex); 601 604 } 602 605 603 606 static struct kobject *ext4_root; ··· 613 610 int err; 614 611 615 612 init_completion(&sbi->s_kobj_unregister); 613 + mutex_lock(&sbi->s_error_notify_mutex); 616 614 err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root, 617 615 "%s", sb->s_id); 616 + mutex_unlock(&sbi->s_error_notify_mutex); 618 617 if (err) { 619 618 kobject_put(&sbi->s_kobj); 620 619 wait_for_completion(&sbi->s_kobj_unregister); ··· 649 644 650 645 if (sbi->s_proc) 651 646 remove_proc_subtree(sb->s_id, ext4_proc_root); 647 + 648 + mutex_lock(&sbi->s_error_notify_mutex); 652 649 kobject_del(&sbi->s_kobj); 650 + mutex_unlock(&sbi->s_error_notify_mutex); 653 651 } 654 652 655 653 int __init ext4_init_sysfs(void)