Skip to contentSkip to conversation

Incorrect xt_iprange boundary check for IPv6

PATCH In review Thomas Jacob 3 participants 6 messages Jan 22, 2011 – Jan 24, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Developed for and tested on 2.6.27.57, but applies and compiles in current mainline as well (haven't tested it there though).

See the following script for a demonstration of the problem:

#!/bin/sh
PREFIX=fc42:4242
LEFTOUT=$PREFIX:0:ffff:ffff:ffff:ffff:ffff
FROM=$PREFIX:1::0
MIDDLE=$PREFIX:1::8000:0:0:0
TILL=$PREFIX:1::ffff:ffff:ffff:ffff
RIGHTOUT=$PREFIX:2::0
SUBNET=$PREFIX:1::/64
SOURCE=fc23:2323::1
CHAIN=iprange_bug

ip6tables -S OUTPUT | fgrep -q -- '-A OUTPUT -j '"$CHAIN" \
	&& ip6tables -D OUTPUT -j $CHAIN

ip6tables -F $CHAIN 2>/dev/null
ip6tables -X $CHAIN 2>/dev/null
ip6tables -N $CHAIN

ip6tables -A $CHAIN -p icmpv6 --icmpv6-type echo-request -s $SOURCE -m iprange --dst-range $FROM-$TILL ip6tables -A $CHAIN -p icmpv6 --icmpv6-type echo-request -s $SOURCE -d $SUBNET -j DROP

ip6tables -I OUTPUT 1 -j $CHAIN

ip addr replace $SOURCE/128 dev lo
ip addr replace $LEFTOUT/128 dev lo
ip addr replace $FROM/128 dev lo
ip addr replace $MIDDLE/128 dev lo
ip addr replace $TILL/128 dev lo
ip addr replace $RIGHTOUT/128 dev lo

for IP in $LEFTOUT $FROM $MIDDLE $TILL $RIGHTOUT do
ping6 -c 1 -W 1 -q -I $SOURCE $IP | grep ^PING
done

echo
ip6tables -vnL $CHAIN

ip addr del $RIGHTOUT/128 dev lo
ip addr del $TILL/128 dev lo
ip addr del $MIDDLE/128 dev lo
ip addr del $FROM/128 dev lo
ip addr del $LEFTOUT/128 dev lo
ip addr del $SOURCE/128 dev lo

ip6tables -D OUTPUT -j $CHAIN
ip6tables -F $CHAIN
ip6tables -X $CHAIN

#1 lore raw

PATCH 1/1 Incorrect xt_iprange boundary check for IPv6

iprange_ipv6_sub was substracting 2 unsigned ints and then casting the result to int to find out whether they are lt, eq or gt each other, this doesn't work if the full 32 bits of each part can be used in IPv6 addresses. Patch should remedy that without significant performance penalties.

---
 net/netfilter/xt_iprange.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)
net/netfilter/xt_iprange.c +7 -9
index 4b5741b..3cc5d21 100644
@@ -96,15 +96,13 @@ iprange_mt4(const struct sk_buff *skb, const struct net_device *in,
 }
 
 static inline int
-iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
+iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
 {
 	unsigned int i;
-	int r;
 
 	for (i = 0; i < 4; ++i) {
-		r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
-		if (r != 0)
-			return r;
+		if(ntohl(a->s6_addr32[i]) != ntohl(b->s6_addr32[i]))
+			return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
 	}
 
 	return 0;
@@ -121,15 +119,15 @@ iprange_mt6(const struct sk_buff *skb, const struct net_device *in,
 	bool m;
 
 	if (info->flags & IPRANGE_SRC) {
-		m  = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
-		m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
+		m  = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
+		m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
 		m ^= !!(info->flags & IPRANGE_SRC_INV);
 		if (m)
 			return false;
 	}
 	if (info->flags & IPRANGE_DST) {
-		m  = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
-		m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
+		m  = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
+		m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
 		m ^= !!(info->flags & IPRANGE_DST_INV);
 		if (m)
 			return false;
Signature · 1 line
1.5.6.5
#2 lore raw
Quoted 5 lines · Thomas Jacob

iprange_ipv6_sub was substracting 2 unsigned ints and then casting the result to int to find out whether they are lt, eq or gt each other, this doesn't work if the full 32 bits of each part can be used in IPv6 addresses. Patch should remedy that without significant performance penalties.

Correctness before speed ;)

The algo change looks ok to me, thanks for spotting.

Quoted 16 lines

static inline int

net/netfilter/xt_iprange.c +1 -1
-iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
+iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
{
	unsigned int i;
net/netfilter/xt_iprange.c +0 -1
-	int r;
	for (i = 0; i < 4; ++i) {
net/netfilter/xt_iprange.c +2 -3
-		r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
-		if (r != 0)
-			return r;
+		if(ntohl(a->s6_addr32[i]) != ntohl(b->s6_addr32[i]))
+			return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
	}

	return 0;
#3 lore raw
In reply to Thomas Jacob

Hi Thomas,

Quoted 22 lines · Thomas Jacob

iprange_ipv6_sub was substracting 2 unsigned ints and then casting the result to int to find out whether they are lt, eq or gt each other, this doesn't work if the full 32 bits of each part can be used in IPv6 addresses. Patch should remedy that without significant performance penalties.

---
 net/netfilter/xt_iprange.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)
net/netfilter/xt_iprange.c +1 -1
index 4b5741b..3cc5d21 100644
@@ -96,15 +96,13 @@ iprange_mt4(const struct sk_buff *skb, const struct net_device *in,
 }
 
 static inline int
-iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
+iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
 {

net/netfilter/xt_iprange.c iprange_mt4(const struct sk_buff *skb, const struct net_device *in, · line 106

Quoted 10 lines
net/netfilter/xt_iprange.c +2 -4
 	unsigned int i;
-	int r;
 
 	for (i = 0; i < 4; ++i) {
-		r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
-		if (r != 0)
-			return r;
+		if(ntohl(a->s6_addr32[i]) != ntohl(b->s6_addr32[i]))
+			return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
 	}

Why do you convert to host order in the inequality test? It could be left out.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#4 lore raw
Quoted 7 lines · Jozsef Kadlecsik

net/netfilter/xt_iprange.c iprange_mt4(const struct sk_buff *skb, const struct net_device *in, · line 106

Quoted 4 lines
net/netfilter/xt_iprange.c +2 -1
-			return r;
+		if(ntohl(a->s6_addr32[i]) != ntohl(b->s6_addr32[i]))
+			return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
 	}

Why do you convert to host order in the inequality test? It could be left out.

Yes it can be, just a left over from the old code I suppose. Didn't really thing about that ;)

But if we are talking about performance, what about the following part:

if (info->flags & IPRANGE_SRC) {
m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6); m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr); m ^= !!(info->flags & IPRANGE_SRC_INV);
if (m)

    return false;
}

Shouldn't this be m ||= .. in the second call to iprange_ipv6_lt, to allow for some short circuit optimizations? Or in order to reduce the rather asymmetric behaviour then, shouldn't these two function calls be merged into something like

static inline int
iprange_ipv6_cmp(const struct in6_addr *a, const struct in6_addr *b,
const struct in6_addr *x )

{
        unsigned int i;

        for (i = 0; i < 4; ++i) {
                if(x->s6_addr32[i] != a->s6_addr32[i])
                  return ntohl(x->s6_addr32[i])<ntohl(a->s6_addr32[i]);
                if(x->s6_addr32[i] != b->s6_addr32[i])
                  return ntohl(b->s6_addr32[i])<ntohl(x->s6_addr32[i]);
               
        }

        return 0;
}

Or would that be bad from a 1st level caching point of view?

#5 lore raw
Quoted 10 lines · Thomas Jacob

if (info->flags & IPRANGE_SRC) {
m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6); m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr); m ^= !!(info->flags & IPRANGE_SRC_INV);
if (m)
return false;
}

Shouldn't this be m ||= ..

C does not have a ||= operator. This is not Perl.
Which is why either you make sure iprange_ipv6_lt returns a bool, or force one by means of !!.

Quoted 1 line

to allow for some short circuit optimizations?

Since there is no ||=, it would be

m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6); if (!m)

	m = iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);