Skip to content

Commit cd3b4ca

Browse files
rolandshoemakerFiloSottile
authored andcommitted
archive/zip: fix panic in Reader.Open
When operating on a Zip file that contains a file prefixed with "../", Open(...) would cause a panic in toValidName when attempting to strip the prefixed path components. Fixes CVE-2021-27919 Fixes #44916 Change-Id: Ic755d8126cb0897e2cbbdacf572439c38dde7b35 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1004761 Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Katie Hockman <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/300489 Trust: Katie Hockman <[email protected]> Run-TryBot: Katie Hockman <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Alexander Rakoczy <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 1811aea commit cd3b4ca

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/archive/zip/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func toValidName(name string) string {
664664
if strings.HasPrefix(p, "/") {
665665
p = p[len("/"):]
666666
}
667-
for strings.HasPrefix(name, "../") {
667+
for strings.HasPrefix(p, "../") {
668668
p = p[len("../"):]
669669
}
670670
return p

src/archive/zip/reader_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,38 @@ func TestFS(t *testing.T) {
10811081
t.Fatal(err)
10821082
}
10831083
}
1084+
1085+
func TestCVE202127919(t *testing.T) {
1086+
// Archive containing only the file "../test.txt"
1087+
data := []byte{
1088+
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1089+
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1090+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1091+
0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e,
1092+
0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
1093+
0x74, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0xc8, 0x2c,
1094+
0x56, 0x48, 0x54, 0x28, 0x49, 0x2d, 0x2e, 0x51,
1095+
0x28, 0x49, 0xad, 0x28, 0x51, 0x48, 0xcb, 0xcc,
1096+
0x49, 0xd5, 0xe3, 0x02, 0x04, 0x00, 0x00, 0xff,
1097+
0xff, 0x50, 0x4b, 0x07, 0x08, 0xc0, 0xd7, 0xed,
1098+
0xc3, 0x20, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00,
1099+
0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00, 0x14,
1100+
0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1101+
0x00, 0xc0, 0xd7, 0xed, 0xc3, 0x20, 0x00, 0x00,
1102+
0x00, 0x1a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
1103+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1104+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e,
1105+
0x2e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
1106+
0x78, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00,
1107+
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x39, 0x00,
1108+
0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00,
1109+
}
1110+
r, err := NewReader(bytes.NewReader([]byte(data)), int64(len(data)))
1111+
if err != nil {
1112+
t.Fatalf("Error reading the archive: %v", err)
1113+
}
1114+
_, err = r.Open("test.txt")
1115+
if err != nil {
1116+
t.Errorf("Error reading file: %v", err)
1117+
}
1118+
}

0 commit comments

Comments
 (0)