public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs: btrfs: fix btrfs_ioctl_space_info() slot_count  TOCTOU which can lead to info-leak
@ 2026-03-22  6:39 Yochai Eisenrich
  2026-03-23 14:26 ` David Sterba
  2026-03-23 22:34 ` [PATCH] fs: btrfs: fix btrfs_ioctl_space_info() slot_count Teng Liu
  0 siblings, 2 replies; 5+ messages in thread
From: Yochai Eisenrich @ 2026-03-22  6:39 UTC (permalink / raw)
  To: Chris Mason
  Cc: Yochai Eisenrich, David Sterba, security, linux-btrfs,
	Yochai Eisenrich

From: Yochai Eisenrich <yochaie@sweet.security>

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")

Signed-off-by: Yochai Eisenrich <echelonh@gmail.com>

---
 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 d75d31b606e4..93c5ea91f401 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2897,7 +2897,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;
@@ -2953,7 +2953,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)))
 		return -EFAULT;

 out:
--
2.53.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-23 22:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-22  6:39 [PATCH] fs: btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak Yochai Eisenrich
2026-03-23 14:26 ` David Sterba
2026-03-23 15:03   ` Yochai E
2026-03-23 18:54     ` David Sterba
2026-03-23 22:34 ` [PATCH] fs: btrfs: fix btrfs_ioctl_space_info() slot_count Teng Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox