Blame SOURCES/iptables-1.4.21-restore_support_acquiring_the_lock.patch

26b15f
Adapted version of
26b15f
26b15f
commit 999eaa241212d3952ddff39a99d0d55a74e3639e
26b15f
Author: Lorenzo Colitti <lorenzo@google.com>
26b15f
Date:   Thu Mar 16 16:55:02 2017 +0900
26b15f
26b15f
    iptables-restore: support acquiring the lock.
26b15f
    
26b15f
    Currently, ip[6]tables-restore does not perform any locking, so it
26b15f
    is not safe to use concurrently with ip[6]tables.
26b15f
    
26b15f
    This patch makes ip[6]tables-restore wait for the lock if -w
26b15f
    was specified. Arguments to -w and -W are supported in the same
26b15f
    was as they are in ip[6]tables.
26b15f
    
26b15f
    The lock is not acquired on startup. Instead, it is acquired when
26b15f
    a new table handle is created (on encountering '*') and released
26b15f
    when the table is committed (COMMIT). This makes it possible to
26b15f
    keep long-running iptables-restore processes in the background
26b15f
    (for example, reading commands from a pipe opened by a system
26b15f
    management daemon) and simultaneously run iptables commands.
26b15f
    
26b15f
    If -w is not specified, then the command proceeds without taking
26b15f
    the lock.
26b15f
    
26b15f
    Tested as follows:
26b15f
    
26b15f
    1. Run iptables-restore -w, and check that iptables commands work
26b15f
       with or without -w.
26b15f
    2. Type "*filter" into the iptables-restore input. Verify that
26b15f
       a) ip[6]tables commands without -w fail with "another app is
26b15f
          currently holding the xtables lock...".
26b15f
       b) ip[6]tables commands with "-w 2" fail after 2 seconds.
26b15f
       c) ip[6]tables commands with "-w" hang until "COMMIT" is
26b15f
          typed into the iptables-restore window.
26b15f
    3. With the lock held by an ip6tables-restore process:
26b15f
         strace -e flock /tmp/iptables/sbin/iptables-restore -w 1 -W 100000
26b15f
       shows 11 calls to flock and fails.
26b15f
    4. Run an iptables-restore with -w and one without -w, and check:
26b15f
       a) Type "*filter" in the first and then the second, and the
26b15f
          second exits with an error.
26b15f
       b) Type "*filter" in the second and "*filter" "-S" "COMMIT"
26b15f
          into the first. The rules are listed only when the first
26b15f
          copy sees "COMMIT".
26b15f
    
26b15f
    Signed-off-by: Narayan Kamath <narayan@google.com>
26b15f
    Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
