6969import jdk .internal .ref .CleanerFactory ;
7070import jdk .internal .vm .annotation .Stable ;
7171import sun .nio .cs .UTF_8 ;
72- import sun .security .action .GetBooleanAction ;
72+ import sun .nio .fs .DefaultFileSystemProvider ;
73+ import sun .security .action .GetPropertyAction ;
7374import sun .security .util .SignatureFileVerifier ;
7475
7576import static java .util .zip .ZipConstants64 .*;
@@ -123,11 +124,12 @@ public class ZipFile implements ZipConstants, Closeable {
123124 public static final int OPEN_DELETE = 0x4 ;
124125
125126 /**
126- * Flag which specifies whether the validation of the Zip64 extra
127- * fields should be disabled
127+ * Flag to specify whether the Extra ZIP64 validation should be
128+ * disabled.
128129 */
129- private static final boolean disableZip64ExtraFieldValidation =
130- GetBooleanAction .privilegedGetProperty ("jdk.util.zip.disableZip64ExtraFieldValidation" );
130+ private static final boolean DISABLE_ZIP64_EXTRA_VALIDATION =
131+ getDisableZip64ExtraFieldValidation ();
132+
131133 /**
132134 * Opens a zip file for reading.
133135 *
@@ -1086,6 +1088,21 @@ private int[] getMetaInfVersions() {
10861088 }
10871089
10881090 private static boolean isWindows ;
1091+ /**
1092+ * Returns the value of the System property which indicates whether the
1093+ * Extra ZIP64 validation should be disabled.
1094+ */
1095+ static boolean getDisableZip64ExtraFieldValidation () {
1096+ boolean result ;
1097+ String value = GetPropertyAction .privilegedGetProperty (
1098+ "jdk.util.zip.disableZip64ExtraFieldValidation" );
1099+ if (value == null ) {
1100+ result = false ;
1101+ } else {
1102+ result = value .isEmpty () || value .equalsIgnoreCase ("true" );
1103+ }
1104+ return result ;
1105+ }
10891106
10901107 static {
10911108 SharedSecrets .setJavaUtilZipFileAccess (
@@ -1204,7 +1221,7 @@ private int checkAndAddEntry(int pos, int index)
12041221 }
12051222
12061223 int elen = CENEXT (cen , pos );
1207- if (elen > 0 && !disableZip64ExtraFieldValidation ) {
1224+ if (elen > 0 && !DISABLE_ZIP64_EXTRA_VALIDATION ) {
12081225 long extraStartingOffset = pos + CENHDR + nlen ;
12091226 if ((int )extraStartingOffset != extraStartingOffset ) {
12101227 zerror ("invalid CEN header (bad extra offset)" );
@@ -1248,25 +1265,32 @@ private void checkExtraFields(int cenPos, int startingOffset,
12481265 zerror ("Invalid CEN header (extra data field size too long)" );
12491266 }
12501267 int currentOffset = startingOffset ;
1251- while (currentOffset < extraEndOffset ) {
1268+ // Walk through each Extra Header. Each Extra Header Must consist of:
1269+ // Header ID - 2 bytes
1270+ // Data Size - 2 bytes:
1271+ while (currentOffset + Integer .BYTES <= extraEndOffset ) {
12521272 int tag = get16 (cen , currentOffset );
12531273 currentOffset += Short .BYTES ;
12541274
12551275 int tagBlockSize = get16 (cen , currentOffset );
1276+ currentOffset += Short .BYTES ;
12561277 int tagBlockEndingOffset = currentOffset + tagBlockSize ;
12571278
12581279 // The ending offset for this tag block should not go past the
12591280 // offset for the end of the extra field
12601281 if (tagBlockEndingOffset > extraEndOffset ) {
1261- zerror ("Invalid CEN header (invalid zip64 extra data field size)" );
1282+ zerror (String .format (
1283+ "Invalid CEN header (invalid extra data field size for " +
1284+ "tag: 0x%04x at %d)" ,
1285+ tag , cenPos ));
12621286 }
1263- currentOffset += Short .BYTES ;
12641287
12651288 if (tag == ZIP64_EXTID ) {
12661289 // Get the compressed size;
12671290 long csize = CENSIZ (cen , cenPos );
12681291 // Get the uncompressed size;
12691292 long size = CENLEN (cen , cenPos );
1293+
12701294 checkZip64ExtraFieldValues (currentOffset , tagBlockSize ,
12711295 csize , size );
12721296 }
@@ -1290,6 +1314,16 @@ private void checkZip64ExtraFieldValues(int off, int blockSize, long csize,
12901314 long size )
12911315 throws ZipException {
12921316 byte [] cen = this .cen ;
1317+ // if ZIP64_EXTID blocksize == 0, which may occur with some older
1318+ // versions of Apache Ant and Commons Compress, validate csize and size
1319+ // to make sure neither field == ZIP64_MAGICVAL
1320+ if (blockSize == 0 ) {
1321+ if (csize == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL ) {
1322+ zerror ("Invalid CEN header (invalid zip64 extra data field size)" );
1323+ }
1324+ // Only validate the ZIP64_EXTID data if the block size > 0
1325+ return ;
1326+ }
12931327 // Validate the Zip64 Extended Information Extra Field (0x0001)
12941328 // length.
12951329 if (!isZip64ExtBlockSizeValid (blockSize )) {
0 commit comments