Re: Patch for random source ports with map rules - bug in random port selection?

Darren Reed <[email protected]>
Newsgroups gmane.comp.security.firewalls.ipfilter
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
 
Manuel Kasper wrote:
| Hello Darren,
|
| thanks for the quick source port randomization patch!
|
| While trying to apply it to m0n0wall today, two things struck me as 
odd with the following code:
|
|> !                 port = ipf_random() % (ntohs(np->in_pmax) -
|> !                                ntohs(np->in_pmin));
|
|
| 1. np->in_pmin isn't added to the generated random number, which means 
that the result will be
|    between 0 and (in_pmax - in_pmin - 1), rather than between in_pmin 
and in_pmax (inclusive).
|
| 2. No htons() is done on the result, and thus the resulting port 
numbers don't turn out as expected.
|
| A quick test with a map rule port range of 6000:7000 resulted in port 
numbers like the following (as seen on the network):
|     26114, 65282, 48898, 12033, 51459, 32258, ...
| all of which, when byte-swapped, are < 1000.
|
| The fix seems to be as follows:
|
| port = htons((ipf_random() %
|           (ntohs(np->in_pmax) - ntohs(np->in_pmin) + 1))
|           + ntohs(np->in_pmin));

That's what happens when you don't get enough sleep.

Darren

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
 
iEYEARECAAYFAkiLbmUACgkQP7JIXtvLbFUTkQCgpZ1aGUM+yRHoEE6V31lGjBNw
G5kAn3ReH15J7eiDK9Dkmf4yeT51CdLM
=oUlC
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.