From 894d9522fae95ce1d88d6547bf556b1d2c1256c0 Mon Sep 17 00:00:00 2001 From: zhang-mingyi66 Date: Thu, 10 Aug 2023 13:01:15 +0800 Subject: [PATCH] backport patches from upstream (cherry picked from commit 93793bcc0e9df8fe4edccf037351fb192583241a) --- ...Ensure-FD-3-during-bpf_map__reuse_fd.patch | 52 +++++++++ ...bbpf-always-opens-files-with-O_CLOEX.patch | 103 ++++++++++++++++++ libbpf.spec | 9 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch create mode 100644 backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch diff --git a/backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch b/backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch new file mode 100644 index 0000000..d930585 --- /dev/null +++ b/backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch @@ -0,0 +1,52 @@ +From fa1a18d38bfb77207b0a3137e211a706fd8487f4 Mon Sep 17 00:00:00 2001 +From: Andrii Nakryiko +Date: Thu, 25 May 2023 15:13:11 -0700 +Subject: [PATCH] libbpf: Ensure FD >= 3 during bpf_map__reuse_fd() + +Improve bpf_map__reuse_fd() logic and ensure that dup'ed map FD is +"good" (>= 3) and has O_CLOEXEC flags. Use fcntl(F_DUPFD_CLOEXEC) for +that, similarly to ensure_good_fd() helper we already use in low-level +APIs that work with bpf() syscall. + +Conflict: NA +Reference:https://github.com/libbpf/libbpf/commit/fa1a18d38bfb77207b0a3137e211a706fd8487f4 + +Suggested-by: Lennart Poettering +Signed-off-by: Andrii Nakryiko +Signed-off-by: Daniel Borkmann +Link: https://lore.kernel.org/bpf/20230525221311.2136408-2-andrii@kernel.org +--- + src/libbpf.c | 13 ++++++------- + 1 file changed, 6 insertions(+), 7 deletions(-) + +diff --git a/src/libbpf.c b/src/libbpf.c +index 60ef4c5e3..47632606b 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -4414,18 +4414,17 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) + if (!new_name) + return libbpf_err(-errno); + +- new_fd = open("/", O_RDONLY | O_CLOEXEC); ++ /* ++ * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. ++ * This is similar to what we do in ensure_good_fd(), but without ++ * closing original FD. ++ */ ++ new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); + if (new_fd < 0) { + err = -errno; + goto err_free_new_name; + } + +- new_fd = dup3(fd, new_fd, O_CLOEXEC); +- if (new_fd < 0) { +- err = -errno; +- goto err_close_new_fd; +- } +- + err = zclose(map->fd); + if (err) { + err = -errno; +-- +2.23.0 diff --git a/backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch b/backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch new file mode 100644 index 0000000..2b87444 --- /dev/null +++ b/backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch @@ -0,0 +1,103 @@ +From ba7a44da68adfa4f0d8285de4cdc16e3754929a8 Mon Sep 17 00:00:00 2001 +From: Andrii Nakryiko +Date: Thu, 25 May 2023 15:13:10 -0700 +Subject: [PATCH] libbpf: Ensure libbpf always opens files with O_CLOEXEC + +Make sure that libbpf code always gets FD with O_CLOEXEC flag set, +regardless if file is open through open() or fopen(). For the latter +this means to add "e" to mode string, which is supported since pretty +ancient glibc v2.7. + +Also drop the outdated TODO comment in usdt.c, which was already completed. + +Conflict: code modify in libbpf_probes.c is in libbpf.c +Reference:https://github.com/libbpf/libbpf/commit/ba7a44da68adfa4f0d8285de4cdc16e3754929a8 + +Suggested-by: Lennart Poettering +Signed-off-by: Andrii Nakryiko +Signed-off-by: Daniel Borkmann +Link: https://lore.kernel.org/bpf/20230525221311.2136408-1-andrii@kernel.org +--- + src/btf.c | 2 +- + src/libbpf.c | 8 ++++---- + src/usdt.c | 5 ++--- + 3 files changed, 7 insertions(+), 8 deletions(-) + +diff --git a/src/btf.c b/src/btf.c +index 0a2c07924..8484b563b 100644 +--- a/src/btf.c ++++ b/src/btf.c +@@ -1064,7 +1064,7 @@ static struct btf *btf_parse_raw(const char *path, struct btf *base_btf) + int err = 0; + long sz; + +- f = fopen(path, "rb"); ++ f = fopen(path, "rbe"); + if (!f) { + err = -errno; + goto err_out; +diff --git a/src/libbpf.c b/src/libbpf.c +index 1ceb3a9..60ef4c5 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -826,7 +826,7 @@ __u32 get_kernel_version(void) + if (faccessat(AT_FDCWD, ubuntu_kver_file, R_OK, AT_EACCESS) == 0) { + FILE *f; + +- f = fopen(ubuntu_kver_file, "r"); ++ f = fopen(ubuntu_kver_file, "re"); + if (f) { + if (fscanf(f, "%*s %*s %d.%d.%d\n", &major, &minor, &patch) == 3) { + fclose(f); +@@ -4351,7 +4351,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) + snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd); + memset(info, 0, sizeof(*info)); + +- fp = fopen(file, "r"); ++ fp = fopen(file, "re"); + if (!fp) { + err = -errno; + pr_warn("failed to open %s: %d. No procfs support?\n", file, +@@ -7455,7 +7455,7 @@ int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx) + int ret, err = 0; + FILE *f; + +- f = fopen("/proc/kallsyms", "r"); ++ f = fopen("/proc/kallsyms", "re"); + if (!f) { + err = -errno; + pr_warn("failed to open /proc/kallsyms: %d\n", err); +@@ -10075,7 +10075,7 @@ static int parse_uint_from_file(const char *file, const char *fmt) + int err, ret; + FILE *f; + +- f = fopen(file, "r"); ++ f = fopen(file, "re"); + if (!f) { + err = -errno; + pr_debug("failed to open '%s': %s\n", file, +diff --git a/src/usdt.c b/src/usdt.c +index 086eef355..f1a141555 100644 +--- a/src/usdt.c ++++ b/src/usdt.c +@@ -466,7 +466,7 @@ static int parse_vma_segs(int pid, const char *lib_path, struct elf_seg **segs, + + proceed: + sprintf(line, "/proc/%d/maps", pid); +- f = fopen(line, "r"); ++ f = fopen(line, "re"); + if (!f) { + err = -errno; + pr_warn("usdt: failed to open '%s' to get base addr of '%s': %d\n", +@@ -954,8 +954,7 @@ struct bpf_link *usdt_manager_attach_usdt(struct usdt_manager *man, const struct + spec_map_fd = bpf_map__fd(man->specs_map); + ip_map_fd = bpf_map__fd(man->ip_to_spec_id_map); + +- /* TODO: perform path resolution similar to uprobe's */ +- fd = open(path, O_RDONLY); ++ fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) { + err = -errno; + pr_warn("usdt: failed to open ELF binary '%s': %d\n", path, err); +-- +2.23.0 diff --git a/libbpf.spec b/libbpf.spec index ad6bbf3..175a49d 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 9 +Release: 10 Summary: Libbpf library License: LGPLv2 or BSD @@ -31,6 +31,8 @@ Patch0015: backport-libbpf-disassociate-section-handler-on-explicit-bpf_.pa Patch0016: backport-libbpf-Use-correct-return-pointer-in-attach_raw_tp.patch Patch0017: backport-libbpf-Use-elf_getshdrnum-instead-of-e_shnum.patch Patch0018: 0001-sync-bpf-helper-funcs-from-kernel.patch +Patch0019: backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch +Patch0020: backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch # This package supersedes libbpf from kernel-tools, # which has default Epoch: 0. By having Epoch: 1 @@ -83,6 +85,11 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Mon Aug 14 2023 zhangmingyi 2:0.8.1-10 +- backport patches from upstream: + backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch + backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch + * Fri Aug 4 2023 JofDiamonds -2:0.8.1-9 - sync bpf helper funcs from kernel -- Gitee