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): keep a wrapped command's chunks inside their own buffer

Olcay Seygan252285a04947
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4962e2150..6ed9ca316 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -348,6 +348,7 @@ found and fixed — EA's own, not port damage.**
- 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.
+- An order too big for one packet arrives in pieces, and a piece that claims to belong outside the order is dropped instead of landing there.
- 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/NetCommandWrapperList.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandWrapperList.cpp
index 00892a8da..8dcb00cc7 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandWrapperList.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandWrapperList.cpp
@@ -93,22 +93,49 @@ void NetCommandWrapperListNode::copyChunkData(NetWrapperCommandMsg *msg) {
return;
}
- DEBUG_ASSERTCRASH(msg->getChunkNumber() < m_numChunks, ("MunkeeChunk %d of %d\n",
- msg->getChunkNumber(), m_numChunks));
- if (msg->getChunkNumber() >= m_numChunks)
- return;
+ UnsignedInt chunkNumber = msg->getChunkNumber();
- DEBUG_LOG(("NetCommandWrapperListNode::copyChunkData() - copying chunk %d\n",
- msg->getChunkNumber()));
+ if (chunkNumber >= m_numChunks) {
+ DEBUG_CRASH(("Data chunk %d exceeds the expected maximum of %d chunks\n",
+ chunkNumber, m_numChunks));
+ return;
+ }
- if (m_chunksPresent[msg->getChunkNumber()] == TRUE) {
+ if (m_chunksPresent[chunkNumber] == TRUE) {
// we already received this chunk, no need to recopy it.
return;
}
- m_chunksPresent[msg->getChunkNumber()] = TRUE;
- UnsignedInt offset = msg->getDataOffset();
- memcpy(m_data + offset, msg->getData(), msg->getDataLength());
+ // The offset and the length both come off the wire and neither was checked. A wrapped command
+ // whose chunk claims an offset or a size beyond the buffer this node allocated for the whole
+ // command wrote straight past the end of that heap block - and the sender is a peer, not us.
+ UnsignedInt chunkDataOffset = msg->getDataOffset();
+ UnsignedInt chunkDataLength = msg->getDataLength();
+
+ if (chunkDataOffset >= m_dataLength) {
+ DEBUG_CRASH(("Data chunk offset %d exceeds the total data length %d\n",
+ chunkDataOffset, m_dataLength));
+ return;
+ }
+
+ if (chunkDataLength > MAX_PACKET_SIZE) {
+ DEBUG_CRASH(("Data chunk size %d is greater than the maximum packet size %d\n",
+ chunkDataLength, MAX_PACKET_SIZE));
+ return;
+ }
+
+ if (chunkDataOffset + chunkDataLength > m_dataLength) {
+ DEBUG_CRASH(("Data chunk at %d for %d bytes runs past the data array\n",
+ chunkDataOffset, chunkDataLength));
+ return;
+ }
+
+ DEBUG_LOG(("NetCommandWrapperListNode::copyChunkData() - copying chunk %d\n", chunkNumber));
+
+ memcpy(m_data + chunkDataOffset, msg->getData(), chunkDataLength);
+
+ // only a chunk that was really copied counts as present, or the command completes with a hole
+ m_chunksPresent[chunkNumber] = TRUE;
++m_numChunksPresent;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index da746e4db..f2801108e 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/NetCommandWrapperList.h"
#include "GameNetwork/NetPacket.h"
#include "GameNetwork/GameInfo.h"
#include <float.h>
@@ -4299,6 +4300,70 @@ TEST(an_overlong_announced_filename_is_truncated_instead_of_overflowing)
msg->detach();
}
+/* A command too big for one packet is sent as numbered chunks and reassembled into one heap block
+ sized from the first chunk's claim. Each later chunk then said where it went and how long it
+ was, and neither number was checked before the memcpy - so a peer could place its bytes anywhere
+ it liked relative to that block. The offset, the length and their sum are all checked now, and
+ a refused chunk no longer counts towards the command being complete. */
+static NetWrapperCommandMsg *makeWrapperChunk( UnsignedInt chunkNumber, UnsignedInt numChunks,
+ UnsignedInt totalDataLength, UnsignedInt offset, UnsignedByte *data, UnsignedInt dataLength )
+{
+ NetWrapperCommandMsg *msg = newInstance( NetWrapperCommandMsg );
+ msg->setWrappedCommandID( 42 );
+ msg->setNumChunks( numChunks );
+ msg->setTotalDataLength( totalDataLength );
+ msg->setChunkNumber( chunkNumber );
+ msg->setDataOffset( offset );
+ msg->setData( data, dataLength );
+ return msg;
+}
+
+TEST(a_wrapped_command_chunk_cannot_write_outside_its_own_buffer)
+{
+ UnsignedByte first[4] = { 0x11, 0x22, 0x33, 0x44 };
+ UnsignedByte second[4] = { 0x55, 0x66, 0x77, 0x88 };
+
+ NetWrapperCommandMsg *chunk0 = makeWrapperChunk( 0, 2, 8, 0, first, 4 );
+ NetCommandWrapperListNode *node = newInstance( NetCommandWrapperListNode )( chunk0 );
+
+ CHECK_EQ( (Int)node->getRawDataLength(), 8 );
+
+ node->copyChunkData( chunk0 ); // the honest first half
+ CHECK( !node->isComplete() );
+ CHECK_EQ( node->getPercentComplete(), 50 );
+
+ // an offset past the end of the block
+ NetWrapperCommandMsg *farOffset = makeWrapperChunk( 1, 2, 8, 4096, second, 4 );
+ node->copyChunkData( farOffset );
+ CHECK( !node->isComplete() );
+ CHECK_EQ( node->getPercentComplete(), 50 ); // refused, and not counted as received
+ farOffset->detach();
+
+ // a length no packet could have carried
+ NetWrapperCommandMsg *hugeLength = makeWrapperChunk( 1, 2, 8, 4, second, MAX_PACKET_SIZE + 1 );
+ node->copyChunkData( hugeLength );
+ CHECK( !node->isComplete() );
+ hugeLength->detach();
+
+ // in range on its own, but the tail runs off the end
+ NetWrapperCommandMsg *overhang = makeWrapperChunk( 1, 2, 8, 6, second, 4 );
+ node->copyChunkData( overhang );
+ CHECK( !node->isComplete() );
+ overhang->detach();
+
+ // and the honest second half still completes the command
+ NetWrapperCommandMsg *chunk1 = makeWrapperChunk( 1, 2, 8, 4, second, 4 );
+ node->copyChunkData( chunk1 );
+ CHECK( node->isComplete() );
+ CHECK_EQ( node->getPercentComplete(), 100 );
+ CHECK_MEM( first, node->getRawData(), 4 );
+ CHECK_MEM( second, node->getRawData() + 4, 4 );
+ chunk1->detach();
+
+ node->deleteInstance();
+ chunk0->detach();
+}
+
//////////////////////////////////////////////////////////////////////////////
// Starting a new game
//////////////////////////////////////////////////////////////////////////////