@@ -68,6 +68,7 @@ struct bpf_test {
6868 int fixup_prog2 [MAX_FIXUPS ];
6969 int fixup_map_in_map [MAX_FIXUPS ];
7070 int fixup_cgroup_storage [MAX_FIXUPS ];
71+ int fixup_percpu_cgroup_storage [MAX_FIXUPS ];
7172 const char * errstr ;
7273 const char * errstr_unpriv ;
7374 uint32_t retval ;
@@ -4676,7 +4677,7 @@ static struct bpf_test tests[] = {
46764677 .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
46774678 },
46784679 {
4679- "invalid per- cgroup storage access 3" ,
4680+ "invalid cgroup storage access 3" ,
46804681 .insns = {
46814682 BPF_MOV64_IMM (BPF_REG_2 , 0 ),
46824683 BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
@@ -4743,6 +4744,121 @@ static struct bpf_test tests[] = {
47434744 .errstr = "get_local_storage() doesn't support non-zero flags" ,
47444745 .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
47454746 },
4747+ {
4748+ "valid per-cpu cgroup storage access" ,
4749+ .insns = {
4750+ BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4751+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4752+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4753+ BPF_FUNC_get_local_storage ),
4754+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , 0 ),
4755+ BPF_MOV64_REG (BPF_REG_0 , BPF_REG_1 ),
4756+ BPF_ALU64_IMM (BPF_AND , BPF_REG_0 , 1 ),
4757+ BPF_EXIT_INSN (),
4758+ },
4759+ .fixup_percpu_cgroup_storage = { 1 },
4760+ .result = ACCEPT ,
4761+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4762+ },
4763+ {
4764+ "invalid per-cpu cgroup storage access 1" ,
4765+ .insns = {
4766+ BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4767+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4768+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4769+ BPF_FUNC_get_local_storage ),
4770+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , 0 ),
4771+ BPF_MOV64_REG (BPF_REG_0 , BPF_REG_1 ),
4772+ BPF_ALU64_IMM (BPF_AND , BPF_REG_0 , 1 ),
4773+ BPF_EXIT_INSN (),
4774+ },
4775+ .fixup_map1 = { 1 },
4776+ .result = REJECT ,
4777+ .errstr = "cannot pass map_type 1 into func bpf_get_local_storage" ,
4778+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4779+ },
4780+ {
4781+ "invalid per-cpu cgroup storage access 2" ,
4782+ .insns = {
4783+ BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4784+ BPF_LD_MAP_FD (BPF_REG_1 , 1 ),
4785+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4786+ BPF_FUNC_get_local_storage ),
4787+ BPF_ALU64_IMM (BPF_AND , BPF_REG_0 , 1 ),
4788+ BPF_EXIT_INSN (),
4789+ },
4790+ .result = REJECT ,
4791+ .errstr = "fd 1 is not pointing to valid bpf_map" ,
4792+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4793+ },
4794+ {
4795+ "invalid per-cpu cgroup storage access 3" ,
4796+ .insns = {
4797+ BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4798+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4799+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4800+ BPF_FUNC_get_local_storage ),
4801+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , 256 ),
4802+ BPF_ALU64_IMM (BPF_ADD , BPF_REG_1 , 1 ),
4803+ BPF_MOV64_IMM (BPF_REG_0 , 0 ),
4804+ BPF_EXIT_INSN (),
4805+ },
4806+ .fixup_percpu_cgroup_storage = { 1 },
4807+ .result = REJECT ,
4808+ .errstr = "invalid access to map value, value_size=64 off=256 size=4" ,
4809+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4810+ },
4811+ {
4812+ "invalid per-cpu cgroup storage access 4" ,
4813+ .insns = {
4814+ BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4815+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4816+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4817+ BPF_FUNC_get_local_storage ),
4818+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , -2 ),
4819+ BPF_MOV64_REG (BPF_REG_0 , BPF_REG_1 ),
4820+ BPF_ALU64_IMM (BPF_ADD , BPF_REG_1 , 1 ),
4821+ BPF_EXIT_INSN (),
4822+ },
4823+ .fixup_cgroup_storage = { 1 },
4824+ .result = REJECT ,
4825+ .errstr = "invalid access to map value, value_size=64 off=-2 size=4" ,
4826+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4827+ },
4828+ {
4829+ "invalid per-cpu cgroup storage access 5" ,
4830+ .insns = {
4831+ BPF_MOV64_IMM (BPF_REG_2 , 7 ),
4832+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4833+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4834+ BPF_FUNC_get_local_storage ),
4835+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , 0 ),
4836+ BPF_MOV64_REG (BPF_REG_0 , BPF_REG_1 ),
4837+ BPF_ALU64_IMM (BPF_AND , BPF_REG_0 , 1 ),
4838+ BPF_EXIT_INSN (),
4839+ },
4840+ .fixup_percpu_cgroup_storage = { 1 },
4841+ .result = REJECT ,
4842+ .errstr = "get_local_storage() doesn't support non-zero flags" ,
4843+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4844+ },
4845+ {
4846+ "invalid per-cpu cgroup storage access 6" ,
4847+ .insns = {
4848+ BPF_MOV64_REG (BPF_REG_2 , BPF_REG_1 ),
4849+ BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
4850+ BPF_RAW_INSN (BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
4851+ BPF_FUNC_get_local_storage ),
4852+ BPF_LDX_MEM (BPF_W , BPF_REG_1 , BPF_REG_0 , 0 ),
4853+ BPF_MOV64_REG (BPF_REG_0 , BPF_REG_1 ),
4854+ BPF_ALU64_IMM (BPF_AND , BPF_REG_0 , 1 ),
4855+ BPF_EXIT_INSN (),
4856+ },
4857+ .fixup_percpu_cgroup_storage = { 1 },
4858+ .result = REJECT ,
4859+ .errstr = "get_local_storage() doesn't support non-zero flags" ,
4860+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4861+ },
47464862 {
47474863 "multiple registers share map_lookup_elem result" ,
47484864 .insns = {
@@ -12615,15 +12731,17 @@ static int create_map_in_map(void)
1261512731 return outer_map_fd ;
1261612732}
1261712733
12618- static int create_cgroup_storage (void )
12734+ static int create_cgroup_storage (bool percpu )
1261912735{
12736+ enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
12737+ BPF_MAP_TYPE_CGROUP_STORAGE ;
1262012738 int fd ;
1262112739
12622- fd = bpf_create_map (BPF_MAP_TYPE_CGROUP_STORAGE ,
12623- sizeof (struct bpf_cgroup_storage_key ),
12740+ fd = bpf_create_map (type , sizeof (struct bpf_cgroup_storage_key ),
1262412741 TEST_DATA_LEN , 0 , 0 );
1262512742 if (fd < 0 )
12626- printf ("Failed to create array '%s'!\n" , strerror (errno ));
12743+ printf ("Failed to create cgroup storage '%s'!\n" ,
12744+ strerror (errno ));
1262712745
1262812746 return fd ;
1262912747}
@@ -12641,6 +12759,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
1264112759 int * fixup_prog2 = test -> fixup_prog2 ;
1264212760 int * fixup_map_in_map = test -> fixup_map_in_map ;
1264312761 int * fixup_cgroup_storage = test -> fixup_cgroup_storage ;
12762+ int * fixup_percpu_cgroup_storage = test -> fixup_percpu_cgroup_storage ;
1264412763
1264512764 if (test -> fill_helper )
1264612765 test -> fill_helper (test );
@@ -12710,12 +12829,20 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
1271012829 }
1271112830
1271212831 if (* fixup_cgroup_storage ) {
12713- map_fds [7 ] = create_cgroup_storage ();
12832+ map_fds [7 ] = create_cgroup_storage (false );
1271412833 do {
1271512834 prog [* fixup_cgroup_storage ].imm = map_fds [7 ];
1271612835 fixup_cgroup_storage ++ ;
1271712836 } while (* fixup_cgroup_storage );
1271812837 }
12838+
12839+ if (* fixup_percpu_cgroup_storage ) {
12840+ map_fds [8 ] = create_cgroup_storage (true);
12841+ do {
12842+ prog [* fixup_percpu_cgroup_storage ].imm = map_fds [8 ];
12843+ fixup_percpu_cgroup_storage ++ ;
12844+ } while (* fixup_percpu_cgroup_storage );
12845+ }
1271912846}
1272012847
1272112848static void do_test_single (struct bpf_test * test , bool unpriv ,
0 commit comments