[IP] Allow 0xFFFF as Valid Network Port Number (#5074)

* Allow 0xFFFF as valid port number

* Note possible reason for failure in TCPAllocatePort

* Return NtStatus error on TCP/IP out-of-ports failure

* Replace unavailable "ERR" with "DbgPrint"
This commit is contained in:
Doug Lyons 2023-02-17 16:11:00 -06:00 committed by GitHub
parent 1fdbc80e58
commit 874d317a71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 17 deletions

View file

@ -77,10 +77,14 @@ NTSTATUS TCPListen(PCONNECTION_ENDPOINT Connection, UINT Backlog)
if (NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
{ {
/* Allocate the port in the port bitmap */ /* Allocate the port in the port bitmap */
Connection->AddressFile->Port = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port); UINT AllocatedPort = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
/* This should never fail unless all ports are in use */
/* This should never fail */ if (AllocatedPort == (UINT) -1)
ASSERT(Connection->AddressFile->Port != 0xFFFF); {
DbgPrint("ERR: No more ports available.\n");
return STATUS_TOO_MANY_ADDRESSES;
}
Connection->AddressFile->Port = AllocatedPort;
} }
} }
} }

View file

@ -214,7 +214,7 @@ NTSTATUS TCPStartup(VOID)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = PortsStartup( &TCPPorts, 1, 0xfffe ); Status = PortsStartup(&TCPPorts, 1, 0xffff);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
return Status; return Status;
@ -370,6 +370,8 @@ NTSTATUS TCPConnect
/* Check if we had an unspecified port */ /* Check if we had an unspecified port */
if (!Connection->AddressFile->Port) if (!Connection->AddressFile->Port)
{ {
UINT AllocatedPort;
/* We did, so we need to copy back the port */ /* We did, so we need to copy back the port */
Status = TCPGetSockAddress(Connection, (PTRANSPORT_ADDRESS)&LocalAddress, FALSE); Status = TCPGetSockAddress(Connection, (PTRANSPORT_ADDRESS)&LocalAddress, FALSE);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
@ -379,10 +381,14 @@ NTSTATUS TCPConnect
} }
/* Allocate the port in the port bitmap */ /* Allocate the port in the port bitmap */
Connection->AddressFile->Port = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port); AllocatedPort = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
/* This should never fail unless all ports are in use */
/* This should never fail */ if (AllocatedPort == (UINT) -1)
ASSERT(Connection->AddressFile->Port != 0xFFFF); {
DbgPrint("ERR: No more ports available.\n");
return STATUS_TOO_MANY_ADDRESSES;
}
Connection->AddressFile->Port = AllocatedPort;
} }
connaddr.addr = RemoteAddress.Address.IPv4Address; connaddr.addr = RemoteAddress.Address.IPv4Address;

View file

@ -404,6 +404,7 @@ NTSTATUS FileOpenAddress(
PVOID Options) PVOID Options)
{ {
PADDRESS_FILE AddrFile; PADDRESS_FILE AddrFile;
UINT AllocatedPort;
TI_DbgPrint(MID_TRACE, ("Called (Proto %d).\n", Protocol)); TI_DbgPrint(MID_TRACE, ("Called (Proto %d).\n", Protocol));
@ -472,14 +473,15 @@ NTSTATUS FileOpenAddress(
if (Address->Address[0].Address[0].sin_port) if (Address->Address[0].Address[0].sin_port)
{ {
/* The client specified an explicit port so we force a bind to this */ /* The client specified an explicit port so we force a bind to this */
AddrFile->Port = TCPAllocatePort(Address->Address[0].Address[0].sin_port); AllocatedPort = TCPAllocatePort(Address->Address[0].Address[0].sin_port);
/* Check for bind success */ /* Check for bind success */
if (AddrFile->Port == 0xffff) if (AllocatedPort == (UINT)-1)
{ {
ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG); ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
return STATUS_ADDRESS_ALREADY_EXISTS; return STATUS_ADDRESS_ALREADY_EXISTS;
} }
AddrFile->Port = AllocatedPort;
/* Sanity check */ /* Sanity check */
ASSERT(Address->Address[0].Address[0].sin_port == AddrFile->Port); ASSERT(Address->Address[0].Address[0].sin_port == AddrFile->Port);
@ -487,14 +489,15 @@ NTSTATUS FileOpenAddress(
else if (!AddrIsUnspecified(&AddrFile->Address)) else if (!AddrIsUnspecified(&AddrFile->Address))
{ {
/* The client is trying to bind to a local address so allocate a port now too */ /* The client is trying to bind to a local address so allocate a port now too */
AddrFile->Port = TCPAllocatePort(0); AllocatedPort = TCPAllocatePort(0);
/* Check for bind success */ /* Check for bind success */
if (AddrFile->Port == 0xffff) if (AllocatedPort == (UINT)-1)
{ {
ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG); ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
return STATUS_ADDRESS_ALREADY_EXISTS; return STATUS_ADDRESS_ALREADY_EXISTS;
} }
AddrFile->Port = AllocatedPort;
} }
else else
{ {
@ -509,16 +512,16 @@ NTSTATUS FileOpenAddress(
case IPPROTO_UDP: case IPPROTO_UDP:
TI_DbgPrint(MID_TRACE,("Allocating udp port\n")); TI_DbgPrint(MID_TRACE,("Allocating udp port\n"));
AddrFile->Port = AllocatedPort = UDPAllocatePort(Address->Address[0].Address[0].sin_port);
UDPAllocatePort(Address->Address[0].Address[0].sin_port);
if ((Address->Address[0].Address[0].sin_port && if ((Address->Address[0].Address[0].sin_port &&
AddrFile->Port != Address->Address[0].Address[0].sin_port) || AllocatedPort != Address->Address[0].Address[0].sin_port) ||
AddrFile->Port == 0xffff) AllocatedPort == (UINT)-1)
{ {
ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG); ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
return STATUS_ADDRESS_ALREADY_EXISTS; return STATUS_ADDRESS_ALREADY_EXISTS;
} }
AddrFile->Port = AllocatedPort;
TI_DbgPrint(MID_TRACE,("Setting port %d (wanted %d)\n", TI_DbgPrint(MID_TRACE,("Setting port %d (wanted %d)\n",
AddrFile->Port, AddrFile->Port,