Overflow in nsAttrAndChildArray::GrowBy() causes buffer overrun
An integer overflow in nsAttrAndChildArray::GrowBy() causes the function to allocate a buffer that is too small for the data written into it. An attacker could potentially trigger a buffer overrun, though the report notes the bug is likely only manifest on x64 builds and the proof-of-concept was still being tested.
- Fixed in 26 days
- Paid in 32 days
- Disclosed after 26 days
Created attachment 8773534
bug399_poc_1.htm
nsAttrAndChildArray::GrowBy() (dom\base\nsAttrAndChildArray.cpp) can experience an integer overflow, which causes the function to allocate a buffer that is too small for the data that other members of nsAttrAndChildArray write into it. Probably the bug can be manifested only on x64 builds. The bug is in line 792:
765: bool
766: nsAttrAndChildArray::GrowBy(uint32_t aGrowSize)
767: {
768: CheckedUint32 size = 0;
769: if (mImpl) {
770: size += mImpl->mBufferSize;
771: size += NS_IMPL_EXTRA_SIZE;
772: if (!size.isValid()) {
773: return false;
774: }
775: }
776:
777: CheckedUint32 minSize = size.value();
778: minSize += aGrowSize;
779: if (!minSize.isValid()) {
780: return false;
781: }
782:
783: if (minSize.value() <= ATTRCHILD_ARRAY_LINEAR_THRESHOLD) {
784: do {
785: size += ATTRCHILD_ARRAY_GROWSIZE;
786: if (!size.isValid()) {
787: return false;
788: }
789: } while (size.value() < minSize.value());
790: }
791: else {
792: size = 1u << mozilla::CeilingLog2(minSize.value());
...whose RHS undetectably overflows if minSize.value() >= 0x80000001. The rest of the expression then assigns the overflowed value to |size|, which views it as valid, so the later check on lines 798-800:
793: }
794:
795: bool needToInitialize = !mImpl;
796: CheckedUint32 neededSize = size;
797: neededSize *= sizeof(void*);
798: if (!neededSize.isValid()) {
799: return false;
800: }
doesn't detect the problem, causing line 802:
801:
802: Impl* newImpl = static_cast<Impl*>(realloc(mImpl, neededSize.value()));
to allocate a buffer that is far too small to contain the data.
I am currently running the attached POC to see whether it will crash FF, but it's slow, so I've posted the bug anyway.
You can demonstrate the bug by using the debugger to set aGrowSize such that line 778 computes minSize >= 0x80000001. Then step through line 792 and notice that |size| becomes tiny.
Cf. https://bugzilla.mozilla.org/show_bug.cgi?id=1172055 , which fixed other problems in this function, but left this one tiny window open.
The bug is still present in tip-of-the-tree trunk http://hg.mozilla.org/mozilla-central/file/tip/dom/base/nsAttrAndChildArray.cpp .
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the left-shift expression `1u << CeilingLog2(minSize.value())` overflows when minSize is large, and the overflowed result is assigned to a checked integer without detection.
Impact
The bug could cause a buffer overrun, potentially leading to a crash or arbitrary code execution, but the report notes it probably only manifests on x64 builds and the proof-of-concept was still being tested.
What the attacker needed
X64 build likely required; ability to control aGrowSize
How it was found
- Set aGrowSize so that line 778 computes minSize >= 0x80000001.
- Step through line 792 and observe that |size| becomes tiny.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This is a bug in a web browser's code that manages a list of attributes and child elements. When the list needs to grow, a calculation can overflow and produce a very small number, so the program reserves too little memory. Later, writing data into that small space can corrupt memory, which might let an attacker run harmful code or crash the browser.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.