Skip to content

Commit 35c20e0

Browse files
committed
Set MP4Array size only after successful alloc.
m_numElements was set before checking newSize and trying MP4Realloc. In case the size check or MP4Realloc threw an exception, deallocators would iterate over the never allocated new number of elements and attempt calling delete on them. Also check size of ftyp atom to avoid the uint32_t underflow that caused the bad alloc request. Addresses: https://nvd.nist.gov/vuln/detail/CVE-2018-17236
1 parent b8d0528 commit 35c20e0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/atom_ftyp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ void MP4FtypAtom::Generate()
5353

5454
void MP4FtypAtom::Read()
5555
{
56+
if ( m_size < 8 )
57+
throw new EXCEPTION("Invalid ftyp atom size");
58+
5659
compatibleBrands.SetCount( (m_size - 8) / 4 ); // brands array fills rest of atom
5760
MP4Atom::Read();
5861
}

src/mp4array.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ class MP4Array {
7777
throw new PLATFORM_EXCEPTION("illegal array index", ERANGE); \
7878
} \
7979
if (m_numElements == m_maxNumElements) { \
80-
m_maxNumElements = max(m_maxNumElements, (MP4ArrayIndex)1) * 2; \
80+
MP4ArrayIndex newSize = max(m_maxNumElements, (MP4ArrayIndex)1) * 2; \
8181
m_elements = (type*)MP4Realloc(m_elements, \
82-
m_maxNumElements * sizeof(type)); \
82+
newSize * sizeof(type)); \
83+
m_maxNumElements = newSize; \
8384
} \
8485
memmove(&m_elements[newIndex + 1], &m_elements[newIndex], \
8586
(m_numElements - newIndex) * sizeof(type)); \
@@ -100,12 +101,12 @@ class MP4Array {
100101
} \
101102
} \
102103
void Resize(MP4ArrayIndex newSize) { \
104+
if ( (uint64_t) newSize * sizeof(type) > 0xFFFFFFFF ) \
105+
throw new PLATFORM_EXCEPTION("requested array size exceeds 4GB", ERANGE); /* prevent overflow */ \
106+
m_elements = (type*)MP4Realloc(m_elements, \
107+
newSize * sizeof(type)); \
103108
m_numElements = newSize; \
104109
m_maxNumElements = newSize; \
105-
if ( (uint64_t) m_maxNumElements * sizeof(type) > 0xFFFFFFFF ) \
106-
throw new PLATFORM_EXCEPTION("requested array size exceeds 4GB", ERANGE); /* prevent overflow */ \
107-
m_elements = (type*)MP4Realloc(m_elements, \
108-
m_maxNumElements * sizeof(type)); \
109110
} \
110111
\
111112
type& operator[](MP4ArrayIndex index) { \

0 commit comments

Comments
 (0)