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 8B85747DF8D for ; Fri, 15 May 2026 11:56:12 +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=1778846172; cv=none; b=cARWJGn6MFfv9tln3fbLkzvoRNrjhSvEm0gu59bGkUKwhl+H7yEAO00JfOMF6R0gY1gLll1WOmU2f8PFnoJMzJTOw43KfVXsUHFP2m31sdduL8OZx2Y+HsNc4pefg2dHCqaGEjH3m0PD5g+gPZhog2NeQLSllVEibhgq1O68Cvw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778846172; c=relaxed/simple; bh=qqkg34gUJxItCrN09ctUYw8EN1ZabiIGhuEtuo9OX3c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g8Z9SecCjJDKJIaDLUtWrEdOu7gHoWUSODKEiFGaf1CUO9EcxPiH6Kjvt4+u3N0FG7aNlT/kaSjEDGehexjKr2Iwz7R4yteK4xAdTMawb0Tvs/jUyAnpmNfRR9dZlD4b+9ngjJeth3LoHSsTehdKXEtxMwx0rpOcgBNP8XQAjHM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sXMO7p1l; 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="sXMO7p1l" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91BB0C2BCB0; Fri, 15 May 2026 11:56:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778846172; bh=qqkg34gUJxItCrN09ctUYw8EN1ZabiIGhuEtuo9OX3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sXMO7p1lFjfIZfwyun+OaRoiZ4iiYZctT6KgajnLwLhaopw+ChBnxoE7LnSOvqMZ/ mBt0XBg66fD+vSrOV7MyPKOW8HBTvUbp9P0lbqXdsAFuo5XkhurmcHYhW2BCfGhghj Bab3yeb9eZuLlelhyvckcwVcd3zOme7SUrQ1TZUmMFjVJp0zo0VZjSUmrZujPISJy0 DF+euqHzU89P9OTeP8ilbkvbElAm0t1C68zEG2ST+w9RptppQaY0jTJiGd9fArQuMI YHC+QtXGLCUkyhs5N7zLHm5/2nnITl3dP+0+X/i3D9ybYQi1iAemkk473bntrh51pg +STAjr6djd2HQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Yochai Eisenrich , Yochai Eisenrich , David Sterba , Sasha Levin Subject: [PATCH 6.12.y] btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak Date: Fri, 15 May 2026 07:56:09 -0400 Message-ID: <20260515115609.3040954-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026051217-john-driveway-14af@gregkh> References: <2026051217-john-driveway-14af@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 45852dbf9dfbc..a61022182f45d 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3113,7 +3113,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; @@ -3169,7 +3169,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