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=1796380303; b=yEJ8yU9NyCaRMQnGHihTjJucy/JeWPYNgXVOkR4c5TbPm+OyNHqhSO4nTFrLDYJ+gMCx Kavr7rMo15K2HXvg1cnYFVQrf/xRL/FuQ8w/s96VPeUbJTUluKAUR9x7ecrrNdGv4WgB8 ATclzFUMKob+EBMwG1wwOpYSWxUL9mqkXGU9D0LqBP49VMaYWF1UGD6qbr5C+ZiGnzylO VzxkzOpczRXHH+GfhFC9Nyd+frivptd37uLn86ZlUoYLzE97xbuOEIfGXYfxY3Y17NHv4 9rTaBA6B96ZD+88tjpHD0JdBwwqAsDBysSa/pmInlKPBKBMJAsvZ4S7tZGukwprlr9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380303; 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=8iK4694YDHLAbb7DanN16+kOQQfJ4f8oqGwmZoaVx0E=; b=t36FgHI+rsgHIX7qKzgZxjS31I8N3UsNG0v5nPhp+KMcI1trEMiq0bw0+Pydzo5vNXkU o22kW3etrsoD3GFoNNga7sRFRbQ10OTlY4gVPlhj7ioxuoG9sC1I4vbu67WG/K/qFEaEk IQik1VravCB25fMM1NmPZVhfKdj5BV0nFRg9PbxGG8eAh4So0qWg238OHPTP7rxY18LZd OhrWn79hLtki8YGsOCgf5CNMCAdxmdJLM8jh6V0B8iABJ2lTNv0/mwRLlqvzsPUPN+P0M jZlPZOiyxT3lgD71Bm6NPrZcjD0TvtJk1U6ZG2pqb2FEp+u6bySOQ6tmXRR0BiMdcnQ== 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="A5HjIP/F"; 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 A4D68606A322; Fri, 4 Dec 2026 10:31:43 +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=A5HjIP/F Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A9B21F00A08; Fri, 4 Dec 2026 10:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380303; bh=8iK4694YDHLAbb7DanN16+kOQQfJ4f8oqGwmZoaVx0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=A5HjIP/Fe0C2TT/uBcat46Bn2wqjlzyUsUOb/9SAJL8sz5zuPX6gpG8U1Svu3kjNS vo9GUIBZFF8v96gORGyj+9vI3oTSo0rQOYrqCXUkwwNL0islfQVClu7HhvQgkvkZRB YLxUH107BlxeylXyu1UTeD0uPJZeGpbjYynA/Ktk= 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 7.3 214/742] btrfs: remove devid kobject when deleting a missing device Date: Fri, 4 Dec 2026 19:01:18 +0900 Message-ID: <20261204100640.764276441@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 054f7161cb112..fa39d1b34f145 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2479,11 +2479,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