aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Borkmann <daniel@iogearbox.net>2015-10-29 09:58:07 -0400
committerDavid S. Miller <davem@davemloft.net>2015-11-02 22:48:39 -0500
commitc210129760a010b555372ef74f4e1a46d4eb8a22 (patch)
tree89e51ef793586fa8a752674d59aea233f1a68283
parentaa79781b65b9cf79807ade78f2703f5e9402c336 (diff)
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: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/bpf.h2
-rw-r--r--kernel/bpf/syscall.c41
-rw-r--r--kernel/bpf/verifier.c3
3 files changed, 25 insertions, 21 deletions
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 75718fa28260..0b5fb6acef64 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -167,7 +167,7 @@ struct bpf_prog *bpf_prog_get(u32 ufd);
167void bpf_prog_put(struct bpf_prog *prog); 167void bpf_prog_put(struct bpf_prog *prog);
168void bpf_prog_put_rcu(struct bpf_prog *prog); 168void bpf_prog_put_rcu(struct bpf_prog *prog);
169 169
170struct bpf_map *bpf_map_get(struct fd f); 170struct bpf_map *__bpf_map_get(struct fd f);
171void bpf_map_put(struct bpf_map *map); 171void bpf_map_put(struct bpf_map *map);
172 172
173extern int sysctl_unprivileged_bpf_disabled; 173extern int sysctl_unprivileged_bpf_disabled;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 2b89ef0a9757..3fff82ca68fa 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -162,19 +162,29 @@ free_map:
162/* if error is returned, fd is released. 162/* if error is returned, fd is released.
163 * On success caller should complete fd access with matching fdput() 163 * On success caller should complete fd access with matching fdput()
164 */ 164 */
165struct bpf_map *bpf_map_get(struct fd f) 165struct bpf_map *__bpf_map_get(struct fd f)
166{ 166{
167 struct bpf_map *map;
168
169 if (!f.file) 167 if (!f.file)
170 return ERR_PTR(-EBADF); 168 return ERR_PTR(-EBADF);
171
172 if (f.file->f_op != &bpf_map_fops) { 169 if (f.file->f_op != &bpf_map_fops) {
173 fdput(f); 170 fdput(f);
174 return ERR_PTR(-EINVAL); 171 return ERR_PTR(-EINVAL);
175 } 172 }
176 173
177 map = f.file->private_data; 174 return f.file->private_data;
175}
176
177static struct bpf_map *bpf_map_get(u32 ufd)
178{
179 struct fd f = fdget(ufd);
180 struct bpf_map *map;
181
182 map = __bpf_map_get(f);
183 if (IS_ERR(map))
184 return map;
185
186 atomic_inc(&map->refcnt);
187 fdput(f);
178 188
179 return map; 189 return map;
180} 190}
@@ -202,7 +212,7 @@ static int map_lookup_elem(union bpf_attr *attr)
202 return -EINVAL; 212 return -EINVAL;
203 213
204 f = fdget(ufd); 214 f = fdget(ufd);
205 map = bpf_map_get(f); 215 map = __bpf_map_get(f);
206 if (IS_ERR(map)) 216 if (IS_ERR(map))
207 return PTR_ERR(map); 217 return PTR_ERR(map);
208 218
@@ -261,7 +271,7 @@ static int map_update_elem(union bpf_attr *attr)
261 return -EINVAL; 271 return -EINVAL;
262 272
263 f = fdget(ufd); 273 f = fdget(ufd);
264 map = bpf_map_get(f); 274 map = __bpf_map_get(f);
265 if (IS_ERR(map)) 275 if (IS_ERR(map))
266 return PTR_ERR(map); 276 return PTR_ERR(map);
267 277
@@ -314,7 +324,7 @@ static int map_delete_elem(union bpf_attr *attr)
314 return -EINVAL; 324 return -EINVAL;
315 325
316 f = fdget(ufd); 326 f = fdget(ufd);
317 map = bpf_map_get(f); 327 map = __bpf_map_get(f);
318 if (IS_ERR(map)) 328 if (IS_ERR(map))
319 return PTR_ERR(map); 329 return PTR_ERR(map);
320 330
@@ -355,7 +365,7 @@ static int map_get_next_key(union bpf_attr *attr)
355 return -EINVAL; 365 return -EINVAL;
356 366
357 f = fdget(ufd); 367 f = fdget(ufd);
358 map = bpf_map_get(f); 368 map = __bpf_map_get(f);
359 if (IS_ERR(map)) 369 if (IS_ERR(map))
360 return PTR_ERR(map); 370 return PTR_ERR(map);
361 371
@@ -549,21 +559,16 @@ static int bpf_prog_new_fd(struct bpf_prog *prog)
549 O_RDWR | O_CLOEXEC); 559 O_RDWR | O_CLOEXEC);
550} 560}
551 561
552static struct bpf_prog *get_prog(struct fd f) 562static struct bpf_prog *__bpf_prog_get(struct fd f)
553{ 563{
554 struct bpf_prog *prog;
555
556 if (!f.file) 564 if (!f.file)
557 return ERR_PTR(-EBADF); 565 return ERR_PTR(-EBADF);
558
559 if (f.file->f_op != &bpf_prog_fops) { 566 if (f.file->f_op != &bpf_prog_fops) {
560 fdput(f); 567 fdput(f);
561 return ERR_PTR(-EINVAL); 568 return ERR_PTR(-EINVAL);
562 } 569 }
563 570
564 prog = f.file->private_data; 571 return f.file->private_data;
565
566 return prog;
567} 572}
568 573
569/* called by sockets/tracing/seccomp before attaching program to an event 574/* called by sockets/tracing/seccomp before attaching program to an event
@@ -574,13 +579,13 @@ struct bpf_prog *bpf_prog_get(u32 ufd)
574 struct fd f = fdget(ufd); 579 struct fd f = fdget(ufd);
575 struct bpf_prog *prog; 580 struct bpf_prog *prog;
576 581
577 prog = get_prog(f); 582 prog = __bpf_prog_get(f);
578
579 if (IS_ERR(prog)) 583 if (IS_ERR(prog))
580 return prog; 584 return prog;
581 585
582 atomic_inc(&prog->aux->refcnt); 586 atomic_inc(&prog->aux->refcnt);
583 fdput(f); 587 fdput(f);
588
584 return prog; 589 return prog;
585} 590}
586EXPORT_SYMBOL_GPL(bpf_prog_get); 591EXPORT_SYMBOL_GPL(bpf_prog_get);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b56cf51f8d42..fdc88c5a60e3 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1989,8 +1989,7 @@ static int replace_map_fd_with_map_ptr(struct verifier_env *env)
1989 } 1989 }
1990 1990
1991 f = fdget(insn->imm); 1991 f = fdget(insn->imm);
1992 1992 map = __bpf_map_get(f);
1993 map = bpf_map_get(f);
1994 if (IS_ERR(map)) { 1993 if (IS_ERR(map)) {
1995 verbose("fd %d is not pointing to valid bpf_map\n", 1994 verbose("fd %d is not pointing to valid bpf_map\n",
1996 insn->imm); 1995 insn->imm);