-
Notifications
You must be signed in to change notification settings - Fork 268
Manually bound sockets should only receive packets from the peer #2592
Description
If a TCP or UDP socket has its peer address set with connect(), it should only ever receive packets from that peer address.
connect(2):
If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.
In Shadow, a connect() after a bind() will still allow the UDP socket to receive packets from any address.
Since the UDP socket may have been bound before the connect() call (and would therefore be associated with the network interface using peer_ip=0 and peer_port=0), we can't rely on the network interface to drop packets from an incorrect peer address. We should maybe filter out these packets from within _udp_processPacket instead. Alternatively, we could disassociate from the interface and re-associate using the new peer ip/port during every connect() call. In this case we'd have to be careful that two UDP sockets don't end up with the same port. For example if two sockets are associated with an interface using different peers, and therefore have different association keys, but the bind_ip and bind_port are the same.
There may be a related issue with TCP sockets where if you bind a TCP socket, it will be associated with the network interface using peer_ip=0 and peer_port=0. Then if you later call connect(), it won't be re-associated using the new peer name, and it could receive packets from any address (not just the peer). I haven't looked into this case at all, I don't think it's an issue since applications can't generate raw packets in Shadow, but it would still be good to fix anyway.