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): bound the transfer filename the sender chooses
Olcay Seygan81e63c00a570
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d260cc343..4962e2150 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -347,6 +347,7 @@ found and fixed — EA's own, not port damage.**- Two blocks of 2003 assembly destroyed registers and took down the main menu.- Quitting faulted twice every time; it now takes about half a second.- A long chat message or an unusual map name could kill the process.+- Nor can a map handed to you over the network, however long a name the other machine gives it.- The window no longer goes *Not Responding* during the menu's camera moves.- Poison clouds, mine clearing, garrison kills and crew-killing weapons: all traced and fixed.- Blowing up a full transport is survivable now, in every remaining variant.diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cppindex 5e6980e8f..f12544fad 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp@@ -26,6 +26,7 @@#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine+#include "stringex.h"#include "GameNetwork/NetPacket.h"#include "GameNetwork/NetCommandMsg.h"#include "GameNetwork/NetworkDefs.h"@@ -5708,15 +5709,13 @@ NetCommandMsg * NetPacket::readWrapperMessage(UnsignedByte *data, Int &i) {NetCommandMsg * NetPacket::readFileMessage(UnsignedByte *data, Int &i) {NetFileCommandMsg *msg = newInstance(NetFileCommandMsg);char filename[_MAX_PATH];- char *c = filename;- while (data[i] != 0) {- *c = data[i];- ++c;- ++i;- }- *c = 0;- ++i;+ // The sender decides how long this string is, and the old loop copied it into a _MAX_PATH+ // stack buffer one byte at a time with nothing stopping it - a longer name overwrote the+ // stack. Bound the copy; i still advances over the whole name, truncated or not, so the+ // rest of the message is read from the right offset either way.+ i += strlcpy(filename, (const char *)(data + i), sizeof(filename));+ ++i; // step over the terminatormsg->setPortableFilename(AsciiString(filename)); // it's transferred as a portable filenameUnsignedInt dataLength = 0;@@ -5738,15 +5737,13 @@ NetCommandMsg * NetPacket::readFileMessage(UnsignedByte *data, Int &i) {NetCommandMsg * NetPacket::readFileAnnounceMessage(UnsignedByte *data, Int &i) {NetFileAnnounceCommandMsg *msg = newInstance(NetFileAnnounceCommandMsg);char filename[_MAX_PATH];- char *c = filename;- while (data[i] != 0) {- *c = data[i];- ++c;- ++i;- }- *c = 0;- ++i;+ // The sender decides how long this string is, and the old loop copied it into a _MAX_PATH+ // stack buffer one byte at a time with nothing stopping it - a longer name overwrote the+ // stack. Bound the copy; i still advances over the whole name, truncated or not, so the+ // rest of the message is read from the right offset either way.+ i += strlcpy(filename, (const char *)(data + i), sizeof(filename));+ ++i; // step over the terminatormsg->setPortableFilename(AsciiString(filename)); // it's transferred as a portable filenameUnsignedShort fileID = 0;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 7a509c9f0..da746e4db 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -57,6 +57,7 @@#include "GameClient/Gadget.h"#include "GameNetwork/NetworkUtil.h"#include "GameNetwork/NetCommandList.h"+#include "GameNetwork/NetPacket.h"#include "GameNetwork/GameInfo.h"#include <float.h>#include "GameClient/Water.h"@@ -4229,6 +4230,75 @@ TEST(netcommand_list_orders_a_burst_of_acks_by_acknowledged_command)list->deleteInstance();}+/* The file transfer messages carry a filename the sender chooses the length of, and the reader+ copied it into a _MAX_PATH stack buffer a byte at a time with nothing stopping it. A name+ longer than that overwrote the stack of whoever was parsing the packet - a host feeding a+ client, or a client feeding the host. The copy is bounded now; the read offset still steps+ over the whole name, so everything after it in the message is still read from the right place. */+struct NetPacketFileReader : public NetPacket+{+ using NetPacket::readFileMessage;+ using NetPacket::readFileAnnounceMessage;+};++TEST(an_overlong_transfer_filename_is_truncated_instead_of_overflowing)+{+ static const Int NAME_LEN = 400; // _MAX_PATH is 260+ UnsignedByte buf[NAME_LEN + 1 + sizeof(UnsignedInt) + 4];+ Int at = 0;+ Int k;+ for( k = 0; k < NAME_LEN; ++k )+ buf[at++] = (UnsignedByte)('a' + (k % 26));+ buf[at++] = 0;++ UnsignedInt dataLength = 4;+ memcpy( buf + at, &dataLength, sizeof(dataLength) );+ at += sizeof(dataLength);+ buf[at++] = 0xDE; buf[at++] = 0xAD; buf[at++] = 0xBE; buf[at++] = 0xEF;++ Int i = 0;+ NetCommandMsg *msg = NetPacketFileReader::readFileMessage( buf, i );+ NetFileCommandMsg *fileMsg = (NetFileCommandMsg *)msg;++ // the name is cut to the buffer rather than written past it+ CHECK_EQ( fileMsg->getPortableFilename().getLength(), _MAX_PATH - 1 );++ // ...but the offset walked the whole name, so what follows it still parses+ CHECK_EQ( i, at );+ CHECK_EQ( (Int)fileMsg->getFileLength(), 4 );+ CHECK_EQ( (Int)fileMsg->getFileData()[0], 0xDE );+ CHECK_EQ( (Int)fileMsg->getFileData()[3], 0xEF );++ msg->detach();+}++TEST(an_overlong_announced_filename_is_truncated_instead_of_overflowing)+{+ static const Int NAME_LEN = 400;+ UnsignedByte buf[NAME_LEN + 1 + sizeof(UnsignedShort) + sizeof(UnsignedByte)];+ Int at = 0;+ Int k;+ for( k = 0; k < NAME_LEN; ++k )+ buf[at++] = (UnsignedByte)('a' + (k % 26));+ buf[at++] = 0;++ UnsignedShort fileID = 0x1234;+ memcpy( buf + at, &fileID, sizeof(fileID) );+ at += sizeof(fileID);+ buf[at++] = 0x5A; // player mask++ Int i = 0;+ NetCommandMsg *msg = NetPacketFileReader::readFileAnnounceMessage( buf, i );+ NetFileAnnounceCommandMsg *announce = (NetFileAnnounceCommandMsg *)msg;++ CHECK_EQ( announce->getPortableFilename().getLength(), _MAX_PATH - 1 );+ CHECK_EQ( i, at );+ CHECK_EQ( (Int)announce->getFileID(), 0x1234 );+ CHECK_EQ( (Int)announce->getPlayerMask(), 0x5A );++ msg->detach();+}+//////////////////////////////////////////////////////////////////////////////// Starting a new game//////////////////////////////////////////////////////////////////////////////