diff options
author | tb <> | 2015-12-07 02:38:54 +0000 |
---|---|---|
committer | tb <> | 2015-12-07 02:38:54 +0000 |
commit | 4d26f018548e418a929ddebcd02db00c053fd576 (patch) | |
tree | d9831e76399642a51694a54ec1ba8536d70a5cb9 | |
parent | f449a45336602f59009527a500289cd9d94a9e21 (diff) | |
download | openbsd-4d26f018548e418a929ddebcd02db00c053fd576.tar.gz openbsd-4d26f018548e418a929ddebcd02db00c053fd576.tar.bz2 openbsd-4d26f018548e418a929ddebcd02db00c053fd576.zip |
Get rid of modulo bias and replace the naive shuffle by the
Knuth-Fisher-Yates shuffle to make the random sequence of ports
less biased. Based on the implementation in sys/netinet/ip_id.c.
With helpful input from daniel@ and beck@
ok beck@ despite eye twitching
-rw-r--r-- | src/usr.bin/nc/netcat.c | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 4c3ed4e97f..cfc5a2363b 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: netcat.c,v 1.144 2015/11/23 01:23:56 bcook Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.145 2015/12/07 02:38:54 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
4 | * Copyright (c) 2015 Bob Beck. All rights reserved. | 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. |
@@ -58,7 +58,6 @@ | |||
58 | #include "atomicio.h" | 58 | #include "atomicio.h" |
59 | 59 | ||
60 | #define PORT_MAX 65535 | 60 | #define PORT_MAX 65535 |
61 | #define PORT_MAX_LEN 6 | ||
62 | #define UNIX_DG_TMP_SOCKET_SIZE 19 | 61 | #define UNIX_DG_TMP_SOCKET_SIZE 19 |
63 | 62 | ||
64 | #define POLL_STDIN 0 | 63 | #define POLL_STDIN 0 |
@@ -1289,25 +1288,22 @@ build_ports(char *p) | |||
1289 | lo = cp; | 1288 | lo = cp; |
1290 | } | 1289 | } |
1291 | 1290 | ||
1292 | /* Load ports sequentially. */ | 1291 | /* |
1293 | for (cp = lo; cp <= hi; cp++) { | 1292 | * Initialize portlist with a random permutation. Based on |
1294 | portlist[x] = calloc(1, PORT_MAX_LEN); | 1293 | * Knuth, as in ip_randomid() in sys/netinet/ip_id.c. |
1295 | if (portlist[x] == NULL) | 1294 | */ |
1296 | err(1, NULL); | ||
1297 | snprintf(portlist[x], PORT_MAX_LEN, "%d", cp); | ||
1298 | x++; | ||
1299 | } | ||
1300 | |||
1301 | /* Randomly swap ports. */ | ||
1302 | if (rflag) { | 1295 | if (rflag) { |
1303 | int y; | 1296 | for (x = 0; x <= hi - lo; x++) { |
1304 | char *c; | 1297 | cp = arc4random_uniform(x + 1); |
1305 | 1298 | portlist[x] = portlist[cp]; | |
1306 | for (x = 0; x <= (hi - lo); x++) { | 1299 | if (asprintf(&portlist[cp], "%d", x + lo) < 0) |
1307 | y = (arc4random() & 0xFFFF) % (hi - lo); | 1300 | err(1, "asprintf"); |
1308 | c = portlist[x]; | 1301 | } |
1309 | portlist[x] = portlist[y]; | 1302 | } else { /* Load ports sequentially. */ |
1310 | portlist[y] = c; | 1303 | for (cp = lo; cp <= hi; cp++) { |
1304 | if (asprintf(&portlist[x], "%d", cp) < 0) | ||
1305 | err(1, "asprintf"); | ||
1306 | x++; | ||
1311 | } | 1307 | } |
1312 | } | 1308 | } |
1313 | } else { | 1309 | } else { |