joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 1 | /* |
coffeys | 55ddfb1 | 2013-06-06 14:10:44 +0100 | [diff] [blame] | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 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 | /* |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 25 | * @test |
mullan | 40a8bb1 | 2013-08-27 12:04:32 -0400 | [diff] [blame] | 26 | * @bug 6741606 7146431 8000450 8019830 |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 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 |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 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 | */ |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 45 | public class CheckPackageAccess { |
| 46 | |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 47 | /* |
| 48 | * This array should be updated whenever new packages are added to the |
| 49 | * package.access property in the java.security file |
mullan | 8ee655c | 2013-10-17 10:18:44 -0400 | [diff] [blame] | 50 | * NOTE: it should be in the same order as the java.security file |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 51 | */ |
| 52 | private static final String[] packages = { |
| 53 | "sun.", |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 54 | "com.sun.xml.internal.", |
| 55 | "com.sun.imageio.", |
| 56 | "com.sun.istack.internal.", |
| 57 | "com.sun.jmx.", |
mullan | 40a8bb1 | 2013-08-27 12:04:32 -0400 | [diff] [blame] | 58 | "com.sun.media.sound.", |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 59 | "com.sun.proxy.", |
alanb | a8f8870 | 2013-10-22 11:43:04 +0100 | [diff] [blame] | 60 | "com.sun.corba.se.", |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 61 | "com.sun.org.apache.bcel.internal.", |
| 62 | "com.sun.org.apache.regexp.internal.", |
| 63 | "com.sun.org.apache.xerces.internal.", |
| 64 | "com.sun.org.apache.xpath.internal.", |
| 65 | "com.sun.org.apache.xalan.internal.extensions.", |
| 66 | "com.sun.org.apache.xalan.internal.lib.", |
| 67 | "com.sun.org.apache.xalan.internal.res.", |
| 68 | "com.sun.org.apache.xalan.internal.templates.", |
| 69 | "com.sun.org.apache.xalan.internal.utils.", |
| 70 | "com.sun.org.apache.xalan.internal.xslt.", |
| 71 | "com.sun.org.apache.xalan.internal.xsltc.cmdline.", |
| 72 | "com.sun.org.apache.xalan.internal.xsltc.compiler.", |
| 73 | "com.sun.org.apache.xalan.internal.xsltc.trax.", |
| 74 | "com.sun.org.apache.xalan.internal.xsltc.util.", |
| 75 | "com.sun.org.apache.xml.internal.res.", |
| 76 | "com.sun.org.apache.xml.internal.security.", |
| 77 | "com.sun.org.apache.xml.internal.serializer.utils.", |
| 78 | "com.sun.org.apache.xml.internal.utils.", |
| 79 | "com.sun.org.glassfish.", |
| 80 | "com.oracle.xmlns.internal.", |
| 81 | "com.oracle.webservices.internal.", |
| 82 | "oracle.jrockit.jfr.", |
| 83 | "org.jcp.xml.dsig.internal.", |
| 84 | "jdk.internal.", |
| 85 | "jdk.nashorn.internal.", |
| 86 | "jdk.nashorn.tools." |
| 87 | }; |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 88 | |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 89 | public static void main(String[] args) throws Exception { |
| 90 | List<String> pkgs = new ArrayList<>(Arrays.asList(packages)); |
| 91 | String osName = System.getProperty("os.name"); |
| 92 | if (osName.contains("OS X")) { |
| 93 | pkgs.add("apple."); // add apple package for OS X |
| 94 | } else if (osName.startsWith("Windows")) { |
| 95 | pkgs.add("com.sun.java.accessibility."); |
| 96 | } |
| 97 | |
| 98 | List<String> jspkgs = |
| 99 | getPackages(Security.getProperty("package.access")); |
| 100 | |
mullan | 8ee655c | 2013-10-17 10:18:44 -0400 | [diff] [blame] | 101 | if (!isOpenJDKOnly()) { |
| 102 | String lastPkg = pkgs.get(pkgs.size() - 1); |
| 103 | |
mullan | 506b2d3 | 2013-10-11 08:43:07 -0400 | [diff] [blame] | 104 | // Remove any closed packages from list before comparing |
mullan | 8ee655c | 2013-10-17 10:18:44 -0400 | [diff] [blame] | 105 | int index = jspkgs.indexOf(lastPkg); |
| 106 | if (index != -1 && index != jspkgs.size() - 1) { |
| 107 | jspkgs.subList(index + 1, jspkgs.size()).clear(); |
| 108 | } |
mullan | 506b2d3 | 2013-10-11 08:43:07 -0400 | [diff] [blame] | 109 | } |
| 110 | |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 111 | // Sort to ensure lists are comparable |
| 112 | Collections.sort(pkgs); |
| 113 | Collections.sort(jspkgs); |
| 114 | |
| 115 | if (!pkgs.equals(jspkgs)) { |
| 116 | for (String p : pkgs) |
| 117 | if (!jspkgs.contains(p)) |
| 118 | System.out.println("In golden set, but not in j.s file: " + p); |
| 119 | for (String p : jspkgs) |
| 120 | if (!pkgs.contains(p)) |
| 121 | System.out.println("In j.s file, but not in golden set: " + p); |
| 122 | |
| 123 | |
| 124 | throw new RuntimeException("restricted packages are not " + |
| 125 | "consistent with java.security file"); |
| 126 | } |
| 127 | System.setSecurityManager(new SecurityManager()); |
| 128 | SecurityManager sm = System.getSecurityManager(); |
| 129 | for (String pkg : packages) { |
| 130 | String subpkg = pkg + "foo"; |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 131 | try { |
| 132 | sm.checkPackageAccess(pkg); |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 133 | throw new RuntimeException("Able to access " + pkg + |
| 134 | " package"); |
| 135 | } catch (SecurityException se) { } |
| 136 | try { |
| 137 | sm.checkPackageAccess(subpkg); |
| 138 | throw new RuntimeException("Able to access " + subpkg + |
| 139 | " package"); |
coffeys | 55ddfb1 | 2013-06-06 14:10:44 +0100 | [diff] [blame] | 140 | } catch (SecurityException se) { } |
| 141 | try { |
| 142 | sm.checkPackageDefinition(pkg); |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 143 | throw new RuntimeException("Able to define class in " + pkg + |
| 144 | " package"); |
| 145 | } catch (SecurityException se) { } |
| 146 | try { |
| 147 | sm.checkPackageDefinition(subpkg); |
| 148 | throw new RuntimeException("Able to define class in " + subpkg + |
| 149 | " package"); |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 150 | } catch (SecurityException se) { } |
| 151 | } |
coffeys | 52be22a | 2013-07-09 16:00:41 +0100 | [diff] [blame] | 152 | System.out.println("Test passed"); |
| 153 | } |
| 154 | |
| 155 | private static List<String> getPackages(String p) { |
| 156 | List<String> packages = new ArrayList<>(); |
| 157 | if (p != null && !p.equals("")) { |
| 158 | StringTokenizer tok = new StringTokenizer(p, ","); |
| 159 | while (tok.hasMoreElements()) { |
| 160 | String s = tok.nextToken().trim(); |
| 161 | packages.add(s); |
| 162 | } |
| 163 | } |
| 164 | return packages; |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 165 | } |
mullan | 8ee655c | 2013-10-17 10:18:44 -0400 | [diff] [blame] | 166 | |
| 167 | private static boolean isOpenJDKOnly() { |
| 168 | String prop = System.getProperty("java.runtime.name"); |
| 169 | return prop != null && prop.startsWith("OpenJDK"); |
| 170 | } |
joehw | 08a68c4 | 2012-04-17 11:21:35 -0700 | [diff] [blame] | 171 | } |