In the Linux kernel, the following vulnerability has been resolved:
btrfs: fix use-after-free when attempting to join an aborted transaction
When we are trying to join the current transaction and if it's aborted,
we read its 'aborted' field after unlocking fs_info->trans_lock and
without holding any extra reference count on it. This means that a
concurrent task that is aborting the transaction may free the transaction
before we read its 'aborted' field, leading to a use-after-free.
Fix this by reading the 'aborted' field while holding fs_info->trans_lock
since any freeing task must first acquire that lock and set
fs_info->running_transaction to NULL before freeing the transaction.
This was reported by syzbot and Dmitry with the following stack traces
from KASAN:
==================================================================BUG: KASAN: slab-use-after-free in join_transaction+0xd9b/0xda0fs/btrfs/transaction.c:278Read of size 4 at addr ffff888011839024 by task kworker/u4:9/1128
CPU: 0 UID: 0 PID: 1128 Comm: kworker/u4:9 Not tainted 6.13.0-rc7-syzkaller-00019-gc45323b7560e #0
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2~bpo12+1 04/01/2014
Workqueue: events_unbound btrfs_async_reclaim_data_space
Call Trace:<TASK>__dump_stacklib/dump_stack.c:94[inline]dump_stack_lvl+0x241/0x360lib/dump_stack.c:120print_address_descriptionmm/kasan/report.c:378[inline]print_report+0x169/0x550mm/kasan/report.c:489kasan_report+0x143/0x180mm/kasan/report.c:602join_transaction+0xd9b/0xda0fs/btrfs/transaction.c:278start_transaction+0xaf8/0x1670fs/btrfs/transaction.c:697flush_space+0x448/0xcf0fs/btrfs/space-info.c:803btrfs_async_reclaim_data_space+0x159/0x510fs/btrfs/space-info.c:1321process_one_workkernel/workqueue.c:3236[inline]process_scheduled_works+0xa66/0x1840kernel/workqueue.c:3317worker_thread+0x870/0xd30kernel/workqueue.c:3398kthread+0x2f0/0x390kernel/kthread.c:389ret_from_fork+0x4b/0x80arch/x86/kernel/process.c:147ret_from_fork_asm+0x1a/0x30arch/x86/entry/entry_64.S:244</TASK>Allocated by task 5315:kasan_save_stackmm/kasan/common.c:47[inline]kasan_save_track+0x3f/0x80mm/kasan/common.c:68poison_kmalloc_redzonemm/kasan/common.c:377[inline]__kasan_kmalloc+0x98/0xb0mm/kasan/common.c:394kasan_kmallocinclude/linux/kasan.h:260[inline]__kmalloc_cache_noprof+0x243/0x390mm/slub.c:4329kmalloc_noprofinclude/linux/slab.h:901[inline]join_transaction+0x144/0xda0fs/btrfs/transaction.c:308start_transaction+0xaf8/0x1670fs/btrfs/transaction.c:697btrfs_create_common+0x1b2/0x2e0fs/btrfs/inode.c:6572lookup_openfs/namei.c:3649[inline]open_last_lookupsfs/namei.c:3748[inline]path_openat+0x1c03/0x3590fs/namei.c:3984do_filp_open+0x27f/0x4e0fs/namei.c:4014do_sys_openat2+0x13e/0x1d0fs/open.c:1402do_sys_openfs/open.c:1417[inline]__do_sys_creatfs/open.c:1495[inline]__se_sys_creatfs/open.c:1489[inline]__x64_sys_creat+0x123/0x170fs/open.c:1489do_syscall_x64arch/x86/entry/common.c:52[inline]do_syscall_64+0xf3/0x230arch/x86/entry/common.c:83entry_SYSCALL_64_after_hwframe+0x77/0x7fFreed by task 5336:kasan_save_stackmm/kasan/common.c:47[inline]kasan_save_track+0x3f/0x80mm/kasan/common.c:68kasan_save_free_info+0x40/0x50mm/kasan/generic.c:582poison_slab_objectmm/kasan/common.c:247[inline]__kasan_slab_free+0x59/0x70mm/kasan/common.c:264kasan_slab_freeinclude/linux/kasan.h:233[inline]slab_free_hookmm/slub.c:2353[inline]slab_freemm/slub.c:4613[inline]kfree+0x196/0x430mm/slub.c:4761cleanup_transactionfs/btrfs/transaction.c:2063[inline]btrfs_commit_transaction+0x2c97/0x3720fs/btrfs/transaction.c:2598insert_balance_item+0x1284/0x20b0fs/btrfs/volumes.c:3757 btrfs_balance+0x992/