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 | |
| parent | 63391b66a7c5d10e84d4d8c64510e3620152dc26 (diff) | |
| download | mp4-6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114.tar.gz mp4-6216b2ed1fcd0b06a9e0b3ae74e5373a8cccd114.zip | |
track: bound malformed sample tables
| -rw-r--r-- | iter.go | 13 | ||||
| -rw-r--r-- | track/fuzz_test.go | 43 | ||||
| -rw-r--r-- | track/samplecount_test.go | 78 | ||||
| -rw-r--r-- | track/track.go | 18 |
4 files changed, 152 insertions, 0 deletions
@@ -32,6 +32,19 @@ func NewStszIter(data []byte) StszIter { // Count returns the total number of samples. func (it *StszIter) Count() uint32 { return it.count } +// ConstantSize reports whether the box encodes one size for every sample rather +// than a per-sample size table. +func (it *StszIter) ConstantSize() bool { return it.sampleSize != 0 } + +// SizeTableLen returns the number of per-sample size entries the box stores. It +// is zero for the constant-size form. +func (it *StszIter) SizeTableLen() int { + if it.sampleSize != 0 || len(it.buf) < 8 { + return 0 + } + return (len(it.buf) - 8) / 4 +} + // Next returns the next sample size. Returns (0, false) when done. func (it *StszIter) Next() (uint32, bool) { if it.index >= it.count { diff --git a/track/fuzz_test.go b/track/fuzz_test.go new file mode 100644 index 0000000..313c901 --- /dev/null +++ b/track/fuzz_test.go @@ -0,0 +1,43 @@ +package track_test + +import ( + "os" + "testing" + + "github.com/tetsuo/mp4" + "github.com/tetsuo/mp4/track" +) + +const fuzzSample = "../test-data/big-buck-bunny-480p-30sec.mp4" + +// FuzzParseTracks feeds arbitrary bytes to the moov parser. A malformed moov +// must return an error rather than panic. The seeds include a real moov when +// available and a moov holding a truncated mvhd, which reaches a field +// extractor with fewer bytes than the box layout requires. +func FuzzParseTracks(f *testing.F) { + if data, err := os.ReadFile(fuzzSample); err == nil { + r := mp4.NewReader(data) + for r.Next() { + if r.Type() == mp4.TypeMoov { + f.Add(append([]byte(nil), r.RawBox()...)) + break + } + } + } + f.Add([]byte(nil)) + f.Add([]byte{0, 0, 0, 8, 'm', 'o', 'o', 'v'}) + f.Add([]byte{ + 0, 0, 0, 0x18, 'm', 'o', 'o', 'v', + 0, 0, 0, 0x10, 'm', 'v', 'h', 'd', 0, 0, 0, 0, 0, 0, 0, 0, + }) + + f.Fuzz(func(t *testing.T, data []byte) { + tracks, _, _, err := track.ParseTracks(data) + if err != nil { + return + } + for _, tr := range tracks { + _ = tr.Codec() + } + }) +} diff --git a/track/samplecount_test.go b/track/samplecount_test.go new file mode 100644 index 0000000..66d252b --- /dev/null +++ b/track/samplecount_test.go @@ -0,0 +1,78 @@ +package track_test + +import ( + "encoding/binary" + "runtime" + "testing" + + "github.com/tetsuo/mp4" + "github.com/tetsuo/mp4/track" +) + +// patchFirstStsz overwrites the sample_size and sample_count fields of the first +// stsz box in buf. A non-zero sample size selects the constant-size form, which +// carries no per-sample table. +func patchFirstStsz(buf []byte, sampleSize, count uint32) bool { + var walk func(r *mp4.Reader) bool + walk = func(r *mp4.Reader) bool { + for r.Next() { + if r.Type() == mp4.TypeStsz { + off := r.DataOffset() + binary.BigEndian.PutUint32(buf[off:off+4], sampleSize) + binary.BigEndian.PutUint32(buf[off+4:off+8], count) + return true + } + if mp4.IsContainerBox(r.Type()) { + r.Enter() + found := walk(r) + r.Exit() + if found { + return true + } + } + } + return false + } + r := mp4.NewReader(buf) + return walk(&r) +} + +// TestParseTracksRejectsImplausibleSampleCount patches a track's stsz to declare +// far more samples than the file describes. The parser must reject the count +// rather than allocate for it: 1<<30 samples would otherwise need about 32 GB, +// since Sample is 32 bytes. Both stsz forms are covered, since a per-sample +// table is bounded by its entries and a constant-size box by a fixed cap. +func TestParseTracksRejectsImplausibleSampleCount(t *testing.T) { + cases := []struct { + name string + sampleSize uint32 + }{ + {"per-sample", 0}, + {"constant-size", 4}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + moov := readMoov(t, "../test-data/big-buck-bunny-480p-30sec.mp4") + if !patchFirstStsz(moov, tc.sampleSize, 1<<30) { + t.Fatal("no stsz box found to patch") + } + + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + tracks, _, _, _ := track.ParseTracks(moov) + runtime.ReadMemStats(&after) + + if grew := after.TotalAlloc - before.TotalAlloc; grew > 64<<20 { + t.Errorf("ParseTracks allocated %d bytes for a bogus stsz count, want bounded", grew) + } + // No real sample file approaches this many samples, so a surviving + // track this large means the bogus count was trusted. + for _, tr := range tracks { + if len(tr.Samples) > 1<<20 { + t.Errorf("track kept %d samples from a bogus count, want it dropped", len(tr.Samples)) + } + } + }) + } +} 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] |
