]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak
authorYochai Eisenrich <yochaie@sweet.security>
Sun, 22 Mar 2026 06:39:35 +0000 (08:39 +0200)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 Apr 2026 16:56:05 +0000 (18:56 +0200)
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 <echelonh@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c

index a4d715bbed57ba576671e7a5666924b3bceb3bc8..b2e447f5005c167e5fb27251a8016d39b0fb5f72 100644 (file)
@@ -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: