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 EF1E53D5661 for ; Fri, 15 May 2026 12:06:22 +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=1778846783; cv=none; b=aQ31rIkJ8288nJ69BfLdIJtj2AMD+CDwOlfCtzL9U/RCW/yz0dZiLlv1mi5p+2eF+MMF65nY8cxVPvO4jjLMQMLo0WMDx0oqUbR9tkuU0PFma/TSa3DgZbumnDMMfM7Gaxj2VxWyTOFxKBnrr81WnY4H84Zf/dbwe/EsnrZMsJ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778846783; c=relaxed/simple; bh=3K6wUcbLgVBNAeUNAP7O4EXg/fMLFso+LqrfvFEoVZ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TDl23T9wChFaS+7ioW70G/2h2JQs39nlMtxg9ywcn/kHVdUalGejyGGaYtI1uxJEOB1d63e5k+s6kmFtoE2PcOPRMhqvV97uDqt5BSt5QFUzP9thOak94VoHR/tsDOQ1NT2Y1eSaxuPYO7aTlJ1wnZ+HXeErTC+EM8N+2cvWZso= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YbfKnCcl; 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="YbfKnCcl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A3B2C2BCB0; Fri, 15 May 2026 12:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778846782; bh=3K6wUcbLgVBNAeUNAP7O4EXg/fMLFso+LqrfvFEoVZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YbfKnCclPkvohL3n0c5XqmMTxeUSyvFiDohI//gK/cmeflf2rutbcmxeh576UUCgX GF9x1AUqWa3gihjK0jaz5zPVPyLIZv9GSzgCwyX3S6SDoa4TZYJGcTMtXYmQ5xQzoY bL5y6lr4Vhb+CZn6WWi3OuUDbsMmiMP0B+Fzm7dxj/pwPfT0DCC+vj9f59NZzELfbc FhrPzXKvbdnnRXqB29/8rxnYVn9o12SS28ClGiccdHTLpsnPWww6He8OW5g7HAzyMW YuXW+lWabU8qq2fY41CmotHN2CU0FQ1AG3TjHSdrtHlomawaI0wxzFwv8fVGqlFTs3 33WVu6B0EQb0w== From: Sasha Levin To: stable@vger.kernel.org Cc: Yochai Eisenrich , Yochai Eisenrich , David Sterba , Sasha Levin Subject: [PATCH 6.6.y] btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak Date: Fri, 15 May 2026 08:06:20 -0400 Message-ID: <20260515120620.3073457-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026051218-budding-fidelity-6094@gregkh> References: <2026051218-budding-fidelity-6094@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 4723013995f5b..d17d1eff8eff4 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3087,7 +3087,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; @@ -3143,7 +3143,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