libbpf: keep FD_CLOEXEC flag when dup()'ing FD
Andrii Nakryiko
maintainerPATCH 1/1 libbpf: keep FD_CLOEXEC flag when dup()'ing FD
Make sure to preserve and/or enforce FD_CLOEXEC flag on duped FDs. Use dup3() with O_CLOEXEC flag for that.
Without this fix libbpf effectively clears FD_CLOEXEC flag on each of BPF map/prog FD, which is definitely not the right or expected behavior.
--- tools/lib/bpf/libbpf_internal.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf_internal.h +3 -7
@@ -597,13 +597,9 @@ static inline int ensure_good_fd(int fd)return fd; } -static inline int sys_dup2 (int oldfd,int newfd) +static inline int sys_dup3 (int oldfd,int newfd,int flags) { -#ifdef __NR_dup2 -return syscall (__NR_dup2, oldfd, newfd); -#else -return syscall (__NR_dup3, oldfd, newfd,0 ); -#endif +return syscall (__NR_dup3, oldfd, newfd, flags); }/* Point *fixed_fd* to the same file that *tmp_fd* points to.
@@ -614,7 +610,7 @@ static inline int reuse_fd(int fixed_fd, int tmp_fd) {int err; - err =sys_dup2 (tmp_fd, fixed_fd); + err =sys_dup3 (tmp_fd, fixed_fd,O_CLOEXEC ); err = err <0 ? -errno :0 ;close (tmp_fd);/* clean up temporary FD */ return err;
Signature · 1 line
2.43.0
Quoted 5 lines · Andrii Nakryiko
Make sure to preserve and/or enforce
FD_CLOEXECflag on duped FDs. Usedup3()withO_CLOEXECflag for that.Without this fix libbpf effectively clears
FD_CLOEXECflag on each of BPF map/prog FD, which is definitely not the right or expected behavior.
jirka
Quoted 41 lines
Reported-by Lennart Poettering <[email protected]>Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")Signed-off-by Andrii Nakryiko <[email protected]>--- tools/lib/bpf/libbpf_internal.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/lib/bpf/libbpf_internal.h +3
-7@@ -597,13 +597,9 @@ static inline int ensure_good_fd(int fd)return fd; } -static inline int sys_dup2 (int oldfd,int newfd) +static inline int sys_dup3 (int oldfd,int newfd,int flags) { -#ifdef __NR_dup2 -return syscall (__NR_dup2, oldfd, newfd); -#else -return syscall (__NR_dup3, oldfd, newfd,0 ); -#endif +return syscall (__NR_dup3, oldfd, newfd, flags); }/* Point *fixed_fd* to the same file that *tmp_fd* points to. @@ -614,7 +610,7 @@ static inline int reuse_fd(int fixed_fd, int tmp_fd) {int err; - err =sys_dup2 (tmp_fd, fixed_fd); + err =sys_dup3 (tmp_fd, fixed_fd,O_CLOEXEC ); err = err <0 ? -errno :0 ;close (tmp_fd);/* clean up temporary FD */ return err;Signature · 1 line
2.43.0
Quoted 5 lines · Andrii Nakryiko
Make sure to preserve and/or enforce
FD_CLOEXECflag on duped FDs. Usedup3()withO_CLOEXECflag for that.Without this fix libbpf effectively clears
FD_CLOEXECflag on each of BPF map/prog FD, which is definitely not the right or expected behavior.
Thanks!
lgtm, superficially.
Quoted 37 lines
Reported-by Lennart Poettering <[email protected]>Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")Signed-off-by Andrii Nakryiko <[email protected]>--- tools/lib/bpf/libbpf_internal.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/lib/bpf/libbpf_internal.h +3
-8@@ -597,13 +597,9 @@ static inline int ensure_good_fd(int fd)return fd; } -static inline int sys_dup2 (int oldfd,int newfd) +static inline int sys_dup3 (int oldfd,int newfd,int flags) { -#ifdef __NR_dup2 -return syscall (__NR_dup2, oldfd, newfd); -#else -return syscall (__NR_dup3, oldfd, newfd,0 ); -#endif +return syscall (__NR_dup3, oldfd, newfd, flags); }/* Point *fixed_fd* to the same file that *tmp_fd* points to. @@ -614,7 +610,7 @@ static inline int reuse_fd(int fixed_fd, int tmp_fd) {int err; - err =sys_dup2 (tmp_fd, fixed_fd); + err =sys_dup3 (tmp_fd, fixed_fd,O_CLOEXEC ); err = err <0 ? -errno :0 ;close (tmp_fd);/* clean up temporary FD */ return err; --
Quoted 3 lines
2.43.0
Lennart
--
Lennart Poettering, Berlin
Quoted 6 lines · Lennart Poettering
Quoted 3 lines · Andrii Nakryiko
Make sure to preserve and/or enforce
FD_CLOEXECflag on duped FDs. Usedup3()withO_CLOEXECflag for that.
Quoted 6 lines
Quoted 2 lines
Without this fix libbpf effectively clears
FD_CLOEXECflag on each of BPF map/prog FD, which is definitely not the right or expected behavior.Thanks!
lgtm, superficially.
This is now in libbpf v1.4.3 bug fix release ([0])
[0] https://github.com/libbpf/libbpf/releases/tag/v1.4.3
Quoted 46 lines
Quoted 38 lines
Reported-by Lennart Poettering <[email protected]>Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")Signed-off-by Andrii Nakryiko <[email protected]>--- tools/lib/bpf/libbpf_internal.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/lib/bpf/libbpf_internal.h +3
-8@@ -597,13 +597,9 @@ static inline int ensure_good_fd(int fd)return fd; } -static inline int sys_dup2 (int oldfd,int newfd) +static inline int sys_dup3 (int oldfd,int newfd,int flags) { -#ifdef __NR_dup2 -return syscall (__NR_dup2, oldfd, newfd); -#else -return syscall (__NR_dup3, oldfd, newfd,0 ); -#endif +return syscall (__NR_dup3, oldfd, newfd, flags); }/* Point *fixed_fd* to the same file that *tmp_fd* points to. @@ -614,7 +610,7 @@ static inline int reuse_fd(int fixed_fd, int tmp_fd) {int err; - err =sys_dup2 (tmp_fd, fixed_fd); + err =sys_dup3 (tmp_fd, fixed_fd,O_CLOEXEC ); err = err <0 ? -errno :0 ;close (tmp_fd);/* clean up temporary FD */ return err; --Quoted 3 lines
2.43.0
Lennart
--
Lennart Poettering, Berlin
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 11 lines · On Wed, 29 May 2024 15:32:39 -0700 you wrote:
Make sure to preserve and/or enforce
FD_CLOEXECflag on duped FDs. Usedup3()withO_CLOEXECflag for that.Without this fix libbpf effectively clears
FD_CLOEXECflag on each of BPF map/prog FD, which is definitely not the right or expected behavior.Reported-by Lennart Poettering <[email protected]>Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")Signed-off-by Andrii Nakryiko <[email protected]>[...]
Here is the summary with links:
- [bpf-next] libbpf: keep FD_CLOEXEC flag when dup()'ing FD
https://git.kernel.org/bpf/bpf-next/c/531876c80004
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html