2018-02-03 16:01:47 +01:00
|
|
|
From: Matthias Gerstner <matthias.gerstner@suse.com>
|
|
|
|
Date: 2017-06-26T10:53:24+0000
|
|
|
|
|
|
|
|
- fix a locking issue of iptables-batch which can cause it to spuriously fail
|
|
|
|
when other programs modify the iptables rules in parallel (bnc#1045130).
|
|
|
|
This can especially affect SuSEfirewall2 during startup.
|
|
|
|
|
2018-02-03 15:28:48 +01:00
|
|
|
---
|
|
|
|
iptables/iptables-batch.c | 21 +++++++++++++++++++++
|
|
|
|
iptables/xshared.c | 8 +++++++-
|
|
|
|
2 files changed, 28 insertions(+), 1 deletion(-)
|
|
|
|
|
|
|
|
Index: iptables-1.6.2/iptables/iptables-batch.c
|
2017-06-29 13:37:40 +02:00
|
|
|
===================================================================
|
2018-02-03 15:28:48 +01:00
|
|
|
--- iptables-1.6.2.orig/iptables/iptables-batch.c
|
|
|
|
+++ iptables-1.6.2/iptables/iptables-batch.c
|
|
|
|
@@ -403,6 +403,27 @@ main(int argc, char *argv[])
|
|
|
|
tables[3].name = "raw";
|
2017-06-29 13:37:40 +02:00
|
|
|
tables[3].handle = NULL;
|
|
|
|
current_table = &tables[0];
|
|
|
|
+ /*
|
2018-02-03 15:28:48 +01:00
|
|
|
+ * We need to lock the complete batch processing against parallel
|
|
|
|
+ * modification by other processes. Otherwise, we can end up with
|
2017-06-29 13:37:40 +02:00
|
|
|
+ * EAGAIN errors.
|
|
|
|
+ *
|
2018-02-03 15:28:48 +01:00
|
|
|
+ * The do_command{4,6} function already locks itself, but the complete
|
|
|
|
+ * call sequence needs to be locked until the commit is performed.
|
2017-06-29 13:37:40 +02:00
|
|
|
+ *
|
2018-02-03 15:28:48 +01:00
|
|
|
+ * Sadly, the xtables_lock() implementation is not very cooperative.
|
|
|
|
+ * There is no unlock() equivalent. The lock file descriptor is smiply
|
|
|
|
+ * left open until the process exits. Thus, we would have deadlocks
|
|
|
|
+ * when calling do_command{4,6} the second time.
|
2017-06-29 13:37:40 +02:00
|
|
|
+ *
|
|
|
|
+ * To prevent this, part of this patch adds logic to avoid taking the
|
|
|
|
+ * lock a second time in the same process in xtables_lock()
|
|
|
|
+ */
|
2018-02-03 15:28:48 +01:00
|
|
|
+ const struct timeval wait_interval = {.tv_sec = 1};
|
|
|
|
+ if (!xtables_lock_or_exit(-1, &wait_interval)) {
|
2017-06-29 13:37:40 +02:00
|
|
|
+ fprintf(stderr, "failed to acquire the xtables lock\n");
|
|
|
|
+ exit(1);
|
|
|
|
+ }
|
2018-02-03 15:28:48 +01:00
|
|
|
|
2017-06-29 13:37:40 +02:00
|
|
|
while((r = getline(&iline, &llen, fp)) != -1)
|
|
|
|
{
|
2018-02-03 15:28:48 +01:00
|
|
|
Index: iptables-1.6.2/iptables/xshared.c
|
2017-06-29 13:37:40 +02:00
|
|
|
===================================================================
|
2018-02-03 15:28:48 +01:00
|
|
|
--- iptables-1.6.2.orig/iptables/xshared.c
|
|
|
|
+++ iptables-1.6.2/iptables/xshared.c
|
|
|
|
@@ -248,9 +248,13 @@ void xs_init_match(struct xtables_match
|
|
|
|
|
|
|
|
static int xtables_lock(int wait, struct timeval *wait_interval)
|
2017-06-29 13:37:40 +02:00
|
|
|
{
|
|
|
|
+ static bool already_locked = false;
|
2018-02-03 15:28:48 +01:00
|
|
|
struct timeval time_left, wait_time;
|
2017-06-29 13:37:40 +02:00
|
|
|
int fd, i = 0;
|
|
|
|
|
2018-02-03 15:28:48 +01:00
|
|
|
+ if (already_locked)
|
|
|
|
+ /* Avoid deadlocks, see iptables-batch.c */
|
2017-06-29 13:37:40 +02:00
|
|
|
+ return true;
|
|
|
|
time_left.tv_sec = wait;
|
|
|
|
time_left.tv_usec = 0;
|
|
|
|
|
2018-02-03 15:28:48 +01:00
|
|
|
@@ -262,8 +266,10 @@ static int xtables_lock(int wait, struct
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait == -1) {
|
|
|
|
- if (flock(fd, LOCK_EX) == 0)
|
|
|
|
+ if (flock(fd, LOCK_EX) == 0) {
|
2017-06-29 13:37:40 +02:00
|
|
|
+ already_locked = true;
|
2018-02-03 15:28:48 +01:00
|
|
|
return fd;
|
2017-06-29 13:37:40 +02:00
|
|
|
+ }
|
2018-02-03 15:28:48 +01:00
|
|
|
|
|
|
|
fprintf(stderr, "Can't lock %s: %s\n", XT_LOCK_NAME,
|
|
|
|
strerror(errno));
|