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.nashorn.internal.",
  90         "jdk.nashorn.tools.",
  91         "jdk.xml.internal.",
  92         "com.sun.activation.registries."
  93     };
  94 
  95     public static void main(String[] args) throws Exception {
  96         List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
  97         String osName = System.getProperty("os.name");
  98         if (osName.contains("OS X")) {
  99             pkgs.add("apple.");  // add apple package for OS X
 100         } else if (osName.startsWith("Windows")) {
 101             pkgs.add("com.sun.java.accessibility.");
 102         }
 103 
 104         List<String> jspkgs =
 105             getPackages(Security.getProperty("package.access"));
 106 
 107         if (!isOpenJDKOnly()) {
 108             String lastPkg = pkgs.get(pkgs.size() - 1);
 109 
 110             // Remove any closed packages from list before comparing
 111             int index = jspkgs.indexOf(lastPkg);
 112             if (index != -1 && index != jspkgs.size() - 1) {
 113                 jspkgs.subList(index + 1, jspkgs.size()).clear();
 114             }
 115         }
 116 
 117         // Sort to ensure lists are comparable
 118         Collections.sort(pkgs);
 119         Collections.sort(jspkgs);
 120 
 121         if (!pkgs.equals(jspkgs)) {
 122             for (String p : pkgs)
 123                 if (!jspkgs.contains(p))
 124                     System.out.println("In golden set, but not in j.s file: " + p);
 125             for (String p : jspkgs)
 126                 if (!pkgs.contains(p))
 127                     System.out.println("In j.s file, but not in golden set: " + p);
 128 
 129 
 130             throw new RuntimeException("restricted packages are not " +
 131                                        "consistent with java.security file");
 132         }
 133         System.setSecurityManager(new SecurityManager());
 134         SecurityManager sm = System.getSecurityManager();
 135         for (String pkg : packages) {
 136             String subpkg = pkg + "foo";
 137             try {
 138                 sm.checkPackageAccess(pkg);
 139                 throw new RuntimeException("Able to access " + pkg +
 140                                            " package");
 141             } catch (SecurityException se) { }
 142             try {
 143                 sm.checkPackageAccess(subpkg);
 144                 throw new RuntimeException("Able to access " + subpkg +
 145                                            " package");
 146             } catch (SecurityException se) { }
 147             try {
 148                 sm.checkPackageDefinition(pkg);
 149                 throw new RuntimeException("Able to define class in " + pkg +
 150                                            " package");
 151             } catch (SecurityException se) { }
 152             try {
 153                 sm.checkPackageDefinition(subpkg);
 154                 throw new RuntimeException("Able to define class in " + subpkg +
 155                                            " package");
 156             } catch (SecurityException se) { }
 157         }
 158         System.out.println("Test passed");
 159     }
 160 
 161     private static List<String> getPackages(String p) {
 162         List<String> packages = new ArrayList<>();
 163         if (p != null && !p.equals("")) {
 164             StringTokenizer tok = new StringTokenizer(p, ",");
 165             while (tok.hasMoreElements()) {
 166                 String s = tok.nextToken().trim();
 167                 packages.add(s);
 168             }
 169         }
 170         return packages;
 171     }
 172 
 173     private static boolean isOpenJDKOnly() {
 174         String prop = System.getProperty("java.runtime.name");
 175         return prop != null && prop.startsWith("OpenJDK");
 176     }
 177 }
--- EOF ---