Skip to contentSkip to conversation

bpf: Return EINVAL instead of NULL for map_lookup_elem of queue

PATCH In review Florian Lehner 3 participants 3 messages Jun 8, 2024 – Jun 11, 2024 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Return EINVAL instead of NULL for map_lookup_elem of queue

Programs should use map_peek_elem over map_lookup_elem for queues. NULL is also not a valid queue return nor a proper error, that could be handled.

---
 kernel/bpf/queue_stack_maps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/queue_stack_maps.c +2 -1
index d869f51ea93a..85bead55024d 100644
@@ -234,7 +234,8 @@ static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
 /* Called from syscall or from eBPF program */
 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
 {
-	return NULL;
+	/* The eBPF program should use map_peek_elem instead */
+	return ERR_PTR(-EINVAL);
 }
 
 /* Called from syscall or from eBPF program */
Signature · 1 line
2.45.2
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Florian Lehner

Programs should use map_peek_elem over map_lookup_elem for queues. NULL is also not a valid queue return nor a proper error, that could be handled.

Quoted 18 lines
---
  kernel/bpf/queue_stack_maps.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/queue_stack_maps.c +2 -1
index d869f51ea93a..85bead55024d 100644
@@ -234,7 +234,8 @@ static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
  /* Called from syscall or from eBPF program */
  static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
  {
-	return NULL;
+	/* The eBPF program should use map_peek_elem instead */
+	return ERR_PTR(-EINVAL);
  }
 
  /* Called from syscall or from eBPF program */
#2 lore raw
In reply to Florian Lehner
Quoted 14 lines · Florian Lehner

Programs should use map_peek_elem over map_lookup_elem for queues. NULL is also not a valid queue return nor a proper error, that could be handled.

---
 kernel/bpf/queue_stack_maps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/queue_stack_maps.c +0 -0
index d869f51ea93a..85bead55024d 100644

kernel/bpf/queue_stack_maps.c static long queue_stack_map_push_elem(struct bpf_map *map, void *value, · line 238

Quoted 6 lines
kernel/bpf/queue_stack_maps.c +2 -1
@@ -234,7 +234,8 @@ static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
 /* Called from syscall or from eBPF program */
 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
 {
-       return NULL;
+       /* The eBPF program should use map_peek_elem instead */
+       return ERR_PTR(-EINVAL);

The commit log, the code change and comment are highly misleading. bpf prog cannot call this function due to the verifier restrictions. bpf syscall cannot reach this code path either.
This is effectively a dead code.
But if there is a verifier bug or obscure sequence of events and bpf prog manages to call this function then it must return NULL. ERR_PTR(-EINVAL) will look like a valid addr to a prog and it will crash.

pw-bot: cr