mirror of
https://github.com/vbatts/tar-split.git
synced 2025-04-21 14:24:38 +00:00
Merge pull request #77 from vbatts/fix_cve-2022-2879
archive/tar: fix for CVE-2022-2879
This commit is contained in:
commit
80a63ccec4
2 changed files with 16 additions and 2 deletions
|
@ -143,6 +143,10 @@ const (
|
||||||
blockSize = 512 // Size of each block in a tar stream
|
blockSize = 512 // Size of each block in a tar stream
|
||||||
nameSize = 100 // Max length of the name field in USTAR format
|
nameSize = 100 // Max length of the name field in USTAR format
|
||||||
prefixSize = 155 // Max length of the prefix field in USTAR format
|
prefixSize = 155 // Max length of the prefix field in USTAR format
|
||||||
|
|
||||||
|
// Max length of a special file (PAX header, GNU long name or link).
|
||||||
|
// This matches the limit used by libarchive.
|
||||||
|
maxSpecialFileSize = 1 << 20
|
||||||
)
|
)
|
||||||
|
|
||||||
// blockPadding computes the number of bytes needed to pad offset up to the
|
// blockPadding computes the number of bytes needed to pad offset up to the
|
||||||
|
|
|
@ -144,7 +144,7 @@ func (tr *Reader) next() (*Header, error) {
|
||||||
continue // This is a meta header affecting the next header
|
continue // This is a meta header affecting the next header
|
||||||
case TypeGNULongName, TypeGNULongLink:
|
case TypeGNULongName, TypeGNULongLink:
|
||||||
format.mayOnlyBe(FormatGNU)
|
format.mayOnlyBe(FormatGNU)
|
||||||
realname, err := io.ReadAll(tr)
|
realname, err := readSpecialFile(tr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -338,7 +338,7 @@ func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
|
||||||
// parsePAX parses PAX headers.
|
// parsePAX parses PAX headers.
|
||||||
// If an extended header (type 'x') is invalid, ErrHeader is returned
|
// If an extended header (type 'x') is invalid, ErrHeader is returned
|
||||||
func parsePAX(r io.Reader) (map[string]string, error) {
|
func parsePAX(r io.Reader) (map[string]string, error) {
|
||||||
buf, err := io.ReadAll(r)
|
buf, err := readSpecialFile(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -889,6 +889,16 @@ func tryReadFull(r io.Reader, b []byte) (n int, err error) {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readSpecialFile is like io.ReadAll except it returns
|
||||||
|
// ErrFieldTooLong if more than maxSpecialFileSize is read.
|
||||||
|
func readSpecialFile(r io.Reader) ([]byte, error) {
|
||||||
|
buf, err := io.ReadAll(io.LimitReader(r, maxSpecialFileSize+1))
|
||||||
|
if len(buf) > maxSpecialFileSize {
|
||||||
|
return nil, ErrFieldTooLong
|
||||||
|
}
|
||||||
|
return buf, err
|
||||||
|
}
|
||||||
|
|
||||||
// discard skips n bytes in r, reporting an error if unable to do so.
|
// discard skips n bytes in r, reporting an error if unable to do so.
|
||||||
func discard(tr *Reader, n int64) error {
|
func discard(tr *Reader, n int64) error {
|
||||||
var seekSkipped, copySkipped int64
|
var seekSkipped, copySkipped int64
|
||||||
|
|
Loading…
Add table
Reference in a new issue