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.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,8 +25,6 @@
package java.net;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.io.IOException;
@ -105,9 +103,7 @@ class DefaultInterface {
continue;
boolean ip4 = false, ip6 = false, isNonLinkLocal = false;
PrivilegedAction<Enumeration<InetAddress>> pa = ni::getInetAddresses;
@SuppressWarnings("removal")
Enumeration<InetAddress> addrs = AccessController.doPrivileged(pa);
Enumeration<InetAddress> addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
if (!addr.isAnyLocalAddress()) {

View file

@ -42,7 +42,6 @@ import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import sun.net.util.IPAddressUtil;
import sun.net.PortConfig;
import sun.security.action.GetBooleanAction;
import sun.security.util.RegisteredDomain;
import sun.security.util.SecurityConstants;
import sun.security.util.Debug;
@ -211,7 +210,8 @@ public final class SocketPermission extends Permission
private transient boolean trusted;
// 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 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
return new InetSocketAddress(s, port);
} else if (address.isLoopbackAddress()) {
if (privilegedLocalHost().getHostAddress().equals(s)) {
if (getLocalHost().getHostAddress().equals(s)) {
return new InetSocketAddress(s, port);
} else {
throw new FtpProtocolException(ERROR_MSG);
}
} else if (s.startsWith("127.")) {
if (privilegedLocalHost().equals(address)) {
if (getLocalHost().equals(address)) {
return new InetSocketAddress(s, port);
} else {
throw new FtpProtocolException(ERROR_MSG);
@ -724,7 +724,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
}
String hostName = address.getHostName();
if (!(IPAddressUtil.isIPv4LiteralAddress(hostName) || IPAddressUtil.isIPv6LiteralAddress(hostName))) {
InetAddress[] names = privilegedGetAllByName(hostName);
InetAddress[] names = getAllByName(hostName);
String resAddress = Arrays
.stream(names)
.map(InetAddress::getHostAddress)
@ -738,7 +738,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
throw new FtpProtocolException(ERROR_MSG);
}
private static InetAddress privilegedLocalHost() throws FtpProtocolException {
private static InetAddress getLocalHost() throws FtpProtocolException {
try {
return InetAddress.getLocalHost();
} 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 {
return InetAddress.getAllByName(hostName);
} 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.
*
* 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
@SuppressWarnings("removal")
private void loadConfig() {
assert Thread.holdsLock(lock);
@ -130,15 +129,9 @@ public final class ResolverConfigurationImpl
}
// get the name servers from /etc/resolv.conf
nameservers =
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
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 */
});
nameservers = resolvconf("nameserver", 1, 5);
// get the search list (or domain)
searchlist = getSearchList();
@ -149,54 +142,19 @@ public final class ResolverConfigurationImpl
// obtain search list or local domain
@SuppressWarnings("removal")
private ArrayList<String> getSearchList() {
ArrayList<String> sl;
// first try the search keyword in /etc/resolv.conf
sl = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public ArrayList<String> run() {
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;
}
ArrayList<String> sl = resolvconf("search", 6, 1);
if (sl.size() > 0) return sl;
// No search keyword so use local domain
// try domain keyword in /etc/resolv.conf
sl = java.security.AccessController.doPrivileged(
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;
}
sl = resolvconf("domain", 1, 1);
if (sl.size() > 0) return sl;
// no local domain so try fallback (RPC) domain or
// hostName

View file

@ -35,8 +35,6 @@ import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintStream;
import sun.security.action.GetPropertyAction;
/**
* A NetHooks provider that converts sockets from the TCP to SDP protocol prior
* to binding or connecting.
@ -54,7 +52,7 @@ public class SdpProvider extends NetHooks.Provider {
private PrintStream log;
public SdpProvider() {
Properties props = GetPropertyAction.privilegedGetProperties();
Properties props = System.getProperties();
// if this property is not defined then there is nothing to do.
String file = props.getProperty("com.sun.sdp.conf");
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.AuthScheme;
import sun.net.www.protocol.http.HttpURLConnection;
import sun.security.action.GetPropertyAction;
/**
* NTLMAuthentication:
@ -79,10 +78,10 @@ public final class NTLMAuthentication extends AuthenticationInfo {
private static final String defaultDomain;
/* Whether cache is enabled for NTLM */
private static final boolean ntlmCache;
static {
Properties props = GetPropertyAction.privilegedGetProperties();
defaultDomain = props.getProperty("http.auth.ntlm.domain", "");
String ntlmCacheProp = props.getProperty("jdk.ntlm.cache", "true");
defaultDomain = System.getProperty("http.auth.ntlm.domain", "");
String ntlmCacheProp = System.getProperty("jdk.ntlm.cache", "true");
ntlmCache = Boolean.parseBoolean(ntlmCacheProp);
}
@ -101,23 +100,6 @@ public final class NTLMAuthentication extends AuthenticationInfo {
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;
Client client;
@ -150,9 +132,13 @@ public final class NTLMAuthentication extends AuthenticationInfo {
username = s.substring (i+1);
}
password = pw.getPassword();
init0();
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);
} catch (NTLMException ne) {
try {