Blame SOURCES/iptables-1.4.21-restore_support_acquiring_the_lock.patch

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