Skip to content

Commit f5ee467

Browse files
Yochai Eisenrichgregkh
authored andcommitted
btrfs: fix btrfs_ioctl_space_info() slot_count TOCTOU which can lead to info-leak
[ Upstream commit 973e57c ] 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: 7fde62b ("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> [ adapted upstream's `return -EFAULT;` to stable's `ret = -EFAULT;` fall-through to existing `out:` cleanup label ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6b57d6e commit f5ee467

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

fs/btrfs/ioctl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,7 +3087,7 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
30873087
return -ENOMEM;
30883088

30893089
space_args.total_spaces = 0;
3090-
dest = kmalloc(alloc_size, GFP_KERNEL);
3090+
dest = kzalloc(alloc_size, GFP_KERNEL);
30913091
if (!dest)
30923092
return -ENOMEM;
30933093
dest_orig = dest;
@@ -3143,7 +3143,8 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
31433143
user_dest = (struct btrfs_ioctl_space_info __user *)
31443144
(arg + sizeof(struct btrfs_ioctl_space_args));
31453145

3146-
if (copy_to_user(user_dest, dest_orig, alloc_size))
3146+
if (copy_to_user(user_dest, dest_orig,
3147+
space_args.total_spaces * sizeof(*dest_orig)))
31473148
ret = -EFAULT;
31483149

31493150
kfree(dest_orig);

0 commit comments

Comments
 (0)