26b15f
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
26b15f
26b15f
diff -up iptables-1.4.21/iptables/ip6tables.c.restore_support_acquiring_the_lock iptables-1.4.21/iptables/ip6tables.c
26b15f
--- iptables-1.4.21/iptables/ip6tables.c.restore_support_acquiring_the_lock	2017-04-05 14:55:52.561008864 +0200
26b15f
+++ iptables-1.4.21/iptables/ip6tables.c	2017-04-05 14:55:52.564008888 +0200
26b15f
@@ -1767,7 +1767,7 @@ int do_command6(int argc, char *argv[],
26b15f
 	generic_opt_check(command, cs.options);
26b15f
 
26b15f
 	/* Attempt to acquire the xtables lock */
26b15f
-	if (!restore && !xtables_lock(wait, &wait_interval)) {
26b15f
+	if (!restore && xtables_lock(wait, &wait_interval) == XT_LOCK_BUSY) {
26b15f
 		fprintf(stderr, "Another app is currently holding the xtables lock. ");
26b15f
 		if (wait == 0)
26b15f
 			fprintf(stderr, "Perhaps you want to use the -w option?\n");
26b15f
diff -up iptables-1.4.21/iptables/ip6tables-restore.c.restore_support_acquiring_the_lock iptables-1.4.21/iptables/ip6tables-restore.c
26b15f
--- iptables-1.4.21/iptables/ip6tables-restore.c.restore_support_acquiring_the_lock	2013-11-22 12:18:13.000000000 +0100
26b15f
+++ iptables-1.4.21/iptables/ip6tables-restore.c	2017-04-05 14:58:41.513393942 +0200
26b15f
@@ -15,6 +15,7 @@
26b15f
 #include <stdio.h>
26b15f
 #include <stdlib.h>
26b15f
 #include "ip6tables.h"
26b15f
+#include "xshared.h"
26b15f
 #include "xtables.h"
26b15f
 #include "libiptc/libip6tc.h"
26b15f
 #include "ip6tables-multi.h"
26b15f
@@ -25,18 +26,24 @@
26b15f
 #define DEBUGP(x, args...)
26b15f
 #endif
26b15f
 
26b15f
-static int binary = 0, counters = 0, verbose = 0, noflush = 0;
26b15f
+static int binary = 0, counters = 0, verbose = 0, noflush = 0, wait = 0;
26b15f
+
26b15f
+static struct timeval wait_interval = {
26b15f
+	.tv_sec	= 1,
26b15f
+};
26b15f
 
26b15f
 /* Keeping track of external matches and targets.  */
26b15f
 static const struct option options[] = {
26b15f
-	{.name = "binary",   .has_arg = false, .val = 'b'},
26b15f
-	{.name = "counters", .has_arg = false, .val = 'c'},
26b15f
-	{.name = "verbose",  .has_arg = false, .val = 'v'},
26b15f
-	{.name = "test",     .has_arg = false, .val = 't'},
26b15f
-	{.name = "help",     .has_arg = false, .val = 'h'},
26b15f
-	{.name = "noflush",  .has_arg = false, .val = 'n'},
26b15f
-	{.name = "modprobe", .has_arg = true,  .val = 'M'},
26b15f
-	{.name = "table",    .has_arg = true,  .val = 'T'},
26b15f
+	{.name = "binary",        .has_arg = 0, .val = 'b'},
26b15f
+	{.name = "counters",      .has_arg = 0, .val = 'c'},
26b15f
+	{.name = "verbose",       .has_arg = 0, .val = 'v'},
26b15f
+	{.name = "test",          .has_arg = 0, .val = 't'},
26b15f
+	{.name = "help",          .has_arg = 0, .val = 'h'},
26b15f
+	{.name = "noflush",       .has_arg = 0, .val = 'n'},
26b15f
+	{.name = "modprobe",      .has_arg = 1, .val = 'M'},
26b15f
+	{.name = "table",         .has_arg = 1, .val = 'T'},
26b15f
+	{.name = "wait",          .has_arg = 2, .val = 'w'},
26b15f
+	{.name = "wait-interval", .has_arg = 2, .val = 'W'},
26b15f
 	{NULL},
26b15f
 };
26b15f
 
26b15f
@@ -44,14 +51,16 @@ static void print_usage(const char *name
26b15f
 
26b15f
 static void print_usage(const char *name, const char *version)
26b15f
 {
26b15f
-	fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
26b15f
+	fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h] [-w secs] [-W usecs]\n"
26b15f
 			"	   [ --binary ]\n"
26b15f
 			"	   [ --counters ]\n"
26b15f
 			"	   [ --verbose ]\n"
26b15f
 			"	   [ --test ]\n"
26b15f
 			"	   [ --help ]\n"
26b15f
+			"	   [ --wait=<seconds>\n"
26b15f
+			"	   [ --wait-interval=<usecs>\n"
26b15f
 			"	   [ --noflush ]\n"
26b15f
-			"          [ --modprobe=<command>]\n", name);
26b15f
+			"	   [ --modprobe=<command>]\n", name);
26b15f
 
26b15f
 	exit(1);
26b15f
 }
26b15f
@@ -182,7 +191,7 @@ int ip6tables_restore_main(int argc, cha
26b15f
 {
26b15f
 	struct xtc_handle *handle = NULL;
26b15f
 	char buffer[10240];
26b15f
-	int c;
26b15f
+	int c, lock;
26b15f
 	char curtable[XT_TABLE_MAXNAMELEN + 1];
26b15f
 	FILE *in;
26b15f
 	int in_table = 0, testing = 0;
26b15f
@@ -190,6 +199,7 @@ int ip6tables_restore_main(int argc, cha
26b15f
 	const struct xtc_ops *ops = &ip6tc_ops;
26b15f
 
26b15f
 	line = 0;
26b15f
+	lock = XT_LOCK_NOT_ACQUIRED;
26b15f
 
26b15f
 	ip6tables_globals.program_name = "ip6tables-restore";
26b15f
 	c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
26b15f
@@ -204,7 +214,7 @@ int ip6tables_restore_main(int argc, cha
26b15f
 	init_extensions6();
26b15f
 #endif
26b15f
 
26b15f
-	while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
26b15f
+	while ((c = getopt_long(argc, argv, "bcvthnwWM:T:", options, NULL)) != -1) {
26b15f
 		switch (c) {
26b15f
 			case 'b':
26b15f
 				binary = 1;
26b15f
@@ -225,6 +235,12 @@ int ip6tables_restore_main(int argc, cha
26b15f
 			case 'n':
26b15f
 				noflush = 1;
26b15f
 				break;
26b15f
+			case 'w':
26b15f
+				wait = parse_wait_time(argc, argv);
26b15f
+				break;
26b15f
+			case 'W':
26b15f
+				parse_wait_interval(argc, argv, &wait_interval);
26b15f
+				break;
26b15f
 			case 'M':
26b15f
 				xtables_modprobe_program = optarg;
26b15f
 				break;
26b15f
@@ -269,8 +285,23 @@ int ip6tables_restore_main(int argc, cha
26b15f
 				DEBUGP("Not calling commit, testing\n");
26b15f
 				ret = 1;
26b15f
 			}
26b15f
+
26b15f
+			/* Done with the current table, release the lock. */
26b15f
+			if (lock >= 0) {
26b15f
+				xtables_unlock(lock);
26b15f
+				lock = XT_LOCK_NOT_ACQUIRED;
26b15f
+			}
26b15f
+
26b15f
 			in_table = 0;
26b15f
 		} else if ((buffer[0] == '*') && (!in_table)) {
26b15f
+			/* Acquire a lock before we create a new table handle */
26b15f
+			lock = xtables_lock(wait, &wait_interval);
26b15f
+			if (lock == XT_LOCK_BUSY) {
26b15f
+				fprintf(stderr, "Another app is currently holding the xtables lock. "
26b15f
+					"Perhaps you want to use the -w option?\n");
26b15f
+				exit(RESOURCE_PROBLEM);
26b15f
+			}
26b15f
+
26b15f
 			/* New table */
26b15f
 			char *table;
26b15f
 
26b15f
diff -up iptables-1.4.21/iptables/iptables.c.restore_support_acquiring_the_lock iptables-1.4.21/iptables/iptables.c
26b15f
--- iptables-1.4.21/iptables/iptables.c.restore_support_acquiring_the_lock	2017-04-05 14:55:52.562008872 +0200
26b15f
+++ iptables-1.4.21/iptables/iptables.c	2017-04-05 14:55:52.564008888 +0200
26b15f
@@ -1754,7 +1754,7 @@ int do_command4(int argc, char *argv[],
26b15f
 	generic_opt_check(command, cs.options);
26b15f
 
26b15f
 	/* Attempt to acquire the xtables lock */
26b15f
-	if (!restore && !xtables_lock(wait, &wait_interval)) {
26b15f
+	if (!restore && xtables_lock(wait, &wait_interval) == XT_LOCK_BUSY) {
26b15f
 		fprintf(stderr, "Another app is currently holding the xtables lock. ");
26b15f
 		if (wait == 0)
26b15f
 			fprintf(stderr, "Perhaps you want to use the -w option?\n");
26b15f
diff -up iptables-1.4.21/iptables/iptables-restore.c.restore_support_acquiring_the_lock iptables-1.4.21/iptables/iptables-restore.c
26b15f
--- iptables-1.4.21/iptables/iptables-restore.c.restore_support_acquiring_the_lock	2013-11-22 12:18:13.000000000 +0100
26b15f
+++ iptables-1.4.21/iptables/iptables-restore.c	2017-04-05 15:00:17.389179935 +0200
26b15f
@@ -12,6 +12,7 @@
26b15f
 #include <stdio.h>
26b15f
 #include <stdlib.h>
26b15f
 #include "iptables.h"
26b15f
+#include "xshared.h"
26b15f
 #include "xtables.h"
26b15f
 #include "libiptc/libiptc.h"
26b15f
 #include "iptables-multi.h"
26b15f
@@ -22,18 +23,24 @@
26b15f
 #define DEBUGP(x, args...)
26b15f
 #endif
26b15f
 
26b15f
-static int binary = 0, counters = 0, verbose = 0, noflush = 0;
26b15f
+static int binary = 0, counters = 0, verbose = 0, noflush = 0, wait = 0;
26b15f
+
26b15f
+static struct timeval wait_interval = {
26b15f
+	.tv_sec	= 1,
26b15f
+};
26b15f
 
26b15f
 /* Keeping track of external matches and targets.  */
26b15f
 static const struct option options[] = {
26b15f
-	{.name = "binary",   .has_arg = false, .val = 'b'},
26b15f
-	{.name = "counters", .has_arg = false, .val = 'c'},
26b15f
-	{.name = "verbose",  .has_arg = false, .val = 'v'},
26b15f
-	{.name = "test",     .has_arg = false, .val = 't'},
26b15f
-	{.name = "help",     .has_arg = false, .val = 'h'},
26b15f
-	{.name = "noflush",  .has_arg = false, .val = 'n'},
26b15f
-	{.name = "modprobe", .has_arg = true,  .val = 'M'},
26b15f
-	{.name = "table",    .has_arg = true,  .val = 'T'},
26b15f
+	{.name = "binary",        .has_arg = 0, .val = 'b'},
26b15f
+	{.name = "counters",      .has_arg = 0, .val = 'c'},
26b15f
+	{.name = "verbose",       .has_arg = 0, .val = 'v'},
26b15f
+	{.name = "test",          .has_arg = 0, .val = 't'},
26b15f
+	{.name = "help",          .has_arg = 0, .val = 'h'},
26b15f
+	{.name = "noflush",       .has_arg = 0, .val = 'n'},
26b15f
+	{.name = "modprobe",      .has_arg = 1, .val = 'M'},
26b15f
+	{.name = "table",         .has_arg = 1, .val = 'T'},
26b15f
+	{.name = "wait",          .has_arg = 2, .val = 'w'},
26b15f
+	{.name = "wait-interval", .has_arg = 2, .val = 'W'},
26b15f
 	{NULL},
26b15f
 };
26b15f
 
26b15f
@@ -43,15 +50,17 @@ static void print_usage(const char *name
26b15f
 
26b15f
 static void print_usage(const char *name, const char *version)
26b15f
 {
26b15f
-	fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
26b15f
+	fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h] [-W usecs]\n"
26b15f
 			"	   [ --binary ]\n"
26b15f
 			"	   [ --counters ]\n"
26b15f
 			"	   [ --verbose ]\n"
26b15f
 			"	   [ --test ]\n"
26b15f
 			"	   [ --help ]\n"
26b15f
 			"	   [ --noflush ]\n"
26b15f
+			"	   [ --wait=<seconds>\n"
26b15f
+			"	   [ --wait-interval=<usecs>\n"
26b15f
 			"	   [ --table= ]\n"
26b15f
-			"          [ --modprobe=<command>]\n", name);
26b15f
+			"	   [ --modprobe=<command>]\n", name);
26b15f
 
26b15f
 	exit(1);
26b15f
 }
26b15f
@@ -182,7 +191,7 @@ iptables_restore_main(int argc, char *ar
26b15f
 {
26b15f
 	struct xtc_handle *handle = NULL;
26b15f
 	char buffer[10240];
26b15f
-	int c;
26b15f
+	int c, lock;
26b15f
 	char curtable[XT_TABLE_MAXNAMELEN + 1];
26b15f
 	FILE *in;
26b15f
 	int in_table = 0, testing = 0;
26b15f
@@ -190,6 +199,7 @@ iptables_restore_main(int argc, char *ar
26b15f
 	const struct xtc_ops *ops = &iptc_ops;
26b15f
 
26b15f
 	line = 0;
26b15f
+	lock = XT_LOCK_NOT_ACQUIRED;
26b15f
 
26b15f
 	iptables_globals.program_name = "iptables-restore";
26b15f
 	c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
26b15f
@@ -204,7 +214,7 @@ iptables_restore_main(int argc, char *ar
26b15f
 	init_extensions4();
26b15f
 #endif
26b15f
 
26b15f
-	while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
26b15f
+	while ((c = getopt_long(argc, argv, "bcvthnwWM:T:", options, NULL)) != -1) {
26b15f
 		switch (c) {
26b15f
 			case 'b':
26b15f
 				binary = 1;
26b15f
@@ -225,6 +235,12 @@ iptables_restore_main(int argc, char *ar
26b15f
 			case 'n':
26b15f
 				noflush = 1;
26b15f
 				break;
26b15f
+			case 'w':
26b15f
+				wait = parse_wait_time(argc, argv);
26b15f
+				break;
26b15f
+			case 'W':
26b15f
+				parse_wait_interval(argc, argv, &wait_interval);
26b15f
+				break;
26b15f
 			case 'M':
26b15f
 				xtables_modprobe_program = optarg;
26b15f
 				break;
26b15f
@@ -269,8 +285,23 @@ iptables_restore_main(int argc, char *ar
26b15f
 				DEBUGP("Not calling commit, testing\n");
26b15f
 				ret = 1;
26b15f
 			}
26b15f
+
26b15f
+			/* Done with the current table, release the lock. */
26b15f
+			if (lock >= 0) {
26b15f
+				xtables_unlock(lock);
26b15f
+				lock = XT_LOCK_NOT_ACQUIRED;
26b15f
+			}
26b15f
+
26b15f
 			in_table = 0;
26b15f
 		} else if ((buffer[0] == '*') && (!in_table)) {
26b15f
+			/* Acquire a lock before we create a new table handle */
26b15f
+			lock = xtables_lock(wait, &wait_interval);
26b15f
+			if (lock == XT_LOCK_BUSY) {
26b15f
+				fprintf(stderr, "Another app is currently holding the xtables lock. "
26b15f
+					"Perhaps you want to use the -w option?\n");
26b15f
+				exit(RESOURCE_PROBLEM);
26b15f
+			}
26b15f
+
26b15f
 			/* New table */
26b15f
 			char *table;
26b15f
 
26b15f
diff -up iptables-1.4.21/iptables/xshared.c.restore_support_acquiring_the_lock iptables-1.4.21/iptables/xshared.c
26b15f
--- iptables-1.4.21/iptables/xshared.c.restore_support_acquiring_the_lock	2017-04-05 14:55:52.562008872 +0200
26b15f
+++ iptables-1.4.21/iptables/xshared.c	2017-04-05 14:55:52.565008896 +0200
26b15f
@@ -246,7 +246,7 @@ void xs_init_match(struct xtables_match
26b15f
 		match->init(match->m);
26b15f
 }
26b15f
 
26b15f
-bool xtables_lock(int wait, struct timeval *wait_interval)
26b15f
+int xtables_lock(int wait, struct timeval *wait_interval)
26b15f
 {
26b15f
 	struct timeval time_left, wait_time;
26b15f
 	int fd, i = 0;
26b15f
@@ -256,22 +256,22 @@ bool xtables_lock(int wait, struct timev
26b15f
 
26b15f
 	fd = open(XT_LOCK_NAME, O_CREAT, 0600);
26b15f
 	if (fd < 0)
26b15f
-		return true;
26b15f
+		return XT_LOCK_UNSUPPORTED;
26b15f
 
26b15f
 	if (wait == -1) {
26b15f
 		if (flock(fd, LOCK_EX) == 0)
26b15f
-			return true;
26b15f
+			return fd;
26b15f
 
26b15f
 		fprintf(stderr, "Can't lock %s: %s\n", XT_LOCK_NAME,
26b15f
 			strerror(errno));
26b15f
-		return false;
26b15f
+		return XT_LOCK_BUSY;
26b15f
 	}
26b15f
 
26b15f
 	while (1) {
26b15f
 		if (flock(fd, LOCK_EX | LOCK_NB) == 0)
26b15f
-			return true;
26b15f
+			return fd;
26b15f
 		else if (timercmp(&time_left, wait_interval, <))
26b15f
-			return false;
26b15f
+			return XT_LOCK_BUSY;
26b15f
 
26b15f
 		if (++i % 10 == 0) {
26b15f
 			fprintf(stderr, "Another app is currently holding the xtables lock; "
26b15f
@@ -285,6 +285,12 @@ bool xtables_lock(int wait, struct timev
26b15f
 	}
26b15f
 }
26b15f
 
26b15f
+void xtables_unlock(int lock)
26b15f
+{
26b15f
+	if (lock >= 0)
26b15f
+		close(lock);
26b15f
+}
26b15f
+
26b15f
 int parse_wait_time(int argc, char *argv[])
26b15f
 {
26b15f
 	int wait = -1;
26b15f
diff -up iptables-1.4.21/iptables/xshared.h.restore_support_acquiring_the_lock iptables-1.4.21/iptables/xshared.h
26b15f
--- iptables-1.4.21/iptables/xshared.h.restore_support_acquiring_the_lock	2017-04-05 14:55:52.562008872 +0200
26b15f
+++ iptables-1.4.21/iptables/xshared.h	2017-04-05 14:55:52.565008896 +0200
26b15f
@@ -84,7 +84,28 @@ extern struct xtables_match *load_proto(
26b15f
 extern int subcmd_main(int, char **, const struct subcommand *);
26b15f
 extern void xs_init_target(struct xtables_target *);
26b15f
 extern void xs_init_match(struct xtables_match *);
26b15f
-bool xtables_lock(int wait, struct timeval *wait_interval);
26b15f
+
26b15f
+/**
26b15f
+ * Values for the iptables lock.
26b15f
+ *
26b15f
+ * A value >= 0 indicates the lock filedescriptor. Other values are:
26b15f
+ *
26b15f
+ * XT_LOCK_UNSUPPORTED : The system does not support locking, execution will
26b15f
+ * proceed lockless.
26b15f
+ *
26b15f
+ * XT_LOCK_BUSY : The lock was held by another process. xtables_lock only
26b15f
+ * returns this value when |wait| == false. If |wait| == true, xtables_lock
26b15f
+ * will not return unless the lock has been acquired.
26b15f
+ *
26b15f
+ * XT_LOCK_NOT_ACQUIRED : We have not yet attempted to acquire the lock.
26b15f
+ */
26b15f
+enum {
26b15f
+	XT_LOCK_BUSY = -1,
26b15f
+	XT_LOCK_UNSUPPORTED  = -2,
26b15f
+	XT_LOCK_NOT_ACQUIRED  = -3,
26b15f
+};
26b15f
+extern int xtables_lock(int wait, struct timeval *tv);
26b15f
+extern void xtables_unlock(int lock);
26b15f
 
26b15f
 int parse_wait_time(int argc, char *argv[]);
26b15f
 void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval);