1 /*
2  * Copyright (c) 2001, 2012, 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 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 /* @test
25  * @bug 4465315
26  * @summary The RMI runtime's server-side DGC implementation object should
27  * not be exported with the arbitrary access control context that was in
28  * effect when it gets lazily created.  For example, calls to it should not
29  * fail due to the "accept" SocketPermission check simply because of the
30  * access control context that it was exported with.
31  * @author Peter Jones
32  *
33  * @library ../../testlibrary
34  * @modules java.rmi/sun.rmi.registry
35  *          java.rmi/sun.rmi.server
36  *          java.rmi/sun.rmi.transport
37  *          java.rmi/sun.rmi.transport.tcp
38  * @build TestLibrary DGCImplInsulation_Stub
39  * @run main/othervm/policy=security.policy DGCImplInsulation
40  */
41 
42 import java.lang.ref.Reference;
43 import java.lang.ref.ReferenceQueue;
44 import java.lang.ref.WeakReference;
45 import java.net.SocketPermission;
46 import java.rmi.MarshalledObject;
47 import java.rmi.Remote;
48 import java.rmi.server.UnicastRemoteObject;
49 import java.security.AccessControlContext;
50 import java.security.CodeSource;
51 import java.security.Permissions;
52 import java.security.PrivilegedExceptionAction;
53 import java.security.ProtectionDomain;
54 import java.security.cert.Certificate;
55 
56 public class DGCImplInsulation implements java.rmi.Remote {
57 
58     private static final long TIMEOUT = 5000;
59 
main(String[] args)60     public static void main(String[] args) throws Exception {
61 
62         TestLibrary.suggestSecurityManager(null);
63 
64         Permissions perms = new Permissions();
65         perms.add(new SocketPermission("*:1024-", "listen"));
66         AccessControlContext acc =
67             new AccessControlContext(new ProtectionDomain[] {
68                 new ProtectionDomain(
69                     new CodeSource(null, (Certificate[]) null), perms) });
70 
71         Remote impl = new DGCImplInsulation();;
72 
73         try {
74             Remote stub = (Remote) java.security.AccessController.doPrivileged(
75                 new ExportAction(impl));
76             System.err.println("exported remote object; local stub: " + stub);
77 
78             MarshalledObject mobj = new MarshalledObject(stub);
79             stub = (Remote) mobj.get();
80             System.err.println("marshalled/unmarshalled stub: " + stub);
81 
82             ReferenceQueue refQueue = new ReferenceQueue();
83             Reference weakRef = new WeakReference(impl, refQueue);
84             impl = null;
85             System.gc();
86             if (refQueue.remove(TIMEOUT) == weakRef) {
87                 throw new RuntimeException(
88                     "TEST FAILED: remote object garbage collected");
89             } else {
90                 System.err.println("TEST PASSED");
91                 stub = null;
92                 System.gc();
93                 Thread.sleep(2000);
94                 System.gc();
95             }
96         } finally {
97             try {
98                 UnicastRemoteObject.unexportObject(impl, true);
99             } catch (Exception e) {
100             }
101         }
102     }
103 
104     private static class ExportAction implements PrivilegedExceptionAction {
105         private final Remote impl;
ExportAction(Remote impl)106         ExportAction(Remote impl) {
107             this.impl = impl;
108         }
run()109         public Object run() throws Exception {
110             return UnicastRemoteObject.exportObject(impl);
111         }
112     }
113 }
114