Skip to content

Commit

Permalink
8066709: Make some JDK system properties read only
Browse files Browse the repository at this point in the history
Reviewed-by: lancea, sundar, bchristi, weijun, mchung, alanb, mullan
  • Loading branch information
Roger Riggs committed Jun 27, 2018
1 parent cad47f4 commit 4098f25
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.nio.file.attribute.*;
import java.nio.file.spi.FileTypeDetector;
import java.io.IOException;
import java.security.AccessController;
import sun.security.action.GetPropertyAction;

import jdk.internal.util.StaticProperty;

/**
* Linux implementation of FileSystemProvider
Expand Down Expand Up @@ -102,7 +102,7 @@ public <A extends BasicFileAttributes> A readAttributes(Path file,

@Override
FileTypeDetector getFileTypeDetector() {
String userHome = GetPropertyAction.privilegedGetProperty("user.home");
String userHome = StaticProperty.userHome();
Path userMimeTypes = Path.of(userHome, ".mime.types");
Path etcMimeTypes = Path.of("/etc/mime.types");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.nio.file.Path;
import java.nio.file.spi.FileTypeDetector;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;

/**
Expand All @@ -45,8 +46,7 @@ MacOSXFileSystem newFileSystem(String dir) {

@Override
FileTypeDetector getFileTypeDetector() {
Path userMimeTypes = Path.of(GetPropertyAction
.privilegedGetProperty("user.home"), ".mime.types");
Path userMimeTypes = Path.of(StaticProperty.userHome(), ".mime.types");

return chain(new MimeTypesFileTypeDetector(userMimeTypes),
new UTIFileTypeDetector());
Expand Down
33 changes: 32 additions & 1 deletion src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import jdk.internal.util.StaticProperty;
import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.reflect.CallerSensitive;
Expand Down Expand Up @@ -669,7 +670,16 @@ public static native void arraycopy(Object src, int srcPos,
* {@code getProperties} operation, it may choose to permit the
* {@link #getProperty(String)} operation.
*
* @implNote In addition to the standard system properties, the system
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified.</strong>
* Property values may be cached during initialization or on first use.
* Setting a standard property after initialization using {@link #getProperties()},
* {@link #setProperties(Properties)}, {@link #setProperty(String, String)}, or
* {@link #clearProperty(String)} may not have the desired effect.
*
* @implNote
* In addition to the standard system properties, the system
* properties may include the following keys:
* <table class="striped">
* <caption style="display:none">Shows property keys and associated values</caption>
Expand Down Expand Up @@ -736,6 +746,11 @@ public static String lineSeparator() {
* {@code null}, then the current set of system properties is
* forgotten.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param props the new system properties.
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow access
Expand Down Expand Up @@ -768,6 +783,11 @@ public static void setProperties(Properties props) {
* properties is first created and initialized in the same manner as
* for the {@code getProperties} method.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param key the name of the system property.
* @return the string value of the system property,
* or {@code null} if there is no property with that key.
Expand Down Expand Up @@ -837,6 +857,11 @@ public static String getProperty(String key, String def) {
* If no exception is thrown, the specified property is set to the given
* value.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param key the name of the system property.
* @param value the value of the system property.
* @return the previous value of the system property,
Expand Down Expand Up @@ -875,6 +900,11 @@ public static String setProperty(String key, String value) {
* permission. This may result in a SecurityException being thrown.
* If no exception is thrown, the specified property is removed.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} method for details.
*
* @param key the name of the system property to be removed.
* @return the previous string value of the system property,
* or {@code null} if there was no property with that key.
Expand Down Expand Up @@ -1927,6 +1957,7 @@ private static void initPhase1() {
VM.saveAndRemoveProperties(props);

lineSeparator = props.getProperty("line.separator");
StaticProperty.javaHome(); // Load StaticProperty to cache the property values
VersionProps.init();

FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
Expand Down
7 changes: 4 additions & 3 deletions src/java.base/share/classes/java/net/SocksSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;

import jdk.internal.util.StaticProperty;
import sun.net.SocksProxy;
import sun.net.spi.DefaultProxySelector;
import sun.net.www.ParseUtil;
import sun.security.action.GetPropertyAction;
/* import org.ietf.jgss.*; */

/**
Expand Down Expand Up @@ -178,7 +179,7 @@ public PasswordAuthentication run() {
userName = pw.getUserName();
password = new String(pw.getPassword());
} else {
userName = GetPropertyAction.privilegedGetProperty("user.name");
userName = StaticProperty.userName();
}
if (userName == null)
return false;
Expand Down Expand Up @@ -1088,7 +1089,7 @@ private String getUserName() {
userName = System.getProperty("user.name");
} catch (SecurityException se) { /* swallow Exception */ }
} else {
userName = GetPropertyAction.privilegedGetProperty("user.name");
userName = StaticProperty.userName();
}
return userName;
}
Expand Down
3 changes: 2 additions & 1 deletion src/java.base/share/classes/java/security/Security.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;

import jdk.internal.misc.SharedSecrets;
import jdk.internal.util.StaticProperty;
import sun.security.util.Debug;
import sun.security.util.PropertyExpander;

