Skip to content

Commit 8c404ef

Browse files
lynxisjow-
authored andcommitted
iptables.c: lock the xtables.lock
When using fw3 together with other applications or scripts a race conditions might occur. When fw3 is preparing the new tables, another application can use the executable `iptables` which modifies the kernel-tables. libxtables will notify this and fails when fw3 is committing the changes resulting in a failed firewall. Now waits in a while loop until the lock is gone, activate the lock itself and applies changes. To reproduce the bug the following two scripts should run in parrallel, after a few seconds the latter stop and leaves a broken firewall: while true; do iptables -N locking; done and while [ "$(iptables -w -L OUTPUT | wc -l)" -gt 2 ]; do fw3 reload; done The following message will appear Warning: iptc_commit(): Resource temporarily unavailable and connectivity is gone. Tested in an LXC and Qemu container. Signed-off-by: Alexander Couzens <[email protected]> [fixed waiting for unlock and commit message] Signed-off-by: Paul Spooren <[email protected]>
1 parent c1d3a4d commit 8c404ef

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

iptables.c

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
#include "iptables.h"
5757

58+
#define XT_LOCK_NAME "/var/run/xtables.lock"
59+
static int xt_lock_fd = -1;
5860

5961
struct fw3_ipt_rule {
6062
struct fw3_ipt_handle *h;
@@ -168,6 +170,11 @@ fw3_ipt_open(enum fw3_family family, enum fw3_table table)
168170

169171
xtables_init();
170172

173+
while (!fw3_lock_path(&xt_lock_fd, XT_LOCK_NAME)) {
174+
warn("Currently busy xtables.lock - wait 1 second");
175+
sleep(1);
176+
}
177+
171178
if (family == FW3_FAMILY_V6)
172179
{
173180
#ifndef DISABLE_IPV6
@@ -192,6 +199,7 @@ fw3_ipt_open(enum fw3_family family, enum fw3_table table)
192199
if (!h->handle)
193200
{
194201
free(h);
202+
fw3_unlock_path(&xt_lock_fd, XT_LOCK_NAME);
195203
return NULL;
196204
}
197205

@@ -561,6 +569,7 @@ fw3_ipt_commit(struct fw3_ipt_handle *h)
561569
void
562570
fw3_ipt_close(struct fw3_ipt_handle *h)
563571
{
572+
fw3_unlock_path(&xt_lock_fd, XT_LOCK_NAME);
564573
free(h);
565574
}
566575

0 commit comments

Comments
 (0)