From a4e005570415eaf4206ecbd9004e41db4499b03c Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Sun, 11 Oct 2026 08:27:50 -0700 Subject: [PATCH] Guard two more short-payload reads in TS parsing Both were found while verifying #2349 and are reachable from stream data: - parse_SDT passes section_length - 4 - 8 to the services loop, and section_length comes from the stream. Below 12 (the smallest legal section: 8 bytes of fields plus the CRC) the unsigned subtraction wraps and the loop reads past the buffer. It got past both existing length checks. - read_video_pes_header reads the PES length and nextheader[6] before its own sbuflen < 9 check. With #2349 stopping the earlier crash in ts_readstream, a capbuf holding only a 3-byte start code now reaches it when flushed at EOF and is read past its end. Return -1 for a caller buffer shorter than 7 bytes, as the < 9 path already does. --- src/lib_ccx/stream_functions.c | 7 +++++++ src/lib_ccx/ts_tables.c | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib_ccx/stream_functions.c b/src/lib_ccx/stream_functions.c index 9f6000fa..e83e4f1f 100644 --- a/src/lib_ccx/stream_functions.c +++ b/src/lib_ccx/stream_functions.c @@ -295,6 +295,13 @@ int read_video_pes_header(struct ccx_demuxer *ctx, struct demuxer_data *data, un // Read the next video PES // ((nextheader[3]&0xf0)==0xe0) long long result; + + // A caller-supplied buffer must hold the PES length and nextheader[6] + // before either is read; a truncated packet can leave fewer bytes. + if (sbuflen > 0 && sbuflen < 7) + { + return -1; + } unsigned peslen = nextheader[4] << 8 | nextheader[5]; unsigned payloadlength = 0; // Length of packet data bytes static LLONG current_pts_33 = 0; // Last PTS from the header, without rollover bits diff --git a/src/lib_ccx/ts_tables.c b/src/lib_ccx/ts_tables.c index 2874ecbd..82ccc382 100644 --- a/src/lib_ccx/ts_tables.c +++ b/src/lib_ccx/ts_tables.c @@ -985,7 +985,9 @@ void parse_SDT(struct ccx_demuxer *ctx) // | payload_start[4]); // unsigned version_number = (payload_start[5] & 0x3E) >> 1; - if (section_length > payload_length - 4) + // 12 is the smallest legal section: 8 bytes of fields plus the CRC. + // Anything shorter would make the services loop length below underflow. + if (section_length < 12 || section_length > payload_length - 4) { return; }