Skip to content

Commit

Permalink
archive/tar: terminate when reading malformed sparse files
Browse files Browse the repository at this point in the history
Fixes #10968.

Change-Id: I027bc571a71629ac49c2a0ff101b2950af6e7531
Reviewed-on: https://go-review.googlesource.com/10482
Reviewed-by: David Symonds <dsymonds@golang.org>
Run-TryBot: David Symonds <dsymonds@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
osocurioso authored and dsymonds committed May 28, 2015
1 parent 4c050fe commit c2fe4a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/archive/tar/reader.go
Expand Up @@ -791,6 +791,9 @@ func (sfr *sparseFileReader) Read(b []byte) (n int, err error) {
// Otherwise, we're at the end of the file
return 0, io.EOF
}
if sfr.tot < sfr.sp[0].offset {
return 0, io.ErrUnexpectedEOF
}
if sfr.pos < sfr.sp[0].offset {
// We're in a hole
n = sfr.readHole(b, sfr.sp[0].offset)
Expand Down
19 changes: 19 additions & 0 deletions src/archive/tar/reader_test.go
Expand Up @@ -757,3 +757,22 @@ func TestNegativeHdrSize(t *testing.T) {
}
io.Copy(ioutil.Discard, r)
}

// This used to hang in (*sparseFileReader).readHole due to missing
// verification of sparse offsets against file size.
func TestIssue10968(t *testing.T) {
f, err := os.Open("testdata/issue10968.tar")
if err != nil {
t.Fatal(err)
}
defer f.Close()
r := NewReader(f)
_, err = r.Next()
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(ioutil.Discard, r)
if err != io.ErrUnexpectedEOF {
t.Fatalf("expected %q, got %q", io.ErrUnexpectedEOF, err)
}
}
Binary file added src/archive/tar/testdata/issue10968.tar
Binary file not shown.

0 comments on commit c2fe4a0

Please sign in to comment.