diff options
author | Alexei Starovoitov <ast@plumgrid.com> | 2015-05-28 22:26:02 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2015-05-31 03:27:51 -0400 |
commit | abf2e7d6e2e315b32ee00067a69aaad2cf4e1b3f (patch) | |
tree | 073ee9134421f2ab9edcd226a48de1a4bea72554 /kernel/bpf | |
parent | d1f5f2bb91094c9e6f7c98610d637ffdaedef116 (diff) |
bpf: add missing rcu protection when releasing programs from prog_array
Normally the program attachment place (like sockets, qdiscs) takes
care of rcu protection and calls bpf_prog_put() after a grace period.
The programs stored inside prog_array may not be attached anywhere,
so prog_array needs to take care of preserving rcu protection.
Otherwise bpf_tail_call() will race with bpf_prog_put().
To solve that introduce bpf_prog_put_rcu() helper function and use
it in 3 places where unattached program can decrement refcnt:
closing program fd, deleting/replacing program in prog_array.
Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")
Reported-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf')
-rw-r--r-- | kernel/bpf/arraymap.c | 4 | ||||
-rw-r--r-- | kernel/bpf/syscall.c | 19 |
2 files changed, 20 insertions, 3 deletions
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 614bcd4c1d74..cb31229a6fa4 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c | |||
@@ -202,7 +202,7 @@ static int prog_array_map_update_elem(struct bpf_map *map, void *key, | |||
202 | 202 | ||
203 | old_prog = xchg(array->prog + index, prog); | 203 | old_prog = xchg(array->prog + index, prog); |
204 | if (old_prog) | 204 | if (old_prog) |
205 | bpf_prog_put(old_prog); | 205 | bpf_prog_put_rcu(old_prog); |
206 | 206 | ||
207 | return 0; | 207 | return 0; |
208 | } | 208 | } |
@@ -218,7 +218,7 @@ static int prog_array_map_delete_elem(struct bpf_map *map, void *key) | |||
218 | 218 | ||
219 | old_prog = xchg(array->prog + index, NULL); | 219 | old_prog = xchg(array->prog + index, NULL); |
220 | if (old_prog) { | 220 | if (old_prog) { |
221 | bpf_prog_put(old_prog); | 221 | bpf_prog_put_rcu(old_prog); |
222 | return 0; | 222 | return 0; |
223 | } else { | 223 | } else { |
224 | return -ENOENT; | 224 | return -ENOENT; |
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 98a69bd83069..a1b14d197a4f 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c | |||
@@ -432,6 +432,23 @@ static void free_used_maps(struct bpf_prog_aux *aux) | |||
432 | kfree(aux->used_maps); | 432 | kfree(aux->used_maps); |
433 | } | 433 | } |
434 | 434 | ||
435 | static void __prog_put_rcu(struct rcu_head *rcu) | ||
436 | { | ||
437 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | ||
438 | |||
439 | free_used_maps(aux); | ||
440 | bpf_prog_free(aux->prog); | ||
441 | } | ||
442 | |||
443 | /* version of bpf_prog_put() that is called after a grace period */ | ||
444 | void bpf_prog_put_rcu(struct bpf_prog *prog) | ||
445 | { | ||
446 | if (atomic_dec_and_test(&prog->aux->refcnt)) { | ||
447 | prog->aux->prog = prog; | ||
448 | call_rcu(&prog->aux->rcu, __prog_put_rcu); | ||
449 | } | ||
450 | } | ||
451 | |||
435 | void bpf_prog_put(struct bpf_prog *prog) | 452 | void bpf_prog_put(struct bpf_prog *prog) |
436 | { | 453 | { |
437 | if (atomic_dec_and_test(&prog->aux->refcnt)) { | 454 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
@@ -445,7 +462,7 @@ static int bpf_prog_release(struct inode *inode, struct file *filp) | |||
445 | { | 462 | { |
446 | struct bpf_prog *prog = filp->private_data; | 463 | struct bpf_prog *prog = filp->private_data; |
447 | 464 | ||
448 | bpf_prog_put(prog); | 465 | bpf_prog_put_rcu(prog); |
449 | return 0; | 466 | return 0; |
450 | } | 467 | } |
451 | 468 | ||