8186464: ZipFile cannot read some InfoZip ZIP64 zip files

Reviewed-by: martin
This commit is contained in:
Xueming Shen 2017-09-20 16:41:54 -07:00
parent dae210f6d6
commit 02b9452ed3
3 changed files with 126 additions and 55 deletions
src
java.base/share/classes/java/util/zip
jdk.zipfs/share/classes/jdk/nio/zipfs
test/jdk/java/util/zip/ZipFile

@ -1122,30 +1122,36 @@ class ZipFile implements ZipConstants, Closeable {
zerror("zip comment read failed"); zerror("zip comment read failed");
} }
} }
if (end.cenlen == ZIP64_MAGICVAL || // must check for a zip64 end record; it is always permitted to be present
end.cenoff == ZIP64_MAGICVAL || try {
end.centot == ZIP64_MAGICCOUNT) byte[] loc64 = new byte[ZIP64_LOCHDR];
{ if (end.endpos < ZIP64_LOCHDR ||
// need to find the zip64 end; readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
try { != loc64.length || GETSIG(loc64) != ZIP64_LOCSIG) {
byte[] loc64 = new byte[ZIP64_LOCHDR]; return end;
if (readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR) }
!= loc64.length || GETSIG(loc64) != ZIP64_LOCSIG) { long end64pos = ZIP64_LOCOFF(loc64);
return end; byte[] end64buf = new byte[ZIP64_ENDHDR];
} if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
long end64pos = ZIP64_LOCOFF(loc64); != end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) {
byte[] end64buf = new byte[ZIP64_ENDHDR]; return end;
if (readFullyAt(end64buf, 0, end64buf.length, end64pos) }
!= end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) { // end64 candidate found,
return end; long cenlen64 = ZIP64_ENDSIZ(end64buf);
} long cenoff64 = ZIP64_ENDOFF(end64buf);
// end64 found, re-calcualte everything. long centot64 = ZIP64_ENDTOT(end64buf);
end.cenlen = ZIP64_ENDSIZ(end64buf); // double-check
end.cenoff = ZIP64_ENDOFF(end64buf); if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MAGICVAL ||
end.centot = (int)ZIP64_ENDTOT(end64buf); // assume total < 2g cenoff64 != end.cenoff && end.cenoff != ZIP64_MAGICVAL ||
end.endpos = end64pos; centot64 != end.centot && end.centot != ZIP64_MAGICCOUNT) {
} catch (IOException x) {} // no zip64 loc/end return end;
} }
// to use the end64 values
end.cenlen = cenlen64;
end.cenoff = cenoff64;
end.centot = (int)centot64; // assume total < 2g
end.endpos = end64pos;
} catch (IOException x) {} // no zip64 loc/end
return end; return end;
} }
} }

