Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set size limit when allocating binary offsets. #29

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions binary_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ type binaryParser struct {
io.ReadSeeker // reader for plist data
}

// safeUint64Slice returns a slice of uint64. If the desired size of the slice
// is too large to fit in memory, it returns an error rather than panicking.
func safeUint64Slice(size uint64) (slice []uint64, err error) {
defer func() {
if r := recover(); r != nil {
slice = nil
err = r.(error)
}
}()
return make([]uint64, size), nil
}
const numObjectsMax = 4 << 20

// newBinaryParser takes in a ReadSeeker for the bytes of a binary plist and
// returns a parser after reading the offset table and trailer.
Expand All @@ -58,10 +48,14 @@ func newBinaryParser(r io.ReadSeeker) (*binaryParser, error) {
if _, err := bp.Seek(int64(bp.OffsetTableOffset), io.SeekStart); err != nil {
return nil, fmt.Errorf("plist: couldn't seek to start of offset table: %v", err)
}
var err error
if bp.OffsetTable, err = safeUint64Slice(bp.NumObjects); err != nil {
return nil, err

// numObjectsMax is arbitrary. Please fix.
// TODO(github.com/groob/plist/issues/28)
if bp.NumObjects > numObjectsMax {
return nil, fmt.Errorf("plist: offset size larger than expected %d", numObjectsMax)
}

bp.OffsetTable = make([]uint64, bp.NumObjects)
if bp.OffsetIntSize > 8 {
return nil, fmt.Errorf("plist: can't decode when offset int size (%d) is greater than 8", bp.OffsetIntSize)
}
Expand Down
26 changes: 16 additions & 10 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,24 @@ func TestUnmarshaler(t *testing.T) {
}

func TestFuzzCrashers(t *testing.T) {
infos, err := ioutil.ReadDir("testdata/crashers")
dir := filepath.Join("testdata", "crashers")
testDir, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
t.Fatalf("reading dir %q: %s", dir, err)
}

for _, info := range infos {
crasher, err := ioutil.ReadFile(filepath.Join("testdata", "crashers", info.Name()))
if err != nil {
t.Fatal(err)
}
var i interface{}
Unmarshal(crasher, &i)
}
for _, tc := range testDir {
tc := tc
t.Run(tc.Name(), func(t *testing.T) {
t.Parallel()

crasher, err := ioutil.ReadFile(filepath.Join("testdata", "crashers", tc.Name()))
if err != nil {
t.Fatal(err)
}

var i interface{}
Unmarshal(crasher, &i)
})
}
}