8218021: Have jarsigner preserve posix permission attributes

Reviewed-by: weijun, lancea, alanb
This commit is contained in:
Sean Coffey 2020-07-02 08:17:31 +00:00
parent dc63bf261b
commit 3d9bad16d1
14 changed files with 292 additions and 17 deletions

View file

@ -57,7 +57,7 @@ public class ZipEntry implements ZipConstants, Cloneable {
int flag = 0; // general purpose flag
byte[] extra; // optional extra field data for entry
String comment; // optional comment string for entry
int posixPerms = -1;// posix permissions
/**
* Compression method for uncompressed entries.
*/
@ -131,6 +131,7 @@ public class ZipEntry implements ZipConstants, Cloneable {
flag = e.flag;
extra = e.extra;
comment = e.comment;
posixPerms = e.posixPerms;
}
/**

View file

@ -657,6 +657,11 @@ public class ZipFile implements ZipConstants, Closeable {
e.size = CENLEN(cen, pos);
e.csize = CENSIZ(cen, pos);
e.method = CENHOW(cen, pos);
if (CENVEM_FA(cen, pos) == FILE_ATTRIBUTES_UNIX) {
// 12 bits for setuid, setgid, sticky + perms
e.posixPerms = CENATX_PERMS(cen, pos) & 0xFFF;
}
if (elen != 0) {
int start = pos + CENHDR + nlen;
e.setExtra0(Arrays.copyOfRange(cen, start, start + elen), true, false);
@ -1092,6 +1097,16 @@ public class ZipFile implements ZipConstants, Closeable {
public Stream<String> entryNameStream(ZipFile zip) {
return zip.entryNameStream();
}
// only set posix perms value via ZipEntry contructor for now
@Override
public int getPosixPerms(ZipEntry ze) {
return ze.posixPerms;
}
@Override
public void setPosixPerms(ZipEntry ze, int perms) {
ze.posixPerms = perms;
}
}
);
isWindows = VM.getSavedProperty("os.name").contains("Windows");

View file

@ -506,6 +506,15 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
}
}
/**
* Adds information about compatibility of file attribute information
* to a version value.
*/
private int versionMadeBy(ZipEntry e, int version) {
return (e.posixPerms < 0) ? version :
VERSION_MADE_BY_BASE_UNIX | (version & 0xff);
}
/*
* Write central directory (CEN) header for specified entry.
* REMIND: add support for file attributes
@ -537,10 +546,10 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
}
writeInt(CENSIG); // CEN header signature
if (hasZip64) {
writeShort(45); // ver 4.5 for zip64
writeShort(versionMadeBy(e,45)); // ver 4.5 for zip64
writeShort(45);
} else {
writeShort(version); // version made by
writeShort(versionMadeBy(e, version)); // version made by
writeShort(version); // version needed to extract
}
writeShort(flag); // general purpose bit flag
@ -597,7 +606,8 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
}
writeShort(0); // starting disk number
writeShort(0); // internal file attributes (unused)
writeInt(0); // external file attributes (unused)
// external file attributes, used for storing posix permissions
writeInt(e.posixPerms > 0 ? e.posixPerms << 16 : 0);
writeInt(offset); // relative offset of local header
writeBytes(nameBytes, 0, nameBytes.length);

View file

@ -215,6 +215,17 @@ class ZipUtils {
return LG(b, 0);
}
/*
* File attribute compatibility types of CEN field "version made by"
*/
static final int FILE_ATTRIBUTES_UNIX = 3; // Unix
/*
* Base values for CEN field "version made by"
*/
static final int VERSION_MADE_BY_BASE_UNIX = FILE_ATTRIBUTES_UNIX << 8; // Unix
// local file (LOC) header fields
static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature
static final int LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract
@ -250,6 +261,7 @@ class ZipUtils {
// central directory header (CEN) fields
static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
static final int CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
static final int CENVEM_FA(byte[] b, int pos) { return CH(b, pos + 5); } // file attribute compatibility
static final int CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
static final int CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
static final int CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
@ -263,6 +275,7 @@ class ZipUtils {
static final int CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
static final int CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
static final int CENATX_PERMS(byte[] b, int pos) { return SH(b, pos + 40);} // posix permission data
static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
// The END header is followed by a variable length comment of size < 64k.

View file

@ -30,6 +30,7 @@ import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public interface JavaUtilZipFileAccess {
@ -40,5 +41,7 @@ public interface JavaUtilZipFileAccess {
public Enumeration<JarEntry> entries(ZipFile zip);
public Stream<JarEntry> stream(ZipFile zip);
public Stream<String> entryNameStream(ZipFile zip);
public void setPosixPerms(ZipEntry ze, int posixPerms);
public int getPosixPerms(ZipEntry ze);
}

View file

@ -149,6 +149,7 @@ module java.base {
java.management,
java.naming,
java.rmi,
jdk.jartool,
jdk.jlink,
jdk.net,
jdk.incubator.foreign;

View file

@ -248,7 +248,7 @@ public class DistributionPointFetcher {
debug.println("Trying to fetch CRL from DP " + uri);
}
Event.report("event.crl.check", uri.toString());
Event.report(Event.ReporterCategory.CRLCHECK, "event.crl.check", uri.toString());
CertStore ucs = null;
try {
ucs = URICertStore.getInstance(new URICertStoreParameters(uri));

View file

@ -234,7 +234,7 @@ public final class OCSP {
debug.println("connecting to OCSP service at: " + url);
}
Event.report("event.ocsp.check", url.toString());
Event.report(Event.ReporterCategory.CRLCHECK, "event.ocsp.check", url.toString());
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setConnectTimeout(CONNECT_TIMEOUT);
con.setReadTimeout(CONNECT_TIMEOUT);

View file

@ -35,21 +35,27 @@ package sun.security.util;
public final class Event {
private Event() {}
public enum ReporterCategory {
CRLCHECK(),
POSIXPERMS();
private Reporter reporter;
}
public interface Reporter {
public void handle(String type, Object... args);
}
private static Reporter reporter;
public static void setReportListener(Reporter re) {
reporter = re;
public static void setReportListener(ReporterCategory cat, Reporter re) {
cat.reporter = re;
}
public static void clearReportListener() {
reporter = null;
public static void clearReportListener(ReporterCategory cat) {
cat.reporter = null;
}
public static void report(String type, Object... args) {
Reporter currentReporter = reporter;
public static void report(ReporterCategory cat, String type, Object... args) {
Reporter currentReporter = cat.reporter;
if (currentReporter != null) {
currentReporter.handle(type, args);