Skip to content

Commit 5cf6592

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
flow_dissector: Drop BPF flow dissector prog ref on netns cleanup
When attaching a flow dissector program to a network namespace with bpf(BPF_PROG_ATTACH, ...) we grab a reference to bpf_prog. If netns gets destroyed while a flow dissector is still attached, and there are no other references to the prog, we leak the reference and the program remains loaded. Leak can be reproduced by running flow dissector tests from selftests/bpf: # bpftool prog list # ./test_flow_dissector.sh ... selftests: test_flow_dissector [PASS] # bpftool prog list 4: flow_dissector name _dissect tag e314084d332a5338 gpl loaded_at 2020-05-20T18:50:53+0200 uid 0 xlated 552B jited 355B memlock 4096B map_ids 3,4 btf_id 4 # Fix it by detaching the flow dissector program when netns is going away. Fixes: d58e468 ("flow_dissector: implements flow dissector BPF hook") Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Reviewed-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent dfeb376 commit 5cf6592

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

net/core/flow_dissector.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,10 @@ int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
160160
return ret;
161161
}
162162

163-
int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
163+
static int flow_dissector_bpf_prog_detach(struct net *net)
164164
{
165165
struct bpf_prog *attached;
166-
struct net *net;
167166

168-
net = current->nsproxy->net_ns;
169167
mutex_lock(&flow_dissector_mutex);
170168
attached = rcu_dereference_protected(net->flow_dissector_prog,
171169
lockdep_is_held(&flow_dissector_mutex));
@@ -179,6 +177,24 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
179177
return 0;
180178
}
181179

180+
int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
181+
{
182+
return flow_dissector_bpf_prog_detach(current->nsproxy->net_ns);
183+
}
184+
185+
static void __net_exit flow_dissector_pernet_pre_exit(struct net *net)
186+
{
187+
/* We're not racing with attach/detach because there are no
188+
* references to netns left when pre_exit gets called.
189+
*/
190+
if (rcu_access_pointer(net->flow_dissector_prog))
191+
flow_dissector_bpf_prog_detach(net);
192+
}
193+
194+
static struct pernet_operations flow_dissector_pernet_ops __net_initdata = {
195+
.pre_exit = flow_dissector_pernet_pre_exit,
196+
};
197+
182198
/**
183199
* __skb_flow_get_ports - extract the upper layer ports and return them
184200
* @skb: sk_buff to extract the ports from
@@ -1836,7 +1852,7 @@ static int __init init_default_flow_dissectors(void)
18361852
skb_flow_dissector_init(&flow_keys_basic_dissector,
18371853
flow_keys_basic_dissector_keys,
18381854
ARRAY_SIZE(flow_keys_basic_dissector_keys));
1839-
return 0;
1840-
}
18411855

1856+
return register_pernet_subsys(&flow_dissector_pernet_ops);
1857+
}
18421858
core_initcall(init_default_flow_dissectors);

0 commit comments

Comments
 (0)