From e1fd8b299aafe250e37055601750538c5b9346d1 Mon Sep 17 00:00:00 2001 From: zhang-mingyi66 Date: Sat, 12 Aug 2023 16:07:56 +0800 Subject: [PATCH] backport patches from upstream --- ...Ensure-FD-3-during-bpf_map__reuse_fd.patch | 52 +++++++++++++++++++ libbpf.spec | 6 ++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.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..a90d4d0 --- /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: Context adaptation +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 -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/libbpf.spec b/libbpf.spec index cdd52dd..b30c6b8 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 8 +Release: 9 Summary: Libbpf library License: LGPLv2 or BSD @@ -28,6 +28,7 @@ Patch0011: backport-libbpf-Fix-null-pointer-dereference-in-find_prog_by_.pa Patch0012: backport-sync-start-syncing-include-uapi-linux-fcntl.h-UAPI-h.patch Patch0013: backport-libbpf-Handle-size-overflow-for-ringbuf-mmap.patch Patch0014: backport-libbpf-Use-page-size-as-max_entries-when-probing-rin.patch +Patch0015: backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch # This package supersedes libbpf from kernel-tools, # which has default Epoch: 0. By having Epoch: 1 @@ -80,6 +81,9 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Mon Aug 14 2023 zhangmingyi 2:0.3-9 +- backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch + * Fri Apr 28 2023 zhangmingyi -2:0.3-8 - add -Werror -Wl,--no-as-needed options -- Gitee