8345223: Remove stray doPrivileged in java.base java.net and sun.net classes after JEP 486 integration

Reviewed-by: alanb, aefimov, michaelm
This commit is contained in:
Daniel Fuchs 2024-11-29 15:58:57 +00:00
parent a80ccf2cd2
commit e9136b5e08
6 changed files with 30 additions and 92 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,8 +25,6 @@
package java.net; package java.net;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration; import java.util.Enumeration;
import java.io.IOException; import java.io.IOException;
@ -105,9 +103,7 @@ class DefaultInterface {
continue; continue;
boolean ip4 = false, ip6 = false, isNonLinkLocal = false; boolean ip4 = false, ip6 = false, isNonLinkLocal = false;
PrivilegedAction<Enumeration<InetAddress>> pa = ni::getInetAddresses; Enumeration<InetAddress> addrs = ni.getInetAddresses();
@SuppressWarnings("removal")
Enumeration<InetAddress> addrs = AccessController.doPrivileged(pa);
while (addrs.hasMoreElements()) { while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement(); InetAddress addr = addrs.nextElement();
if (!addr.isAnyLocalAddress()) { if (!addr.isAnyLocalAddress()) {

View file

@ -42,7 +42,6 @@ import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import sun.net.util.IPAddressUtil; import sun.net.util.IPAddressUtil;
import sun.net.PortConfig; import sun.net.PortConfig;
import sun.security.action.GetBooleanAction;
import sun.security.util.RegisteredDomain; import sun.security.util.RegisteredDomain;
import sun.security.util.SecurityConstants; import sun.security.util.SecurityConstants;
import sun.security.util.Debug; import sun.security.util.Debug;
@ -211,7 +210,8 @@ public final class SocketPermission extends Permission
private transient boolean trusted; private transient boolean trusted;
// true if the sun.net.trustNameService system property is set // true if the sun.net.trustNameService system property is set
private static final boolean trustNameService = GetBooleanAction.privilegedGetProperty("sun.net.trustNameService"); private static final boolean trustNameService =
Boolean.getBoolean("sun.net.trustNameService");
private static Debug debug = null; private static Debug debug = null;
private static boolean debugInit = false; private static boolean debugInit = false;

View file

@ -710,13 +710,13 @@ public class FtpClient extends sun.net.ftp.FtpClient {
} else if (address.isLoopbackAddress() && s.startsWith("127.")) { // can be 127.0 } else if (address.isLoopbackAddress() && s.startsWith("127.")) { // can be 127.0
return new InetSocketAddress(s, port); return new InetSocketAddress(s, port);
} else if (address.isLoopbackAddress()) { } else if (address.isLoopbackAddress()) {
if (privilegedLocalHost().getHostAddress().equals(s)) { if (getLocalHost().getHostAddress().equals(s)) {
return new InetSocketAddress(s, port); return new InetSocketAddress(s, port);
} else { } else {
throw new FtpProtocolException(ERROR_MSG); throw new FtpProtocolException(ERROR_MSG);
} }
} else if (s.startsWith("127.")) { } else if (s.startsWith("127.")) {
if (privilegedLocalHost().equals(address)) { if (getLocalHost().equals(address)) {
return new InetSocketAddress(s, port); return new InetSocketAddress(s, port);
} else { } else {
throw new FtpProtocolException(ERROR_MSG); throw new FtpProtocolException(ERROR_MSG);
@ -724,7 +724,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
} }
String hostName = address.getHostName(); String hostName = address.getHostName();
if (!(IPAddressUtil.isIPv4LiteralAddress(hostName) || IPAddressUtil.isIPv6LiteralAddress(hostName))) { if (!(IPAddressUtil.isIPv4LiteralAddress(hostName) || IPAddressUtil.isIPv6LiteralAddress(hostName))) {
InetAddress[] names = privilegedGetAllByName(hostName); InetAddress[] names = getAllByName(hostName);
String resAddress = Arrays String resAddress = Arrays
.stream(names) .stream(names)
.map(InetAddress::getHostAddress) .map(InetAddress::getHostAddress)
@ -738,7 +738,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
throw new FtpProtocolException(ERROR_MSG); throw new FtpProtocolException(ERROR_MSG);
} }
private static InetAddress privilegedLocalHost() throws FtpProtocolException { private static InetAddress getLocalHost() throws FtpProtocolException {
try { try {
return InetAddress.getLocalHost(); return InetAddress.getLocalHost();
} catch (Exception e) { } catch (Exception e) {
@ -748,7 +748,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
} }
} }
private static InetAddress[] privilegedGetAllByName(String hostName) throws FtpProtocolException { private static InetAddress[] getAllByName(String hostName) throws FtpProtocolException {
try { try {
return InetAddress.getAllByName(hostName); return InetAddress.getAllByName(hostName);
} catch (Exception e) { } catch (Exception e) {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -117,7 +117,6 @@ public final class ResolverConfigurationImpl
// Load DNS configuration from OS // Load DNS configuration from OS
@SuppressWarnings("removal")
private void loadConfig() { private void loadConfig() {
assert Thread.holdsLock(lock); assert Thread.holdsLock(lock);
@ -130,15 +129,9 @@ public final class ResolverConfigurationImpl
} }
// get the name servers from /etc/resolv.conf // get the name servers from /etc/resolv.conf
nameservers = // typically MAXNS is 3 but we've picked 5 here
java.security.AccessController.doPrivileged( // to allow for additional servers if required.
new java.security.PrivilegedAction<>() { nameservers = resolvconf("nameserver", 1, 5);
public ArrayList<String> run() {
// typically MAXNS is 3 but we've picked 5 here
// to allow for additional servers if required.
return resolvconf("nameserver", 1, 5);
} /* run */
});
// get the search list (or domain) // get the search list (or domain)
searchlist = getSearchList(); searchlist = getSearchList();
@ -149,54 +142,19 @@ public final class ResolverConfigurationImpl
// obtain search list or local domain // obtain search list or local domain
@SuppressWarnings("removal")
private ArrayList<String> getSearchList() { private ArrayList<String> getSearchList() {
ArrayList<String> sl;
// first try the search keyword in /etc/resolv.conf // first try the search keyword in /etc/resolv.conf
sl = java.security.AccessController.doPrivileged( // first try search keyword (max 6 domains)
new java.security.PrivilegedAction<>() { ArrayList<String> sl = resolvconf("search", 6, 1);
public ArrayList<String> run() { if (sl.size() > 0) return sl;
ArrayList<String> ll;
// first try search keyword (max 6 domains)
ll = resolvconf("search", 6, 1);
if (ll.size() > 0) {
return ll;
}
return null;
} /* run */
});
if (sl != null) {
return sl;
}
// No search keyword so use local domain // No search keyword so use local domain
// try domain keyword in /etc/resolv.conf // try domain keyword in /etc/resolv.conf
sl = resolvconf("domain", 1, 1);
sl = java.security.AccessController.doPrivileged( if (sl.size() > 0) return sl;
new java.security.PrivilegedAction<>() {
public ArrayList<String> run() {
ArrayList<String> ll;
ll = resolvconf("domain", 1, 1);
if (ll.size() > 0) {
return ll;
}
return null;
} /* run */
});
if (sl != null) {
return sl;
}
// no local domain so try fallback (RPC) domain or // no local domain so try fallback (RPC) domain or
// hostName // hostName

View file

@ -35,8 +35,6 @@ import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import sun.security.action.GetPropertyAction;
/** /**
* A NetHooks provider that converts sockets from the TCP to SDP protocol prior * A NetHooks provider that converts sockets from the TCP to SDP protocol prior
* to binding or connecting. * to binding or connecting.
@ -54,7 +52,7 @@ public class SdpProvider extends NetHooks.Provider {
private PrintStream log; private PrintStream log;
public SdpProvider() { public SdpProvider() {
Properties props = GetPropertyAction.privilegedGetProperties(); Properties props = System.getProperties();
// if this property is not defined then there is nothing to do. // if this property is not defined then there is nothing to do.
String file = props.getProperty("com.sun.sdp.conf"); String file = props.getProperty("com.sun.sdp.conf");
if (file == null) { if (file == null) {

View file

@ -41,7 +41,6 @@ import sun.net.www.HeaderParser;
import sun.net.www.protocol.http.AuthenticationInfo; import sun.net.www.protocol.http.AuthenticationInfo;
import sun.net.www.protocol.http.AuthScheme; import sun.net.www.protocol.http.AuthScheme;
import sun.net.www.protocol.http.HttpURLConnection; import sun.net.www.protocol.http.HttpURLConnection;
import sun.security.action.GetPropertyAction;
/** /**
* NTLMAuthentication: * NTLMAuthentication:
@ -72,21 +71,21 @@ import sun.security.action.GetPropertyAction;
public final class NTLMAuthentication extends AuthenticationInfo { public final class NTLMAuthentication extends AuthenticationInfo {
private static final NTLMAuthenticationCallback NTLMAuthCallback = private static final NTLMAuthenticationCallback NTLMAuthCallback =
NTLMAuthenticationCallback.getNTLMAuthenticationCallback(); NTLMAuthenticationCallback.getNTLMAuthenticationCallback();
private String hostname; private String hostname;
/* Domain to use if not specified by user */ /* Domain to use if not specified by user */
private static final String defaultDomain; private static final String defaultDomain;
/* Whether cache is enabled for NTLM */ /* Whether cache is enabled for NTLM */
private static final boolean ntlmCache; private static final boolean ntlmCache;
static { static {
Properties props = GetPropertyAction.privilegedGetProperties(); defaultDomain = System.getProperty("http.auth.ntlm.domain", "");
defaultDomain = props.getProperty("http.auth.ntlm.domain", ""); String ntlmCacheProp = System.getProperty("jdk.ntlm.cache", "true");
String ntlmCacheProp = props.getProperty("jdk.ntlm.cache", "true");
ntlmCache = Boolean.parseBoolean(ntlmCacheProp); ntlmCache = Boolean.parseBoolean(ntlmCacheProp);
} }
public static boolean supportsTransparentAuth () { public static boolean supportsTransparentAuth() {
return false; return false;
} }
@ -101,23 +100,6 @@ public final class NTLMAuthentication extends AuthenticationInfo {
return false; return false;
} }
@SuppressWarnings("removal")
private void init0() {
hostname = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public String run() {
String localhost;
try {
localhost = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
localhost = "localhost";
}
return localhost;
}
});
};
PasswordAuthentication pw; PasswordAuthentication pw;
Client client; Client client;
@ -150,9 +132,13 @@ public final class NTLMAuthentication extends AuthenticationInfo {
username = s.substring (i+1); username = s.substring (i+1);
} }
password = pw.getPassword(); password = pw.getPassword();
init0();
try { try {
String version = GetPropertyAction.privilegedGetProperty("ntlm.version"); hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostname = "localhost";
}
try {
String version = System.getProperty("ntlm.version");
client = new Client(version, hostname, username, ntdomain, password); client = new Client(version, hostname, username, ntdomain, password);
} catch (NTLMException ne) { } catch (NTLMException ne) {
try { try {