bpf: btf: Check array t->size
authorMartin KaFai Lau <kafai@fb.com>
Sat, 2 Jun 2018 16:06:50 +0000 (09:06 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 2 Jun 2018 18:22:36 +0000 (11:22 -0700)
This patch ensures array's t->size is 0.

The array size is decided by its individual elem's size and the
number of elements.  Hence, t->size is not used and
it must be 0.

A test case is added to test_btf.c

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/btf.c
tools/testing/selftests/bpf/test_btf.c

index 3d20aa1f4b543194a34724f193ae8f78df016222..84ad532f28547c2a96c5998ec9a79422bc37503d 100644 (file)
@@ -1342,6 +1342,11 @@ static s32 btf_array_check_meta(struct btf_verifier_env *env,
                return -EINVAL;
        }
 
+       if (t->size) {
+               btf_verifier_log_type(env, t, "size != 0");
+               return -EINVAL;
+       }
+
        /* Array elem type and index type cannot be in type void,
         * so !array->type and !array->index_type are not allowed.
         */
index 35064df688c16d2cd667a006b01809b9dd25e364..fd8246e84149bfab1b42e3b5e7f980e9ce620308 100644 (file)
@@ -1178,6 +1178,29 @@ static struct btf_raw_test raw_tests[] = {
        .err_str = "Invalid index",
 },
 
+{
+       .descr = "array test. t->size != 0\"",
+       .raw_types = {
+               /* int */                               /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
+               /* int[16] */                           /* [2] */
+               BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
+               BTF_ARRAY_ENC(1, 1, 16),
+               BTF_END_RAW,
+       },
+       .str_sec = "",
+       .str_sec_size = sizeof(""),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "array_test_map",
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 4,
+       .btf_load_err = true,
+       .err_str = "size != 0",
+},
+
 {
        .descr = "int test. invalid int_data",
        .raw_types = {