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

bpf: align and clean bpf_{map,prog}_get helpers

Add a bpf_map_get() function that we're going to use later on and
align/clean the remaining helpers a bit so that we have them a bit
more consistent:

  - __bpf_map_get() and __bpf_prog_get() that both work on the fd
    struct, check whether the descriptor is eBPF and return the
    pointer to the map/prog stored in the private data.

    Also, we can return f.file->private_data directly, the function
    signature is enough of a documentation already.

  - bpf_map_get() and bpf_prog_get() that both work on u32 user fd,
    call their respective __bpf_map_get()/__bpf_prog_get() variants,
    and take a reference.
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent aa79781b
...@@ -167,7 +167,7 @@ struct bpf_prog *bpf_prog_get(u32 ufd); ...@@ -167,7 +167,7 @@ struct bpf_prog *bpf_prog_get(u32 ufd);
void bpf_prog_put(struct bpf_prog *prog); void bpf_prog_put(struct bpf_prog *prog);
void bpf_prog_put_rcu(struct bpf_prog *prog); void bpf_prog_put_rcu(struct bpf_prog *prog);
struct bpf_map *bpf_map_get(struct fd f); struct bpf_map *__bpf_map_get(struct fd f);
void bpf_map_put(struct bpf_map *map); void bpf_map_put(struct bpf_map *map);
extern int sysctl_unprivileged_bpf_disabled; extern int sysctl_unprivileged_bpf_disabled;
......
...@@ -162,19 +162,29 @@ static int map_create(union bpf_attr *attr) ...@@ -162,19 +162,29 @@ static int map_create(union bpf_attr *attr)
/* if error is returned, fd is released. /* if error is returned, fd is released.
* On success caller should complete fd access with matching fdput() * On success caller should complete fd access with matching fdput()
*/ */
struct bpf_map *bpf_map_get(struct fd f) struct bpf_map *__bpf_map_get(struct fd f)
{ {
struct bpf_map *map;
if (!f.file) if (!f.file)
return ERR_PTR(-EBADF); return ERR_PTR(-EBADF);
if (f.file->f_op != &bpf_map_fops) { if (f.file->f_op != &bpf_map_fops) {
fdput(f); fdput(f);
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
map = f.file->private_data; return f.file->private_data;
}
static struct bpf_map *bpf_map_get(u32 ufd)
{
struct fd f = fdget(ufd);
struct bpf_map *map;
map = __bpf_map_get(f);
if (IS_ERR(map))
return map;
atomic_inc(&map->refcnt);
fdput(f);
return map; return map;
} }
...@@ -202,7 +212,7 @@ static int map_lookup_elem(union bpf_attr *attr) ...@@ -202,7 +212,7 @@ static int map_lookup_elem(union bpf_attr *attr)
return -EINVAL; return -EINVAL;
f = fdget(ufd); 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);
...@@ -261,7 +271,7 @@ static int map_update_elem(union bpf_attr *attr) ...@@ -261,7 +271,7 @@ static int map_update_elem(union bpf_attr *attr)
return -EINVAL; return -EINVAL;
f = fdget(ufd); 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);
...@@ -314,7 +324,7 @@ static int map_delete_elem(union bpf_attr *attr) ...@@ -314,7 +324,7 @@ static int map_delete_elem(union bpf_attr *attr)
return -EINVAL; return -EINVAL;
f = fdget(ufd); 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);
...@@ -355,7 +365,7 @@ static int map_get_next_key(union bpf_attr *attr) ...@@ -355,7 +365,7 @@ static int map_get_next_key(union bpf_attr *attr)
return -EINVAL; return -EINVAL;
f = fdget(ufd); 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);
...@@ -549,21 +559,16 @@ static int bpf_prog_new_fd(struct bpf_prog *prog) ...@@ -549,21 +559,16 @@ static int bpf_prog_new_fd(struct bpf_prog *prog)
O_RDWR | O_CLOEXEC); O_RDWR | O_CLOEXEC);
} }
static struct bpf_prog *get_prog(struct fd f) static struct bpf_prog *__bpf_prog_get(struct fd f)
{ {
struct bpf_prog *prog;
if (!f.file) if (!f.file)
return ERR_PTR(-EBADF); return ERR_PTR(-EBADF);
if (f.file->f_op != &bpf_prog_fops) { if (f.file->f_op != &bpf_prog_fops) {
fdput(f); fdput(f);
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
prog = f.file->private_data; return f.file->private_data;
return prog;
} }
/* called by sockets/tracing/seccomp before attaching program to an event /* called by sockets/tracing/seccomp before attaching program to an event
...@@ -574,13 +579,13 @@ struct bpf_prog *bpf_prog_get(u32 ufd) ...@@ -574,13 +579,13 @@ struct bpf_prog *bpf_prog_get(u32 ufd)
struct fd f = fdget(ufd); struct fd f = fdget(ufd);
struct bpf_prog *prog; struct bpf_prog *prog;
prog = get_prog(f); prog = __bpf_prog_get(f);
if (IS_ERR(prog)) if (IS_ERR(prog))
return prog; return prog;
atomic_inc(&prog->aux->refcnt); atomic_inc(&prog->aux->refcnt);
fdput(f); fdput(f);
return prog; return prog;
} }
EXPORT_SYMBOL_GPL(bpf_prog_get); EXPORT_SYMBOL_GPL(bpf_prog_get);
......
...@@ -1989,8 +1989,7 @@ static int replace_map_fd_with_map_ptr(struct verifier_env *env) ...@@ -1989,8 +1989,7 @@ static int replace_map_fd_with_map_ptr(struct verifier_env *env)
} }
f = fdget(insn->imm); f = fdget(insn->imm);
map = __bpf_map_get(f);
map = bpf_map_get(f);
if (IS_ERR(map)) { if (IS_ERR(map)) {
verbose("fd %d is not pointing to valid bpf_map\n", verbose("fd %d is not pointing to valid bpf_map\n",
insn->imm); insn->imm);
......
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