Skip to contentSkip to conversation

libbpf: set close-on-exec flag on gzopen

PATCH Applied Martin Kelly 4 participants 7 messages Aug 10, 2023 – Aug 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: set close-on-exec flag on gzopen

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

---
 tools/lib/bpf/libbpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -2
index 17883f5a44b9..b14a4376a86e 100644
@@ -1978,9 +1978,9 @@ static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
 		return -ENAMETOOLONG;
 
 	/* gzopen also accepts uncompressed files. */
-	file = gzopen(buf, "r");
+	file = gzopen(buf, "re");
 	if (!file)
-		file = gzopen("/proc/config.gz", "r");
+		file = gzopen("/proc/config.gz", "re");
 
 	if (!file) {
 		pr_warn("failed to open system Kconfig\n");
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines · Martin Kelly

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

---
  tools/lib/bpf/libbpf.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 17883f5a44b9..b14a4376a86e 100644
@@ -1978,9 +1978,9 @@ static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
  		return -ENAMETOOLONG;

tools/lib/bpf/libbpf.c static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) · line 1986

Quoted 10 lines
tools/lib/bpf/libbpf.c +2 -2
 
  	/* gzopen also accepts uncompressed files. */
-	file = gzopen(buf, "r");
+	file = gzopen(buf, "re");
  	if (!file)
-		file = gzopen("/proc/config.gz", "r");
+		file = gzopen("/proc/config.gz", "re");
 
  	if (!file) {
  		pr_warn("failed to open system Kconfig\n");

Sorry for double-sending the patch; the first was missing the bpf-next prefix and I wasn't sure if that would be an issue. Feel free to ignore this patch, as the other already got a reply.

#2 lore raw
Quoted 24 lines · Martin Kelly
Quoted 23 lines · Martin Kelly

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

---
  tools/lib/bpf/libbpf.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -1
index 17883f5a44b9..b14a4376a86e 100644
@@ -1978,9 +1978,9 @@ static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
  		return -ENAMETOOLONG;
  	/* gzopen also accepts uncompressed files. */
-	file = gzopen(buf, "r");
+	file = gzopen(buf, "re");
  	if (!file)
Quoted 8 lines

tools/lib/bpf/libbpf.c static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) · line 1986

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -1
-		file = gzopen("/proc/config.gz", "r");
+		file = gzopen("/proc/config.gz", "re");
  	if (!file) {
  		pr_warn("failed to open system Kconfig\n");

Sorry for double-sending the patch; the first was missing the bpf-next prefix and I wasn't sure if that would be an issue. Feel free to ignore this patch, as the other already got a reply.

Oops, I missed your resend:

https://lore.kernel.org/bpf/[email protected]/

Next time pls at least reply to the first 'wrong' one :-)

#3 lore raw
Quoted 33 lines · Stanislav Fomichev
Quoted 23 lines · Martin Kelly
Quoted 22 lines · Martin Kelly

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

---
   tools/lib/bpf/libbpf.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -1
index 17883f5a44b9..b14a4376a86e 100644
@@ -1978,9 +1978,9 @@ static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
   		return -ENAMETOOLONG;
   	/* gzopen also accepts uncompressed files. */
-	file = gzopen(buf, "r");
+	file = gzopen(buf, "re");
Quoted 8 lines

tools/lib/bpf/libbpf.c static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) · line 1986

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
   	if (!file)
-		file = gzopen("/proc/config.gz", "r");
+		file = gzopen("/proc/config.gz", "re");
   	if (!file) {
   		pr_warn("failed to open system Kconfig\n");

Sorry for double-sending the patch; the first was missing the bpf-next prefix and I wasn't sure if that would be an issue. Feel free to ignore this patch, as the other already got a reply.

Quoted 1 line

Next time pls at least reply to the first 'wrong' one :-)

Yeah, I replied to this one as you had already replied to the first, and I didn't want to add confusion and split the conversation into two pieces. I saw a 40 minute or so delay before the initial patch was posted to vger, and I had thought "maybe it's because I was missing the bpf-next prefix" and then resent... only to have the initial message appear a few minutes later :). So, my bad, I jumped the gun.

Next time I'll make sure to add the right prefix initially and to take into account mailing list delay.

#4 lore raw
In reply to Martin Kelly
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Thu, 10 Aug 2023 14:43:53 -0700 you wrote:

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

[...]

Here is the summary with links:
- [bpf-next] libbpf: set close-on-exec flag on gzopen

    https://git.kernel.org/bpf/bpf-next/c/8e50750f122e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#5 lore raw
In reply to Martin Kelly
Quoted 9 lines · Martin Kelly

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

Looks good, thanks for the fix, applied! Do we also need to convert the fmemopen in bpf_object__read_kconfig_mem - if yes, could you also send a patch?

Thanks,
Daniel

#6 lore raw
Quoted 9 lines · Daniel Borkmann
Quoted 8 lines · Martin Kelly

From: Marco Vedovati <[email protected]>

Enable the close-on-exec flag when using gzopen

This is especially important for multithreaded programs making use of libbpf, where a fork + exec could race with libbpf library calls, potentially resulting in a file descriptor leaked to the new process.

Quoted 8 lines
Quoted 1 line

Looks good, thanks for the fix, applied! Do we also need to convert the fmemopen in bpf_object__read_kconfig_mem - if yes, could you also send a patch?

Thanks,
Daniel

Good thinking, but it looks like (from libc sources and a quick test) the underlying fmemopen doesn't accept the "e" mode for close-on-exec. On glibc 2.35 at least, it's calling into _IO_fopencookie, which then executes this switch and rejects the flag.