Skip to contentSkip to conversation

iptables: accept lock file name at runtime

PATCH Superseded Giuseppe Scrivano 2 participants 3 messages Jul 14, 2020 – Jul 15, 2020 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iptables: accept lock file name at runtime

allow users to override at runtime the lock file to use through the XTABLES_LOCKFILE environment variable.

It allows using iptables from a network namespace owned by an user that has no write access to XT_LOCK_NAME (by default under /run), and without setting up a new mount namespace.

$ XTABLES_LOCKFILE=/tmp/xtables unshare -rn iptables ...
---
 iptables/xshared.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
iptables/xshared.c +6 -1
index c1d1371a..291f1c4b 100644
@@ -248,13 +248,18 @@ void xs_init_match(struct xtables_match *match)
 
 static int xtables_lock(int wait, struct timeval *wait_interval)
 {
+	const *lock_file;
 	struct timeval time_left, wait_time;
 	int fd, i = 0;
 
 	time_left.tv_sec = wait;
 	time_left.tv_usec = 0;
 
-	fd = open(XT_LOCK_NAME, O_CREAT, 0600);
+	lock_file = getenv("XTABLES_LOCKFILE");
+	if (lock_file == NULL || lock_file[0] == '\0')
+		lock_file = XT_LOCK_NAME;
+
+	fd = open(lock_file, O_CREAT, 0600);
 	if (fd < 0) {
 		fprintf(stderr, "Fatal: can't open lock file %s: %s\n",
 			XT_LOCK_NAME, strerror(errno));
Signature · 1 line
2.26.2
reviewer
#1 lore raw

Hi,

Quoted 19 lines · Giuseppe Scrivano

allow users to override at runtime the lock file to use through the XTABLES_LOCKFILE environment variable.

It allows using iptables from a network namespace owned by an user that has no write access to XT_LOCK_NAME (by default under /run), and without setting up a new mount namespace.

$ XTABLES_LOCKFILE=/tmp/xtables unshare -rn iptables ...
---
 iptables/xshared.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
iptables/xshared.c +0 -0
index c1d1371a..291f1c4b 100644

iptables/xshared.c void xs_init_match(struct xtables_match *match) · line 251

Quoted 4 lines
iptables/xshared.c +1 -0
@@ -248,13 +248,18 @@ void xs_init_match(struct xtables_match *match)
 
 static int xtables_lock(int wait, struct timeval *wait_interval)
 {
+	const *lock_file;

This does not look right. Typo?

Cheers, Phil

#2 lore raw
Quoted 23 lines · Phil Sutter

Hi,

Quoted 20 lines · Giuseppe Scrivano

allow users to override at runtime the lock file to use through the XTABLES_LOCKFILE environment variable.

It allows using iptables from a network namespace owned by an user that has no write access to XT_LOCK_NAME (by default under /run), and without setting up a new mount namespace.

$ XTABLES_LOCKFILE=/tmp/xtables unshare -rn iptables ...
---
 iptables/xshared.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
iptables/xshared.c +0 -0
index c1d1371a..291f1c4b 100644
@@ -248,13 +248,18 @@ void xs_init_match(struct xtables_match *match)
 
Quoted 5 lines

iptables/xshared.c void xs_init_match(struct xtables_match *match) · line 251

Quoted 3 lines
iptables/xshared.c +1 -0
 static int xtables_lock(int wait, struct timeval *wait_interval)
 {
+	const *lock_file;

This does not look right. Typo?

yes sorry, I've messed it up. I'll send a v2.

Thanks,
Giuseppe