提交 a1d93406 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

libbpf: Reject static maps

mainline inclusion
from mainline-5.14-rc1
commit c1cccec9
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c1cccec9c63637c4c5ee0aa2da2850d983c19e88

-------------------------------------------------

Static maps never really worked with libbpf, because all such maps were always
silently resolved to the very first map. Detect static maps (both legacy and
BTF-defined) and report user-friendly error.

Tested locally by switching few maps (legacy and BTF-defined) in selftests to
static ones and verifying that now libbpf rejects them loudly.
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210513233643.194711-2-andrii@kernel.org
(cherry picked from commit c1cccec9)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 a5e30967
...@@ -1795,7 +1795,6 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict) ...@@ -1795,7 +1795,6 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
if (!symbols) if (!symbols)
return -EINVAL; return -EINVAL;
scn = elf_sec_by_idx(obj, obj->efile.maps_shndx); scn = elf_sec_by_idx(obj, obj->efile.maps_shndx);
data = elf_sec_data(obj, scn); data = elf_sec_data(obj, scn);
if (!scn || !data) { if (!scn || !data) {
...@@ -1855,6 +1854,12 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict) ...@@ -1855,6 +1854,12 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
return -LIBBPF_ERRNO__FORMAT; return -LIBBPF_ERRNO__FORMAT;
} }
if (GELF_ST_TYPE(sym.st_info) == STT_SECTION
|| GELF_ST_BIND(sym.st_info) == STB_LOCAL) {
pr_warn("map '%s' (legacy): static maps are not supported\n", map_name);
return -ENOTSUP;
}
map->libbpf_type = LIBBPF_MAP_UNSPEC; map->libbpf_type = LIBBPF_MAP_UNSPEC;
map->sec_idx = sym.st_shndx; map->sec_idx = sym.st_shndx;
map->sec_offset = sym.st_value; map->sec_offset = sym.st_value;
...@@ -2262,6 +2267,16 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def ...@@ -2262,6 +2267,16 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def
pr_debug("map '%s': found inner map definition.\n", map->name); pr_debug("map '%s': found inner map definition.\n", map->name);
} }
static const char *btf_var_linkage_str(__u32 linkage)
{
switch (linkage) {
case BTF_VAR_STATIC: return "static";
case BTF_VAR_GLOBAL_ALLOCATED: return "global";
case BTF_VAR_GLOBAL_EXTERN: return "extern";
default: return "unknown";
}
}
static int bpf_object__init_user_btf_map(struct bpf_object *obj, static int bpf_object__init_user_btf_map(struct bpf_object *obj,
const struct btf_type *sec, const struct btf_type *sec,
int var_idx, int sec_idx, int var_idx, int sec_idx,
...@@ -2294,10 +2309,9 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, ...@@ -2294,10 +2309,9 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
map_name, btf_kind_str(var)); map_name, btf_kind_str(var));
return -EINVAL; return -EINVAL;
} }
if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED && if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
var_extra->linkage != BTF_VAR_STATIC) { pr_warn("map '%s': unsupported map linkage %s.\n",
pr_warn("map '%s': unsupported var linkage %u.\n", map_name, btf_var_linkage_str(var_extra->linkage));
map_name, var_extra->linkage);
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册