Skip to content

Commit fff7e1a

Browse files
Lance Andersenslowhog
Lance Andersen
authored andcommittedJul 18, 2023
8302483: Enhance ZIP performance
Reviewed-by: ahgross, alanb, rhalade, coffeys
1 parent 4ae3d8f commit fff7e1a

File tree

4 files changed

+194
-7
lines changed

4 files changed

+194
-7
lines changed
 

‎src/java.base/share/classes/java/util/zip/ZipFile.java

+130-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import jdk.internal.vm.annotation.Stable;
7070
import sun.nio.cs.UTF_8;
7171
import sun.nio.fs.DefaultFileSystemProvider;
72+
import sun.security.action.GetBooleanAction;
7273
import sun.security.util.SignatureFileVerifier;
7374

7475
import static java.util.zip.ZipConstants64.*;
@@ -121,6 +122,12 @@ public class ZipFile implements ZipConstants, Closeable {
121122
*/
122123
public static final int OPEN_DELETE = 0x4;
123124

125+
/**
126+
* Flag which specifies whether the validation of the Zip64 extra
127+
* fields should be disabled
128+
*/
129+
private static final boolean disableZip64ExtraFieldValidation =
130+
GetBooleanAction.privilegedGetProperty("jdk.util.zip.disableZip64ExtraFieldValidation");
124131
/**
125132
* Opens a zip file for reading.
126133
*
@@ -1199,6 +1206,16 @@ private int checkAndAddEntry(int pos, int index)
11991206
if (entryPos + nlen > cen.length - ENDHDR) {
12001207
zerror("invalid CEN header (bad header size)");
12011208
}
1209+
1210+
int elen = CENEXT(cen, pos);
1211+
if (elen > 0 && !disableZip64ExtraFieldValidation) {
1212+
long extraStartingOffset = pos + CENHDR + nlen;
1213+
if ((int)extraStartingOffset != extraStartingOffset) {
1214+
zerror("invalid CEN header (bad extra offset)");
1215+
}
1216+
checkExtraFields(pos, (int)extraStartingOffset, elen);
1217+
}
1218+
12021219
try {
12031220
ZipCoder zcp = zipCoderForPos(pos);
12041221
int hash = zcp.checkedHash(cen, entryPos, nlen);
@@ -1214,7 +1231,6 @@ private int checkAndAddEntry(int pos, int index)
12141231
// a String via zcp.toString, an Exception will be thrown
12151232
int clen = CENCOM(cen, pos);
12161233
if (clen > 0) {
1217-
int elen = CENEXT(cen, pos);
12181234
int start = entryPos + nlen + elen;
12191235
zcp.toString(cen, start, clen);
12201236
}
@@ -1224,6 +1240,119 @@ private int checkAndAddEntry(int pos, int index)
12241240
return nlen;
12251241
}
12261242

1243+
/**
1244+
* Validate the Zip64 Extra block fields
1245+
* @param startingOffset Extra Field starting offset within the CEN
1246+
* @param extraFieldLen Length of this Extra field
1247+
* @throws ZipException If an error occurs validating the Zip64 Extra
1248+
* block
1249+
*/
1250+
private void checkExtraFields(int cenPos, int startingOffset,
1251+
int extraFieldLen) throws ZipException {
1252+
// Extra field Length cannot exceed 65,535 bytes per the PKWare
1253+
// APP.note 4.4.11
1254+
if (extraFieldLen > 0xFFFF) {
1255+
zerror("invalid extra field length");
1256+
}
1257+
// CEN Offset where this Extra field ends
1258+
int extraEndOffset = startingOffset + extraFieldLen;
1259+
if (extraEndOffset > cen.length) {
1260+
zerror("Invalid CEN header (extra data field size too long)");
1261+
}
1262+
int currentOffset = startingOffset;
1263+
while (currentOffset < extraEndOffset) {
1264+
int tag = get16(cen, currentOffset);
1265+
currentOffset += Short.BYTES;
1266+
1267+
int tagBlockSize = get16(cen, currentOffset);
1268+
int tagBlockEndingOffset = currentOffset + tagBlockSize;
1269+
1270+
// The ending offset for this tag block should not go past the
1271+
// offset for the end of the extra field
1272+
if (tagBlockEndingOffset > extraEndOffset) {
1273+
zerror("Invalid CEN header (invalid zip64 extra data field size)");
1274+
}
1275+
currentOffset += Short.BYTES;
1276+
1277+
if (tag == ZIP64_EXTID) {
1278+
// Get the compressed size;
1279+
long csize = CENSIZ(cen, cenPos);
1280+
// Get the uncompressed size;
1281+
long size = CENLEN(cen, cenPos);
1282+
checkZip64ExtraFieldValues(currentOffset, tagBlockSize,
1283+
csize, size);
1284+
}
1285+
currentOffset += tagBlockSize;
1286+
}
1287+
}
1288+
1289+
/**
1290+
* Validate the Zip64 Extended Information Extra Field (0x0001) block
1291+
* size and that the uncompressed size and compressed size field
1292+
* values are not negative.
1293+
* Note: As we do not use the LOC offset or Starting disk number
1294+
* field value we will not validate them
1295+
* @param off the starting offset for the Zip64 field value
1296+
* @param blockSize the size of the Zip64 Extended Extra Field
1297+
* @param csize CEN header compressed size value
1298+
* @param size CEN header uncompressed size value
1299+
* @throws ZipException if an error occurs
1300+
*/
1301+
private void checkZip64ExtraFieldValues(int off, int blockSize, long csize,
1302+
long size)
1303+
throws ZipException {
1304+
byte[] cen = this.cen;
1305+
// Validate the Zip64 Extended Information Extra Field (0x0001)
1306+
// length.
1307+
if (!isZip64ExtBlockSizeValid(blockSize)) {
1308+
zerror("Invalid CEN header (invalid zip64 extra data field size)");
1309+
}
1310+
// Check the uncompressed size is not negative
1311+
// Note we do not need to check blockSize is >= 8 as
1312+
// we know its length is at least 8 from the call to
1313+
// isZip64ExtBlockSizeValid()
1314+
if ((size == ZIP64_MAGICVAL)) {
1315+
if(get64(cen, off) < 0) {
1316+
zerror("Invalid zip64 extra block size value");
1317+
}
1318+
}
1319+
// Check the compressed size is not negative
1320+
if ((csize == ZIP64_MAGICVAL) && (blockSize >= 16)) {
1321+
if (get64(cen, off + 8) < 0) {
1322+
zerror("Invalid zip64 extra block compressed size value");
1323+
}
1324+
}
1325+
}
1326+
1327+
/**
1328+
* Validate the size and contents of a Zip64 extended information field
1329+
* The order of the Zip64 fields is fixed, but the fields MUST
1330+
* only appear if the corresponding LOC or CEN field is set to 0xFFFF:
1331+
* or 0xFFFFFFFF:
1332+
* Uncompressed Size - 8 bytes
1333+
* Compressed Size - 8 bytes
1334+
* LOC Header offset - 8 bytes
1335+
* Disk Start Number - 4 bytes
1336+
* See PKWare APP.Note Section 4.5.3 for more details
1337+
*
1338+
* @param blockSize the Zip64 Extended Information Extra Field size
1339+
* @return true if the extra block size is valid; false otherwise
1340+
*/
1341+
private static boolean isZip64ExtBlockSizeValid(int blockSize) {
1342+
/*
1343+
* As the fields must appear in order, the block size indicates which
1344+
* fields to expect:
1345+
* 8 - uncompressed size
1346+
* 16 - uncompressed size, compressed size
1347+
* 24 - uncompressed size, compressed sise, LOC Header offset
1348+
* 28 - uncompressed size, compressed sise, LOC Header offset,
1349+
* and Disk start number
1350+
*/
1351+
return switch(blockSize) {
1352+
case 8, 16, 24, 28 -> true;
1353+
default -> false;
1354+
};
1355+
}
12271356
private int getEntryHash(int index) { return entries[index]; }
12281357
private int getEntryNext(int index) { return entries[index + 1]; }
12291358
private int getEntryPos(int index) { return entries[index + 2]; }

‎src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,11 @@ private void readExtra(ZipFileSystem zipfs) throws IOException {
30703070
if (extra == null)
30713071
return;
30723072
int elen = extra.length;
3073+
// Extra field Length cannot exceed 65,535 bytes per the PKWare
3074+
// APP.note 4.4.11
3075+
if (elen > 0xFFFF) {
3076+
throw new ZipException("invalid extra field length");
3077+
}
30733078
int off = 0;
30743079
int newOff = 0;
30753080
boolean hasZip64LocOffset = false;
@@ -3079,26 +3084,40 @@ private void readExtra(ZipFileSystem zipfs) throws IOException {
30793084
int tag = SH(extra, pos);
30803085
int sz = SH(extra, pos + 2);
30813086
pos += 4;
3082-
if (pos + sz > elen) // invalid data
3083-
break;
3087+
if (pos + sz > elen) { // invalid data
3088+
throw new ZipException("Invalid CEN header (invalid zip64 extra data field size)");
3089+
}
30843090
switch (tag) {
30853091
case EXTID_ZIP64 :
3092+
// Check to see if we have a valid block size
3093+
if (!isZip64ExtBlockSizeValid(sz)) {
3094+
throw new ZipException("Invalid CEN header (invalid zip64 extra data field size)");
3095+
}
30863096
if (size == ZIP64_MINVAL) {
30873097
if (pos + 8 > elen) // invalid zip64 extra
30883098
break; // fields, just skip
30893099
size = LL(extra, pos);
3100+
if (size < 0) {
3101+
throw new ZipException("Invalid zip64 extra block size value");
3102+
}
30903103
pos += 8;
30913104
}
30923105
if (csize == ZIP64_MINVAL) {
30933106
if (pos + 8 > elen)
30943107
break;
30953108
csize = LL(extra, pos);
3109+
if (csize < 0) {
3110+
throw new ZipException("Invalid zip64 extra block compressed size value");
3111+
}
30963112
pos += 8;
30973113
}
30983114
if (locoff == ZIP64_MINVAL) {
30993115
if (pos + 8 > elen)
31003116
break;
31013117
locoff = LL(extra, pos);
3118+
if (locoff < 0) {
3119+
throw new ZipException("Invalid zip64 extra block LOC offset value");
3120+
}
31023121
}
31033122
break;
31043123
case EXTID_NTFS:
@@ -3156,6 +3175,36 @@ private void readExtra(ZipFileSystem zipfs) throws IOException {
31563175
extra = null;
31573176
}
31583177

3178+
/**
3179+
* Validate the size and contents of a Zip64 extended information field
3180+
* The order of the Zip64 fields is fixed, but the fields MUST
3181+
* only appear if the corresponding LOC or CEN field is set to 0xFFFF:
3182+
* or 0xFFFFFFFF:
3183+
* Uncompressed Size - 8 bytes
3184+
* Compressed Size - 8 bytes
3185+
* LOC Header offset - 8 bytes
3186+
* Disk Start Number - 4 bytes
3187+
* See PKWare APP.Note Section 4.5.3 for more details
3188+
*
3189+
* @param blockSize the Zip64 Extended Information Extra Field size
3190+
* @return true if the extra block size is valid; false otherwise
3191+
*/
3192+
private static boolean isZip64ExtBlockSizeValid(int blockSize) {
3193+
/*
3194+
* As the fields must appear in order, the block size indicates which
3195+
* fields to expect:
3196+
* 8 - uncompressed size
3197+
* 16 - uncompressed size, compressed size
3198+
* 24 - uncompressed size, compressed sise, LOC Header offset
3199+
* 28 - uncompressed size, compressed sise, LOC Header offset,
3200+
* and Disk start number
3201+
*/
3202+
return switch(blockSize) {
3203+
case 8, 16, 24, 28 -> true;
3204+
default -> false;
3205+
};
3206+
}
3207+
31593208
/**
31603209
* Read the LOC extra field to obtain the Info-ZIP Extended Timestamp fields
31613210
* @param zipfs The Zip FS to use

‎test/jdk/java/util/zip/TestExtraTime.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,8 @@
2929
*/
3030

3131
import java.io.*;
32+
import java.nio.ByteBuffer;
33+
import java.nio.ByteOrder;
3234
import java.nio.file.Files;
3335
import java.nio.file.Path;
3436
import java.nio.file.Paths;
@@ -59,7 +61,14 @@ public static void main(String[] args) throws Throwable{
5961

6062
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
6163

62-
for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
64+
// A structurally valid extra data example
65+
byte[] sampleExtra = new byte[Short.BYTES*3];
66+
ByteBuffer.wrap(sampleExtra).order(ByteOrder.LITTLE_ENDIAN)
67+
.putShort((short) 123) // ID: 123
68+
.putShort((short) Short.BYTES) // Size: 2
69+
.putShort((short) 42); // Data: Two bytes
70+
71+
for (byte[] extra : new byte[][] { null, sampleExtra}) {
6372

6473
// ms-dos 1980 epoch problem
6574
test0(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);

‎test/jdk/java/util/zip/ZipFile/CorruptedZipFiles.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void insufficientFilenameLength() throws IOException {
260260
public void excessiveExtraFieldLength() throws IOException {
261261
short existingExtraLength = buffer.getShort(cenpos + CENEXT);
262262
buffer.putShort(cenpos+CENEXT, (short) (existingExtraLength + 1));
263-
assertZipException(".*bad header size.*");
263+
assertZipException(".*invalid zip64 extra data field size.*");
264264
}
265265

266266
/*
@@ -271,7 +271,7 @@ public void excessiveExtraFieldLength() throws IOException {
271271
@Test
272272
public void excessiveExtraFieldLength2() throws IOException {
273273
buffer.putShort(cenpos+CENEXT, (short) 0xfdfd);
274-
assertZipException(".*bad header size.*");
274+
assertZipException(".*extra data field size too long.*");
275275
}
276276

277277
/*

0 commit comments

Comments
 (0)
Please sign in to comment.