Skip to content

Commit c9d29f4

Browse files
mauriciovasquezbernalAlexei Starovoitov
authored andcommitted
bpf/syscall: allow key to be null in map functions
This commit adds the required logic to allow key being NULL in case the key_size of the map is 0. A new __bpf_copy_key function helper only copies the key from userpsace when key_size != 0, otherwise it enforces that key must be null. Signed-off-by: Mauricio Vasquez B <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1449916 commit c9d29f4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

kernel/bpf/syscall.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,17 @@ int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
651651
return -ENOTSUPP;
652652
}
653653

654+
static void *__bpf_copy_key(void __user *ukey, u64 key_size)
655+
{
656+
if (key_size)
657+
return memdup_user(ukey, key_size);
658+
659+
if (ukey)
660+
return ERR_PTR(-EINVAL);
661+
662+
return NULL;
663+
}
664+
654665
/* last field in 'union bpf_attr' used by this command */
655666
#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
656667

@@ -678,7 +689,7 @@ static int map_lookup_elem(union bpf_attr *attr)
678689
goto err_put;
679690
}
680691

681-
key = memdup_user(ukey, map->key_size);
692+
key = __bpf_copy_key(ukey, map->key_size);
682693
if (IS_ERR(key)) {
683694
err = PTR_ERR(key);
684695
goto err_put;
@@ -785,7 +796,7 @@ static int map_update_elem(union bpf_attr *attr)
785796
goto err_put;
786797
}
787798

788-
key = memdup_user(ukey, map->key_size);
799+
key = __bpf_copy_key(ukey, map->key_size);
789800
if (IS_ERR(key)) {
790801
err = PTR_ERR(key);
791802
goto err_put;
@@ -888,7 +899,7 @@ static int map_delete_elem(union bpf_attr *attr)
888899
goto err_put;
889900
}
890901

891-
key = memdup_user(ukey, map->key_size);
902+
key = __bpf_copy_key(ukey, map->key_size);
892903
if (IS_ERR(key)) {
893904
err = PTR_ERR(key);
894905
goto err_put;
@@ -941,7 +952,7 @@ static int map_get_next_key(union bpf_attr *attr)
941952
}
942953

943954
if (ukey) {
944-
key = memdup_user(ukey, map->key_size);
955+
key = __bpf_copy_key(ukey, map->key_size);
945956
if (IS_ERR(key)) {
946957
err = PTR_ERR(key);
947958
goto err_put;

0 commit comments

Comments
 (0)