8344215: Remove calls to SecurityManager and doPrivileged in java.net.Socket and java.net.ServerSocket after JEP 486 integration

Reviewed-by: dfuchs, alanb, jpai
This commit is contained in:
Volkan Yazıcı 2024-11-20 13:59:52 +00:00 committed by Daniel Fuchs
parent 4bc826ac1e
commit 21f0ed50a2
2 changed files with 6 additions and 112 deletions

View File

@ -33,7 +33,6 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.Collections; import java.util.Collections;
import sun.security.util.SecurityConstants;
import sun.net.PlatformSocketImpl; import sun.net.PlatformSocketImpl;
/** /**
@ -89,13 +88,6 @@ public class ServerSocket implements java.io.Closeable {
// used to coordinate creating and closing underlying socket // used to coordinate creating and closing underlying socket
private final Object socketLock = new Object(); private final Object socketLock = new Object();
/**
* Creates a server socket with the given {@code SocketImpl}.
*/
private ServerSocket(Void unused, SocketImpl impl) {
this.impl = Objects.requireNonNull(impl);
}
/** /**
* Creates a server socket with a user-specified {@code SocketImpl}. * Creates a server socket with a user-specified {@code SocketImpl}.
* *
@ -106,16 +98,7 @@ public class ServerSocket implements java.io.Closeable {
* @since 12 * @since 12
*/ */
protected ServerSocket(SocketImpl impl) { protected ServerSocket(SocketImpl impl) {
this(checkPermission(), impl); this.impl = Objects.requireNonNull(impl);
}
private static Void checkPermission() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);
}
return null;
} }
/** /**
@ -237,7 +220,7 @@ public class ServerSocket implements java.io.Closeable {
this.impl = createImpl(); this.impl = createImpl();
try { try {
bind(new InetSocketAddress(bindAddr, port), backlog); bind(new InetSocketAddress(bindAddr, port), backlog);
} catch (IOException | SecurityException e) { } catch (IOException e) {
close(); close();
throw e; throw e;
} }
@ -338,11 +321,6 @@ public class ServerSocket implements java.io.Closeable {
if (backlog < 1) if (backlog < 1)
backlog = 50; backlog = 50;
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkListen(epoint.getPort());
// SocketImpl bind+listen throw if already bound or closed // SocketImpl bind+listen throw if already bound or closed
SocketImpl impl = getImpl(); SocketImpl impl = getImpl();
impl.bind(epoint.getAddress(), epoint.getPort()); impl.bind(epoint.getAddress(), epoint.getPort());
@ -364,14 +342,7 @@ public class ServerSocket implements java.io.Closeable {
if (!isBound()) if (!isBound())
return null; return null;
try { try {
InetAddress in = getImpl().getInetAddress(); return getImpl().getInetAddress();
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkConnect(in.getHostAddress(), -1);
return in;
} catch (SecurityException e) {
return InetAddress.getLoopbackAddress();
} catch (SocketException e) { } catch (SocketException e) {
// nothing // nothing
// If we're bound, the impl has been created // If we're bound, the impl has been created
@ -631,17 +602,6 @@ public class ServerSocket implements java.io.Closeable {
throw e; throw e;
} }
// check permission, close SocketImpl/connection if denied
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
} catch (SecurityException se) {
si.close();
throw se;
}
}
} }
/** /**
@ -835,15 +795,10 @@ public class ServerSocket implements java.io.Closeable {
* *
* @return a string representation of this socket. * @return a string representation of this socket.
*/ */
@SuppressWarnings("removal")
public String toString() { public String toString() {
if (!isBound()) if (!isBound())
return "ServerSocket[unbound]"; return "ServerSocket[unbound]";
InetAddress in; InetAddress in = impl.getInetAddress();
if (System.getSecurityManager() != null)
in = getInetAddress();
else
in = impl.getInetAddress();
return "ServerSocket[addr=" + in + return "ServerSocket[addr=" + in +
",localport=" + impl.getLocalPort() + "]"; ",localport=" + impl.getLocalPort() + "]";
} }
@ -884,11 +839,6 @@ public class ServerSocket implements java.io.Closeable {
if (factory != null) { if (factory != null) {
throw new SocketException("factory already defined"); throw new SocketException("factory already defined");
} }
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
factory = fac; factory = fac;
} }

View File

