bpf: fix end-of-list detection in cgroup_storage_get_next_key()
Subsystems (3)
PATCH 1/1 bpf: fix end-of-list detection in cgroup_storage_get_next_key()
list_next_entry() never returns NULL -- when the current element is the last entry it wraps to the list head via container_of(). The subsequent NULL check is therefore dead code and get_next_key() never returns -ENOENT for the last element, instead reading storage->key from a bogus pointer that aliases internal map fields and copying the result to userspace.
Replace it with list_entry_is_head() so the function correctly returns -ENOENT when there are no more entries.
--- kernel/bpf/local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/local_storage.c +1 -1
@@ -270,7 +270,7 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key, goto enoent; storage =list_next_entry (storage, list_map); -if (!storage) +if (list_entry_is_head (storage, &map->list, list_map)) goto enoent; }else { storage = list_first_entry(&map->list,
Signature · 1 line
2.43.0
Quoted 32 lines · Weiming Shi
list_next_entry()never returns NULL -- when the current element is the last entry it wraps to the list head viacontainer_of(). The subsequent NULL check is therefore dead code andget_next_key()never returns-ENOENTfor the last element, instead readingstorage->keyfrom a bogus pointer that aliases internal map fields and copying the result to userspace.Replace it with
list_entry_is_head()so the function correctly returns-ENOENTwhen there are no more entries.Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")Reported-by Xiang Mei <[email protected]>Signed-off-by Weiming Shi <[email protected]>--- kernel/bpf/local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/local_storage.c +1
-2@@ -270,7 +270,7 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key, goto enoent; storage =list_next_entry (storage, list_map); -if (!storage) +if (list_entry_is_head (storage, &map->list, list_map)) goto enoent; }else { storage = list_first_entry(&map->list, --
Quoted 3 lines
2.43.0
Looks correct to me. It might also be worth adding a selftest for this cornet case.
Reviewed by Sun Jian <[email protected]>
Paul Chaignon
Acked-byQuoted 9 lines · sun jian
Quoted 8 lines · Weiming Shi
list_next_entry()never returns NULL -- when the current element is the last entry it wraps to the list head viacontainer_of(). The subsequent NULL check is therefore dead code andget_next_key()never returns-ENOENTfor the last element, instead readingstorage->keyfrom a bogus pointer that aliases internal map fields and copying the result to userspace.
Quoted 6 lines
Quoted 6 lines
Replace it with
list_entry_is_head()so the function correctly returns-ENOENTwhen there are no more entries.Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")Reported-by Xiang Mei <[email protected]>Signed-off-by Weiming Shi <[email protected]>
Quoted 11 lines
Quoted 11 lines
--- kernel/bpf/local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/local_storage.c +0
-0@@ -270,7 +270,7 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key, goto enoent;
Quoted 12 lines
Quoted 10 lines
kernel/bpf/local_storage.c +1
-2storage =list_next_entry (storage, list_map); -if (!storage) +if (list_entry_is_head (storage, &map->list, list_map)) goto enoent; }else { storage = list_first_entry(&map->list, --2.43.0
Looks correct to me. It might also be worth adding a selftest for this cornet case.
I agree, it would be good to cover this in selftests. You can use the following diff for that:
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c +5 -0
@@ -86,6 +86,11 @@ void test_cgroup_storage(void) err =SYS_NOFAIL (PING_CMD );ASSERT_OK (err,"sixth ping" ); + err =bpf_map__get_next_key (skel->maps.cgroup_storage, &key, &key, +sizeof (key)); +ASSERT_ERR (err,"bpf_map__get_next_key should fail" ); +ASSERT_EQ (errno,ENOENT ,"no second key" ); + cleanup_progs:cgroup_storage__destroy (skel); cleanup_network:
Quoted 3 lines
Reviewed by Sun Jian <[email protected]>
Quoted 65 lines · Paul Chaignon
Quoted 9 lines · sun jian
Quoted 8 lines · Weiming Shi
list_next_entry()never returns NULL -- when the current element is the last entry it wraps to the list head viacontainer_of(). The subsequent NULL check is therefore dead code andget_next_key()never returns-ENOENTfor the last element, instead readingstorage->keyfrom a bogus pointer that aliases internal map fields and copying the result to userspace.Quoted 6 lines
Quoted 6 lines
Replace it with
list_entry_is_head()so the function correctly returns-ENOENTwhen there are no more entries.Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")Reported-by Xiang Mei <[email protected]>Signed-off-by Weiming Shi <[email protected]>Acked-by Paul Chaignon <[email protected]>Quoted 11 lines
Quoted 11 lines
--- kernel/bpf/local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/local_storage.c +0
-0@@ -270,7 +270,7 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key, goto enoent;Quoted 12 lines
Quoted 10 lines
kernel/bpf/local_storage.c +1
-2storage =list_next_entry (storage, list_map); -if (!storage) +if (list_entry_is_head (storage, &map->list, list_map)) goto enoent; }else { storage = list_first_entry(&map->list, --2.43.0
Looks correct to me. It might also be worth adding a selftest for this cornet case.
I agree, it would be good to cover this in selftests. You can use the following diff for that:
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c +5
-0@@ -86,6 +86,11 @@ void test_cgroup_storage(void) err =SYS_NOFAIL (PING_CMD );ASSERT_OK (err,"sixth ping" ); + err =bpf_map__get_next_key (skel->maps.cgroup_storage, &key, &key, +sizeof (key)); +ASSERT_ERR (err,"bpf_map__get_next_key should fail" ); +ASSERT_EQ (errno,ENOENT ,"no second key" ); + cleanup_progs:cgroup_storage__destroy (skel); cleanup_network:Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
Reviewed by Sun Jian <[email protected]>
Thanks for the your review and the selftest suggestion! I've incorporated it in v2.