Subject: Re: UDPv4 port allocation problem

From: Rick Jones <[email-masked]>
Date: Thu, 23 Aug 2007 10:04:23 -0700

Tóth László Attila wrote:
> Hello,
> 
> I noticed that it is possible that the kernel allocates the same UDP

_Which_ kernel - or rather which rev?  There are lots of linux kernels 
potentially out there...

> port to an application that was used and closed immediately before the
> new application got it. This means that applications that do not specify
> an exact port and rely on the  kernel to allocate a port for them might
> see traffic originally meant for another application.
> 
> Imagine that two applications want to resolve a name in DNS at about the
> same time. The following happens:
>  * first app sends out the DNS query then closes the socket without
> waiting for an answer (e.g. it got interrupted by Ctrl+C)
>  * second app opens an UDP socket, and gets the same port, originally
> assigned to app#1, sends out the DNS query
>  * DNS server responds, the response goes to app#2
> 
> DNS might not be the perfect example, but you get the idea. 
> Applications do not expect to receive data on newly opened sockets, not
> to mention the security implications.

Actually, all applications using UDP are required to cope with just about 
anything since there are no guarantees with UDP of anything other than the 
checksum generally protecting one from corrupt data.

In the specific case of DNS, the resolver library will (damn well better) be 
checking the answer it gets against the query it sent.  There will be a 
transaction ID check, and IIRC a check of the returned query against the query sent.

> TCP on the other hand increases the allocated port number for each new
> socket, the same behaviour for UDP would add certain amount of time that
> decreases this risk.

Does it always?  If you wait for the length of TIME_WAIT before issuing another 
bind() request does the port number still increase?

While it might be nice to step through the anonymous port space in some fashion 
(I suspect the argument would be made that it should be somewhat random to 
preclude guessing from the outside), applications using UDP are still required 
to expect the unexpected wrt data arriving on their socket.

rick jones

> 
> Is the current behaviour intended?
> 
> Regards,
> Laszlo Attila Toth
> -
> To unsubscribe from this list: send the line "unsubscribe netdev" in
> the body of a message to [email-masked]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html