Commit 592867bf authored by Daniel Borkmann's avatar Daniel Borkmann Committed by David S. Miller

ebpf: fix fd refcount leaks related to maps in bpf syscall

We may already have gotten a proper fd struct through fdget(), so
whenever we return at the end of an map operation, we need to call
fdput(). However, each map operation from syscall side first probes
CHECK_ATTR() to verify that unused fields in the bpf_attr union are
zero.

In case of malformed input, we return with error, but the lookup to
the map_fd was already performed at that time, so that we return
without an corresponding fdput(). Fix it by performing an fdget()
only right before bpf_map_get(). The fdget() invocation on maps in
the verifier is not affected.

Fixes: db20fd2b ("bpf: add lookup/update/delete/iterate methods to BPF maps")
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@plumgrid.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 74e98eb0
...@@ -155,14 +155,15 @@ static int map_lookup_elem(union bpf_attr *attr) ...@@ -155,14 +155,15 @@ static int map_lookup_elem(union bpf_attr *attr)
void __user *ukey = u64_to_ptr(attr->key); void __user *ukey = u64_to_ptr(attr->key);
void __user *uvalue = u64_to_ptr(attr->value); void __user *uvalue = u64_to_ptr(attr->value);
int ufd = attr->map_fd; int ufd = attr->map_fd;
struct fd f = fdget(ufd);
struct bpf_map *map; struct bpf_map *map;
void *key, *value, *ptr; void *key, *value, *ptr;
struct fd f;
int err; int err;
if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
return -EINVAL; return -EINVAL;
f = fdget(ufd);
map = bpf_map_get(f); map = bpf_map_get(f);
if (IS_ERR(map)) if (IS_ERR(map))
return PTR_ERR(map); return PTR_ERR(map);
...@@ -213,14 +214,15 @@ static int map_update_elem(union bpf_attr *attr) ...@@ -213,14 +214,15 @@ static int map_update_elem(union bpf_attr *attr)
void __user *ukey = u64_to_ptr(attr->key); void __user *ukey = u64_to_ptr(attr->key);
void __user *uvalue = u64_to_ptr(attr->value); void __user *uvalue = u64_to_ptr(attr->value);
int ufd = attr->map_fd; int ufd = attr->map_fd;
struct fd f = fdget(ufd);
struct bpf_map *map; struct bpf_map *map;
void *key, *value; void *key, *value;
struct fd f;
int err; int err;
if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
return -EINVAL; return -EINVAL;
f = fdget(ufd);
map = bpf_map_get(f); map = bpf_map_get(f);
if (IS_ERR(map)) if (IS_ERR(map))
return PTR_ERR(map); return PTR_ERR(map);
...@@ -265,14 +267,15 @@ static int map_delete_elem(union bpf_attr *attr) ...@@ -265,14 +267,15 @@ static int map_delete_elem(union bpf_attr *attr)
{ {
void __user *ukey = u64_to_ptr(attr->key); void __user *ukey = u64_to_ptr(attr->key);
int ufd = attr->map_fd; int ufd = attr->map_fd;
struct fd f = fdget(ufd);
struct bpf_map *map; struct bpf_map *map;
struct fd f;
void *key; void *key;
int err; int err;
if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
return -EINVAL; return -EINVAL;
f = fdget(ufd);
map = bpf_map_get(f); map = bpf_map_get(f);
if (IS_ERR(map)) if (IS_ERR(map))
return PTR_ERR(map); return PTR_ERR(map);
...@@ -305,14 +308,15 @@ static int map_get_next_key(union bpf_attr *attr) ...@@ -305,14 +308,15 @@ static int map_get_next_key(union bpf_attr *attr)
void __user *ukey = u64_to_ptr(attr->key); void __user *ukey = u64_to_ptr(attr->key);
void __user *unext_key = u64_to_ptr(attr->next_key); void __user *unext_key = u64_to_ptr(attr->next_key);
int ufd = attr->map_fd; int ufd = attr->map_fd;
struct fd f = fdget(ufd);
struct bpf_map *map; struct bpf_map *map;
void *key, *next_key; void *key, *next_key;
struct fd f;
int err; int err;
if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
return -EINVAL; return -EINVAL;
f = fdget(ufd);
map = bpf_map_get(f); map = bpf_map_get(f);
if (IS_ERR(map)) if (IS_ERR(map))
return PTR_ERR(map); return PTR_ERR(map);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment