fix possible buffer overflow in world streaming packet

This commit is contained in:
DrPerkyLegit committed 2026-10-30 14:47:31 -04:00
1 parent d051e5bab6
commit b3e474ac87
2 files changed
+31 -15

No files matched your search

+29 -13
View File
@@ -13,6 +13,8 @@
#define BLOCK_REGION_UPDATE_FULLCHUNK 0x01
#define BLOCK_REGION_UPDATE_ZEROHEIGHT 0x02 // added so we can still send a byte for ys, which really needs the range 0-256
#define MAX_COMPRESSED_CHUNK_SIZE 5 * 1024 * 1024
BlockRegionUpdatePacket::~BlockRegionUpdatePacket()
{
@@ -71,19 +73,34 @@ BlockRegionUpdatePacket::BlockRegionUpdatePacket(int x, int y, int z, int xs, in
}
else
{
// We don't know how this will compress - just make a fixed length buffer to initially decompress into
// Some small sets of blocks can end up compressing into something bigger than their source
unsigned char *ucTemp = new unsigned char[(256 * 16 * 16 * 5)/2];
unsigned int inputSize = (256 * 16 * 16 * 5)/2;
Compression::getCompression()->CompressLZXRLE(ucTemp, &inputSize, rawBuffer.data, (unsigned int) rawBuffer.length);
//app.DebugPrintf("Chunk (%d,%d) compressed from %d to size %d\n", x>>4, z>>4, rawBuffer.length, inputSize);
unsigned char *ucTemp2 = new unsigned char[inputSize];
memcpy(ucTemp2,ucTemp,inputSize);
delete [] ucTemp;
buffer = byteArray(ucTemp2,inputSize);
unsigned int outputCapacity = static_cast<unsigned int>(rawBuffer.length) * 2 + 1024;
outputCapacity = outputCapacity < MAX_COMPRESSED_CHUNK_SIZE ? outputCapacity : MAX_COMPRESSED_CHUNK_SIZE;
unsigned char *compressedBuffer = nullptr;
unsigned int compressedSize = 0;
HRESULT compressionResult = E_FAIL;
while (outputCapacity > 0) {
compressedBuffer = new unsigned char[outputCapacity];
compressedSize = outputCapacity;
compressionResult = Compression::getCompression()->CompressLZXRLE(compressedBuffer, &compressedSize, rawBuffer.data, static_cast<unsigned int>(rawBuffer.length));
if (compressionResult == S_OK && compressedSize <= outputCapacity) break;
delete[] compressedBuffer;
compressedBuffer = nullptr;
if (outputCapacity == MAX_COMPRESSED_CHUNK_SIZE) break;
outputCapacity = outputCapacity * 2 < MAX_COMPRESSED_CHUNK_SIZE ? outputCapacity * 2 : MAX_COMPRESSED_CHUNK_SIZE;
}
if (compressedBuffer != nullptr) {
buffer = byteArray(compressedBuffer, compressedSize);
size = compressedSize;
} else {
size = 0;
buffer = byteArray();
app.DebugPrintf("[BRUP-READ] *** CHUNK COMPRESSION FAILED at (%d,%d), rawSize=%u hr=0x%08X\n", x >> 4, z >> 4, static_cast<unsigned int>(rawBuffer.length), static_cast<unsigned int>(compressionResult));
}
delete [] rawBuffer.data;
size = inputSize;
}
}
@@ -105,7 +122,6 @@ void BlockRegionUpdatePacket::read(DataInputStream *dis) //throws IOException
levelIdx = ( size >> 30 ) & 3;
size &= 0x3fffffff;
const int MAX_COMPRESSED_CHUNK_SIZE = 5 * 1024 * 1024;
if (size < 0 || size > MAX_COMPRESSED_CHUNK_SIZE)
{
size = 0;