History

bpf: Fix use-after-free in offloaded map/prog info fill

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.

Jiayuan Chen committed 7 months ago

Commit a0c584f1 parent 8697bdd1 file · +4 −6Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/offload.c+4−6
@@ -435,9 +435,8 @@ static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data) · 434 unmodified lines
435 435
436 if (aux->offload) {436 if (aux->offload) {
437 args->info->ifindex = aux->offload->netdev->ifindex;437 args->info->ifindex = aux->offload->netdev->ifindex;
438 net = dev_net(aux->offload->netdev);438 net = maybe_get_net(dev_net(aux->offload->netdev));
439 get_net(net);439 ns = net ? &net->ns : NULL;
440 ns = &net->ns;
441 } else {440 } else {
442 args->info->ifindex = 0;441 args->info->ifindex = 0;
443 ns = NULL;442 ns = NULL;
@@ -647,9 +646,8 @@ static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data) · 203 unmodified lines
647 646
648 if (args->offmap->netdev) {647 if (args->offmap->netdev) {
649 args->info->ifindex = args->offmap->netdev->ifindex;648 args->info->ifindex = args->offmap->netdev->ifindex;
650 net = dev_net(args->offmap->netdev);649 net = maybe_get_net(dev_net(args->offmap->netdev));
651 get_net(net);650 ns = net ? &net->ns : NULL;
652 ns = &net->ns;
653 } else {651 } else {
654 args->info->ifindex = 0;652 args->info->ifindex = 0;
655 ns = NULL;653 ns = NULL;