Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer overflow(s) in CDataFileReader::Open() in engine/shared/datafile.cpp #2070

0n3m4ns4rmy opened this issue Mar 22, 2019 · 1 comment


Copy link

commented Mar 22, 2019

Hello Teeworlds dev team,

There is an integer overflow bug in CDataFileReader::Open() which can lead to a buffer overflow and possibly RCE. The integer overflow(s) occurs when calculating the Size and AllocSize.

unsigned Size = 0;
Size += Header.m_NumItemTypes*sizeof(CDatafileItemType);
Size += (Header.m_NumItems+Header.m_NumRawData)*sizeof(int);
if(Header.m_Version == 4)
	Size += Header.m_NumRawData*sizeof(int); // v4 has uncompressed data sizes aswell
Size += Header.m_ItemSize;

unsigned AllocSize = Size;
AllocSize += sizeof(CDatafile); // add space for info structure
AllocSize += Header.m_NumRawData*sizeof(void*); // add space for data pointers

As you can see there is no check to see if Size or AllocSize overflow. The integer overflow of AllocSize can lead to AllocSize being smaller than Size. AllocSize then gets used to allocate memory for the CDataFile object and Size gets used to read data to this object leading to a buffer overflow.

CDatafile *pTmpDataFile = (CDatafile*)mem_alloc(AllocSize, 1);
pTmpDataFile->m_Header = Header;
pTmpDataFile->m_DataStartOffset = sizeof(CDatafileHeader) + Size;
pTmpDataFile->m_ppDataPtrs = (char**)(pTmpDataFile+1);
pTmpDataFile->m_pData = (char *)(pTmpDataFile+1)+Header.m_NumRawData*sizeof(char *);
pTmpDataFile->m_File = File;
pTmpDataFile->m_Crc = Crc;

// clear the data pointers
mem_zero(pTmpDataFile->m_ppDataPtrs, Header.m_NumRawData*sizeof(void*));

// read types, offsets, sizes and item data
unsigned ReadSize = io_read(File, pTmpDataFile->m_pData, Size);


Mans van Someren


This comment has been minimized.

Copy link

commented Mar 25, 2019

I guess we would need to clamp the inputs

  • Header.m_NumItemTypes (probably not)
  • Header.m_NumItems
  • Header.m_NumRawData
  • Header.m_ItemSize

to reasonable values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.