Skip to content

Commit a8e7a4e

Browse files
jrfastabgregkh
authored andcommitted
bpf: sockmap, fix leaking maps with attached but not detached progs
[ Upstream commit 3d9e952 ] 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: 174a79f ("bpf: sockmap with sk redirect support") Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 05c062c commit a8e7a4e

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

kernel/bpf/sockmap.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,6 @@ static void sock_map_free(struct bpf_map *map)
601601
}
602602
rcu_read_unlock();
603603

604-
if (stab->bpf_verdict)
605-
bpf_prog_put(stab->bpf_verdict);
606-
if (stab->bpf_parse)
607-
bpf_prog_put(stab->bpf_parse);
608-
609604
sock_map_remove_complete(stab);
610605
}
611606

@@ -877,13 +872,27 @@ static int sock_map_update_elem(struct bpf_map *map,
877872
return err;
878873
}
879874

875+
static void sock_map_release(struct bpf_map *map, struct file *map_file)
876+
{
877+
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
878+
struct bpf_prog *orig;
879+
880+
orig = xchg(&stab->bpf_parse, NULL);
881+
if (orig)
882+
bpf_prog_put(orig);
883+
orig = xchg(&stab->bpf_verdict, NULL);
884+
if (orig)
885+
bpf_prog_put(orig);
886+
}
887+
880888
const struct bpf_map_ops sock_map_ops = {
881889
.map_alloc = sock_map_alloc,
882890
.map_free = sock_map_free,
883891
.map_lookup_elem = sock_map_lookup,
884892
.map_get_next_key = sock_map_get_next_key,
885893
.map_update_elem = sock_map_update_elem,
886894
.map_delete_elem = sock_map_delete_elem,
895+
.map_release = sock_map_release,
887896
};
888897

889898
BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,

0 commit comments

Comments
 (0)