Skip to contentSkip to conversation

bpf: add missing NULL check for __dev_get_by_index

PATCH In review Qasim Ijaz 3 participants 3 messages Mar 8, 2025 – Mar 9, 2025 BPF

lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 bpf: add missing NULL check for __dev_get_by_index

The __dev_get_by_index function can return NULL if it fails to find a device with the provided ifindex.

We should handle this case by adding a NULL check
and cleaning up if it does happened.

Fixes a38845729ea3 ("bpf: offload: add map offload infrastructure")
---
 kernel/bpf/offload.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/bpf/offload.c +6 -0
index a10153c3be2d..28a30fa4457a 100644
@@ -530,6 +530,12 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
 	bpf_map_init_from_attr(&offmap->map, attr);
 	rtnl_lock();
 	offmap->netdev = __dev_get_by_index(net, attr->map_ifindex);
+	if (!offmap->netdev) {
+		rtnl_unlock();
+		bpf_map_area_free(offmap);
+		return ERR_PTR(-ENODEV);
+	}	
+
 	netdev_lock_ops(offmap->netdev);
 	down_write(&bpf_devs_lock);
 	err = bpf_dev_offload_check(offmap->netdev);
Signature · 1 line
2.39.5
#2 lore raw
In reply to Qasim Ijaz
Quoted 18 lines · Qasim Ijaz

The __dev_get_by_index function can return NULL if it fails to find a device with the provided ifindex.

We should handle this case by adding a NULL check
and cleaning up if it does happened.

Fixes a38845729ea3 ("bpf: offload: add map offload infrastructure")
---
 kernel/bpf/offload.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/bpf/offload.c +0 -0
index a10153c3be2d..28a30fa4457a 100644

kernel/bpf/offload.c struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) · line 537

Quoted 8 lines
kernel/bpf/offload.c +5 -0
@@ -530,6 +530,12 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
        bpf_map_init_from_attr(&offmap->map, attr);
        rtnl_lock();
        offmap->netdev = __dev_get_by_index(net, attr->map_ifindex);
+       if (!offmap->netdev) {
+               rtnl_unlock();
+               bpf_map_area_free(offmap);
+               return ERR_PTR(-ENODEV);
+       }

1. you're using some old git tree.
there is no such code in bpf_map_offload_map_alloc().

2. The actual code has bpf_dev_offload_check(offmap->netdev) that does the NULL check already.

pw-bot: cr

Quoted 7 lines
kernel/bpf/offload.c +1 -1
+
        netdev_lock_ops(offmap->netdev);
        down_write(&bpf_devs_lock);
        err = bpf_dev_offload_check(offmap->netdev);
--

2.39.5