Skip to content

Commit fea55b0

Browse files
mrpregregkh
authored andcommitted
bpf: Fix use-after-free in offloaded map/prog info fill
[ Upstream commit a0c584f ] When querying info for an offloaded BPF map or program, bpf_map_offload_info_fill_ns() and bpf_prog_offload_info_fill_ns() obtain the network namespace with get_net(dev_net(offmap->netdev)). However, the associated netdev's netns may be racing with teardown during netns destruction. If the netns refcount has already reached 0, get_net() performs a refcount_t increment on 0, triggering: refcount_t: addition on 0; use-after-free. Although rtnl_lock and bpf_devs_lock ensure the netdev pointer remains valid, they cannot prevent the netns refcount from reaching zero. Fix this by using maybe_get_net() instead of get_net(). maybe_get_net() uses refcount_inc_not_zero() and returns NULL if the refcount is already zero, which causes ns_get_path_cb() to fail and the caller to return -ENOENT -- the correct behavior when the netns is being destroyed. Fixes: 675fc27 ("bpf: offload: report device information for offloaded programs") Fixes: 52775b3 ("bpf: offload: report device information about offloaded maps") Reported-by: Yinhao Hu <dddddd@hust.edu.cn> Reported-by: Kaiyan Mei <M202472210@hust.edu.cn> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn> Closes: https://lore.kernel.org/bpf/f0aa3678-79c9-47ae-9e8c-02a3d1df160a@hust.edu.cn/ Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20260409023733.168050-1-jiayuan.chen@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent cd6b991 commit fea55b0

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

kernel/bpf/offload.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,8 @@ static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data)
274274

275275
if (aux->offload) {
276276
args->info->ifindex = aux->offload->netdev->ifindex;
277-
net = dev_net(aux->offload->netdev);
278-
get_net(net);
279-
ns = &net->ns;
277+
net = maybe_get_net(dev_net(aux->offload->netdev));
278+
ns = net ? &net->ns : NULL;
280279
} else {
281280
args->info->ifindex = 0;
282281
ns = NULL;
@@ -501,9 +500,8 @@ static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data)
501500

502501
if (args->offmap->netdev) {
503502
args->info->ifindex = args->offmap->netdev->ifindex;
504-
net = dev_net(args->offmap->netdev);
505-
get_net(net);
506-
ns = &net->ns;
503+
net = maybe_get_net(dev_net(args->offmap->netdev));
504+
ns = net ? &net->ns : NULL;
507505
} else {
508506
args->info->ifindex = 0;
509507
ns = NULL;

0 commit comments

Comments
 (0)