Kernel bug: KASAN: use-after-free Read in bch2_extent_ptr_to_text
Subsystem: bcachefs
Status: open

Crash report:
  allowing incompatible features above 0.0: (unknown version)
  features: lz4,new_siphash,inline_data,new_extent_overwrite,btree_ptr_v2,new_varint,journal_no_flush,alloc_v2,extents_across_btree_nodes
bcachefs (loop0): Using encoding defined by superblock: utf8-12.1.0
bcachefs (loop0): invalid bkey in superblock btree=extents level=1: u64s 11 type btree_ptr_v2 SPOS_MAX len 0 ver 0: seq c6c25c03258c59c5 written 16 min_key POS_MIN durability: 1 ptr: 0:27:0 gen 0
  pointer before first bucket (27 < 1024), deleting
==================================================================
BUG: KASAN: use-after-free in bucket_gen_get_rcu fs/bcachefs/buckets.h:82 [inline]
BUG: KASAN: use-after-free in dev_ptr_stale_rcu fs/bcachefs/buckets.h:147 [inline]
BUG: KASAN: use-after-free in bch2_extent_ptr_to_text+0x883/0x890 fs/bcachefs/extents.c:1247
Read of size 1 at addr ffff8880992caf4e by task syz.0.17/6104

CPU: 0 UID: 0 PID: 6104 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT_{RT,(full)} 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/18/2025
Call Trace:
 <TASK>
 dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120
 print_address_description mm/kasan/report.c:378 [inline]
 print_report+0xca/0x240 mm/kasan/report.c:482
 kasan_report+0x118/0x150 mm/kasan/report.c:595
 bucket_gen_get_rcu fs/bcachefs/buckets.h:82 [inline]
 dev_ptr_stale_rcu fs/bcachefs/buckets.h:147 [inline]
 bch2_extent_ptr_to_text+0x883/0x890 fs/bcachefs/extents.c:1247
 bch2_bkey_ptrs_to_text+0xd75/0x1310 fs/bcachefs/extents.c:1335
 __bch2_bkey_fsck_err+0x3f7/0x540 fs/bcachefs/error.c:691
 extent_ptr_validate fs/bcachefs/extents.c:-1 [inline]
 bch2_bkey_ptrs_validate+0x1d56/0x24c0 fs/bcachefs/extents.c:1442
 bch2_btree_ptr_v2_validate+0x406/0x8d0 fs/bcachefs/extents.c:345
 bch2_bkey_val_validate fs/bcachefs/bkey_methods.c:143 [inline]
 bch2_bkey_validate+0x291/0x4e0 fs/bcachefs/bkey_methods.c:251
 journal_validate_key+0x631/0xe50 fs/bcachefs/journal_io.c:388
 journal_entry_btree_root_validate+0x22a/0x620 fs/bcachefs/journal_io.c:480
 bch2_journal_entry_validate+0x15b/0x220 fs/bcachefs/journal_io.c:874
 bch2_sb_clean_validate_late+0x16c/0x330 fs/bcachefs/sb-clean.c:44
 bch2_read_superblock_clean+0xd9/0x260 fs/bcachefs/sb-clean.c:172
 bch2_fs_recovery+0x158/0x3a50 fs/bcachefs/recovery.c:738
 bch2_fs_start+0xaaf/0xda0 fs/bcachefs/super.c:1213
 bch2_fs_get_tree+0xb39/0x1520 fs/bcachefs/fs.c:2488
 vfs_get_tree+0x8f/0x2b0 fs/super.c:1815
 do_new_mount+0x2a2/0x9e0 fs/namespace.c:3808
 do_mount fs/namespace.c:4136 [inline]
 __do_sys_mount fs/namespace.c:4347 [inline]
 __se_sys_mount+0x317/0x410 fs/namespace.c:4324
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fa145eb066a
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:00007fa145515e68 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007fa145515ef0 RCX: 00007fa145eb066a
RDX: 0000200000005b40 RSI: 0000200000000000 RDI: 00007fa145515eb0
RBP: 0000200000005b40 R08: 00007fa145515ef0 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000200000000000
R13: 00007fa145515eb0 R14: 000000000000594b R15: 0000200000000080
 </TASK>

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x992ca
flags: 0x80000000000000(node=0|zone=1)
raw: 0080000000000000 ffffea000264b288 ffffea000264b288 0000000000000000
raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner info is not present (never set?)

Memory state around the buggy address:
 ffff8880992cae00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
 ffff8880992cae80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
>ffff8880992caf00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
                                              ^
 ffff8880992caf80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
 ffff8880992cb000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================