1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8164705 8168410
* @summary check compatibility after FilePermission change
* @library /test/lib
* @run main CompatImpact prepare
* @run main CompatImpact builtin
* @run main/othervm -Djdk.security.filePermCompat=true CompatImpact mine
* @run main/fail CompatImpact mine
* @run main CompatImpact dopriv
*/
import jdk.test.lib.process.Proc;
import java.io.File;
import java.io.FilePermission;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Policy;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.security.SecurityPermission;
public class CompatImpact {
public static void main(String[] args) throws Exception {
switch (args[0]) {
// copy class files to future classpath
case "prepare":
// cp in .
String cp = System.getProperty("test.classes");
Files.copy(Paths.get(cp, "CompatImpact.class"),
Paths.get("CompatImpact.class"));
Files.copy(Paths.get(cp, "CompatImpact$MP.class"),
Paths.get("CompatImpact$MP.class"));
Files.write(Paths.get("f"), new byte[10]);
// cp in ./sub
Files.createDirectory(Paths.get("sub"));
Files.copy(Paths.get(cp, "CompatImpact.class"),
Paths.get("sub", "CompatImpact.class"));
Files.copy(Paths.get(cp, "CompatImpact$MP.class"),
Paths.get("sub", "CompatImpact$MP.class"));
Files.write(Paths.get("sub", "f"), new byte[10]);
// cp in ./inner
Files.createDirectory(Paths.get("inner"));
Files.copy(Paths.get(cp, "CompatImpact$DoPrivInner.class"),
Paths.get("inner", "CompatImpact$DoPrivInner.class"));
break;
// default policy always covered, user-defined depends on
// system property jdk.security.filePermCompact.
case "builtin":
case "mine":
cp = System.getProperty("test.classes");
Proc p;
String failed = "";
String testcase = "";
String cwd = System.getProperty("user.dir");
// Granting a FilePermission on an absolute path
testcase = "PonA";
p = p(args[0], cwd + "/f")
.args("f", cwd + "/f")
.debug(testcase)
.start();
if (p.waitFor() != 0) {
Files.copy(Paths.get(testcase + ".stderr"), System.out);
failed += testcase + " ";
}
// Granting a FilePermission on a relative path
testcase = "PonR";
p = p(args[0], "f")
.args("f", cwd + "/f")
.debug(testcase)
.start();
if (p.waitFor() != 0) {
Files.copy(Paths.get(testcase + ".stderr"), System.out);
failed += testcase + " ";
}
// Reading file on classpath, not cwd
testcase = "cp";
String cprel = Paths.get(cwd).relativize(Paths.get(cp))
.normalize().toString();
p = p(args[0], "x")
.args(cp + "/f", cprel + "/f")
.debug(testcase)
.start();
if (p.waitFor() != 0) {
Files.copy(Paths.get(testcase + ".stderr"), System.out);
failed += testcase + " ";
}
// Reading file on classpath, cwd
testcase = "cpHere";
p = p(args[0], "x")
.args(cwd + "/f", "f", "RES")
.cp(".") // Must! cancel the old CLASSPATH.
.debug(testcase)
.start();
if (p.waitFor() != 0) {
Files.copy(Paths.get(testcase + ".stderr"), System.out);
failed += testcase + " ";
}
// Reading file on classpath, cwd
testcase = "cpSub";
p = p(args[0], "x")
.args(cwd + "/sub/f", "sub/f", "RES")
.cp("sub") // Must! There's CLASSPATH.
.debug(testcase)
.start();
if (p.waitFor() != 0) {
Files.copy(Paths.get(testcase + ".stderr"), System.out);
failed += testcase + " ";
}
if (!failed.isEmpty()) {
throw new Exception(failed + "failed");
}
break;
// test <policy_type> <grant> <read...>
case "test":
if (args[1].equals("mine")) {
Policy.setPolicy(new MP(args[2]));
}
Exception e = null;
for (int i = 3; i < args.length; i++) {
try {
System.out.println(args[i]);
if (args[i].equals("RES")) {
CompatImpact.class.getResourceAsStream("f")
.close();
} else {
new File(args[i]).exists();
}
} catch (Exception e2) {
e = e2;
e2.printStackTrace(System.out);
}
}
if (e != null) {
System.err.println("====================");
throw e;
}
break;
// doPrivWithPerm test launcher
case "dopriv":
cwd = System.getProperty("user.dir");
// caller (CompatImpact doprivouter, no permission) in sub,
// executor (DoPrivInner, AllPermission) in inner.
p = Proc.create("CompatImpact")
.args("doprivouter")
.prop("java.security.manager", "")
.grant(new File("inner"))
.perm(new AllPermission())
.cp("sub", "inner")
.debug("doPriv")
.args(cwd)
.start();
if (p.waitFor() != 0) {
throw new Exception("dopriv test fails");
}
break;
// doprivouter <cwd>
case "doprivouter":
DoPrivInner.main(args);
break;
default:
throw new Exception("unknown " + args[0]);
}
}
// Call by CompatImpact doprivouter, with AllPermission
public static class DoPrivInner {
public static void main(String[] args) throws Exception {
AccessController.doPrivileged((PrivilegedAction<Boolean>)
() -> new File("x").exists(),
null,
new FilePermission(args[1] + "/x", "read"));
AccessController.doPrivileged((PrivilegedAction<Boolean>)
() -> new File(args[1] + "/x").exists(),
null,
new FilePermission("x", "read"));
try {
AccessController.doPrivileged((PrivilegedAction<Boolean>)
() -> new File("x").exists(),
null,
new FilePermission("y", "read"));
throw new Exception("Should not read");
} catch (SecurityException se) {
// Expected
}
}
}
// Return a Proc object for different policy types
private static Proc p(String type, String f) throws Exception {
Proc p = Proc.create("CompatImpact")
.prop("java.security.manager", "")
.inheritProp("jdk.security.filePermCompat");
p.args("test", type);
switch (type) {
case "builtin":
// For builtin policy, reading access to f can be
// granted as a permission
p.perm(new FilePermission(f, "read"));
p.args("-");
break;
case "mine":
// For my policy, f is passed into test and new MP(f)
// will be set as new policy
p.perm(new SecurityPermission("setPolicy"));
p.perm(new SecurityPermission("getPolicy"));
p.args(f);
break;
default:
throw new Exception("unknown " + type);
}
return p;
}
// My own Policy impl, with only one granted permission, also not smart
// enough to know whether ProtectionDomain grants any permission
static class MP extends Policy {
static final Policy DEFAULT_POLICY = Policy.getPolicy();
final PermissionCollection pc;
MP(String f) {
FilePermission p = new FilePermission(f, "read");
pc = p.newPermissionCollection();
pc.add(p);
}
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
return pc;
}
@Override
public PermissionCollection getPermissions(ProtectionDomain domain) {
return pc;
}
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return pc.implies(permission) || DEFAULT_POLICY.implies(domain, permission);
}
}
}
|