Kernel bug: KASAN: use-after-free Read in bch2_checksum
Subsystem: bcachefs
Status: fixed

Crash report:
==================================================================
BUG: KASAN: slab-use-after-free in poly1305_update include/crypto/poly1305.h:83 [inline]
BUG: KASAN: slab-use-after-free in bch2_checksum+0x209/0x490 fs/bcachefs/checksum.c:157
Read of size 8 at addr ffff88805ba1b070 by task syz.1.17/6035

CPU: 0 UID: 0 PID: 6035 Comm: syz.1.17 Not tainted 6.15.0-rc6-syzkaller-00047-ge9565e23cd89 #0 PREEMPT(full) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/07/2025
Call Trace:
 <TASK>
 dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120
 print_address_description mm/kasan/report.c:408 [inline]
 print_report+0xb4/0x290 mm/kasan/report.c:521
 kasan_report+0x118/0x150 mm/kasan/report.c:634
 check_region_inline mm/kasan/generic.c:-1 [inline]
 kasan_check_range+0x29a/0x2b0 mm/kasan/generic.c:189
 __asan_memcpy+0x29/0x70 mm/kasan/shadow.c:105
 poly1305_update include/crypto/poly1305.h:83 [inline]
 bch2_checksum+0x209/0x490 fs/bcachefs/checksum.c:157
 bch2_btree_node_read_done+0x1003/0x5470 fs/bcachefs/btree_io.c:1133
 btree_node_read_work+0x565/0xef0 fs/bcachefs/btree_io.c:1367
 bch2_btree_node_read+0x2151/0x27a0 fs/bcachefs/btree_io.c:-1
 __bch2_btree_root_read fs/bcachefs/btree_io.c:1801 [inline]
 bch2_btree_root_read+0x5e7/0x750 fs/bcachefs/btree_io.c:1823
 read_btree_roots+0x2cb/0x800 fs/bcachefs/recovery.c:582
 bch2_fs_recovery+0x2356/0x37b0 fs/bcachefs/recovery.c:929
 bch2_fs_start+0x70b/0xae0 fs/bcachefs/super.c:1096
 bch2_fs_get_tree+0xad7/0x13b0 fs/bcachefs/fs.c:2505
 vfs_get_tree+0x92/0x2b0 fs/super.c:1759
 do_new_mount+0x24a/0xa40 fs/namespace.c:3881
 do_mount fs/namespace.c:4221 [inline]
 __do_sys_mount fs/namespace.c:4432 [inline]
 __se_sys_mount+0x317/0x410 fs/namespace.c:4409
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xf6/0x210 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f6fa8d9010a
Code: d8 64 89 02 48 c7 c0 ff ff ff ff eb a6 e8 de 1a 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f6fa9bf7e68 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007f6fa9bf7ef0 RCX: 00007f6fa8d9010a
RDX: 000020000000f640 RSI: 0000200000000140 RDI: 00007f6fa9bf7eb0
RBP: 000020000000f640 R08: 00007f6fa9bf7ef0 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000200000000140
R13: 00007f6fa9bf7eb0 R14: 000000000000f623 R15: 0000200000000180
 </TASK>

