Skip to content

Commit c2a04b0

Browse files
Jamie IlesAndreas Gruenbacher
Jamie Iles
authored and
Andreas Gruenbacher
committed
gfs2: use-after-free in sysfs deregistration
syzkaller found the following splat with CONFIG_DEBUG_KOBJECT_RELEASE=y: Read of size 1 at addr ffff000028e896b8 by task kworker/1:2/228 CPU: 1 PID: 228 Comm: kworker/1:2 Tainted: G S 5.9.0-rc8+ #101 Hardware name: linux,dummy-virt (DT) Workqueue: events kobject_delayed_cleanup Call trace: dump_backtrace+0x0/0x4d8 show_stack+0x34/0x48 dump_stack+0x174/0x1f8 print_address_description.constprop.0+0x5c/0x550 kasan_report+0x13c/0x1c0 __asan_report_load1_noabort+0x34/0x60 memcmp+0xd0/0xd8 gfs2_uevent+0xc4/0x188 kobject_uevent_env+0x54c/0x1240 kobject_uevent+0x2c/0x40 __kobject_del+0x190/0x1d8 kobject_delayed_cleanup+0x2bc/0x3b8 process_one_work+0x96c/0x18c0 worker_thread+0x3f0/0xc30 kthread+0x390/0x498 ret_from_fork+0x10/0x18 Allocated by task 1110: kasan_save_stack+0x28/0x58 __kasan_kmalloc.isra.0+0xc8/0xe8 kasan_kmalloc+0x10/0x20 kmem_cache_alloc_trace+0x1d8/0x2f0 alloc_super+0x64/0x8c0 sget_fc+0x110/0x620 get_tree_bdev+0x190/0x648 gfs2_get_tree+0x50/0x228 vfs_get_tree+0x84/0x2e8 path_mount+0x1134/0x1da8 do_mount+0x124/0x138 __arm64_sys_mount+0x164/0x238 el0_svc_common.constprop.0+0x15c/0x598 do_el0_svc+0x60/0x150 el0_svc+0x34/0xb0 el0_sync_handler+0xc8/0x5b4 el0_sync+0x15c/0x180 Freed by task 228: kasan_save_stack+0x28/0x58 kasan_set_track+0x28/0x40 kasan_set_free_info+0x24/0x48 __kasan_slab_free+0x118/0x190 kasan_slab_free+0x14/0x20 slab_free_freelist_hook+0x6c/0x210 kfree+0x13c/0x460 Use the same pattern as f2fs + ext4 where the kobject destruction must complete before allowing the FS itself to be freed. This means that we need an explicit free_sbd in the callers. Cc: Bob Peterson <[email protected]> Cc: Andreas Gruenbacher <[email protected]> Signed-off-by: Jamie Iles <[email protected]> [Also go to fail_free when init_names fails.] Signed-off-by: Andreas Gruenbacher <[email protected]>
1 parent 0e539ca commit c2a04b0

File tree

4 files changed

+11
-18
lines changed

4 files changed

+11
-18
lines changed

fs/gfs2/incore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ struct gfs2_sbd {
705705
struct super_block *sd_vfs;
706706
struct gfs2_pcpu_lkstats __percpu *sd_lkstats;
707707
struct kobject sd_kobj;
708+
struct completion sd_kobj_unregister;
708709
unsigned long sd_flags; /* SDF_... */
709710
struct gfs2_sb_host sd_sb;
710711

fs/gfs2/ops_fstype.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,26 +1062,14 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
10621062
}
10631063

10641064
error = init_names(sdp, silent);
1065-
if (error) {
1066-
/* In this case, we haven't initialized sysfs, so we have to
1067-
manually free the sdp. */
1068-
free_sbd(sdp);
1069-
sb->s_fs_info = NULL;
1070-
return error;
1071-
}
1065+
if (error)
1066+
goto fail_free;
10721067

10731068
snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s", sdp->sd_table_name);
10741069

10751070
error = gfs2_sys_fs_add(sdp);
1076-
/*
1077-
* If we hit an error here, gfs2_sys_fs_add will have called function
1078-
* kobject_put which causes the sysfs usage count to go to zero, which
1079-
* causes sysfs to call function gfs2_sbd_release, which frees sdp.
1080-
* Subsequent error paths here will call gfs2_sys_fs_del, which also
1081-
* kobject_put to free sdp.
1082-
*/
10831071
if (error)
1084-
return error;
1072+
goto fail_free;
10851073

10861074
gfs2_create_debugfs_file(sdp);
10871075

@@ -1179,9 +1167,9 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
11791167
gfs2_lm_unmount(sdp);
11801168
fail_debug:
11811169
gfs2_delete_debugfs_file(sdp);
1182-
/* gfs2_sys_fs_del must be the last thing we do, since it causes
1183-
* sysfs to call function gfs2_sbd_release, which frees sdp. */
11841170
gfs2_sys_fs_del(sdp);
1171+
fail_free:
1172+
free_sbd(sdp);
11851173
sb->s_fs_info = NULL;
11861174
return error;
11871175
}

fs/gfs2/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ static void gfs2_put_super(struct super_block *sb)
744744

745745
/* At this point, we're through participating in the lockspace */
746746
gfs2_sys_fs_del(sdp);
747+
free_sbd(sdp);
747748
}
748749

749750
/**

fs/gfs2/sys.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static void gfs2_sbd_release(struct kobject *kobj)
303303
{
304304
struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
305305

306-
free_sbd(sdp);
306+
complete(&sdp->sd_kobj_unregister);
307307
}
308308

309309
static struct kobj_type gfs2_ktype = {
@@ -655,6 +655,7 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
655655
sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
656656
sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
657657

658+
init_completion(&sdp->sd_kobj_unregister);
658659
sdp->sd_kobj.kset = gfs2_kset;
659660
error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
660661
"%s", sdp->sd_table_name);
@@ -685,6 +686,7 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
685686
fail_reg:
686687
fs_err(sdp, "error %d adding sysfs files\n", error);
687688
kobject_put(&sdp->sd_kobj);
689+
wait_for_completion(&sdp->sd_kobj_unregister);
688690
sb->s_fs_info = NULL;
689691
return error;
690692
}
@@ -695,6 +697,7 @@ void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
695697
sysfs_remove_group(&sdp->sd_kobj, &tune_group);
696698
sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
697699
kobject_put(&sdp->sd_kobj);
700+
wait_for_completion(&sdp->sd_kobj_unregister);
698701
}
699702

700703
static int gfs2_uevent(struct kset *kset, struct kobject *kobj,

0 commit comments

Comments
 (0)