author | mullan |
Fri, 12 Jun 2015 12:50:41 -0400 | |
changeset 31141 | ef5ddf021137 |
child 31804 | 013c14047253 |
permissions | -rw-r--r-- |
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
1 |
/* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
4 |
* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
8 |
* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
13 |
* accompanied this code). |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
14 |
* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
18 |
* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
21 |
* questions. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
22 |
*/ |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
23 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
24 |
import java.io.IOException; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
25 |
import java.net.URL; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
26 |
import java.security.CodeSource; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
27 |
import java.security.Permission; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
28 |
import java.security.Policy; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
29 |
import java.security.ProtectionDomain; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
30 |
import java.security.SecureClassLoader; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
31 |
import java.security.cert.Certificate; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
32 |
import java.util.ArrayList; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
33 |
import java.util.Arrays; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
34 |
import java.util.Base64; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
35 |
import java.util.Collections; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
36 |
import java.util.List; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
37 |
import java.util.PropertyPermission; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
38 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
39 |
/* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
40 |
* @test |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
41 |
* @bug 6826789 |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
42 |
* @summary Make sure equivalent ProtectionDomains are granted the same |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
43 |
* permissions when the CodeSource URLs are different but resolve |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
44 |
* to the same ip address after name service resolution. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
45 |
* @run main/othervm/java.security.policy=DefineClass.policy DefineClass |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
46 |
*/ |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
47 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
48 |
public class DefineClass { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
49 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
50 |
// permissions that are expected to be granted by the policy file |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
51 |
private final static Permission[] GRANTED_PERMS = new Permission[] { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
52 |
new PropertyPermission("user.home", "read"), |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
53 |
new PropertyPermission("user.name", "read") |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
54 |
}; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
55 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
56 |
// Base64 encoded bytes of a simple class: "public class Foo {}" |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
57 |
private final static String FOO_CLASS = |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
58 |
"yv66vgAAADQADQoAAwAKBwALBwAMAQAGPGluaXQ+AQADKClWAQAEQ29kZQEA" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
59 |
"D0xpbmVOdW1iZXJUYWJsZQEAClNvdXJjZUZpbGUBAAhGb28uamF2YQwABAAF" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
60 |
"AQADRm9vAQAQamF2YS9sYW5nL09iamVjdAAhAAIAAwAAAAAAAQABAAQABQAB" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
61 |
"AAYAAAAdAAEAAQAAAAUqtwABsQAAAAEABwAAAAYAAQAAAAEAAQAIAAAAAgAJ"; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
62 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
63 |
// Base64 encoded bytes of a simple class: "public class Bar {}" |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
64 |
private final static String BAR_CLASS = |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
65 |
"yv66vgAAADQADQoAAwAKBwALBwAMAQAGPGluaXQ+AQADKClWAQAEQ29kZQEA" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
66 |
"D0xpbmVOdW1iZXJUYWJsZQEAClNvdXJjZUZpbGUBAAhCYXIuamF2YQwABAAF" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
67 |
"AQADQmFyAQAQamF2YS9sYW5nL09iamVjdAAhAAIAAwAAAAAAAQABAAQABQAB" + |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
68 |
"AAYAAAAdAAEAAQAAAAUqtwABsQAAAAEABwAAAAYAAQAAAAEAAQAIAAAAAgAJ"; |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
69 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
70 |
public static void main(String[] args) throws Exception { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
71 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
72 |
MySecureClassLoader scl = new MySecureClassLoader(); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
73 |
Policy p = Policy.getPolicy(); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
74 |
ArrayList<Permission> perms1 = getPermissions(scl, p, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
75 |
"http://localhost/", |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
76 |
"Foo", FOO_CLASS); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
77 |
checkPerms(perms1, GRANTED_PERMS); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
78 |
ArrayList<Permission> perms2 = getPermissions(scl, p, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
79 |
"http://127.0.0.1/", |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
80 |
"Bar", BAR_CLASS); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
81 |
checkPerms(perms2, GRANTED_PERMS); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
82 |
assert(perms1.equals(perms2)); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
83 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
84 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
85 |
// returns the permissions granted to the codebase URL |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
86 |
private static ArrayList<Permission> getPermissions(MySecureClassLoader scl, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
87 |
Policy p, String url, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
88 |
String className, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
89 |
String classBytes) |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
90 |
throws IOException { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
91 |
CodeSource cs = new CodeSource(new URL(url), (Certificate[])null); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
92 |
Base64.Decoder bd = Base64.getDecoder(); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
93 |
byte[] bytes = bd.decode(classBytes); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
94 |
Class<?> c = scl.defineMyClass(className, bytes, cs); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
95 |
ProtectionDomain pd = c.getProtectionDomain(); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
96 |
return Collections.list(p.getPermissions(pd).elements()); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
97 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
98 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
99 |
private static void checkPerms(List<Permission> perms, |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
100 |
Permission... grantedPerms) |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
101 |
throws Exception |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
102 |
{ |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
103 |
if (!perms.containsAll(Arrays.asList(grantedPerms))) { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
104 |
throw new Exception("Granted permissions not correct"); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
105 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
106 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
107 |
|
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
108 |
// A SecureClassLoader that allows the test to define its own classes |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
109 |
private static class MySecureClassLoader extends SecureClassLoader { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
110 |
Class<?> defineMyClass(String name, byte[] b, CodeSource cs) { |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
111 |
return super.defineClass(name, b, 0, b.length, cs); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
112 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
113 |
} |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
diff
changeset
|
114 |
} |