Out-of-bounds read in AVIFDecoderStream::ReadAt due to missing offset validation
The AVIF decoder's stream adapter computes the read size by subtracting the offset from the buffer length without validating the offset. An attacker could supply a crafted AVIF file with an out-of-range offset, causing an out-of-bounds read of memory.
Originally disclosed on Mozilla
- Fixed in 5 days
- Paid in 5 days
- Disclosed after 5 days
Description
AVIFDecoderStream::ReadAt computes the read size using mBuffer->length() - offset without validating offset. If offset is negative or greater than the buffer length, the subtraction underflows/overflows and size remains large, leading to memcpy reading past the end of mBuffer. This is an out‑of‑bounds read in a code path processing untrusted AVIF input.
Affected Code
image/decoders/nsAVIFDecoder.cpp:140size = std::min(size, size_t(mBuffer->length() - offset)); ... memcpy(data, mBuffer->begin() + offset, size);- Reference for expected behavior:
dom/media/mp4/BufferStream.cpp:19validates offsets and returnsNS_ERROR_DOM_MEDIA_RANGE_ERR.
Proof of Concept (minimal)
Vector<uint8_t> buffer; buffer.append(0); RefPtr<AVIFDecoderStream> stream = new AVIFDecoderStream(&buffer); uint8_t out = 0; size_t bytes = 0; stream->ReadAt(2, &out, sizeof(out), &bytes);
This triggers an out‑of‑bounds read because offset is beyond buffer.length().
Suggested Fix
Add range checks before computing the remaining length, similar to BufferStream::ReadAt: if (offset < 0 || uint64_t(offset) > mBuffer->length()) { return NS_ERROR_DOM_MEDIA_RANGE_ERR; } size_t remaining = mBuffer->length() - size_t(offset); size = std::min(size, remaining); if (size == 0) { return NS_ERROR_DOM_MEDIA_RANGE_ERR; }
Notes
- This bug is in the AVIF ByteStream adapter and is not aligned with other ByteStream implementations that validate offsets.
- I’m preparing an ASan-only gtest to make this deterministic, but in my current headless VM environment the Firefox gtest harness aborts before executing any tests due to an ASan runtime thread-interceptor issue (‘Joining already joined thread’ / pthread intercept failure in glib). I’ll attach the ASan crash log / gtest output from a compatible environment as follow-up.
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
The code subtracts the offset from the buffer length without first checking that the offset is within the buffer bounds, causing an integer underflow and an oversized memcpy.
Impact
An attacker could cause an out-of-bounds read by supplying a crafted AVIF file, potentially leading to a crash or information disclosure.
What the attacker needed
Ability to supply a crafted AVIF file
The fix
The code should validate the offset before computing the remaining length, returning an error if the offset is negative or exceeds the buffer length, similar to BufferStream::ReadAt.
How it was found
- Create a small buffer containing a single zero byte.
- Wrap the buffer in an AVIFDecoderStream object.
- Call ReadAt with an offset of 2, which is beyond the buffer length.
- Observe the out-of-bounds read.
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.
A program reads image data from a buffer. It calculates how many bytes to copy by subtracting a position from the buffer size, but it never checks that the position is inside the buffer. If the position is too large, the calculation wraps around and the program copies memory from outside the buffer, which can crash or leak data.
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.