Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gamenetwork): guard the LAN door on the name, not on the map
Olcay Seygan233c0e2dd8fb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e571213ec..58eb7bda9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -155,6 +155,8 @@ found and fixed — EA's own, not port damage.**- A player leaving is reported as a player leaving, not a desync.- A desync now writes a per-object report on both machines for comparison.- LAN refuses to start between two machines whose game files differ.+- A player whose name holds a comma, a colon or a control character is turned away at the door instead of scrambling the lobby's idea of who is in the room.+- An open seat can be taken. The map's number of starting positions used to overrule the host: a four position map turned away the fifth player even with a seat left open for him, and that seat stayed open on everyone's screen.- A map sent to you over the network has to be a map. The other machine used to name any file it liked and fill it with anything at all, and it was written where the name pointed; now the name cannot leave the map folder, the kind has to be one a map transfer carries, and the contents have to match the kind.- The processor's rounding mode is reset from the right register every frame.- The disconnect screen no longer interrupts a game that is merely slow.diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.hindex be7a491a6..9e04ecbe6 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h@@ -53,6 +53,11 @@ AsciiString GetAsciiNetCommandType(NetCommandType type);Bool IsSafeTransferPath(const AsciiString &filePath);Bool IsValidTransferFileContent(const AsciiString &filePath, const UnsignedByte *data, UnsignedInt dataSize);+/* Whether a player name another machine sends us may be shown and put in the lobby's game state+ string. Refuses the separators that string is parsed on, control characters, lone surrogates,+ and a name made of nothing but spaces. */+Bool IsUsablePlayerName(const WideChar *playerName);+// Command ids are sixteen bits wide and wrap. Ordering them with < or > puts every command// issued after a wrap in front of every command issued before it, so the answer comes from the// distance between them instead: newVal is newer when it sits within half the id space ahead ofdiff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cppindex 7412a14c5..d45d86814 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp@@ -39,6 +39,7 @@#include "GameNetwork/GameDataMatch.h"#include "GameNetwork/LANAPI.h"#include "GameNetwork/LANAPICallbacks.h"+#include "GameNetwork/NetworkUtil.h"#include "GameClient/MapUtil.h"void LANAPI::handleRequestLocations( LANMessage *msg, UnsignedInt senderIP )@@ -295,7 +296,24 @@ void LANAPI::handleRequestJoin( LANMessage *msg, UnsignedInt senderIP )}}- // We're the host, so see if he has a duplicate name+ // We're the host, and the name he sends is his own choosing. It ends up in the game+ // state string the lobby passes around, which is parsed on commas, colons and+ // semicolons - so a name holding one of those rewrites everyone's idea of who is in+ // the room. Control characters and lone surrogates do their own damage in a text+ // field, and a name of nothing but spaces looks like an empty seat.+ if (canJoin && !IsUsablePlayerName(msg->name))+ {+ // deny as a duplicate name: the client already knows how to show that one+ reply.LANMessageType = LANMessage::MSG_JOIN_DENY;+ reply.GameNotJoined.reason = LANAPIInterface::RET_DUPLICATE_NAME;+ reply.GameNotJoined.gameIP = m_localIP;+ reply.GameNotJoined.playerIP = senderIP;+ canJoin = false;++ DEBUG_LOG(("LANAPI::handleRequestJoin - join denied, the name holds characters a name may not.\n"));+ }++ // Then see if he has a duplicate namefor (player = 0; canJoin && player<MAX_SLOTS; ++player){LANGameSlot *slot = m_currentGame->getLANSlot(player);@@ -313,53 +331,35 @@ void LANAPI::handleRequestJoin( LANMessage *msg, UnsignedInt senderIP )}}- // See if there's room- // First get the number of players currently in the room.- Int numPlayers = 0;- for (player = 0; player < MAX_SLOTS; ++player)- {- if (m_currentGame->getLANSlot(player)->isOccupied()- && !(m_currentGame->getLANSlot(player)->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER))- {- ++numPlayers;- }- }-- // now get the number of starting spots on the map.- Int numStartingSpots = MAX_SLOTS;- const MapMetaData *md = TheMapCache->findMap(m_currentGame->getMap());- if (md != NULL)+ // See if there's room. An open slot is room: the host is the one who opens and closes+ // them, and the map's own number of starting positions used to overrule that - so a+ // four position map turned away the fifth player even when the host had left a seat+ // open for him, and the slot he was refused stayed open on everyone's screen.+ for (player = 0; canJoin && player<MAX_SLOTS; ++player){- numStartingSpots = md->m_numPlayers;- }-- if (numPlayers < numStartingSpots) {- for (player = 0; canJoin && player<MAX_SLOTS; ++player)+ if (m_currentGame->getLANSlot(player)->isOpen()){- if (m_currentGame->getLANSlot(player)->isOpen())- {- // OK, add him in.- reply.LANMessageType = LANMessage::MSG_JOIN_ACCEPT;- wcsncpy(reply.GameJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);- reply.GameJoined.gameName[g_lanGameNameLength] = 0;- reply.GameJoined.slotPosition = player;- reply.GameJoined.gameIP = m_localIP;- reply.GameJoined.playerIP = senderIP;-- LANGameSlot newSlot;- newSlot.setState(SLOT_PLAYER, UnicodeString(msg->name));- newSlot.setIP(senderIP);- newSlot.setPort(NETWORK_BASE_PORT_NUMBER);- newSlot.setLastHeard(timeGetTime());- newSlot.setSerial(msg->GameToJoin.serial);- m_currentGame->setSlot(player,newSlot);- DEBUG_LOG(("LANAPI::handleRequestJoin - added player %ls at ip 0x%08x to the game\n", msg->name, senderIP));-- OnPlayerJoin(player, UnicodeString(msg->name));- responseIP = 0;+ // OK, add him in.+ reply.LANMessageType = LANMessage::MSG_JOIN_ACCEPT;+ wcsncpy(reply.GameJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);+ reply.GameJoined.gameName[g_lanGameNameLength] = 0;+ reply.GameJoined.slotPosition = player;+ reply.GameJoined.gameIP = m_localIP;+ reply.GameJoined.playerIP = senderIP;++ LANGameSlot newSlot;+ newSlot.setState(SLOT_PLAYER, UnicodeString(msg->name));+ newSlot.setIP(senderIP);+ newSlot.setPort(NETWORK_BASE_PORT_NUMBER);+ newSlot.setLastHeard(timeGetTime());+ newSlot.setSerial(msg->GameToJoin.serial);+ m_currentGame->setSlot(player,newSlot);+ DEBUG_LOG(("LANAPI::handleRequestJoin - added player %ls at ip 0x%08x to the game\n", msg->name, senderIP));++ OnPlayerJoin(player, UnicodeString(msg->name));+ responseIP = 0;- break;- }+ break;}}if (canJoin && player == MAX_SLOTS)diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cppindex fa293650e..2e8e0ae8a 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp@@ -432,3 +432,49 @@ Bool IsValidTransferFileContent(const AsciiString &filePath, const UnsignedBytereturn TRUE;}++//----------------------------------------------------------------------------------------------+// A player name another machine sends us.+//+// The name goes into the game state string the lobby passes around, and that string is parsed on+// commas, colons and semicolons - so a name holding one of those rewrites everyone's idea of who+// is in the room. Control characters and half a surrogate pair do their own damage in a text+// field, and a name made only of spaces reads as an empty seat.+//----------------------------------------------------------------------------------------------++static Bool isCharacterANameMayNotHold(const WideChar c)+{+ return c < L' ' // C0 controls+ || c == L',' || c == L':' || c == L';' // the game state separators+ || (c >= L'\x007f' && c <= L'\x009f') // DEL and the C1 controls+ || c == L'\x2028' || c == L'\x2029' // line and paragraph separators+ || (c >= L'\xd800' && c <= L'\xdfff'); // lone surrogates+}++static Bool isSpaceCharacter(const WideChar c)+{+ return c == L' '+ || c == L'\xa0' // no-break space+ || c == L'\x1680' // ogham space mark+ || (c >= L'\x2000' && c <= L'\x200a') // en/em, figure, thin, hair+ || c == L'\x202f' // narrow no-break space+ || c == L'\x205f' // medium mathematical space+ || c == L'\x3000'; // ideographic space+}++Bool IsUsablePlayerName(const WideChar *playerName)+{+ if (playerName == NULL)+ return FALSE;++ Bool sawSomethingReadable = FALSE;+ for (const WideChar *c = playerName; *c; ++c)+ {+ if (isCharacterANameMayNotHold(*c))+ return FALSE;+ if (!isSpaceCharacter(*c))+ sawSomethingReadable = TRUE;+ }++ return sawSomethingReadable;+}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 69139e4f0..bac042c96 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -4424,6 +4424,35 @@ TEST(a_transferred_file_has_to_be_what_its_name_says_it_is)CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.tga", text, 8 ) );}+/* The name a joining player sends goes into the game state string the lobby passes around, and that+ string is parsed on commas, colons and semicolons - so a name holding one of those rewrites+ everyone's idea of who is in the room. It was taken exactly as sent. */+TEST(a_player_name_from_another_machine_cannot_rewrite_the_lobby)+{+ CHECK( IsUsablePlayerName( L"Olcay" ) );+ CHECK( IsUsablePlayerName( L"[GLA] scud" ) );+ CHECK( IsUsablePlayerName( L"\x00fcmit" ) ); // accents and non-latin are fine++ // the three separators the game state string is cut on+ CHECK( !IsUsablePlayerName( L"a,b" ) );+ CHECK( !IsUsablePlayerName( L"a:b" ) );+ CHECK( !IsUsablePlayerName( L"a;b" ) );++ // control characters, in both bands, and the line separators+ CHECK( !IsUsablePlayerName( L"a\nb" ) );+ CHECK( !IsUsablePlayerName( L"a\x0085" "b" ) );+ CHECK( !IsUsablePlayerName( L"a\x2028" "b" ) );++ // half a surrogate pair is not a character+ CHECK( !IsUsablePlayerName( L"a\xd800" "b" ) );++ // a name has to have something in it+ CHECK( !IsUsablePlayerName( L"" ) );+ CHECK( !IsUsablePlayerName( L" " ) );+ CHECK( !IsUsablePlayerName( L"\x3000\x00a0" ) ); // ideographic and no-break spaces+ CHECK( !IsUsablePlayerName( NULL ) );+}+TEST(a_transferred_name_cannot_climb_out_of_its_directory){CHECK( IsSafeTransferPath( "maps\\foo\\foo.map" ) );