Privileged block to create socket input/output streams should be in AbstractPlainSocketImpl niosocketimpl-branch
authoralanb
Sat, 09 Feb 2019 08:54:02 +0000
branchniosocketimpl-branch
changeset 57170 52bc64277201
parent 57169 1d423d38840d
child 57171 d8ed7335dadd
Privileged block to create socket input/output streams should be in AbstractPlainSocketImpl
src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
src/java.base/share/classes/java/net/Socket.java
--- a/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sat Feb 09 07:56:40 2019 +0000
+++ b/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sat Feb 09 08:54:02 2019 +0000
@@ -30,6 +30,9 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
@@ -470,8 +473,14 @@
                 throw new IOException("Socket Closed");
             if (shut_rd)
                 throw new IOException("Socket input is shutdown");
-            if (socketInputStream == null)
-                socketInputStream = new SocketInputStream(this);
+            if (socketInputStream == null) {
+                PrivilegedExceptionAction<SocketInputStream> pa = () -> new SocketInputStream(this);
+                try {
+                    socketInputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketInputStream;
     }
@@ -489,8 +498,14 @@
                 throw new IOException("Socket Closed");
             if (shut_wr)
                 throw new IOException("Socket output is shutdown");
-            if (socketOutputStream == null)
-                socketOutputStream = new SocketOutputStream(this);
+            if (socketOutputStream == null) {
+                PrivilegedExceptionAction<SocketOutputStream> pa = () -> new SocketOutputStream(this);
+                try {
+                    socketOutputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketOutputStream;
     }
--- a/src/java.base/share/classes/java/net/Socket.java	Sat Feb 09 07:56:40 2019 +0000
+++ b/src/java.base/share/classes/java/net/Socket.java	Sat Feb 09 08:54:02 2019 +0000
@@ -27,15 +27,13 @@
 
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.FileDescriptor;
 import java.io.IOException;
 import java.nio.channels.SocketChannel;
 import java.security.AccessController;
-import java.security.PrivilegedExceptionAction;
 import java.security.PrivilegedAction;
 import java.util.Set;
 import java.util.Collections;
-import sun.nio.ch.NioSocketImpl;
+
 import static java.net.SocketImpl.getDefaultSocketImpl;
 
 /**
@@ -142,7 +140,7 @@
             }
             impl = type == Proxy.Type.SOCKS 
                 ? new SocksSocketImpl(p, getDefaultSocketImpl(false))
-	        : new HttpConnectSocketImpl(p, getDefaultSocketImpl(false));
+	            : new HttpConnectSocketImpl(p, getDefaultSocketImpl(false));
             impl.setSocket(this);
         } else {
             if (p == Proxy.NO_PROXY) {
@@ -925,19 +923,7 @@
         if (isInputShutdown())
             throw new SocketException("Socket input is shutdown");
         // wrap the input stream so that the close method closes this socket
-        InputStream is = null;
-        try {
-            is = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<InputStream>() {
-                    public InputStream run() throws IOException {
-                        return impl.getInputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
-        }
-
-        return new SocketInputStream(this, is);
+        return new SocketInputStream(this, impl.getInputStream());
     }
 
     private static class SocketInputStream extends InputStream {
@@ -993,18 +979,7 @@
         if (isOutputShutdown())
             throw new SocketException("Socket output is shutdown");
         // wrap the output stream so that the close method closes this socket
-        OutputStream os = null;
-        try {
-            os = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<OutputStream>() {
-                    public OutputStream run() throws IOException {
-                        return impl.getOutputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
-        }
-        return new SocketOutputStream(this, os);
+        return new SocketOutputStream(this, impl.getOutputStream());
     }
 
     private static class SocketOutputStream extends OutputStream {