From 6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 15 Sep 2026 00:06:58 +0200 Subject: track: bound malformed sample tables --- track/track.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'track/track.go') diff --git a/track/track.go b/track/track.go index 11dfd00..40eb3cb 100644 --- a/track/track.go +++ b/track/track.go @@ -726,6 +726,12 @@ func childBox(mr *mp4.Reader, childOffset int, boxType mp4.BoxType) []byte { // parseSamples parses sample table data and populates track.Samples. // Returns an error if required sample table data is missing or corrupt. +// maxSamples caps the sample count of a constant-size stsz, which carries no +// per-sample data to bound it; a per-sample table is bounded by its own entries +// instead. The cap keeps a malformed constant-size box from forcing a large +// allocation while staying well above any real constant-size track. +const maxSamples = 1 << 20 + func (t *Track) parseSamples() error { if t.raw.stszData == nil || t.raw.sttsData == nil || t.raw.stscData == nil { return fmt.Errorf("track %d: %w: missing required sample table data (stsz/stts/stsc)", t.ID, ErrInvalidTrack) @@ -741,6 +747,18 @@ func (t *Track) parseSamples() error { return nil } + // A malformed stsz can declare far more samples than the file describes, + // which would otherwise size the allocation below from an untrusted count. A + // per-sample size table cannot describe more samples than it stores; the + // constant-size form carries no per-sample data, so bound it at maxSamples. + if stszIt.ConstantSize() { + if numSamples > maxSamples { + return fmt.Errorf("track %d: %w: constant-size stsz declares %d samples", t.ID, ErrCorruptData, numSamples) + } + } else if numSamples > stszIt.SizeTableLen() { + return fmt.Errorf("track %d: %w: stsz declares %d samples but stores %d", t.ID, ErrCorruptData, numSamples, stszIt.SizeTableLen()) + } + var samples []Sample if cap(t.Samples) >= numSamples { samples = t.Samples[:numSamples] -- cgit v1.3.1