@ -28,7 +28,6 @@ package java.net;
import jdk.internal.event.SocketReadEvent; import jdk.internal.event.SocketReadEvent;
import jdk.internal.event.SocketWriteEvent; import jdk.internal.event.SocketWriteEvent;
import jdk.internal.invoke.MhUtil; import jdk.internal.invoke.MhUtil;
import sun.security.util.SecurityConstants;
import java.io.InputStream; import java.io.InputStream;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
@ -157,15 +156,6 @@ public class Socket implements java.io.Closeable {
return (s & SHUT_OUT) != 0; return (s & SHUT_OUT) != 0;
} }
/**
* Creates an unconnected socket with the given {@code SocketImpl}.
*/
private Socket(Void unused, SocketImpl impl) {
if (impl != null) {
this.impl = impl;
}
}
/** /**
* Creates an unconnected Socket. * Creates an unconnected Socket.
* <p> * <p>
@ -212,22 +202,10 @@ public class Socket implements java.io.Closeable {
: sun.net.ApplicationProxy.create(proxy); : sun.net.ApplicationProxy.create(proxy);
Proxy.Type type = p.type(); Proxy.Type type = p.type();
if (type == Proxy.Type.SOCKS || type == Proxy.Type.HTTP) { if (type == Proxy.Type.SOCKS || type == Proxy.Type.HTTP) {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
InetSocketAddress epoint = (InetSocketAddress) p.address(); InetSocketAddress epoint = (InetSocketAddress) p.address();
if (epoint.getAddress() != null) { if (epoint.getAddress() != null) {
checkAddress (epoint.getAddress(), "Socket"); checkAddress (epoint.getAddress(), "Socket");
} }
if (security != null) {
if (epoint.isUnresolved())
epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort());
if (epoint.isUnresolved())
security.checkConnect(epoint.getHostName(), epoint.getPort());
else
security.checkConnect(epoint.getAddress().getHostAddress(),
epoint.getPort());
}
// create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl // create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false); SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate) impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate)
@ -259,18 +237,9 @@ public class Socket implements java.io.Closeable {
* @since 1.1 * @since 1.1
*/ */
protected Socket(SocketImpl impl) throws SocketException { protected Socket(SocketImpl impl) throws SocketException {
this(checkPermission(impl), impl);
}
private static Void checkPermission(SocketImpl impl) {
if (impl != null) { if (impl != null) {
@SuppressWarnings("removal") this.impl = impl;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);
}
} }
return null;
} }
/** /**
@ -497,7 +466,7 @@ public class Socket implements java.io.Closeable {
if (localAddr != null) if (localAddr != null)
bind(localAddr); bind(localAddr);
connect(address); connect(address);
} catch (IOException | IllegalArgumentException | SecurityException e) { } catch (IOException | IllegalArgumentException e) {
try { try {
close(); close();
} catch (IOException ce) { } catch (IOException ce) {
@ -684,15 +653,6 @@ public class Socket implements java.io.Closeable {
int port = epoint.getPort(); int port = epoint.getPort();
checkAddress(addr, "connect"); checkAddress(addr, "connect");
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
if (epoint.isUnresolved())
security.checkConnect(epoint.getHostName(), port);
else
security.checkConnect(addr.getHostAddress(), port);
}
try { try {
getImpl().connect(epoint, timeout); getImpl().connect(epoint, timeout);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
@ -743,11 +703,6 @@ public class Socket implements java.io.Closeable {
InetAddress addr = epoint.getAddress(); InetAddress addr = epoint.getAddress();
int port = epoint.getPort(); int port = epoint.getPort();
checkAddress (addr, "bind"); checkAddress (addr, "bind");
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkListen(port);
}
getImpl().bind(addr, port); getImpl().bind(addr, port);
getAndBitwiseOrState(BOUND); getAndBitwiseOrState(BOUND);
} }
@ -795,15 +750,9 @@ public class Socket implements java.io.Closeable {
InetAddress in = null; InetAddress in = null;
try { try {
in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkConnect(in.getHostAddress(), -1);
if (in.isAnyLocalAddress()) { if (in.isAnyLocalAddress()) {
in = InetAddress.anyLocalAddress(); in = InetAddress.anyLocalAddress();
} }
} catch (SecurityException e) {
in = InetAddress.getLoopbackAddress();
} catch (Exception e) { } catch (Exception e) {
in = InetAddress.anyLocalAddress(); // "0.0.0.0" in = InetAddress.anyLocalAddress(); // "0.0.0.0"
} }
@ -1855,11 +1804,6 @@ public class Socket implements java.io.Closeable {
if (factory != null) { if (factory != null) {
throw new SocketException("factory already defined"); throw new SocketException("factory already defined");
} }
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
factory = fac; factory = fac;
} }