2024-05-09 01:01:23

by Ubisectech Sirius

[permalink] [raw]
Subject: KASAN: slab-use-after-free Read in bch2_fs_recovery

Hello.
We are Ubisectech Sirius Team, the vulnerability lab of China ValiantSec. Recently, our team has discovered a issue in Linux kernel 6.7. Attached to the email were a PoC file of the issue.

Stack dump:
==================================================================
BUG: KASAN: slab-use-after-free in bch2_fs_recovery+0x4bfa/0x52d0 fs/bcachefs/recovery.c:775
Read of size 8 at addr ffff88806b8a6f90 by task syz-executor.3/30852

CPU: 1 PID: 30852 Comm: syz-executor.3 Not tainted 6.7.0 #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106
print_address_description mm/kasan/report.c:364 [inline]
print_report+0xc1/0x5e0 mm/kasan/report.c:475
kasan_report+0xbe/0xf0 mm/kasan/report.c:588
bch2_fs_recovery+0x4bfa/0x52d0 fs/bcachefs/recovery.c:775
bch2_fs_start+0x365/0x5e0 fs/bcachefs/super.c:978
bch2_fs_open+0x1ac9/0x3890 fs/bcachefs/super.c:1968
bch2_mount+0x538/0x13c0 fs/bcachefs/fs.c:1863
legacy_get_tree+0x109/0x220 fs/fs_context.c:662
vfs_get_tree+0x93/0x380 fs/super.c:1771
do_new_mount fs/namespace.c:3337 [inline]
path_mount+0x679/0x1e40 fs/namespace.c:3664
do_mount fs/namespace.c:3677 [inline]
__do_sys_mount fs/namespace.c:3886 [inline]
__se_sys_mount fs/namespace.c:3863 [inline]
__x64_sys_mount+0x287/0x310 fs/namespace.c:3863
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0x43/0x120 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x6f/0x77
RIP: 0033:0x7f14eb691b3e
Code: 48 c7 c0 ff ff ff ff eb aa e8 be 0d 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 f3 0f 1e fa 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f14ec38be38 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00000000000119fb RCX: 00007f14eb691b3e
RDX: 0000000020000080 RSI: 0000000020000040 RDI: 00007f14ec38be90
RBP: 00007f14ec38bed0 R08: 00007f14ec38bed0 R09: 0000000001210014
R10: 0000000001210014 R11: 0000000000000202 R12: 0000000020000080
R13: 0000000020000040 R14: 00007f14ec38be90 R15: 0000000020000400
</TASK>

Allocated by task 30852:
kasan_save_stack+0x22/0x50 mm/kasan/common.c:45
kasan_set_track+0x25/0x30 mm/kasan/common.c:52
____kasan_kmalloc mm/kasan/common.c:374 [inline]
____kasan_kmalloc mm/kasan/common.c:333 [inline]
__kasan_kmalloc+0xaa/0xb0 mm/kasan/common.c:383
kasan_kmalloc include/linux/kasan.h:198 [inline]
__do_kmalloc_node mm/slab_common.c:1007 [inline]
__kmalloc_node_track_caller+0x5e/0xd0 mm/slab_common.c:1027
__do_krealloc mm/slab_common.c:1395 [inline]
krealloc+0x5e/0x110 mm/slab_common.c:1428
bch2_sb_realloc+0x1ea/0x4c0 fs/bcachefs/super-io.c:207
__copy_super+0x83c/0xb90 fs/bcachefs/super-io.c:591
bch2_sb_to_fs+0xa9/0x170 fs/bcachefs/super-io.c:618
bch2_fs_alloc fs/bcachefs/super.c:778 [inline]
bch2_fs_open+0x151e/0x3890 fs/bcachefs/super.c:1947
bch2_mount+0x538/0x13c0 fs/bcachefs/fs.c:1863
legacy_get_tree+0x109/0x220 fs/fs_context.c:662
vfs_get_tree+0x93/0x380 fs/super.c:1771
do_new_mount fs/namespace.c:3337 [inline]
path_mount+0x679/0x1e40 fs/namespace.c:3664
do_mount fs/namespace.c:3677 [inline]
__do_sys_mount fs/namespace.c:3886 [inline]
__se_sys_mount fs/namespace.c:3863 [inline]
__x64_sys_mount+0x287/0x310 fs/namespace.c:3863
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0x43/0x120 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x6f/0x77

