public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
@ 2024-10-14 15:56 syzbot
  2024-10-14 17:34 ` syzbot
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: syzbot @ 2024-10-14 15:56 UTC (permalink / raw)
  To: clm, dsterba, josef, linux-btrfs, linux-kernel, syzkaller-bugs

Hello,

syzbot found the following issue on:

HEAD commit:    d61a00525464 Add linux-next specific files for 20241011
git tree:       linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=16f9a887980000
kernel config:  https://syzkaller.appspot.com/x/.config?x=8554528c7f4bf3fb
dashboard link: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
compiler:       Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40
syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=1266c727980000
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=17ce9440580000

Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/f615720e9964/disk-d61a0052.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/c4a45c7583c6/vmlinux-d61a0052.xz
kernel image: https://storage.googleapis.com/syzbot-assets/d767ab86d0d0/bzImage-d61a0052.xz
mounted in repro: https://storage.googleapis.com/syzbot-assets/e1c3551adc6e/mount_0.gz

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com

BTRFS info (device loop0): first mount of filesystem 395ef67a-297e-477c-816d-cd80a5b93e5d
BTRFS info (device loop0): using sha256 (sha256-avx2) checksum algorithm
BTRFS info (device loop0): using free-space-tree
==================================================================
BUG: KASAN: slab-use-after-free in add_delayed_ref+0x12ca/0x1e00 fs/btrfs/delayed-ref.c:1077
Read of size 8 at addr ffff888027c83570 by task syz-executor406/11166

CPU: 1 UID: 0 PID: 11166 Comm: syz-executor406 Not tainted 6.12.0-rc2-next-20241011-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
 print_address_description mm/kasan/report.c:377 [inline]
 print_report+0x169/0x550 mm/kasan/report.c:488
 kasan_report+0x143/0x180 mm/kasan/report.c:601
 add_delayed_ref+0x12ca/0x1e00 fs/btrfs/delayed-ref.c:1077
 btrfs_alloc_tree_block+0xdfb/0x1440 fs/btrfs/extent-tree.c:5209
 btrfs_force_cow_block+0x526/0x1da0 fs/btrfs/ctree.c:573
 btrfs_cow_block+0x35e/0xa40 fs/btrfs/ctree.c:754
 btrfs_search_slot+0xbdd/0x30d0 fs/btrfs/ctree.c:2116
 btrfs_insert_empty_items+0x9c/0x1a0 fs/btrfs/ctree.c:4314
 btrfs_create_new_inode+0xe27/0x1f60 fs/btrfs/inode.c:6344
 btrfs_create_common+0x1d4/0x2e0 fs/btrfs/inode.c:6578
 vfs_mkdir+0x2f9/0x4f0 fs/namei.c:4257
 do_mkdirat+0x264/0x3a0 fs/namei.c:4280
 __do_sys_mkdirat fs/namei.c:4295 [inline]
 __se_sys_mkdirat fs/namei.c:4293 [inline]
 __x64_sys_mkdirat+0x87/0xa0 fs/namei.c:4293
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f5219f05379
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 41 1d 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 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:00007f5219eb3168 EFLAGS: 00000246 ORIG_RAX: 0000000000000102
RAX: ffffffffffffffda RBX: 00007f5219f8b6c8 RCX: 00007f5219f05379
RDX: 0000000000000000 RSI: 0000000020000200 RDI: 0000000000000005
RBP: 00007f5219f8b6c0 R08: 00007f5219eb36c0 R09: 0000000000000000
R10: 00007f5219eb36c0 R11: 0000000000000246 R12: 00007f5219f8b6cc
R13: 0000000000000016 R14: 00007fff9f716d90 R15: 00007fff9f716e78
 </TASK>

Allocated by task 11166:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3f/0x80 mm/kasan/common.c:68
 unpoison_slab_object mm/kasan/common.c:319 [inline]
 __kasan_slab_alloc+0x66/0x80 mm/kasan/common.c:345
 kasan_slab_alloc include/linux/kasan.h:250 [inline]
 slab_post_alloc_hook mm/slub.c:4094 [inline]
 slab_alloc_node mm/slub.c:4143 [inline]
 kmem_cache_alloc_noprof+0x1d9/0x380 mm/slub.c:4150
 add_delayed_ref+0x13a/0x1e00 fs/btrfs/delayed-ref.c:1020
 btrfs_alloc_tree_block+0xdfb/0x1440 fs/btrfs/extent-tree.c:5209
 btrfs_force_cow_block+0x526/0x1da0 fs/btrfs/ctree.c:573
 btrfs_cow_block+0x35e/0xa40 fs/btrfs/ctree.c:754
 btrfs_search_slot+0xbdd/0x30d0 fs/btrfs/ctree.c:2116
 btrfs_insert_empty_items+0x9c/0x1a0 fs/btrfs/ctree.c:4314
 btrfs_create_new_inode+0xe27/0x1f60 fs/btrfs/inode.c:6344
 btrfs_create_common+0x1d4/0x2e0 fs/btrfs/inode.c:6578
 vfs_mkdir+0x2f9/0x4f0 fs/namei.c:4257
 do_mkdirat+0x264/0x3a0 fs/namei.c:4280
 __do_sys_mkdirat fs/namei.c:4295 [inline]
 __se_sys_mkdirat fs/namei.c:4293 [inline]
 __x64_sys_mkdirat+0x87/0xa0 fs/namei.c:4293
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 55:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3f/0x80 mm/kasan/common.c:68
 kasan_save_free_info+0x40/0x50 mm/kasan/generic.c:582
 poison_slab_object mm/kasan/common.c:247 [inline]
 __kasan_slab_free+0x59/0x70 mm/kasan/common.c:264
 kasan_slab_free include/linux/kasan.h:233 [inline]
 slab_free_hook mm/slub.c:2329 [inline]
 slab_free mm/slub.c:4588 [inline]
 kmem_cache_free+0x1a2/0x440 mm/slub.c:4690
 btrfs_put_delayed_ref_head fs/btrfs/delayed-ref.h:358 [inline]
 cleanup_ref_head fs/btrfs/extent-tree.c:1932 [inline]
 __btrfs_run_delayed_refs+0x3d34/0x4680 fs/btrfs/extent-tree.c:2113
 btrfs_run_delayed_refs+0xe3/0x2c0 fs/btrfs/extent-tree.c:2213
 btrfs_commit_transaction+0x4be/0x3740 fs/btrfs/transaction.c:2197
 btrfs_qgroup_rescan_worker+0x17ac/0x1c60 fs/btrfs/qgroup.c:3864
 btrfs_work_helper+0x390/0xc50 fs/btrfs/async-thread.c:314
 process_one_work kernel/workqueue.c:3229 [inline]
 process_scheduled_works+0xa63/0x1850 kernel/workqueue.c:3310
 worker_thread+0x870/0xd30 kernel/workqueue.c:3391
 kthread+0x2f0/0x390 kernel/kthread.c:389
 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244

The buggy address belongs to the object at ffff888027c83570
 which belongs to the cache btrfs_delayed_ref_head of size 328
The buggy address is located 0 bytes inside of
 freed 328-byte region [ffff888027c83570, ffff888027c836b8)

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x27c82
head: order:1 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000000040(head|node=0|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 00fff00000000040 ffff88802fe6a000 ffffea0001ecef00 dead000000000004
raw: 0000000000000000 0000000080140014 00000001f5000000 0000000000000000
head: 00fff00000000040 ffff88802fe6a000 ffffea0001ecef00 dead000000000004
head: 0000000000000000 0000000080140014 00000001f5000000 0000000000000000
head: 00fff00000000001 ffffea00009f2081 ffffffffffffffff 0000000000000000
head: 0000000000000002 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 1, migratetype Unmovable, gfp_mask 0xd2040(__GFP_IO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 5557, tgid 5556 (syz-executor406), ts 235052280151, free_ts 234997412486
 set_page_owner include/linux/page_owner.h:32 [inline]
 post_alloc_hook+0x1f3/0x230 mm/page_alloc.c:1537
 prep_new_page mm/page_alloc.c:1545 [inline]
 get_page_from_freelist+0x3129/0x3270 mm/page_alloc.c:3493
 __alloc_pages_noprof+0x292/0x710 mm/page_alloc.c:4769
 alloc_pages_mpol_noprof+0x3e8/0x680 mm/mempolicy.c:2265
 alloc_slab_page+0x6a/0x120 mm/slub.c:2399
 allocate_slab+0x5a/0x2f0 mm/slub.c:2565
 new_slab mm/slub.c:2618 [inline]
 ___slab_alloc+0xcd1/0x14b0 mm/slub.c:3805
 __slab_alloc+0x58/0xa0 mm/slub.c:3895
 __slab_alloc_node mm/slub.c:3970 [inline]
 slab_alloc_node mm/slub.c:4131 [inline]
 kmem_cache_alloc_noprof+0x268/0x380 mm/slub.c:4150
 add_delayed_ref+0x13a/0x1e00 fs/btrfs/delayed-ref.c:1020
 btrfs_free_tree_block+0x354/0xd80 fs/btrfs/extent-tree.c:3455
 btrfs_force_cow_block+0xf44/0x1da0 fs/btrfs/ctree.c:622
 btrfs_cow_block+0x35e/0xa40 fs/btrfs/ctree.c:754
 btrfs_search_slot+0xbdd/0x30d0 fs/btrfs/ctree.c:2116
 btrfs_insert_empty_items+0x9c/0x1a0 fs/btrfs/ctree.c:4314
 btrfs_create_new_inode+0xe27/0x1f60 fs/btrfs/inode.c:6344
page last free pid 5558 tgid 5558 stack trace:
 reset_page_owner include/linux/page_owner.h:25 [inline]
 free_pages_prepare mm/page_alloc.c:1108 [inline]
 free_unref_page+0xcfb/0xf20 mm/page_alloc.c:2674
 __slab_free+0x31b/0x3d0 mm/slub.c:4499
 qlink_free mm/kasan/quarantine.c:163 [inline]
 qlist_free_all+0x9a/0x140 mm/kasan/quarantine.c:179
 kasan_quarantine_reduce+0x14f/0x170 mm/kasan/quarantine.c:286
 __kasan_slab_alloc+0x23/0x80 mm/kasan/common.c:329
 kasan_slab_alloc include/linux/kasan.h:250 [inline]
 slab_post_alloc_hook mm/slub.c:4094 [inline]
 slab_alloc_node mm/slub.c:4143 [inline]
 kmem_cache_alloc_noprof+0x1d9/0x380 mm/slub.c:4150
 getname_flags+0xb7/0x540 fs/namei.c:139
 do_sys_openat2+0xd2/0x1d0 fs/open.c:1409
 do_sys_open fs/open.c:1430 [inline]
 __do_sys_openat fs/open.c:1446 [inline]
 __se_sys_openat fs/open.c:1441 [inline]
 __x64_sys_openat+0x247/0x2a0 fs/open.c:1441
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
 ffff888027c83400: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff888027c83480: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff888027c83500: fb fb fb fb fb fb fc fc fc fc fc fc fc fc fa fb
                                                             ^
 ffff888027c83580: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff888027c83600: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
  2024-10-14 15:56 [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref syzbot
@ 2024-10-14 17:34 ` syzbot
  2024-10-15  9:55 ` Edward Adam Davis
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: syzbot @ 2024-10-14 17:34 UTC (permalink / raw)
  To: clm, dsterba, fdmanana, josef, linux-btrfs, linux-kernel,
	syzkaller-bugs, wqu

syzbot has bisected this issue to:

commit a3aad8f4f5d9d9a977ee5be32b07bcc083cc8d28
Author: Filipe Manana <fdmanana@suse.com>
Date:   Tue Sep 24 16:12:32 2024 +0000

    btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record

bisection log:  https://syzkaller.appspot.com/x/bisect.txt?x=14596887980000
start commit:   d61a00525464 Add linux-next specific files for 20241011
git tree:       linux-next
final oops:     https://syzkaller.appspot.com/x/report.txt?x=16596887980000
console output: https://syzkaller.appspot.com/x/log.txt?x=12596887980000
kernel config:  https://syzkaller.appspot.com/x/.config?x=8554528c7f4bf3fb
dashboard link: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=1266c727980000
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=17ce9440580000

Reported-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
Fixes: a3aad8f4f5d9 ("btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record")

For information about bisection process see: https://goo.gl/tpsmEJ#bisection

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
  2024-10-14 15:56 [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref syzbot
  2024-10-14 17:34 ` syzbot