Expand Down Expand Up @@ -214,7 +215,7 @@ private static File securityPropFile(String filename) {
// maybe check for a system property which will specify where to
// look. Someday.
String sep = File.separator;
return new File(System.getProperty("java.home") + sep + "conf" + sep +
return new File(StaticProperty.javaHome() + sep + "conf" + sep +
"security" + sep + filename);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@
*/
package java.time.zone;

import jdk.internal.util.StaticProperty;

import java.io.ByteArrayInputStream;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -106,7 +106,7 @@ final class TzdbZoneRulesProvider extends ZoneRulesProvider {
*/
public TzdbZoneRulesProvider() {
try {
String libDir = System.getProperty("java.home") + File.separator + "lib";
String libDir = StaticProperty.javaHome() + File.separator + "lib";
try (DataInputStream dis = new DataInputStream(
new BufferedInputStream(new FileInputStream(
new File(libDir, "tzdb.dat"))))) {
Expand Down
6 changes: 4 additions & 2 deletions src/java.base/share/classes/java/util/Currency.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.regex.Matcher;
import java.util.spi.CurrencyNameProvider;
import java.util.stream.Collectors;

import jdk.internal.util.StaticProperty;
import sun.util.locale.provider.CalendarDataUtility;
import sun.util.locale.provider.LocaleServiceProviderPool;
import sun.util.logging.PlatformLogger;
Expand Down Expand Up @@ -236,7 +238,7 @@ public Void run() {
// look for the properties file for overrides
String propsFile = System.getProperty("java.util.currency.data");
if (propsFile == null) {
propsFile = System.getProperty("java.home") + File.separator + "lib" +
propsFile = StaticProperty.javaHome() + File.separator + "lib" +
File.separator + "currency.properties";
}
try {
Expand Down Expand Up @@ -578,7 +580,7 @@ public int getNumericCode() {

/**
* Returns the 3 digit ISO 4217 numeric code of this currency as a {@code String}.
* Unlike {@link getNumericCode()}, which returns the numeric code as {@code int},
* Unlike {@link #getNumericCode()}, which returns the numeric code as {@code int},
* this method always returns the numeric code as a 3 digit string.
* e.g. a numeric value of 32 would be returned as "032",
* and a numeric value of 6 would be returned as "006".
Expand Down
7 changes: 3 additions & 4 deletions src/java.base/share/classes/java/util/TimeZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@
package java.util;

import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.ZoneId;
import java.util.Properties;

import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;
import sun.util.calendar.ZoneInfo;
import sun.util.calendar.ZoneInfoFile;
Expand Down Expand Up @@ -667,7 +666,7 @@ private static synchronized TimeZone setDefaultZone() {
// if the time zone ID is not set (yet), perform the
// platform to Java time zone ID mapping.
if (zoneID == null || zoneID.isEmpty()) {
String javaHome = props.getProperty("java.home");
String javaHome = StaticProperty.javaHome();
try {
zoneID = getSystemTimeZoneID(javaHome);
if (zoneID == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import java.security.*;

import java.security.Provider.Service;

import jdk.internal.util.StaticProperty;

import sun.security.jca.*;
import sun.security.jca.GetInstance.Instance;
import sun.security.util.Debug;
Expand All @@ -71,8 +73,8 @@ import sun.security.util.Debug;
*/

final class JceSecurity {


private static final Debug debug = Debug.getInstance("jca");

static final SecureRandom RANDOM = new SecureRandom();
Expand Down Expand Up @@ -307,7 +309,7 @@ final class JceSecurity {

// Prepend java.home to get the full path. normalize() in
// case an extra "." or ".." snuck in somehow.
String javaHomeProperty = System.getProperty("java.home");
String javaHomeProperty = StaticProperty.javaHome();
Path javaHomePolicyPath = Paths.get(javaHomeProperty, "conf",
"security", "policy").normalize();
Path cryptoPolicyPath = Paths.get(javaHomeProperty, "conf", "security",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import jdk.internal.misc.SharedSecrets;
import jdk.internal.module.Modules;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.util.StaticProperty;

/**
* Find resources and packages in modules defined to the boot class loader or
Expand All @@ -57,7 +58,7 @@ private BootLoader() { }

// The unnamed module for the boot loader
private static final Module UNNAMED_MODULE;
private static final String JAVA_HOME = System.getProperty("java.home");
private static final String JAVA_HOME = StaticProperty.javaHome();

static {
UNNAMED_MODULE = SharedSecrets.getJavaLangAccess().defineUnnamedModule(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import jdk.internal.jimage.ImageReaderFactory;
import jdk.internal.misc.JavaNetUriAccess;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.util.StaticProperty;
import jdk.internal.module.ModuleHashes.HashSupplier;

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ public static ModuleFinder ofSystem() {
}

// probe to see if this is an images build
String home = System.getProperty("java.home");
String home = StaticProperty.javaHome();
Path modules = Path.of(home, "lib", "modules");
if (Files.isRegularFile(modules)) {
if (USE_FAST_PATH) {
Expand Down
Loading

0 comments on commit 4098f25

Please sign in to comment.