Skip to content

Commit e93e901

Browse files
authored
Merge pull request #85 from mtrmac/backport
Port a fix for CVE-2025-58183
2 parents ced2b07 + 55da7d6 commit e93e901

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

archive/tar/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
errMissData = errors.New("archive/tar: sparse file references non-existent data")
3535
errUnrefData = errors.New("archive/tar: sparse file contains unreferenced data")
3636
errWriteHole = errors.New("archive/tar: write non-NUL byte in sparse hole")
37+
errSparseTooLong = errors.New("archive/tar: sparse map too long")
3738
)
3839

3940
type headerError []string

archive/tar/reader.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,17 @@ func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
581581
cntNewline int64
582582
buf bytes.Buffer
583583
blk block
584+
totalSize int
584585
)
585586

586587
// feedTokens copies data in blocks from r into buf until there are
587588
// at least cnt newlines in buf. It will not read more blocks than needed.
588589
feedTokens := func(n int64) error {
589590
for cntNewline < n {
591+
totalSize += len(blk)
592+
if totalSize > maxSpecialFileSize {
593+
return errSparseTooLong
594+
}
590595
if _, err := mustReadFull(r, blk[:]); err != nil {
591596
return err
592597
}
@@ -619,8 +624,8 @@ func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
619624
}
620625

621626
// Parse for all member entries.
622-
// numEntries is trusted after this since a potential attacker must have
623-
// committed resources proportional to what this library used.
627+
// numEntries is trusted after this since feedTokens limits the number of
628+
// tokens based on maxSpecialFileSize.
624629
if err := feedTokens(2 * numEntries); err != nil {
625630
return nil, err
626631
}

archive/tar/reader_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,11 @@ func TestReader(t *testing.T) {
619619
},
620620
Format: FormatPAX,
621621
}},
622+
}, {
623+
// Small compressed file that uncompresses to
624+
// a file with a very large GNU 1.0 sparse map.
625+
file: "testdata/gnu-sparse-many-zeros.tar.bz2",
626+
err: errSparseTooLong,
622627
}}
623628

624629
for _, v := range vectors {
1.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)