@ -79,6 +79,7 @@ class ZipFileSystem extends FileSystem {
private static final boolean isWindows = AccessController.doPrivileged( private static final boolean isWindows = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) () -> System.getProperty("os.name") (PrivilegedAction<Boolean>) () -> System.getProperty("os.name")
.startsWith("Windows")); .startsWith("Windows"));
private final boolean forceEnd64;
ZipFileSystem(ZipFileSystemProvider provider, ZipFileSystem(ZipFileSystemProvider provider,
Path zfpath, Path zfpath,
@ -91,12 +92,13 @@ class ZipFileSystem extends FileSystem {
(String)env.get("encoding") : "UTF-8"; (String)env.get("encoding") : "UTF-8";
this.noExtt = "false".equals(env.get("zipinfo-time")); this.noExtt = "false".equals(env.get("zipinfo-time"));
this.useTempFile = TRUE.equals(env.get("useTempFile")); this.useTempFile = TRUE.equals(env.get("useTempFile"));
this.forceEnd64 = "true".equals(env.get("forceZIP64End"));
this.provider = provider; this.provider = provider;
this.zfpath = zfpath; this.zfpath = zfpath;
if (Files.notExists(zfpath)) { if (Files.notExists(zfpath)) {
if (createNew) { if (createNew) {
try (OutputStream os = Files.newOutputStream(zfpath, CREATE_NEW, WRITE)) { try (OutputStream os = Files.newOutputStream(zfpath, CREATE_NEW, WRITE)) {
new END().write(os, 0); new END().write(os, 0, forceEnd64);
} }
} else { } else {
throw new FileSystemNotFoundException(zfpath.toString()); throw new FileSystemNotFoundException(zfpath.toString());
@ -1000,28 +1002,36 @@ class ZipFileSystem extends FileSystem {
end.cenoff = ENDOFF(buf); end.cenoff = ENDOFF(buf);
end.comlen = ENDCOM(buf); end.comlen = ENDCOM(buf);
end.endpos = pos + i; end.endpos = pos + i;
if (end.cenlen == ZIP64_MINVAL || // try if there is zip64 end;
end.cenoff == ZIP64_MINVAL || byte[] loc64 = new byte[ZIP64_LOCHDR];
end.centot == ZIP64_MINVAL32) if (end.endpos < ZIP64_LOCHDR ||
{ readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
// need to find the zip64 end; != loc64.length ||
byte[] loc64 = new byte[ZIP64_LOCHDR]; !locator64SigAt(loc64, 0)) {
if (readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR) return end;
!= loc64.length) {
return end;
}
long end64pos = ZIP64_LOCOFF(loc64);
byte[] end64buf = new byte[ZIP64_ENDHDR];
if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
!= end64buf.length) {
return end;
}
// end64 found, re-calcualte everything.
end.cenlen = ZIP64_ENDSIZ(end64buf);
end.cenoff = ZIP64_ENDOFF(end64buf);
end.centot = (int)ZIP64_ENDTOT(end64buf); // assume total < 2g
end.endpos = end64pos;
} }
long end64pos = ZIP64_LOCOFF(loc64);
byte[] end64buf = new byte[ZIP64_ENDHDR];
if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
!= end64buf.length ||
!end64SigAt(end64buf, 0)) {
return end;
}
// end64 found,
long cenlen64 = ZIP64_ENDSIZ(end64buf);
long cenoff64 = ZIP64_ENDOFF(end64buf);
long centot64 = ZIP64_ENDTOT(end64buf);
// double-check
if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MINVAL ||
cenoff64 != end.cenoff && end.cenoff != ZIP64_MINVAL ||
centot64 != end.centot && end.centot != ZIP64_MINVAL32) {
return end;
}
// to use the end64 values
end.cenlen = cenlen64;
end.cenoff = cenoff64;
end.centot = (int)centot64; // assume total < 2g
end.endpos = end64pos;
return end; return end;
} }
} }
@ -1192,7 +1202,7 @@ class ZipFileSystem extends FileSystem {
// sync the zip file system, if there is any udpate // sync the zip file system, if there is any udpate
private void sync() throws IOException { private void sync() throws IOException {
//System.out.printf("->sync(%s) starting....!%n", toString()); // System.out.printf("->sync(%s) starting....!%n", toString());
// check ex-closer // check ex-closer
if (!exChClosers.isEmpty()) { if (!exChClosers.isEmpty()) {
for (ExChannelCloser ecc : exChClosers) { for (ExChannelCloser ecc : exChClosers) {
@ -1282,7 +1292,7 @@ class ZipFileSystem extends FileSystem {
} }
end.centot = elist.size(); end.centot = elist.size();
end.cenlen = written - end.cenoff; end.cenlen = written - end.cenoff;
end.write(os, written); end.write(os, written, forceEnd64);
} }
if (!streams.isEmpty()) { if (!streams.isEmpty()) {
// //
@ -1712,8 +1722,8 @@ class ZipFileSystem extends FileSystem {
long endpos; long endpos;
// int disktot; // int disktot;
void write(OutputStream os, long offset) throws IOException { void write(OutputStream os, long offset, boolean forceEnd64) throws IOException {
boolean hasZip64 = false; boolean hasZip64 = forceEnd64; // false;
long xlen = cenlen; long xlen = cenlen;
long xoff = cenoff; long xoff = cenoff;
if (xlen >= ZIP64_MINVAL) { if (xlen >= ZIP64_MINVAL) {
@ -1738,8 +1748,8 @@ class ZipFileSystem extends FileSystem {
writeShort(os, 45); // version needed to extract writeShort(os, 45); // version needed to extract
writeInt(os, 0); // number of this disk writeInt(os, 0); // number of this disk
writeInt(os, 0); // central directory start disk writeInt(os, 0); // central directory start disk
writeLong(os, centot); // number of directory entires on disk writeLong(os, centot); // number of directory entries on disk
writeLong(os, centot); // number of directory entires writeLong(os, centot); // number of directory entries
writeLong(os, cenlen); // length of central directory writeLong(os, cenlen); // length of central directory
writeLong(os, cenoff); // offset of central directory writeLong(os, cenoff); // offset of central directory

@ -22,19 +22,27 @@
*/ */
/* @test /* @test
@bug 4241361 4842702 4985614 6646605 5032358 6923692 6233323 8144977 @bug 4241361 4842702 4985614 6646605 5032358 6923692 6233323 8144977 8186464
@summary Make sure we can read a zip file. @summary Make sure we can read a zip file.
@key randomness @key randomness
*/ */
import java.io.*; import java.io.*;
import java.net.URI;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Map;
import java.util.zip.*; import java.util.zip.*;
import static java.nio.charset.StandardCharsets.US_ASCII;
public class ReadZip { public class ReadZip {
private static void unreached (Object o) private static void unreached (Object o)
throws Exception throws Exception
@ -137,8 +145,6 @@ public class ReadZip {
newZip.delete(); newZip.delete();
} }
// Throw a FNF exception when read a non-existing zip file // Throw a FNF exception when read a non-existing zip file
try { unreached (new ZipFile( try { unreached (new ZipFile(
new File(System.getProperty("test.src", "."), new File(System.getProperty("test.src", "."),
@ -146,5 +152,54 @@ public class ReadZip {
+ String.valueOf(new java.util.Random().nextInt()) + String.valueOf(new java.util.Random().nextInt())
+ ".zip"))); + ".zip")));
} catch (NoSuchFileException nsfe) {} } catch (NoSuchFileException nsfe) {}
// read a zip file with ZIP64 end
Path path = Paths.get(System.getProperty("test.dir", ""), "end64.zip");
try {
URI uri = URI.create("jar:" + path.toUri());
Map<String, Object> env = Map.of("create", "true", "forceZIP64End", "true");
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
Files.write(fs.getPath("hello"), "hello".getBytes());
}
try (ZipFile zf = new ZipFile(path.toFile())) {
if (!"hello".equals(new String(zf.getInputStream(new ZipEntry("hello"))
.readAllBytes(),
US_ASCII)))
throw new RuntimeException("zipfile: read entry failed");
} catch (IOException x) {
throw new RuntimeException("zipfile: zip64 end failed");
}
try (FileSystem fs = FileSystems.newFileSystem(uri, Map.of())) {
if (!"hello".equals(new String(Files.readAllBytes(fs.getPath("hello")))))
throw new RuntimeException("zipfs: read entry failed");
} catch (IOException x) {
throw new RuntimeException("zipfile: zip64 end failed");
}
} finally {
Files.deleteIfExists(path);
}
// read a zip file created via "echo hello | zip dst.zip -", which uses
// ZIP64 end record
if (Files.notExists(Paths.get("/usr/bin/zip")))
return;
try {
Process zip = new ProcessBuilder("zip", path.toString().toString(), "-").start();
OutputStream os = zip.getOutputStream();
os.write("hello".getBytes(US_ASCII));
os.close();
zip.waitFor();
if (zip.exitValue() == 0 && Files.exists(path)) {
try (ZipFile zf = new ZipFile(path.toFile())) {
if (!"hello".equals(new String(zf.getInputStream(new ZipEntry("-"))
.readAllBytes())))
throw new RuntimeException("zipfile: read entry failed");
} catch (IOException x) {
throw new RuntimeException("zipfile: zip64 end failed");
}
}
} finally {
Files.deleteIfExists(path);
}
} }
} }