< prev index next >

src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java

Print this page
rev 55994 : 8229773: Resolve permissions for code source URLs lazily
Reviewed-by: alanb, mullan, rriggs, dfuchs


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.loader;
  27 
  28 import java.io.File;
  29 import java.io.FilePermission;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.lang.module.ModuleDescriptor;
  33 import java.lang.module.ModuleReference;
  34 import java.lang.module.ModuleReader;
  35 import java.lang.ref.SoftReference;
  36 import java.net.MalformedURLException;
  37 import java.net.URI;
  38 import java.net.URL;
  39 import java.nio.ByteBuffer;
  40 import java.security.AccessController;
  41 import java.security.CodeSigner;
  42 import java.security.CodeSource;
  43 import java.security.Permission;
  44 import java.security.PermissionCollection;
  45 import java.security.PrivilegedAction;
  46 import java.security.PrivilegedActionException;
  47 import java.security.PrivilegedExceptionAction;
  48 import java.security.SecureClassLoader;
  49 import java.util.ArrayList;
  50 import java.util.Collections;
  51 import java.util.Enumeration;
  52 import java.util.Iterator;
  53 import java.util.List;
  54 import java.util.Map;
  55 import java.util.NoSuchElementException;
  56 import java.util.Optional;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Function;
  59 import java.util.jar.Attributes;
  60 import java.util.jar.Manifest;
  61 import java.util.stream.Stream;
  62 
  63 import jdk.internal.access.SharedSecrets;
  64 import jdk.internal.misc.VM;
  65 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
  66 import jdk.internal.module.Resources;
  67 import jdk.internal.vm.annotation.Stable;

  68 
  69 
  70 /**
  71  * The platform or application class loader. Resources loaded from modules
  72  * defined to the boot class loader are also loaded via an instance of this
  73  * ClassLoader type.
  74  *
  75  * <p> This ClassLoader supports loading of classes and resources from modules.
  76  * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
  77  * method. Defining a module to this ClassLoader has the effect of making the
  78  * types in the module visible. </p>
  79  *
  80  * <p> This ClassLoader also supports loading of classes and resources from a
  81  * class path of URLs that are specified to the ClassLoader at construction
  82  * time. The class path may expand at runtime (the Class-Path attribute in JAR
  83  * files or via instrumentation agents). </p>
  84  *
  85  * <p> The delegation model used by this ClassLoader differs to the regular
  86  * delegation model. When requested to load a class then this ClassLoader first
  87  * maps the class name to its package name. If there is a module defined to a


 962     // -- permissions
 963 
 964     /**
 965      * Returns the permissions for the given CodeSource.
 966      */
 967     @Override
 968     protected PermissionCollection getPermissions(CodeSource cs) {
 969         PermissionCollection perms = super.getPermissions(cs);
 970 
 971         // add the permission to access the resource
 972         URL url = cs.getLocation();
 973         if (url == null)
 974             return perms;
 975 
 976         // avoid opening connection when URL is to resource in run-time image
 977         if (url.getProtocol().equals("jrt")) {
 978             perms.add(new RuntimePermission("accessSystemModules"));
 979             return perms;
 980         }
 981 
 982         // open connection to determine the permission needed
 983         try {
 984             Permission p = url.openConnection().getPermission();
 985             if (p != null) {
 986                 // for directories then need recursive access
 987                 if (p instanceof FilePermission) {
 988                     String path = p.getName();
 989                     if (path.endsWith(File.separator)) {
 990                         path += "-";
 991                         p = new FilePermission(path, "read");
 992                     }
 993                 }
 994                 perms.add(p);
 995             }
 996         } catch (IOException ioe) { }
 997 
 998         return perms;
 999     }
