2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / SecurityManager.java
blobe54ebc62f70e86e01f161725a342dee3c0b33bb4
1 /* SecurityManager.java -- security checks for privileged actions
2 Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang;
41 import java.awt.AWTPermission;
42 import java.io.File;
43 import java.io.FileDescriptor;
44 import java.io.FilePermission;
45 import java.lang.reflect.Member;
46 import java.net.InetAddress;
47 import java.net.SocketPermission;
48 import java.security.AllPermission;
49 import java.security.Permission;
50 import java.security.Security;
51 import java.security.SecurityPermission;
52 import java.util.PropertyPermission;
54 /**
55 * SecurityManager is a class you can extend to create your own Java
56 * security policy. By default, there is no SecurityManager installed in
57 * 1.1, which means that all things are permitted to all people. The security
58 * manager, if set, is consulted before doing anything with potentially
59 * dangerous results, and throws a <code>SecurityException</code> if the
60 * action is forbidden.
62 * <p>A typical check is as follows, just before the dangerous operation:<br>
63 * <pre>
64 * SecurityManager sm = System.getSecurityManager();
65 * if (sm != null)
66 * sm.checkABC(<em>argument</em>, ...);
67 * </pre>
68 * Note that this is thread-safe, by caching the security manager in a local
69 * variable rather than risking a NullPointerException if the mangager is
70 * changed between the check for null and before the permission check.
72 * <p>The special method <code>checkPermission</code> is a catchall, and
73 * the default implementation calls
74 * <code>AccessController.checkPermission</code>. In fact, all the other
75 * methods default to calling checkPermission.
77 * <p>Sometimes, the security check needs to happen from a different context,
78 * such as when called from a worker thread. In such cases, use
79 * <code>getSecurityContext</code> to take a snapshot that can be passed
80 * to the worker thread:<br>
81 * <pre>
82 * Object context = null;
83 * SecurityManager sm = System.getSecurityManager();
84 * if (sm != null)
85 * context = sm.getSecurityContext(); // defaults to an AccessControlContext
86 * // now, in worker thread
87 * if (sm != null)
88 * sm.checkPermission(permission, context);
89 * <pre>
91 * <p>Permissions fall into these categories: File, Socket, Net, Security,
92 * Runtime, Property, AWT, Reflect, and Serializable. Each of these
93 * permissions have a property naming convention, that follows a hierarchical
94 * naming convention, to make it easy to grant or deny several permissions
95 * at once. Some permissions also take a list of permitted actions, such
96 * as "read" or "write", to fine-tune control even more. The permission
97 * <code>java.security.AllPermission</code> grants all permissions.
99 * <p>The default methods in this class deny all things to all people. You
100 * must explicitly grant permission for anything you want to be legal when
101 * subclassing this class.
103 * @author John Keiser
104 * @author Eric Blake <ebb9@email.byu.edu>
105 * @see ClassLoader
106 * @see SecurityException
107 * @see #checkTopLevelWindow(Object)
108 * @see System#getSecurityManager()
109 * @see System#setSecurityManager(SecurityManager)
110 * @see AccessController
111 * @see AccessControlContext
112 * @see AccessControlException
113 * @see Permission
114 * @see BasicPermission
115 * @see java.io.FilePermission
116 * @see java.net.SocketPermission
117 * @see java.util.PropertyPermission
118 * @see RuntimePermission
119 * @see java.awt.AWTPermission
120 * @see Policy
121 * @see SecurityPermission
122 * @see ProtectionDomain
123 * @since 1.0
124 * @status still missing 1.4 functionality
126 public class SecurityManager
129 * Tells whether or not the SecurityManager is currently performing a
130 * security check.
131 * @deprecated Use {@link #checkPermission(Permission)} instead.
133 protected boolean inCheck;
136 * Construct a new security manager. There may be a security check, of
137 * <code>RuntimePermission("createSecurityManager")</code>.
139 * @throws SecurityException if permission is denied
141 public SecurityManager()
143 SecurityManager sm = System.getSecurityManager();
144 if (sm != null)
145 sm.checkPermission(new RuntimePermission("createSecurityManager"));
149 * Tells whether or not the SecurityManager is currently performing a
150 * security check.
152 * @return true if the SecurityManager is in a security check
153 * @see #inCheck
154 * @deprecated use {@link #checkPermission(Permission)} instead
156 public boolean getInCheck()
158 return inCheck;
162 * Get a list of all the classes currently executing methods on the Java
163 * stack. getClassContext()[0] is the currently executing method (ie. the
164 * class that CALLED getClassContext, not SecurityManager).
166 * @return an array of classes on the Java execution stack
168 protected Class[] getClassContext()
170 return VMSecurityManager.getClassContext();
174 * Find the ClassLoader of the first non-system class on the execution
175 * stack. A non-system class is one whose ClassLoader is not equal to
176 * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This
177 * will return null in three cases:<br><nl>
178 * <li>All methods on the stack are from system classes</li>
179 * <li>All methods on the stack up to the first "privileged" caller, as
180 * created by {@link AccessController.doPrivileged(PrivilegedAction)},
181 * are from system classes</li>
182 * <li>A check of <code>java.security.AllPermission</code> succeeds.</li>
183 * </nl>
185 * @return the most recent non-system ClassLoader on the execution stack
186 * @deprecated use {@link #checkPermission(Permission)} instead
188 protected ClassLoader currentClassLoader()
190 return VMSecurityManager.currentClassLoader();
194 * Find the first non-system class on the execution stack. A non-system
195 * class is one whose ClassLoader is not equal to
196 * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This
197 * will return null in three cases:<br><nl>
198 * <li>All methods on the stack are from system classes</li>
199 * <li>All methods on the stack up to the first "privileged" caller, as
200 * created by {@link AccessController.doPrivileged(PrivilegedAction)},
201 * are from system classes</li>
202 * <li>A check of <code>java.security.AllPermission</code> succeeds.</li>
203 * </nl>
205 * @return the most recent non-system Class on the execution stack
206 * @deprecated use {@link #checkPermission(Permission)} instead
208 protected Class currentLoadedClass()
210 Class[] c = getClassContext();
211 for (int i = 0; i < c.length; i++)
212 if (c[i].getClassLoader() != null)
213 return c[i];
214 return null;
218 * Get the depth of a particular class on the execution stack.
220 * @param className the fully-qualified name to search for
221 * @return the index of the class on the stack, or -1
222 * @deprecated use {@link #checkPermission(Permission)} instead
224 protected int classDepth(String className)
226 Class[] c = getClassContext();
227 for (int i = 0; i < c.length; i++)
228 if (className.equals(c[i].getName()))
229 return i;
230 return -1;
234 * Get the depth on the execution stack of the most recent non-system class.
235 * A non-system class is one whose ClassLoader is not equal to
236 * {@link ClassLoader#getSystemClassLoader()} or its ancestors. This
237 * will return -1 in three cases:<br><nl>
238 * <li>All methods on the stack are from system classes</li>
239 * <li>All methods on the stack up to the first "privileged" caller, as
240 * created by {@link AccessController.doPrivileged(PrivilegedAction)},
241 * are from system classes</li>
242 * <li>A check of <code>java.security.AllPermission</code> succeeds.</li>
243 * </nl>
245 * @return the index of the most recent non-system Class on the stack
246 * @deprecated use {@link #checkPermission(Permission)} instead
248 protected int classLoaderDepth()
250 Class[] c = getClassContext();
251 for (int i = 0; i < c.length; i++)
252 if (c[i].getClassLoader() != null)
253 return i;
254 return -1;
258 * Tell whether the specified class is on the execution stack.
260 * @param className the fully-qualified name of the class to find
261 * @return whether the specified class is on the execution stack
262 * @deprecated use {@link #checkPermission(Permission)} instead
264 protected boolean inClass(String className)
266 return classDepth(className) != -1;
270 * Tell whether there is a class loaded with an explicit ClassLoader on
271 * the stack.
273 * @return whether a class with an explicit ClassLoader is on the stack
274 * @deprecated use {@link #checkPermission(Permission)} instead
276 protected boolean inClassLoader()
278 return classLoaderDepth() != -1;
282 * Get an implementation-dependent Object that contains enough information
283 * about the current environment to be able to perform standard security
284 * checks later. This is used by trusted methods that need to verify that
285 * their callers have sufficient access to perform certain operations.
287 * <p>Currently the only methods that use this are checkRead() and
288 * checkConnect(). The default implementation returns an
289 * <code>AccessControlContext</code>.
291 * @return a security context
292 * @see #checkConnect(String, int, Object)
293 * @see #checkRead(String, Object)
294 * @see AccessControlContext
295 * @see AccessController#getContext()
297 public Object getSecurityContext()
299 // XXX Should be: return AccessController.getContext();
300 return new SecurityContext(getClassContext());
304 * Check if the current thread is allowed to perform an operation that
305 * requires the specified <code>Permission</code>. This defaults to
306 * <code>AccessController.checkPermission</code>.
308 * @param perm the <code>Permission</code> required
309 * @throws SecurityException if permission is denied
310 * @throws NullPointerException if perm is null
311 * @since 1.2
313 public void checkPermission(Permission perm)
315 // XXX Should be: AccessController.checkPermission(perm);
316 throw new SecurityException("Operation not allowed");
320 * Check if the current thread is allowed to perform an operation that
321 * requires the specified <code>Permission</code>. This is done in a
322 * context previously returned by <code>getSecurityContext()</code>. The
323 * default implementation expects context to be an AccessControlContext,
324 * and it calls <code>AccessControlContext.checkPermission(perm)</code>.
326 * @param perm the <code>Permission</code> required
327 * @param context a security context
328 * @throws SecurityException if permission is denied, or if context is
329 * not an AccessControlContext
330 * @throws NullPointerException if perm is null
331 * @see #getSecurityContext()
332 * @see AccessControlContext#checkPermission(Permission)
333 * @since 1.2
335 public void checkPermission(Permission perm, Object context)
337 // XXX Should be:
338 // if (! (context instanceof AccessControlContext))
339 // throw new SecurityException("Missing context");
340 // ((AccessControlContext) context).checkPermission(perm);
341 throw new SecurityException("Operation not allowed");
345 * Check if the current thread is allowed to create a ClassLoader. This
346 * method is called from ClassLoader.ClassLoader(), and checks
347 * <code>RuntimePermission("createClassLoader")</code>. If you override
348 * this, you should call <code>super.checkCreateClassLoader()</code> rather
349 * than throwing an exception.
351 * @throws SecurityException if permission is denied
352 * @see ClassLoader#ClassLoader()
354 public void checkCreateClassLoader()
356 checkPermission(new RuntimePermission("createClassLoader"));
360 * Check if the current thread is allowed to modify another Thread. This is
361 * called by Thread.stop(), suspend(), resume(), interrupt(), destroy(),
362 * setPriority(), setName(), and setDaemon(). The default implementation
363 * checks <code>RuntimePermission("modifyThread") on system threads (ie.
364 * threads in ThreadGroup with a null parent), and returns silently on
365 * other threads.
367 * <p>If you override this, you must do two things. First, call
368 * <code>super.checkAccess(t)</code>, to make sure you are not relaxing
369 * requirements. Second, if the calling thread has
370 * <code>RuntimePermission("modifyThread")</code>, return silently, so that
371 * core classes (the Classpath library!) can modify any thread.
373 * @param t the other Thread to check
374 * @throws SecurityException if permission is denied
375 * @throws NullPointerException if t is null
376 * @see Thread#stop()
377 * @see Thread#suspend()
378 * @see Thread#resume()
379 * @see Thread#setPriority(int)
380 * @see Thread#setName(String)
381 * @see Thread#setDaemon(boolean)
383 public void checkAccess(Thread t)
385 if (t.group != null && t.group.getParent() != null)
386 checkPermission(new RuntimePermission("modifyThread"));
390 * Check if the current thread is allowed to modify a ThreadGroup. This is
391 * called by Thread.Thread() (to add a thread to the ThreadGroup),
392 * ThreadGroup.ThreadGroup() (to add this ThreadGroup to a parent),
393 * ThreadGroup.stop(), suspend(), resume(), interrupt(), destroy(),
394 * setDaemon(), and setMaxPriority(). The default implementation
395 * checks <code>RuntimePermission("modifyThread") on the system group (ie.
396 * the one with a null parent), and returns silently on other groups.
398 * <p>If you override this, you must do two things. First, call
399 * <code>super.checkAccess(t)</code>, to make sure you are not relaxing
400 * requirements. Second, if the calling thread has
401 * <code>RuntimePermission("modifyThreadGroup")</code>, return silently,
402 * so that core classes (the Classpath library!) can modify any thread.
404 * @param g the ThreadGroup to check
405 * @throws SecurityException if permission is denied
406 * @throws NullPointerException if g is null
407 * @see Thread#Thread()
408 * @see ThreadGroup#ThreadGroup()
409 * @see ThreadGroup#stop()
410 * @see ThreadGroup#suspend()
411 * @see ThreadGroup#resume()
412 * @see ThreadGroup#interrupt()
413 * @see ThreadGroup#setDaemon(boolean)
414 * @see ThreadGroup#setMaxPriority(int)
416 public void checkAccess(ThreadGroup g)
418 if (g.getParent() != null)
419 checkPermission(new RuntimePermission("modifyThreadGroup"));
423 * Check if the current thread is allowed to exit the JVM with the given
424 * status. This method is called from Runtime.exit() and Runtime.halt().
425 * The default implementation checks
426 * <code>RuntimePermission("exitVM")</code>. If you override this, call
427 * <code>super.checkExit</code> rather than throwing an exception.
429 * @param status the status to exit with
430 * @throws SecurityException if permission is denied
431 * @see Runtime#exit(int)
432 * @see Runtime#halt(int)
434 public void checkExit(int status)
436 checkPermission(new RuntimePermission("exitVM"));
440 * Check if the current thread is allowed to execute the given program. This
441 * method is called from Runtime.exec(). If the name is an absolute path,
442 * the default implementation checks
443 * <code>FilePermission(program, "execute")</code>, otherwise it checks
444 * <code>FilePermission("&lt;&lt;ALL FILES&gt;&gt;", "execute")</code>. If
445 * you override this, call <code>super.checkExec</code> rather than
446 * throwing an exception.
448 * @param program the name of the program to exec
449 * @throws SecurityException if permission is denied
450 * @throws NullPointerException if program is null
451 * @see Runtime#exec(String[], String[], File)
453 public void checkExec(String program)
455 if (! program.equals(new File(program).getAbsolutePath()))
456 program = "<<ALL FILES>>";
457 checkPermission(new FilePermission(program, "execute"));
461 * Check if the current thread is allowed to link in the given native
462 * library. This method is called from Runtime.load() (and hence, by
463 * loadLibrary() as well). The default implementation checks
464 * <code>RuntimePermission("loadLibrary." + filename)</code>. If you
465 * override this, call <code>super.checkLink</code> rather than throwing
466 * an exception.
468 * @param filename the full name of the library to load
469 * @throws SecurityException if permission is denied
470 * @throws NullPointerException if filename is null
471 * @see Runtime#load(String)
473 public void checkLink(String filename)
475 // Use the toString() hack to do the null check.
476 checkPermission(new RuntimePermission("loadLibrary."
477 + filename.toString()));
481 * Check if the current thread is allowed to read the given file using the
482 * FileDescriptor. This method is called from
483 * FileInputStream.FileInputStream(). The default implementation checks
484 * <code>RuntimePermission("readFileDescriptor")</code>. If you override
485 * this, call <code>super.checkRead</code> rather than throwing an
486 * exception.
488 * @param desc the FileDescriptor representing the file to access
489 * @throws SecurityException if permission is denied
490 * @throws NullPointerException if desc is null
491 * @see FileInputStream#FileInputStream(FileDescriptor)
493 public void checkRead(FileDescriptor desc)
495 if (desc == null)
496 throw new NullPointerException();
497 checkPermission(new RuntimePermission("readFileDescriptor"));
501 * Check if the current thread is allowed to read the given file. This
502 * method is called from FileInputStream.FileInputStream(),
503 * RandomAccessFile.RandomAccessFile(), File.exists(), canRead(), isFile(),
504 * isDirectory(), lastModified(), length() and list(). The default
505 * implementation checks <code>FilePermission(filename, "read")</code>. If
506 * you override this, call <code>super.checkRead</code> rather than
507 * throwing an exception.
509 * @param filename the full name of the file to access
510 * @throws SecurityException if permission is denied
511 * @throws NullPointerException if filename is null
512 * @see File
513 * @see FileInputStream#FileInputStream(String)
514 * @see RandomAccessFile#RandomAccessFile(String)
516 public void checkRead(String filename)
518 checkPermission(new FilePermission(filename, "read"));
522 * Check if the current thread is allowed to read the given file. using the
523 * given security context. The context must be a result of a previous call
524 * to <code>getSecurityContext()</code>. The default implementation checks
525 * <code>AccessControlContext.checkPermission(new FilePermission(filename,
526 * "read"))</code>. If you override this, call <code>super.checkRead</code>
527 * rather than throwing an exception.
529 * @param filename the full name of the file to access
530 * @param context the context to determine access for
531 * @throws SecurityException if permission is denied, or if context is
532 * not an AccessControlContext
533 * @throws NullPointerException if filename is null
534 * @see #getSecurityContext()
535 * @see AccessControlContext#checkPermission(Permission)
537 public void checkRead(String filename, Object context)
539 // XXX Should be:
540 // if (! (context instanceof AccessControlContext))
541 // throw new SecurityException("Missing context");
542 // AccessControlContext ac = (AccessControlContext) context;
543 // ac.checkPermission(new FilePermission(filename, "read"));
544 throw new SecurityException("Cannot read files via file names.");
548 * Check if the current thread is allowed to write the given file using the
549 * FileDescriptor. This method is called from
550 * FileOutputStream.FileOutputStream(). The default implementation checks
551 * <code>RuntimePermission("writeFileDescriptor")</code>. If you override
552 * this, call <code>super.checkWrite</code> rather than throwing an
553 * exception.
555 * @param desc the FileDescriptor representing the file to access
556 * @throws SecurityException if permission is denied
557 * @throws NullPointerException if desc is null
558 * @see FileOutputStream#FileOutputStream(FileDescriptor)
560 public void checkWrite(FileDescriptor desc)
562 if (desc == null)
563 throw new NullPointerException();
564 checkPermission(new RuntimePermission("writeFileDescriptor"));
568 * Check if the current thread is allowed to write the given file. This
569 * method is called from FileOutputStream.FileOutputStream(),
570 * RandomAccessFile.RandomAccessFile(), File.canWrite(), mkdir(), and
571 * renameTo(). The default implementation checks
572 * <code>FilePermission(filename, "write")</code>. If you override this,
573 * call <code>super.checkWrite</code> rather than throwing an exception.
575 * @param filename the full name of the file to access
576 * @throws SecurityException if permission is denied
577 * @throws NullPointerException if filename is null
578 * @see File
579 * @see File#canWrite()
580 * @see File#mkdir()
581 * @see File#renameTo()
582 * @see FileOutputStream#FileOutputStream(String)
583 * @see RandomAccessFile#RandomAccessFile(String)
585 public void checkWrite(String filename)
587 checkPermission(new FilePermission(filename, "write"));
591 * Check if the current thread is allowed to delete the given file. This
592 * method is called from File.delete(). The default implementation checks
593 * <code>FilePermission(filename, "delete")</code>. If you override this,
594 * call <code>super.checkDelete</code> rather than throwing an exception.
596 * @param filename the full name of the file to delete
597 * @throws SecurityException if permission is denied
598 * @throws NullPointerException if filename is null
599 * @see File#delete()
601 public void checkDelete(String filename)
603 checkPermission(new FilePermission(filename, "delete"));
607 * Check if the current thread is allowed to connect to a given host on a
608 * given port. This method is called from Socket.Socket(). A port number
609 * of -1 indicates the caller is attempting to determine an IP address, so
610 * the default implementation checks
611 * <code>SocketPermission(host, "resolve")</code>. Otherwise, the default
612 * implementation checks
613 * <code>SocketPermission(host + ":" + port, "connect")</code>. If you
614 * override this, call <code>super.checkConnect</code> rather than throwing
615 * an exception.
617 * @param host the host to connect to
618 * @param port the port to connect on
619 * @throws SecurityException if permission is denied
620 * @throws NullPointerException if host is null
621 * @see Socket#Socket()
623 public void checkConnect(String host, int port)
625 if (port == -1)
626 checkPermission(new SocketPermission(host, "resolve"));
627 else
628 // Use the toString() hack to do the null check.
629 checkPermission(new SocketPermission(host.toString() + ":" + port,
630 "connect"));
634 * Check if the current thread is allowed to connect to a given host on a
635 * given port, using the given security context. The context must be a
636 * result of a previous call to <code>getSecurityContext</code>. A port
637 * number of -1 indicates the caller is attempting to determine an IP
638 * address, so the default implementation checks
639 * <code>AccessControlContext.checkPermission(new SocketPermission(host,
640 * "resolve"))</code>. Otherwise, the default implementation checks
641 * <code>AccessControlContext.checkPermission(new SocketPermission(host
642 * + ":" + port, "connect"))</code>. If you override this, call
643 * <code>super.checkConnect</code> rather than throwing an exception.
645 * @param host the host to connect to
646 * @param port the port to connect on
647 * @param context the context to determine access for
648 * @throws SecurityException if permission is denied, or if context is
649 * not an AccessControlContext
650 * @throws NullPointerException if host is null
651 * @see #getSecurityContext()
652 * @see AccessControlContext#checkPermission(Permission)
654 public void checkConnect(String host, int port, Object securityContext)
656 // XXX Should be:
657 // if (! (context instanceof AccessControlContext))
658 // throw new SecurityException("Missing context");
659 // AccessControlContext ac = (AccessControlContext) context;
660 // if (port == -1)
661 // ac.checkPermission(new SocketPermission(host, "resolve"));
662 // else
663 // // Use the toString() hack to do the null check.
664 // ac.checkPermission(new SocketPermission(host.toString + ":" +port,
665 // "connect"));
666 throw new SecurityException("Cannot make network connections.");
670 * Check if the current thread is allowed to listen to a specific port for
671 * data. This method is called by ServerSocket.ServerSocket(). The default
672 * implementation checks
673 * <code>SocketPermission("localhost:" + (port == 0 ? "1024-" : "" + port),
674 * "listen")</code>. If you override this, call
675 * <code>super.checkListen</code> rather than throwing an exception.
677 * @param port the port to listen on
678 * @throws SecurityException if permission is denied
679 * @see ServerSocket#ServerSocket(int)
681 public void checkListen(int port)
683 checkPermission(new SocketPermission("localhost:"
684 + (port == 0 ? "1024-" : "" +port),
685 "listen"));
689 * Check if the current thread is allowed to accept a connection from a
690 * particular host on a particular port. This method is called by
691 * ServerSocket.implAccept(). The default implementation checks
692 * <code>SocketPermission(host + ":" + port, "accept")</code>. If you
693 * override this, call <code>super.checkAccept</code> rather than throwing
694 * an exception.
696 * @param host the host which wishes to connect
697 * @param port the port the connection will be on
698 * @throws SecurityException if permission is denied
699 * @throws NullPointerException if host is null
700 * @see ServerSocket#accept()
702 public void checkAccept(String host, int port)
704 // Use the toString() hack to do the null check.
705 checkPermission(new SocketPermission(host.toString() + ":" + port,
706 "accept"));
710 * Check if the current thread is allowed to read and write multicast to
711 * a particular address. The default implementation checks
712 * <code>SocketPermission(addr.getHostAddress(), "accept,connect")</code>.
713 * If you override this, call <code>super.checkMulticast</code> rather than
714 * throwing an exception.
716 * @param addr the address to multicast to
717 * @throws SecurityException if permission is denied
718 * @throws NullPointerException if host is null
719 * @since 1.1
721 public void checkMulticast(InetAddress addr)
723 checkPermission(new SocketPermission(addr.getHostAddress(),
724 "accept,connect"));
728 *Check if the current thread is allowed to read and write multicast to
729 * a particular address with a particular ttl (time-to-live) value. The
730 * default implementation ignores ttl, and checks
731 * <code>SocketPermission(addr.getHostAddress(), "accept,connect")</code>.
732 * If you override this, call <code>super.checkMulticast</code> rather than
733 * throwing an exception.
735 * @param addr the address to multicast to
736 * @param ttl value in use for multicast send
737 * @throws SecurityException if permission is denied
738 * @throws NullPointerException if host is null
739 * @since 1.1
740 * @deprecated use {@link #checkPermission(Permission)} instead
742 public void checkMulticast(InetAddress addr, byte ttl)
744 checkPermission(new SocketPermission(addr.getHostAddress(),
745 "accept,connect"));
749 * Check if the current thread is allowed to read or write all the system
750 * properties at once. This method is called by System.getProperties()
751 * and setProperties(). The default implementation checks
752 * <code>PropertyPermission("*", "read,write")</code>. If you override
753 * this, call <code>super.checkPropertiesAccess</code> rather than
754 * throwing an exception.
756 * @throws SecurityException if permission is denied
757 * @see System#getProperties()
758 * @see System#setProperties(Properties)
760 public void checkPropertiesAccess()
762 checkPermission(new PropertyPermission("*", "read,write"));
766 * Check if the current thread is allowed to read a particular system
767 * property (writes are checked directly via checkPermission). This method
768 * is called by System.getProperty() and setProperty(). The default
769 * implementation checks <code>PropertyPermission(key, "read")</code>. If
770 * you override this, call <code>super.checkPropertyAccess</code> rather
771 * than throwing an exception.
773 * @throws SecurityException if permission is denied
774 * @throws NullPointerException if key is null
775 * @throws IllegalArgumentException if key is ""
776 * @see System#getProperty(String)
778 public void checkPropertyAccess(String key)
780 checkPermission(new PropertyPermission(key, "read"));
784 * Check if the current thread is allowed to create a top-level window. If
785 * it is not, the operation should still go through, but some sort of
786 * nonremovable warning should be placed on the window to show that it
787 * is untrusted. This method is called by Window.Window(). The default
788 * implementation checks
789 * <code>AWTPermission("showWindowWithoutWarningBanner")</code>, and returns
790 * true if no exception was thrown. If you override this, use
791 * <code>return super.checkTopLevelWindow</code> rather than returning
792 * false.
794 * @param window the window to create
795 * @return true if there is permission to show the window without warning
796 * @throws NullPointerException if window is null
797 * @see Window#Window(Frame)
799 public boolean checkTopLevelWindow(Object window)
801 if (window == null)
802 throw new NullPointerException();
805 checkPermission(new AWTPermission("showWindowWithoutWarningBanner"));
806 return true;
808 catch (SecurityException e)
810 return false;
815 * Check if the current thread is allowed to create a print job. This
816 * method is called by Toolkit.getPrintJob(). The default implementation
817 * checks <code>RuntimePermission("queuePrintJob")</code>. If you override
818 * this, call <code>super.checkPrintJobAccess</code> rather than throwing
819 * an exception.
821 * @throws SecurityException if permission is denied
822 * @see Toolkit#getPrintJob(Frame, String, Properties)
823 * @since 1.1
825 public void checkPrintJobAccess()
827 checkPermission(new RuntimePermission("queuePrintJob"));
831 * Check if the current thread is allowed to use the system clipboard. This
832 * method is called by Toolkit.getSystemClipboard(). The default
833 * implementation checks <code>AWTPermission("accessClipboard")</code>. If
834 * you override this, call <code>super.checkSystemClipboardAccess</code>
835 * rather than throwing an exception.
837 * @throws SecurityException if permission is denied
838 * @see Toolkit#getSystemClipboard()
839 * @since 1.1
841 public void checkSystemClipboardAccess()
843 checkPermission(new AWTPermission("accessClipboard"));
847 * Check if the current thread is allowed to use the AWT event queue. This
848 * method is called by Toolkit.getSystemEventQueue(). The default
849 * implementation checks <code>AWTPermission("accessEventQueue")</code>.
850 * you override this, call <code>super.checkAwtEventQueueAccess</code>
851 * rather than throwing an exception.
853 * @throws SecurityException if permission is denied
854 * @see Toolkit#getSystemEventQueue()
855 * @since 1.1
857 public void checkAwtEventQueueAccess()
859 // Should be: checkPermission(new AWTPermission("accessEventQueue"));
860 throw new SecurityException("Cannot access the AWT event queue.");
864 * Check if the current thread is allowed to access the specified package
865 * at all. This method is called by ClassLoader.loadClass() in user-created
866 * ClassLoaders. The default implementation gets a list of all restricted
867 * packages, via <code>Security.getProperty("package.access")</code>. Then,
868 * if packageName starts with or equals any restricted package, it checks
869 * <code>RuntimePermission("accessClassInPackage." + packageName)</code>.
870 * If you override this, you should call
871 * <code>super.checkPackageAccess</code> before doing anything else.
873 * @param packageName the package name to check access to
874 * @throws SecurityException if permission is denied
875 * @throws NullPointerException if packageName is null
876 * @see ClassLoader#loadClass(String, boolean)
877 * @see Security#getProperty(String)
879 public void checkPackageAccess(String packageName)
881 checkPackageList(packageName, "access", "accessClassInPackage.");
885 * Check if the current thread is allowed to define a class into the
886 * specified package. This method is called by ClassLoader.loadClass() in
887 * user-created ClassLoaders. The default implementation gets a list of all
888 * restricted packages, via
889 * <code>Security.getProperty("package.definition")</code>. Then, if
890 * packageName starts with or equals any restricted package, it checks
891 * <code>RuntimePermission("defineClassInPackage." + packageName)</code>.
892 * If you override this, you should call
893 * <code>super.checkPackageDefinition</code> before doing anything else.
895 * @param packageName the package name to check access to
896 * @throws SecurityException if permission is denied
897 * @throws NullPointerException if packageName is null
898 * @see ClassLoader#loadClass(String, boolean)
899 * @see Security#getProperty(String)
901 public void checkPackageDefinition(String packageName)
903 checkPackageList(packageName, "definition", "defineClassInPackage.");
907 * Check if the current thread is allowed to set the current socket factory.
908 * This method is called by Socket.setSocketImplFactory(),
909 * ServerSocket.setSocketFactory(), and URL.setURLStreamHandlerFactory().
910 * The default implementation checks
911 * <code>RuntimePermission("setFactory")</code>. If you override this, call
912 * <code>super.checkSetFactory</code> rather than throwing an exception.
914 * @throws SecurityException if permission is denied
915 * @see Socket#setSocketImplFactory(SocketImplFactory)
916 * @see ServerSocket#setSocketFactory(SocketImplFactory)
917 * @see URL#setURLStreamHandlerFactory(URLStreamHandlerFactory)
919 public void checkSetFactory()
921 checkPermission(new RuntimePermission("setFactory"));
925 * Check if the current thread is allowed to get certain types of Methods,
926 * Fields and Constructors from a Class object. This method is called by
927 * Class.getMethod[s](), Class.getField[s](), Class.getConstructor[s],
928 * Class.getDeclaredMethod[s](), Class.getDeclaredField[s](), and
929 * Class.getDeclaredConstructor[s](). The default implementation allows
930 * PUBLIC access, and access to classes defined by the same classloader as
931 * the code performing the reflection. Otherwise, it checks
932 * <code>RuntimePermission("accessDeclaredMembers")</code>. If you override
933 * this, do not call <code>super.checkMemberAccess</code>, as this would
934 * mess up the stack depth check that determines the ClassLoader requesting
935 * the access.
937 * @param c the Class to check
938 * @param memberType either DECLARED or PUBLIC
939 * @throws SecurityException if permission is denied, including when
940 * memberType is not DECLARED or PUBLIC
941 * @throws NullPointerException if c is null
942 * @see Class
943 * @see Member#DECLARED
944 * @see Member#PUBLIC
945 * @since 1.1
947 public void checkMemberAccess(Class c, int memberType)
949 if (c == null)
950 throw new NullPointerException();
951 if (memberType == Member.PUBLIC)
952 return;
953 // XXX Allow access to classes created by same classloader before next
954 // check.
955 checkPermission(new RuntimePermission("accessDeclaredMembers"));
959 * Test whether a particular security action may be taken. The default
960 * implementation checks <code>SecurityPermission(action)</code>. If you
961 * override this, call <code>super.checkSecurityAccess</code> rather than
962 * throwing an exception.
964 * @param action the desired action to take
965 * @throws SecurityException if permission is denied
966 * @throws NullPointerException if action is null
967 * @throws IllegalArgumentException if action is ""
968 * @since 1.1
970 public void checkSecurityAccess(String action)
972 checkPermission(new SecurityPermission(action));
976 * Get the ThreadGroup that a new Thread should belong to by default. Called
977 * by Thread.Thread(). The default implementation returns the current
978 * ThreadGroup of the current Thread. <STRONG>Spec Note:</STRONG> it is not
979 * clear whether the new Thread is guaranteed to pass the
980 * checkAccessThreadGroup() test when using this ThreadGroup, but I presume
981 * so.
983 * @return the ThreadGroup to put the new Thread into
984 * @since 1.1
986 public ThreadGroup getThreadGroup()
988 return Thread.currentThread().getThreadGroup();
992 * Helper that checks a comma-separated list of restricted packages, from
993 * <code>Security.getProperty("package.definition")</code>, for the given
994 * package access permission. If packageName starts with or equals any
995 * restricted package, it checks
996 * <code>RuntimePermission(permission + packageName)</code>.
998 * @param packageName the package name to check access to
999 * @param restriction the list of restrictions, after "package."
1000 * @param permission the base permission, including the '.'
1001 * @throws SecurityException if permission is denied
1002 * @throws NullPointerException if packageName is null
1003 * @see #checkPackageAccess(String)
1004 * @see #checkPackageDefinition(String)
1006 void checkPackageList(String packageName, String restriction,
1007 String permission)
1009 // Use the toString() hack to do the null check.
1010 Permission p = new RuntimePermission(permission + packageName.toString());
1011 String list = Security.getProperty("package." + restriction);
1012 if (list == null)
1013 return;
1014 while (! "".equals(packageName))
1016 for (int index = list.indexOf(packageName);
1017 index != -1; index = list.indexOf(packageName, index + 1))
1019 int packageNameCount = packageName.length();
1020 if (index + packageNameCount == list.length()
1021 || list.charAt(index + packageNameCount) == ',')
1023 checkPermission(p);
1024 return;
1027 int index = packageName.lastIndexOf('.');
1028 packageName = index < 0 ? "" : packageName.substring(0, index);
1031 } // class SecurityManager
1033 // XXX This class is unnecessary.
1034 class SecurityContext {
1035 Class[] classes;
1036 SecurityContext(Class[] classes) {
1037 this.classes = classes;