Path: blob/master/test/jdk/java/lang/ProcessBuilder/SecurityManagerClinit.java
41149 views
/*1* Copyright 2010 Google Inc. All Rights Reserved.2* Copyright (c) 2019, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @bug 698074727* @summary Check that Process-related classes have the proper28* doPrivileged blocks, and can be initialized with an adversarial29* security manager.30* @run main/othervm -Djava.security.manager=allow SecurityManagerClinit31* @author Martin Buchholz32*/3334import java.io.*;35import java.security.*;3637public class SecurityManagerClinit {38private static class SimplePolicy extends Policy {39static final Policy DEFAULT_POLICY = Policy.getPolicy();4041private Permissions perms;4243public SimplePolicy(Permission... permissions) {44perms = new Permissions();45for (Permission permission : permissions)46perms.add(permission);47}4849public boolean implies(ProtectionDomain pd, Permission p) {50return perms.implies(p) || DEFAULT_POLICY.implies(pd, p);51}52}5354public static void main(String[] args) throws Throwable {55String javaExe =56System.getProperty("java.home") +57File.separator + "bin" + File.separator + "java";5859final SimplePolicy policy =60new SimplePolicy61(new FilePermission("<<ALL FILES>>", "execute"),62new RuntimePermission("setSecurityManager"));63Policy.setPolicy(policy);6465System.setSecurityManager(new SecurityManager());6667try {68String[] cmd = { javaExe, "-version" };69Process p = Runtime.getRuntime().exec(cmd);70p.getOutputStream().close();71p.getInputStream().close();72p.getErrorStream().close();73p.waitFor();74} finally {75System.setSecurityManager(null);76}77}78}798081