Skip to content

Commit

Permalink
Limit manifest's change set size (#1119)
Browse files Browse the repository at this point in the history
This PR limits the amount of memory we allocated for reading
the manifest changes set's size. When a manifest file is corrupted,
in the worst case we might end up allocating more than 4GB.

This PR ensures we don't over-allocate the byte slice.
Fixes #490
  • Loading branch information
Ibrahim Jarif authored Nov 20, 2019
1 parent ffb3450 commit 5f94f68
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ func ReplayManifestFile(fp *os.File) (Manifest, int64, error) {
version, magicVersion)
}

stat, err := fp.Stat()
if err != nil {
return Manifest{}, 0, err
}

build := createManifest()
var offset int64
for {
Expand All @@ -366,6 +371,12 @@ func ReplayManifestFile(fp *os.File) (Manifest, int64, error) {
return Manifest{}, 0, err
}
length := y.BytesToU32(lenCrcBuf[0:4])
// Sanity check to ensure we don't over-allocate memory.
if length > uint32(stat.Size()) {
return Manifest{}, 0, errors.Errorf(
"Buffer length: %d greater than file size: %d. Manifest file might be corrupted",
length, stat.Size())
}
var buf = make([]byte, length)
if _, err := io.ReadFull(&r, buf); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
Expand Down

0 comments on commit 5f94f68

Please sign in to comment.