* [PATCH bpf-next 0/2] Two libbpf fixes
@ 2019-04-23 22:45 Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 1/2] bpf, libbpf: handle old kernels more graceful wrt global data sections Daniel Borkmann
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Daniel Borkmann @ 2019-04-23 22:45 UTC (permalink / raw)
To: ast; +Cc: andrii.nakryiko, bpf, netdev, Daniel Borkmann
Two small fixes in relation to global data handling. Thanks!
Daniel Borkmann (2):
bpf, libbpf: handle old kernels more graceful wrt global data sections
bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement
tools/lib/bpf/libbpf.c | 105 +++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 89 insertions(+), 16 deletions(-)
--
2.9.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH bpf-next 1/2] bpf, libbpf: handle old kernels more graceful wrt global data sections
2019-04-23 22:45 [PATCH bpf-next 0/2] Two libbpf fixes Daniel Borkmann
@ 2019-04-23 22:45 ` Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 2/2] bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement Daniel Borkmann
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Daniel Borkmann @ 2019-04-23 22:45 UTC (permalink / raw)
To: ast; +Cc: andrii.nakryiko, bpf, netdev, Daniel Borkmann
Andrii reported a corner case where e.g. global static data is present
in the BPF ELF file in form of .data/.bss/.rodata section, but without
any relocations to it. Such programs could be loaded before commit
d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections"),
whereas afterwards if kernel lacks support then loading would fail.
Add a probing mechanism which skips setting up libbpf internal maps
in case of missing kernel support. In presence of relocation entries,
we abort the load attempt.
Fixes: d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections")
Reported-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
tools/lib/bpf/libbpf.c | 99 +++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 86 insertions(+), 13 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d817bf2..85315de 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -126,6 +126,8 @@ static inline __u64 ptr_to_u64(const void *ptr)
struct bpf_capabilities {
/* v4.14: kernel support for program & map names. */
__u32 name:1;
+ /* v5.2: kernel support for global data sections. */
+ __u32 global_data:1;
};
/*
@@ -854,12 +856,15 @@ bpf_object__init_maps(struct bpf_object *obj, int flags)
*
* TODO: Detect array of map and report error.
*/
- if (obj->efile.data_shndx >= 0)
- nr_maps_glob++;
- if (obj->efile.rodata_shndx >= 0)
- nr_maps_glob++;
- if (obj->efile.bss_shndx >= 0)
- nr_maps_glob++;
+ if (obj->caps.global_data) {
+ if (obj->efile.data_shndx >= 0)
+ nr_maps_glob++;
+ if (obj->efile.rodata_shndx >= 0)
+ nr_maps_glob++;
+ if (obj->efile.bss_shndx >= 0)
+ nr_maps_glob++;
+ }
+
for (i = 0; data && i < nr_syms; i++) {
GElf_Sym sym;
@@ -971,6 +976,9 @@ bpf_object__init_maps(struct bpf_object *obj, int flags)
map_idx++;
}
+ if (!obj->caps.global_data)
+ goto finalize;
+
/*
* Populate rest of obj->maps with libbpf internal maps.
*/
@@ -988,6 +996,7 @@ bpf_object__init_maps(struct bpf_object *obj, int flags)
ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
LIBBPF_MAP_BSS,
obj->efile.bss, NULL);
+finalize:
if (!ret)
qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
compare_bpf_map);
@@ -1333,11 +1342,17 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
bpf_object__shndx_is_data(obj, shdr_idx)) {
type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
- if (type != LIBBPF_MAP_UNSPEC &&
- GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
- pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
- name, insn_idx, insns[insn_idx].code);
- return -LIBBPF_ERRNO__RELOC;
+ if (type != LIBBPF_MAP_UNSPEC) {
+ if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
+ pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
+ name, insn_idx, insns[insn_idx].code);
+ return -LIBBPF_ERRNO__RELOC;
+ }
+ if (!obj->caps.global_data) {
+ pr_warning("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
+ name, insn_idx);
+ return -LIBBPF_ERRNO__RELOC;
+ }
}
for (map_idx = 0; map_idx < nr_maps; map_idx++) {
@@ -1496,9 +1511,67 @@ bpf_object__probe_name(struct bpf_object *obj)
}
static int
+bpf_object__probe_global_data(struct bpf_object *obj)
+{
+ struct bpf_load_program_attr prg_attr;
+ struct bpf_create_map_attr map_attr;
+ char *cp, errmsg[STRERR_BUFSIZE];
+ struct bpf_insn insns[] = {
+ BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
+ BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ int ret, map;
+
+ memset(&map_attr, 0, sizeof(map_attr));
+ map_attr.map_type = BPF_MAP_TYPE_ARRAY;
+ map_attr.key_size = sizeof(int);
+ map_attr.value_size = 32;
+ map_attr.max_entries = 1;
+
+ map = bpf_create_map_xattr(&map_attr);
+ if (map < 0) {
+ cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+ pr_warning("Error in %s():%s(%d). Couldn't create simple array map.\n",
+ __func__, cp, errno);
+ return -errno;
+ }
+
+ insns[0].imm = map;
+
+ memset(&prg_attr, 0, sizeof(prg_attr));
+ prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
+ prg_attr.insns = insns;
+ prg_attr.insns_cnt = ARRAY_SIZE(insns);
+ prg_attr.license = "GPL";
+
+ ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
+ if (ret >= 0) {
+ obj->caps.global_data = 1;
+ close(ret);
+ }
+
+ close(map);
+ return 0;
+}
+
+static int
bpf_object__probe_caps(struct bpf_object *obj)
{
- return bpf_object__probe_name(obj);
+ int (*probe_fn[])(struct bpf_object *obj) = {
+ bpf_object__probe_name,
+ bpf_object__probe_global_data,
+ };
+ int i, ret;
+
+ for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
+ ret = probe_fn[i](obj);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
}
static int
@@ -2100,6 +2173,7 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
CHECK_ERR(bpf_object__elf_init(obj), err, out);
CHECK_ERR(bpf_object__check_endianness(obj), err, out);
+ CHECK_ERR(bpf_object__probe_caps(obj), err, out);
CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
@@ -2193,7 +2267,6 @@ int bpf_object__load(struct bpf_object *obj)
obj->loaded = true;
- CHECK_ERR(bpf_object__probe_caps(obj), err, out);
CHECK_ERR(bpf_object__create_maps(obj), err, out);
CHECK_ERR(bpf_object__relocate(obj), err, out);
CHECK_ERR(bpf_object__load_progs(obj), err, out);
--
2.9.5
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf-next 2/2] bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement
2019-04-23 22:45 [PATCH bpf-next 0/2] Two libbpf fixes Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 1/2] bpf, libbpf: handle old kernels more graceful wrt global data sections Daniel Borkmann
@ 2019-04-23 22:45 ` Daniel Borkmann
2019-04-23 23:22 ` [PATCH bpf-next 0/2] Two libbpf fixes Andrii Nakryiko
2019-04-25 20:54 ` Alexei Starovoitov
3 siblings, 0 replies; 5+ messages in thread
From: Daniel Borkmann @ 2019-04-23 22:45 UTC (permalink / raw)
To: ast; +Cc: andrii.nakryiko, bpf, netdev, Daniel Borkmann
Ran into it while testing; in bpf_object__init_maps() data can be NULL
in the case where no map section is present. Therefore we simply cannot
access data->d_size before NULL test. Move the pr_debug() where it's
safe to access.
Fixes: d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
tools/lib/bpf/libbpf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 85315de..9052061 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -875,14 +875,14 @@ bpf_object__init_maps(struct bpf_object *obj, int flags)
nr_maps++;
}
- /* Alloc obj->maps and fill nr_maps. */
- pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
- nr_maps, data->d_size);
if (!nr_maps && !nr_maps_glob)
return 0;
/* Assume equally sized map definitions */
if (data) {
+ pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
+ nr_maps, data->d_size);
+
map_def_sz = data->d_size / nr_maps;
if (!data->d_size || (data->d_size % nr_maps) != 0) {
pr_warning("unable to determine map definition size "
--
2.9.5
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next 0/2] Two libbpf fixes
2019-04-23 22:45 [PATCH bpf-next 0/2] Two libbpf fixes Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 1/2] bpf, libbpf: handle old kernels more graceful wrt global data sections Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 2/2] bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement Daniel Borkmann
@ 2019-04-23 23:22 ` Andrii Nakryiko
2019-04-25 20:54 ` Alexei Starovoitov
3 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2019-04-23 23:22 UTC (permalink / raw)
To: Daniel Borkmann; +Cc: Alexei Starovoitov, bpf, Networking
On Tue, Apr 23, 2019 at 3:46 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> Two small fixes in relation to global data handling. Thanks!
>
> Daniel Borkmann (2):
> bpf, libbpf: handle old kernels more graceful wrt global data sections
> bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement
>
> tools/lib/bpf/libbpf.c | 105 +++++++++++++++++++++++++++++++++++++++++--------
> 1 file changed, 89 insertions(+), 16 deletions(-)
Thanks for the fixes!
Acked-by: Andrii Nakryiko <andriin@fb.com>
>
> --
> 2.9.5
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next 0/2] Two libbpf fixes
2019-04-23 22:45 [PATCH bpf-next 0/2] Two libbpf fixes Daniel Borkmann
` (2 preceding siblings ...)
2019-04-23 23:22 ` [PATCH bpf-next 0/2] Two libbpf fixes Andrii Nakryiko
@ 2019-04-25 20:54 ` Alexei Starovoitov
3 siblings, 0 replies; 5+ messages in thread
From: Alexei Starovoitov @ 2019-04-25 20:54 UTC (permalink / raw)
To: Daniel Borkmann
Cc: Alexei Starovoitov, Andrii Nakryiko, bpf, Network Development
On Tue, Apr 23, 2019 at 3:46 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> Two small fixes in relation to global data handling. Thanks!
Applied. Thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-04-25 20:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-23 22:45 [PATCH bpf-next 0/2] Two libbpf fixes Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 1/2] bpf, libbpf: handle old kernels more graceful wrt global data sections Daniel Borkmann
2019-04-23 22:45 ` [PATCH bpf-next 2/2] bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement Daniel Borkmann
2019-04-23 23:22 ` [PATCH bpf-next 0/2] Two libbpf fixes Andrii Nakryiko
2019-04-25 20:54 ` Alexei Starovoitov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.