Skip to contentSkip to conversation

libxtables: fix wrong naddr when using localhost

PATCH In review Alexander Alemayhu 2 participants 3 messages Mar 7, 2017 – Mar 8, 2017 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxtables: fix wrong naddr when using localhost

$ iptables-translate -A INPUT -p tcp -s localhost --dport 8000 -j ACCEPT

gives

nft add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept

with this patch we get

nft add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept

---
 libxtables/xtables.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libxtables/xtables.c +1 -1
index d43f97066ea9..defc57414de3 100644
@@ -1376,7 +1376,7 @@ static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
 		return NULL;
 	} else {
 		for (p = res; p != NULL; p = p->ai_next)
-			++*naddr;
+			*naddr++;
 		addr = xtables_calloc(*naddr, sizeof(struct in_addr));
 		for (i = 0, p = res; p != NULL; p = p->ai_next)
 			memcpy(&addr[i++],
Signature · 1 line
2.9.3
#1 lore raw
Quoted 10 lines · Alexander Alemayhu
$ iptables-translate -A INPUT -p tcp -s localhost --dport 8000 -j ACCEPT

gives

nft add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept

with this patch we get

nft add rule ip filter INPUT ip saddr 127.0.0.1 tcp dport 8000 counter accept

This is actually a generic problem:

# iptables -I INPUT -p tcp -s localhost

results in:

# iptables-save 
# Generated by iptables-save v1.6.1 on Wed Mar  8 10:53:07 2017
*filter
:INPUT ACCEPT [13:1628]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:317]
-A INPUT -s 127.0.0.1/32 -p tcp
-A INPUT -s 127.0.0.1/32 -p tcp
COMMIT
# Completed on Wed Mar  8 10:53:07 2017

Original problem was introduce at:

commit 2d2b5e046aa56a518160716a9ddf9df53fc79c1f

Author: Arpan Kapoor <[email protected]>
Date:   Thu Mar 17 18:27:19 2016 +0530

libxtables: Replace gethostbyname() with getaddrinfo()

Did you also run iptables tests? See iptables-test.py, although I guess our test infrastructure is not catching up this case.

Thanks!

#2 lore raw
Quoted 15 lines · Pablo Neira Ayuso

This is actually a generic problem:

# iptables -I INPUT -p tcp -s localhost

results in:

# iptables-save 
# Generated by iptables-save v1.6.1 on Wed Mar  8 10:53:07 2017
*filter
:INPUT ACCEPT [13:1628]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:317]
(quoted diff) +0 -2
-A INPUT -s 127.0.0.1/32 -p tcp
-A INPUT -s 127.0.0.1/32 -p tcp
Quoted 3 lines

COMMIT
# Completed on Wed Mar 8 10:53:07 2017

What's the right way to fix this? I want to use iptables-translate in a web application[0] so I can copy and use the translations. Adding the extra line forces me to manually edit the output.

Quoted 11 lines

Original problem was introduce at:

commit 2d2b5e046aa56a518160716a9ddf9df53fc79c1f

Author: Arpan Kapoor <[email protected]>
Date:   Thu Mar 17 18:27:19 2016 +0530

libxtables: Replace gethostbyname() with getaddrinfo()

Did you also run iptables tests? See iptables-test.py, although I guess our test infrastructure is not catching up this case.

Before and after patch I get:
102 test files, 706 unit tests, 687 passed

Thanks.

[0]: https://2nft.alemayhu.com/

Signature · 3 lines
Mit freundlichen Grüßen

Alexander Alemayhu