8344218: Remove calls to SecurityManager and and doPrivileged in java.net.NetworkInterface after JEP 486 integration

Reviewed-by: dfuchs
This commit is contained in:
Eirik Bjørsnøs 2024-11-19 05:44:30 +00:00
parent d85dd77edf
commit b12c5b4d18

View File

@ -27,6 +27,7 @@ package java.net;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Spliterator; import java.util.Spliterator;
@ -79,9 +80,9 @@ public final class NetworkInterface {
private String name; private String name;
private String displayName; private String displayName;
private int index; private int index;
private InetAddress addrs[]; private InetAddress[] addrs;
private InterfaceAddress bindings[]; private InterfaceAddress[] bindings;
private NetworkInterface childs[]; private NetworkInterface[] childs;
private NetworkInterface parent = null; private NetworkInterface parent = null;
private boolean virtual = false; private boolean virtual = false;
private static final NetworkInterface defaultInterface; private static final NetworkInterface defaultInterface;
@ -118,87 +119,46 @@ public final class NetworkInterface {
} }
/** /**
* Get an Enumeration with all, or a subset, of the InetAddresses bound to * Get an Enumeration of the InetAddresses bound to this network interface.
* this network interface.
* *
* @implNote * @implNote
* The returned enumeration contains all, or a subset, of the InetAddresses that were * The returned enumeration contains the InetAddresses that were bound to
* bound to the interface at the time the {@linkplain #getNetworkInterfaces() * the interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read} * interface configuration was read}
* *
* @return an Enumeration object with all, or a subset, of the InetAddresses * @return an Enumeration object with the InetAddresses bound to this
* bound to this network interface * network interface
* @see #inetAddresses() * @see #inetAddresses()
*/ */
public Enumeration<InetAddress> getInetAddresses() { public Enumeration<InetAddress> getInetAddresses() {
return enumerationFromArray(getCheckedInetAddresses()); return enumerationFromArray(addrs);
} }
/** /**
* Get a Stream of all, or a subset, of the InetAddresses bound to this * Get a Stream of the InetAddresses bound to this network interface.
* network interface.
* *
* @implNote * @implNote
* The stream contains all, or a subset, of the InetAddresses that were * The stream contains the InetAddresses that were bound to the
* bound to the interface at the time the {@linkplain #getNetworkInterfaces() * interface at the time the {@linkplain #getNetworkInterfaces()
* interface configuration was read} * interface configuration was read}
* *
* @return a Stream object with all, or a subset, of the InetAddresses * @return a Stream object with the InetAddresses bound to this network interface
* bound to this network interface
* @since 9 * @since 9
*/ */
public Stream<InetAddress> inetAddresses() { public Stream<InetAddress> inetAddresses() {
return streamFromArray(getCheckedInetAddresses()); return streamFromArray(addrs);
}
private InetAddress[] getCheckedInetAddresses() {
InetAddress[] local_addrs = new InetAddress[addrs.length];
boolean trusted = true;
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
trusted = false;
}
}
int i = 0;
for (int j = 0; j < addrs.length; j++) {
try {
if (!trusted) {
sec.checkConnect(addrs[j].getHostAddress(), -1);
}
local_addrs[i++] = addrs[j];
} catch (SecurityException e) { }
}
return Arrays.copyOf(local_addrs, i);
} }
/** /**
* Get a List of all, or a subset, of the {@code InterfaceAddresses} * Get a List of the {@code InterfaceAddresses} of this network interface.
* of this network interface. *
* @return a {@code List} object with the InterfaceAddress of this
* network interface
* *
* @return a {@code List} object with all, or a subset, of the
* InterfaceAddress of this network interface
* @since 1.6 * @since 1.6
*/ */
public java.util.List<InterfaceAddress> getInterfaceAddresses() { public List<InterfaceAddress> getInterfaceAddresses() {
java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1); return bindings == null ? List.of() : List.of(bindings);
if (bindings != null) {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
for (int j=0; j<bindings.length; j++) {
try {
if (sec != null) {
sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
}
lst.add(bindings[j]);
} catch (SecurityException e) { }
}
}
return lst;
} }
/** /**
@ -556,18 +516,6 @@ public final class NetworkInterface {
* @since 1.6 * @since 1.6
*/ */
public byte[] getHardwareAddress() throws SocketException { public byte[] getHardwareAddress() throws SocketException {
@SuppressWarnings("removal")
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
try {
sec.checkPermission(new NetPermission("getNetworkInformation"));
} catch (SecurityException e) {
if (!getInetAddresses().hasMoreElements()) {
// don't have connect permission to any local address
return null;
}
}
}
if (isLoopback0(name, index)) { if (isLoopback0(name, index)) {
return null; return null;
} }