Skip to content

Commit ba346b3

Browse files
gheiftChris Mason
authored andcommitted
btrfs: tree_search, search_ioctl: direct copy to userspace
By copying each found item seperatly to userspace, we do not need extra buffer in the kernel. Signed-off-by: Gerhard Heift <Gerhard@Heift.Name> Signed-off-by: Chris Mason <clm@fb.com> Acked-by: David Sterba <dsterba@suse.cz>
1 parent 550ac1d commit ba346b3

1 file changed

Lines changed: 33 additions & 15 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ static noinline int copy_to_sk(struct btrfs_root *root,
19581958
struct btrfs_key *key,
19591959
struct btrfs_ioctl_search_key *sk,
19601960
size_t *buf_size,
1961-
char *buf,
1961+
char __user *ubuf,
19621962
unsigned long *sk_offset,
19631963
int *num_found)
19641964
{
@@ -2018,14 +2018,22 @@ static noinline int copy_to_sk(struct btrfs_root *root,
20182018
sh.transid = found_transid;
20192019

20202020
/* copy search result header */
2021-
memcpy(buf + *sk_offset, &sh, sizeof(sh));
2021+
if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2022+
ret = -EFAULT;
2023+
goto out;
2024+
}
2025+
20222026
*sk_offset += sizeof(sh);
20232027

20242028
if (item_len) {
2025-
char *p = buf + *sk_offset;
2029+
char __user *up = ubuf + *sk_offset;
20262030
/* copy the item */
2027-
read_extent_buffer(leaf, p,
2028-
item_off, item_len);
2031+
if (read_extent_buffer_to_user(leaf, up,
2032+
item_off, item_len)) {
2033+
ret = -EFAULT;
2034+
goto out;
2035+
}
2036+
20292037
*sk_offset += item_len;
20302038
}
20312039
(*num_found)++;
@@ -2052,13 +2060,22 @@ static noinline int copy_to_sk(struct btrfs_root *root,
20522060
} else
20532061
ret = 1;
20542062
out:
2063+
/*
2064+
* 0: all items from this leaf copied, continue with next
2065+
* 1: * more items can be copied, but unused buffer is too small
2066+
* * all items were found
2067+
* Either way, it will stops the loop which iterates to the next
2068+
* leaf
2069+
* -EOVERFLOW: item was to large for buffer
2070+
* -EFAULT: could not copy extent buffer back to userspace
2071+
*/
20552072
return ret;
20562073
}
20572074

20582075
static noinline int search_ioctl(struct inode *inode,
20592076
struct btrfs_ioctl_search_key *sk,
20602077
size_t *buf_size,
2061-
char *buf)
2078+
char __user *ubuf)
20622079
{
20632080
struct btrfs_root *root;
20642081
struct btrfs_key key;
@@ -2106,7 +2123,7 @@ static noinline int search_ioctl(struct inode *inode,
21062123
ret = 0;
21072124
goto err;
21082125
}
2109-
ret = copy_to_sk(root, path, &key, sk, buf_size, buf,
2126+
ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
21102127
&sk_offset, &num_found);
21112128
btrfs_release_path(path);
21122129
if (ret)
@@ -2124,22 +2141,24 @@ static noinline int search_ioctl(struct inode *inode,
21242141
static noinline int btrfs_ioctl_tree_search(struct file *file,
21252142
void __user *argp)
21262143
{
2127-
struct btrfs_ioctl_search_args *args;
2144+
struct btrfs_ioctl_search_args __user *uargs;
2145+
struct btrfs_ioctl_search_key sk;
21282146
struct inode *inode;
21292147
int ret;
21302148
size_t buf_size;
21312149

21322150
if (!capable(CAP_SYS_ADMIN))
21332151
return -EPERM;
21342152

2135-
args = memdup_user(argp, sizeof(*args));
2136-
if (IS_ERR(args))
2137-
return PTR_ERR(args);
2153+
uargs = (struct btrfs_ioctl_search_args __user *)argp;
21382154

2139-
buf_size = sizeof(args->buf);
2155+
if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2156+
return -EFAULT;
2157+
2158+
buf_size = sizeof(uargs->buf);
21402159

21412160
inode = file_inode(file);
2142-
ret = search_ioctl(inode, &args->key, &buf_size, args->buf);
2161+
ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
21432162

21442163
/*
21452164
* In the origin implementation an overflow is handled by returning a
@@ -2148,9 +2167,8 @@ static noinline int btrfs_ioctl_tree_search(struct file *file,
21482167
if (ret == -EOVERFLOW)
21492168
ret = 0;
21502169

2151-
if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2170+
if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
21522171
ret = -EFAULT;
2153-
kfree(args);
21542172
return ret;
21552173
}
21562174

0 commit comments

Comments
 (0)