Blame SOURCES/libxtables-Use-posix_spawn-instead-of-vfork.patch

43df5c
From fbcd6c97015324480f843c08da338c9d580b2b31 Mon Sep 17 00:00:00 2001
43df5c
From: Phil Sutter <psutter@redhat.com>
43df5c
Date: Fri, 15 Mar 2019 17:51:28 +0100
43df5c
Subject: [PATCH] libxtables: Use posix_spawn() instead of vfork()
43df5c
43df5c
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1525980
43df5c
Upstream Status: iptables commit d95c1e8b65c4e
43df5c
43df5c
commit d95c1e8b65c4ec66b8fcd2f7ede257853a888750
43df5c
Author: Phil Sutter <phil@nwl.cc>
43df5c
Date:   Wed Sep 19 15:17:05 2018 +0200
43df5c
43df5c
    libxtables: Use posix_spawn() instead of vfork()
43df5c
43df5c
    According to covscan, vfork() may lead to a deadlock in the parent
43df5c
    process. It suggests to use posix_spawn() instead. Since the latter
43df5c
    combines vfork() and exec() calls, use it for xtables_insmod().
43df5c
43df5c
    Signed-off-by: Phil Sutter <phil@nwl.cc>
43df5c
    Signed-off-by: Florian Westphal <fw@strlen.de>
43df5c
43df5c
Signed-off-by: Phil Sutter <psutter@redhat.com>
43df5c
---
43df5c
 libxtables/xtables.c | 15 +++++----------
43df5c
 1 file changed, 5 insertions(+), 10 deletions(-)
43df5c
43df5c
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
43df5c
index bca9863acc566..7210d3706bf26 100644
43df5c
--- a/libxtables/xtables.c
43df5c
+++ b/libxtables/xtables.c
43df5c
@@ -21,6 +21,7 @@
43df5c
 #include <fcntl.h>
43df5c
 #include <inttypes.h>
43df5c
 #include <netdb.h>
43df5c
+#include <spawn.h>
43df5c
 #include <stdarg.h>
43df5c
 #include <stdbool.h>
43df5c
 #include <stdio.h>
43df5c
@@ -343,6 +344,7 @@ int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
43df5c
 	char *buf = NULL;
43df5c
 	char *argv[4];
43df5c
 	int status;
43df5c
+	pid_t pid;
43df5c
 
43df5c
 	/* If they don't explicitly set it, read out of kernel */
43df5c
 	if (!modprobe) {
43df5c
@@ -363,18 +365,11 @@ int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
43df5c
 	 */
43df5c
 	fflush(stdout);
43df5c
 
43df5c
-	switch (vfork()) {
43df5c
-	case 0:
43df5c
-		execv(argv[0], argv);
43df5c
-
43df5c
-		/* not usually reached */
43df5c
-		_exit(1);
43df5c
-	case -1:
43df5c
+	if (posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL)) {
43df5c
 		free(buf);
43df5c
 		return -1;
43df5c
-
43df5c
-	default: /* parent */
43df5c
-		wait(&status);
43df5c
+	} else {
43df5c
+		waitpid(pid, &status, 0);
43df5c
 	}
43df5c
 
43df5c
 	free(buf);
43df5c
-- 
43df5c
2.21.0
43df5c