Skip to contentSkip to conversation

libbpf: keep FD_CLOEXEC flag when dup()'ing FD

PATCH Applied Andrii Nakryiko 5 participants 5 messages May 29, 2024 – Jun 3, 2024

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 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.

Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")
---
 tools/lib/bpf/libbpf_internal.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf_internal.h +3 -7
index a0dcfb82e455..7e7e686008c6 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Andrii Nakryiko

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.

jirka

Quoted 41 lines
Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")
---
 tools/lib/bpf/libbpf_internal.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf_internal.h +3 -7
index a0dcfb82e455..7e7e686008c6 100644
@@ -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
#2 lore raw
In reply to Andrii Nakryiko
Quoted 5 lines · Andrii Nakryiko

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.

Thanks!

lgtm, superficially.

Quoted 37 lines
Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")
---
 tools/lib/bpf/libbpf_internal.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf_internal.h +3 -8
index a0dcfb82e455..7e7e686008c6 100644
@@ -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

#3 lore raw
Quoted 6 lines · Lennart Poettering
Quoted 3 lines · Andrii Nakryiko

Make sure to preserve and/or enforce FD_CLOEXEC flag on duped FDs. Use dup3() with O_CLOEXEC flag for that.

Quoted 6 lines
Quoted 2 lines

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.

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
Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")
---
 tools/lib/bpf/libbpf_internal.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf_internal.h +3 -8
index a0dcfb82e455..7e7e686008c6 100644
@@ -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

#4 lore raw
In reply to Andrii Nakryiko
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_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.

Fixes bc308d011ab8 ("libbpf: call dup2() syscall directly")

[...]

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