1000 
1001 
1002     // -- miscellaneous supporting methods
1003 
1004     /**
1005      * Returns the ModuleReader for the given module, creating it if needed.
1006      */
1007     private ModuleReader moduleReaderFor(ModuleReference mref) {
1008         ModuleReader reader = moduleToReader.get(mref);
1009         if (reader == null) {
1010             // avoid method reference during startup
1011             Function<ModuleReference, ModuleReader> create = new Function<>() {
1012                 public ModuleReader apply(ModuleReference moduleReference) {
1013                     try {
1014                         return mref.open();
1015                     } catch (IOException e) {
1016                         // Return a null module reader to avoid a future class
1017                         // load attempting to open the module again.
1018                         return new NullModuleReader();
1019                     }
1020                 }




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.loader;
  27 


  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.lang.module.ModuleDescriptor;
  31 import java.lang.module.ModuleReference;
  32 import java.lang.module.ModuleReader;
  33 import java.lang.ref.SoftReference;
  34 import java.net.MalformedURLException;
  35 import java.net.URI;
  36 import java.net.URL;
  37 import java.nio.ByteBuffer;
  38 import java.security.AccessController;
  39 import java.security.CodeSigner;
  40 import java.security.CodeSource;

  41 import java.security.PermissionCollection;
  42 import java.security.PrivilegedAction;
  43 import java.security.PrivilegedActionException;
  44 import java.security.PrivilegedExceptionAction;
  45 import java.security.SecureClassLoader;
  46 import java.util.ArrayList;
  47 import java.util.Collections;
  48 import java.util.Enumeration;
  49 import java.util.Iterator;
  50 import java.util.List;
  51 import java.util.Map;
  52 import java.util.NoSuchElementException;
  53 import java.util.Optional;
  54 import java.util.concurrent.ConcurrentHashMap;
  55 import java.util.function.Function;
  56 import java.util.jar.Attributes;
  57 import java.util.jar.Manifest;
  58 import java.util.stream.Stream;
  59 
  60 import jdk.internal.access.SharedSecrets;
  61 import jdk.internal.misc.VM;
  62 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
  63 import jdk.internal.module.Resources;
  64 import jdk.internal.vm.annotation.Stable;
  65 import sun.security.util.LazyCodeSourcePermissionCollection;
  66 
  67 
  68 /**
  69  * The platform or application class loader. Resources loaded from modules
  70  * defined to the boot class loader are also loaded via an instance of this
  71  * ClassLoader type.
  72  *
  73  * <p> This ClassLoader supports loading of classes and resources from modules.
  74  * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
  75  * method. Defining a module to this ClassLoader has the effect of making the
  76  * types in the module visible. </p>
  77  *
  78  * <p> This ClassLoader also supports loading of classes and resources from a
  79  * class path of URLs that are specified to the ClassLoader at construction
  80  * time. The class path may expand at runtime (the Class-Path attribute in JAR
  81  * files or via instrumentation agents). </p>
  82  *
  83  * <p> The delegation model used by this ClassLoader differs to the regular
  84  * delegation model. When requested to load a class then this ClassLoader first
  85  * maps the class name to its package name. If there is a module defined to a


 960     // -- permissions
 961 
 962     /**
 963      * Returns the permissions for the given CodeSource.
 964      */
 965     @Override
 966     protected PermissionCollection getPermissions(CodeSource cs) {
 967         PermissionCollection perms = super.getPermissions(cs);
 968 
 969         // add the permission to access the resource
 970         URL url = cs.getLocation();
 971         if (url == null)
 972             return perms;
 973 
 974         // avoid opening connection when URL is to resource in run-time image
 975         if (url.getProtocol().equals("jrt")) {
 976             perms.add(new RuntimePermission("accessSystemModules"));
 977             return perms;
 978         }
 979 
 980         return new LazyCodeSourcePermissionCollection(perms, cs);










 981     }







 982 
 983     // -- miscellaneous supporting methods
 984 
 985     /**
 986      * Returns the ModuleReader for the given module, creating it if needed.
 987      */
 988     private ModuleReader moduleReaderFor(ModuleReference mref) {
 989         ModuleReader reader = moduleToReader.get(mref);
 990         if (reader == null) {
 991             // avoid method reference during startup
 992             Function<ModuleReference, ModuleReader> create = new Function<>() {
 993                 public ModuleReader apply(ModuleReference moduleReference) {
 994                     try {
 995                         return mref.open();
 996                     } catch (IOException e) {
 997                         // Return a null module reader to avoid a future class
 998                         // load attempting to open the module again.
 999                         return new NullModuleReader();
1000                     }
1001                 }


< prev index next >