libbpf: detect supported kernel BTF features and sanitize BTF
Subsystems (4)
PATCH v2 1/1 libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to have valid BPF object files with valid BTF information, that still won't load successfully due to Clang emitting newer BTF features (e.g., BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as possible (names, sizes, etc) where possible without violating kernel's validation rules.
v1->v2:
- add internal libbpf_util.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
--- tools/lib/bpf/libbpf.c | 130 +++++++++++++++++++++++++++++++- tools/lib/bpf/libbpf_internal.h | 27 +++++++ tools/lib/bpf/libbpf_probes.c | 73 ++++++++++-------- tools/lib/bpf/libbpf_util.h | 12 +++ 4 files changed, 209 insertions(+), 33 deletions(-) create mode 100644 tools/lib/bpf/libbpf_internal.h
tools/lib/bpf/libbpf.c +129 -1
@@ -44,6 +44,7 @@#include "btf.h" #include "str_error.h" #include "libbpf_util.h" +#include "libbpf_internal.h" #ifndef EM_BPF #define EM_BPF 247
@@ -128,6 +129,10 @@ struct bpf_capabilities { __u32 name:1;/* v5.2: kernel support for global data sections. */ __u32 global_data:1; +/* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */ + __u32 btf_func:1; +/* BTF_KIND_VAR and BTF_KIND_DATASEC support */ + __u32 btf_datasec:1; };/*
@@ -1021,6 +1026,74 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)return false; } +static void bpf_object__sanitize_btf (struct bpf_object *obj) +{ +bool has_datasec = obj->caps.btf_datasec; +bool has_func = obj->caps.btf_func; +struct btf *btf = obj->btf; +struct btf_type *t; +int i, j, vlen; +__u16 kind; + +if (!obj->btf || (has_func && has_datasec)) +return ; + +for (i =1 ; i <=btf__get_nr_types (btf); i++) { + t = (struct btf_type *)btf__type_by_id (btf, i); + kind =BTF_INFO_KIND (t->info); + +if (!has_datasec && kind ==BTF_KIND_VAR ) { +/* replace VAR with INT */ + t->info =BTF_INFO_ENC (BTF_KIND_INT ,0 ,0 ); + t->size =sizeof (int ); + *(int *)(t+1 ) =BTF_INT_ENC (0 ,0 ,32 ); + }else if (!has_datasec && kind ==BTF_KIND_DATASEC ) { +/* replace DATASEC with STRUCT */ +struct btf_var_secinfo *v = (void *)(t +1 ); +struct btf_member *m = (void *)(t +1 ); +struct btf_type *vt; +char *name; + + name = (char *)btf__name_by_offset (btf, t->name_off); +while (*name) { +if (*name =='.' ) + *name ='_' ; + name++; + } + + vlen =BTF_INFO_VLEN (t->info); + t->info =BTF_INFO_ENC (BTF_KIND_STRUCT ,0 , vlen); +for (j =0 ; j < vlen; j++, v++, m++) { +/* order of field assignments is important */ + m->offset = v->offset *8 ; + m->type = v->type; +/* preserve variable name as member name */ + vt = (void *)btf__type_by_id (btf, v->type); + m->name_off = vt->name_off; + } + }else if (!has_func && kind ==BTF_KIND_FUNC_PROTO ) { +/* replace FUNC_PROTO with ENUM */ + vlen =BTF_INFO_VLEN (t->info); + t->info =BTF_INFO_ENC (BTF_KIND_ENUM ,0 , vlen); + t->size =sizeof (__u32);/* kernel enforced */ + }else if (!has_func && kind ==BTF_KIND_FUNC ) { +/* replace FUNC with TYPEDEF */ + t->info =BTF_INFO_ENC (BTF_KIND_TYPEDEF ,0 ,0 ); + } + } +} + +static void bpf_object__sanitize_btf_ext (struct bpf_object *obj) +{ +if (!obj->btf_ext) +return ; + +if (!obj->caps.btf_func) { +btf_ext__free (obj->btf_ext); + obj->btf_ext =NULL ; + } +} +static int bpf_object__elf_collect (struct bpf_object *obj,int flags) {Elf *elf = obj->efile.elf;
@@ -1164,8 +1237,10 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->btf =NULL ; }else { err =btf__finalize_data (obj, obj->btf); -if (!err) +if (!err) { +bpf_object__sanitize_btf (obj); err =btf__load (obj->btf); + }if (err) {pr_warning ("Error finalizing and loading %s into kernel: %d. Ignored and continue.\n" ,BTF_ELF_SEC , err);
@@ -1187,6 +1262,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)BTF_EXT_ELF_SEC , PTR_ERR(obj->btf_ext)); obj->btf_ext =NULL ; + }else { +bpf_object__sanitize_btf_ext (obj); } } }
@@ -1556,12 +1633,63 @@ bpf_object__probe_global_data(struct bpf_object *obj)return 0 ; } +static int bpf_object__probe_btf_func (struct bpf_object *obj) +{ +const char strs[] ="\0int\0x\0a" ; +/* void x(int a) {} */ +__u32 types[] = { +/* int */ +BTF_TYPE_INT_ENC (1 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +/* FUNC_PROTO */ /* [2] */ +BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,1 ),0 ), +BTF_PARAM_ENC (7 ,1 ), +/* FUNC x */ /* [3] */ +BTF_TYPE_ENC (5 ,BTF_INFO_ENC (BTF_KIND_FUNC ,0 ,0 ),2 ), + }; +int res; + + res =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (res <0 ) +return res; +if (res >0 ) + obj->caps.btf_func =1 ; +return 0 ; +} + +static int bpf_object__probe_btf_datasec (struct bpf_object *obj) +{ +const char strs[] ="\0x\0.data" ; +/* static int a; */ +__u32 types[] = { +/* int */ +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +/* VAR x */ /* [2] */ +BTF_TYPE_ENC (1 ,BTF_INFO_ENC (BTF_KIND_VAR ,0 ,0 ),1 ), +BTF_VAR_STATIC , +/* DATASEC val */ /* [3] */ +BTF_TYPE_ENC (3 ,BTF_INFO_ENC (BTF_KIND_DATASEC ,0 ,1 ),4 ), +BTF_VAR_SECINFO_ENC (2 ,0 ,4 ), + }; +int res; + + res =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (res <0 ) +return res; +if (res >0 ) + obj->caps.btf_datasec =1 ; +return 0 ; +} +static int bpf_object__probe_caps (struct bpf_object *obj) {int (*probe_fn[])(struct bpf_object *obj) = { bpf_object__probe_name, bpf_object__probe_global_data, + bpf_object__probe_btf_func, + bpf_object__probe_btf_datasec, };int i, ret;
tools/lib/bpf/libbpf_internal.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ + +/* +* Internal libbpf helpers. +* +* Copyright (c) 2019 Facebook +*/ + +#ifndef __LIBBPF_LIBBPF_INTERNAL_H +#define __LIBBPF_LIBBPF_INTERNAL_H + +#define BTF_INFO_ENC (kind, kind_flag, vlen) \ + ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) +#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type) +#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ + ((encoding) << 24 | (bits_offset) << 16 | (nr_bits)) +#define BTF_TYPE_INT_ENC (name, encoding, bits_offset, bits, sz) \ + BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \ + BTF_INT_ENC(encoding, bits_offset, bits) +#define BTF_MEMBER_ENC (name, type, bits_offset) (name), (type), (bits_offset) +#define BTF_PARAM_ENC (name, type) (name), (type) +#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size) + +int libbpf__probe_raw_btf (const char *raw_types,size_t types_len, +const char *str_sec,size_t str_len); + +#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
tools/lib/bpf/libbpf_probes.c +41 -32
105 lines not on this page · raw message
tools/lib/bpf/libbpf_util.h +12 -0
@@ -57,4 +57,16 @@ do { \ } /* extern "C" */#endif +#define BTF_INFO_ENC (kind, kind_flag, vlen) \ + ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) +#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type) +#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ + ((encoding) << 24 | (bits_offset) << 16 | (nr_bits)) +#define BTF_TYPE_INT_ENC (name, encoding, bits_offset, bits, sz) \ + BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \ + BTF_INT_ENC(encoding, bits_offset, bits) +#define BTF_MEMBER_ENC (name, type, bits_offset) (name), (type), (bits_offset) +#define BTF_PARAM_ENC (name, type) (name), (type) +#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size) +#endif
Signature · 1 line
2.17.1
Quoted 23 lines · Andrii Nakryiko
Depending on used versions of libbpf, Clang, and kernel, it's possible to have valid BPF object files with valid BTF information, that still won't load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info,BTF_KIND_DATASEC, etc), that are not yet supported by older kernel.This patch adds detection of BTF features and sanitizes BPF object's BTF by substituting various supported BTF kinds, which have compatible layout:
-BTF_KIND_FUNC->BTF_KIND_TYPEDEF
-BTF_KIND_FUNC_PROTO->BTF_KIND_ENUM
-BTF_KIND_VAR->BTF_KIND_INT
-BTF_KIND_DATASEC->BTF_KIND_STRUCTReplacement is done in such a way as to preserve as much information as possible (names, sizes, etc) where possible without violating kernel's validation rules.
v1->v2:
- add internallibbpf_util.hw/ common stuff
- switch SK storage BTF to use newlibbpf__probe_raw_btf()Reported-by Alexei Starovoitov <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>
...
Quoted 9 lines
tools/lib/bpf/libbpf_util.h +1
-0@@ -57,4 +57,16 @@ do { \ } /* extern "C" */#endif +#define BTF_INFO_ENC (kind, kind_flag, vlen) \
tools/lib/bpf/libbpf_util.h do { \ · line 70
Quoted 10 lines
tools/lib/bpf/libbpf_util.h +10
-0+ ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) +#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type) +#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ + ((encoding) << 24 | (bits_offset) << 16 | (nr_bits)) +#define BTF_TYPE_INT_ENC (name, encoding, bits_offset, bits, sz) \ + BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \ + BTF_INT_ENC(encoding, bits_offset, bits) +#define BTF_MEMBER_ENC (name, type, bits_offset) (name), (type), (bits_offset) +#define BTF_PARAM_ENC (name, type) (name), (type) +#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size)
hmm. why are those needed in libbpf_util.h ? I thought the goal is move them into libbpf_internal.h and not to expose to users?
Quoted 34 lines · Alexei Starovoitov
Quoted 23 lines · Andrii Nakryiko
Depending on used versions of libbpf, Clang, and kernel, it's possible to have valid BPF object files with valid BTF information, that still won't load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info,BTF_KIND_DATASEC, etc), that are not yet supported by older kernel.This patch adds detection of BTF features and sanitizes BPF object's BTF by substituting various supported BTF kinds, which have compatible layout:
-BTF_KIND_FUNC->BTF_KIND_TYPEDEF
-BTF_KIND_FUNC_PROTO->BTF_KIND_ENUM
-BTF_KIND_VAR->BTF_KIND_INT
-BTF_KIND_DATASEC->BTF_KIND_STRUCTReplacement is done in such a way as to preserve as much information as possible (names, sizes, etc) where possible without violating kernel's validation rules.
v1->v2:
- add internallibbpf_util.hw/ common stuff
- switch SK storage BTF to use newlibbpf__probe_raw_btf()Reported-by Alexei Starovoitov <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>...
Quoted 8 lines
tools/lib/bpf/libbpf_util.h +0
-0@@ -57,4 +57,16 @@ do { \ } /* extern "C" */#endif
Quoted 15 lines
tools/lib/bpf/libbpf_util.h do { \ · line 70
Quoted 11 lines
tools/lib/bpf/libbpf_util.h +11
-0+#define BTF_INFO_ENC (kind, kind_flag, vlen) \ + ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN)) +#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type) +#define BTF_INT_ENC (encoding, bits_offset, nr_bits) \ + ((encoding) << 24 | (bits_offset) << 16 | (nr_bits)) +#define BTF_TYPE_INT_ENC (name, encoding, bits_offset, bits, sz) \ + BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \ + BTF_INT_ENC(encoding, bits_offset, bits) +#define BTF_MEMBER_ENC (name, type, bits_offset) (name), (type), (bits_offset) +#define BTF_PARAM_ENC (name, type) (name), (type) +#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size)hmm. why are those needed in
libbpf_util.h? I thought the goal is move them intolibbpf_internal.hand not to expose to users?
Oh... Sorry, my bad, leftover from previous refactoring... Looks like there will be v3.