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

(cherry picked from commit 5f94f68)
  • Loading branch information
Ibrahim Jarif committed Mar 12, 2020
1 parent 3343184 commit 48aaa1c
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 @@ -351,6 +351,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 @@ -364,6 +369,12 @@ func ReplayManifestFile(fp *os.File) (Manifest, int64, error) {
return Manifest{}, 0, err
}
length := binary.BigEndian.Uint32(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 48aaa1c

Please sign in to comment.