Skip to content

Commit bf5a6cb

Browse files
author
Grok Compression
committed
Fix heap buffer overflow in MJ2 stco_decompact (CVE pending)
The stco_decompact function writes to tk->samples_[samples_count] without checking whether samples_count has exceeded the vector size. When a crafted MJ2 file has samples_per_chunk (from stsc box) larger than the actual sample count (from stts box), the inner loop writes past the end of the vector. This is an attacker-controlled linear heap write: both the value (chunk.offset_ from stco box) and length (samples_per_chunk from stsc box) are attacker- controlled 32-bit fields from the file. Fix (defense in depth): 1. Add bounds check in stco_decompact inner loop to stop before OOB write 2. Validate samples_per_chunk in stsc_decompact against actual sample count, rejecting inconsistent files early Reported-by: Claude / Ada Logics
1 parent 0a1e165 commit bf5a6cb

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/lib/core/fileformat/decompress/FileFormatMJ2Decompress.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,19 @@ void FileFormatMJ2Decompress::stsc_decompact(mj2_tk* tk)
548548
{
549549
if(tk->sampletochunk_.size() == 1)
550550
{
551+
auto samples_per_chunk = tk->sampletochunk_[0].samples_per_chunk_;
552+
if(samples_per_chunk == 0 || samples_per_chunk > tk->samples_.size())
553+
{
554+
grklog.error("MJ2 STSC: samples_per_chunk %u is inconsistent with sample count %u",
555+
samples_per_chunk, (uint32_t)tk->samples_.size());
556+
return;
557+
}
551558
auto num_chunks = (uint32_t)ceil((double)tk->samples_.size() /
552-
(double)tk->sampletochunk_[0].samples_per_chunk_);
559+
(double)samples_per_chunk);
553560
for(uint32_t k = 0; k < num_chunks; k++)
554561
{
555562
mj2_chunk chunk;
556-
chunk.num_samples_ = tk->sampletochunk_[0].samples_per_chunk_;
563+
chunk.num_samples_ = samples_per_chunk;
557564
tk->chunks_.push_back(chunk);
558565
}
559566
}
@@ -627,6 +634,11 @@ void FileFormatMJ2Decompress::stco_decompact(mj2_tk* tk)
627634
uint32_t intra_chunk_offset = 0;
628635
for(uint32_t j = 0; j < chunk.num_samples_; j++)
629636
{
637+
if(samples_count >= tk->samples_.size())
638+
{
639+
grklog.error("MJ2 STCO: chunk samples exceed declared sample count");
640+
return;
641+
}
630642
tk->samples_[samples_count].offset_ = intra_chunk_offset + chunk.offset_;
631643
intra_chunk_offset += tk->samples_[samples_count].samples_size_;
632644
samples_count++;

0 commit comments

Comments
 (0)