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(gamelogic): let a command name only what its sender controls
Olcay Seyganf9e55ac3eb59
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ac1153eb9..3c0e410e7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -303,6 +303,14 @@ map. Ctrl+A is still everything at once.on its own — from twelve frames a second to thirty in about six seconds. A room held down by amachine that really is that slow settles just above it and stays there instead of surging andfalling back.+- **Nobody can fire your superweapon but you.** An order arriving over the network says which unit+ or building it is about, and the game used to run it on whatever it named — it never checked that+ the player who sent the order owned the thing. A doctored order could set off another player's+ superweapon, or redirect one that was already on its way. Orders are now refused unless the sender+ controls what they name. The same went for the retaliation setting, which arrived carrying a+ player number and would happily change somebody else's; it no longer carries one, because it was+ only ever about the player who sent it.+- **An online match could be forced into a disconnect screen with no way out.** The command thatstarts a new game was obeyed no matter what the machine was doing — including in the middle of amatch. The player who received it quietly restarted into a different game; everybody else wasdiff --git a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.hindex 2d115635a..da96a983f 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h@@ -631,7 +631,7 @@ public:MSG_CREATE_FORMATION, ///< Creates a formation.MSG_LOGIC_CRC, ///< CRC from the logic passed around in a network game :)MSG_SET_MINE_CLEARING_DETAIL, ///< CRC from the logic passed around in a network game :)- MSG_ENABLE_RETALIATION_MODE, ///< Turn retaliation mode on or off for the specified player.+ MSG_ENABLE_RETALIATION_MODE, ///< Turn retaliation mode on or off for the player who sent it.MSG_DO_HOLD_POSITION, ///< Every selected unit guards the spot it is standing on (fork)MSG_BEGIN_DEBUG_NETWORK_MESSAGES = 1900, ///< network messages that exist only in debug/internal builds. all grouped separately.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Player.h b/GeneralsMD/Code/GameEngine/Include/Common/Player.hindex 91e8db568..2371f17c8 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/Player.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/Player.h@@ -124,6 +124,18 @@ inline Bool isValidHotkeySquadIndex( Int squadNumber )return squadNumber >= 0 && squadNumber < NUM_HOTKEY_SQUADS;}+//+// A network command names the object it acts on by id, and an id is only a number - nothing in the+// message ties it to the machine that sent it, so a doctored one can name any object on the map,+// including one it does not own. The dispatcher runs whatever it is handed, so the ownership+// question has to be asked there. This compares identity and never looks inside either player.+//+class Player;+inline Bool isPlayerCommandingOwnObject( const Player *commandingPlayer, const Player *objectController )+{+ return commandingPlayer != NULL && commandingPlayer == objectController;+}+// ------------------------------------------------------------------------------------------------typedef Int PlayerIndex;#define PLAYER_INDEX_INVALID -1diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cppindex 54df2e99c..3345b1363 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp@@ -753,7 +753,8 @@ void Player::update()GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_ENABLE_RETALIATION_MODE );if( msg ){- msg->appendIntegerArgument( getPlayerIndex() );+ // No player index: the message can only ever be about the player who sent it, and+ // carrying the index made it look like it could be about somebody else.msg->appendBooleanArgument( TheGlobalData->m_clientRetaliationModeEnabled );}}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cppindex 1f72ee1e5..2fe70f8a3 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp@@ -686,14 +686,14 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )case GameMessage::MSG_ENABLE_RETALIATION_MODE:{- //Logically turns on or off retaliation mode for a specified player.- Int playerIndex = msg->getArgument( 0 )->integer;- Bool enableRetaliation = msg->getArgument( 1 )->boolean;-- Player *player = ThePlayerList->getNthPlayer( playerIndex );- if( player )- {- player->setLogicalRetaliationModeEnabled( enableRetaliation );+ // Turns retaliation mode on or off for the player who sent the message. The message used+ // to carry a player index as well, and the case obeyed it - so a doctored one could set+ // anybody's retaliation mode. Player::update only ever posts this for itself, so the index+ // was always the sender's own and the argument is gone.+ const Bool enableRetaliation = msg->getArgument( 0 )->boolean;+ if( thisPlayer )+ {+ thisPlayer->setLogicalRetaliationModeEnabled( enableRetaliation );}break;}@@ -734,6 +734,13 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )Object* source = TheGameLogic->findObjectByID(sourceID);if (source != NULL){+ if( !isPlayerCommandingOwnObject( thisPlayer, source->getControllingPlayer() ) )+ {+ DEBUG_CRASH( ("MSG_DO_SPECIAL_POWER: a command from player %d named object %d, which it does not control",+ msg->getPlayerIndex(), (Int)sourceID) );+ break;+ }+AIGroup* theGroup = TheAI->createGroup();theGroup->add(source);theGroup->groupDoSpecialPower( specialPowerID, options );@@ -775,6 +782,13 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )Object* source = TheGameLogic->findObjectByID(sourceID);if (source != NULL){+ if( !isPlayerCommandingOwnObject( thisPlayer, source->getControllingPlayer() ) )+ {+ DEBUG_CRASH( ("MSG_DO_SPECIAL_POWER_AT_LOCATION: a command from player %d named object %d, which it does not control",+ msg->getPlayerIndex(), (Int)sourceID) );+ break;+ }+AIGroup* theGroup = TheAI->createGroup();theGroup->add(source);theGroup->groupDoSpecialPowerAtLocation( specialPowerID, &targetCoord, angle, objectInWay, options );@@ -814,6 +828,13 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )Object* source = TheGameLogic->findObjectByID(sourceID);if (source != NULL){+ if( !isPlayerCommandingOwnObject( thisPlayer, source->getControllingPlayer() ) )+ {+ DEBUG_CRASH( ("MSG_DO_SPECIAL_POWER_AT_OBJECT: a command from player %d named object %d, which it does not control",+ msg->getPlayerIndex(), (Int)sourceID) );+ break;+ }+AIGroup* theGroup = TheAI->createGroup();theGroup->add(source);theGroup->groupDoSpecialPowerAtObject( specialPowerID, target, options );@@ -1279,6 +1300,13 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )Object* source = TheGameLogic->findObjectByID(sourceID);if (source != NULL){+ if( !isPlayerCommandingOwnObject( thisPlayer, source->getControllingPlayer() ) )+ {+ DEBUG_CRASH( ("MSG_DO_SPECIAL_POWER_OVERRIDE_DESTINATION: a command from player %d named object %d, which it does not control",+ msg->getPlayerIndex(), (Int)sourceID) );+ break;+ }+AIGroup* theGroup = TheAI->createGroup();theGroup->add(source);theGroup->groupOverrideSpecialPowerDestination( spType, loc, CMD_FROM_PLAYER );diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 2aeba72a5..332d9c5bf 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3700,3 +3700,24 @@ TEST(a_new_game_only_starts_from_a_standing_start)refusedEveryBusyState = FALSE;CHECK( refusedEveryBusyState );}++/* A network command names the object it acts on by id, and nothing in the message ties that id to+ the machine that sent it. The special power cases ran whatever object they were handed, so a+ doctored command could fire another player's superweapon. They ask this first now. The+ predicate compares identity only and never dereferences either pointer, so the test can use+ stand-in addresses for players. */+TEST(a_command_may_only_name_an_object_its_sender_controls)+{+ const Player *alice = (const Player *)0x100;+ const Player *bob = (const Player *)0x200;++ CHECK( isPlayerCommandingOwnObject( alice, alice ) ); // your own unit+ CHECK( !isPlayerCommandingOwnObject( alice, bob ) ); // somebody else's+ CHECK( !isPlayerCommandingOwnObject( bob, alice ) );++ // an object with no controller is nobody's to command, and neither is anything at all when the+ // message names a player slot that is not in the game+ CHECK( !isPlayerCommandingOwnObject( alice, NULL ) );+ CHECK( !isPlayerCommandingOwnObject( NULL, alice ) );+ CHECK( !isPlayerCommandingOwnObject( NULL, NULL ) );+}