-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
YiFei Zhu
authored and
Alexei Starovoitov
committed
Jul 26, 2020
1 parent
d4a89c1
commit 9e5bd1f
Showing
4 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
|
||
#ifndef __PROGS_CG_STORAGE_MULTI_H | ||
#define __PROGS_CG_STORAGE_MULTI_H | ||
|
||
#include <asm/types.h> | ||
|
||
struct cgroup_value { | ||
__u32 egress_pkts; | ||
__u32 ingress_pkts; | ||
}; | ||
|
||
#endif |
45 changes: 45 additions & 0 deletions
45
tools/testing/selftests/bpf/progs/cg_storage_multi_egress_ingress.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
|
||
/* | ||
* Copyright 2020 Google LLC. | ||
*/ | ||
|
||
#include <errno.h> | ||
#include <linux/bpf.h> | ||
#include <linux/ip.h> | ||
#include <linux/udp.h> | ||
#include <bpf/bpf_helpers.h> | ||
|
||
#include "progs/cg_storage_multi.h" | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); | ||
__type(key, struct bpf_cgroup_storage_key); | ||
__type(value, struct cgroup_value); | ||
} cgroup_storage SEC(".maps"); | ||
|
||
__u32 invocations = 0; | ||
|
||
SEC("cgroup_skb/egress") | ||
int egress(struct __sk_buff *skb) | ||
{ | ||
struct cgroup_value *ptr_cg_storage = | ||
bpf_get_local_storage(&cgroup_storage, 0); | ||
|
||
__sync_fetch_and_add(&ptr_cg_storage->egress_pkts, 1); | ||
__sync_fetch_and_add(&invocations, 1); | ||
|
||
return 1; | ||
} | ||
|
||
SEC("cgroup_skb/ingress") | ||
int ingress(struct __sk_buff *skb) | ||
{ | ||
struct cgroup_value *ptr_cg_storage = | ||
bpf_get_local_storage(&cgroup_storage, 0); | ||
|
||
__sync_fetch_and_add(&ptr_cg_storage->ingress_pkts, 1); | ||
__sync_fetch_and_add(&invocations, 1); | ||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters