Blame SOURCES/iptables-1.4.21-flock_wait.patch

43df5c
From aa562a660d1555b13cffbac1e744033e91f82707 Mon Sep 17 00:00:00 2001
43df5c
From: Pablo Neira Ayuso <pablo@netfilter.org>
43df5c
Date: Fri, 16 Jan 2015 14:21:57 +0100
43df5c
Subject: iptables: use flock() instead of abstract unix sockets
43df5c
43df5c
Abstract unix sockets cannot be used to synchronize several concurrent
43df5c
instances of iptables since an unpriviledged process can create them and
43df5c
prevent the legitimate iptables instance from running.
43df5c
43df5c
Use flock() and /run instead as suggested by Lennart Poettering.
43df5c
43df5c
Fixes: 93587a0 ("ip[6]tables: Add locking to prevent concurrent instances")
43df5c
Reported-by: Lennart Poettering <lennart@poettering.net>
43df5c
Cc: Phil Oester <kernel@linuxace.com>
43df5c
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
43df5c
43df5c
diff --git a/iptables/xshared.c b/iptables/xshared.c
43df5c
index b18022e..7beb86b 100644
43df5c
--- a/iptables/xshared.c
43df5c
+++ b/iptables/xshared.c
43df5c
@@ -9,11 +9,11 @@
43df5c
 #include <sys/socket.h>
43df5c
 #include <sys/un.h>
43df5c
 #include <unistd.h>
43df5c
+#include <fcntl.h>
43df5c
 #include <xtables.h>
43df5c
 #include "xshared.h"
43df5c
 
43df5c
-#define XT_SOCKET_NAME "xtables"
43df5c
-#define XT_SOCKET_LEN 8
43df5c
+#define XT_LOCK_NAME	"/run/xtables.lock"
43df5c
 
43df5c
 /*
43df5c
  * Print out any special helps. A user might like to be able to add a --help
43df5c
@@ -245,22 +245,14 @@ void xs_init_match(struct xtables_match *match)
43df5c
 
43df5c
 bool xtables_lock(int wait)
43df5c
 {
43df5c
-	int i = 0, ret, xt_socket;
43df5c
-	struct sockaddr_un xt_addr;
43df5c
-	int waited = 0;
43df5c
-
43df5c
-	memset(&xt_addr, 0, sizeof(xt_addr));
43df5c
-	xt_addr.sun_family = AF_UNIX;
43df5c
-	strcpy(xt_addr.sun_path+1, XT_SOCKET_NAME);
43df5c
-	xt_socket = socket(AF_UNIX, SOCK_STREAM, 0);
43df5c
-	/* If we can't even create a socket, fall back to prior (lockless) behavior */
43df5c
-	if (xt_socket < 0)
43df5c
+	int fd, waited = 0, i = 0;
43df5c
+
43df5c
+	fd = open(XT_LOCK_NAME, O_CREAT, 0600);
43df5c
+	if (fd < 0)
43df5c
 		return true;
43df5c
 
43df5c
 	while (1) {
43df5c
-		ret = bind(xt_socket, (struct sockaddr*)&xt_addr,
43df5c
-			   offsetof(struct sockaddr_un, sun_path)+XT_SOCKET_LEN);
43df5c
-		if (ret == 0)
43df5c
+		if (flock(fd, LOCK_EX | LOCK_NB) == 0)
43df5c
 			return true;
43df5c
 		else if (wait >= 0 && waited >= wait)
43df5c
 			return false;
43df5c
-- 
43df5c
cgit v0.10.2
43df5c
43df5c
commit 6dc53c514f1e4683e51a877b3a2f3128cfccef28
43df5c
Author: Pablo Neira Ayuso <pablo@netfilter.org>
43df5c
Date:   Mon Feb 16 16:57:39 2015 +0100
43df5c
43df5c
    xshared: calm down compilation warning
43df5c
    
43df5c
    xshared.c: In function ‘xtables_lock’:
43df5c
    xshared.c:255:3: warning: implicit declaration of function ‘flock’ [-Wimplicit-function-declaration]
43df5c
    
43df5c
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
43df5c
43df5c
diff --git a/iptables/xshared.c b/iptables/xshared.c
43df5c
index 7beb86b..81c2581 100644
43df5c
--- a/iptables/xshared.c
43df5c
+++ b/iptables/xshared.c
43df5c
@@ -6,6 +6,7 @@
43df5c
 #include <stdio.h>
43df5c
 #include <stdlib.h>
43df5c
 #include <string.h>
43df5c
+#include <sys/file.h>
43df5c
 #include <sys/socket.h>
43df5c
 #include <sys/un.h>
43df5c
 #include <unistd.h>