Skip to contentSkip to conversation

udp: port starting location not random

PATCH In review Stephen Hemminger 4 participants 8 messages Oct 4, 2012

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

PATCH 1/1 udp: port starting location not random

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c +1 -1
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;
 		/*
 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
 		 */
maintainer
#1 lore raw

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 29 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 5 lines
net/ipv4/udp.c +1 -1
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

#2 lore raw
Quoted 34 lines · On Thu, 04 Oct 2012 17:12:46 -0400 (EDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 31 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c +0 -0
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
Quoted 6 lines

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 3 lines
net/ipv4/udp.c +1 -1
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

The standalone program gets same result.

#3 lore raw
Quoted 37 lines · Stephen Hemminger
Quoted 34 lines · On Thu, 04 Oct 2012 17:12:46 -0400 (EDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 31 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c +0 -0
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
Quoted 8 lines
Quoted 6 lines

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 3 lines
net/ipv4/udp.c +1 -1
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

The standalone program gets same result.

Hey, I hope you understand random32() does allocate a 32bit value, not a 15bit one...

If really we had such a bug, I am pretty sure we would have noticed.

maintainer
#4 lore raw

From: Eric Dumazet <[email protected]>
Date: Thu, 04 Oct 2012 23:45:53 +0200

Quoted 45 lines · Stephen Hemminger
Quoted 44 lines
Quoted 40 lines · On Thu, 04 Oct 2012 17:12:46 -0400 (EDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 29 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 5 lines
net/ipv4/udp.c +1 -1
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

Quoted 6 lines
Quoted 1 line

The standalone program gets same result.

Hey, I hope you understand random32() does allocate a 32bit value, not a 15bit one...

If really we had such a bug, I am pretty sure we would have noticed.

But the issue is is "remaining" that's of limited range, not rand. I didn't say to shift up 'rand', but rather 'remaining'.

#5 lore raw
Quoted 50 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Thu, 04 Oct 2012 23:45:53 +0200

Quoted 47 lines · Stephen Hemminger
Quoted 45 lines
Quoted 34 lines · On Thu, 04 Oct 2012 17:12:46 -0400 (EDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 31 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c +0 -0
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
Quoted 6 lines

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 3 lines
net/ipv4/udp.c +1 -1
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

The standalone program gets same result.

Quoted 7 lines
Quoted 4 lines

Hey, I hope you understand random32() does allocate a 32bit value, not a 15bit one...

If really we had such a bug, I am pretty sure we would have noticed.

But the issue is is "remaining" that's of limited range, not rand. I didn't say to shift up 'rand', but rather 'remaining'.

Sorry I see no issue.

remaining is the delta between high and low, and its right, unless your compiler or cpu has a nice bug.

remaining = (high - low) + 1;

And I see no bug at all here.

The only assumption is that rand is a random number between 0 and 0xFFFFFFFF

$RANDOM is between 0 and 0x7FFF

Thats the difference

#6 lore raw

Nevermind, it depends on random being big enough. This shows that it works with large enough random(). The hash I was getting from VXLAN was too small...

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(int ac, char **av) {
	int low, high, remaining;
	unsigned int rand;
	unsigned short first;
	
	low = atoi(av[1]);
	high = atoi(av[2]);
	remaining = (high - low) + 1;
	srandom(getpid());
	rand = (uint32_t) random();
	
	first = (((uint64_t)rand * remaining) >> 32) + low;

	printf("%d %d %u => %u\n", low, high, rand, first);
	return 0;
}
#7 lore raw
In reply to David Miller
Quoted 50 lines · On Thu, 04 Oct 2012 17:50:09 -0400 (EDT) David Miller <[email protected]>…

From: Eric Dumazet <[email protected]>
Date: Thu, 04 Oct 2012 23:45:53 +0200

Quoted 47 lines · Stephen Hemminger
Quoted 45 lines
Quoted 34 lines · On Thu, 04 Oct 2012 17:12:46 -0400 (EDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Thu, 4 Oct 2012 14:08:28 -0700

Quoted 31 lines

While working on VXLAN, noticed a bug in UDP introduced by:

commit 9088c5609584684149f3fb5b065aa7f18dcb03ff

Author: Eric Dumazet <[email protected]>
Date:   Wed Oct 8 11:44:17 2008 -0700

udp: Improve port randomization

The logic for choosing where to start for port randomization incorrectly calculates the starting port number. It is always ends up using the low end of the range independent of the value of random. This causes all UDP port searches to start at the same port.

Doing the following fixes it but at the cost of doing a real divide.

---
Resend, previous send was not going to netdev.

Not sure if worth fixing for stable, because only has performance impact and some application might be depending on current broken behaviour.

net/ipv4/udp.c +0 -0
@@ -216,7 +216,7 @@ int udp_lib_get_port(struct sock *sk, un
 		remaining = (high - low) + 1;
 
Quoted 6 lines

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, un · line 219

Quoted 3 lines
net/ipv4/udp.c +1 -1
 		rand = net_random();
-		first = (((u64)rand * remaining) >> 32) + low;
+		first = rand % remaining + low;

Try replacing "remaining" with "(remaining << (64 - 16))" in the expression instead.

The standalone program gets same result.

Quoted 7 lines
Quoted 4 lines

Hey, I hope you understand random32() does allocate a 32bit value, not a 15bit one...

If really we had such a bug, I am pretty sure we would have noticed.

But the issue is is "remaining" that's of limited range, not rand. I didn't say to shift up 'rand', but rather 'remaining'.

I think the issue is the Gcc is deciding to truncate the math. If it is done as separate steps, then the right answer happens:

Good:

	t = ((uint64_t) rand * remaining) >> 32;
	first = t + low;

Bad:

	first = (((uint64_t)rand * remaining) >> (64-16)) + low;