8218618: Program fails when using JDK addressed by UNC path and using Security Manager
authormullan
Thu, 07 Mar 2019 14:29:43 -0500
changeset 54024 e445608fce9b
parent 54023 a764c49570c6
child 54025 f0af7fd0c9ca
8218618: Program fails when using JDK addressed by UNC path and using Security Manager Reviewed-by: weijun
src/java.base/share/classes/sun/security/provider/PolicyFile.java
--- a/src/java.base/share/classes/sun/security/provider/PolicyFile.java	Thu Mar 07 10:18:23 2019 -0800
+++ b/src/java.base/share/classes/sun/security/provider/PolicyFile.java	Thu Mar 07 14:29:43 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URI;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.*;
 import java.security.*;
@@ -37,7 +38,6 @@
 import java.security.cert.X509Certificate;
 import javax.security.auth.Subject;
 import javax.security.auth.x500.X500Principal;
-import java.io.FilePermission;
 import java.net.SocketPermission;
 import java.net.NetPermission;
 import java.util.concurrent.ConcurrentHashMap;
@@ -273,23 +273,6 @@
     private static Set<URL> badPolicyURLs =
         Collections.newSetFromMap(new ConcurrentHashMap<URL,Boolean>());
 
-    // The default.policy file
-    private static final URL DEFAULT_POLICY_URL =
-        AccessController.doPrivileged(new PrivilegedAction<>() {
-            @Override
-            public URL run() {
-                String sep = File.separator;
-                try {
-                    return Path.of(StaticProperty.javaHome(),
-                                     "lib", "security",
-                                     "default.policy").toUri().toURL();
-                } catch (MalformedURLException mue) {
-                    // should not happen
-                    throw new Error("Malformed default.policy URL: " + mue);
-                }
-            }
-        });
-
     /**
      * Initializes the Policy object and reads the default policy
      * configuration file(s) into the Policy object.
@@ -349,13 +332,10 @@
     private void initPolicyFile(final PolicyInfo newInfo, final URL url) {
 
         // always load default.policy
-        if (debug != null) {
-            debug.println("reading " + DEFAULT_POLICY_URL);
-        }
         AccessController.doPrivileged(new PrivilegedAction<>() {
             @Override
             public Void run() {
-                init(DEFAULT_POLICY_URL, newInfo, true);
+                initDefaultPolicy(newInfo);
                 return null;
             }
         });
@@ -373,7 +353,7 @@
             AccessController.doPrivileged(new PrivilegedAction<>() {
                 @Override
                 public Void run() {
-                    if (init(url, newInfo, false) == false) {
+                    if (init(url, newInfo) == false) {
                         // use static policy if all else fails
                         initStaticPolicy(newInfo);
                     }
@@ -429,7 +409,7 @@
                             if (debug != null) {
                                 debug.println("reading "+policyURL);
                             }
-                            if (init(policyURL, newInfo, false)) {
+                            if (init(policyURL, newInfo)) {
                                 loaded_policy = true;
                             }
                         } catch (Exception e) {
@@ -472,7 +452,7 @@
                         if (debug != null) {
                             debug.println("reading " + policy_url);
                         }
-                        if (init(policy_url, newInfo, false)) {
+                        if (init(policy_url, newInfo)) {
                             loaded_policy = true;
                         }
                     } catch (Exception e) {
@@ -492,11 +472,34 @@
         return loadedPolicy;
     }
 
+    private void initDefaultPolicy(PolicyInfo newInfo) {
+        Path defaultPolicy = Path.of(StaticProperty.javaHome(),
+                                     "lib",
+                                     "security",
+                                     "default.policy");
+        if (debug != null) {
+            debug.println("reading " + defaultPolicy);
+        }
+        try (BufferedReader br = Files.newBufferedReader(defaultPolicy)) {
+
+            PolicyParser pp = new PolicyParser(expandProperties);
+            pp.read(br);
+
+            Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
+            while (enum_.hasMoreElements()) {
+                PolicyParser.GrantEntry ge = enum_.nextElement();
+                addGrantEntry(ge, null, newInfo);
+            }
+        } catch (Exception e) {
+            throw new InternalError("Failed to load default.policy", e);
+        }
+    }
+
     /**
      * Reads a policy configuration into the Policy object using a
      * Reader object.
      */
-    private boolean init(URL policy, PolicyInfo newInfo, boolean defPolicy) {
+    private boolean init(URL policy, PolicyInfo newInfo) {
 
         // skip parsing policy file if it has been previously parsed and
         // has syntax errors
@@ -537,9 +540,6 @@
             }
             return true;
         } catch (PolicyParser.ParsingException pe) {
-            if (defPolicy) {
-                throw new InternalError("Failed to load default.policy", pe);
-            }
             // record bad policy file to avoid later reparsing it
             badPolicyURLs.add(policy);
             Object[] source = {policy, pe.getNonlocalizedMessage()};
@@ -549,9 +549,6 @@
                 pe.printStackTrace();
             }
         } catch (Exception e) {
-            if (defPolicy) {
-                throw new InternalError("Failed to load default.policy", e);
-            }
             if (debug != null) {
                 debug.println("error parsing "+policy);
                 debug.println(e.toString());