Skip to contentSkip to conversation

libbpf: set close-on-exec flag on gzopen

PATCH In review Martin Kelly 2 participants 2 messages Aug 10, 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
Acked-by on v1 1/1
#1 lore raw
Quoted 7 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.

We do have fopen(, "re") all over the place; gzopen seem to pass mode to fopen.

Fixes 8601fd422148 ("libbpf: Allow to augment system Kconfig through extra optional config")

For fixes, not sure whether anybody actually builds libbpf from the kernel tree...