]> git.baikalelectronics.ru Git - kernel.git/commit
bpf: sockmap, fix leaking maps with attached but not detached progs
authorJohn Fastabend <john.fastabend@gmail.com>
Mon, 5 Feb 2018 18:17:54 +0000 (10:17 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 6 Feb 2018 10:39:32 +0000 (11:39 +0100)
commit12ea30b0113c0e2f315dffa5c30cdee02867b86f
tree07ee2d1dc8963140d2e718a8600112892c8add3f
parent91ac8c26a2a7c170ef1d9eb3f251e8a74b3d5040
bpf: sockmap, fix leaking maps with attached but not detached progs

When a program is attached to a map we increment the program refcnt
to ensure that the program is not removed while it is potentially
being referenced from sockmap side. However, if this same program
also references the map (this is a reasonably common pattern in
my programs) then the verifier will also increment the maps refcnt
from the verifier. This is to ensure the map doesn't get garbage
collected while the program has a reference to it.

So we are left in a state where the map holds the refcnt on the
program stopping it from being removed and releasing the map refcnt.
And vice versa the program holds a refcnt on the map stopping it
from releasing the refcnt on the prog.

All this is fine as long as users detach the program while the
map fd is still around. But, if the user omits this detach command
we are left with a dangling map we can no longer release.

To resolve this when the map fd is released decrement the program
references and remove any reference from the map to the program.
This fixes the issue with possibly dangling map and creates a
user side API constraint. That is, the map fd must be held open
for programs to be attached to a map.

Fixes: 26ac83c2c880 ("bpf: sockmap with sk redirect support")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
kernel/bpf/sockmap.c