1 /*
   2  * Copyright (c) 2012, 2014, 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
  27  *  @summary Make sure all restricted packages listed in the package.access
  28  *           property in the java.security file are blocked
  29  *  @run main/othervm CheckPackageAccess
  30  */
  31 
  32 import java.security.Security;
  33 import java.util.Collections;
  34 import java.util.Arrays;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.StringTokenizer;
  38 
  39 /*
  40  * The main benefit of this test is to catch merge errors or other types
  41  * of issues where one or more of the packages are accidentally
  42  * removed. This is why the packages that are known to be restricted have to
  43  * be explicitly listed below.
  44  */
  45 public class CheckPackageAccess {
  46 
  47     /*
  48      * This array should be updated whenever new packages are added to the
  49      * package.access property in the java.security file
  50      * NOTE: it should be in the same order as the java.security file
  51      */
  52     private static final String[] packages = {
  53         "sun.",
  54         "com.sun.xml.internal.",
  55         "com.sun.imageio.",
  56         "com.sun.istack.internal.",
  57         "com.sun.jmx.",
  58         "com.sun.media.sound.",
  59         "com.sun.naming.internal.",
  60         "com.sun.proxy.",
  61         "com.sun.corba.se.",
  62         "com.sun.org.apache.bcel.internal.",
  63         "com.sun.org.apache.regexp.internal.",
  64         "com.sun.org.apache.xerces.internal.",
  65         "com.sun.org.apache.xpath.internal.",
  66         "com.sun.org.apache.xalan.internal.extensions.",
  67         "com.sun.org.apache.xalan.internal.lib.",
  68         "com.sun.org.apache.xalan.internal.res.",
  69         "com.sun.org.apache.xalan.internal.templates.",
  70         "com.sun.org.apache.xalan.internal.utils.",
  71         "com.sun.org.apache.xalan.internal.xslt.",
  72         "com.sun.org.apache.xalan.internal.xsltc.cmdline.",
  73         "com.sun.org.apache.xalan.internal.xsltc.compiler.",
  74         "com.sun.org.apache.xalan.internal.xsltc.trax.",
  75         "com.sun.org.apache.xalan.internal.xsltc.util.",
  76         "com.sun.org.apache.xml.internal.res.",
  77         "com.sun.org.apache.xml.internal.security.",
  78         "com.sun.org.apache.xml.internal.serializer.utils.",
  79         "com.sun.org.apache.xml.internal.utils.",
  80         "com.sun.org.glassfish.",
  81         "com.oracle.xmlns.internal.",
  82         "com.oracle.webservices.internal.",
  83         "oracle.jrockit.jfr.",
  84         "org.jcp.xml.dsig.internal.",
  85         "jdk.internal.",
  86         "jdk.nashorn.internal.",
  87         "jdk.nashorn.tools.",
  88         "com.sun.activation.registries."
  89     };
  90 
  91     public static void main(String[] args) throws Exception {
  92         List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
  93         String osName = System.getProperty("os.name");
  94         if (osName.contains("OS X")) {
  95             pkgs.add("apple.");  // add apple package for OS X
  96         } else if (osName.startsWith("Windows")) {
  97             pkgs.add("com.sun.java.accessibility.");
  98         }
  99 
 100         List<String> jspkgs =
 101             getPackages(Security.getProperty("package.access"));
 102 
 103         if (!isOpenJDKOnly()) {
 104             String lastPkg = pkgs.get(pkgs.size() - 1);
 105 
 106             // Remove any closed packages from list before comparing
 107             int index = jspkgs.indexOf(lastPkg);
 108             if (index != -1 && index != jspkgs.size() - 1) {
 109                 jspkgs.subList(index + 1, jspkgs.size()).clear();
 110             }
 111         }
 112 
 113         // Sort to ensure lists are comparable
 114         Collections.sort(pkgs);
 115         Collections.sort(jspkgs);
 116 
 117         if (!pkgs.equals(jspkgs)) {
 118             for (String p : pkgs)
 119                 if (!jspkgs.contains(p))
 120                     System.out.println("In golden set, but not in j.s file: " + p);
 121             for (String p : jspkgs)
 122                 if (!pkgs.contains(p))
 123                     System.out.println("In j.s file, but not in golden set: " + p);
 124 
 125 
 126             throw new RuntimeException("restricted packages are not " +
 127                                        "consistent with java.security file");
 128         }
 129         System.setSecurityManager(new SecurityManager());
 130         SecurityManager sm = System.getSecurityManager();
 131         for (String pkg : packages) {
 132             String subpkg = pkg + "foo";
 133             try {
 134                 sm.checkPackageAccess(pkg);
 135                 throw new RuntimeException("Able to access " + pkg +
 136                                            " package");
 137             } catch (SecurityException se) { }
 138             try {
 139                 sm.checkPackageAccess(subpkg);
 140                 throw new RuntimeException("Able to access " + subpkg +
 141                                            " package");
 142             } catch (SecurityException se) { }
 143             try {
 144                 sm.checkPackageDefinition(pkg);
 145                 throw new RuntimeException("Able to define class in " + pkg +
 146                                            " package");
 147             } catch (SecurityException se) { }
 148             try {
 149                 sm.checkPackageDefinition(subpkg);
 150                 throw new RuntimeException("Able to define class in " + subpkg +
 151                                            " package");
 152             } catch (SecurityException se) { }
 153         }
 154         System.out.println("Test passed");
 155     }
 156 
 157     private static List<String> getPackages(String p) {
 158         List<String> packages = new ArrayList<>();
 159         if (p != null && !p.equals("")) {
 160             StringTokenizer tok = new StringTokenizer(p, ",");
 161             while (tok.hasMoreElements()) {
 162                 String s = tok.nextToken().trim();
 163                 packages.add(s);
 164             }
 165         }
 166         return packages;
 167     }
 168 
 169     private static boolean isOpenJDKOnly() {
 170         String prop = System.getProperty("java.runtime.name");
 171         return prop != null && prop.startsWith("OpenJDK");
 172     }
 173 }