| 1 |
From: Kohei Tokunaga <[email protected]> |
| 2 |
Date: Tue, 26 May 2026 03:25:52 +0000 |
| 3 |
Subject: estargz: Fix panic in go 1.27 by emitting footer manually |
| 4 |
|
| 5 |
In Go 1.27, gzip.Writer behavior changed when closing an empty stream |
| 6 |
with gzip.NoCompression, generating a 48-byte buffer instead of the |
| 7 |
expected 51-byte footer and causing a panic in gzipFooterBytes. |
| 8 |
Fix this by emitting the gzip/deflate footer manually (CreateGzipFooter). |
| 9 |
|
| 10 |
Origin: upstream, https://github.com/containerd/stargz-snapshotter/commit/0398955aa5b5b25202a99d48a577ee65f184c30f |
| 11 |
Bug-Debian: https://bugs.debian.org/1146189 |
| 12 |
Forwarded: not-needed |
| 13 |
--- |
| 14 |
estargz/externaltoc/externaltoc.go | 19 +++++++-------- |
| 15 |
estargz/gzip.go | 47 +++++++++++++++++++++++++++++++------- |
| 16 |
estargz/gzip_test.go | 12 ++++------ |
| 17 |
3 files changed, 51 insertions(+), 27 deletions(-) |
| 18 |
|
| 19 |
diff --git a/estargz/externaltoc/externaltoc.go b/estargz/externaltoc/externaltoc.go |
| 20 |
index 5f46617..5517a9f 100644 |
| 21 |
--- a/estargz/externaltoc/externaltoc.go |
| 22 |
+++ b/estargz/externaltoc/externaltoc.go |
| 23 |
@@ -126,23 +126,20 @@ const FooterSize = 46 |
| 24 |
|
| 25 |
// gzipFooterBytes returns the 104 bytes footer. |
| 26 |
func gzipFooterBytes() ([]byte, error) { |
| 27 |
- buf := bytes.NewBuffer(make([]byte, 0, FooterSize)) |
| 28 |
- gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) // MUST be NoCompression to keep 51 bytes |
| 29 |
- |
| 30 |
- // Extra header indicating the offset of TOCJSON |
| 31 |
+ // Extra header indicating the external toc |
| 32 |
// https://tools.ietf.org/html/rfc1952#section-2.3.1.1 |
| 33 |
header := make([]byte, 4) |
| 34 |
header[0], header[1] = 'S', 'G' |
| 35 |
subfield := "STARGZEXTERNALTOC" // len("STARGZEXTERNALTOC") = 17 |
| 36 |
binary.LittleEndian.PutUint16(header[2:4], uint16(len(subfield))) // little-endian per RFC1952 |
| 37 |
- gz.Header.Extra = append(header, []byte(subfield)...) |
| 38 |
- if err := gz.Close(); err != nil { |
| 39 |
- return nil, err |
| 40 |
- } |
| 41 |
- if buf.Len() != FooterSize { |
| 42 |
- panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), FooterSize)) |
| 43 |
+ extra := append(header, []byte(subfield)...) |
| 44 |
+ |
| 45 |
+ buf := estargz.CreateGzipFooter(extra) |
| 46 |
+ |
| 47 |
+ if len(buf) != FooterSize { |
| 48 |
+ panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), FooterSize)) |
| 49 |
} |
| 50 |
- return buf.Bytes(), nil |
| 51 |
+ return buf, nil |
| 52 |
} |
| 53 |
|
| 54 |
func NewGzipDecompressor(provideTOCFunc func() ([]byte, error)) *GzipDecompressor { |
| 55 |
diff --git a/estargz/gzip.go b/estargz/gzip.go |
| 56 |
index f24afe3..1ebe41d 100644 |
| 57 |
--- a/estargz/gzip.go |
| 58 |
+++ b/estargz/gzip.go |
| 59 |
@@ -100,21 +100,52 @@ func (gc *GzipCompressor) WriteTOCAndFooter(w io.Writer, off int64, toc *JTOC, d |
| 60 |
|
| 61 |
// gzipFooterBytes returns the 51 bytes footer. |
| 62 |
func gzipFooterBytes(tocOff int64) []byte { |
| 63 |
- buf := bytes.NewBuffer(make([]byte, 0, FooterSize)) |
| 64 |
- gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) // MUST be NoCompression to keep 51 bytes |
| 65 |
- |
| 66 |
// Extra header indicating the offset of TOCJSON |
| 67 |
// https://tools.ietf.org/html/rfc1952#section-2.3.1.1 |
| 68 |
header := make([]byte, 4) |
| 69 |
header[0], header[1] = 'S', 'G' |
| 70 |
subfield := fmt.Sprintf("%016xSTARGZ", tocOff) |
| 71 |
binary.LittleEndian.PutUint16(header[2:4], uint16(len(subfield))) // little-endian per RFC1952 |
| 72 |
- gz.Header.Extra = append(header, []byte(subfield)...) |
| 73 |
- gz.Close() |
| 74 |
- if buf.Len() != FooterSize { |
| 75 |
- panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), FooterSize)) |
| 76 |
+ extra := append(header, []byte(subfield)...) |
| 77 |
+ |
| 78 |
+ buf := CreateGzipFooter(extra) |
| 79 |
+ if len(buf) != FooterSize { |
| 80 |
+ panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), FooterSize)) |
| 81 |
} |
| 82 |
- return buf.Bytes() |
| 83 |
+ return buf |
| 84 |
+} |
| 85 |
+ |
| 86 |
+// CreateGzipFooter creates eStargz's footer with the specified extra field. |
| 87 |
+func CreateGzipFooter(extra []byte) []byte { |
| 88 |
+ // Gzip header (10 bytes) |
| 89 |
+ // https://datatracker.ietf.org/doc/html/rfc1952 |
| 90 |
+ buf := make([]byte, 10) |
| 91 |
+ buf[0] = 0x1f // ID1 |
| 92 |
+ buf[1] = 0x8b // ID2 |
| 93 |
+ buf[2] = 8 // "deflate" |
| 94 |
+ buf[3] = 0x4 // FEXTRA |
| 95 |
+ // MTIME = 0 |
| 96 |
+ // XFL = 0 |
| 97 |
+ buf[9] = 255 // unknown |
| 98 |
+ |
| 99 |
+ // Extra field |
| 100 |
+ xlen := make([]byte, 2) |
| 101 |
+ binary.LittleEndian.PutUint16(xlen, uint16(len(extra))) |
| 102 |
+ buf = append(buf, append(xlen, extra...)...) |
| 103 |
+ |
| 104 |
+ // Flate header (5 bytes) |
| 105 |
+ // https://datatracker.ietf.org/doc/html/rfc1951 |
| 106 |
+ flateHeader := []byte{ |
| 107 |
+ 1, // BFINAL = 1 (last block), BFTYPE = 0 (no compression), remaining bits are ignored |
| 108 |
+ 0, 0, // LEN = 0 |
| 109 |
+ 0xff, 0xff, // NLEN (the one's complement of LEN) |
| 110 |
+ } |
| 111 |
+ buf = append(buf, flateHeader...) |
| 112 |
+ |
| 113 |
+ // Gzip footer (8 bytes): CRC32 = 0, ISIZE = 0 |
| 114 |
+ buf = append(buf, make([]byte, 8)...) |
| 115 |
+ |
| 116 |
+ return buf |
| 117 |
} |
| 118 |
|
| 119 |
type GzipDecompressor struct{} |
| 120 |
diff --git a/estargz/gzip_test.go b/estargz/gzip_test.go |
| 121 |
index 346725a..c20aee4 100644 |
| 122 |
--- a/estargz/gzip_test.go |
| 123 |
+++ b/estargz/gzip_test.go |
| 124 |
@@ -23,7 +23,6 @@ |
| 125 |
package estargz |
| 126 |
|
| 127 |
import ( |
| 128 |
- "bytes" |
| 129 |
"compress/gzip" |
| 130 |
"fmt" |
| 131 |
"testing" |
| 132 |
@@ -104,12 +103,9 @@ func checkLegacyFooter(t *testing.T, off int64) { |
| 133 |
} |
| 134 |
|
| 135 |
func legacyFooterBytes(tocOff int64) []byte { |
| 136 |
- buf := bytes.NewBuffer(make([]byte, 0, legacyFooterSize)) |
| 137 |
- gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression) |
| 138 |
- gz.Header.Extra = []byte(fmt.Sprintf("%016xSTARGZ", tocOff)) |
| 139 |
- gz.Close() |
| 140 |
- if buf.Len() != legacyFooterSize { |
| 141 |
- panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), legacyFooterSize)) |
| 142 |
+ buf := CreateGzipFooter(fmt.Appendf(nil, "%016xSTARGZ", tocOff)) |
| 143 |
+ if len(buf) != legacyFooterSize { |
| 144 |
+ panic(fmt.Sprintf("footer buffer = %d, not %d", len(buf), legacyFooterSize)) |
| 145 |
} |
| 146 |
- return buf.Bytes() |
| 147 |
+ return buf |
| 148 |
} |