1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *  @test
  26  *  @bug 6741606 7146431 8000450 8019830 8022945 8027144 8041633 8179423
  27  *       8186080
  28  *  @summary Make sure all restricted packages listed in the package.access
  29  *           property in the java.security file are blocked
  30  *  @run main/othervm CheckPackageAccess
  31  */
  32 
  33 import java.security.Security;
  34 import java.util.Collections;
  35 import java.util.Arrays;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.StringTokenizer;
  39 
  40 /*
  41  * The main benefit of this test is to catch merge errors or other types
  42  * of issues where one or more of the packages are accidentally
  43  * removed. This is why the packages that are known to be restricted have to
  44  * be explicitly listed below.
  45  */
  46 public class CheckPackageAccess {
  47 
  48     /*
  49      * This array should be updated whenever new packages are added to the
  50      * package.access property in the java.security file
  51      * NOTE: it should be in the same order as the java.security file
  52      */
  53     private static final String[] packages = {
  54         "sun.",
  55         "com.sun.xml.internal.",
  56         "com.sun.imageio.",
  57         "com.sun.istack.internal.",
  58         "com.sun.jmx.",
  59         "com.sun.media.sound.",
  60         "com.sun.naming.internal.",
  61         "com.sun.proxy.",
  62         "com.sun.corba.se.",
  63         "com.sun.org.apache.bcel.internal.",
  64         "com.sun.org.apache.regexp.internal.",
  65         "com.sun.org.apache.xerces.internal.",
  66         "com.sun.org.apache.xpath.internal.",
  67         "com.sun.org.apache.xalan.internal.extensions.",
  68         "com.sun.org.apache.xalan.internal.lib.",
  69         "com.sun.org.apache.xalan.internal.res.",
  70         "com.sun.org.apache.xalan.internal.templates.",
  71         "com.sun.org.apache.xalan.internal.utils.",
  72         "com.sun.org.apache.xalan.internal.xslt.",
  73         "com.sun.org.apache.xalan.internal.xsltc.cmdline.",
  74         "com.sun.org.apache.xalan.internal.xsltc.compiler.",
  75         "com.sun.org.apache.xalan.internal.xsltc.trax.",
  76         "com.sun.org.apache.xalan.internal.xsltc.util.",
  77         "com.sun.org.apache.xml.internal.res.",
  78         "com.sun.org.apache.xml.internal.resolver.helpers.",
  79         "com.sun.org.apache.xml.internal.resolver.readers.",
  80         "com.sun.org.apache.xml.internal.security.",
  81         "com.sun.org.apache.xml.internal.serializer.utils.",
  82         "com.sun.org.apache.xml.internal.utils.",
  83         "com.sun.org.glassfish.",
  84         "com.oracle.xmlns.internal.",
  85         "com.oracle.webservices.internal.",
  86         "oracle.jrockit.jfr.",
  87         "org.jcp.xml.dsig.internal.",
  88         "jdk.internal.",
  89         "jdk.jfr.events.",
  90         "jdk.jfr.internal.",
  91         "jdk.management.jfr.internal.",
  92         "jdk.nashorn.internal.",
  93         "jdk.nashorn.tools.",
  94         "jdk.xml.internal.",
  95         "com.sun.activation.registries."
  96     };
  97 
  98     public static void main(String[] args) throws Exception {
  99         List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
 100         String osName = System.getProperty("os.name");
 101         if (osName.contains("OS X")) {
 102             pkgs.add("apple.");  // add apple package for OS X
 103         } else if (osName.startsWith("Windows")) {
 104             pkgs.add("com.sun.java.accessibility.");
 105         }
 106 
 107         List<String> jspkgs =
 108             getPackages(Security.getProperty("package.access"));
 109 
 110         if (!isOpenJDKOnly()) {
 111             String lastPkg = pkgs.get(pkgs.size() - 1);
 112 
 113             // Remove any closed packages from list before comparing
 114             int index = jspkgs.indexOf(lastPkg);
 115             if (index != -1 && index != jspkgs.size() - 1) {
 116                 jspkgs.subList(index + 1, jspkgs.size()).clear();
 117             }
 118         }
 119 
 120         // Sort to ensure lists are comparable
 121         Collections.sort(pkgs);
 122         Collections.sort(jspkgs);
 123 
 124         if (!pkgs.equals(jspkgs)) {
 125             for (String p : pkgs)
 126                 if (!jspkgs.contains(p))
 127                     System.out.println("In golden set, but not in j.s file: " + p);
 128             for (String p : jspkgs)
 129                 if (!pkgs.contains(p))
 130                     System.out.println("In j.s file, but not in golden set: " + p);
 131 
 132 
 133             throw new RuntimeException("restricted packages are not " +
 134                                        "consistent with java.security file");
 135         }
 136         System.setSecurityManager(new SecurityManager());
 137         SecurityManager sm = System.getSecurityManager();
 138         for (String pkg : packages) {
 139             String subpkg = pkg + "foo";
 140             try {
 141                 sm.checkPackageAccess(pkg);
 142                 throw new RuntimeException("Able to access " + pkg +
 143                                            " package");
 144             } catch (SecurityException se) { }
 145             try {
 146                 sm.checkPackageAccess(subpkg);
 147                 throw new RuntimeException("Able to access " + subpkg +
 148                                            " package");
 149             } catch (SecurityException se) { }
 150             try {
 151                 sm.checkPackageDefinition(pkg);
 152                 throw new RuntimeException("Able to define class in " + pkg +
 153                                            " package");
 154             } catch (SecurityException se) { }
 155             try {
 156                 sm.checkPackageDefinition(subpkg);
 157                 throw new RuntimeException("Able to define class in " + subpkg +
 158                                            " package");
 159             } catch (SecurityException se) { }
 160         }
 161         System.out.println("Test passed");
 162     }
 163 
 164     private static List<String> getPackages(String p) {
 165         List<String> packages = new ArrayList<>();
 166         if (p != null && !p.equals("")) {
 167             StringTokenizer tok = new StringTokenizer(p, ",");
 168             while (tok.hasMoreElements()) {
 169                 String s = tok.nextToken().trim();
 170                 packages.add(s);
 171             }
 172         }
 173         return packages;
 174     }
 175 
 176     private static boolean isOpenJDKOnly() {
 177         String prop = System.getProperty("java.runtime.name");
 178         return prop != null && prop.startsWith("OpenJDK");
 179     }
 180 }