Skip to content

Commit 0cdbb4b

Browse files
tohojoborkmann
authored andcommitted
devmap: Allow map lookups from eBPF
We don't currently allow lookups into a devmap from eBPF, because the map lookup returns a pointer directly to the dev->ifindex, which shouldn't be modifiable from eBPF. However, being able to do lookups in devmaps is useful to know (e.g.) whether forwarding to a specific interface is enabled. Currently, programs work around this by keeping a shadow map of another type which indicates whether a map index is valid. Since we now have a flag to make maps read-only from the eBPF side, we can simply lift the lookup restriction if we make sure this flag is always set. Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: Jonathan Lemon <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 43e74c0 commit 0cdbb4b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

kernel/bpf/devmap.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
8989
attr->value_size != 4 || attr->map_flags & ~DEV_CREATE_FLAG_MASK)
9090
return ERR_PTR(-EINVAL);
9191

92+
/* Lookup returns a pointer straight to dev->ifindex, so make sure the
93+
* verifier prevents writes from the BPF side
94+
*/
95+
attr->map_flags |= BPF_F_RDONLY_PROG;
96+
9297
dtab = kzalloc(sizeof(*dtab), GFP_USER);
9398
if (!dtab)
9499
return ERR_PTR(-ENOMEM);

kernel/bpf/verifier.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,12 +3414,9 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
34143414
if (func_id != BPF_FUNC_get_local_storage)
34153415
goto error;
34163416
break;
3417-
/* devmap returns a pointer to a live net_device ifindex that we cannot
3418-
* allow to be modified from bpf side. So do not allow lookup elements
3419-
* for now.
3420-
*/
34213417
case BPF_MAP_TYPE_DEVMAP:
3422-
if (func_id != BPF_FUNC_redirect_map)
3418+
if (func_id != BPF_FUNC_redirect_map &&
3419+
func_id != BPF_FUNC_map_lookup_elem)
34233420
goto error;
34243421
break;
34253422
/* Restrict bpf side of cpumap and xskmap, open when use-cases

0 commit comments

Comments
 (0)