Allocated by task 5944:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:68
 unpoison_slab_object mm/kasan/common.c:319 [inline]
 __kasan_slab_alloc+0x6c/0x80 mm/kasan/common.c:345
 kasan_slab_alloc include/linux/kasan.h:250 [inline]
 slab_post_alloc_hook mm/slub.c:4147 [inline]
 slab_alloc_node mm/slub.c:4196 [inline]
 kmem_cache_alloc_lru_noprof+0x1c6/0x3d0 mm/slub.c:4215
 alloc_inode+0x6a/0x1b0 fs/inode.c:346
 new_inode+0x22/0x170 fs/inode.c:1145
 debugfs_get_inode fs/debugfs/inode.c:72 [inline]
 __debugfs_create_file+0x14d/0x4f0 fs/debugfs/inode.c:447
 debugfs_create_file_short+0x3f/0x60 fs/debugfs/inode.c:485
 add_sta_files net/mac80211/debugfs_netdev.c:845 [inline]
 add_files net/mac80211/debugfs_netdev.c:958 [inline]
 ieee80211_debugfs_add_netdev net/mac80211/debugfs_netdev.c:1011 [inline]
 ieee80211_debugfs_recreate_netdev+0xbd7/0x1460 net/mac80211/debugfs_netdev.c:1035
 ieee80211_if_add+0xc17/0x1390 net/mac80211/iface.c:2202
 ieee80211_register_hw+0x3475/0x4060 net/mac80211/main.c:1604
 mac80211_hwsim_new_radio+0x2f0e/0x5340 drivers/net/wireless/virtual/mac80211_hwsim.c:5559
 hwsim_new_radio_nl+0xea4/0x1b10 drivers/net/wireless/virtual/mac80211_hwsim.c:6243
 genl_family_rcv_msg_doit+0x212/0x300 net/netlink/genetlink.c:1115
 genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]
 genl_rcv_msg+0x60e/0x790 net/netlink/genetlink.c:1210
 netlink_rcv_skb+0x21c/0x490 net/netlink/af_netlink.c:2534
 genl_rcv+0x28/0x40 net/netlink/genetlink.c:1219
 netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline]
 netlink_unicast+0x758/0x8d0 net/netlink/af_netlink.c:1339
 netlink_sendmsg+0x805/0xb30 net/netlink/af_netlink.c:1883
 sock_sendmsg_nosec net/socket.c:712 [inline]
 __sock_sendmsg+0x21c/0x270 net/socket.c:727
 __sys_sendto+0x3bd/0x520 net/socket.c:2180
 __do_sys_sendto net/socket.c:2187 [inline]
 __se_sys_sendto net/socket.c:2183 [inline]
 __x64_sys_sendto+0xde/0x100 net/socket.c:2183
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xf6/0x210 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 15:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:68
 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:576
 poison_slab_object mm/kasan/common.c:247 [inline]
 __kasan_slab_free+0x62/0x70 mm/kasan/common.c:264
 kasan_slab_free include/linux/kasan.h:233 [inline]
 slab_free_hook mm/slub.c:2380 [inline]
 slab_free mm/slub.c:4642 [inline]
 kmem_cache_free+0x192/0x3f0 mm/slub.c:4744
 rcu_do_batch kernel/rcu/tree.c:2568 [inline]
 rcu_core+0xca8/0x1710 kernel/rcu/tree.c:2824
 handle_softirqs+0x286/0x870 kernel/softirq.c:579
 run_ksoftirqd+0x9b/0x100 kernel/softirq.c:968
 smpboot_thread_fn+0x542/0xa60 kernel/smpboot.c:164
 kthread+0x70e/0x8a0 kernel/kthread.c:464
 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:153
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

Last potentially related work creation:
 kasan_save_stack+0x3e/0x60 mm/kasan/common.c:47
 kasan_record_aux_stack+0xbc/0xd0 mm/kasan/generic.c:548
 __call_rcu_common kernel/rcu/tree.c:3082 [inline]
 call_rcu+0x142/0x990 kernel/rcu/tree.c:3202
 destroy_inode fs/inode.c:401 [inline]
 evict+0x847/0x9c0 fs/inode.c:834
 __dentry_kill+0x209/0x660 fs/dcache.c:660
 dput+0x19f/0x2b0 fs/dcache.c:902
 find_next_child fs/libfs.c:603 [inline]
 simple_recursive_removal+0x2c2/0x8e0 fs/libfs.c:618
 debugfs_remove+0x5b/0x70 fs/debugfs/inode.c:805
 ieee80211_debugfs_remove_netdev+0x52/0xb0 net/mac80211/debugfs_netdev.c:1021
 ieee80211_teardown_sdata+0x5a/0x140 net/mac80211/iface.c:818
 ieee80211_if_change_type+0x14c/0x990 net/mac80211/iface.c:1938
 ieee80211_change_iface+0xd5/0x510 net/mac80211/cfg.c:241
 rdev_change_virtual_intf net/wireless/rdev-ops.h:74 [inline]
 cfg80211_change_iface+0x795/0xef0 net/wireless/util.c:1217
 nl80211_set_interface+0x773/0xaa0 net/wireless/nl80211.c:4414
 genl_family_rcv_msg_doit+0x212/0x300 net/netlink/genetlink.c:1115
 genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]
 genl_rcv_msg+0x60e/0x790 net/netlink/genetlink.c:1210
 netlink_rcv_skb+0x21c/0x490 net/netlink/af_netlink.c:2534
 genl_rcv+0x28/0x40 net/netlink/genetlink.c:1219
 netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline]
 netlink_unicast+0x758/0x8d0 net/netlink/af_netlink.c:1339
 netlink_sendmsg+0x805/0xb30 net/netlink/af_netlink.c:1883
 sock_sendmsg_nosec net/socket.c:712 [inline]
 __sock_sendmsg+0x21c/0x270 net/socket.c:727
 __sys_sendto+0x3bd/0x520 net/socket.c:2180
 __do_sys_sendto net/socket.c:2187 [inline]
 __se_sys_sendto net/socket.c:2183 [inline]
 __x64_sys_sendto+0xde/0x100 net/socket.c:2183
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xf6/0x210 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

The buggy address belongs to the object at ffff88805ba1add8
 which belongs to the cache debugfs_inode_cache of size 1176
The buggy address is located 664 bytes inside of
 freed 1176-byte region [ffff88805ba1add8, ffff88805ba1b270)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x5ba18
