diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-15 00:06:58 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-15 00:06:58 +0200 |
| commit | 6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114 (patch) | |
| tree | 2d4cba21bfe0f23b9ee469bb0cb46bab251857fa /track/track.go | |
| parent | 63391b66a7c5d10e84d4d8c64510e3620152dc26 (diff) | |
| download | mp4-6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114.tar.gz mp4-6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114.zip | |
track: bound malformed sample tables
Diffstat (limited to 'track/track.go')
| -rw-r--r-- | track/track.go | 18 |
1 files changed, 18 insertions, 0 deletions
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] |
