Skip to content

Commit 55da7d6

Browse files
neildmtrmac
authored andcommitted
archive/tar: set a limit on the size of GNU sparse file 1.0 regions
Sparse files in tar archives contain only the non-zero components of the file. There are several different encodings for sparse files. When reading GNU tar pax 1.0 sparse files, archive/tar did not set a limit on the size of the sparse region data. A malicious archive containing a large number of sparse blocks could cause archive/tar to read an unbounded amount of data from the archive into memory. Since a malicious input can be highly compressable, a small compressed input could cause very large allocations. Cap the size of the sparse block data to the same limit used for PAX headers (1 MiB). Thanks to Harshit Gupta (Mr HAX) (https://www.linkedin.com/in/iam-harshit-gupta/) for reporting this issue. Fixes CVE-2025-58183 For #75677 Fixes #75711 Change-Id: I70b907b584a7b8676df8a149a1db728ae681a770 Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/2800 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/2987 Reviewed-by: Damien Neil <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/709852 TryBot-Bypass: Michael Pratt <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Michael Pratt <[email protected]> This is a port of upstream commit 2612dcfd3cb6dd73c76e14a24fe1a68e2708e4e3 , "Copyright 2009 The Go Authors." Signed-off-by: Miloslav Trmač <[email protected]>
1 parent ced2b07 commit 55da7d6

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)