Skip to content

Commit 7be7772

Browse files
author
Alan Bateman
committed
8344112: Remove code to support security manager execution mode from DatagramChannel implementation
Reviewed-by: dfuchs
1 parent bd3fec3 commit 7be7772

File tree

2 files changed

+38
-202
lines changed

2 files changed

+38
-202
lines changed

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java

Lines changed: 31 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
import java.nio.channels.SelectionKey;
6161
import java.nio.channels.spi.AbstractSelectableChannel;
6262
import java.nio.channels.spi.SelectorProvider;
63-
import java.security.AccessController;
64-
import java.security.PrivilegedExceptionAction;
6563
import java.util.Collections;
6664
import java.util.HashMap;
6765
import java.util.HashSet;
@@ -293,8 +291,7 @@ public DatagramSocket socket() {
293291
public SocketAddress getLocalAddress() throws IOException {
294292
synchronized (stateLock) {
295293
ensureOpen();
296-
// Perform security check before returning address
297-
return Net.getRevealedLocalAddress(localAddress);
294+
return localAddress;
298295
}
299296
}
300297

@@ -573,24 +570,16 @@ public SocketAddress receive(ByteBuffer dst) throws IOException {
573570
SocketAddress remote = beginRead(blocking, false);
574571
configureSocketNonBlockingIfVirtualThread();
575572
boolean connected = (remote != null);
576-
@SuppressWarnings("removal")
577-
SecurityManager sm = System.getSecurityManager();
578-
if (connected || (sm == null)) {
579-
// connected or no security manager
580-
int n = receive(dst, connected);
581-
if (blocking) {
582-
while (IOStatus.okayToRetry(n) && isOpen()) {
583-
park(Net.POLLIN);
584-
n = receive(dst, connected);
585-
}
586-
}
587-
if (n > 0 || (n == 0 && isOpen())) {
588-
// sender address is in socket address buffer
589-
sender = sourceSocketAddress();
573+
int n = receive(dst, connected);
574+
if (blocking) {
575+
while (IOStatus.okayToRetry(n) && isOpen()) {
576+
park(Net.POLLIN);
577+
n = receive(dst, connected);
590578
}
591-
} else {
592-
// security manager and unconnected
593-
sender = untrustedReceive(dst);
579+
}
580+
if (n > 0 || (n == 0 && isOpen())) {
581+
// sender address is in socket address buffer
582+
sender = sourceSocketAddress();
594583
}
595584
return sender;
596585
} finally {
@@ -601,49 +590,6 @@ public SocketAddress receive(ByteBuffer dst) throws IOException {
601590
}
602591
}
603592

604-
/**
605-
* Receives a datagram into an untrusted buffer. When there is a security
606-
* manager set, and the socket is not connected, datagrams have to be received
607-
* into a buffer that is not accessible to the user. The datagram is copied
608-
* into the user's buffer when the sender address is accepted by the security
609-
* manager.
610-
*/
611-
private SocketAddress untrustedReceive(ByteBuffer dst) throws IOException {
612-
@SuppressWarnings("removal")
613-
SecurityManager sm = System.getSecurityManager();
614-
assert readLock.isHeldByCurrentThread()
615-
&& sm != null && remoteAddress == null;
616-
617-
boolean blocking = isBlocking();
618-
for (;;) {
619-
int n;
620-
ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
621-
try {
622-
n = receive(bb, false);
623-
if (n >= 0) {
624-
// sender address is in socket address buffer
625-
InetSocketAddress isa = sourceSocketAddress();
626-
try {
627-
sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
628-
bb.flip();
629-
dst.put(bb);
630-
return isa;
631-
} catch (SecurityException se) {
632-
// ignore datagram
633-
}
634-
}
635-
} finally {
636-
Util.releaseTemporaryDirectBuffer(bb);
637-
}
638-
639-
if (blocking && IOStatus.okayToRetry(n) && isOpen()) {
640-
park(Net.POLLIN);
641-
} else {
642-
return null;
643-
}
644-
}
645-
}
646-
647593
/**
648594
* Receives a datagram.
649595
*
@@ -675,58 +621,30 @@ void blockingReceive(DatagramPacket p, long nanos) throws IOException {
675621
bufLength = DatagramPackets.getBufLength(p);
676622
}
677623

678-
long startNanos = System.nanoTime();
679-
long remainingNanos = nanos;
680-
SocketAddress sender = null;
624+
boolean completed = false;
681625
try {
682626
SocketAddress remote = beginRead(true, false);
683627
boolean connected = (remote != null);
684-
do {
685-
ByteBuffer dst = tryBlockingReceive(connected, bufLength, remainingNanos);
686-
628+
ByteBuffer dst = tryBlockingReceive(connected, bufLength, nanos);
629+
if (dst != null) {
687630
// if datagram received then get sender and copy to DatagramPacket
688-
if (dst != null) {
689-
try {
690-
// sender address is in socket address buffer
691-
sender = sourceSocketAddress();
692-
693-
// check sender when security manager set and not connected
694-
@SuppressWarnings("removal")
695-
SecurityManager sm = System.getSecurityManager();
696-
if (sm != null && !connected) {
697-
InetSocketAddress isa = (InetSocketAddress) sender;
698-
try {
699-
sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
700-
} catch (SecurityException e) {
701-
sender = null;
702-
}
703-
}
704-
705-
if (sender != null) {
706-
// copy bytes to the DatagramPacket, and set length and sender
707-
synchronized (p) {
708-
// re-read p.bufLength in case DatagramPacket changed
709-
int len = Math.min(dst.limit(), DatagramPackets.getBufLength(p));
710-
dst.get(p.getData(), p.getOffset(), len);
711-
DatagramPackets.setLength(p, len);
712-
p.setSocketAddress(sender);
713-
}
714-
} else {
715-
// need to retry, adjusting timeout if needed
716-
if (nanos > 0) {
717-
remainingNanos = nanos - (System.nanoTime() - startNanos);
718-
if (remainingNanos <= 0) {
719-
throw new SocketTimeoutException("Receive timed out");
720-
}
721-
}
722-
}
723-
} finally {
724-
Util.offerFirstTemporaryDirectBuffer(dst);
631+
try {
632+
SocketAddress sender = sourceSocketAddress();
633+
synchronized (p) {
634+
// copy bytes to the DatagramPacket, and set length and sender.
635+
// Need to re-read p.bufLength in case DatagramPacket changed
636+
int len = Math.min(dst.limit(), DatagramPackets.getBufLength(p));
637+
dst.get(p.getData(), p.getOffset(), len);
638+
DatagramPackets.setLength(p, len);
639+
p.setSocketAddress(sender);
725640
}
641+
} finally {
642+
Util.offerFirstTemporaryDirectBuffer(dst);
726643
}
727-
} while (sender == null && isOpen());
644+
completed = true;
645+
}
728646
} finally {
729-
endRead(true, (sender != null));
647+
endRead(true, completed);
730648
}
731649
} finally {
732650
readLock.unlock();
@@ -884,16 +802,7 @@ public int send(ByteBuffer src, SocketAddress target)
884802
completed = (n > 0);
885803
} else {
886804
// not connected
887-
@SuppressWarnings("removal")
888-
SecurityManager sm = System.getSecurityManager();
889805
InetAddress ia = isa.getAddress();
890-
if (sm != null) {
891-
if (ia.isMulticastAddress()) {
892-
sm.checkMulticast(ia);
893-
} else {
894-
sm.checkConnect(ia.getHostAddress(), isa.getPort());
895-
}
896-
}
897806
if (ia.isLinkLocalAddress())
898807
isa = IPAddressUtil.toScopedAddress(isa);
899808
if (isa.getPort() == 0)
@@ -1344,10 +1253,6 @@ private void bindInternal(SocketAddress local) throws IOException {
13441253
} else {
13451254
isa = Net.checkAddress(local, family);
13461255
}
1347-
@SuppressWarnings("removal")
1348-
SecurityManager sm = System.getSecurityManager();
1349-
if (sm != null)
1350-
sm.checkListen(isa.getPort());
13511256

13521257
Net.bind(family, fd, isa.getAddress(), isa.getPort());
13531258
localAddress = Net.localAddress(fd);
@@ -1373,17 +1278,6 @@ public DatagramChannel connect(SocketAddress sa) throws IOException {
13731278
*/
13741279
DatagramChannel connect(SocketAddress sa, boolean check) throws IOException {
13751280
InetSocketAddress isa = Net.checkAddress(sa, family);
1376-
@SuppressWarnings("removal")
1377-
SecurityManager sm = System.getSecurityManager();
1378-
if (sm != null) {
1379-
InetAddress ia = isa.getAddress();
1380-
if (ia.isMulticastAddress()) {
1381-
sm.checkMulticast(ia);
1382-
} else {
1383-
sm.checkConnect(ia.getHostAddress(), isa.getPort());
1384-
sm.checkAccept(ia.getHostAddress(), isa.getPort());
1385-
}
1386-
}
13871281

13881282
readLock.lock();
13891283
try {
@@ -1589,17 +1483,13 @@ private void repairSocket(InetSocketAddress target)
15891483
/**
15901484
* Defines static methods to access AbstractSelectableChannel non-public members.
15911485
*/
1592-
@SuppressWarnings("removal")
15931486
private static class AbstractSelectableChannels {
15941487
private static final Method FOREACH;
15951488
static {
15961489
try {
1597-
PrivilegedExceptionAction<Method> pae = () -> {
1598-
Method m = AbstractSelectableChannel.class.getDeclaredMethod("forEach", Consumer.class);
1599-
m.setAccessible(true);
1600-
return m;
1601-
};
1602-
FOREACH = AccessController.doPrivileged(pae);
1490+
Method m = AbstractSelectableChannel.class.getDeclaredMethod("forEach", Consumer.class);
1491+
m.setAccessible(true);
1492+
FOREACH = m;
16031493
} catch (Exception e) {
16041494
throw new InternalError(e);
16051495
}
@@ -1646,11 +1536,6 @@ private MembershipKey innerJoin(InetAddress group,
16461536
throw new IllegalArgumentException("Source address is different type to group");
16471537
}
16481538

1649-
@SuppressWarnings("removal")
1650-
SecurityManager sm = System.getSecurityManager();
1651-
if (sm != null)
1652-
sm.checkMulticast(group);
1653-
16541539
synchronized (stateLock) {
16551540
ensureOpen();
16561541

@@ -2051,10 +1936,7 @@ private static class DatagramPackets {
20511936
private static final VarHandle BUF_LENGTH;
20521937
static {
20531938
try {
2054-
PrivilegedExceptionAction<MethodHandles.Lookup> pa = () ->
2055-
MethodHandles.privateLookupIn(DatagramPacket.class, MethodHandles.lookup());
2056-
@SuppressWarnings("removal")
2057-
MethodHandles.Lookup l = AccessController.doPrivileged(pa);
1939+
MethodHandles.Lookup l = MethodHandles.privateLookupIn(DatagramPacket.class, MethodHandles.lookup());
20581940
LENGTH = l.findVarHandle(DatagramPacket.class, "length", int.class);
20591941
BUF_LENGTH = l.findVarHandle(DatagramPacket.class, "bufLength", int.class);
20601942
} catch (Exception e) {

src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
import java.nio.channels.ClosedByInterruptException;
4949
import java.nio.channels.DatagramChannel;
5050
import java.nio.channels.MembershipKey;
51-
import java.security.AccessController;
52-
import java.security.PrivilegedAction;
53-
import java.security.PrivilegedExceptionAction;
5451
import java.util.Objects;
5552
import java.util.Set;
5653
import java.util.concurrent.locks.ReentrantLock;
@@ -167,24 +164,11 @@ public SocketAddress getRemoteSocketAddress() {
167164

168165
@Override
169166
public SocketAddress getLocalSocketAddress() {
170-
InetSocketAddress local = dc.localAddress();
171-
if (local == null || isClosed())
167+
if (isClosed()) {
172168
return null;
173-
174-
InetAddress addr = local.getAddress();
175-
if (addr.isAnyLocalAddress())
176-
return local;
177-
178-
@SuppressWarnings("removal")
179-
SecurityManager sm = System.getSecurityManager();
180-
if (sm != null) {
181-
try {
182-
sm.checkConnect(addr.getHostAddress(), -1);
183-
} catch (SecurityException x) {
184-
return new InetSocketAddress(local.getPort());
185-
}
169+
} else {
170+
return dc.localAddress();
186171
}
187-
return local;
188172
}
189173

190174
@Override
@@ -223,17 +207,7 @@ public InetAddress getLocalAddress() {
223207
InetSocketAddress local = dc.localAddress();
224208
if (local == null)
225209
local = new InetSocketAddress(0);
226-
InetAddress result = local.getAddress();
227-
@SuppressWarnings("removal")
228-
SecurityManager sm = System.getSecurityManager();
229-
if (sm != null) {
230-
try {
231-
sm.checkConnect(result.getHostAddress(), -1);
232-
} catch (SecurityException x) {
233-
return new InetSocketAddress(0).getAddress();
234-
}
235-
}
236-
return result;
210+
return local.getAddress();
237211
}
238212

239213
@Override
@@ -484,11 +458,6 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IO
484458
synchronized (this) {
485459
MembershipKey key = dc.findMembership(group, ni);
486460
if (key != null) {
487-
// already a member but need to check permission anyway
488-
@SuppressWarnings("removal")
489-
SecurityManager sm = System.getSecurityManager();
490-
if (sm != null)
491-
sm.checkMulticast(group);
492461
throw new SocketException("Already a member of group");
493462
}
494463
dc.join(group, ni); // checks permission
@@ -501,10 +470,6 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws I
501470
NetworkInterface ni = (netIf != null) ? netIf : defaultNetworkInterface();
502471
if (isClosed())
503472
throw new SocketException("Socket is closed");
504-
@SuppressWarnings("removal")
505-
SecurityManager sm = System.getSecurityManager();
506-
if (sm != null)
507-
sm.checkMulticast(group);
508473
synchronized (this) {
509474
MembershipKey key = dc.findMembership(group, ni);
510475
if (key == null)
@@ -541,12 +506,7 @@ public InetAddress getInterface() throws SocketException {
541506
return outgoingInetAddress;
542507
} else {
543508
// network interface has changed so update cached values
544-
PrivilegedAction<InetAddress> pa;
545-
pa = () -> ni.inetAddresses().findFirst().orElse(null);
546-
@SuppressWarnings("removal")
547-
InetAddress ia = AccessController.doPrivileged(pa);
548-
if (ia == null)
549-
throw new SocketException("Network interface has no IP address");
509+
InetAddress ia = ni.inetAddresses().findFirst().orElse(null);
550510
outgoingNetworkInterface = ni;
551511
outgoingInetAddress = ia;
552512
return ia;
@@ -660,10 +620,7 @@ private static class NetworkInterfaces {
660620
static final MethodHandle CONSTRUCTOR;
661621
static {
662622
try {
663-
PrivilegedExceptionAction<Lookup> pa = () ->
664-
MethodHandles.privateLookupIn(NetworkInterface.class, MethodHandles.lookup());
665-
@SuppressWarnings("removal")
666-
MethodHandles.Lookup l = AccessController.doPrivileged(pa);
623+
Lookup l = MethodHandles.privateLookupIn(NetworkInterface.class, MethodHandles.lookup());
667624
MethodType methodType = MethodType.methodType(NetworkInterface.class);
668625
GET_DEFAULT = l.findStatic(NetworkInterface.class, "getDefault", methodType);
669626
methodType = MethodType.methodType(void.class, String.class, int.class, InetAddress[].class);
@@ -703,10 +660,7 @@ private static class DatagramSockets {
703660
private static final SocketAddress NO_DELEGATE;
704661
static {
705662
try {
706-
PrivilegedExceptionAction<Lookup> pa = () ->
707-
MethodHandles.privateLookupIn(DatagramSocket.class, MethodHandles.lookup());
708-
@SuppressWarnings("removal")
709-
MethodHandles.Lookup l = AccessController.doPrivileged(pa);
663+
Lookup l = MethodHandles.privateLookupIn(DatagramSocket.class, MethodHandles.lookup());
710664
var handle = l.findStaticVarHandle(DatagramSocket.class, "NO_DELEGATE", SocketAddress.class);
711665
NO_DELEGATE = (SocketAddress) handle.get();
712666
} catch (Exception e) {

0 commit comments

Comments
 (0)