Missing destination-bounds validation in Android remote codec input path — out-of-bounds write in the :media process
The Android remote codec copied a demuxed media sample into the platform codec input buffer using a length taken from the container, without checking it against the destination buffer's capacity. A web page serving a crafted AAC-in-MP4 could therefore cause an out-of-bounds write in the more privileged :media process, demonstrated as a deterministic crash with no user interaction.
Originally disclosed on Mozilla
- Fixed in 11 days
- Paid in 44 days
- Disclosed after 11 days
Created attachment 9597718 poc.zip
Component: GeckoView :: Media / Android codec IPC Type: Out-of-bounds write (memory safety) Reachability: Web content (crafted media) Reproduced on: official Firefox Nightly 154.0a1 for Android (arm64)
Summary
When the Android remote codec copies a demuxed input sample into the platform
MediaCodec input buffer, the copy length (info.size) is taken from the
cross-process Sample and passed to a raw memcpy into the codec input
ByteBuffer without validating it against that buffer's capacity. The copy
length is derived from the container (sample size) and the copied bytes are
derived from the media payload, while the destination is a fixed-size codec
input buffer. No layer between the demuxer and the native copy clamps the length
to the destination size.
A web page that serves a structurally consistent MP4 containing a deliberately
oversized, malformed AAC sample (a sample whose declared stsz/mdat size
exceeds the codec input buffer) causes an attacker-controlled-length,
attacker-supplied-content out-of-bounds sequential write in the non-isolated
:media process (which runs under the application UID and is more privileged
than the isolated web-content process). On official Firefox Nightly 154.0a1 this
is demonstrated with no user interaction — the page autoplays the crafted media
muted — as a deterministic crash of :media with SIGSEGV inside
__memcpy_aarch64_simd, with the symbolized stack
ICodec$Stub.onTransact → Codec.queueInput → feedSampleToBuffer → SampleBuffer.writeToByteBuffer → memcpy.
Impact
- It is an out-of-bounds write —
memcpycopies past the end of the destination codec input buffer. It is not a read, not a null dereference, and not resource exhaustion. - The attacker controls the length of the write (
info.size, taken from the MP4 sample size) and the bytes written (themdatpayload). - The copy is sequential from the destination base, so every byte beyond the
C_buf-sized (here 8192-byte) input buffer is overwritten with attacker-supplied data. Whether a given length faults is purely a function of memory layout: a length that runs off the buffer's mapping faults (theSIGSEGVshown below); a length that stays within neighbouring mapped memory overwrites adjacent allocations without crashing (consistent with the observation that a 64 KiB sample reproduces less reliably than an 8 MiB one). Either way, bytes pastC_bufare written — that is memory corruption. - The corruption lands in the non-isolated
:mediaprocess (full application UID) and is driven entirely from the isolated, sandboxed web-content process (android:isolatedProcess="true"). The write therefore crosses from a low-privilege origin into a higher-privilege process.
The crash demonstrated below is the conservative, directly-observable lower bound of this primitive.
Affected code
| File | Location | Issue |
|---|---|---|
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/SampleBuffer.java |
writeToByteBuffer() L70–80 |
copies size bytes into dest with no size > dest.capacity() check |
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/Codec.java |
feedSampleToBuffer() L160–165 |
len = sample.info.size; copies into mCodec.getInputBuffer(index) (capacity C_buf) |
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/CodecProxy.java |
fillInputBuffer() L271, L301 |
only checks the shared-memory buffer (C_shm), on the sender side |
mozglue/android/SharedMemNatives.cpp |
nativeWriteToDirectBuffer L37–54 |
memcpy(to, from + offset, size) — only null-checks the pointers |
dom/media/platforms/android/RemoteDataDecoder.cpp |
L1090–1092 | info.size = aSample->Size(); the only guard is a debug-only MOZ_ASSERT(... <= INT32_MAX) |
dom/media/platforms/android/AndroidDecoderModule.cpp |
L105–123 | routes AAC audio to MediaCodec (web-reachable path) |
Root cause
Receiver-side copy has no destination-bounds check
In the :media process, Codec.feedSampleToBuffer()
(Codec.java:154-165):
int len = 0;
final Sample sample = mInputSamples.poll().sample;
if (!sample.isEOS() && sample.bufferId != Sample.NO_BUFFER) {
len = sample.info.size; // attacker-influenced, from the Sample
final ByteBuffer buf = mCodec.getInputBuffer(index); // capacity C_buf
mSamplePool
.getInputBuffer(sample.bufferId)
.writeToByteBuffer(buf, sample.info.offset, len); // copies len bytes into buf
}
SampleBuffer.writeToByteBuffer()
(SampleBuffer.java:70-80) forwards to the native copy with no
capacity validation:
public void writeToByteBuffer(final ByteBuffer dest, final int offset, final int size) {
if (!dest.isDirect()) { throw new IOException(...); }
nativeWriteToDirectBuffer(mSharedMem.getPointer(), dest, offset, size);
}
SharedMemNatives.cpp
(SharedMemNatives.cpp:37-54) performs the copy with only null checks:
void ...nativeWriteToDirectBuffer(JNIEnv* jenv, jclass, jlong src, jobject dest,
jint offset, jint size) {
uint8_t* from = reinterpret_cast<uint8_t*>(src);
void* to = jenv->GetDirectBufferAddress(dest);
// ... only null-checks from/to ...
memcpy(to, from + offset, size); // OOB write when size > capacity(dest)
}
There is no check that size <= GetDirectBufferCapacity(dest).
The size is never clamped to the destination
RemoteDataDecoder.cpp
(RemoteDataDecoder.cpp:1090-1092) sets info.size directly from the demuxed sample size,
guarded only by a debug-only assert:
MOZ_ASSERT(aSample->Size() <= INT32_MAX);
mInputBufferInfo->Set(0, static_cast<int32_t>(aSample->Size()),
aSample->mTime.ToMicroseconds(), 0);
The demuxer does not cap single-sample size
A single sample's size flows from the container, bounded only by integer-overflow checks and the resource length — there is no fixed cap and no relation to the declared resolution:
- Non-fragmented (stsz):
create_sample_tableunstable.rs:172-179 —end_offset = start_offset + sample_size, onlyCheckedIntegeroverflow +end_offset==0. - Fragmented (trun): MoofParser.cpp:995-1016, where
sampleSizeis a rawReadU32(), validated only byMakeMediaByteRangeCheckedMoofParser.cpp:39-49 (overflow /INT64_MAX). MediaRawDataWriter::SetSize(MediaData.cpp:785) is a fallible allocation with no cap.
KEY_MAX_INPUT_SIZE is never set (not in AndroidDecoderModule.cpp; not in
FormatParam.java), so C_buf is the codec's default for the negotiated
format (the AAC decoder reported max-input-size = 8192, below).
Why the existing check does not help
CodecProxy.fillInputBuffer()
(CodecProxy.java:271,301) checks
buffer.capacity() < size, but buffer is the shared-memory buffer obtained
via dequeueInput(info.size), so C_shm = max(info.size, default) ≥ info.size
and the check never fires for info.size. It also runs in the sender
(content) process, not in the receiver.
Data flow
content process :media process (application UID)
--------------- -------------------------------
demux crafted MP4
→ MediaRawData size = S (uncapped)
RemoteDataDecoder: info.size = S
CodecProxy.dequeueInput(S) ───────────▶ allocate shared mem, C_shm = S
CodecProxy.fillInputBuffer (copies S into shm; checks C_shm, passes)
ICodec.queueInput (Binder) ───────────▶ Codec.feedSampleToBuffer
len = info.size = S
buf = getInputBuffer(idx) // capacity C_buf
writeToByteBuffer(buf, off, S)
→ nativeWriteToDirectBuffer
→ memcpy(buf, shm+off, S) // OOB write when S > C_buf
C_shm ≥ S keeps the source read in bounds; the unchecked write into the
C_buf-sized destination is out of bounds.
Reachability (web content)
AndroidDecoderModule::SupportsMimeType
(AndroidDecoderModule.cpp:105-123) routes AAC through MediaCodec, while
MP3/Opus/Vorbis/FLAC use the in-tree ffvpx software decoders:
// Always use our own software decoder (in ffvpx) for audio except for AAC
case MediaCodec::MP3: ... case MediaCodec::Opus: ... case MediaCodec::FLAC:
return media::DecodeSupportSet{}; // not MediaCodec
case MediaCodec::H264:
return DecodeSupport::SoftwareDecode; // MediaCodec
So an <audio>/<video> element pointing at a crafted AAC-in-MP4 reaches
this code path from ordinary web content.
Process topology (from the manifest): content runs as isolatedTab
(android:isolatedProcess="true", near-zero privileges) while
MediaManager/:media runs android:isolatedProcess="false" (full application
UID). The write therefore crosses from the isolated web-content process into the
non-isolated, higher-privileged :media process.
Proof of concept
A valid AAC-in-MP4 is taken and its first audio sample is inflated: the stsz
entry for that sample is increased and matching filler bytes are appended to the
mdat payload so the tables and file remain structurally consistent (the sample
bitstream itself is intentionally not valid AAC; the bad copy happens before the
decoder parses it). Generator:
web-poc/craft_aac_oversized.py (65,536-byte
sample) and an 8 MiB variant; outputs:
web-poc/out/aac-oversized.m4a and
web-poc/out/aac-huge.m4a, both derived from the in-tree test file
dom/media/test/small-shot.m4a. Trigger page:
web-poc/auto-poc.html, which plays the crafted file via
<video autoplay muted> / <audio autoplay muted> — muted media autoplays
without any user gesture, so merely opening the page is enough.
Steps:
cd web-poc
python craft_aac_oversized.py # 64 KiB variant; an 8 MiB variant is also produced
python -m http.server 8137 --bind 0.0.0.0
adb reverse tcp:8137 tcp:8137
# trigger — crashes :media with NO user interaction (muted autoplay):
adb shell am start -a android.intent.action.VIEW -d "http://localhost:8137/auto-poc.html" -p org.mozilla.fenix
# control — must NOT crash:
adb shell am start -a android.intent.action.VIEW -d "http://localhost:8137/tap-poc.html?clean=1" -p org.mozilla.fenix
No clicks or taps are issued; loading auto-poc.html alone reproduces the crash. The
first sample only needs to exceed C_buf; a sample well past the buffer's mapping
(8 MiB) makes the fault deterministic. (A "tap to play" variant,
web-poc/tap-poc.html, reproduces the same crash on a single
user gesture.)
Crash evidence (official build)
Build: Firefox Nightly 154.0a1 (org.mozilla.fenix, versionCode 2016167170) —
the official Mozilla CI arm64 build
(gecko.v2.mozilla-central.latest.mobile.fenix-nightly → target.arm64-v8a.apk).
Device: Google Pixel 7, Android 16, arm64-v8a.
no injected libraries (libsigchain in the trace is the standard ART signal chain).
Codec input size reported by the decoder during the live decode:
CCodec: int32_t max-input-size = 8192
A/B: the unmodified AAC control leaves :media alive; merely loading the
muted-autoplay PoC page (no clicks/taps) kills it:
GeckoRemoteManager: remote codec is dead
ActivityManager: Process org.mozilla.fenix:media (pid 6461) has died
Zygote: Process 6461 exited due to signal 11 (Segmentation fault)
SIGSEGV backtrace (pid 6461) — Java frames symbolized by the official build:
#03 pc 00069c54 libc.so (__memcpy_aarch64_simd+276)
#06 pc 019e1e32 base.vdex (org.mozilla.gecko.media.SampleBuffer.writeToByteBuffer+30)
#08 pc 019d741e base.vdex (org.mozilla.gecko.media.Codec$InputProcessor.feedSampleToBuffer+202)
#10 pc 019d7580 base.vdex (org.mozilla.gecko.media.Codec$InputProcessor.queueSample+52)
#14 pc 019d8e4a base.vdex (org.mozilla.gecko.media.Codec.queueInput+6)
#16 pc 019df520 base.vdex (org.mozilla.gecko.media.ICodec$Stub.onTransact+220)
#17 pc 008c8e4c boot.oat (android.os.Binder.execTransactInternal+828)
#23 pc 001ec48c libandroid_runtime.so (JavaBBinder::onTransact+156)
#24 pc 0004b58c libbinder.so (android::IPCThreadState::executeCommand+716)
The stack shows the cross-process ICodec input call (ICodec$Stub.onTransact)
running Codec.queueInput → Codec$InputProcessor.feedSampleToBuffer → SampleBuffer.writeToByteBuffer → memcpy, which faults writing the oversized sample
into the 8192-byte codec input buffer — exactly the path in Root cause, on an
unmodified official build with the Java frames symbolized directly (no inference).
Full raw crash log (all 32 frames): web-poc/tombstone-capture/nightly-full-crash-logcat.txt; curated summary: web-poc/tombstone-capture/nightly-official-crash.txt.
Optional strengthening
The one value not yet recorded directly is the destination ByteBuffer.capacity()
(versus the codec's reported max-input-size = 8192). A one-line temporary guard in
SampleBuffer.writeToByteBuffer records it and makes the overflow deterministic:
if (size > dest.capacity()) {
throw new IOException("OOB: sample size=" + size
+ ", codec input ByteBuffer capacity=" + dest.capacity());
}
Suggested fix
Validate on the receiver side and in the native layer (defense-in-depth).
SampleBuffer.writeToByteBuffer (and symmetrically readFromByteBuffer):
final int shmSize = mSharedMem != null ? mSharedMem.getSize() : 0;
if (offset < 0 || size < 0 || offset > shmSize || size > shmSize - offset
|| size > dest.capacity()) {
throw new IOException("Out-of-bound write");
}
SharedMemNatives.cpp: reject size < 0 and validate size against
GetDirectBufferCapacity(dest) (and offset + size against the source size)
before memcpy.
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 receiver-side copy in the :media process used the sample's declared size as the memcpy length without comparing it to the destination codec input buffer's capacity, and the sender-side check only validated the shared-memory buffer.
Impact
An attacker-controlled-length, attacker-supplied-content out-of-bounds write occurs in the non-isolated :media process, crossing from the sandboxed web-content process; the demonstrated effect is a deterministic crash, with silent corruption of adjacent memory possible when the length stays within mapped memory.
What the attacker needed
Victim opens a crafted web page; no user interaction needed (muted autoplay); Android Firefox build using MediaCodec for AAC
The fix
The receiver-side copy and the native memcpy should validate the size against the destination buffer's capacity (and offset plus size against the source) before copying.
How it was found
- Generate a crafted AAC-in-MP4 whose first sample is inflated in stsz with matching filler in mdat.
- Serve the files over a local HTTP server and forward the port to the device.
- Open the auto-play page in Firefox for Android, which plays the crafted file muted without any user gesture.
- Observe the :media process dying with SIGSEGV inside memcpy.
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 phone's media decoder has a small fixed-size scratch area for incoming audio data. The browser took the size of an audio chunk from the file itself and copied that many bytes into the scratch area without checking it fits, so a malicious file makes the copy spill past the end and corrupt nearby memory. Because this happens in a separate media helper process that has more privileges than the web page's sandbox, it is more serious than a normal crash.
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.