Prevent buffer overflows when using streams

This commit is contained in:
Chris Cowan 2025-06-27 19:27:22 -07:00 committed by Ricardo Luís Vaz Silva
commit ab7b1642bc
18 changed files with 600 additions and 388 deletions

View file

@ -191,10 +191,10 @@ size_t Net_SetEngineInfo(uint8_t*& stream);
bool Net_VerifyEngine(uint8_t*& stream);
void Net_SetupUserInfo();
const char* Net_GetClientName(int client, unsigned int charLimit);
size_t Net_SetUserInfo(int client, uint8_t*& stream);
size_t Net_ReadUserInfo(int client, uint8_t*& stream);
size_t Net_ReadGameInfo(uint8_t*& stream);
size_t Net_SetGameInfo(uint8_t*& stream);
void Net_SetUserInfo(int client, TArrayView<uint8_t>& stream);
void Net_ReadUserInfo(int client, TArrayView<uint8_t>& stream);
void Net_ReadGameInfo(TArrayView<uint8_t>& stream);
void Net_SetGameInfo(TArrayView<uint8_t>& stream);
static SOCKET CreateUDPSocket()
{
@ -833,7 +833,7 @@ static bool Host_CheckForConnections(void* connected)
{
if (Connected[RemoteClient].Status == CSTAT_CONNECTING)
{
uint8_t* stream = &NetBuffer[2];
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[2], MAX_MSGLEN-2);
Net_ReadUserInfo(RemoteClient, stream);
Connected[RemoteClient].Status = CSTAT_WAITING;
I_NetClientConnected(RemoteClient, 16u);
@ -886,8 +886,9 @@ static bool Host_CheckForConnections(void* connected)
memcpy(&NetBuffer[3], GameID, 8);
NetBufferLength = 11u;
uint8_t* stream = &NetBuffer[NetBufferLength];
NetBufferLength += Net_SetGameInfo(stream);
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[NetBufferLength], MAX_MSGLEN - NetBufferLength);
Net_SetGameInfo(stream);
NetBufferLength += stream.Data() - &NetBuffer[NetBufferLength];
SendPacket(con.Address);
clientReady = false;
}
@ -911,8 +912,9 @@ static bool Host_CheckForConnections(void* connected)
NetBufferLength += addrSize;
}
uint8_t* stream = &NetBuffer[NetBufferLength];
NetBufferLength += Net_SetUserInfo(i, stream);
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[NetBufferLength], MAX_MSGLEN - NetBufferLength);
Net_SetUserInfo(i, stream);
NetBufferLength += stream.Data() - &NetBuffer[NetBufferLength];
SendPacket(con.Address);
}
clientReady = false;
@ -1131,7 +1133,7 @@ static bool Guest_ContactHost(void* unused)
{
TicDup = clamp<int>(NetBuffer[2], 1, MAXTICDUP);
memcpy(GameID, &NetBuffer[3], 8);
uint8_t* stream = &NetBuffer[11];
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[11], MAX_MSGLEN - 11);
Net_ReadGameInfo(stream);
Connected[consoleplayer].bHasGameInfo = true;
}
@ -1158,7 +1160,7 @@ static bool Guest_ContactHost(void* unused)
{
Connected[c].Status = CSTAT_READY;
}
uint8_t* stream = &NetBuffer[byte];
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[byte], MAX_MSGLEN - byte);
Net_ReadUserInfo(c, stream);
SetClientAck(consoleplayer, c, true);
@ -1199,8 +1201,9 @@ static bool Guest_ContactHost(void* unused)
NetBuffer[1] = PRE_USER_INFO;
NetBufferLength = 2u;
uint8_t* stream = &NetBuffer[NetBufferLength];
NetBufferLength += Net_SetUserInfo(consoleplayer, stream);
TArrayView<uint8_t> stream = TArrayView(&NetBuffer[NetBufferLength], MAX_MSGLEN - NetBufferLength);
Net_SetUserInfo(consoleplayer, stream);
NetBufferLength += stream.Data() - &NetBuffer[NetBufferLength];
SendPacket(Connected[0].Address);
}
else if (con.Status == CSTAT_WAITING)