Skip to content

Commit 562de88

Browse files
authored
Merge pull request #1953 from THE-Amrit-mahto-05/fix/ts-heap-overflow
Fix/ts heap overflow
2 parents 12adb5e + 774c3a0 commit 562de88

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

docs/CHANGES.TXT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix: 32-bit build failures on i686 and armv7l architectures
77
- Fix: Legacy command-line argument compatibility (-1, -2, -12, --sc, --svc)
88
- Fix: Prevent heap buffer overflow in Teletext processing (security fix)
9+
- Fix: Prevent integer overflow leading to heap buffer overflow in Transport Stream handling (security fix)
910
- Fix: Lazy OCR initialization - only initialize when first DVB subtitle is encountered
1011
- Build: Optimized Windows CI workflow for faster builds
1112
- Fix: Updated GUI with version 0.7.1. A blind attempt to fix a hang on start on some Windows.

src/lib_ccx/ccx_decoders_608.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,20 @@ int write_cc_buffer(ccx_decoder_608_context *context, struct cc_subtitle *sub)
316316

317317
if (!data->empty && context->output_format != CCX_OF_NULL)
318318
{
319-
struct eia608_screen *new_data = (struct eia608_screen *)realloc(sub->data, (sub->nb_data + 1) * sizeof(*data));
319+
size_t new_size;
320+
321+
if (sub->nb_data + 1 > SIZE_MAX / sizeof(struct eia608_screen))
322+
{
323+
ccx_common_logging.log_ftn("Too many screens, cannot allocate more memory.\n");
324+
return 0;
325+
}
326+
327+
new_size = (sub->nb_data + 1) * sizeof(struct eia608_screen);
328+
329+
struct eia608_screen *new_data = (struct eia608_screen *)realloc(sub->data, new_size);
320330
if (!new_data)
321331
{
322-
ccx_common_logging.log_ftn("No Memory left");
332+
ccx_common_logging.log_ftn("Out of memory while reallocating screen buffer\n");
323333
return 0;
324334
}
325335
sub->data = new_data;
@@ -386,10 +396,20 @@ int write_cc_line(ccx_decoder_608_context *context, struct cc_subtitle *sub)
386396

387397
if (!data->empty)
388398
{
389-
struct eia608_screen *new_data = (struct eia608_screen *)realloc(sub->data, (sub->nb_data + 1) * sizeof(*data));
399+
size_t new_size;
400+
401+
if (sub->nb_data + 1 > SIZE_MAX / sizeof(struct eia608_screen))
402+
{
403+
ccx_common_logging.log_ftn("Too many screens, cannot allocate more memory.\n");
404+
return 0;
405+
}
406+
407+
new_size = (sub->nb_data + 1) * sizeof(struct eia608_screen);
408+
409+
struct eia608_screen *new_data = (struct eia608_screen *)realloc(sub->data, new_size);
390410
if (!new_data)
391411
{
392-
ccx_common_logging.log_ftn("No Memory left");
412+
ccx_common_logging.log_ftn("Out of memory while reallocating screen buffer\n");
393413
return 0;
394414
}
395415
sub->data = new_data;

src/lib_ccx/ccx_decoders_isdb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,16 +724,17 @@ static int parse_csi(ISDBSubContext *ctx, const uint8_t *buf, int len)
724724
// Copy buf in arg
725725
for (i = 0; *buf != 0x20; i++)
726726
{
727-
if (i >= (sizeof(arg)) + 1)
727+
if (i >= sizeof(arg) - 1)
728728
{
729-
isdb_log("UnExpected CSI %d >= %d", sizeof(arg) + 1, i);
729+
isdb_log("UnExpected CSI: too long");
730730
break;
731731
}
732732
arg[i] = *buf;
733733
buf++;
734734
}
735735
/* ignore terminating 0x20 character */
736-
arg[i] = *buf++;
736+
if (i < sizeof(arg))
737+
arg[i] = *buf++;
737738

738739
switch (*buf)
739740
{

src/lib_ccx/ts_functions.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ void cinfo_cremation(struct ccx_demuxer *ctx, struct demuxer_data **data)
670670

671671
int copy_payload_to_capbuf(struct cap_info *cinfo, struct ts_payload *payload)
672672
{
673-
int newcapbuflen;
674673

675674
if (cinfo->ignore == CCX_TRUE &&
676675
((cinfo->stream != CCX_STREAM_TYPE_VIDEO_MPEG2 &&
@@ -696,17 +695,22 @@ int copy_payload_to_capbuf(struct cap_info *cinfo, struct ts_payload *payload)
696695
}
697696

698697
// copy payload to capbuf
699-
newcapbuflen = cinfo->capbuflen + payload->length;
700-
if (newcapbuflen > cinfo->capbufsize)
698+
if (payload->length > INT64_MAX - cinfo->capbuflen)
701699
{
702-
unsigned char *new_capbuf = (unsigned char *)realloc(cinfo->capbuf, newcapbuflen);
700+
mprint("Error: capbuf size overflow\n");
701+
return -1;
702+
}
703+
int64_t newcapbuflen = (int64_t)cinfo->capbuflen + payload->length;
704+
if (newcapbuflen > (int64_t)cinfo->capbufsize)
705+
{
706+
unsigned char *new_capbuf = (unsigned char *)realloc(cinfo->capbuf, (size_t)newcapbuflen);
703707
if (!new_capbuf)
704708
return -1;
705709
cinfo->capbuf = new_capbuf;
706-
cinfo->capbufsize = newcapbuflen;
710+
cinfo->capbufsize = newcapbuflen; // Note: capbufsize is int in struct cap_info
707711
}
708712
memcpy(cinfo->capbuf + cinfo->capbuflen, payload->start, payload->length);
709-
cinfo->capbuflen = newcapbuflen;
713+
cinfo->capbuflen = newcapbuflen; // Note: capbuflen is int in struct cap_info
710714

711715
return CCX_OK;
712716
}

0 commit comments

Comments
 (0)