Skip to contentSkip to conversation

conntrack: Fix CIDR to mask conversion on Big Endian

PATCH Superseded Phil Sutter 3 participants 5 messages Sep 2, 2019 – Sep 4, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 conntrack: Fix CIDR to mask conversion on Big Endian

Code assumed host architecture to be Little Endian. Instead produce a proper mask by pushing the set bits into most significant position and apply htonl() on the result.

Fixes 3f6a2e90936bb ("conntrack: add support for CIDR notation")
---
 src/conntrack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/conntrack.c +1 -1
index c980a13f33d2c..baafcbd869c12 100644
@@ -2210,7 +2210,7 @@ nfct_build_netmask(uint32_t *dst, int b, int n)
 			dst[i] = 0xffffffff;
 			b -= 32;
 		} else if (b > 0) {
-			dst[i] = (1 << b) - 1;
+			dst[i] = htonl(((1 << b) - 1) << (32 - b));
 			b = 0;
 		} else {
 			dst[i] = 0;
Signature · 1 line
2.22.0
#1 lore raw
Quoted 15 lines · Phil Sutter

Code assumed host architecture to be Little Endian. Instead produce a proper mask by pushing the set bits into most significant position and apply htonl() on the result.

Fixes 3f6a2e90936bb ("conntrack: add support for CIDR notation")
---
 src/conntrack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/conntrack.c +0 -0
index c980a13f33d2c..baafcbd869c12 100644

src/conntrack.c nfct_build_netmask(uint32_t *dst, int b, int n) · line 2213

Quoted 5 lines
src/conntrack.c +1 -1
@@ -2210,7 +2210,7 @@ nfct_build_netmask(uint32_t *dst, int b, int n)
 			dst[i] = 0xffffffff;
 			b -= 32;
 		} else if (b > 0) {
-			dst[i] = (1 << b) - 1;
+			dst[i] = htonl(((1 << b) - 1) << (32 - b));

Simply this instead?

                        dst[i] = htonl(((1 << b) - 1);
Quoted 6 lines
src/conntrack.c +0 -0
 			b = 0;
 		} else {
 			dst[i] = 0;
Signature · 1 line
2.22.0
author reviewer
#2 lore raw
Quoted 24 lines · Pablo Neira Ayuso
Quoted 15 lines · Phil Sutter

Code assumed host architecture to be Little Endian. Instead produce a proper mask by pushing the set bits into most significant position and apply htonl() on the result.

Fixes 3f6a2e90936bb ("conntrack: add support for CIDR notation")
---
 src/conntrack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/conntrack.c +0 -0
index c980a13f33d2c..baafcbd869c12 100644

src/conntrack.c nfct_build_netmask(uint32_t *dst, int b, int n) · line 2213

Quoted 5 lines
src/conntrack.c +1 -1
@@ -2210,7 +2210,7 @@ nfct_build_netmask(uint32_t *dst, int b, int n)
 			dst[i] = 0xffffffff;
 			b -= 32;
 		} else if (b > 0) {
-			dst[i] = (1 << b) - 1;
+			dst[i] = htonl(((1 << b) - 1) << (32 - b));

Simply this instead?

Quoted 1 line
                        dst[i] = htonl(((1 << b) - 1);

You got me confused, so I played with different options. To see the results, I used:

| union {
|         uint32_t i;
|         char b[4];
| } u;

What we need in b is 'ff ff ff 00' for a prefix length of 24. Your suggested alternative does not compile, so I tried both options for the closing brace:

| htonl((1 << 24) - 1)

This turns into '00 ff ff ff' for both LE and BE, the opposite of what we need.

| htonl((1 << 24)) - 1

This turns into '00 00 00 00' on LE and '00 ff ff ff' on BE.

My code leads to correct result on either architecture and I don't see a simpler way of doing it.

Cheers, Phil

maintainer
#3 lore raw
Quoted 9 lines · Phil Sutter

What we need in b is 'ff ff ff 00' for a prefix length of 24. Your suggested alternative does not compile, so I tried both options for the closing brace:

| htonl((1 << 24) - 1)

This turns into '00 ff ff ff' for both LE and BE, the opposite of what we need.

Quoted 6 lines

| htonl((1 << 24)) - 1

This turns into '00 00 00 00' on LE and '00 ff ff ff' on BE.

My code leads to correct result on either architecture and I don't see a simpler way of doing it.

htonl(~0u << (32 - i)) would work, assuming i > 0 and <= 32.

author reviewer
#4 lore raw

Hi,

Quoted 10 lines · Florian Westphal
Quoted 9 lines · Phil Sutter

What we need in b is 'ff ff ff 00' for a prefix length of 24. Your suggested alternative does not compile, so I tried both options for the closing brace:

| htonl((1 << 24) - 1)

This turns into '00 ff ff ff' for both LE and BE, the opposite of what we need.

Quoted 8 lines
Quoted 6 lines

| htonl((1 << 24)) - 1

This turns into '00 00 00 00' on LE and '00 ff ff ff' on BE.

My code leads to correct result on either architecture and I don't see a simpler way of doing it.

htonl(~0u << (32 - i)) would work, assuming i > 0 and <= 32.

Ah, indeed! Left-shifting all-ones didn't come to mind. I'll send a v2.

Thanks, Phil