mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 14:24:46 +02:00
8344220: Remove calls to SecurityManager and doPrivileged in java.net.InetAddress and sun.net.util.IPAddressUtil after JEP 486 integration
Reviewed-by: jpai, dfuchs, alanb
This commit is contained in:
parent
9527586923
commit
880f9a7b16
6 changed files with 87 additions and 297 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -25,11 +25,8 @@
|
|||
|
||||
package sun.net;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.Security;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
public final class InetAddressCachePolicy {
|
||||
|
||||
// Controls the cache policy for successful lookups only
|
||||
|
@ -60,11 +57,9 @@ public final class InetAddressCachePolicy {
|
|||
* -1: caching forever
|
||||
* any positive value: the number of seconds to cache an address for
|
||||
*
|
||||
* default value is forever (FOREVER), as we let the platform do the
|
||||
* caching. For security reasons, this caching is made forever when
|
||||
* a security manager is set.
|
||||
* default value is 30 seconds
|
||||
*/
|
||||
private static volatile int cachePolicy = FOREVER;
|
||||
private static volatile int cachePolicy = DEFAULT_POSITIVE;
|
||||
|
||||
/* The Java-level namelookup cache stale policy:
|
||||
*
|
||||
|
@ -85,40 +80,22 @@ public final class InetAddressCachePolicy {
|
|||
*/
|
||||
private static volatile int negativeCachePolicy = NEVER;
|
||||
|
||||
/*
|
||||
* Whether or not the cache policy for successful lookups was set
|
||||
* using a property (cmd line).
|
||||
*/
|
||||
private static boolean propertySet;
|
||||
|
||||
/*
|
||||
* Whether or not the cache policy for negative lookups was set
|
||||
* using a property (cmd line).
|
||||
*/
|
||||
private static boolean propertyNegativeSet;
|
||||
|
||||
/*
|
||||
* Initialize
|
||||
*/
|
||||
static {
|
||||
/* If the cache policy property is not specified
|
||||
* then the default positive cache value is used.
|
||||
*/
|
||||
Integer tmp = getProperty(cachePolicyProp, cachePolicyPropFallback);
|
||||
if (tmp != null) {
|
||||
cachePolicy = tmp < 0 ? FOREVER : tmp;
|
||||
propertySet = true;
|
||||
} else {
|
||||
/* No properties defined for positive caching. If there is no
|
||||
* security manager then use the default positive cache value.
|
||||
*/
|
||||
if (System.getSecurityManager() == null) {
|
||||
cachePolicy = DEFAULT_POSITIVE;
|
||||
}
|
||||
}
|
||||
tmp = getProperty(negativeCachePolicyProp,
|
||||
negativeCachePolicyPropFallback);
|
||||
|
||||
if (tmp != null) {
|
||||
negativeCachePolicy = tmp < 0 ? FOREVER : tmp;
|
||||
propertyNegativeSet = true;
|
||||
}
|
||||
if (cachePolicy > 0) {
|
||||
tmp = getProperty(cacheStalePolicyProp,
|
||||
|
@ -130,33 +107,25 @@ public final class InetAddressCachePolicy {
|
|||
}
|
||||
|
||||
private static Integer getProperty(String cachePolicyProp,
|
||||
String cachePolicyPropFallback)
|
||||
{
|
||||
return java.security.AccessController.doPrivileged(
|
||||
new PrivilegedAction<Integer>() {
|
||||
public Integer run() {
|
||||
try {
|
||||
String tmpString = Security.getProperty(
|
||||
cachePolicyProp);
|
||||
if (tmpString != null) {
|
||||
return Integer.valueOf(tmpString);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Ignore
|
||||
}
|
||||
String cachePolicyPropFallback) {
|
||||
try {
|
||||
String tmpString = Security.getProperty(cachePolicyProp);
|
||||
if (tmpString != null) {
|
||||
return Integer.valueOf(tmpString);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
try {
|
||||
String tmpString = System.getProperty(
|
||||
cachePolicyPropFallback);
|
||||
if (tmpString != null) {
|
||||
return Integer.decode(tmpString);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
try {
|
||||
String tmpString = System.getProperty(cachePolicyPropFallback);
|
||||
if (tmpString != null) {
|
||||
return Integer.decode(tmpString);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int get() {
|
||||
|
@ -170,63 +139,4 @@ public final class InetAddressCachePolicy {
|
|||
public static int getNegative() {
|
||||
return negativeCachePolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cache policy for successful lookups if the user has not
|
||||
* already specified a cache policy for it using a
|
||||
* command-property.
|
||||
* @param newPolicy the value in seconds for how long the lookup
|
||||
* should be cached
|
||||
*/
|
||||
public static synchronized void setIfNotSet(int newPolicy) {
|
||||
/*
|
||||
* When setting the new value we may want to signal that the
|
||||
* cache should be flushed, though this doesn't seem strictly
|
||||
* necessary.
|
||||
*/
|
||||
if (!propertySet) {
|
||||
checkValue(newPolicy, cachePolicy);
|
||||
cachePolicy = newPolicy;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cache policy for negative lookups if the user has not
|
||||
* already specified a cache policy for it using a
|
||||
* command-property.
|
||||
* @param newPolicy the value in seconds for how long the lookup
|
||||
* should be cached
|
||||
*/
|
||||
public static void setNegativeIfNotSet(int newPolicy) {
|
||||
/*
|
||||
* When setting the new value we may want to signal that the
|
||||
* cache should be flushed, though this doesn't seem strictly
|
||||
* necessary.
|
||||
*/
|
||||
if (!propertyNegativeSet) {
|
||||
// Negative caching does not seem to have any security
|
||||
// implications.
|
||||
// checkValue(newPolicy, negativeCachePolicy);
|
||||
// but we should normalize negative policy
|
||||
negativeCachePolicy = newPolicy < 0 ? FOREVER : newPolicy;
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkValue(int newPolicy, int oldPolicy) {
|
||||
/*
|
||||
* If malicious code gets a hold of this method, prevent
|
||||
* setting the cache policy to something laxer or some
|
||||
* invalid negative value.
|
||||
*/
|
||||
if (newPolicy == FOREVER)
|
||||
return;
|
||||
|
||||
if ((oldPolicy == FOREVER) ||
|
||||
(newPolicy < oldPolicy) ||
|
||||
(newPolicy < FOREVER)) {
|
||||
|
||||
throw new
|
||||
SecurityException("can't make InetAddress cache more lax");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,6 @@
|
|||
|
||||
package sun.net.util;
|
||||
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
|
@ -35,9 +33,6 @@ import java.net.NetworkInterface;
|
|||
import java.net.SocketException;
|
||||
import java.net.URL;
|
||||
import java.nio.CharBuffer;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
@ -396,25 +391,23 @@ public class IPAddressUtil {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
private static InetAddress findScopedAddress(InetAddress address) {
|
||||
PrivilegedExceptionAction<List<InetAddress>> pa = () -> NetworkInterface.networkInterfaces()
|
||||
.flatMap(NetworkInterface::inetAddresses)
|
||||
.filter(a -> (a instanceof Inet6Address)
|
||||
&& address.equals(a)
|
||||
&& ((Inet6Address) a).getScopeId() != 0)
|
||||
.toList();
|
||||
List<InetAddress> result;
|
||||
try {
|
||||
result = AccessController.doPrivileged(pa);
|
||||
List<InetAddress> result = NetworkInterface.networkInterfaces()
|
||||
.flatMap(NetworkInterface::inetAddresses)
|
||||
.filter(a -> (a instanceof Inet6Address)
|
||||
&& address.equals(a)
|
||||
&& ((Inet6Address) a).getScopeId() != 0)
|
||||
.toList();
|
||||
|
||||
var sz = result.size();
|
||||
if (sz == 0)
|
||||
return null;
|
||||
if (sz > 1)
|
||||
throw new UncheckedIOException(new SocketException(
|
||||
"Duplicate link local addresses: must specify scope-id"));
|
||||
"Duplicate link local addresses: must specify scope-id"));
|
||||
return result.get(0);
|
||||
} catch (PrivilegedActionException pae) {
|
||||
} catch (SocketException socketException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -927,8 +920,8 @@ public class IPAddressUtil {
|
|||
private static final long TERMINAL_PARSE_ERROR = -2L;
|
||||
|
||||
private static final String ALLOW_AMBIGUOUS_IPADDRESS_LITERALS_SP = "jdk.net.allowAmbiguousIPAddressLiterals";
|
||||
private static final boolean ALLOW_AMBIGUOUS_IPADDRESS_LITERALS_SP_VALUE = Boolean.valueOf(
|
||||
GetPropertyAction.privilegedGetProperty(ALLOW_AMBIGUOUS_IPADDRESS_LITERALS_SP, "false"));
|
||||
private static final boolean ALLOW_AMBIGUOUS_IPADDRESS_LITERALS_SP_VALUE =
|
||||
Boolean.getBoolean(ALLOW_AMBIGUOUS_IPADDRESS_LITERALS_SP);
|
||||
private static class MASKS {
|
||||
private static final String DELAY_URL_PARSING_SP = "jdk.net.url.delayParsing";
|
||||
private static final boolean DELAY_URL_PARSING_SP_VALUE;
|
||||
|
@ -939,8 +932,7 @@ public class IPAddressUtil {
|
|||
static final long L_SCOPE_MASK;
|
||||
static final long H_SCOPE_MASK;
|
||||
static {
|
||||
var value = GetPropertyAction.privilegedGetProperty(
|
||||
DELAY_URL_PARSING_SP, "false");
|
||||
var value = System.getProperty(DELAY_URL_PARSING_SP, "false");
|
||||
DELAY_URL_PARSING_SP_VALUE = value.isEmpty()
|
||||
|| Boolean.parseBoolean(value);
|
||||
if (DELAY_URL_PARSING_SP_VALUE) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue