changeset 102:ef756a3c97d0

8071772: Unexpected SecurityException Summary: doPrivileged for critical code Reviewed-by: alkonsta
author snazarki
date Wed, 11 Feb 2015 12:17:04 +0300
parents a4559d391a7d
children a4ed486f64cc
files src/se/classes/com/oracle/dio/registry/RegistryImpl.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java
diffstat 2 files changed, 31 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/registry/RegistryImpl.java	Fri Feb 06 12:29:19 2015 +0300
+++ b/src/se/classes/com/oracle/dio/registry/RegistryImpl.java	Wed Feb 11 12:17:04 2015 +0300
@@ -32,6 +32,10 @@
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.lang.ref.WeakReference;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
+import java.security.PrivilegedActionException;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.Iterator;
@@ -142,14 +146,20 @@
         }
         if (registry == null) {
             registry = new Properties();
-            try {
-                String path = Configuration.getSystemProperty(REGISTRY_FILE_PATH);
-                if (path != null) {
-                    registry.load(new InputStreamReader(new FileInputStream(path), DEFAULT_CHARSET_NAME));
-                }
-            } catch (IOException | SecurityException | IllegalArgumentException e) {
-                registry.clear();
-            }
+            final Properties reg = registry;
+            final String path = Configuration.getSystemProperty(REGISTRY_FILE_PATH);
+            AccessController.doPrivileged(new PrivilegedAction() {
+                    public Object run() {
+                        try {
+                            if (path != null) {
+                                reg.load(new InputStreamReader(new FileInputStream(path), DEFAULT_CHARSET_NAME));
+                            }
+                        } catch (IOException | SecurityException | IllegalArgumentException e) {
+                            reg.clear();
+                        }
+                        return null;
+                    }
+            });
             registryCache = new WeakReference(registry);
         }
         return registry;
@@ -160,7 +170,17 @@
         if (path == null) {
             throw new IOException("Registry is not available");
         }
-        registry.store(new OutputStreamWriter(new FileOutputStream(path), DEFAULT_CHARSET_NAME), null);
+        final Properties reg = registry;
+        try {
+            AccessController.doPrivileged(new PrivilegedExceptionAction(){
+                    public Object run() throws IOException {
+                        reg.store(new OutputStreamWriter(new FileOutputStream(path), DEFAULT_CHARSET_NAME), null);
+                        return null;
+                    }
+                });
+       } catch(PrivilegedActionException e) {
+            throw (IOException)e.getException();
+       }
     }
 
     private RegistryContent readRegistryContent(Properties registry) {
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Fri Feb 06 12:29:19 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Wed Feb 11 12:17:04 2015 +0300
@@ -181,7 +181,7 @@
         return false;
     }
 
-    private String getSecurityName(){
+    private String getSecurityName() {
         UARTConfig cfg = dscr.getConfiguration();
         int devNum = cfg.getControllerNumber();
         String securityName = null;
@@ -206,6 +206,7 @@
             if (ports == null) {
                 ports = Configuration.getProperty("jdk.dio.uart.ports");
             }
+
             if (null != ports) {
                 StringTokenizer t = new StringTokenizer(ports, ",");
                 while(devNum-- > 0 && t.hasMoreTokens()) {