Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Test CGROUP_STORAGE map can't be used by multiple progs

The current assumption is that the lifetime of a cgroup storage
is tied to the program's attachment. The storage is created in
cgroup_bpf_attach, and released upon cgroup_bpf_detach and
cgroup_bpf_release.

Because the current semantics is that each attachment gets a
completely independent cgroup storage, and you can have multiple
programs attached to the same (cgroup, attach type) pair, the key
of the CGROUP_STORAGE map, looking up the map with this pair could
yield multiple storages, and that is not permitted. Therefore,
the kernel verifier checks that two programs cannot share the same
CGROUP_STORAGE map, even if they have different expected attach
types, considering that the actual attach type does not always
have to be equal to the expected attach type.

The test creates a CGROUP_STORAGE map and make it shared across
two different programs, one cgroup_skb/egress and one /ingress.
It asserts that the two programs cannot be both loaded, due to
verifier failure from the above reason.

Signed-off-by: YiFei Zhu <zhuyifei@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/30a6b0da67ae6b0296c4d511bfb19c5f3d035916.1595565795.git.zhuyifei@google.com

authored by

YiFei Zhu and committed by
Alexei Starovoitov
9e5bd1f7 d4a89c1e

+99 -11
+35 -8
tools/testing/selftests/bpf/prog_tests/cg_storage_multi.c
··· 8 8 #include <cgroup_helpers.h> 9 9 #include <network_helpers.h> 10 10 11 + #include "progs/cg_storage_multi.h" 12 + 11 13 #include "cg_storage_multi_egress_only.skel.h" 14 + #include "cg_storage_multi_egress_ingress.skel.h" 12 15 13 16 #define PARENT_CGROUP "/cgroup_storage" 14 17 #define CHILD_CGROUP "/cgroup_storage/child" ··· 19 16 static int duration; 20 17 21 18 static bool assert_storage(struct bpf_map *map, const char *cgroup_path, 22 - __u32 expected) 19 + struct cgroup_value *expected) 23 20 { 24 21 struct bpf_cgroup_storage_key key = {0}; 25 - __u32 value; 22 + struct cgroup_value value; 26 23 int map_fd; 27 24 28 25 map_fd = bpf_map__fd(map); ··· 32 29 if (CHECK(bpf_map_lookup_elem(map_fd, &key, &value) < 0, 33 30 "map-lookup", "errno %d", errno)) 34 31 return true; 35 - if (CHECK(value != expected, 36 - "assert-storage", "got %u expected %u", value, expected)) 32 + if (CHECK(memcmp(&value, expected, sizeof(struct cgroup_value)), 33 + "assert-storage", "storages differ")) 37 34 return true; 38 35 39 36 return false; ··· 42 39 static bool assert_storage_noexist(struct bpf_map *map, const char *cgroup_path) 43 40 { 44 41 struct bpf_cgroup_storage_key key = {0}; 45 - __u32 value; 42 + struct cgroup_value value; 46 43 int map_fd; 47 44 48 45 map_fd = bpf_map__fd(map); ··· 89 86 static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd) 90 87 { 91 88 struct cg_storage_multi_egress_only *obj; 89 + struct cgroup_value expected_cgroup_value; 92 90 struct bpf_link *parent_link = NULL, *child_link = NULL; 93 91 bool err; 94 92 ··· 113 109 if (CHECK(obj->bss->invocations != 1, 114 110 "first-invoke", "invocations=%d", obj->bss->invocations)) 115 111 goto close_bpf_object; 116 - if (assert_storage(obj->maps.cgroup_storage, PARENT_CGROUP, 1)) 112 + expected_cgroup_value = (struct cgroup_value) { .egress_pkts = 1 }; 113 + if (assert_storage(obj->maps.cgroup_storage, 114 + PARENT_CGROUP, &expected_cgroup_value)) 117 115 goto close_bpf_object; 118 116 if (assert_storage_noexist(obj->maps.cgroup_storage, CHILD_CGROUP)) 119 117 goto close_bpf_object; ··· 135 129 if (CHECK(obj->bss->invocations != 3, 136 130 "second-invoke", "invocations=%d", obj->bss->invocations)) 137 131 goto close_bpf_object; 138 - if (assert_storage(obj->maps.cgroup_storage, PARENT_CGROUP, 2)) 132 + expected_cgroup_value = (struct cgroup_value) { .egress_pkts = 2 }; 133 + if (assert_storage(obj->maps.cgroup_storage, 134 + PARENT_CGROUP, &expected_cgroup_value)) 139 135 goto close_bpf_object; 140 - if (assert_storage(obj->maps.cgroup_storage, CHILD_CGROUP, 1)) 136 + expected_cgroup_value = (struct cgroup_value) { .egress_pkts = 1 }; 137 + if (assert_storage(obj->maps.cgroup_storage, 138 + CHILD_CGROUP, &expected_cgroup_value)) 141 139 goto close_bpf_object; 142 140 143 141 close_bpf_object: ··· 149 139 bpf_link__destroy(child_link); 150 140 151 141 cg_storage_multi_egress_only__destroy(obj); 142 + } 143 + 144 + static void test_egress_ingress(int parent_cgroup_fd, int child_cgroup_fd) 145 + { 146 + struct cg_storage_multi_egress_ingress *obj; 147 + 148 + /* Cannot load both programs due to verifier failure: 149 + * "only one cgroup storage of each type is allowed" 150 + */ 151 + obj = cg_storage_multi_egress_ingress__open_and_load(); 152 + CHECK(obj || errno != EBUSY, 153 + "skel-load", "errno %d, expected EBUSY", errno); 154 + 155 + cg_storage_multi_egress_ingress__destroy(obj); 152 156 } 153 157 154 158 void test_cg_storage_multi(void) ··· 178 154 179 155 if (test__start_subtest("egress_only")) 180 156 test_egress_only(parent_cgroup_fd, child_cgroup_fd); 157 + 158 + if (test__start_subtest("egress_ingress")) 159 + test_egress_ingress(parent_cgroup_fd, child_cgroup_fd); 181 160 182 161 close_cgroup_fd: 183 162 close(child_cgroup_fd);
+13
tools/testing/selftests/bpf/progs/cg_storage_multi.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + 3 + #ifndef __PROGS_CG_STORAGE_MULTI_H 4 + #define __PROGS_CG_STORAGE_MULTI_H 5 + 6 + #include <asm/types.h> 7 + 8 + struct cgroup_value { 9 + __u32 egress_pkts; 10 + __u32 ingress_pkts; 11 + }; 12 + 13 + #endif
+45
tools/testing/selftests/bpf/progs/cg_storage_multi_egress_ingress.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + /* 4 + * Copyright 2020 Google LLC. 5 + */ 6 + 7 + #include <errno.h> 8 + #include <linux/bpf.h> 9 + #include <linux/ip.h> 10 + #include <linux/udp.h> 11 + #include <bpf/bpf_helpers.h> 12 + 13 + #include "progs/cg_storage_multi.h" 14 + 15 + struct { 16 + __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); 17 + __type(key, struct bpf_cgroup_storage_key); 18 + __type(value, struct cgroup_value); 19 + } cgroup_storage SEC(".maps"); 20 + 21 + __u32 invocations = 0; 22 + 23 + SEC("cgroup_skb/egress") 24 + int egress(struct __sk_buff *skb) 25 + { 26 + struct cgroup_value *ptr_cg_storage = 27 + bpf_get_local_storage(&cgroup_storage, 0); 28 + 29 + __sync_fetch_and_add(&ptr_cg_storage->egress_pkts, 1); 30 + __sync_fetch_and_add(&invocations, 1); 31 + 32 + return 1; 33 + } 34 + 35 + SEC("cgroup_skb/ingress") 36 + int ingress(struct __sk_buff *skb) 37 + { 38 + struct cgroup_value *ptr_cg_storage = 39 + bpf_get_local_storage(&cgroup_storage, 0); 40 + 41 + __sync_fetch_and_add(&ptr_cg_storage->ingress_pkts, 1); 42 + __sync_fetch_and_add(&invocations, 1); 43 + 44 + return 1; 45 + }
+6 -3
tools/testing/selftests/bpf/progs/cg_storage_multi_egress_only.c
··· 10 10 #include <linux/udp.h> 11 11 #include <bpf/bpf_helpers.h> 12 12 13 + #include "progs/cg_storage_multi.h" 14 + 13 15 struct { 14 16 __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); 15 17 __type(key, struct bpf_cgroup_storage_key); 16 - __type(value, __u32); 18 + __type(value, struct cgroup_value); 17 19 } cgroup_storage SEC(".maps"); 18 20 19 21 __u32 invocations = 0; ··· 23 21 SEC("cgroup_skb/egress") 24 22 int egress(struct __sk_buff *skb) 25 23 { 26 - __u32 *ptr_cg_storage = bpf_get_local_storage(&cgroup_storage, 0); 24 + struct cgroup_value *ptr_cg_storage = 25 + bpf_get_local_storage(&cgroup_storage, 0); 27 26 28 - __sync_fetch_and_add(ptr_cg_storage, 1); 27 + __sync_fetch_and_add(&ptr_cg_storage->egress_pkts, 1); 29 28 __sync_fetch_and_add(&invocations, 1); 30 29 31 30 return 1;