Skip to content

Commit 519bb26

Browse files
committed
8344217: Remove calls to SecurityManager and doPrivileged in java.net.DatagramSocket and java.net.NetMulticastSocket after JEP 486 integration
Reviewed-by: alanb, rriggs, jpai
1 parent d112f35 commit 519bb26

File tree

2 files changed

+12
-120
lines changed

2 files changed

+12
-120
lines changed

src/java.base/share/classes/java/net/DatagramSocket.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,9 @@ public void bind(SocketAddress addr) throws SocketException {
387387
* this socket is unknown - it may or may not be connected to the address
388388
* that it was previously connected to.
389389
*
390-
* <p> Care should be taken to ensure that a connected datagram socket
391-
* is not shared with untrusted code. When a socket is connected,
392-
* {@link #receive receive} and {@link #send send} <b>will not perform
393-
* any security checks</b> on incoming and outgoing packets, other than
394-
* matching the packet's and the socket's address and port. On a send
395-
* operation, if the packet's address is set and the packet's address
396-
* and the socket's address do not match, an {@code IllegalArgumentException}
397-
* will be thrown. A socket connected to a multicast address may only
390+
* <p> When the socket is connected, the send method checks that the
391+
* packet's address matches the remote address that the socket is
392+
* connected to. A socket connected to a multicast address may only
398393
* be used to send packets. Datagrams in the socket's {@linkplain
399394
* java.net.StandardSocketOptions#SO_RCVBUF socket receive buffer}, which
400395
* have not been {@linkplain #receive(DatagramPacket) received} before invoking
@@ -1087,11 +1082,6 @@ public DatagramChannel getChannel() {
10871082
if (factory != null) {
10881083
throw new SocketException("factory already defined");
10891084
}
1090-
@SuppressWarnings("removal")
1091-
SecurityManager security = System.getSecurityManager();
1092-
if (security != null) {
1093-
security.checkSetFactory();
1094-
}
10951085
factory = fac;
10961086
}
10971087

src/java.base/share/classes/java/net/NetMulticastSocket.java

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
import java.io.IOException;
2929
import java.io.UncheckedIOException;
30-
import java.nio.channels.DatagramChannel;
31-
import java.security.AccessController;
32-
import java.security.PrivilegedExceptionAction;
3330
import java.util.Enumeration;
3431
import java.util.Objects;
3532
import java.util.Set;
@@ -110,16 +107,6 @@ private synchronized void connectInternal(InetAddress address, int port) throws
110107
checkAddress(address, "connect");
111108
if (isClosed())
112109
return;
113-
@SuppressWarnings("removal")
114-
SecurityManager security = System.getSecurityManager();
115-
if (security != null) {
116-
if (address.isMulticastAddress()) {
117-
security.checkMulticast(address);
118-
} else {
119-
security.checkConnect(address.getHostAddress(), port);
120-
security.checkAccept(address.getHostAddress(), port);
121-
}
122-
}
123110

124111
if (port == 0) {
125112
throw new SocketException("Can't connect to port 0");
@@ -181,11 +168,7 @@ public synchronized void bind(SocketAddress addr) throws SocketException {
181168
InetAddress iaddr = epoint.getAddress();
182169
int port = epoint.getPort();
183170
checkAddress(iaddr, "bind");
184-
@SuppressWarnings("removal")
185-
SecurityManager sec = System.getSecurityManager();
186-
if (sec != null) {
187-
sec.checkListen(port);
188-
}
171+
189172
try {
190173
getImpl().bind(port, iaddr);
191174
} catch (SocketException e) {
@@ -289,22 +272,7 @@ public void send(DatagramPacket p) throws IOException {
289272
}
290273
if (packetPort < 0 || packetPort > 0xFFFF)
291274
throw new IllegalArgumentException("port out of range: " + packetPort);
292-
// check the address is ok with the security manager on every send.
293-
@SuppressWarnings("removal")
294-
SecurityManager security = System.getSecurityManager();
295-
296-
// The reason you want to synchronize on datagram packet
297-
// is because you don't want an applet to change the address
298-
// while you are trying to send the packet for example
299-
// after the security check but before the send.
300-
if (security != null) {
301-
if (packetAddress.isMulticastAddress()) {
302-
security.checkMulticast(packetAddress);
303-
} else {
304-
security.checkConnect(packetAddress.getHostAddress(),
305-
packetPort);
306-
}
307-
}
275+
308276
if (packetPort == 0) {
309277
throw new SocketException("Can't send to port 0");
310278
}
@@ -333,41 +301,13 @@ public synchronized void receive(DatagramPacket p) throws IOException {
333301
synchronized (p) {
334302
if (!isBound())
335303
bind(new InetSocketAddress(0));
336-
if (connectState == ST_NOT_CONNECTED) {
337-
// check the address is ok with the security manager before every recv.
338-
@SuppressWarnings("removal")
339-
SecurityManager security = System.getSecurityManager();
340-
if (security != null) {
341-
while (true) {
342-
int peekPort = 0;
343-
// peek at the packet to see who it is from.
344-
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
345-
peekPort = getImpl().peekData(peekPacket);
346-
String peekAd = peekPacket.getAddress().getHostAddress();
347-
try {
348-
security.checkAccept(peekAd, peekPort);
349-
// security check succeeded - so now break
350-
// and recv the packet.
351-
break;
352-
} catch (SecurityException se) {
353-
// Throw away the offending packet by consuming
354-
// it in a tmp buffer.
355-
DatagramPacket tmp = new DatagramPacket(new byte[1], 1);
356-
getImpl().receive(tmp);
357-
358-
// silently discard the offending packet
359-
// and continue: unknown/malicious
360-
// entities on nets should not make
361-
// runtime throw security exception and
362-
// disrupt the applet by sending random
363-
// datagram packets.
364-
continue;
365-
}
366-
} // end of while
367-
}
368-
}
369304
DatagramPacket tmp = null;
305+
// explicitFilter may be set to 'true' at connect() time and will
306+
// be set to 'false' in disconnect() - or when there's no more
307+
// pending packets to filter. If explicitFilter is true,
308+
// it means we're connected.
370309
if (explicitFilter) {
310+
assert connectState == ST_CONNECTED;
371311
// We have to do the filtering the old fashioned way since
372312
// the native impl doesn't support connect or the connect
373313
// via the impl failed, or .. "explicitFilter" may be set when
@@ -394,8 +334,7 @@ public synchronized void receive(DatagramPacket p) throws IOException {
394334
}
395335
}
396336
}
397-
// If the security check succeeds, or the datagram is
398-
// connected then receive the packet
337+
// receive the packet
399338
getImpl().receive(p);
400339
if (explicitFilter && tmp == null) {
401340
// packet was not filtered, account for it here
@@ -423,11 +362,6 @@ public InetAddress getLocalAddress() {
423362
if (in.isAnyLocalAddress()) {
424363
in = InetAddress.anyLocalAddress();
425364
}
426-
@SuppressWarnings("removal")
427-
SecurityManager s = System.getSecurityManager();
428-
if (s != null) {
429-
s.checkConnect(in.getHostAddress(), -1);
430-
}
431365
} catch (Exception e) {
432366
in = InetAddress.anyLocalAddress(); // "0.0.0.0"
433367
}
@@ -691,11 +625,6 @@ public void joinGroup(InetAddress mcastaddr) throws IOException {
691625
}
692626

693627
checkAddress(mcastaddr, "joinGroup");
694-
@SuppressWarnings("removal")
695-
SecurityManager security = System.getSecurityManager();
696-
if (security != null) {
697-
security.checkMulticast(mcastaddr);
698-
}
699628

700629
if (!mcastaddr.isMulticastAddress()) {
701630
throw new SocketException("Not a multicast address");
@@ -722,11 +651,6 @@ public void leaveGroup(InetAddress mcastaddr) throws IOException {
722651
}
723652

724653
checkAddress(mcastaddr, "leaveGroup");
725-
@SuppressWarnings("removal")
726-
SecurityManager security = System.getSecurityManager();
727-
if (security != null) {
728-
security.checkMulticast(mcastaddr);
729-
}
730654

731655
if (!mcastaddr.isMulticastAddress()) {
732656
throw new SocketException("Not a multicast address");
@@ -745,11 +669,6 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
745669
throw new IllegalArgumentException("Unsupported address type");
746670

747671
checkAddress(addr.getAddress(), "joinGroup");
748-
@SuppressWarnings("removal")
749-
SecurityManager security = System.getSecurityManager();
750-
if (security != null) {
751-
security.checkMulticast(addr.getAddress());
752-
}
753672

754673
if (!addr.getAddress().isMulticastAddress()) {
755674
throw new SocketException("Not a multicast address");
@@ -768,11 +687,6 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
768687
throw new IllegalArgumentException("Unsupported address type");
769688

770689
checkAddress(addr.getAddress(), "leaveGroup");
771-
@SuppressWarnings("removal")
772-
SecurityManager security = System.getSecurityManager();
773-
if (security != null) {
774-
security.checkMulticast(addr.getAddress());
775-
}
776690

777691
if (!addr.getAddress().isMulticastAddress()) {
778692
throw new SocketException("Not a multicast address");
@@ -893,22 +807,10 @@ public void send(DatagramPacket p, byte ttl)
893807
synchronized(p) {
894808
InetAddress packetAddress = p.getAddress();
895809
checkAddress(packetAddress, "send");
896-
if (connectState == NetMulticastSocket.ST_NOT_CONNECTED) {
810+
if (connectState == ST_NOT_CONNECTED) {
897811
if (packetAddress == null) {
898812
throw new IllegalArgumentException("Address not set");
899813
}
900-
// Security manager makes sure that the multicast address
901-
// is allowed one and that the ttl used is less
902-
// than the allowed maxttl.
903-
SecurityManager security = System.getSecurityManager();
904-
if (security != null) {
905-
if (packetAddress.isMulticastAddress()) {
906-
security.checkMulticast(packetAddress, ttl);
907-
} else {
908-
security.checkConnect(packetAddress.getHostAddress(),
909-
p.getPort());
910-
}
911-
}
912814
} else {
913815
// we're connected
914816
if (packetAddress == null) {

0 commit comments

Comments
 (0)