Skip to content

Commit

Permalink
bpf: Fix error usage of map_fd and fdget() in generic_map_update_batch()
Browse files Browse the repository at this point in the history
1. The ufd in generic_map_update_batch() should be read from batch.map_fd;
2. A call to fdget() should be followed by a symmetric call to fdput().

Fixes: aa2e93b ("bpf: Add generic support for update and delete batch ops")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211019032934.1210517-1-xukuohai@huawei.com
  • Loading branch information
Xu Kuohai authored and Alexei Starovoitov committed Oct 21, 2021
1 parent 28fd085 commit 1acfe7c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions kernel/bpf/syscall.c
Expand Up @@ -1337,12 +1337,11 @@ int generic_map_update_batch(struct bpf_map *map,
void __user *values = u64_to_user_ptr(attr->batch.values);
void __user *keys = u64_to_user_ptr(attr->batch.keys);
u32 value_size, cp, max_count;
int ufd = attr->map_fd;
int ufd = attr->batch.map_fd;
void *key, *value;
struct fd f;
int err = 0;

f = fdget(ufd);
if (attr->batch.elem_flags & ~BPF_F_LOCK)
return -EINVAL;

Expand All @@ -1367,6 +1366,7 @@ int generic_map_update_batch(struct bpf_map *map,
return -ENOMEM;
}

f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
for (cp = 0; cp < max_count; cp++) {
err = -EFAULT;
if (copy_from_user(key, keys + cp * map->key_size,
Expand All @@ -1386,6 +1386,7 @@ int generic_map_update_batch(struct bpf_map *map,

kvfree(value);
kvfree(key);
fdput(f);
return err;
}

Expand Down

0 comments on commit 1acfe7c

Please sign in to comment.