Freed by task 30852:
kasan_save_stack+0x22/0x50 mm/kasan/common.c:45
kasan_set_track+0x25/0x30 mm/kasan/common.c:52
kasan_save_free_info+0x2b/0x50 mm/kasan/generic.c:522
____kasan_slab_free mm/kasan/common.c:236 [inline]
____kasan_slab_free+0x162/0x1c0 mm/kasan/common.c:200
kasan_slab_free include/linux/kasan.h:164 [inline]
slab_free_hook mm/slub.c:1800 [inline]
slab_free_freelist_hook+0x95/0x1d0 mm/slub.c:1826
slab_free mm/slub.c:3809 [inline]
__kmem_cache_free+0xc0/0x180 mm/slub.c:3822
krealloc+0xbd/0x110 mm/slab_common.c:1430
bch2_sb_realloc+0x1ea/0x4c0 fs/bcachefs/super-io.c:207
bch2_sb_field_resize_id+0x19c/0x5a0 fs/bcachefs/super-io.c:239
bch2_sb_counters_from_cpu+0x2d5/0x3d0 fs/bcachefs/counters.c:75
bch2_write_super+0xacd/0x2600 fs/bcachefs/super-io.c:939
bch2_fs_recovery+0x1c96/0x52d0 fs/bcachefs/recovery.c:773
bch2_fs_start+0x365/0x5e0 fs/bcachefs/super.c:978
bch2_fs_open+0x1ac9/0x3890 fs/bcachefs/super.c:1968
bch2_mount+0x538/0x13c0 fs/bcachefs/fs.c:1863
legacy_get_tree+0x109/0x220 fs/fs_context.c:662
vfs_get_tree+0x93/0x380 fs/super.c:1771
do_new_mount fs/namespace.c:3337 [inline]
path_mount+0x679/0x1e40 fs/namespace.c:3664
do_mount fs/namespace.c:3677 [inline]
__do_sys_mount fs/namespace.c:3886 [inline]
__se_sys_mount fs/namespace.c:3863 [inline]
__x64_sys_mount+0x287/0x310 fs/namespace.c:3863
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0x43/0x120 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x6f/0x77

The buggy address belongs to the object at ffff88806b8a6000
which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 3984 bytes inside of
freed 4096-byte region [ffff88806b8a6000, ffff88806b8a7000)

The buggy address belongs to the physical page:
page:ffffea0001ae2800 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x6b8a0
head:ffffea0001ae2800 order:3 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0x4fff00000000840(slab|head|node=1|zone=1|lastcpupid=0x7ff)
page_type: 0xffffffff()
raw: 04fff00000000840 ffff888014842140 dead000000000100 dead000000000122
raw: 0000000000000000 0000000000040004 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0x1d20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC|__GFP_HARDWALL), pid 28419, tgid 28418 (syz-executor.1), ts 779173756005, free_ts 759046523333
set_page_owner include/linux/page_owner.h:31 [inline]
post_alloc_hook+0x2dd/0x350 mm/page_alloc.c:1537
prep_new_page mm/page_alloc.c:1544 [inline]
get_page_from_freelist+0xd38/0x2fa0 mm/page_alloc.c:3312
__alloc_pages+0x21d/0x21f0 mm/page_alloc.c:4568
alloc_pages_mpol+0x245/0x5f0 mm/mempolicy.c:2133
alloc_slab_page mm/slub.c:1870 [inline]
allocate_slab mm/slub.c:2017 [inline]
new_slab+0x28f/0x3d0 mm/slub.c:2070
___slab_alloc+0xac4/0x1480 mm/slub.c:3223
__slab_alloc.constprop.0+0x56/0xb0 mm/slub.c:3322
__slab_alloc_node mm/slub.c:3375 [inline]
slab_alloc_node mm/slub.c:3468 [inline]
__kmem_cache_alloc_node+0x132/0x330 mm/slub.c:3517
kmalloc_trace+0x26/0x60 mm/slab_common.c:1098
kmalloc include/linux/slab.h:600 [inline]
kzalloc include/linux/slab.h:721 [inline]
alloc_super+0x52/0xb50 fs/super.c:326
sget+0x11c/0x600 fs/super.c:889
btrfs_mount_root+0x62b/0xd70 fs/btrfs/super.c:1464
legacy_get_tree+0x109/0x220 fs/fs_context.c:662
vfs_get_tree+0x93/0x380 fs/super.c:1771
fc_mount fs/namespace.c:1114 [inline]
vfs_kern_mount.part.0+0xd7/0x170 fs/namespace.c:1144
vfs_kern_mount+0x40/0x60 fs/namespace.c:1131
page last free stack trace:
reset_page_owner include/linux/page_owner.h:24 [inline]
free_pages_prepare mm/page_alloc.c:1137 [inline]
free_unref_page_prepare+0x4cd/0xa70 mm/page_alloc.c:2347
free_unref_page+0x33/0x3d0 mm/page_alloc.c:2487
vfree+0x180/0x7b0 mm/vmalloc.c:2842
kcov_put kernel/kcov.c:429 [inline]
kcov_put+0x2a/0x40 kernel/kcov.c:425
kcov_close+0x10/0x20 kernel/kcov.c:525
__fput+0x282/0xbc0 fs/file_table.c:394
task_work_run+0x16a/0x260 kernel/task_work.c:180
exit_task_work include/linux/task_work.h:38 [inline]
do_exit+0xaf0/0x2a40 kernel/exit.c:869
do_group_exit+0xd4/0x2a0 kernel/exit.c:1018
get_signal+0x2440/0x2630 kernel/signal.c:2904
arch_do_signal_or_restart+0x81/0x7e0 arch/x86/kernel/signal.c:309
exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
exit_to_user_mode_prepare+0x121/0x240 kernel/entry/common.c:204
__syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
syscall_exit_to_user_mode+0x1e/0x60 kernel/entry/common.c:296
do_syscall_64+0x53/0x120 arch/x86/entry/common.c:89
entry_SYSCALL_64_after_hwframe+0x6f/0x77

Memory state around the buggy address:
ffff88806b8a6e80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff88806b8a6f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88806b8a6f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff88806b8a7000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88806b8a7080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================
Thank you for taking the time to read this email and we look forward to working with you further.






Attachments:
poc.c (335.53 kB)