Probable write beyond bounds in GetSurfaceDataImpl()
The GetSurfaceDataImpl() function in Firefox's nsContentUtils.cpp mishandles integer overflow when computing buffer sizes for large images. On overflow it allocates a 1-byte buffer but then copies a much larger amount of data into it, which could cause a write beyond bounds.
- Fixed in 6 days
- Paid in 10 days
- Disclosed after 6 days
GetSurfaceDataImpl() (dom/base/nsContentUtils.cpp) appears improperly to handle large images, If it detects overflow, it allocates -- and uses -- a 1-byte buffer instead of returning failure status.
This seems very likely to be incorrect. I don't know how to induce FF to run this code, though, so I have no POC.
The bug is in line 8053:
8039: template <typename GetSurfaceDataContext = GetSurfaceDataRawBuffer>
8040: typename GetSurfaceDataContext::ReturnType
8041: GetSurfaceDataImpl(mozilla::gfx::DataSourceSurface* aSurface,
8042: size_t* aLength, int32_t* aStride,
8043: GetSurfaceDataContext aContext = GetSurfaceDataContext())
8044: {
8045: mozilla::gfx::DataSourceSurface::MappedSurface map;
8046: if (!aSurface->Map(mozilla::gfx::DataSourceSurface::MapType::READ, &map)) {
8047: return GetSurfaceDataContext::NullValue();
8048: }
8049:
8050: mozilla::gfx::IntSize size = aSurface->GetSize();
8051: mozilla::CheckedInt32 requiredBytes =
8052: mozilla::CheckedInt32(map.mStride) * mozilla::CheckedInt32(size.height);
...which sets |maxBufLen == 0| on overflow...
8053: size_t maxBufLen = requiredBytes.isValid() ? requiredBytes.value() : 0;
8054: mozilla::gfx::SurfaceFormat format = aSurface->GetFormat();
8055:
8056: // Surface data handling is totally nuts. This is the magic one needs to
8057: // know to access the data.
...then this line computes |bufLen| as something that seems like it must be nonzero, maybe even negative at times...
8058: size_t bufLen = maxBufLen - map.mStride + (size.width * BytesPerPixel(format));
8059:
8060: // nsDependentCString wants null-terminated string.
8061: typename GetSurfaceDataContext::ReturnType surfaceData = aContext.Allocate(maxBufLen + 1);
...then line 8061 allocates a 1-byte buffer...
8062: if (GetSurfaceDataContext::GetBuffer(surfaceData)) {
...and the following lines copy what must be >> 1 byte of data into the 1-byte buffer. (BTW, GetSurfaceDataRawBuffer::GetBuffer() just returns a pointer to the buffer that was allocated by GetSurfaceDataRawBuffer::Allocate()),
8063: memcpy(GetSurfaceDataContext::GetBuffer(surfaceData),
8064: reinterpret_cast<char*>(map.mData),
8065: bufLen);
8066: memset(GetSurfaceDataContext::GetBuffer(surfaceData) + bufLen,
8067: 0,
8068: maxBufLen - bufLen + 1);
8069: }
8070:
8071: *aLength = maxBufLen;
8072: *aStride = map.mStride;
8073:
8074: aSurface->Unmap();
8075: return surfaceData;
8076: }
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 overflow check sets maxBufLen to 0 but the code continues to compute bufLen and copy data instead of returning an error.
Impact
Could lead to a write beyond bounds in the browser, potentially causing a crash or memory corruption, though the reporter has no proof of concept and does not know how to trigger the code.
What the attacker needed
Large image causing integer overflow; code path reachable in Firefox
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.
When a program needs to copy image data, it first works out how big the copy will be. Here, if that size calculation overflows, the code falls back to a tiny 1-byte buffer but still copies the full image data into it. That means data is written past the end of the buffer, which can crash the browser or potentially let an attacker run their own code.
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.