Do not send to the zero port
Port 0 is not meant to identify any remote port so attempting to send a packet to it should return an error. PiperOrigin-RevId: 341009528
This commit is contained in:
parent
29683f3598
commit
955e09dfbd
|
@ -487,6 +487,11 @@ func (e *endpoint) write(p tcpip.Payloader, opts tcpip.WriteOptions) (int64, <-c
|
|||
nicID = e.BindNICID
|
||||
}
|
||||
|
||||
if to.Port == 0 {
|
||||
// Port 0 is an invalid port to send to.
|
||||
return 0, nil, tcpip.ErrInvalidEndpointState
|
||||
}
|
||||
|
||||
dst, netProto, err := e.checkV4MappedLocked(*to)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
|
|
|
@ -1887,6 +1887,22 @@ TEST_P(UdpSocketTest, GetSocketDetachFilter) {
|
|||
SyscallFailsWithErrno(ENOPROTOOPT));
|
||||
}
|
||||
|
||||
TEST_P(UdpSocketTest, SendToZeroPort) {
|
||||
char buf[8];
|
||||
struct sockaddr_storage addr = InetLoopbackAddr();
|
||||
|
||||
// Sending to an invalid port should fail.
|
||||
SetPort(&addr, 0);
|
||||
EXPECT_THAT(sendto(sock_.get(), buf, sizeof(buf), 0,
|
||||
reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)),
|
||||
SyscallFailsWithErrno(EINVAL));
|
||||
|
||||
SetPort(&addr, 1234);
|
||||
EXPECT_THAT(sendto(sock_.get(), buf, sizeof(buf), 0,
|
||||
reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)),
|
||||
SyscallSucceedsWithValue(sizeof(buf)));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(AllInetTests, UdpSocketTest,
|
||||
::testing::Values(AddressFamily::kIpv4,
|
||||
AddressFamily::kIpv6,
|
||||
|
|
Loading…
Reference in New Issue