From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3CDA3DA7E1; Tue, 16 Jun 2026 18:32:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781634758; cv=none; b=DJjGqtnMtp0WRu9bM0mSwElqVzNX2pUrH8q5Ncrxgy78pndXTXFkUpPTJKtNiRJVVImkeU0r+nxAJi5M7Q67wbqUmGE2hOz3SYeJ0X4+PEqWxqMekrtO4ZujHHrHn1JKSNLjZt9PMvfyN4H2g+Wr2gEwhTQfcZthewiNziWkbfs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781634758; c=relaxed/simple; bh=Uzrkh0ozEfXTQqUvOtIuNuhfcWHXtVOrxqD3AJp5mG8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jEbFXJ8VHiXCv7+AzxJY0y0RYiyYFzX/rNWcYYJ9/sCjvGazxxOOMBFRMPTnPxQJaQewIe7OJgnv+Kg9N1PdkkPtNEQagsgGAcXGywWPGQyuFZoA73IPPCavCQsdL5ScD64DN2orMSES4KIJHQBUawQO8NuLdc1cn1dDtJyjEi8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=fQiiajnM; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="fQiiajnM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE3951F00A3A; Tue, 16 Jun 2026 18:32:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1781634757; bh=MGjZw+d/o+5ru1Pt77ERsKxPNWthmbaT4WRyZXpjqiE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fQiiajnMYwDu5iGqZHoSSeF7tl9bpaffgYVeh+jc4fqKFsQ8oqx+N4eEG6sEj2NaQ QvS+wmDaU0Mp+CusfOYdKq8MrpgkYv8eY83N5gGZkZWbUDF3S1ggi7VkyuHzOAzqHZ 3g5ZbLueVwWKJtBcTuCs41mle8ksAZsD/+W8QD2Q= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yochai Eisenrich , David Sterba , Sasha Levin Subject: [PATCH 5.15 324/411] btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak Date: Tue, 16 Jun 2026 20:29:22 +0530 Message-ID: <20260616145118.376023614@linuxfoundation.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260616145100.376842714@linuxfoundation.org> References: <20260616145100.376842714@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yochai Eisenrich [ Upstream commit 973e57c726c1f8e77259d1c8e519519f1e9aea77 ] btrfs_ioctl_space_info() has a TOCTOU race between two passes over the block group RAID type lists. The first pass counts entries to determine the allocation size, then the second pass fills the buffer. The groups_sem rwlock is released between passes, allowing concurrent block group removal to reduce the entry count. When the second pass fills fewer entries than the first pass counted, copy_to_user() copies the full alloc_size bytes including trailing uninitialized kmalloc bytes to userspace. Fix by copying only total_spaces entries (the actually-filled count from the second pass) instead of alloc_size bytes, and switch to kzalloc so any future copy size mismatch cannot leak heap data. Fixes: 7fde62bffb57 ("Btrfs: buffer results in the space_info ioctl") CC: stable@vger.kernel.org # 3.0 Signed-off-by: Yochai Eisenrich Reviewed-by: David Sterba Signed-off-by: David Sterba [ adapted upstream's `return -EFAULT;` to stable's `ret = -EFAULT;` fall-through to existing `out:` cleanup label ] Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ioctl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3612,7 +3612,7 @@ static long btrfs_ioctl_space_info(struc return -ENOMEM; space_args.total_spaces = 0; - dest = kmalloc(alloc_size, GFP_KERNEL); + dest = kzalloc(alloc_size, GFP_KERNEL); if (!dest) return -ENOMEM; dest_orig = dest; @@ -3668,7 +3668,8 @@ static long btrfs_ioctl_space_info(struc user_dest = (struct btrfs_ioctl_space_info __user *) (arg + sizeof(struct btrfs_ioctl_space_args)); - if (copy_to_user(user_dest, dest_orig, alloc_size)) + if (copy_to_user(user_dest, dest_orig, + space_args.total_spaces * sizeof(*dest_orig))) ret = -EFAULT; kfree(dest_orig);