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

Manuel Kasper <[email protected]>
Newsgroups gmane.comp.security.firewalls.ipfilter
Message-ID <[email protected]>
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));

Regards,

Manuel
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.