@@ -68,6 +68,7 @@ struct bpf_test {
68
68
int fixup_prog2 [MAX_FIXUPS ];
69
69
int fixup_map_in_map [MAX_FIXUPS ];
70
70
int fixup_cgroup_storage [MAX_FIXUPS ];
71
+ int fixup_percpu_cgroup_storage [MAX_FIXUPS ];
71
72
const char * errstr ;
72
73
const char * errstr_unpriv ;
73
74
uint32_t retval ;
@@ -4676,7 +4677,7 @@ static struct bpf_test tests[] = {
4676
4677
.prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4677
4678
},
4678
4679
{
4679
- "invalid per- cgroup storage access 3" ,
4680
+ "invalid cgroup storage access 3" ,
4680
4681
.insns = {
4681
4682
BPF_MOV64_IMM (BPF_REG_2 , 0 ),
4682
4683
BPF_LD_MAP_FD (BPF_REG_1 , 0 ),
@@ -4743,6 +4744,121 @@ static struct bpf_test tests[] = {
4743
4744
.errstr = "get_local_storage() doesn't support non-zero flags" ,
4744
4745
.prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
4745
4746
},
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
+ },
4746
4862
{
4747
4863
"multiple registers share map_lookup_elem result" ,
4748
4864
.insns = {
@@ -12615,15 +12731,17 @@ static int create_map_in_map(void)
12615
12731
return outer_map_fd ;
12616
12732
}
12617
12733
12618
- static int create_cgroup_storage (void )
12734
+ static int create_cgroup_storage (bool percpu )
12619
12735
{
12736
+ enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
12737
+ BPF_MAP_TYPE_CGROUP_STORAGE ;
12620
12738
int fd ;
12621
12739
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 ),
12624
12741
TEST_DATA_LEN , 0 , 0 );
12625
12742
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 ));
12627
12745
12628
12746
return fd ;
12629
12747
}
@@ -12641,6 +12759,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
12641
12759
int * fixup_prog2 = test -> fixup_prog2 ;
12642
12760
int * fixup_map_in_map = test -> fixup_map_in_map ;
12643
12761
int * fixup_cgroup_storage = test -> fixup_cgroup_storage ;
12762
+ int * fixup_percpu_cgroup_storage = test -> fixup_percpu_cgroup_storage ;
12644
12763
12645
12764
if (test -> fill_helper )
12646
12765
test -> fill_helper (test );
@@ -12710,12 +12829,20 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
12710
12829
}
12711
12830
12712
12831
if (* fixup_cgroup_storage ) {
12713
- map_fds [7 ] = create_cgroup_storage ();
12832
+ map_fds [7 ] = create_cgroup_storage (false );
12714
12833
do {
12715
12834
prog [* fixup_cgroup_storage ].imm = map_fds [7 ];
12716
12835
fixup_cgroup_storage ++ ;
12717
12836
} while (* fixup_cgroup_storage );
12718
12837
}
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
+ }
12719
12846
}
12720
12847
12721
12848
static void do_test_single (struct bpf_test * test , bool unpriv ,
0 commit comments