From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383504; b=JImNczSmq7Ncr9PCR3SAVs8+AIPGC2MjX5B18gIMNnJpv4wsp6VjFp6P3oJPs9A531K3 srw3UfG90olYgu3KOlq7ncdLxNybcW9vHssM2ZQb/zhi4nqCtrNbkNXQoV7kqJTmQkHq1 wcY3RcqX+Odr6KrWgISJEDBS5cRb6DMoyqBbaXoF/EIiLwg7n5AHmKjb9ZDJ+OmujwNgh OoYU3s196Rsrs9ZpFFQF4yd3z/eXJ2wZcuC6e9LRw7QDjsU7/pCqhiu2PK8hQ/yW4ijYl aZN6UKO9MglKHhPca7KAxLgo2LKx+iVAuaTXPzMlsTaD1b7s9FUVSKmeXuZ49GXTwDw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383504; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=+Os+6VLH+OS3U214ZsoXlJ9zU/0e6bI6JjTIFUVJRbw=; b=v6x2ZO3zUe79ZlJ8NH0hD0/tsOdOjWJM3QzJIOkkXNASipJ0yqUCkqia3Mt/qTAqIPnX ru95yG5+6MPV+775sTAsAVDYg1fg6jeCHcNZrsTbC1Ivb/y/DjmT/LLRvdYklzbdPI49B WK78tEzz1lTz/wwO+KYLlFHiCg0QjOCFBcMhbOcDzysY/+K2XdBeDNPjbolV5YTHlAJaa S2/bxOEPImIvntobptUtMaQqGI0rxeiPUMDAyW4h98x9VU1LazMm31QdzJFbjYHPl7Ndd P5MSUSgQOcZABhp10HUs3ZRdw9ysg1cj3rPpYf4lUaTMKI2v8ztvSx7Ua41cr4cRzjQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=PbHaM8nI; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3372606A322; Fri, 4 Dec 2026 11:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=PbHaM8nI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AC1C1F000C2; Fri, 4 Dec 2026 11:25:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383503; bh=+Os+6VLH+OS3U214ZsoXlJ9zU/0e6bI6JjTIFUVJRbw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=PbHaM8nIp54R7bp7ZpM2a/nS7ZTPqICBKN5+MUiF2FkJvbCgipfXNr0cHVXDSxXJI eJBd5K0ehOWXEOhMxWCPzOmeXV982OO2fy8Q0HsKRisLvEzVmmrkLu+HezcMC52t7j fCMgTfX48ZrPe4QYRdbb/VwX8TLwLUnLCysLfdJM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Daniel Vacek , Binbin Deng <18983559317@163.com>, David Sterba , Sasha Levin Subject: [PATCH 6.12 250/555] btrfs: wait for a grace period before freeing a new device Date: Fri, 4 Dec 2026 19:04:53 +0900 Message-ID: <20261204100618.681202493@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Binbin Deng <18983559317@163.com> [ Upstream commit 38c94b99f8743a5b9aab864a12ecf9a06d36aeb4 ] KASAN reports a slab-use-after-free in btrfs_statfs() walking fs_devices->devices. btrfs_init_new_device() publishes the new device with list_add_rcu() and can fail afterwards, for example with -ENOSPC in init_first_rw_device() when a user with CAP_SYS_ADMIN adds a device that is too small to a seeding filesystem mounted read-only. The error path removes the entry with list_del_rcu() and frees the object in btrfs_free_device() without waiting for a grace period, while btrfs_calc_avail_data_space() and btrfs_ioctl_fs_info() keep reading the device list under rcu_read_lock(). BUG: KASAN: slab-use-after-free in btrfs_statfs+0x11af/0x14a0 Read of size 8 at addr ffff8880096be858 by task poc/252 Call Trace: dump_stack_lvl+0x53/0x70 print_report+0xd0/0x630 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? filename_lookup+0x1a9/0x540 ? btrfs_statfs+0x11af/0x14a0 kasan_report+0xce/0x100 ? btrfs_statfs+0x11af/0x14a0 btrfs_statfs+0x11af/0x14a0 statfs_by_dentry+0x117/0x1e0 user_statfs+0xac/0x130 ? __pfx_user_statfs+0x10/0x10 __do_sys_statfs+0x80/0xe0 ? __pfx___do_sys_statfs+0x10/0x10 do_syscall_64+0xf9/0x540 entry_SYSCALL_64_after_hwframe+0x77/0x7f Allocated by task 256: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 __kasan_kmalloc+0x8f/0xa0 __kmalloc_cache_noprof+0x158/0x370 btrfs_alloc_device+0xad/0x3e0 btrfs_init_new_device+0x3e8/0x36e0 btrfs_ioctl+0x150d/0x6c20 __x64_sys_ioctl+0x134/0x1c0 do_syscall_64+0xf9/0x540 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 256: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 kasan_save_free_info+0x3b/0x60 __kasan_slab_free+0x43/0x70 kfree+0x121/0x380 btrfs_init_new_device+0x4a0/0x36e0 btrfs_ioctl+0x150d/0x6c20 __x64_sys_ioctl+0x134/0x1c0 do_syscall_64+0xf9/0x540 entry_SYSCALL_64_after_hwframe+0x77/0x7f Fix by waiting for an RCU grace period after the device is removed from the list and before it is freed.Place the synchronize_rcu() after dropping the mutexes and before the `error_trans` label. Fixes: 1f78160ce1b1 ("Btrfs: using rcu lock in the reader side of devices list") Reviewed-by: Daniel Vacek Signed-off-by: Binbin Deng <18983559317@163.com> Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 0652c630743e8..0587de27a277f 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -3066,6 +3066,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path btrfs_rollback_sprout(fs_info, seed_devices); mutex_unlock(&fs_info->chunk_mutex); mutex_unlock(&fs_info->fs_devices->device_list_mutex); + synchronize_rcu(); error_trans: if (trans) btrfs_end_transaction(trans); -- 2.53.0