head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
memcg:ffff888028c4aa01
flags: 0xfff00000000040(head|node=0|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 00fff00000000040 ffff888141aaa640 dead000000000122 0000000000000000
raw: 0000000000000000 00000000000c000c 00000000f5000000 ffff888028c4aa01
head: 00fff00000000040 ffff888141aaa640 dead000000000122 0000000000000000
head: 0000000000000000 00000000000c000c 00000000f5000000 ffff888028c4aa01
head: 00fff00000000002 ffffea00016e8601 00000000ffffffff 00000000ffffffff
head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000004
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 2, migratetype Reclaimable, gfp_mask 0xd20d0(__GFP_RECLAIMABLE|__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 5944, tgid 5944 (syz-executor), ts 117413378932, free_ts 116819010185
 set_page_owner include/linux/page_owner.h:32 [inline]
 post_alloc_hook+0x1d8/0x230 mm/page_alloc.c:1718
 prep_new_page mm/page_alloc.c:1726 [inline]
 get_page_from_freelist+0x21c7/0x22a0 mm/page_alloc.c:3688
 __alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:4970
 alloc_pages_mpol+0x232/0x4a0 mm/mempolicy.c:2301
 alloc_slab_page mm/slub.c:2450 [inline]
 allocate_slab+0x8a/0x3b0 mm/slub.c:2618
 new_slab mm/slub.c:2672 [inline]
 ___slab_alloc+0xbfc/0x1480 mm/slub.c:3858
 __slab_alloc mm/slub.c:3948 [inline]
 __slab_alloc_node mm/slub.c:4023 [inline]
 slab_alloc_node mm/slub.c:4184 [inline]
 kmem_cache_alloc_lru_noprof+0x288/0x3d0 mm/slub.c:4215
 alloc_inode+0x6a/0x1b0 fs/inode.c:346
 new_inode+0x22/0x170 fs/inode.c:1145
 debugfs_get_inode fs/debugfs/inode.c:72 [inline]
 __debugfs_create_file+0x14d/0x4f0 fs/debugfs/inode.c:447
 debugfs_create_file_short+0x3f/0x60 fs/debugfs/inode.c:485
 add_common_files net/mac80211/debugfs_netdev.c:829 [inline]
 add_files net/mac80211/debugfs_netdev.c:947 [inline]
 ieee80211_debugfs_add_netdev net/mac80211/debugfs_netdev.c:1011 [inline]
 ieee80211_debugfs_recreate_netdev+0x43e/0x1460 net/mac80211/debugfs_netdev.c:1035
 ieee80211_if_add+0xc17/0x1390 net/mac80211/iface.c:2202
 ieee80211_register_hw+0x3475/0x4060 net/mac80211/main.c:1604
 mac80211_hwsim_new_radio+0x2f0e/0x5340 drivers/net/wireless/virtual/mac80211_hwsim.c:5559
 hwsim_new_radio_nl+0xea4/0x1b10 drivers/net/wireless/virtual/mac80211_hwsim.c:6243
page last free pid 6035 tgid 6034 stack trace:
 reset_page_owner include/linux/page_owner.h:25 [inline]
 free_pages_prepare mm/page_alloc.c:1262 [inline]
 __free_pages_ok+0x910/0xac0 mm/page_alloc.c:1438
 __folio_put+0x21b/0x2c0 mm/swap.c:112
 folio_put include/linux/mm.h:1580 [inline]
 free_large_kmalloc+0x145/0x200 mm/slub.c:4767
 btree_bounce_free fs/bcachefs/btree_io.c:113 [inline]
 bch2_btree_node_read_done+0x3450/0x5470 fs/bcachefs/btree_io.c:1246
 btree_node_read_work+0x565/0xef0 fs/bcachefs/btree_io.c:1367
 bch2_btree_node_read+0x2151/0x27a0 fs/bcachefs/btree_io.c:-1
 __bch2_btree_root_read fs/bcachefs/btree_io.c:1801 [inline]
 bch2_btree_root_read+0x5e7/0x750 fs/bcachefs/btree_io.c:1823
 read_btree_roots+0x2cb/0x800 fs/bcachefs/recovery.c:582
 bch2_fs_recovery+0x2356/0x37b0 fs/bcachefs/recovery.c:929
 bch2_fs_start+0x70b/0xae0 fs/bcachefs/super.c:1096
 bch2_fs_get_tree+0xad7/0x13b0 fs/bcachefs/fs.c:2505
 vfs_get_tree+0x92/0x2b0 fs/super.c:1759
 do_new_mount+0x24a/0xa40 fs/namespace.c:3881
 do_mount fs/namespace.c:4221 [inline]
 __do_sys_mount fs/namespace.c:4432 [inline]
 __se_sys_mount+0x317/0x410 fs/namespace.c:4409
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xf6/0x210 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
 ffff88805ba1af00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff88805ba1af80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88805ba1b000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                                             ^
 ffff88805ba1b080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff88805ba1b100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================