Skip to content
Disclosed report CVE-2016-1946

Truncation in MoofParser::Metadata causes writing beyond buffer's end

q1 Follow

Originally disclosed on Mozilla

Critical View original
  • Fixed in 18 days
  • Paid in 17 days
  • Disclosed after 18 days
Text
Created attachment 8697727
poc.7z: use as described in comment 0 (note: > 4 GB when unzipped)

MoofParser::Metadata (media\libstagefright\binding\MoofParser.cpp) can experience an integer overflow. When it does, it allocates a buffer that is smaller than it ought to be. The function then reads data directly from a specially-contrived MP4 file's FTYP atom into that memory, overwriting whatever else is there. The attacker can exactly control the data written and the amount of overwriting -- from 1 byte to ~4 GB -- by adjusting the lengths and contents of the file's FTYP and MOOV atoms.

I think the bug is practically exploitable only on 32-bit builds (because of the required file length), and the discussion below assumes this condition.

The bug appears still to be present in trunk, though there have been minor changes to the affected file since FF 42.0 was released: http://hg.mozilla.org/mozilla-central/file/tip/media/libstagefright/binding/MoofParser.cpp .


Details
-------

The bug is in line 194, which doesn't check for overflow:

184: already_AddRefed<mozilla::MediaByteBuffer>
185: MoofParser::Metadata()
186: {
187:   MediaByteRange ftyp;
188:   MediaByteRange moov;
189:   ScanForMetadata(ftyp, moov);
190:   if (!ftyp.Length() || !moov.Length()) {
191:     return nullptr;
192:   }
193:   nsRefPtr<MediaByteBuffer> metadata = new MediaByteBuffer();
194:   if (!metadata->SetLength(ftyp.Length() + moov.Length(), fallible)) {
195:     // OOM
196:     return nullptr;
197:   }
198: 
199:   nsRefPtr<mp4_demuxer::BlockingStream> stream = new BlockingStream(mSource);
200:   size_t read;
201:   bool rv =
202:     stream->ReadAt(ftyp.mStart, metadata->Elements(), ftyp.Length(), &read);
203:   if (!rv || read != ftyp.Length()) {
204:     return nullptr;
205:   }
206:   rv =
207:     stream->ReadAt(moov.mStart, metadata->Elements() + ftyp.Length(), moov.Length(), &read);
208:   if (!rv || read != moov.Length()) {
209:     return nullptr;
210:   }
211:   return metadata.forget();
212: }

If the MP4 file is contrived so that the length fields of the FTYP and MOOV atoms overflow a size_t (4 bytes in 32-bit builds), line 194 overflows and allocates a too-small buffer. In the attached POC, the FTYP length field is 0x10008, and the MOOV length field is 0xffff0000. The total, computed as a 32-bit value, is thus 8, which is what line 194 passes to |metadata->SetLength()|, which then allocates that buffer. Line 202 then reads 0x10008 bytes into that buffer, overwriting 0x10000 bytes of memory beyond its end. The data that is written comes directly from the MP4 file. Line 207 (and therefore the attempt to play the file) fails, but that doesn't matter because the overwriting has already occurred.

An attacker can control what is written by adjusting the contents of the FTYP atom. The contents of the MOOV atom are immaterial, and are needed only to pad the file out to the required length.

An attacker can control the amount of overwriting by adjusting the lengths of the FTYP and MOOV atoms. For example, she can overwrite only 32 bytes by reducing the length of the FTYP atom to 0x28, and increasing the length of the MOOV atom to 0xffffffe0.

Note that the "8" in this discussion (e.g., "0x10008" and "0x28") is contrived so that MoofParser::Metadata copies the first 8 bytes of the FTYP atom into the allocated portion of the buffer, leaving the attacker with complete freedom to determine which data is written beyond its end.

An attacker can embed the MP4 file as a 1x1 pixel video so that the victim is unaware that she is "viewing" it.


Use the POC by putting a breakpoint on line 194, then loading the POC. Examine the computation of the sum (view disassembly) and notice that it's 8. Then step up to but not into line 202, open a memory window to the memory at |metadata->Elements()|, and step through line 202. Notice that 0x10000 bytes of attack data are written beyond its end. One or more threads might (or might not) crash when this occurs.

The original report as published on Mozilla's Bugzilla, by q1. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 critical-severity security bug in Audio/Video: Playback. Mozilla made it public on 30 December 2015 as CVE-2016-1946.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

More by q1

View all

71 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.