Overflow in nsDeque::GrowCapacity can cause memory-safety bug
- Fixed in 9 days
- Paid in 18 days
- Disclosed after 9 days
nsDeque::GrowCapacity (xpcom\glue\nsDeque.cpp) does not properly check for overflow. This can cause it to allocate a buffer that is too small to contain the deque's elements, some of which then are written to memory that the deque does not own.
Details:
If the deque's existing capacity |mCapacity| has the range [0x10000000, 0x1fffffff] (1-2 GB), then line 166 (below) computes a valid |theNewSize| with the range [0x40000000, 0x7ffffffc] and the tests on lines 167-70 pass:
163: bool 164: nsDeque::GrowCapacity() 165: { 166: int32_t theNewSize = mCapacity << 2; 167: NS_ASSERTION(theNewSize > mCapacity, "Overflow"); 168: if (theNewSize <= mCapacity) { 169: return false; 170: }
Control passes to line 171, which multiples |theNewSize| by |sizeof(void*)|, which is 4 on x86 architectures. The resulting values [0x100000000, 0x1fffffff0] overflow |size_t| on x86 architectures, and are truncated to [0, 0xfffffff0] [1]:
171: void** temp = (void**)malloc(theNewSize * sizeof(void*)); 172: if (!temp) { 173: return false; 174: }
Line 171 can allocate sizes near the lower end of this range, so then control passes to lines 181-82, which write beyond the end of the buffer:
175: 176: //Here's the interesting part: You can't just move the elements 177: //directly (in situ) from the old buffer to the new one. 178: //Since capacity has changed, the old origin doesn't make 179: //sense anymore. It's better to resequence the elements now. 180: 181: memcpy(temp, mData + mOrigin, sizeof(void*) * (mCapacity - mOrigin)); 182: memcpy(temp + (mCapacity - mOrigin), mData, sizeof(void*) * mOrigin); ...
Any elements added later are also written beyond the end of the buffer.
[1] There is no overflow on x64 architectures, where |sizeof(size_t)| == 8.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity security bug in XPCOM. Mozilla made it public on 6 September 2015 as CVE-2015-7221.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.