Skip to contentSkip to conversation

bpf: fix end-of-list detection in cgroup_storage_get_next_key()

PATCH Superseded Weiming Shi 3 participants 4 messages Apr 1 – Apr 3

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")
---
 kernel/bpf/local_storage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/local_storage.c +1 -1
index 8fca0c64f7b1..23267213a17f 100644
@@ -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
#1 lore raw
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 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.

Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")
---
 kernel/bpf/local_storage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/local_storage.c +1 -2
index 8fca0c64f7b1..23267213a17f 100644
@@ -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]>

Acked-by
#2 lore raw
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 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.

Quoted 6 lines
Quoted 6 lines

Replace it with list_entry_is_head() so the function correctly returns -ENOENT when there are no more entries.

Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")
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
index 8fca0c64f7b1..23267213a17f 100644
@@ -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 -2
                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,
--

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
index cf395715ced4..5451a43b3563 100644
@@ -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]>

#3 lore raw
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 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.

Quoted 6 lines
Quoted 6 lines

Replace it with list_entry_is_head() so the function correctly returns -ENOENT when there are no more entries.

Fixes de9cbbaadba5 ("bpf: introduce cgroup storage maps")
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
index 8fca0c64f7b1..23267213a17f 100644
@@ -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 -2
                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,
--

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
index cf395715ced4..5451a43b3563 100644
@@ -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.