changeset 6953:2bce3c0856af

8028453: AsynchronousSocketChannel.connect() requires SocketPermission due to bind to local address (win) Reviewed-by: alanb, chegar
author michaelm
date Wed, 20 Nov 2013 15:28:54 +0000
parents e6160aedadd5
children 33ae35eeb5a4
files src/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java
diffstat 1 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java	Fri Nov 01 10:40:56 2013 +0000
+++ b/src/windows/classes/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.java	Wed Nov 20 15:28:54 2013 +0000
@@ -31,6 +31,9 @@
 import java.net.*;
 import java.util.concurrent.*;
 import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import sun.misc.Unsafe;
 
 /**
@@ -300,6 +303,19 @@
         }
     }
 
+    private void doPrivilegedBind(final SocketAddress sa) throws IOException {
+        try {
+            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
+                public Void run() throws IOException {
+                    bind(sa);
+                    return null;
+                }
+            });
+        } catch (PrivilegedActionException e) {
+            throw (IOException) e.getException();
+        }
+    }
+
     @Override
     <A> Future<Void> implConnect(SocketAddress remote,
                                  A attachment,
@@ -330,7 +346,12 @@
                 throw new ConnectionPendingException();
             if (localAddress == null) {
                 try {
-                    bind(new InetSocketAddress(0));
+                    SocketAddress any = new InetSocketAddress(0);
+                    if (sm == null) {
+                        bind(any);
+                    } else {
+                        doPrivilegedBind(any);
+                    }
                 } catch (IOException x) {
                     bindException = x;
                 }