Path: blob/master/test/jdk/java/lang/SecurityManager/CheckAccessClassInPackagePermissions.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805520626* @summary Check that each module loaded by the platform loader has the27* proper "accessClassInPackage" RuntimePermissions to access its28* qualified exports.29* @run main CheckAccessClassInPackagePermissions30*/3132import java.lang.module.ModuleDescriptor;33import java.lang.module.ModuleDescriptor.Exports;34import java.net.URL;35import java.security.CodeSigner;36import java.security.CodeSource;37import java.security.Policy;38import java.security.ProtectionDomain;39import java.util.ArrayList;40import java.util.Arrays;41import java.util.HashMap;42import java.util.List;43import java.util.Map;44import java.util.Map.Entry;45import java.util.Optional;46import java.util.Set;47import java.util.stream.Collectors;48import java.util.stream.Stream;4950public class CheckAccessClassInPackagePermissions {5152private static final String[] deployModules = {53"jdk.javaws", "jdk.plugin", "jdk.plugin.server", "jdk.deploy" };5455public static void main(String[] args) throws Exception {5657// Get the modules in the boot layer loaded by the boot or platform58// loader59ModuleLayer bootLayer = ModuleLayer.boot();60Set<Module> modules = bootLayer.modules()61.stream()62.filter(CheckAccessClassInPackagePermissions::isBootOrPlatformMod)63.collect(Collectors.toSet());6465// Create map of target module's qualified export packages66Map<String, List<String>> map = new HashMap<>();67Set<Exports> qualExports =68modules.stream()69.map(Module::getDescriptor)70.map(ModuleDescriptor::exports)71.flatMap(Set::stream)72.filter(Exports::isQualified)73.collect(Collectors.toSet());74for (Exports e : qualExports) {75Set<String> targets = e.targets();76for (String t : targets) {77map.compute(t, (k, ov) -> {78if (ov == null) {79List<String> v = new ArrayList<>();80v.add(e.source());81return v;82} else {83ov.add(e.source());84return ov;85}86});87}88}8990// Check if each target module has the right permissions to access91// its qualified exports92Policy policy = Policy.getPolicy();93List<String> deployMods = Arrays.asList(deployModules);94for (Map.Entry<String, List<String>> me : map.entrySet()) {95String moduleName = me.getKey();9697// skip deploy modules since they are granted permissions in98// deployment policy file99if (deployMods.contains(moduleName)) {100continue;101}102103// is this a module loaded by the platform loader?104Optional<Module> module = bootLayer.findModule(moduleName);105if (!module.isPresent()) {106continue;107}108Module mod = module.get();109if (mod.getClassLoader() != ClassLoader.getPlatformClassLoader()) {110continue;111}112113// create ProtectionDomain simulating module114URL url = new URL("jrt:/" + moduleName);115CodeSource cs = new CodeSource(url, (CodeSigner[])null);116ProtectionDomain pd = new ProtectionDomain(cs, null, null, null);117118List<String> pkgs = me.getValue();119for (String p : pkgs) {120RuntimePermission rp =121new RuntimePermission("accessClassInPackage." + p);122if (!policy.implies(pd, rp)) {123throw new Exception("Module " + mod + " has not been " +124"granted " + rp);125}126}127}128}129130/**131* Returns true if the module's loader is the boot or platform loader.132*/133private static boolean isBootOrPlatformMod(Module m) {134return m.getClassLoader() == null ||135m.getClassLoader() == ClassLoader.getPlatformClassLoader();136}137}138139140