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=1796381968; b=KhYfHieL/9Fr7CulQZHgresUB/3xFPDLz+xkagRi/b+UwhKzxnfajD5bFn9ftO8TJfGr p7tLqVO7k0uayoFOotRtqHWOFZTtJ/jF/GCRAroP88oOzAUO06g2piseagGBp1MMKkB5M GCQkKZhGHcrq5PVV+rrWgyt/v+z/gEaGzXN7pFhOXBMKtjH6xJy89PNbbnICQ5XEH1Bij +tqcwdPCO7hGsasTafVtbjjOm0Az7jH0rAcONb95RpajiONGsLigNgjfA8jYNOcElMbnS Rxw/ibsqf3DmTb1qKo8RwSrvWe9YghAZpRCeSl6taU39s+AaEgSXwVTfQ76GTHOuV/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381968; 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=DQqnqK73+3TNmDYvXdS8jfshpfA3qe2FQ5ZoCZWn5zU=; b=gN9dRz8DExCf/Ou+o99pzYd08b+r8qA9oKJ1YLBbZJUYQpzggWh8nicaeI4rKnf9eioC 5VdH8KetnA1LHxTE8Sg6gYqKeI9gdyZC0ushLz6Y+wDqzxIt9td53kMUzRKnls8gx2vUT f6UuholxB2gW6p/ohCLj/ketduiQd8//kokT+dqSBdHeoDPrb1yVSlsZP6yVnNJr97BV9 OmBdInyPT0c2tqN9zAVB+dtvyYxPfYKq6qzuv65xvwP1ammu8tP8HgWp+0kYfBCgG91qA zQTkAbLTPnAULoYai2ovQ6A/KUKZVnsOkCE3g8RZrm4CMyOb3TjCnOqDo44p8uKqrcA== 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=pOXiGUAc; 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 95E36606A322; Fri, 4 Dec 2026 10:59:28 +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=pOXiGUAc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CB7C1F000C2; Fri, 4 Dec 2026 10:59:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381968; bh=DQqnqK73+3TNmDYvXdS8jfshpfA3qe2FQ5ZoCZWn5zU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pOXiGUAcev8axaNxV9r+koX00BVXR24vTUdk4Gzkn8YzaPt5ZRr2qStHj+DYQrmnb rd/AoN7KRcGV+2O/nGUGZek3sz78YBIYzyURWvtDaXgUOk5RFoFF65dTXHWb+2oA/n /YlwQJJppSt7jPVAE+kKQQVKiCeUDVLP6l55V0O4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qu Wenruo , Cheng Yongkang , David Sterba , Sasha Levin Subject: [PATCH 6.18 251/703] btrfs: remove devid kobject when deleting a missing device Date: Fri, 4 Dec 2026 19:02:30 +0900 Message-ID: <20261204100634.474638684@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cheng Yongkang [ Upstream commit 9383a7fdcb27142d97e65fc76f28475018980b35 ] btrfs_rm_device() removes the sysfs device entry only when device->bdev_file is non-NULL. A missing device (created by add_missing_dev() for an absent disk) has bdev_file == NULL, but btrfs_sysfs_add_device() still registers its devid kobject under /sys/fs/btrfs//devinfo/. When such a device is deleted, btrfs_free_device() frees the btrfs_device while the devid kobject remains registered. The kobject is embedded in the freed btrfs_device and its show functions use container_of() to reach it, so reading any devinfo attribute is a use-after-free: BUG: KASAN: slab-use-after-free in sysfs_kf_seq_show+0x157/0x490 Read of size 8 at addr ffff88801d8172a8 by task poc/9385 ... Freed by task 9385: kfree btrfs_rm_device+0x11ee/0x17b0 btrfs_ioctl_rm_dev+0x265/0x400 Allocated by task 9390: btrfs_alloc_device+0x7e/0x400 device_list_add btrfs_scan_one_device btrfs_control_ioctl The freed object is kmalloc-1k and the faulting access is 680 bytes inside it (offsetof(struct btrfs_device, devid_kobj) = 640 plus offsetof(struct kobject, ktype) = 40), confirming the embedded devid_kobj. btrfs_sysfs_remove_device() already skips the block-device link when device->bdev is NULL and only touches the devid kobject when it is initialized. Call it for missing devices too. Fixes: 668e48af7a94 ("btrfs: sysfs, add devid/dev_state kobject and device attributes") Reviewed-by: Qu Wenruo Assisted-by: LLM Signed-off-by: Cheng Yongkang Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/volumes.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index ed0db22f0bda2..de460c151741e 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2397,11 +2397,11 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, btrfs_assign_next_active_device(device, NULL); - if (device->bdev_file) { + if (device->bdev_file) cur_devices->open_devices--; - /* remove sysfs entry */ - btrfs_sysfs_remove_device(device); - } + + /* Remove sysfs entry, including for a missing device. */ + btrfs_sysfs_remove_device(device); num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; btrfs_set_super_num_devices(fs_info->super_copy, num_devices); -- 2.53.0