1 /*
2  * Copyright 2010 Google Inc.  All Rights Reserved.
3  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * @test
27  * @bug 6980747
28  * @summary Check that Process-related classes have the proper
29  *     doPrivileged blocks, and can be initialized with an adversarial
30  *     security manager.
31  * @run main/othervm SecurityManagerClinit
32  * @author Martin Buchholz
33  */
34 
35 import java.io.*;
36 import java.security.*;
37 
38 public class SecurityManagerClinit {
39     private static class SimplePolicy extends Policy {
40         static final Policy DEFAULT_POLICY = Policy.getPolicy();
41 
42         private Permissions perms;
43 
SimplePolicy(Permission... permissions)44         public SimplePolicy(Permission... permissions) {
45             perms = new Permissions();
46             for (Permission permission : permissions)
47                 perms.add(permission);
48         }
49 
implies(ProtectionDomain pd, Permission p)50         public boolean implies(ProtectionDomain pd, Permission p) {
51             return perms.implies(p) || DEFAULT_POLICY.implies(pd, p);
52         }
53     }
54 
main(String[] args)55     public static void main(String[] args) throws Throwable {
56         String javaExe =
57             System.getProperty("java.home") +
58             File.separator + "bin" + File.separator + "java";
59 
60         final SimplePolicy policy =
61             new SimplePolicy
62             (new FilePermission("<<ALL FILES>>", "execute"),
63              new RuntimePermission("setSecurityManager"));
64         Policy.setPolicy(policy);
65 
66         System.setSecurityManager(new SecurityManager());
67 
68         try {
69             String[] cmd = { javaExe, "-version" };
70             Process p = Runtime.getRuntime().exec(cmd);
71             p.getOutputStream().close();
72             p.getInputStream().close();
73             p.getErrorStream().close();
74             p.waitFor();
75         } finally {
76             System.setSecurityManager(null);
77         }
78     }
79 }
80