Root Cause Analysis
The slab-use-after-free occurs in btrfs_destroy_delalloc_inodes() (fs/btrfs/disk-io.c:4790-4821) because it unlinks btrfs_inode from the delalloc list and releases root->delalloc_lock before calling igrab(&btrfs_inode->vfs_inode).
Mechanism
-
Unprotected window in btrfs_destroy_delalloc_inodes() (fs/btrfs/disk-io.c):
When a btrfs filesystem encounters an error (BTRFS_FS_ERROR(fs_info)), transaction_kthread() (fs/btrfs/disk-io.c:1649-1650) invokes btrfs_cleanup_transaction() -> btrfs_destroy_all_delalloc_inodes() -> btrfs_destroy_delalloc_inodes(root). Inside btrfs_destroy_delalloc_inodes():
- Line 4795-4796: Acquires
root->delalloc_lock and splices root->delalloc_inodes onto the local splice list.
- Line 4800-4802: Picks the first entry
btrfs_inode and calls btrfs_del_delalloc_inode(btrfs_inode) (fs/btrfs/inode.c:2548), which removes btrfs_inode->delalloc_inodes from splice.
- Line 4803: Calls
spin_unlock(&root->delalloc_lock).
- Line 4809: Calls
igrab(&btrfs_inode->vfs_inode) without holding root->delalloc_lock, an rcu_read_lock(), or any reference to btrfs_inode.
By contrast, start_delalloc_inodes() (fs/btrfs/inode.c:8897-8902) calls igrab(&inode->vfs_inode) while still holding root->delalloc_lock.
-
Concurrent inode eviction and RCU freeing:
During unmount (cleanup_mnt() -> deactivate_locked_super() -> btrfs_kill_super() -> kill_anon_super() -> generic_shutdown_super() in fs/super.c:695), VFS calls evict_inodes(sb) (fs/super.c:709) before calling sop->put_super(sb) (fs/super.c:723, which runs btrfs_put_super() -> close_ctree() and only stops fs_info->transaction_kthread at fs/btrfs/disk-io.c:4583).
- While
btrfs_inode is still on root->delalloc_inodes (or splice), evict_inode_truncate_pages() (fs/btrfs/inode.c:5459) called from btrfs_evict_inode() (fs/btrfs/inode.c:5586) clears EXTENT_DELALLOC ranges via btrfs_clear_extent_bit() -> btrfs_clear_delalloc_extent() (fs/btrfs/inode.c:2688-2692), which acquires root->delalloc_lock and calls btrfs_del_delalloc_inode(inode) before btrfs_evict_inode() returns. Thus, holding root->delalloc_lock guarantees that evict() has not yet reached destroy_inode() / call_rcu().
- However, once
btrfs_destroy_delalloc_inodes() removes btrfs_inode from splice and drops root->delalloc_lock at line 4803, btrfs_clear_delalloc_extent() sees list_empty(&inode->delalloc_inodes) and does not block or synchronize with btrfs-transacti.
-
Resulting UAF:
Between spin_unlock(&root->delalloc_lock) (or after atomic_add_unless(&inode->i_count, 1, 0) returns 0 when i_count is already 0) and spin_lock(&inode->i_lock) in igrab() (fs/inode.c:1616), concurrent evict_inodes() -> evict() -> btrfs_evict_inode() -> destroy_inode() (fs/inode.c:392) schedules call_rcu(&inode->i_rcu, i_callback). Once the RCU grace period completes and rcu_cpu_kthread frees the btrfs_inode via kmem_cache_free(), btrfs-transacti accesses the freed vfs_inode.i_lock field (offset 1384 inside struct btrfs_inode) in igrab() -> spin_lock(&inode->i_lock).
Fix Direction
In btrfs_destroy_delalloc_inodes() (fs/btrfs/disk-io.c), igrab(&btrfs_inode->vfs_inode) should be called while holding root->delalloc_lock (before spin_unlock(&root->delalloc_lock)), matching the locking pattern in start_delalloc_inodes(). Because evict_inode_truncate_pages() must acquire root->delalloc_lock inside btrfs_clear_delalloc_extent() -> btrfs_del_delalloc_inode() before btrfs_evict_inode() can proceed to destroy_inode() -> call_rcu(), any btrfs_inode still found on splice under root->delalloc_lock is guaranteed not to be freed yet, and igrab() will safely return NULL if I_FREEING / I_WILL_FREE is already set.