Out-of-bounds read in AVIFDecoderStream::ReadAt due to missing offset validation
Categories
(Core :: Graphics: ImageLib, defect)
Tracking
()
People
(Reporter: stevenjulian1528, Assigned: tnikkel)
References
(Regression)
Details
(4 keywords, Whiteboard: [client-bounty-form][adv-main148+][adv-esr115.33+] [adv-esr140.8+])
Attachments
(4 external files, 2 obsolete files)
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.
Updated•10 months ago
|
Comment 1•10 months ago
|
||
convenience links for the references in "Affected Code" above
image/decoders/nsAVIFDecoder.cpp:140 size = std::min(size, size_t(mBuffer->length() - offset)); ... memcpy(data, mBuffer->begin() + offset, size);
dom/media/mp4/BufferStream.cpp:19 validates offsets and returns NS_ERROR_DOM_MEDIA_RANGE_ERR.
I think they did try to validate the offset with the "min(size, length-offset)" calculation. But length-offset gets cast to size_t so an out of bounds offset gives a huge value rather than a negative one. It appears they did expect a negative value for this case because the next line checks if (size <= 0) {, but of course that can't ever be negative because size is unsigned.
I don't know if this could be exploited in Gecko. I expect the places that call ReadAt() would only call it with a valid offset when they know there's unconsumed data in the buffer. Note that BufferStream needs more checks because it has an internal concept of mStartOffset to worry about.
| Assignee | ||
Comment 2•10 months ago
|
||
(In reply to Daniel Veditz [:dveditz] from comment #1)
I don't know if this could be exploited in Gecko. I expect the places that call ReadAt() would only call it with a valid offset when they know there's unconsumed data in the buffer.
AFAICT the AVIFDecoderStream objects are only used via a SampleIterator object. That does some sanity checking itself, ie
https://searchfox.org/firefox-main/rev/d7b112b2ff492134ce4e4d2862351cd393320136/dom/media/mp4/SampleIterator.cpp#93
https://searchfox.org/firefox-main/rev/d7b112b2ff492134ce4e4d2862351cd393320136/dom/media/mp4/SampleIterator.cpp#117
so any potential attacker controlled offsets/lengths from the avif file would need to get through that or have some explanation of how it could get through (not saying it's impossible, just that it's a higher bar then just putting the offsets into the avif file to get this code to use it).
| Assignee | ||
Updated•10 months ago
|
Comment 3•10 months ago
|
||
Set release status flags based on info from the regressing bug 1788119
| Assignee | ||
Updated•10 months ago
|
| Assignee | ||
Comment 4•10 months ago
|
||
| Reporter | ||
Comment 5•10 months ago
|
||
SampleIterator does do a length check, but it only checks mByteRange.mEnd <= length and never validates mByteRange.mStart >= 0 or that the u64 offsets from mp4parse fit into int64. MP4SampleIndex just copies the u64 offsets into MediaByteRange without bounds checks, so overflow to negative is possible. If mEnd overflows negative, the end<=length check passes and ReadAt still sees an invalid offset. Since AVIFDecoderStream::ReadAt has no offset validation (unlike BufferStream), it’s still an OOB read. So this isn’t fully mitigated yet; I think ReadAt should still be hardened and/or SampleIterator should check start>=0 and range validity
| Reporter | ||
Comment 6•10 months ago
|
||
Also, the sanity checks in SampleIterator only verify mByteRange.mEnd <= length (dom/media/mp4/SampleIterator.cpp:93, dom/media/mp4/SampleIterator.cpp:117). They do not enforce mByteRange.mStart >= 0, mByteRange.mStart <= mByteRange.mEnd, or that mp4parse’s u64 offsets fit into int64. Those u64 values are copied into MediaByteRange (int64) without bounds checks (dom/media/mp4/MP4Metadata.cpp:38, dom/media/mp4/SampleIterator.cpp:433.
MediaByteRange is Interval<int64_t> in dom/media/MediaResource.h:22), so values > INT64_MAX can wrap negative. In that case mEnd <= length still passes and ReadAt sees a negative offset. So the current sanity check doesn’t prevent the OOB read. ReadAt still needs its own range validation (like BufferStream).
| Assignee | ||
Updated•10 months ago
|
| Assignee | ||
Comment 7•10 months ago
|
||
Comment on attachment 9538364 [details]
(secure)
Security Approval Request
- How easily could an exploit be constructed based on the patch?: probably pretty easy to get the OOB read, hard to disguise this, then they have to turn it into an exploit
- Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: Yes
- Which branches (beta, release, and/or ESR) are affected by this flaw, and do the release status flags reflect this affected/unaffected state correctly?: all
- If not all supported branches, which bug introduced the flaw?: Bug 1788119
- Do you have backports for the affected branches?: No
- If not, how different, hard to create, and risky will they be?: easy to create, the only change to this code was to change these functions to return an nsresult instead of a bool
- How likely is this patch to cause regressions; how much testing does it need?: unlikely, only offsets/sizes that overflow are affected
- Is the patch ready to land after security approval is given?: Yes
- Is Android affected?: Yes
Updated•10 months ago
|
Comment 8•10 months ago
|
||
This sounds like it is an out of bounds read, despite some existing checks, so I'll mark it sec-high.
Comment 9•10 months ago
|
||
Comment on attachment 9538364 [details]
(secure)
sec-approval+, a=dveditz
Comment 10•10 months ago
|
||
| Assignee | ||
Comment 11•10 months ago
|
||
Updated•10 months ago
|
| Assignee | ||
Comment 12•10 months ago
|
||
Updated•10 months ago
|
Updated•10 months ago
|
Updated•10 months ago
|
| Assignee | ||
Comment 13•10 months ago
|
||
Updated•10 months ago
|
Comment 14•10 months ago
|
||
firefox-esr140 Uplift Approval Request
- User impact if declined: sec-high
- Code covered by automated testing: yes
- Fix verified in Nightly: no
- Needs manual QE test: no
- Steps to reproduce for manual QE testing: n/a
- Risk associated with taking this patch: low
- Explanation of risk level: handling various integer overflow cases that real content would not encounter
- String changes made/needed: none
- Is Android affected?: yes
| Assignee | ||
Comment 15•10 months ago
|
||
Updated•10 months ago
|
Comment 16•10 months ago
|
||
firefox-esr115 Uplift Approval Request
- User impact if declined: sec-high
- Code covered by automated testing: yes
- Fix verified in Nightly: no
- Needs manual QE test: no
- Steps to reproduce for manual QE testing: n/a
- Risk associated with taking this patch: low
- Explanation of risk level: handling integer overflow real content would not encounter
- String changes made/needed: none
- Is Android affected?: yes
Comment 17•10 months ago
|
||
firefox-beta Uplift Approval Request
- User impact if declined: sec-high
- Code covered by automated testing: yes
- Fix verified in Nightly: no
- Needs manual QE test: no
- Steps to reproduce for manual QE testing: n/a
- Risk associated with taking this patch: low
- Explanation of risk level: handling integer overflow that real content does not encounter
- String changes made/needed: none
- Is Android affected?: yes
| Assignee | ||
Comment 18•10 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D279454
Comment 19•10 months ago
|
||
Updated•10 months ago
|
Updated•10 months ago
|
Comment 20•10 months ago
|
||
| uplift | ||
Updated•10 months ago
|
Updated•10 months ago
|
Updated•10 months ago
|
Updated•10 months ago
|
Comment 21•10 months ago
|
||
| uplift | ||
Updated•10 months ago
|
Updated•10 months ago
|
Comment 22•10 months ago
|
||
| uplift | ||
| Reporter | ||
Comment 23•10 months ago
|
||
Hi team, sorry to bother. This is my first time receiving a bounty, and I'm a bit unsure about the next steps.
I’ve marked it as sec-bounty+, but I haven't received an email regarding the payment process or documentation yet. Could you please advise if there's anything I need to fill out or if there's a typical timeline for processing? Thank you
| Assignee | ||
Updated•10 months ago
|
Comment 24•10 months ago
|
||
The security team is temporarily short-staffed and swamped and bounty processing time is far from normal. Please hang on a bit longer.
| Reporter | ||
Comment 25•9 months ago
|
||
So, i don't need to fill any informations for bounty processing? Or do you need me to fill any docs?
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
| Reporter | ||
Comment 26•9 months ago
|
||
It has been over a month since the bounty determined, but I haven't received email payment for bounty untill now. I just want to remind guys
| Reporter | ||
Comment 27•9 months ago
|
||
also guys can you edit my name in security advisories firefox from stevej to Steven Julian?
Updated•6 months ago
|
Updated•4 months ago
|
Description
•