Skip to contentSkip to conversation

bpf: btf: Use exact btf value_size match in map_check_btf()

PATCH Applied Martin KaFai Lau 2 participants 2 messages Jul 26, 2018 – Jul 27, 2018

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: btf: Use exact btf value_size match in map_check_btf()

The current map_check_btf() in BPF_MAP_TYPE_ARRAY rejects '> map->value_size' to ensure map_seq_show_elem() will not access things beyond an array element.

Yonghong suggested that using '!=' is a more correct check. The 8 bytes round_up on value_size is stored in array->elem_size. Hence, using '!=' on map->value_size is a proper check.

This patch also adds new tests to check the btf array key type and value type. Two of these new tests verify the btf's value_size (the change in this patch).

It also fixes two existing tests that wrongly encoded a btf's type size (pprint_test) and the value_type_id (in one of the raw_tests[]). However, that do not affect these two BTF verification tests before or after this test changes. These two tests mainly failed at array creation time after this patch.

Fixes a26ca7c982cb ("bpf: btf: Add pretty print support to the basic arraymap")
---
 kernel/bpf/arraymap.c                  |  2 +-
 tools/testing/selftests/bpf/test_btf.c | 86 +++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 3 deletions(-)
kernel/bpf/arraymap.c +1 -1
index 544e58f5f642..2aa55d030c77 100644
@@ -378,7 +378,7 @@ static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
 		return -EINVAL;
 
 	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-	if (!value_type || value_size > map->value_size)
+	if (!value_type || value_size != map->value_size)
 		return -EINVAL;
 
 	return 0;
tools/testing/selftests/bpf/test_btf.c +84 -2

104 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 20 lines · Martin KaFai Lau

The current map_check_btf() in BPF_MAP_TYPE_ARRAY rejects '> map->value_size' to ensure map_seq_show_elem() will not access things beyond an array element.

Yonghong suggested that using '!=' is a more correct check. The 8 bytes round_up on value_size is stored in array->elem_size. Hence, using '!=' on map->value_size is a proper check.

This patch also adds new tests to check the btf array key type and value type. Two of these new tests verify the btf's value_size (the change in this patch).

It also fixes two existing tests that wrongly encoded a btf's type size (pprint_test) and the value_type_id (in one of the raw_tests[]). However, that do not affect these two BTF verification tests before or after this test changes. These two tests mainly failed at array creation time after this patch.

Quoted 4 lines
Fixes a26ca7c982cb ("bpf: btf: Add pretty print support to the basic arraymap")

Applied to bpf, thanks Martin!