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=1796383511; b=IavpTL14eMzASO4I4nnmABK40mI3qxwjoD6qzJENGjGLuUxZHK8ixWWRj31g6HTguFof orVjD4Fpxd5bNXMKmk5adXMPdKXMLMdplps750qs4qJyFni0Wlol06flhScUXvWOEdrLH QTGEVr/XlnzPAiWznSGZS/dZUrCLZzMIaANh53mBjUSckiS25h8p6jV92CgG38npYV5iz HYdbCwps1Dh5Cv01WBu8YmecKGGPjViGXxxx3IXdl4e/kw+IwkloPx25iB+l1qi3td/2f rWPzNrg+LASr5Pwogg6qkBvlg6Cx7+mJSUQHSAfkQPoPn36MUvqmh8LFUmE5e3RP+LQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383511; 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=RsxqGLKTHQuW4CI3IKgGY8QwBPTDuRi/dm8SymmE2iA=; b=Q71PxpkFLhXlM5YYqvlbHIbHOVKTKITuCLWvFFK0QtY0MFcEDvpUlwok6O75Op5Qd1G4 iR8Gs/QxBoyG2OQi6xnPDwDjw2f2XC2VYyrL0opdeVkBZz/FsTCelEBv4hhzb3k3Hh8G0 X3jnex0DLy3pqifr1bquM+UdTen17p5D821I7BFGCbKy09vACV0PizJQSPz4Gb9AbIxNs dQJTmd2KdHBqHoaWB4Xo13X1MHEAdz6kElgq+q7huA1xAWQNFjQ3Av0CGJmUlGRkKfmAu MzP5O1bFy21a7N8FoM0L6P8UmDODnp5sgD9D2P1nJRo19Sfp8xwE+wEDMBsncotE2iw== 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="QlcANj/d"; 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 1695A606A32C; Fri, 4 Dec 2026 11:25:10 +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=QlcANj/d Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EEE61F000C2; Fri, 4 Dec 2026 11:25:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383510; bh=RsxqGLKTHQuW4CI3IKgGY8QwBPTDuRi/dm8SymmE2iA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QlcANj/daBcMEI389hM1CzCFY4Yfm/sg/j4Saffa3YDtpGuDCBvLRyNTzEvyq6vJD 9qg+FO9gJesAtMu+CL4Jgc7raqbEDZ+v5F2D3au+MtC0aDAl6UFU8psq0mXUb5Dd+O 44ajfe7Yt1RXb2d1/MkUi9KWDGoLSNbTatZgjAdM= 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.12 253/555] btrfs: remove devid kobject when deleting a missing device Date: Fri, 4 Dec 2026 19:04:56 +0900 Message-ID: <20261204100618.782873180@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: 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.12-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 0587de27a277f..8b210ba3eb642 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2383,11 +2383,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