Skip to content

[FIX] GXF: don't let an oversized mpeg packet overflow the demuxer buffer - #2501

Open
pranayr710 wants to merge 1 commit into
CCExtractor:masterfrom
pranayr710:fix-gxf-mpeg-bufsize
Open

pranayr710 wants to merge 1 commit into
CCExtractor:masterfrom
pranayr710:fix-gxf-mpeg-bufsize

Conversation

@pranayr710

Copy link
Copy Markdown
Contributor

Fixes #2500.

Problem

parse_mpeg_packet() reads mpeg_pic_size bytes into the fixed BUFSIZE (≈2 MB) demuxer buffer without any bounds check:

result = buffered_read(demux, data->buffer + data->len, len);
data->len += len;

len is mpeg_pic_size, a 24-bit value taken straight from the GXF media packet header (so up to ~16 MB). A media packet that declares an oversized mpeg_pic_size overflows data->buffer. The ancillary branch in parse_media() already guards its size (len < ad_track->packet_size); the MPEG2 branch had no equivalent.

Fix

Skip the packet when it would not fit, instead of reading it in:

if (len < 0 || (size_t)data->len + (size_t)len > (size_t)BUFSIZE)
{
    log("GXF: mpeg packet size %d does not fit the demuxer buffer; skipping\n", len);
    result = buffered_skip(demux, len);
    ...
    return ret;
}

Verification (AddressSanitizer)

build result on the repro
before heap-buffer-overflow write via buffered_read, parse_mpeg_packet at ccx_gxf.c:930
after mpeg packet size 2101368 does not fit the demuxer buffer; skipping, processing completes
Reproduction generator (Python)
import struct, sys
def be32(v): return struct.pack(">I", v)
def be16(v): return struct.pack(">H", v)
def pkt(t, body):
    return (b"\x00\x00\x00\x00\x01"+bytes([t])+be32(16+len(body))
            +b"\x00\x00\x00\x00\xe1\xe2"+body)
BUFSIZE = 2048*1024 + 120
mpeg_pic_size = BUFSIZE + 4096
track_sec = bytes([0x8b,0xc0])+be16(2)+b"\x00\x00"       # MPEG2_525 track
mapb = be16(0xe0ff)+be16(0)+be16(len(track_sec))+track_sec
med  = bytes([0x0b,0xc0])+be32(0)+be32(mpeg_pic_size & 0xFFFFFF)+be32(0)+b"\x00\x00"
med += b"\x00"*mpeg_pic_size
open(sys.argv[1] if len(sys.argv)>1 else "gxf_mpeg.gxf","wb").write(
    pkt(0xbc,mapb)+pkt(0xbf,med)+pkt(0xfb,b""))
python mkgxf_mpeg.py gxf_mpeg.gxf
ccextractor gxf_mpeg.gxf -o out.srt        # built with -fsanitize=address

This is a separate defect from #2499/#2498 (the CDP cc_count read); both are "a GXF length field used without bounding it against the destination buffer".


Analysis and the ASan reproduction were done with AI assistance; the fix and this description were reviewed by me.

…ffer

parse_mpeg_packet() does buffered_read(data->buffer + data->len, len)
where len is mpeg_pic_size, a 24-bit value taken straight from the GXF
media packet header. data->buffer is a fixed BUFSIZE (2 MB) allocation
and nothing checks len against it, so a media packet that declares an
mpeg_pic_size larger than the buffer writes past its end.

Reject (skip) the packet when it would not fit instead of reading it in.

Found with AddressSanitizer:
  heap-buffer-overflow WRITE via buffered_read, ../src/lib_ccx/ccx_gxf.c:930
  in parse_mpeg_packet
@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on linux. 171/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 26/28
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 32/34

67 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10124, commit 034fdbc:

  • 0 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Compared with the commit this branch was cut from: the same run as the tip of master (test 10124), so the comparison above already covers it.


No test changes behaviour relative to the tip of master: every failure above fails there too, byte for byte. The approved output for those tests is out of date, which is a baseline to review rather than a regression in this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GXF: heap buffer overflow in parse_mpeg_packet() — oversized mpeg_pic_size overflows the demuxer buffer

2 participants