From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E581F3A2E3F for ; Fri, 15 May 2026 11:45:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778845540; cv=none; b=NEU8OtPP5BgI/mrfP8VzCCTypBrHT8zq04Vs1IPlMv6UsmZxhWLyfKQGYPQ7V7iJwyUrZylk8xVFICWBIg/7FELfOjDBnqCWPTAM6cJwOsHT30veh2AsdLspJYO6VPZ2+IVg7QXVWLVWio6MxTValhnYw86gcOV9lp+tDJNE9dE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778845540; c=relaxed/simple; bh=TXHhMdljYwflLeH+A//k0ZpAt4bEFLEFxQKRuW/SxDQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LzxcKkx5i27c962mRwxCThuy9+Lm5Ry9rQUpzVVaBeCAzSH1MJXhpv2wp6avWRWXqRHwVT09shaRQDpDJcR8/DprX5Bic+eYLshsWmtYyXf+e8+IBuZan9emxRXI53LPzNWQx1e1bvQIcu7KfRqoXmU0BdJ5RtX90S3rM63Vxu0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LjY1jjk/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LjY1jjk/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 334EEC2BCB0; Fri, 15 May 2026 11:45:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778845539; bh=TXHhMdljYwflLeH+A//k0ZpAt4bEFLEFxQKRuW/SxDQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LjY1jjk/DUbPpeZDkmgmrYXW30sIuhnSqLhWmbm5jQ9osWURI7HUEYPvGKIsCZteK JQFbklU9Yp+/U76tjTGTs/OujMbhvSpSNKbEsv/BXY5R8qFB9QDAwmGehaOSQQfRcs 2zuRZT5vtE1UHnk9YLqmQuwSANOe4PnUgdaRhSallKm1E8SvccpC9/HtUiExMh5CqK fAcWzftvkOvn4jhJ3u/0/StZrYGpDZbTHfITJSp0LfM4KiJWF+9I98lwcg7VT/C7Ud L6aipCsLs3YzTUCxQINFSyJAHG9qyY6Mer+CSy/aUdCBfzZh4HIzUr8pD7W8anOQST cRLhLKFEHe1Sg== From: Sasha Levin To: stable@vger.kernel.org Cc: Yochai Eisenrich , Yochai Eisenrich , David Sterba , Sasha Levin Subject: [PATCH 6.18.y] btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak Date: Fri, 15 May 2026 07:45:37 -0400 Message-ID: <20260515114537.3024682-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026051217-juvenile-glucose-6e1a@gregkh> References: <2026051217-juvenile-glucose-6e1a@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- fs/btrfs/ioctl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index bfe253c2849a5..c0691e93e0a58 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3025,7 +3025,7 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 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; @@ -3081,7 +3081,8 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 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); -- 2.53.0