Skip to content
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
6 changes: 6 additions & 0 deletions archive/tar/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ const (
// Max length of a special file (PAX header, GNU long name or link).
// This matches the limit used by libarchive.
maxSpecialFileSize = 1 << 20

// Maximum number of sparse file entries.
// We should never actually hit this limit
// (every sparse encoding will first be limited by maxSpecialFileSize),
// but this adds an additional layer of defense.
maxSparseFileEntries = 1 << 20
)

// blockPadding computes the number of bytes needed to pad offset up to the
Expand Down
28 changes: 24 additions & 4 deletions archive/tar/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, err
}
s := blk.GNU().Sparse()
spd := make(sparseDatas, 0, s.MaxEntries())
for {
totalSize := len(s)
for totalSize < maxSpecialFileSize {
for i := 0; i < s.MaxEntries(); i++ {
// This termination condition is identical to GNU and BSD tar.
if s.Entry(i).Offset()[0] == 0x00 {
Expand All @@ -548,7 +549,11 @@ func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, err
if p.err != nil {
return nil, p.err
}
spd = append(spd, sparseEntry{Offset: offset, Length: length})
var err error
spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
if err != nil {
return nil, err
}
}

if s.IsExtended()[0] > 0 {
Expand All @@ -560,10 +565,12 @@ func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, err
tr.rawBytes.Write(blk[:])
}
s = blk.Sparse()
totalSize += len(s)
continue
}
return spd, nil // Done
}
return nil, errSparseTooLong
}

// readGNUSparseMap1x0 reads the sparse map as stored in GNU's PAX sparse format
Expand Down Expand Up @@ -636,7 +643,10 @@ func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
if err1 != nil || err2 != nil {
return nil, ErrHeader
}
spd = append(spd, sparseEntry{Offset: offset, Length: length})
spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
if err != nil {
return nil, err
}
}
return spd, nil
}
Expand Down Expand Up @@ -670,12 +680,22 @@ func readGNUSparseMap0x1(paxHdrs map[string]string) (sparseDatas, error) {
if err1 != nil || err2 != nil {
return nil, ErrHeader
}
spd = append(spd, sparseEntry{Offset: offset, Length: length})
spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
if err != nil {
return nil, err
}
sparseMap = sparseMap[2:]
}
return spd, nil
}

func appendSparseEntry(spd sparseDatas, ent sparseEntry) (sparseDatas, error) {
if len(spd) >= maxSparseFileEntries {
return nil, errSparseTooLong
}
return append(spd, ent), nil
}

// Read reads from the current file in the tar archive.
// It returns (0, io.EOF) when it reaches the end of that file,
// until Next is called to advance to the next file.
Expand Down
11 changes: 11 additions & 0 deletions archive/tar/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,17 @@ func TestReadOldGNUSparseMap(t *testing.T) {
input: makeInput(FormatGNU, "",
makeSparseStrings(sparseDatas{{10 << 30, 512}, {20 << 30, 512}})...),
wantMap: sparseDatas{{10 << 30, 512}, {20 << 30, 512}},
}, {
input: makeInput(FormatGNU, "",
makeSparseStrings(func() sparseDatas {
var datas sparseDatas
// This is more than enough entries to exceed our limit.
for i := range int64(1 << 20) {
datas = append(datas, sparseEntry{i * 2, (i * 2) + 1})
}
return datas
}())...),
wantErr: errSparseTooLong,
}}

for i, v := range vectors {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/vbatts/tar-split

go 1.17
go 1.22.0

require (
github.com/fatih/color v1.15.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ=
github.com/urfave/cli v1.22.16/go.mod h1:EeJR6BKodywf4zciqrdw6hpCPk68JO9z5LazXZMn5Po=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
Loading