Closed Bug 2037505 Opened 6 months ago Closed 6 months ago

Animated AVIF `co64` sample offsets above `INT64_MAX` can trigger negative-offset heap out-of-bounds reads in `nsAVIFDecoder`

Categories

(Core :: Graphics: ImageLib, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2010933

People

(Reporter: gganji11, Unassigned)

References

Details

(Keywords: ai-involved, reporter-external, Whiteboard: [client-bounty-form])

Attachments

(1 file)

Firefox animated AVIF co64 sample offsets above INT64_MAX can trigger negative-offset heap OOB reads

Title

Animated AVIF co64 sample offsets above INT64_MAX can trigger negative-offset heap out-of-bounds reads in nsAVIFDecoder

Product / Component

Firefox / Gecko image decoder, AVIF animated image path.

Tested against local gecko-dev checkout:

5836a062726f715fda621338a17b51aff30d0a8c

Summary

A malicious animated AVIF file can set co64 chunk offsets above INT64_MAX.
The Rust mp4parse AVIF sample table keeps these offsets as u64 and returns
them through mp4parse_avif_get_indice_table(). Gecko then stores the returned
offsets in MediaByteRange, which is based on signed int64_t, without first
rejecting offsets larger than INT64_MAX.

This can turn attacker-controlled AVIF sample offsets into negative signed
offsets. The animated AVIF decode path later calls:

SampleIterator::GetNext()
  mIndex->mSource->ReadAt(sample->mOffset, ...)

AVIFDecoderStream::ReadAt()
  memcpy(data, mBuffer->begin() + offset, size)

Because AVIFDecoderStream::ReadAt() does not reject offset < 0, this leads
to a heap out-of-bounds read before the buffered AVIF input.

I am not claiming RCE. The demonstrated impact is attacker-controlled content
triggering memory-unsafe OOB read / crash in the AVIF animated image decode
path. Depending on allocator layout and downstream decoder behavior, the
primitive may be security-relevant beyond denial of service, but I have not
proven a clean data exfiltration or code execution path.

Affected Code

co64 offsets are parsed as u64:

third_party/rust/mp4parse/src/lib.rs:4695
  fn read_co64<T: Read>(...)

third_party/rust/mp4parse/src/lib.rs:4700
  offsets.push(be_u64(src)?)?

mp4parse sample table exposes u64 offsets:

third_party/rust/mp4parse/src/unstable.rs:112
  pub start_offset: CheckedInteger<u64>,

third_party/rust/mp4parse_capi/src/lib.rs:1311
  mp4parse_avif_get_indice_table(...)

Gecko converts the u64 offsets into a signed MediaByteRange without an
INT64_MAX bound check:

dom/media/mp4/SampleIterator.cpp:475
  MediaByteRange(indice.start_offset, indice.end_offset);

The incomplete-sample check uses the signed end offset:

dom/media/mp4/SampleIterator.cpp:112-116
  mIndex->mSource->Length(&length);
  if (s->mByteRange.mEnd > length) {
    return nullptr;
  }

If mEnd became negative, the check does not reject it.

The negative offset then reaches:

dom/media/mp4/SampleIterator.cpp:133
  mIndex->mSource->ReadAt(sample->mOffset, writer->Data(), sample->Size(), ...)

The AVIF in-memory stream does not reject negative offsets:

image/decoders/nsAVIFDecoder.cpp:140
  bool AVIFDecoderStream::ReadAt(int64_t offset, void* data, size_t size,
                                 size_t* bytes_read) {
    size = std::min(size, size_t(mBuffer->length() - offset));

    if (size <= 0) {
      return false;
    }

    memcpy(data, mBuffer->begin() + offset, size);
    *bytes_read = size;
    return true;
  }

The same class also lacks an explicit negative-offset guard in:

image/decoders/nsAVIFDecoder.cpp:159
  AVIFDecoderStream::GetContiguousAccess(int64_t aOffset, size_t aSize)

Proof of Concept

Attached files:

animated-avif-co64-neg80.avif
make_co64_negative_offset_avif.py
mozilla_avif_mp4parse_probe/Cargo.toml
mozilla_avif_mp4parse_probe/src/main.rs
avif_stream_negative_offset_probe.cpp
run_mozilla_avif_co64_negative_offset_probe.sh

The PoC starts from Gecko's existing animated AVIF test image:

image/test/mochitest/animated-avif.avif

The mutator changes its stco box into a co64 box and sets the single chunk
offset to:

0xffffffffffffffb0

With two 39-byte samples, mp4parse returns:

indice[0] start_u64=0xffffffffffffffb0 end_u64=0xffffffffffffffd7 start_as_i64=-80 end_as_i64=-41 len_as_i64=39
indice[1] start_u64=0xffffffffffffffd7 end_u64=0xfffffffffffffffe start_as_i64=-41 end_as_i64=-2 len_as_i64=39

PoC SHA-256:

9b63cf5212564cd82e8dbb04714ab451e667bd853b62aa909c84f1f4799fa3a1  animated-avif-co64-neg80.avif

Reproduction

From a local Gecko checkout plus the attached helper files:

ROOT=/tmp/gecko-qcms-bounty \
WORK=/path/to/attached/files/root \
./run_mozilla_avif_co64_negative_offset_probe.sh

Expected parser output:

mp4parse_avif_new=Ok
mp4parse_avif_get_info=Ok has_sequence=true color_track_id=1 major_brand="avis"
mp4parse_avif_get_indice_table=Ok len=2 timescale=10
indice[0] start_u64=0xffffffffffffffb0 end_u64=0xffffffffffffffd7 start_as_i64=-80 end_as_i64=-41 len_as_i64=39
indice[1] start_u64=0xffffffffffffffd7 end_u64=0xfffffffffffffffe start_as_i64=-41 end_as_i64=-2 len_as_i64=39

Expected C++/ASAN proof output:

buffer_length=1337 sample_start=-80 sample_end=-41
sample accepted; sample_size=39; calling ReadAt(sample_start)

ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 39
AVIFDecoderStreamProbe::ReadAt(...)

The local ASAN log shows:

READ of size 39
AVIFDecoderStreamProbe::ReadAt(long, void*, unsigned long, unsigned long*)
repro/mozilla_avif_mp4parse_probe/avif_stream_negative_offset_probe.cpp:24

0x51d000000030 is located 80 bytes to the left of 2048-byte region

Root Cause

There are two missing invariants:

  1. Gecko accepts mp4parse sample offsets larger than INT64_MAX even though
    the downstream byte-range and stream APIs use signed int64_t offsets.

  2. AVIFDecoderStream::ReadAt() and GetContiguousAccess() do not defensively
    reject negative offsets before pointer arithmetic.

Suggested Fix

Reject impossible sample offsets when constructing MP4SampleIndex from
mp4parse indices:

if (indice.start_offset > uint64_t(INT64_MAX) ||
    indice.end_offset > uint64_t(INT64_MAX) ||
    indice.start_offset > indice.end_offset) {
  return;
}

Also harden the AVIF stream adapter:

if (offset < 0) {
  return false;
}
size_t pos = size_t(offset);
if (pos >= mBuffer->length()) {
  return false;
}
size = std::min(size, mBuffer->length() - pos);

For GetContiguousAccess(), reject negative offsets and use checked
aSize <= length - offset style arithmetic before returning
mBuffer->begin() + offset.

Duplicate Check

I searched for public references to this exact issue using:

Mozilla AVIFDecoderStream ReadAt negative offset co64 sample_offset Firefox AVIF
site:bugzilla.mozilla.org AVIFDecoderStream ReadAt offset co64
site:bugzilla.mozilla.org nsAVIFDecoder ReadAt GetContiguousAccess offset

I found historical AVIF crash/assertion bugs and generic AVIF issues, but did
not find a public report for this co64 > INT64_MAX to signed negative
AVIFDecoderStream::ReadAt() path.

Bounty / Security Handling

I am submitting this through the Mozilla Client Bug Bounty form and requesting
security review. If a browser-level ASAN testcase or gtest is preferred, I can
adapt the attached PoC into the requested format.

Flags: sec-bounty?
Group: firefox-core-security → gfx-core-security
Component: Security → Graphics: ImageLib
Keywords: ai-involved
Product: Firefox → Core

(In reply to Kang heechan from comment #0)

Tested against local gecko-dev checkout:

5836a062726f715fda621338a17b51aff30d0a8c

gecko-dev is a dead repo. That commit is from jul 2025.

This sounds a lot like bug 2010933. If you pull an actual up to date copy of the source code you'll find several of the functions you reference have been changed and do not match what you have quoted in your report, specifically AVIFDecoderStream::ReadAt.

Please check if you find any security issue in the latest commit on https://github.com/mozilla-firefox/firefox

Flags: needinfo?(gganji11)
See Also: → CVE-2026-2759
Severity: -- → S2
Flags: needinfo?(tnikkel)
Flags: needinfo?(tnikkel)

Seems to be an already fixed dupe, so lowering priority until we hear back otherwise.

Severity: S2 → S3

Marking as duplicate. If you feel this is wrong, please leave a comment.

Status: UNCONFIRMED → RESOLVED
Closed: 6 months ago
Duplicate of bug: CVE-2026-2759
Resolution: --- → DUPLICATE
Flags: sec-bounty? → sec-bounty-
Group: gfx-core-security

Clearing the needinfo request since this bug is a duplicate of bug 2010933, which has been closed for 9 months and 18 days.

For more information, please visit BugBot documentation.

Flags: needinfo?(gganji11)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: