aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-15 00:05:55 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-15 00:05:55 +0200
commit2f8840b24c94598ec9803c17e9e6a8e76f1855b9 (patch)
treed3df49e8c88bd203c44daff20d4b416df2fb4466
parent8277ea3feb44f634248788cc50d2fe355a58a5d6 (diff)
downloadmp4-2f8840b24c94598ec9803c17e9e6a8e76f1855b9.tar.gz
mp4-2f8840b24c94598ec9803c17e9e6a8e76f1855b9.zip
mp4: harden box parsing against malformed input
-rw-r--r--fuzz_test.go111
-rw-r--r--reader.go51
2 files changed, 162 insertions, 0 deletions
diff --git a/fuzz_test.go b/fuzz_test.go
new file mode 100644
index 0000000..12ae521
--- /dev/null
+++ b/fuzz_test.go
@@ -0,0 +1,111 @@
+package mp4_test
+
+import (
+ "bytes"
+ "os"
+ "testing"
+
+ "github.com/tetsuo/mp4"
+)
+
+// fuzzSample is a real MP4 used to seed the corpus when it is present. Starting
+// the mutator from valid box structure reaches parsing code that purely random
+// bytes rarely would.
+const fuzzSample = "test-data/big-buck-bunny-480p-30sec.mp4"
+
+// seedBoxes adds a real file when available plus small hand-built boxes,
+// including malformed ones, so the corpus covers realistic and degenerate
+// structures. The last two seeds are a truncated mvhd, which reaches a field
+// extractor with fewer bytes than the box layout requires.
+func seedBoxes(f *testing.F) {
+ if data, err := os.ReadFile(fuzzSample); err == nil {
+ f.Add(data)
+ }
+ f.Add([]byte(nil))
+ f.Add([]byte{0, 0, 0, 8, 'f', 't', 'y', 'p'})
+ f.Add([]byte{0, 0, 0, 1, 'm', 'd', 'a', 't', 0, 0, 0, 0, 0, 0, 0, 0}) // 64-bit size 0
+ f.Add([]byte{255, 255, 255, 255, 'f', 'r', 'e', 'e'}) // oversized declared size
+ f.Add([]byte{0, 0, 0, 0x10, 'm', 'v', 'h', 'd', 0, 0, 0, 0, 0, 0, 0, 0})
+ 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,
+ })
+}
+
+// FuzzScanner drives the streaming top-level box scanner over arbitrary input.
+// No input may panic; malformed data must surface through Err instead.
+func FuzzScanner(f *testing.F) {
+ seedBoxes(f)
+ buf := make([]byte, 1<<20)
+ f.Fuzz(func(t *testing.T, data []byte) {
+ sc := mp4.NewScanner(bytes.NewReader(data))
+ for sc.Next() {
+ e := sc.Entry()
+ if n := e.DataSize(); n >= 0 && n <= int64(len(buf)) {
+ _ = sc.ReadBody(buf[:n])
+ }
+ }
+ _ = sc.Err()
+ })
+}
+
+// FuzzReader walks arbitrary bytes as a box tree and calls the typed field
+// extractor for whichever box type it lands on. It bounds its own descent below
+// the reader's nesting limit so the harness itself cannot overflow the stack.
+// No input may panic.
+func FuzzReader(f *testing.F) {
+ seedBoxes(f)
+ f.Fuzz(func(t *testing.T, data []byte) {
+ r := mp4.NewReader(data)
+ walkBoxes(&r, 0)
+ })
+}
+
+// fuzzMaxDepth caps the walker below the reader's internal nesting limit.
+const fuzzMaxDepth = 12
+
+func walkBoxes(r *mp4.Reader, depth int) {
+ for r.Next() {
+ _ = r.Size()
+ _ = r.Version()
+ _ = r.Flags()
+ _ = r.Data()
+ _ = r.RawBox()
+ readTypedFields(r)
+ if depth < fuzzMaxDepth {
+ r.Enter()
+ walkBoxes(r, depth+1)
+ r.Exit()
+ }
+ }
+}
+
+// readTypedFields calls the extractor matching the current box type. Each reads
+// fixed offsets, so a truncated box must not read out of bounds.
+func readTypedFields(r *mp4.Reader) {
+ switch r.Type() {
+ case mp4.TypeMvhd:
+ r.ReadMvhd()
+ case mp4.TypeTkhd:
+ r.ReadTkhd()
+ case mp4.TypeMdhd:
+ r.ReadMdhd()
+ case mp4.TypeHdlr:
+ r.ReadHdlr()
+ r.ReadHdlrName()
+ case mp4.TypeMehd:
+ r.ReadMehd()
+ case mp4.TypeTrex:
+ r.ReadTrex()
+ case mp4.TypeMfhd:
+ r.ReadMfhd()
+ case mp4.TypeTfhd:
+ r.ReadTfhd()
+ case mp4.TypeTfdt:
+ r.ReadTfdt()
+ case mp4.TypeElst:
+ r.ReadElst()
+ case mp4.TypeStsd, mp4.TypeDref:
+ r.EntryCount()
+ }
+}
diff --git a/reader.go b/reader.go
index 86902e9..587c45d 100644
--- a/reader.go
+++ b/reader.go
@@ -117,6 +117,12 @@ func (r *Reader) Next() bool {
return false
}
+ // A box cannot be smaller than the header already read; rejecting it keeps
+ // dataStart within boxEnd so Data never slices with a low bound past its high.
+ if r.boxEnd < ptr {
+ return false
+ }
+
// Parse full box header if applicable
if IsFullBox(r.boxType) {
if r.boxEnd-ptr < 4 {
@@ -212,6 +218,9 @@ func (r *Reader) Skip(n int) {
// Used for boxes like stsd and dref that begin with a count field.
func (r *Reader) EntryCount() uint32 {
data := r.Data()
+ if len(data) < 4 {
+ return 0
+ }
return be.Uint32(data[0:4])
}
@@ -222,11 +231,17 @@ func (r *Reader) ReadMvhd() (timescale uint32, duration uint64, nextTrackId uint
version := r.Version()
if version == 1 {
// v1: ctime(8)+mtime(8)+timescale(4)+duration(8)+rate(4)+volume(2)+reserved(10)+matrix(36)+predefined(24)+nextTrackId(4) = 108
+ if len(data) < 108 {
+ return
+ }
timescale = be.Uint32(data[16:20])
duration = be.Uint64(data[20:28])
nextTrackId = be.Uint32(data[104:108])
} else {
// v0: ctime(4)+mtime(4)+timescale(4)+duration(4)+rate(4)+volume(2)+reserved(10)+matrix(36)+predefined(24)+nextTrackId(4) = 96
+ if len(data) < 96 {
+ return
+ }
timescale = be.Uint32(data[8:12])
duration = uint64(be.Uint32(data[12:16]))
nextTrackId = be.Uint32(data[92:96])
@@ -242,6 +257,9 @@ func (r *Reader) ReadTkhd() (trackId uint32, duration uint64, width, height uint
version := r.Version()
if version == 1 {
// v1: ctime(8)+mtime(8)+trackId(4)+reserved(4)+duration(8)
+ if len(data) < 92 {
+ return
+ }
trackId = be.Uint32(data[16:20])
duration = be.Uint64(data[24:32])
// +reserved(8)+layer(2)+altGroup(2)+volume(2)+reserved(2)+matrix(36)+width(4)+height(4)
@@ -249,6 +267,9 @@ func (r *Reader) ReadTkhd() (trackId uint32, duration uint64, width, height uint
height = be.Uint32(data[88:92])
} else {
// v0: ctime(4)+mtime(4)+trackId(4)+reserved(4)+duration(4)
+ if len(data) < 80 {
+ return
+ }
trackId = be.Uint32(data[8:12])
duration = uint64(be.Uint32(data[16:20]))
// +reserved(8)+layer(2)+altGroup(2)+volume(2)+reserved(2)+matrix(36)+width(4)+height(4)
@@ -265,11 +286,17 @@ func (r *Reader) ReadMdhd() (timescale uint32, duration uint64, language uint16)
version := r.Version()
if version == 1 {
// v1: ctime(8)+mtime(8)+timescale(4)+duration(8)+lang(2)+quality(2)
+ if len(data) < 30 {
+ return
+ }
timescale = be.Uint32(data[16:20])
duration = be.Uint64(data[20:28])
language = be.Uint16(data[28:30])
} else {
// v0: ctime(4)+mtime(4)+timescale(4)+duration(4)+lang(2)+quality(2)
+ if len(data) < 18 {
+ return
+ }
timescale = be.Uint32(data[8:12])
duration = uint64(be.Uint32(data[12:16]))
language = be.Uint16(data[16:18])
@@ -282,6 +309,9 @@ func (r *Reader) ReadMdhd() (timescale uint32, duration uint64, language uint16)
func (r *Reader) ReadHdlr() [4]byte {
data := r.Data()
var t [4]byte
+ if len(data) < 8 {
+ return t
+ }
copy(t[:], data[4:8])
return t
}
@@ -305,8 +335,14 @@ func (r *Reader) ReadMehd() (fragmentDuration uint64) {
data := r.Data()
version := r.Version()
if version == 1 {
+ if len(data) < 8 {
+ return
+ }
fragmentDuration = be.Uint64(data[0:8])
} else {
+ if len(data) < 4 {
+ return
+ }
fragmentDuration = uint64(be.Uint32(data[0:4]))
}
return
@@ -336,6 +372,9 @@ func (r *Reader) ReadElst() (mediaTime int64, ok bool) {
// default sample size, and default sample flags.
func (r *Reader) ReadTrex() (trackId, defSampleDescIdx, defSampleDuration, defSampleSize, defSampleFlags uint32) {
data := r.Data()
+ if len(data) < 20 {
+ return
+ }
trackId = be.Uint32(data[0:4])
defSampleDescIdx = be.Uint32(data[4:8])
defSampleDuration = be.Uint32(data[8:12])
@@ -347,6 +386,9 @@ func (r *Reader) ReadTrex() (trackId, defSampleDescIdx, defSampleDuration, defSa
// ReadMfhd extracts the sequence number from an mfhd box.
func (r *Reader) ReadMfhd() (sequenceNumber uint32) {
data := r.Data()
+ if len(data) < 4 {
+ return
+ }
sequenceNumber = be.Uint32(data[0:4])
return
}
@@ -354,6 +396,9 @@ func (r *Reader) ReadMfhd() (sequenceNumber uint32) {
// ReadTfhd extracts the track ID from a tfhd box.
func (r *Reader) ReadTfhd() (trackId uint32) {
data := r.Data()
+ if len(data) < 4 {
+ return
+ }
trackId = be.Uint32(data[0:4])
return
}
@@ -363,8 +408,14 @@ func (r *Reader) ReadTfdt() (baseMediaDecodeTime uint64) {
data := r.Data()
version := r.Version()
if version == 1 {
+ if len(data) < 8 {
+ return
+ }
baseMediaDecodeTime = be.Uint64(data[0:8])
} else {
+ if len(data) < 4 {
+ return
+ }
baseMediaDecodeTime = uint64(be.Uint32(data[0:4]))
}
return