From 3b7e14cbb39e3a120f5cd1359fcd1f04a10271df Mon Sep 17 00:00:00 2001 From: jinzhiguang Date: Fri, 22 Nov 2024 14:30:55 +0800 Subject: [PATCH] backport patches from upstream: backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch --- ...w-BTF-objects-inherit-input-endianne.patch | 42 +++++ ...bject__open_skeleton-s-mishandling-o.patch | 151 ++++++++++++++++++ libbpf.spec | 9 +- 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch create mode 100644 backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch diff --git a/backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch b/backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch new file mode 100644 index 0000000..0570671 --- /dev/null +++ b/backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch @@ -0,0 +1,42 @@ +From bef446c3d077acd0c30aae59eef8aa4baa1b4c89 Mon Sep 17 00:00:00 2001 +From: Tony Ambardar +Date: Fri, 30 Aug 2024 02:51:50 -0700 +Subject: [PATCH 1/1] libbpf: Ensure new BTF objects inherit input endianness + +New split BTF needs to preserve base's endianness. Similarly, when +creating a distilled BTF, we need to preserve original endianness. + +Fix by updating libbpf's btf__distill_base() and btf_new_empty() to retain +the byte order of any source BTF objects when creating new ones. + +Fixes: ba451366bf44 ("libbpf: Implement basic split BTF support") +Fixes: 58e185a0dc35 ("libbpf: Add btf__distill_base() creating split BTF with distilled base BTF") +Reported-by: Song Liu +Reported-by: Eduard Zingerman +Suggested-by: Eduard Zingerman +Signed-off-by: Tony Ambardar +Signed-off-by: Andrii Nakryiko +Tested-by: Alan Maguire +Acked-by: Eduard Zingerman +Link: https://lore.kernel.org/bpf/6358db36c5f68b07873a0a5be2d062b1af5ea5f8.camel@gmail.com/ +Link: https://lore.kernel.org/bpf/20240830095150.278881-1-tony.ambardar@gmail.com +(cherry picked from commit fe28fae57a9463fbf935b2fbdeb1aaa8390aa6a2) +--- + src/btf.c | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/src/btf.c b/src/btf.c +index 0a2c079..a2d1abf 100644 +--- a/src/btf.c ++++ b/src/btf.c +@@ -832,6 +832,7 @@ static struct btf *btf_new_empty(struct btf *base_btf) + btf->base_btf = base_btf; + btf->start_id = btf__type_cnt(base_btf); + btf->start_str_off = base_btf->hdr->str_len; ++ btf->swapped_endian = base_btf->swapped_endian; + } + + /* +1 for empty string at offset 0 */ +-- +2.43.0 + diff --git a/backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch b/backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch new file mode 100644 index 0000000..fd1ff0f --- /dev/null +++ b/backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch @@ -0,0 +1,151 @@ +From 142926a3e2aeabb40f212e5cabe4f6c14d820d52 Mon Sep 17 00:00:00 2001 +From: Andrii Nakryiko +Date: Tue, 27 Aug 2024 13:37:21 -0700 +Subject: [PATCH 1/1] libbpf: Fix bpf_object__open_skeleton()'s mishandling of + options +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +We do an ugly copying of options in bpf_object__open_skeleton() just to +be able to set object name from skeleton's recorded name (while still +allowing user to override it through opts->object_name). + +This is not just ugly, but it also is broken due to memcpy() that +doesn't take into account potential skel_opts' and user-provided opts' +sizes differences due to backward and forward compatibility. This leads +to copying over extra bytes and then failing to validate options +properly. It could, technically, lead also to SIGSEGV, if we are unlucky. + +So just get rid of that memory copy completely and instead pass +default object name into bpf_object_open() directly, simplifying all +this significantly. The rule now is that obj_name should be non-NULL for +bpf_object_open() when called with in-memory buffer, so validate that +explicitly as well. + +We adopt bpf_object__open_mem() to this as well and generate default +name (based on buffer memory address and size) outside of bpf_object_open(). + +Fixes: d66562fba1ce ("libbpf: Add BPF object skeleton support") +Reported-by: Daniel Müller +Signed-off-by: Andrii Nakryiko +Signed-off-by: Daniel Borkmann +Reviewed-by: Daniel Müller +Acked-by: Eduard Zingerman +Link: https://lore.kernel.org/bpf/20240827203721.1145494-1-andrii@kernel.org +(cherry picked from commit f6f24022d3054d2855612e642f8fe9f1148b4275) +--- + src/libbpf.c | 52 +++++++++++++++++++--------------------------------- + 1 file changed, 19 insertions(+), 33 deletions(-) + +diff --git a/src/libbpf.c b/src/libbpf.c +index 1b95c06..14dcf49 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -7281,16 +7281,19 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object + } + + static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, ++ const char *obj_name, + const struct bpf_object_open_opts *opts) + { +- const char *obj_name, *kconfig, *btf_tmp_path; ++ const char *kconfig, *btf_tmp_path; + struct bpf_object *obj; +- char tmp_name[64]; + int err; + char *log_buf; + size_t log_size; + __u32 log_level; + ++ if (obj_buf && !obj_name) ++ return ERR_PTR(-EINVAL); ++ + if (elf_version(EV_CURRENT) == EV_NONE) { + pr_warn("failed to init libelf for %s\n", + path ? : "(mem buf)"); +@@ -7300,16 +7303,12 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, + if (!OPTS_VALID(opts, bpf_object_open_opts)) + return ERR_PTR(-EINVAL); + +- obj_name = OPTS_GET(opts, object_name, NULL); ++ obj_name = OPTS_GET(opts, object_name, NULL) ?: obj_name; + if (obj_buf) { +- if (!obj_name) { +- snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", +- (unsigned long)obj_buf, +- (unsigned long)obj_buf_sz); +- obj_name = tmp_name; +- } + path = obj_name; + pr_debug("loading object '%s' from buffer\n", obj_name); ++ } else { ++ pr_debug("loading object from %s\n", path); + } + + log_buf = OPTS_GET(opts, kernel_log_buf, NULL); +@@ -7375,9 +7374,7 @@ bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts) + if (!path) + return libbpf_err_ptr(-EINVAL); + +- pr_debug("loading %s\n", path); +- +- return libbpf_ptr(bpf_object_open(path, NULL, 0, opts)); ++ return libbpf_ptr(bpf_object_open(path, NULL, 0, NULL, opts)); + } + + struct bpf_object *bpf_object__open(const char *path) +@@ -7389,10 +7386,15 @@ struct bpf_object * + bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, + const struct bpf_object_open_opts *opts) + { ++ char tmp_name[64]; ++ + if (!obj_buf || obj_buf_sz == 0) + return libbpf_err_ptr(-EINVAL); + +- return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts)); ++ /* create a (quite useless) default "name" for this memory buffer object */ ++ snprintf(tmp_name, sizeof(tmp_name), "%lx-%zx", (unsigned long)obj_buf, obj_buf_sz); ++ ++ return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, tmp_name, opts)); + } + + static int bpf_object_unload(struct bpf_object *obj) +@@ -12586,29 +12588,13 @@ static int populate_skeleton_progs(const struct bpf_object *obj, + int bpf_object__open_skeleton(struct bpf_object_skeleton *s, + const struct bpf_object_open_opts *opts) + { +- DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts, +- .object_name = s->name, +- ); + struct bpf_object *obj; + int err; + +- /* Attempt to preserve opts->object_name, unless overriden by user +- * explicitly. Overwriting object name for skeletons is discouraged, +- * as it breaks global data maps, because they contain object name +- * prefix as their own map name prefix. When skeleton is generated, +- * bpftool is making an assumption that this name will stay the same. +- */ +- if (opts) { +- memcpy(&skel_opts, opts, sizeof(*opts)); +- if (!opts->object_name) +- skel_opts.object_name = s->name; +- } +- +- obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); +- err = libbpf_get_error(obj); +- if (err) { +- pr_warn("failed to initialize skeleton BPF object '%s': %d\n", +- s->name, err); ++ obj = bpf_object_open(NULL, s->data, s->data_sz, s->name, opts); ++ if (IS_ERR(obj)) { ++ err = PTR_ERR(obj); ++ pr_warn("failed to initialize skeleton BPF object '%s': %d\n", s->name, err); + return libbpf_err(err); + } + +-- +2.43.0 + diff --git a/libbpf.spec b/libbpf.spec index c0746a0..cca4e59 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 5 +Release: 6 Summary: Libbpf library License: LGPLv2 or BSD @@ -21,6 +21,8 @@ Patch0004: backport-libbpf-Free-btf_vmlinux-when-closing-bpf_object.patch Patch0005: backport-libbpf-Avoid-uninitialized-value-in-BPF_CORE_READ_BI.patch Patch0006: backport-libbpf-Add-NULL-checks-to-bpf_object__prev_map,next_.patch Patch0007: backport-libbpf-Apply-map_set_def_max_entries-for-inner_maps-.patch +Patch0008: backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch +Patch0009: backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch # This package supersedes libbpf from kernel-tools, # which has default Epoch: 0. By having Epoch: 1 @@ -73,6 +75,11 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Fri Nov 22 2024 jinzhiguang 2:1.2.2-6 +- backport patch from upstream: + backport-libbpf-Fix-bpf_object__open_skeleton-s-mishandling-o.patch + backport-libbpf-Ensure-new-BTF-objects-inherit-input-endianne.patch + * Wed Oct 09 2024 zhangmingyi 2:1.2.2-5 - backport patch from upstream: backport-libbpf-Add-NULL-checks-to-bpf_object__prev_map,next_.patch -- Gitee