Skip to content

Commit f4c697e

Browse files
Lukas Czernerkdave
authored andcommitted
btrfs: return EINVAL if start > total_bytes in fitrim ioctl
We should retirn EINVAL if the start is beyond the end of the file system in the btrfs_ioctl_fitrim(). Fix that by adding the appropriate check for it. Also in the btrfs_trim_fs() it is possible that len+start might overflow if big values are passed. Fix it by decrementing the len so that start+len is equal to the file system size in the worst case. Signed-off-by: Lukas Czerner <lczerner@redhat.com>
1 parent 008873e commit f4c697e

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
282282
struct fstrim_range range;
283283
u64 minlen = ULLONG_MAX;
284284
u64 num_devices = 0;
285+
u64 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
285286
int ret;
286287

287288
if (!capable(CAP_SYS_ADMIN))
@@ -300,12 +301,15 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
300301
}
301302
}
302303
rcu_read_unlock();
304+
303305
if (!num_devices)
304306
return -EOPNOTSUPP;
305-
306307
if (copy_from_user(&range, arg, sizeof(range)))
307308
return -EFAULT;
309+
if (range.start > total_bytes)
310+
return -EINVAL;
308311

312+
range.len = min(range.len, total_bytes - range.start);
309313
range.minlen = max(range.minlen, minlen);
310314
ret = btrfs_trim_fs(root, &range);
311315
if (ret < 0)

0 commit comments

Comments
 (0)