Skip to content

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.

fix(gamenetwork): a map transfer may only write a map

Olcay Seyganf523b3e8a4c2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d816e327d..e571213ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -155,6 +155,7 @@ 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 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.
- The keepalive interval setting is read now, and kept in a sane range.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
index 49d1d2949..be7a491a6 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
@@ -46,6 +46,13 @@ Bool CommandRequiresDirectSend(NetCommandMsg *msg);
Bool IsCommandSynchronized(NetCommandType type);
AsciiString GetAsciiNetCommandType(NetCommandType type);
+/* What another machine is allowed to put on this disk during a map transfer. IsSafeTransferPath
+ refuses a name that climbs out of the directory it belongs in; IsValidTransferFileContent
+ refuses an extension a transfer never carries, a file over the size ceiling for its kind, and
+ bytes that are not what the extension claims. */
+Bool IsSafeTransferPath(const AsciiString &filePath);
+Bool IsValidTransferFileContent(const AsciiString &filePath, const UnsignedByte *data, UnsignedInt dataSize);
+
// 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 of
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index f1db41b06..b1d77fba7 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -723,7 +723,15 @@ void ConnectionManager::processFile(NetFileCommandMsg *msg)
DEBUG_LOG(("%ls\n", log.str()));
#endif
- if (TheFileSystem->doesFileExist(msg->getRealFilename().str()))
+ AsciiString realFileName = msg->getRealFilename();
+
+ if (!IsSafeTransferPath(msg->getPortableFilename()) || !IsSafeTransferPath(realFileName))
+ {
+ DEBUG_LOG(("ConnectionManager::processFile - refused '%s'\n", realFileName.str()));
+ return;
+ }
+
+ if (TheFileSystem->doesFileExist(realFileName.str()))
{
DEBUG_LOG(("File exists already!\n"));
//return;
@@ -755,13 +763,28 @@ void ConnectionManager::processFile(NetFileCommandMsg *msg)
}
#endif // COMPRESS_TARGAS
- File *fp = TheFileSystem->openFile(msg->getRealFilename().str(), File::CREATE | File::BINARY | File::WRITE);
+ // the bytes are still in memory here, so check them before any of them reach the disk
+ if (!IsValidTransferFileContent(realFileName, buf, (UnsignedInt)len))
+ {
+ DEBUG_LOG(("ConnectionManager::processFile - '%s' is not what it says it is, dropped\n",
+ realFileName.str()));
+#ifdef COMPRESS_TARGAS
+ if (deleteBuf)
+ {
+ delete[] buf;
+ buf = NULL;
+ }
+#endif // COMPRESS_TARGAS
+ return;
+ }
+
+ File *fp = TheFileSystem->openFile(realFileName.str(), File::CREATE | File::BINARY | File::WRITE);
if (fp)
{
fp->write(buf, len);
fp->close();
fp = NULL;
- DEBUG_LOG(("Wrote %d bytes to file %s!\n",len,msg->getRealFilename().str()));
+ DEBUG_LOG(("Wrote %d bytes to file %s!\n",len,realFileName.str()));
}
else
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
index 1ae3df6cf..fa293650e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
@@ -26,6 +26,7 @@
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "GameNetwork/NetworkUtil.h"
+#include "TARGA.H"
Int MAX_FRAMES_AHEAD = 128;
@@ -298,3 +299,136 @@ AsciiString GetAsciiNetCommandType(NetCommandType type) {
}
return s;
}
+
+//----------------------------------------------------------------------------------------------
+// What another machine is allowed to put on this disk.
+//
+// A map transfer arrives as a filename and a block of bytes, and both come from the other end.
+// Nothing checked either one: whatever it named was written wherever the name resolved to, with
+// whatever was in it. A host could hand a joining player any file it liked.
+//
+// Three gates, cheapest first: the name may not climb out of the directory it belongs in, the
+// extension has to be one of the six a map transfer ever carries, and the bytes have to look like
+// what the extension claims - with a size ceiling per kind, so a "map" cannot be a gigabyte.
+//----------------------------------------------------------------------------------------------
+
+enum TransferFileType
+{
+ TransferFileType_Invalid = -1,
+ TransferFileType_Map,
+ TransferFileType_Ini,
+ TransferFileType_Str,
+ TransferFileType_Txt,
+ TransferFileType_Tga,
+ TransferFileType_Wak,
+ TransferFileType_Count
+};
+
+struct TransferFileRule
+{
+ const char *ext;
+ UnsignedInt maxSize;
+};
+
+static const TransferFileRule transferFileRules[TransferFileType_Count] =
+{
+ { ".map", 5 * 1024 * 1024 },
+ { ".ini", 2 * 1024 * 1024 },
+ { ".str", 512 * 1024 },
+ { ".txt", 1 * 1024 * 1024 },
+ { ".tga", 2 * 1024 * 1024 },
+ { ".wak", 128 * 1024 },
+};
+
+static TransferFileType getTransferFileType(const char *extension)
+{
+ for (Int i = 0; i < TransferFileType_Count; ++i)
+ {
+ if (stricmp(extension, transferFileRules[i].ext) == 0)
+ return (TransferFileType)i;
+ }
+ return TransferFileType_Invalid;
+}
+
+// The portable path keeps its last two components, so a ".." among them walks out of the map
+// directory. Nothing legitimate needs one.
+Bool IsSafeTransferPath(const AsciiString &filePath)
+{
+ const char *p = filePath.str();
+ if (strstr(p, "..") != NULL)
+ {
+ DEBUG_LOG(("Transfer path '%s' tries to climb out of its directory\n", p));
+ return FALSE;
+ }
+ return TRUE;
+}
+
+Bool IsValidTransferFileContent(const AsciiString &filePath, const UnsignedByte *data, UnsignedInt dataSize)
+{
+ const char *fileExt = strrchr(filePath.str(), '.');
+ if (fileExt == NULL)
+ {
+ DEBUG_LOG(("File '%s' has no extension\n", filePath.str()));
+ return FALSE;
+ }
+
+ const TransferFileType fileType = getTransferFileType(fileExt);
+ if (fileType == TransferFileType_Invalid)
+ {
+ DEBUG_LOG(("File '%s' has an extension a transfer never carries\n", filePath.str()));
+ return FALSE;
+ }
+
+ const TransferFileRule &rule = transferFileRules[fileType];
+ if (dataSize > rule.maxSize)
+ {
+ DEBUG_LOG(("File '%s' is %d bytes, over the %d byte limit for its kind\n",
+ filePath.str(), dataSize, rule.maxSize));
+ return FALSE;
+ }
+
+ switch (fileType)
+ {
+ case TransferFileType_Ini:
+ {
+ // an INI is text; a null byte in one means it is something else wearing the name
+ for (UnsignedInt i = 0; i < dataSize; ++i)
+ {
+ if (data[i] == 0)
+ {
+ DEBUG_LOG(("INI file '%s' holds null bytes, so it is not an INI\n", filePath.str()));
+ return FALSE;
+ }
+ }
+ break;
+ }
+
+ case TransferFileType_Tga:
+ {
+ // the Targa 2 footer is the last 26 bytes, and its signature the 18 before the end.
+ // Read it by offset rather than as a struct: the struct carries a constructor and the
+ // padding that comes with it.
+ static const Int TGA2_FOOTER_SIZE = 26;
+ static const Int TGA2_SIGNATURE_LEN = 16;
+ if (dataSize < (UnsignedInt)(sizeof(TGAHeader) + TGA2_FOOTER_SIZE))
+ {
+ DEBUG_LOG(("TGA file '%s' is too small to be one\n", filePath.str()));
+ return FALSE;
+ }
+ const UnsignedByte *sig = data + dataSize - (TGA2_SIGNATURE_LEN + 2);
+ if (memcmp(sig, TGA2_SIGNATURE, TGA2_SIGNATURE_LEN) != 0
+ || sig[TGA2_SIGNATURE_LEN] != '.'
+ || sig[TGA2_SIGNATURE_LEN + 1] != '\0')
+ {
+ DEBUG_LOG(("TGA file '%s' has no TRUEVISION-XFILE footer\n", filePath.str()));
+ return FALSE;
+ }
+ break;
+ }
+
+ default:
+ break;
+ }
+
+ return TRUE;
+}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 92d514d43..69139e4f0 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -4371,6 +4371,69 @@ TEST(an_overlong_announced_filename_is_truncated_instead_of_overflowing)
msg->detach();
}
+/* A map transfer arrives as a filename and a block of bytes, both chosen by the other machine, and
+ nothing checked either one: whatever it named was written wherever the name resolved to, with
+ whatever was in it. A host could hand a joining player any file it liked, anywhere the name
+ reached. */
+static UnsignedByte *makeTga( Int payloadBytes, Bool withFooter )
+{
+ static UnsignedByte tga[512];
+ memset( tga, 0, sizeof(tga) );
+ if( withFooter )
+ {
+ UnsignedByte *sig = tga + payloadBytes - 18;
+ memcpy( sig, "TRUEVISION-XFILE", 16 );
+ sig[16] = '.';
+ sig[17] = '\0';
+ }
+ return tga;
+}
+
+TEST(a_transferred_file_has_to_be_what_its_name_says_it_is)
+{
+ UnsignedByte text[64];
+ memset( text, 'x', sizeof(text) );
+
+ // the six kinds a transfer carries are taken
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.map", text, sizeof(text) ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.ini", text, sizeof(text) ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.str", text, sizeof(text) ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.txt", text, sizeof(text) ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.wak", text, sizeof(text) ) );
+
+ // anything else is not
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.exe", text, sizeof(text) ) );
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.dll", text, sizeof(text) ) );
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.bat", text, sizeof(text) ) );
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\noextension", text, sizeof(text) ) );
+
+ // an INI that holds null bytes is a binary wearing the name
+ UnsignedByte binary[64];
+ memset( binary, 'x', sizeof(binary) );
+ binary[13] = 0;
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.ini", binary, sizeof(binary) ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.txt", binary, sizeof(binary) ) ); // a txt may
+
+ // the size ceiling is per kind
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.wak", text, 256 * 1024 ) );
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.map", text, 256 * 1024 ) );
+
+ // a targa needs its footer, and enough bytes to have one
+ CHECK( IsValidTransferFileContent( "maps\\foo\\foo.tga", makeTga( 128, TRUE ), 128 ) );
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.tga", makeTga( 128, FALSE ), 128 ) );
+ CHECK( !IsValidTransferFileContent( "maps\\foo\\foo.tga", text, 8 ) );
+}
+
+TEST(a_transferred_name_cannot_climb_out_of_its_directory)
+{
+ CHECK( IsSafeTransferPath( "maps\\foo\\foo.map" ) );
+ CHECK( IsSafeTransferPath( "MAPS\\some map\\some map.map" ) );
+
+ CHECK( !IsSafeTransferPath( "maps\\..\\..\\windows\\system32\\evil.map" ) );
+ CHECK( !IsSafeTransferPath( "..\\evil.map" ) );
+ CHECK( !IsSafeTransferPath( "maps/../evil.map" ) );
+}
+
/* The 2003 datagram was 512 bytes all in, 476 of it payload - a quarter of what any link carries
today, and the reason a busy frame needs four or five datagrams where one would do. The payload
is sized from a safe UDP ceiling now, and the three numbers have to stay in step: what goes on