@ 2024-10-15  9:55 ` Edward Adam Davis
  2024-10-15 10:15   ` [syzbot] " syzbot
  2024-10-15 10:49 ` Edward Adam Davis
  2024-10-15 12:26 ` [PATCH next] btrfs: Accessing head_ref within delayed_refs lock Edward Adam Davis
  3 siblings, 1 reply; 9+ messages in thread
From: Edward Adam Davis @ 2024-10-15  9:55 UTC (permalink / raw)
  To: syzbot+c3a3a153f0190dca5be9; +Cc: linux-kernel, syzkaller-bugs

using delayed_refs->lock to sync btrfs_work_helper and add_delayed_ref

#syz test

diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 13c2e00d1270..cb57e65c9872 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -1058,7 +1058,6 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 	head_ref = new_head_ref;
 
 	merged = insert_delayed_ref(trans, head_ref, node);
-	spin_unlock(&delayed_refs->lock);
 
 	/*
 	 * Need to update the delayed_refs_rsv with any changes we may have
@@ -1073,8 +1072,12 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 	if (merged)
 		kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
 
-	if (qrecord_inserted)
-		return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+	if (qrecord_inserted) {
+		ret = btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+		spin_unlock(&delayed_refs->lock);
+		return ret;
+	}
+	spin_unlock(&delayed_refs->lock);
 	return 0;
 
 free_record:


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
  2024-10-15  9:55 ` Edward Adam Davis
@ 2024-10-15 10:15   ` syzbot
  0 siblings, 0 replies; 9+ messages in thread
From: syzbot @ 2024-10-15 10:15 UTC (permalink / raw)
  To: eadavis, linux-kernel, syzkaller-bugs

Hello,

syzbot has tested the proposed patch but the reproducer is still triggering an issue:
BUG: sleeping function called from invalid context in ulist_alloc

BTRFS info (device loop0): first mount of filesystem 395ef67a-297e-477c-816d-cd80a5b93e5d
BTRFS info (device loop0): using sha256 (sha256-avx2) checksum algorithm
BTRFS info (device loop0): using free-space-tree
BUG: sleeping function called from invalid context at include/linux/sched/mm.h:321
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 6014, name: syz.0.15
preempt_count: 1, expected: 0
RCU nest depth: 0, expected: 0
8 locks held by syz.0.15/6014:
 #0: ffff888032202420 (sb_writers#13){.+.+}-{0:0}, at: mnt_want_write+0x3f/0x90 fs/namespace.c:515
 #1: ffff888066478d90 (&type->i_mutex_dir_key#8/1){+.+.}-{3:3}, at: inode_lock_nested include/linux/fs.h:851 [inline]
 #1: ffff888066478d90 (&type->i_mutex_dir_key#8/1){+.+.}-{3:3}, at: filename_create+0x260/0x540 fs/namei.c:4026
 #2: ffff888032202610 (sb_internal#2){.+.+}-{0:0}, at: btrfs_create_common+0x1b2/0x2e0 fs/btrfs/inode.c:6572
 #3: ffff88802e9ce458 (btrfs_trans_num_writers){++++}-{0:0}, at: join_transaction+0x15e/0xd70 fs/btrfs/transaction.c:313
 #4: ffff88802e9ce480 (btrfs_trans_num_extwriters){++++}-{0:0}, at: join_transaction+0x15e/0xd70 fs/btrfs/transaction.c:313
 #5: ffff888078a220f8 (btrfs-tree-01){++++}-{3:3}, at: btrfs_tree_lock_nested+0x2f/0x250 fs/btrfs/locking.c:189
 #6: ffff8880302980f8 (btrfs-tree-01/1){+.+.}-{3:3}, at: btrfs_tree_lock_nested+0x2f/0x250 fs/btrfs/locking.c:189
 #7: ffff88802174d388 (&cur_trans->delayed_refs.lock){+.+.}-{2:2}, at: spin_lock include/linux/spinlock.h:351 [inline]
 #7: ffff88802174d388 (&cur_trans->delayed_refs.lock){+.+.}-{2:2}, at: add_delayed_ref+0x6e6/0x1e70 fs/btrfs/delayed-ref.c:1045
Preemption disabled at:
[<0000000000000000>] 0x0
CPU: 0 UID: 0 PID: 6014 Comm: syz.0.15 Not tainted 6.12.0-rc3-next-20241015-syzkaller-04439-gb852e1e7a038-dirty #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
 __might_resched+0x5d4/0x780 kernel/sched/core.c:8653
 might_alloc include/linux/sched/mm.h:321 [inline]
 slab_pre_alloc_hook mm/slub.c:4045 [inline]
 slab_alloc_node mm/slub.c:4123 [inline]
 __kmalloc_cache_noprof+0x61/0x390 mm/slub.c:4299
 kmalloc_noprof include/linux/slab.h:879 [inline]
 ulist_alloc+0x6e/0x140 fs/btrfs/ulist.c:101
 btrfs_find_all_roots_safe+0xf3/0x4c0 fs/btrfs/backref.c:1748
 btrfs_find_all_roots+0x84/0xf0 fs/btrfs/backref.c:1799
 btrfs_qgroup_trace_extent_post+0x1cd/0x2e0 fs/btrfs/qgroup.c:2105
 add_delayed_ref+0x131c/0x1e70 fs/btrfs/delayed-ref.c:1076
 btrfs_alloc_tree_block+0xdfb/0x1440 fs/btrfs/extent-tree.c:5209
 btrfs_force_cow_block+0x526/0x1da0 fs/btrfs/ctree.c:573
 btrfs_cow_block+0x35e/0xa40 fs/btrfs/ctree.c:754
 btrfs_search_slot+0xbdd/0x30d0 fs/btrfs/ctree.c:2116
 btrfs_insert_empty_items+0x9c/0x1a0 fs/btrfs/ctree.c:4314
 btrfs_create_new_inode+0xe27/0x1f60 fs/btrfs/inode.c:6344
 btrfs_create_common+0x1d4/0x2e0 fs/btrfs/inode.c:6578
 vfs_mkdir+0x2f9/0x4f0 fs/namei.c:4257
 do_mkdirat+0x264/0x3a0 fs/namei.c:4280
 __do_sys_mkdirat fs/namei.c:4295 [inline]
 __se_sys_mkdirat fs/namei.c:4293 [inline]
 __x64_sys_mkdirat+0x87/0xa0 fs/namei.c:4293
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f8dcbd7dff9
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 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:00007f8dccb5c038 EFLAGS: 00000246 ORIG_RAX: 0000000000000102
RAX: ffffffffffffffda RBX: 00007f8dcbf35f80 RCX: 00007f8dcbd7dff9
RDX: 0000000000000000 RSI: 0000000020000200 RDI: 0000000000000005
RBP: 00007f8dcbdf0296 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 0000000000000000 R14: 00007f8dcbf35f80 R15: 00007ffe00c6dd48
 </TASK>


Tested on:

commit:         b852e1e7 Add linux-next specific files for 20241015
git tree:       linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=1186d727980000
kernel config:  https://syzkaller.appspot.com/x/.config?x=3cb0191818709a2b
dashboard link: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
compiler:       Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40
patch:          https://syzkaller.appspot.com/x/patch.diff?x=12c2045f980000


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
  2024-10-14 15:56 [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref syzbot
  2024-10-14 17:34 ` syzbot
  2024-10-15  9:55 ` Edward Adam Davis
@ 2024-10-15 10:49 ` Edward Adam Davis
  2024-10-15 11:14   ` [syzbot] " syzbot
  2024-10-15 12:26 ` [PATCH next] btrfs: Accessing head_ref within delayed_refs lock Edward Adam Davis
  3 siblings, 1 reply; 9+ messages in thread
From: Edward Adam Davis @ 2024-10-15 10:49 UTC (permalink / raw)
  To: syzbot+c3a3a153f0190dca5be9; +Cc: linux-kernel, syzkaller-bugs

move head_ref->bytenr to the protection range of delayed_refs->lock

#syz test

diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 13c2e00d1270..f50fc05847a1 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -1012,6 +1012,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 	int action = generic_ref->action;
 	bool merged;
 	int ret;
+	u64 bytenr;
 
 	node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
 	if (!node)
@@ -1056,6 +1057,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		goto free_record;
 	}
 	head_ref = new_head_ref;
+	bytenr = head_ref->bytenr;
 
 	merged = insert_delayed_ref(trans, head_ref, node);
 	spin_unlock(&delayed_refs->lock);
@@ -1074,7 +1076,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
 
 	if (qrecord_inserted)
-		return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+		return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
 	return 0;
 
 free_record:


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref
  2024-10-15 10:49 ` Edward Adam Davis
@ 2024-10-15 11:14   ` syzbot
  0 siblings, 0 replies; 9+ messages in thread
From: syzbot @ 2024-10-15 11:14 UTC (permalink / raw)
  To: eadavis, linux-kernel, syzkaller-bugs

Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
Tested-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com

Tested on:

commit:         b852e1e7 Add linux-next specific files for 20241015
git tree:       linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=143ec030580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=3cb0191818709a2b
dashboard link: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
compiler:       Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40
patch:          https://syzkaller.appspot.com/x/patch.diff?x=16ced727980000

Note: testing is done by a robot and is best-effort only.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH next] btrfs: Accessing head_ref within delayed_refs lock
  2024-10-14 15:56 [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref syzbot
                   ` (2 preceding siblings ...)
  2024-10-15 10:49 ` Edward Adam Davis
@ 2024-10-15 12:26 ` Edward Adam Davis
  2024-10-15 12:49   ` Filipe Manana
  3 siblings, 1 reply; 9+ messages in thread
From: Edward Adam Davis @ 2024-10-15 12:26 UTC (permalink / raw)
  To: syzbot+c3a3a153f0190dca5be9
  Cc: clm, dsterba, josef, linux-btrfs, linux-kernel, syzkaller-bugs

This is because the thread routine btrfs_work_helper released head_def after
exiting delayed_refs->lock in add_delayed_ref.
Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
outside the delayed_refs->lock.

Move head_ref->bytenr into the protection range of delayed_refs->lock 
to avoid uaf in add_delayed_ref.

Fixes: a3aad8f4f5d9 ("btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record")
Reported-and-tested-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 fs/btrfs/delayed-ref.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 13c2e00d1270..f50fc05847a1 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -1012,6 +1012,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 	int action = generic_ref->action;
 	bool merged;
 	int ret;
+	u64 bytenr;
 
 	node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
 	if (!node)
@@ -1056,6 +1057,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		goto free_record;
 	}
 	head_ref = new_head_ref;
+	bytenr = head_ref->bytenr;
 
 	merged = insert_delayed_ref(trans, head_ref, node);
 	spin_unlock(&delayed_refs->lock);
@@ -1074,7 +1076,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
 
 	if (qrecord_inserted)
-		return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+		return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
 	return 0;
 
 free_record:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH next] btrfs: Accessing head_ref within delayed_refs lock
  2024-10-15 12:26 ` [PATCH next] btrfs: Accessing head_ref within delayed_refs lock Edward Adam Davis
@ 2024-10-15 12:49   ` Filipe Manana
  2024-10-15 16:38     ` David Sterba
  0 siblings, 1 reply; 9+ messages in thread
From: Filipe Manana @ 2024-10-15 12:49 UTC (permalink / raw)
  To: Edward Adam Davis
  Cc: syzbot+c3a3a153f0190dca5be9, clm, dsterba, josef, linux-btrfs,
	linux-kernel, syzkaller-bugs

On Tue, Oct 15, 2024 at 1:40 PM Edward Adam Davis <eadavis@qq.com> wrote:
>
> This is because the thread routine btrfs_work_helper released head_def after
> exiting delayed_refs->lock in add_delayed_ref.

This should be explained a lot better. Starting the changelog with
"This is because..." is odd. It should explain why the head reference
was freed (because delayed references were run).

> Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
> outside the delayed_refs->lock.
>
> Move head_ref->bytenr into the protection range of delayed_refs->lock
> to avoid uaf in add_delayed_ref.


This was already fixed yesterday, in a simpler way and with an
explanation of what's going on to trigger the use-after-free:

https://lore.kernel.org/linux-btrfs/02fc507b62b19be2348fc08de8b13bd7af1a440e.1728922973.git.fdmanana@suse.com/

Thanks.


>
> Fixes: a3aad8f4f5d9 ("btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record")
> Reported-and-tested-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
>  fs/btrfs/delayed-ref.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
> index 13c2e00d1270..f50fc05847a1 100644
> --- a/fs/btrfs/delayed-ref.c
> +++ b/fs/btrfs/delayed-ref.c
> @@ -1012,6 +1012,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>         int action = generic_ref->action;
>         bool merged;
>         int ret;
> +       u64 bytenr;
>
>         node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
>         if (!node)
> @@ -1056,6 +1057,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>                 goto free_record;
>         }
>         head_ref = new_head_ref;
> +       bytenr = head_ref->bytenr;
>
>         merged = insert_delayed_ref(trans, head_ref, node);
>         spin_unlock(&delayed_refs->lock);
> @@ -1074,7 +1076,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>                 kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
>
>         if (qrecord_inserted)
> -               return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
> +               return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
>         return 0;
>
>  free_record:
> --
> 2.43.0
>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH next] btrfs: Accessing head_ref within delayed_refs lock
  2024-10-15 12:49   ` Filipe Manana
@ 2024-10-15 16:38     ` David Sterba
  0 siblings, 0 replies; 9+ messages in thread
From: David Sterba @ 2024-10-15 16:38 UTC (permalink / raw)
  To: Filipe Manana
  Cc: Edward Adam Davis, syzbot+c3a3a153f0190dca5be9, clm, dsterba,
	josef, linux-btrfs, linux-kernel, syzkaller-bugs

On Tue, Oct 15, 2024 at 01:49:04PM +0100, Filipe Manana wrote:
> On Tue, Oct 15, 2024 at 1:40 PM Edward Adam Davis <eadavis@qq.com> wrote:
> >
> > This is because the thread routine btrfs_work_helper released head_def after
> > exiting delayed_refs->lock in add_delayed_ref.
> 
> This should be explained a lot better. Starting the changelog with
> "This is because..." is odd. It should explain why the head reference
> was freed (because delayed references were run).
> 
> > Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
> > outside the delayed_refs->lock.
> >
> > Move head_ref->bytenr into the protection range of delayed_refs->lock
> > to avoid uaf in add_delayed_ref.
> 
> 
> This was already fixed yesterday, in a simpler way and with an
> explanation of what's going on to trigger the use-after-free:
> 
> https://lore.kernel.org/linux-btrfs/02fc507b62b19be2348fc08de8b13bd7af1a440e.1728922973.git.fdmanana@suse.com/

This will be in the upcoming linux-next, so we'll not get the syzbot and
build reports anymore.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-10-15 16:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-14 15:56 [syzbot] [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref syzbot
2024-10-14 17:34 ` syzbot
2024-10-15  9:55 ` Edward Adam Davis
2024-10-15 10:15   ` [syzbot] " syzbot
2024-10-15 10:49 ` Edward Adam Davis
2024-10-15 11:14   ` [syzbot] " syzbot
2024-10-15 12:26 ` [PATCH next] btrfs: Accessing head_ref within delayed_refs lock Edward Adam Davis
2024-10-15 12:49   ` Filipe Manana
2024-10-15 16:38     ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox