From: Kohei Tokunaga Date: Tue, 26 May 2026 03:25:52 +0000 Subject: estargz: Fix panic in go 1.27 by emitting footer manually In Go 1.27, gzip.Writer behavior changed when closing an empty stream with gzip.NoCompression, generating a 48-byte buffer instead of the expected 51-byte footer and causing a panic in gzipFooterBytes. Fix this by emitting the gzip/deflate footer manually (CreateGzipFooter). Origin: upstream, https://github.com/containerd/stargz-snapshotter/commit/0398955aa5b5b25202a99d48a577ee65f184c30f Bug-Debian: https://bugs.debian.org/1146189 Forwarded: not-needed --- estargz/externaltoc/externaltoc.go | 19 +++++++-------- estargz/gzip.go | 47 +++++++++++++++++++++++++++++++------- estargz/gzip_test.go | 12 ++++------ 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/estargz/externaltoc/externaltoc.go b/estargz/externaltoc/externaltoc.go index 5f46617..5517a9f 100644 --- a/estargz/externaltoc/externaltoc.go +++ b/estargz/externaltoc/externaltoc.go @@ -126,23 +126,20 @@ const FooterSize = 46 // gzipFooterBytes returns the 104 bytes footer. func gzipFooterBytes() ([]byte, error) { - buf := bytes.NewBuffer(make([]byte, 0, FooterSize)) - gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) // MUST be NoCompression to keep 51 bytes - - // Extra header indicating the offset of TOCJSON + // Extra header indicating the external toc // https://tools.ietf.org/html/rfc1952#section-2.3.1.1 header := make([]byte, 4) header[0], header[1] = 'S', 'G' subfield := "STARGZEXTERNALTOC" // len("STARGZEXTERNALTOC") = 17 binary.LittleEndian.PutUint16(header[2:4], uint16(len(subfield))) // little-endian per RFC1952 - gz.Header.Extra = append(header, []byte(subfield)...) - if err := gz.Close(); err != nil { - return nil, err - } - if buf.Len() != FooterSize { - panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), FooterSize)) + extra := append(header, []byte(subfield)...) + + buf := estargz.CreateGzipFooter(extra) + + if len(buf) != FooterSize { + panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), FooterSize)) } - return buf.Bytes(), nil + return buf, nil } func NewGzipDecompressor(provideTOCFunc func() ([]byte, error)) *GzipDecompressor { diff --git a/estargz/gzip.go b/estargz/gzip.go index f24afe3..1ebe41d 100644 --- a/estargz/gzip.go +++ b/estargz/gzip.go @@ -100,21 +100,52 @@ func (gc *GzipCompressor) WriteTOCAndFooter(w io.Writer, off int64, toc *JTOC, d // gzipFooterBytes returns the 51 bytes footer. func gzipFooterBytes(tocOff int64) []byte { - buf := bytes.NewBuffer(make([]byte, 0, FooterSize)) - gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) // MUST be NoCompression to keep 51 bytes - // Extra header indicating the offset of TOCJSON // https://tools.ietf.org/html/rfc1952#section-2.3.1.1 header := make([]byte, 4) header[0], header[1] = 'S', 'G' subfield := fmt.Sprintf("%016xSTARGZ", tocOff) binary.LittleEndian.PutUint16(header[2:4], uint16(len(subfield))) // little-endian per RFC1952 - gz.Header.Extra = append(header, []byte(subfield)...) - gz.Close() - if buf.Len() != FooterSize { - panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), FooterSize)) + extra := append(header, []byte(subfield)...) + + buf := CreateGzipFooter(extra) + if len(buf) != FooterSize { + panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), FooterSize)) } - return buf.Bytes() + return buf +} + +// CreateGzipFooter creates eStargz's footer with the specified extra field. +func CreateGzipFooter(extra []byte) []byte { + // Gzip header (10 bytes) + // https://datatracker.ietf.org/doc/html/rfc1952 + buf := make([]byte, 10) + buf[0] = 0x1f // ID1 + buf[1] = 0x8b // ID2 + buf[2] = 8 // "deflate" + buf[3] = 0x4 // FEXTRA + // MTIME = 0 + // XFL = 0 + buf[9] = 255 // unknown + + // Extra field + xlen := make([]byte, 2) + binary.LittleEndian.PutUint16(xlen, uint16(len(extra))) + buf = append(buf, append(xlen, extra...)...) + + // Flate header (5 bytes) + // https://datatracker.ietf.org/doc/html/rfc1951 + flateHeader := []byte{ + 1, // BFINAL = 1 (last block), BFTYPE = 0 (no compression), remaining bits are ignored + 0, 0, // LEN = 0 + 0xff, 0xff, // NLEN (the one's complement of LEN) + } + buf = append(buf, flateHeader...) + + // Gzip footer (8 bytes): CRC32 = 0, ISIZE = 0 + buf = append(buf, make([]byte, 8)...) + + return buf } type GzipDecompressor struct{} diff --git a/estargz/gzip_test.go b/estargz/gzip_test.go index 346725a..c20aee4 100644 --- a/estargz/gzip_test.go +++ b/estargz/gzip_test.go @@ -23,7 +23,6 @@ package estargz import ( - "bytes" "compress/gzip" "fmt" "testing" @@ -104,12 +103,9 @@ func checkLegacyFooter(t *testing.T, off int64) { } func legacyFooterBytes(tocOff int64) []byte { - buf := bytes.NewBuffer(make([]byte, 0, legacyFooterSize)) - gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) - gz.Header.Extra = []byte(fmt.Sprintf("%016xSTARGZ", tocOff)) - gz.Close() - if buf.Len() != legacyFooterSize { - panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), legacyFooterSize)) + buf := CreateGzipFooter(fmt.Appendf(nil, "%016xSTARGZ", tocOff)) + if len(buf) != legacyFooterSize { + panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), legacyFooterSize)) } - return buf.Bytes() + return buf }