1. /*
  2. * @(#)SecurityManager.java 1.129 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.security.*;
  9. import java.io.FileDescriptor;
  10. import java.io.File;
  11. import java.io.FilePermission;
  12. import java.awt.AWTPermission;
  13. import java.util.PropertyPermission;
  14. import java.lang.RuntimePermission;
  15. import java.net.SocketPermission;
  16. import java.net.NetPermission;
  17. import java.util.Hashtable;
  18. import java.net.InetAddress;
  19. import java.lang.reflect.Member;
  20. import java.lang.reflect.*;
  21. import java.net.URL;
  22. import sun.security.util.SecurityConstants;
  23. /**
  24. * The security manager is a class that allows
  25. * applications to implement a security policy. It allows an
  26. * application to determine, before performing a possibly unsafe or
  27. * sensitive operation, what the operation is and whether
  28. * it is being attempted in a security context that allows the
  29. * operation to be performed. The
  30. * application can allow or disallow the operation.
  31. * <p>
  32. * The <code>SecurityManager</code> class contains many methods with
  33. * names that begin with the word <code>check</code>. These methods
  34. * are called by various methods in the Java libraries before those
  35. * methods perform certain potentially sensitive operations. The
  36. * invocation of such a <code>check</code> method typically looks like this:
  37. * <p><blockquote><pre>
  38. * SecurityManager security = System.getSecurityManager();
  39. * if (security != null) {
  40. * security.check<i>XXX</i>(argument,  . . . );
  41. * }
  42. * </pre></blockquote>
  43. * <p>
  44. * The security manager is thereby given an opportunity to prevent
  45. * completion of the operation by throwing an exception. A security
  46. * manager routine simply returns if the operation is permitted, but
  47. * throws a <code>SecurityException</code> if the operation is not
  48. * permitted. The only exception to this convention is
  49. * <code>checkTopLevelWindow</code>, which returns a
  50. * <code>boolean</code> value.
  51. * <p>
  52. * The current security manager is set by the
  53. * <code>setSecurityManager</code> method in class
  54. * <code>System</code>. The current security manager is obtained
  55. * by the <code>getSecurityManager</code> method.
  56. * <p>
  57. * The special method
  58. * {@link SecurityManager#checkPermission(java.security.Permission)}
  59. * determines whether an access request indicated by a specified
  60. * permission should be granted or denied. The
  61. * default implementation calls
  62. *
  63. * <pre>
  64. * AccessController.checkPermission(perm);
  65. * </pre>
  66. *
  67. * <p>
  68. * If a requested access is allowed,
  69. * <code>checkPermission</code> returns quietly. If denied, a
  70. * <code>SecurityException</code> is thrown.
  71. * <p>
  72. * As of Java 2 SDK v1.2, the default implementation of each of the other
  73. * <code>check</code> methods in <code>SecurityManager</code> is to
  74. * call the <code>SecurityManager checkPermission</code> method
  75. * to determine if the calling thread has permission to perform the requested
  76. * operation.
  77. * <p>
  78. * Note that the <code>checkPermission</code> method with
  79. * just a single permission argument always performs security checks
  80. * within the context of the currently executing thread.
  81. * Sometimes a security check that should be made within a given context
  82. * will actually need to be done from within a
  83. * <i>different</i> context (for example, from within a worker thread).
  84. * The {@link SecurityManager#getSecurityContext getSecurityContext} method
  85. * and the {@link SecurityManager#checkPermission(java.security.Permission,
  86. * java.lang.Object) checkPermission}
  87. * method that includes a context argument are provided
  88. * for this situation. The
  89. * <code>getSecurityContext</code> method returns a "snapshot"
  90. * of the current calling context. (The default implementation
  91. * returns an AccessControlContext object.) A sample call is
  92. * the following:
  93. *
  94. * <pre>
  95. * Object context = null;
  96. * SecurityManager sm = System.getSecurityManager();
  97. * if (sm != null) context = sm.getSecurityContext();
  98. * </pre>
  99. *
  100. * <p>
  101. * The <code>checkPermission</code> method
  102. * that takes a context object in addition to a permission
  103. * makes access decisions based on that context,
  104. * rather than on that of the current execution thread.
  105. * Code within a different context can thus call that method,
  106. * passing the permission and the
  107. * previously-saved context object. A sample call, using the
  108. * SecurityManager <code>sm</code> obtained as in the previous example,
  109. * is the following:
  110. *
  111. * <pre>
  112. * if (sm != null) sm.checkPermission(permission, context);
  113. * </pre>
  114. *
  115. * <p>Permissions fall into these categories: File, Socket, Net,
  116. * Security, Runtime, Property, AWT, Reflect, and Serializable.
  117. * The classes managing these various
  118. * permission categories are <code>java.io.FilePermission</code>,
  119. * <code>java.net.SocketPermission</code>,
  120. * <code>java.net.NetPermission</code>,
  121. * <code>java.security.SecurityPermission</code>,
  122. * <code>java.lang.RuntimePermission</code>,
  123. * <code>java.util.PropertyPermission</code>,
  124. * <code>java.awt.AWTPermission</code>,
  125. * <code>java.lang.reflect.ReflectPermission</code>, and
  126. * <code>java.io.SerializablePermission</code>.
  127. *
  128. * <p>All but the first two (FilePermission and SocketPermission) are
  129. * subclasses of <code>java.security.BasicPermission</code>, which itself
  130. * is an abstract subclass of the
  131. * top-level class for permissions, which is
  132. * <code>java.security.Permission</code>. BasicPermission defines the
  133. * functionality needed for all permissions that contain a name
  134. * that follows the hierarchical property naming convention
  135. * (for example, "exitVM", "setFactory", "queuePrintJob", etc).
  136. * An asterisk
  137. * may appear at the end of the name, following a ".", or by itself, to
  138. * signify a wildcard match. For example: "a.*" or "*" is valid,
  139. * "*a" or "a*b" is not valid.
  140. *
  141. * <p>FilePermission and SocketPermission are subclasses of the
  142. * top-level class for permissions
  143. * (<code>java.security.Permission</code>). Classes like these
  144. * that have a more complicated name syntax than that used by
  145. * BasicPermission subclass directly from Permission rather than from
  146. * BasicPermission. For example,
  147. * for a <code>java.io.FilePermission</code> object, the permission name is
  148. * the path name of a file (or directory).
  149. *
  150. * <p>Some of the permission classes have an "actions" list that tells
  151. * the actions that are permitted for the object. For example,
  152. * for a <code>java.io.FilePermission</code> object, the actions list
  153. * (such as "read, write") specifies which actions are granted for the
  154. * specified file (or for files in the specified directory).
  155. *
  156. * <p>Other permission classes are for "named" permissions -
  157. * ones that contain a name but no actions list; you either have the
  158. * named permission or you don't.
  159. *
  160. * <p>Note: There is also a <code>java.security.AllPermission</code>
  161. * permission that implies all permissions. It exists to simplify the work
  162. * of system administrators who might need to perform multiple
  163. * tasks that require all (or numerous) permissions.
  164. * <p>
  165. * See <a href ="../../../guide/security/permissions.html">
  166. * Permissions in the Java 2 SDK</a> for permission-related information.
  167. * This document includes, for example, a table listing the various SecurityManager
  168. * <code>check</code> methods and the permission(s) the default
  169. * implementation of each such method requires.
  170. * It also contains a table of all the version 1.2 methods
  171. * that require permissions, and for each such method tells
  172. * which permission it requires.
  173. * <p>
  174. * For more information about <code>SecurityManager</code> changes made in
  175. * the Java 2 SDK and advice regarding porting of 1.1-style security managers,
  176. * see the <a href="../../../guide/security/index.html">security documentation</a>.
  177. *
  178. * @author Arthur van Hoff
  179. * @author Roland Schemers
  180. *
  181. * @version 1.129, 01/23/03
  182. * @see java.lang.ClassLoader
  183. * @see java.lang.SecurityException
  184. * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
  185. * checkTopLevelWindow
  186. * @see java.lang.System#getSecurityManager() getSecurityManager
  187. * @see java.lang.System#setSecurityManager(java.lang.SecurityManager)
  188. * setSecurityManager
  189. * @see java.security.AccessController AccessController
  190. * @see java.security.AccessControlContext AccessControlContext
  191. * @see java.security.AccessControlException AccessControlException
  192. * @see java.security.Permission
  193. * @see java.security.BasicPermission
  194. * @see java.io.FilePermission
  195. * @see java.net.SocketPermission
  196. * @see java.util.PropertyPermission
  197. * @see java.lang.RuntimePermission
  198. * @see java.awt.AWTPermission
  199. * @see java.security.Policy Policy
  200. * @see java.security.SecurityPermission SecurityPermission
  201. * @see java.security.ProtectionDomain
  202. *
  203. * @since JDK1.0
  204. */
  205. public
  206. class SecurityManager {
  207. /**
  208. * This field is <code>true</code> if there is a security check in
  209. * progress; <code>false</code> otherwise.
  210. *
  211. * @deprecated This type of security checking is not recommended.
  212. * It is recommended that the <code>checkPermission</code>
  213. * call be used instead.
  214. */
  215. protected boolean inCheck;
  216. /*
  217. * Have we been initialized. Effective against finalizer attacks.
  218. */
  219. private boolean initialized = false;
  220. /**
  221. * returns true if the current context has been granted AllPermission
  222. */
  223. private boolean hasAllPermission()
  224. {
  225. try {
  226. checkPermission(SecurityConstants.ALL_PERMISSION);
  227. return true;
  228. } catch (SecurityException se) {
  229. return false;
  230. }
  231. }
  232. /**
  233. * Tests if there is a security check in progress.
  234. *
  235. * @return the value of the <code>inCheck</code> field. This field
  236. * should contain <code>true</code> if a security check is
  237. * in progress,
  238. * <code>false</code> otherwise.
  239. * @see java.lang.SecurityManager#inCheck
  240. * @deprecated This type of security checking is not recommended.
  241. * It is recommended that the <code>checkPermission</code>
  242. * call be used instead.
  243. */
  244. public boolean getInCheck() {
  245. return inCheck;
  246. }
  247. /**
  248. * Constructs a new <code>SecurityManager</code>.
  249. *
  250. * <p> If there is a security manager already installed, this method first
  251. * calls the security manager's <code>checkPermission</code> method
  252. * with the <code>RuntimePermission("createSecurityManager")</code>
  253. * permission to ensure the calling thread has permission to create a new
  254. * security manager.
  255. * This may result in throwing a <code>SecurityException</code>.
  256. *
  257. * @exception java.lang.SecurityException if a security manager already
  258. * exists and its <code>checkPermission</code> method
  259. * doesn't allow creation of a new security manager.
  260. * @see java.lang.System#getSecurityManager()
  261. * @see #checkPermission(java.security.Permission) checkPermission
  262. * @see java.lang.RuntimePermission
  263. */
  264. public SecurityManager() {
  265. synchronized(SecurityManager.class) {
  266. SecurityManager sm = System.getSecurityManager();
  267. if (sm != null) {
  268. // ask the currently installed security manager if we
  269. // can create a new one.
  270. sm.checkPermission(new RuntimePermission
  271. ("createSecurityManager"));
  272. }
  273. initialized = true;
  274. }
  275. }
  276. /**
  277. * Returns the current execution stack as an array of classes.
  278. * <p>
  279. * The length of the array is the number of methods on the execution
  280. * stack. The element at index <code>0</code> is the class of the
  281. * currently executing method, the element at index <code>1</code> is
  282. * the class of that method's caller, and so on.
  283. *
  284. * @return the execution stack.
  285. */
  286. protected native Class[] getClassContext();
  287. /**
  288. * Returns the class loader of the most recently executing method from
  289. * a class defined using a non-system class loader. A non-system
  290. * class loader is defined as being a class loader that is not equal to
  291. * the system class loader (as returned
  292. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  293. * <p>
  294. * This method will return
  295. * <code>null</code> in the following three cases:<p>
  296. * <ol>
  297. * <li>All methods on the execution stack are from classes
  298. * defined using the system class loader or one of its ancestors.
  299. *
  300. * <li>All methods on the execution stack up to the first
  301. * "privileged" caller
  302. * (see {@link java.security.AccessController#doPrivileged})
  303. * are from classes
  304. * defined using the system class loader or one of its ancestors.
  305. *
  306. * <li> A call to <code>checkPermission</code> with
  307. * <code>java.security.AllPermission</code> does not
  308. * result in a SecurityException.
  309. *
  310. * </ol>
  311. *
  312. * @return the class loader of the most recent occurrence on the stack
  313. * of a method from a class defined using a non-system class
  314. * loader.
  315. *
  316. * @deprecated This type of security checking is not recommended.
  317. * It is recommended that the <code>checkPermission</code>
  318. * call be used instead.
  319. *
  320. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  321. * @see #checkPermission(java.security.Permission) checkPermission
  322. */
  323. protected ClassLoader currentClassLoader()
  324. {
  325. ClassLoader cl = currentClassLoader0();
  326. if ((cl != null) && hasAllPermission())
  327. cl = null;
  328. return cl;
  329. }
  330. private native ClassLoader currentClassLoader0();
  331. /**
  332. * Returns the class of the most recently executing method from
  333. * a class defined using a non-system class loader. A non-system
  334. * class loader is defined as being a class loader that is not equal to
  335. * the system class loader (as returned
  336. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  337. * <p>
  338. * This method will return
  339. * <code>null</code> in the following three cases:<p>
  340. * <ol>
  341. * <li>All methods on the execution stack are from classes
  342. * defined using the system class loader or one of its ancestors.
  343. *
  344. * <li>All methods on the execution stack up to the first
  345. * "privileged" caller
  346. * (see {@link java.security.AccessController#doPrivileged})
  347. * are from classes
  348. * defined using the system class loader or one of its ancestors.
  349. *
  350. * <li> A call to <code>checkPermission</code> with
  351. * <code>java.security.AllPermission</code> does not
  352. * result in a SecurityException.
  353. *
  354. * </ol>
  355. *
  356. * @return the class of the most recent occurrence on the stack
  357. * of a method from a class defined using a non-system class
  358. * loader.
  359. *
  360. * @deprecated This type of security checking is not recommended.
  361. * It is recommended that the <code>checkPermission</code>
  362. * call be used instead.
  363. *
  364. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  365. * @see #checkPermission(java.security.Permission) checkPermission
  366. */
  367. protected Class currentLoadedClass() {
  368. Class c = currentLoadedClass0();
  369. if ((c != null) && hasAllPermission())
  370. c = null;
  371. return c;
  372. }
  373. /**
  374. * Returns the stack depth of the specified class.
  375. *
  376. * @param name the fully qualified name of the class to search for.
  377. * @return the depth on the stack frame of the first occurrence of a
  378. * method from a class with the specified name;
  379. * <code>-1</code> if such a frame cannot be found.
  380. * @deprecated This type of security checking is not recommended.
  381. * It is recommended that the <code>checkPermission</code>
  382. * call be used instead.
  383. *
  384. */
  385. protected native int classDepth(String name);
  386. /**
  387. * Returns the stack depth of the most recently executing method
  388. * from a class defined using a non-system class loader. A non-system
  389. * class loader is defined as being a class loader that is not equal to
  390. * the system class loader (as returned
  391. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  392. * <p>
  393. * This method will return
  394. * -1 in the following three cases:<p>
  395. * <ol>
  396. * <li>All methods on the execution stack are from classes
  397. * defined using the system class loader or one of its ancestors.
  398. *
  399. * <li>All methods on the execution stack up to the first
  400. * "privileged" caller
  401. * (see {@link java.security.AccessController#doPrivileged})
  402. * are from classes
  403. * defined using the system class loader or one of its ancestors.
  404. *
  405. * <li> A call to <code>checkPermission</code> with
  406. * <code>java.security.AllPermission</code> does not
  407. * result in a SecurityException.
  408. *
  409. * </ol>
  410. *
  411. * @return the depth on the stack frame of the most recent occurrence of
  412. * a method from a class defined using a non-system class loader.
  413. *
  414. * @deprecated This type of security checking is not recommended.
  415. * It is recommended that the <code>checkPermission</code>
  416. * call be used instead.
  417. *
  418. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  419. * @see #checkPermission(java.security.Permission) checkPermission
  420. */
  421. protected int classLoaderDepth()
  422. {
  423. int depth = classLoaderDepth0();
  424. if (depth != -1) {
  425. if (hasAllPermission())
  426. depth = -1;
  427. else
  428. depth--; // make sure we don't include ourself
  429. }
  430. return depth;
  431. }
  432. private native int classLoaderDepth0();
  433. /**
  434. * Tests if a method from a class with the specified
  435. * name is on the execution stack.
  436. *
  437. * @param name the fully qualified name of the class.
  438. * @return <code>true</code> if a method from a class with the specified
  439. * name is on the execution stack; <code>false</code> otherwise.
  440. * @deprecated This type of security checking is not recommended.
  441. * It is recommended that the <code>checkPermission</code>
  442. * call be used instead.
  443. */
  444. protected boolean inClass(String name) {
  445. return classDepth(name) >= 0;
  446. }
  447. /**
  448. * Basically, tests if a method from a class defined using a
  449. * class loader is on the execution stack.
  450. *
  451. * @return <code>true</code> if a call to <code>currentClassLoader</code>
  452. * has a non-null return value.
  453. *
  454. * @deprecated This type of security checking is not recommended.
  455. * It is recommended that the <code>checkPermission</code>
  456. * call be used instead.
  457. * @see #currentClassLoader() currentClassLoader
  458. */
  459. protected boolean inClassLoader() {
  460. return currentClassLoader() != null;
  461. }
  462. /**
  463. * Creates an object that encapsulates the current execution
  464. * environment. The result of this method is used, for example, by the
  465. * three-argument <code>checkConnect</code> method and by the
  466. * two-argument <code>checkRead</code> method.
  467. * These methods are needed because a trusted method may be called
  468. * on to read a file or open a socket on behalf of another method.
  469. * The trusted method needs to determine if the other (possibly
  470. * untrusted) method would be allowed to perform the operation on its
  471. * own.
  472. * <p> The default implementation of this method is to return
  473. * an <code>AccessControlContext</code> object.
  474. *
  475. * @return an implementation-dependent object that encapsulates
  476. * sufficient information about the current execution environment
  477. * to perform some security checks later.
  478. * @see java.lang.SecurityManager#checkConnect(java.lang.String, int,
  479. * java.lang.Object) checkConnect
  480. * @see java.lang.SecurityManager#checkRead(java.lang.String,
  481. * java.lang.Object) checkRead
  482. * @see java.security.AccessControlContext AccessControlContext
  483. */
  484. public Object getSecurityContext() {
  485. return AccessController.getContext();
  486. }
  487. /**
  488. * Throws a <code>SecurityException</code> if the requested
  489. * access, specified by the given permission, is not permitted based
  490. * on the security policy currently in effect.
  491. * <p>
  492. * This method calls <code>AccessController.checkPermission</code>
  493. * with the given permission.
  494. *
  495. * @param perm the requested permission.
  496. * @exception SecurityException if access is not permitted based on
  497. * the current security policy.
  498. * @exception NullPointerException if the permission argument is
  499. * <code>null</code>.
  500. * @since 1.2
  501. */
  502. public void checkPermission(Permission perm) {
  503. java.security.AccessController.checkPermission(perm);
  504. }
  505. /**
  506. * Throws a <code>SecurityException</code> if the
  507. * specified security context is denied access to the resource
  508. * specified by the given permission.
  509. * The context must be a security
  510. * context returned by a previous call to
  511. * <code>getSecurityContext</code> and the access control
  512. * decision is based upon the configured security policy for
  513. * that security context.
  514. * <p>
  515. * If <code>context</code> is an instance of
  516. * <code>AccessControlContext</code> then the
  517. * <code>AccessControlContext.checkPermission</code> method is
  518. * invoked with the specified permission.
  519. * <p>
  520. * If <code>context</code> is not an instance of
  521. * <code>AccessControlContext</code> then a
  522. * <code>SecurityException</code> is thrown.
  523. *
  524. * @param perm the specified permission
  525. * @param context a system-dependent security context.
  526. * @exception SecurityException if the specified security context
  527. * is not an instance of <code>AccessControlContext</code>
  528. * (e.g., is <code>null</code>), or is denied access to the
  529. * resource specified by the given permission.
  530. * @exception NullPointerException if the permission argument is
  531. * <code>null</code>.
  532. * @see java.lang.SecurityManager#getSecurityContext()
  533. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  534. * @since 1.2
  535. */
  536. public void checkPermission(Permission perm, Object context) {
  537. if (context instanceof AccessControlContext) {
  538. ((AccessControlContext)context).checkPermission(perm);
  539. } else {
  540. throw new SecurityException();
  541. }
  542. }
  543. /**
  544. * Throws a <code>SecurityException</code> if the
  545. * calling thread is not allowed to create a new class loader.
  546. * <p>
  547. * This method calls <code>checkPermission</code> with the
  548. * <code>RuntimePermission("createClassLoader")</code>
  549. * permission.
  550. * <p>
  551. * If you override this method, then you should make a call to
  552. * <code>super.checkCreateClassLoader</code>
  553. * at the point the overridden method would normally throw an
  554. * exception.
  555. *
  556. * @exception SecurityException if the calling thread does not
  557. * have permission
  558. * to create a new class loader.
  559. * @see java.lang.ClassLoader#ClassLoader()
  560. * @see #checkPermission(java.security.Permission) checkPermission
  561. */
  562. public void checkCreateClassLoader() {
  563. checkPermission(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
  564. }
  565. /**
  566. * reference to the root thread group, used for the checkAccess
  567. * methods.
  568. */
  569. private static ThreadGroup rootGroup = getRootGroup();
  570. private static ThreadGroup getRootGroup() {
  571. ThreadGroup root = Thread.currentThread().getThreadGroup();
  572. while (root.getParent() != null) {
  573. root = root.getParent();
  574. }
  575. return root;
  576. }
  577. /**
  578. * Throws a <code>SecurityException</code> if the
  579. * calling thread is not allowed to modify the thread argument.
  580. * <p>
  581. * This method is invoked for the current security manager by the
  582. * <code>stop</code>, <code>suspend</code>, <code>resume</code>,
  583. * <code>setPriority</code>, <code>setName</code>, and
  584. * <code>setDaemon</code> methods of class <code>Thread</code>.
  585. * <p>
  586. * If the thread argument is a system thread (belongs to
  587. * the thread group with a <code>null</code> parent) then
  588. * this method calls <code>checkPermission</code> with the
  589. * <code>RuntimePermission("modifyThread")</code> permission.
  590. * If the thread argument is <i>not</i> a system thread,
  591. * this method just returns silently.
  592. * <p>
  593. * Applications that want a stricter policy should override this
  594. * method. If this method is overridden, the method that overrides
  595. * it should additionally check to see if the calling thread has the
  596. * <code>RuntimePermission("modifyThread")</code> permission, and
  597. * if so, return silently. This is to ensure that code granted
  598. * that permission (such as the SDK itself) is allowed to
  599. * manipulate any thread.
  600. * <p>
  601. * If this method is overridden, then
  602. * <code>super.checkAccess</code> should
  603. * be called by the first statement in the overridden method, or the
  604. * equivalent security check should be placed in the overridden method.
  605. *
  606. * @param t the thread to be checked.
  607. * @exception SecurityException if the calling thread does not have
  608. * permission to modify the thread.
  609. * @exception NullPointerException if the thread argument is
  610. * <code>null</code>.
  611. * @see java.lang.Thread#resume() resume
  612. * @see java.lang.Thread#setDaemon(boolean) setDaemon
  613. * @see java.lang.Thread#setName(java.lang.String) setName
  614. * @see java.lang.Thread#setPriority(int) setPriority
  615. * @see java.lang.Thread#stop() stop
  616. * @see java.lang.Thread#suspend() suspend
  617. * @see #checkPermission(java.security.Permission) checkPermission
  618. */
  619. public void checkAccess(Thread t) {
  620. if (t == null) {
  621. throw new NullPointerException("thread can't be null");
  622. }
  623. if (t.getThreadGroup() == rootGroup) {
  624. checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION);
  625. } else {
  626. // just return
  627. }
  628. }
  629. /**
  630. * Throws a <code>SecurityException</code> if the
  631. * calling thread is not allowed to modify the thread group argument.
  632. * <p>
  633. * This method is invoked for the current security manager when a
  634. * new child thread or child thread group is created, and by the
  635. * <code>setDaemon</code>, <code>setMaxPriority</code>,
  636. * <code>stop</code>, <code>suspend</code>, <code>resume</code>, and
  637. * <code>destroy</code> methods of class <code>ThreadGroup</code>.
  638. * <p>
  639. * If the thread group argument is the system thread group (
  640. * has a <code>null</code> parent) then
  641. * this method calls <code>checkPermission</code> with the
  642. * <code>RuntimePermission("modifyThreadGroup")</code> permission.
  643. * If the thread group argument is <i>not</i> the system thread group,
  644. * this method just returns silently.
  645. * <p>
  646. * Applications that want a stricter policy should override this
  647. * method. If this method is overridden, the method that overrides
  648. * it should additionally check to see if the calling thread has the
  649. * <code>RuntimePermission("modifyThreadGroup")</code> permission, and
  650. * if so, return silently. This is to ensure that code granted
  651. * that permission (such as the SDK itself) is allowed to
  652. * manipulate any thread.
  653. * <p>
  654. * If this method is overridden, then
  655. * <code>super.checkAccess</code> should
  656. * be called by the first statement in the overridden method, or the
  657. * equivalent security check should be placed in the overridden method.
  658. *
  659. * @param g the thread group to be checked.
  660. * @exception SecurityException if the calling thread does not have
  661. * permission to modify the thread group.
  662. * @exception NullPointerException if the thread group argument is
  663. * <code>null</code>.
  664. * @see java.lang.ThreadGroup#destroy() destroy
  665. * @see java.lang.ThreadGroup#resume() resume
  666. * @see java.lang.ThreadGroup#setDaemon(boolean) setDaemon
  667. * @see java.lang.ThreadGroup#setMaxPriority(int) setMaxPriority
  668. * @see java.lang.ThreadGroup#stop() stop
  669. * @see java.lang.ThreadGroup#suspend() suspend
  670. * @see #checkPermission(java.security.Permission) checkPermission
  671. */
  672. public void checkAccess(ThreadGroup g) {
  673. if (g == null) {
  674. throw new NullPointerException("thread group can't be null");
  675. }
  676. if (g == rootGroup) {
  677. checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
  678. } else {
  679. // just return
  680. }
  681. }
  682. /**
  683. * Throws a <code>SecurityException</code> if the
  684. * calling thread is not allowed to cause the Java Virtual Machine to
  685. * halt with the specified status code.
  686. * <p>
  687. * This method is invoked for the current security manager by the
  688. * <code>exit</code> method of class <code>Runtime</code>. A status
  689. * of <code>0</code> indicates success; other values indicate various
  690. * errors.
  691. * <p>
  692. * This method calls <code>checkPermission</code> with the
  693. * <code>RuntimePermission("exitVM")</code> permission.
  694. * <p>
  695. * If you override this method, then you should make a call to
  696. * <code>super.checkExit</code>
  697. * at the point the overridden method would normally throw an
  698. * exception.
  699. *
  700. * @param status the exit status.
  701. * @exception SecurityException if the calling thread does not have
  702. * permission to halt the Java Virtual Machine with
  703. * the specified status.
  704. * @see java.lang.Runtime#exit(int) exit
  705. * @see #checkPermission(java.security.Permission) checkPermission
  706. */
  707. public void checkExit(int status) {
  708. checkPermission(new RuntimePermission("exitVM"));
  709. }
  710. /**
  711. * Throws a <code>SecurityException</code> if the
  712. * calling thread is not allowed to create a subprocess.
  713. * <p>
  714. * This method is invoked for the current security manager by the
  715. * <code>exec</code> methods of class <code>Runtime</code>.
  716. * <p>
  717. * This method calls <code>checkPermission</code> with the
  718. * <code>FilePermission(cmd,"execute")</code> permission
  719. * if cmd is an absolute path, otherwise it calls
  720. * <code>checkPermission</code> with
  721. * <code>FilePermission("<<ALL FILES>>","execute")</code>.
  722. * <p>
  723. * If you override this method, then you should make a call to
  724. * <code>super.checkExec</code>
  725. * at the point the overridden method would normally throw an
  726. * exception.
  727. *
  728. * @param cmd the specified system command.
  729. * @exception SecurityException if the calling thread does not have
  730. * permission to create a subprocess.
  731. * @exception NullPointerException if the <code>cmd</code> argument is
  732. * <code>null</code>.
  733. * @see java.lang.Runtime#exec(java.lang.String)
  734. * @see java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  735. * @see java.lang.Runtime#exec(java.lang.String[])
  736. * @see java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  737. * @see #checkPermission(java.security.Permission) checkPermission
  738. */
  739. public void checkExec(String cmd) {
  740. File f = new File(cmd);
  741. if (f.isAbsolute()) {
  742. checkPermission(new FilePermission(cmd,
  743. SecurityConstants.FILE_EXECUTE_ACTION));
  744. } else {
  745. checkPermission(new FilePermission("<<ALL FILES>>",
  746. SecurityConstants.FILE_EXECUTE_ACTION));
  747. }
  748. }
  749. /**
  750. * Throws a <code>SecurityException</code> if the
  751. * calling thread is not allowed to dynamic link the library code
  752. * specified by the string argument file. The argument is either a
  753. * simple library name or a complete filename.
  754. * <p>
  755. * This method is invoked for the current security manager by
  756. * methods <code>load</code> and <code>loadLibrary</code> of class
  757. * <code>Runtime</code>.
  758. * <p>
  759. * This method calls <code>checkPermission</code> with the
  760. * <code>RuntimePermission("loadLibrary."+lib)</code> permission.
  761. * <p>
  762. * If you override this method, then you should make a call to
  763. * <code>super.checkLink</code>
  764. * at the point the overridden method would normally throw an
  765. * exception.
  766. *
  767. * @param lib the name of the library.
  768. * @exception SecurityException if the calling thread does not have
  769. * permission to dynamically link the library.
  770. * @exception NullPointerException if the <code>lib</code> argument is
  771. * <code>null</code>.
  772. * @see java.lang.Runtime#load(java.lang.String)
  773. * @see java.lang.Runtime#loadLibrary(java.lang.String)
  774. * @see #checkPermission(java.security.Permission) checkPermission
  775. */
  776. public void checkLink(String lib) {
  777. if (lib == null) {
  778. throw new NullPointerException("library can't be null");
  779. }
  780. checkPermission(new RuntimePermission("loadLibrary."+lib));
  781. }
  782. /**
  783. * Throws a <code>SecurityException</code> if the
  784. * calling thread is not allowed to read from the specified file
  785. * descriptor.
  786. * <p>
  787. * This method calls <code>checkPermission</code> with the
  788. * <code>RuntimePermission("readFileDescriptor")</code>
  789. * permission.
  790. * <p>
  791. * If you override this method, then you should make a call to
  792. * <code>super.checkRead</code>
  793. * at the point the overridden method would normally throw an
  794. * exception.
  795. *
  796. * @param fd the system-dependent file descriptor.
  797. * @exception SecurityException if the calling thread does not have
  798. * permission to access the specified file descriptor.
  799. * @exception NullPointerException if the file descriptor argument is
  800. * <code>null</code>.
  801. * @see java.io.FileDescriptor
  802. * @see #checkPermission(java.security.Permission) checkPermission
  803. */
  804. public void checkRead(FileDescriptor fd) {
  805. if (fd == null) {
  806. throw new NullPointerException("file descriptor can't be null");
  807. }
  808. checkPermission(new RuntimePermission("readFileDescriptor"));
  809. }
  810. /**
  811. * Throws a <code>SecurityException</code> if the
  812. * calling thread is not allowed to read the file specified by the
  813. * string argument.
  814. * <p>
  815. * This method calls <code>checkPermission</code> with the
  816. * <code>FilePermission(file,"read")</code> permission.
  817. * <p>
  818. * If you override this method, then you should make a call to
  819. * <code>super.checkRead</code>
  820. * at the point the overridden method would normally throw an
  821. * exception.
  822. *
  823. * @param file the system-dependent file name.
  824. * @exception SecurityException if the calling thread does not have
  825. * permission to access the specified file.
  826. * @exception NullPointerException if the <code>file</code> argument is
  827. * <code>null</code>.
  828. * @see #checkPermission(java.security.Permission) checkPermission
  829. */
  830. public void checkRead(String file) {
  831. checkPermission(new FilePermission(file,
  832. SecurityConstants.FILE_READ_ACTION));
  833. }
  834. /**
  835. * Throws a <code>SecurityException</code> if the
  836. * specified security context is not allowed to read the file
  837. * specified by the string argument. The context must be a security
  838. * context returned by a previous call to
  839. * <code>getSecurityContext</code>.
  840. * <p> If <code>context</code> is an instance of
  841. * <code>AccessControlContext</code> then the
  842. * <code>AccessControlContext.checkPermission</code> method will
  843. * be invoked with the <code>FilePermission(file,"read")</code> permission.
  844. * <p> If <code>context</code> is not an instance of
  845. * <code>AccessControlContext</code> then a
  846. * <code>SecurityException</code> is thrown.
  847. * <p>
  848. * If you override this method, then you should make a call to
  849. * <code>super.checkRead</code>
  850. * at the point the overridden method would normally throw an
  851. * exception.
  852. *
  853. * @param file the system-dependent filename.
  854. * @param context a system-dependent security context.
  855. * @exception SecurityException if the specified security context
  856. * is not an instance of <code>AccessControlContext</code>
  857. * (e.g., is <code>null</code>), or does not have permission
  858. * to read the specified file.
  859. * @exception NullPointerException if the <code>file</code> argument is
  860. * <code>null</code>.
  861. * @see java.lang.SecurityManager#getSecurityContext()
  862. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  863. */
  864. public void checkRead(String file, Object context) {
  865. checkPermission(
  866. new FilePermission(file, SecurityConstants.FILE_READ_ACTION),
  867. context);
  868. }
  869. /**
  870. * Throws a <code>SecurityException</code> if the
  871. * calling thread is not allowed to write to the specified file
  872. * descriptor.
  873. * <p>
  874. * This method calls <code>checkPermission</code> with the
  875. * <code>RuntimePermission("writeFileDescriptor")</code>
  876. * permission.
  877. * <p>
  878. * If you override this method, then you should make a call to
  879. * <code>super.checkWrite</code>
  880. * at the point the overridden method would normally throw an
  881. * exception.
  882. *
  883. * @param fd the system-dependent file descriptor.
  884. * @exception SecurityException if the calling thread does not have
  885. * permission to access the specified file descriptor.
  886. * @exception NullPointerException if the file descriptor argument is
  887. * <code>null</code>.
  888. * @see java.io.FileDescriptor
  889. * @see #checkPermission(java.security.Permission) checkPermission
  890. */
  891. public void checkWrite(FileDescriptor fd) {
  892. if (fd == null) {
  893. throw new NullPointerException("file descriptor can't be null");
  894. }
  895. checkPermission(new RuntimePermission("writeFileDescriptor"));
  896. }
  897. /**
  898. * Throws a <code>SecurityException</code> if the
  899. * calling thread is not allowed to write to the file specified by
  900. * the string argument.
  901. * <p>
  902. * This method calls <code>checkPermission</code> with the
  903. * <code>FilePermission(file,"write")</code> permission.
  904. * <p>
  905. * If you override this method, then you should make a call to
  906. * <code>super.checkWrite</code>
  907. * at the point the overridden method would normally throw an
  908. * exception.
  909. *
  910. * @param file the system-dependent filename.
  911. * @exception SecurityException if the calling thread does not
  912. * have permission to access the specified file.
  913. * @exception NullPointerException if the <code>file</code> argument is
  914. * <code>null</code>.
  915. * @see #checkPermission(java.security.Permission) checkPermission
  916. */
  917. public void checkWrite(String file) {
  918. checkPermission(new FilePermission(file,
  919. SecurityConstants.FILE_WRITE_ACTION));
  920. }
  921. /**
  922. * Throws a <code>SecurityException</code> if the
  923. * calling thread is not allowed to delete the specified file.
  924. * <p>
  925. * This method is invoked for the current security manager by the
  926. * <code>delete</code> method of class <code>File</code>.
  927. * <p>
  928. * This method calls <code>checkPermission</code> with the
  929. * <code>FilePermission(file,"delete")</code> permission.
  930. * <p>
  931. * If you override this method, then you should make a call to
  932. * <code>super.checkDelete</code>
  933. * at the point the overridden method would normally throw an
  934. * exception.
  935. *
  936. * @param file the system-dependent filename.
  937. * @exception SecurityException if the calling thread does not
  938. * have permission to delete the file.
  939. * @exception NullPointerException if the <code>file</code> argument is
  940. * <code>null</code>.
  941. * @see java.io.File#delete()
  942. * @see #checkPermission(java.security.Permission) checkPermission
  943. */
  944. public void checkDelete(String file) {
  945. checkPermission(new FilePermission(file,
  946. SecurityConstants.FILE_DELETE_ACTION));
  947. }
  948. /**
  949. * Throws a <code>SecurityException</code> if the
  950. * calling thread is not allowed to open a socket connection to the
  951. * specified host and port number.
  952. * <p>
  953. * A port number of <code>-1</code> indicates that the calling
  954. * method is attempting to determine the IP address of the specified
  955. * host name.
  956. * <p>
  957. * This method calls <code>checkPermission</code> with the
  958. * <code>SocketPermission(host+":"+port,"connect")</code> permission if
  959. * the port is not equal to -1. If the port is equal to -1, then
  960. * it calls <code>checkPermission</code> with the
  961. * <code>SocketPermission(host,"resolve")</code> permission.
  962. * <p>
  963. * If you override this method, then you should make a call to
  964. * <code>super.checkConnect</code>
  965. * at the point the overridden method would normally throw an
  966. * exception.
  967. *
  968. * @param host the host name port to connect to.
  969. * @param port the protocol port to connect to.
  970. * @exception SecurityException if the calling thread does not have
  971. * permission to open a socket connection to the specified
  972. * <code>host</code> and <code>port</code>.
  973. * @exception NullPointerException if the <code>host</code> argument is
  974. * <code>null</code>.
  975. * @see #checkPermission(java.security.Permission) checkPermission
  976. */
  977. public void checkConnect(String host, int port) {
  978. if (host == null) {
  979. throw new NullPointerException("host can't be null");
  980. }
  981. if (!host.startsWith("[") && host.indexOf(':') != -1) {
  982. host = "[" + host + "]";
  983. }
  984. if (port == -1) {
  985. checkPermission(new SocketPermission(host,
  986. SecurityConstants.SOCKET_RESOLVE_ACTION));
  987. } else {
  988. checkPermission(new SocketPermission(host+":"+port,
  989. SecurityConstants.SOCKET_CONNECT_ACTION));
  990. }
  991. }
  992. /**
  993. * Throws a <code>SecurityException</code> if the
  994. * specified security context is not allowed to open a socket
  995. * connection to the specified host and port number.
  996. * <p>
  997. * A port number of <code>-1</code> indicates that the calling
  998. * method is attempting to determine the IP address of the specified
  999. * host name.
  1000. * <p> If <code>context</code> is not an instance of
  1001. * <code>AccessControlContext</code> then a
  1002. * <code>SecurityException</code> is thrown.
  1003. * <p>
  1004. * Otherwise, the port number is checked. If it is not equal
  1005. * to -1, the <code>context</code>'s <code>checkPermission</code>
  1006. * method is called with a
  1007. * <code>SocketPermission(host+":"+port,"connect")</code> permission.
  1008. * If the port is equal to -1, then
  1009. * the <code>context</code>'s <code>checkPermission</code> method
  1010. * is called with a
  1011. * <code>SocketPermission(host,"resolve")</code> permission.
  1012. * <p>
  1013. * If you override this method, then you should make a call to
  1014. * <code>super.checkConnect</code>
  1015. * at the point the overridden method would normally throw an
  1016. * exception.
  1017. *
  1018. * @param host the host name port to connect to.
  1019. * @param port the protocol port to connect to.
  1020. * @param context a system-dependent security context.
  1021. * @exception SecurityException if the specified security context
  1022. * is not an instance of <code>AccessControlContext</code>
  1023. * (e.g., is <code>null</code>), or does not have permission
  1024. * to open a socket connection to the specified
  1025. * <code>host</code> and <code>port</code>.
  1026. * @exception NullPointerException if the <code>host</code> argument is
  1027. * <code>null</code>.
  1028. * @see java.lang.SecurityManager#getSecurityContext()
  1029. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  1030. */
  1031. public void checkConnect(String host, int port, Object context) {
  1032. if (host == null) {
  1033. throw new NullPointerException("host can't be null");
  1034. }
  1035. if (!host.startsWith("[") && host.indexOf(':') != -1) {
  1036. host = "[" + host + "]";
  1037. }
  1038. if (port == -1)
  1039. checkPermission(new SocketPermission(host,
  1040. SecurityConstants.SOCKET_RESOLVE_ACTION),
  1041. context);
  1042. else
  1043. checkPermission(new SocketPermission(host+":"+port,
  1044. SecurityConstants.SOCKET_CONNECT_ACTION),
  1045. context);
  1046. }
  1047. /**
  1048. * Throws a <code>SecurityException</code> if the
  1049. * calling thread is not allowed to wait for a connection request on
  1050. * the specified local port number.
  1051. * <p>
  1052. * If port is not 0, this method calls
  1053. * <code>checkPermission</code> with the
  1054. * <code>SocketPermission("localhost:"+port,"listen")</code>.
  1055. * If port is zero, this method calls <code>checkPermission</code>
  1056. * with <code>SocketPermission("localhost:1024-","listen").</code>
  1057. * <p>
  1058. * If you override this method, then you should make a call to
  1059. * <code>super.checkListen</code>
  1060. * at the point the overridden method would normally throw an
  1061. * exception.
  1062. *
  1063. * @param port the local port.
  1064. * @exception SecurityException if the calling thread does not have
  1065. * permission to listen on the specified port.
  1066. * @see #checkPermission(java.security.Permission) checkPermission
  1067. */
  1068. public void checkListen(int port) {
  1069. if (port == 0) {
  1070. checkPermission(SecurityConstants.LOCAL_LISTEN_PERMISSION);
  1071. } else {
  1072. checkPermission(new SocketPermission("localhost:"+port,
  1073. SecurityConstants.SOCKET_LISTEN_ACTION));
  1074. }
  1075. }
  1076. /**
  1077. * Throws a <code>SecurityException</code> if the
  1078. * calling thread is not permitted to accept a socket connection from
  1079. * the specified host and port number.
  1080. * <p>
  1081. * This method is invoked for the current security manager by the
  1082. * <code>accept</code> method of class <code>ServerSocket</code>.
  1083. * <p>
  1084. * This method calls <code>checkPermission</code> with the
  1085. * <code>SocketPermission(host+":"+port,"accept")</code> permission.
  1086. * <p>
  1087. * If you override this method, then you should make a call to
  1088. * <code>super.checkAccept</code>
  1089. * at the point the overridden method would normally throw an
  1090. * exception.
  1091. *
  1092. * @param host the host name of the socket connection.
  1093. * @param port the port number of the socket connection.
  1094. * @exception SecurityException if the calling thread does not have
  1095. * permission to accept the connection.
  1096. * @exception NullPointerException if the <code>host</code> argument is
  1097. * <code>null</code>.
  1098. * @see java.net.ServerSocket#accept()
  1099. * @see #checkPermission(java.security.Permission) checkPermission
  1100. */
  1101. public void checkAccept(String host, int port) {
  1102. if (host == null) {
  1103. throw new NullPointerException("host can't be null");
  1104. }
  1105. if (!host.startsWith("[") && host.indexOf(':') != -1) {
  1106. host = "[" + host + "]";
  1107. }
  1108. checkPermission(new SocketPermission(host+":"+port,
  1109. SecurityConstants.SOCKET_ACCEPT_ACTION));
  1110. }
  1111. /**
  1112. * Throws a <code>SecurityException</code> if the
  1113. * calling thread is not allowed to use
  1114. * (join/leave/send/receive) IP multicast.
  1115. * <p>
  1116. * This method calls <code>checkPermission</code> with the
  1117. * <code>java.net.SocketPermission(maddr.getHostAddress(),
  1118. * "accept,connect")</code> permission.
  1119. * <p>
  1120. * If you override this method, then you should make a call to
  1121. * <code>super.checkMulticast</code>
  1122. * at the point the overridden method would normally throw an
  1123. * exception.
  1124. *
  1125. * @param maddr Internet group address to be used.
  1126. * @exception SecurityException if the calling thread is not allowed to
  1127. * use (join/leave/send/receive) IP multicast.
  1128. * @exception NullPointerException if the address argument is
  1129. * <code>null</code>.
  1130. * @since JDK1.1
  1131. * @see #checkPermission(java.security.Permission) checkPermission
  1132. */
  1133. public void checkMulticast(InetAddress maddr) {
  1134. String host = maddr.getHostAddress();
  1135. if (!host.startsWith("[") && host.indexOf(':') != -1) {
  1136. host = "[" + host + "]";
  1137. }
  1138. checkPermission(new SocketPermission(host,
  1139. SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
  1140. }
  1141. /**
  1142. * Throws a <code>SecurityException</code> if the
  1143. * calling thread is not allowed to use
  1144. * (join/leave/send/receive) IP multicast.
  1145. * <p>
  1146. * This method calls <code>checkPermission</code> with the
  1147. * <code>java.net.SocketPermission(maddr.getHostAddress(),
  1148. * "accept,connect")</code> permission.
  1149. * <p>
  1150. * If you override this method, then you should make a call to
  1151. * <code>super.checkMulticast</code>
  1152. * at the point the overridden method would normally throw an
  1153. * exception.
  1154. *
  1155. * @param maddr Internet group address to be used.
  1156. * @param ttl value in use, if it is multicast send.
  1157. * Note: this particular implementation does not use the ttl
  1158. * parameter.
  1159. * @exception SecurityException if the calling thread is not allowed to
  1160. * use (join/leave/send/receive) IP multicast.
  1161. * @exception NullPointerException if the address argument is
  1162. * <code>null</code>.
  1163. * @since JDK1.1
  1164. * @deprecated Use #checkPermission(java.security.Permission) instead
  1165. * @see #checkPermission(java.security.Permission) checkPermission
  1166. */
  1167. public void checkMulticast(InetAddress maddr, byte ttl) {
  1168. String host = maddr.getHostAddress();
  1169. if (!host.startsWith("[") && host.indexOf(':') != -1) {
  1170. host = "[" + host + "]";
  1171. }
  1172. checkPermission(new SocketPermission(host,
  1173. SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
  1174. }
  1175. /**
  1176. * Throws a <code>SecurityException</code> if the
  1177. * calling thread is not allowed to access or modify the system
  1178. * properties.
  1179. * <p>
  1180. * This method is used by the <code>getProperties</code> and
  1181. * <code>setProperties</code> methods of class <code>System</code>.
  1182. * <p>
  1183. * This method calls <code>checkPermission</code> with the
  1184. * <code>PropertyPermission("*", "read,write")</code> permission.
  1185. * <p>
  1186. * If you override this method, then you should make a call to
  1187. * <code>super.checkPropertiesAccess</code>
  1188. * at the point the overridden method would normally throw an
  1189. * exception.
  1190. * <p>
  1191. *
  1192. * @exception SecurityException if the calling thread does not have
  1193. * permission to access or modify the system properties.
  1194. * @see java.lang.System#getProperties()
  1195. * @see java.lang.System#setProperties(java.util.Properties)
  1196. * @see #checkPermission(java.security.Permission) checkPermission
  1197. */
  1198. public void checkPropertiesAccess() {
  1199. checkPermission(new PropertyPermission("*",
  1200. SecurityConstants.PROPERTY_RW_ACTION));
  1201. }
  1202. /**
  1203. * Throws a <code>SecurityException</code> if the
  1204. * calling thread is not allowed to access the system property with
  1205. * the specified <code>key</code> name.
  1206. * <p>
  1207. * This method is used by the <code>getProperty</code> method of
  1208. * class <code>System</code>.
  1209. * <p>
  1210. * This method calls <code>checkPermission</code> with the
  1211. * <code>PropertyPermission(key, "read")</code> permission.
  1212. * <p>
  1213. * <p>
  1214. * If you override this method, then you should make a call to
  1215. * <code>super.checkPropertyAccess</code>
  1216. * at the point the overridden method would normally throw an
  1217. * exception.
  1218. *
  1219. * @param key a system property key.
  1220. *
  1221. * @exception SecurityException if the calling thread does not have
  1222. * permission to access the specified system property.
  1223. * @exception NullPointerException if the <code>key</code> argument is
  1224. * <code>null</code>.
  1225. * @exception IllegalArgumentException if <code>key</code> is empty.
  1226. *
  1227. * @see java.lang.System#getProperty(java.lang.String)
  1228. * @see #checkPermission(java.security.Permission) checkPermission
  1229. */
  1230. public void checkPropertyAccess(String key) {
  1231. checkPermission(new PropertyPermission(key,
  1232. SecurityConstants.PROPERTY_READ_ACTION));
  1233. }
  1234. /**
  1235. * Returns <code>false</code> if the calling
  1236. * thread is not trusted to bring up the top-level window indicated
  1237. * by the <code>window</code> argument. In this case, the caller can
  1238. * still decide to show the window, but the window should include
  1239. * some sort of visual warning. If the method returns
  1240. * <code>true</code>, then the window can be shown without any
  1241. * special restrictions.
  1242. * <p>
  1243. * See class <code>Window</code> for more information on trusted and
  1244. * untrusted windows.
  1245. * <p>
  1246. * This method calls
  1247. * <code>checkPermission</code> with the
  1248. * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission,
  1249. * and returns <code>true</code> if a SecurityException is not thrown,
  1250. * otherwise it returns <code>false</code>.
  1251. * <p>
  1252. * If you override this method, then you should make a call to
  1253. * <code>super.checkTopLevelWindow</code>
  1254. * at the point the overridden method would normally return
  1255. * <code>false</code>, and the value of
  1256. * <code>super.checkTopLevelWindow</code> should
  1257. * be returned.
  1258. *
  1259. * @param window the new window that is being created.
  1260. * @return <code>true</code> if the calling thread is trusted to put up
  1261. * top-level windows; <code>false</code> otherwise.
  1262. * @exception NullPointerException if the <code>window</code> argument is
  1263. * <code>null</code>.
  1264. * @see java.awt.Window
  1265. * @see #checkPermission(java.security.Permission) checkPermission
  1266. */
  1267. public boolean checkTopLevelWindow(Object window) {
  1268. if (window == null) {
  1269. throw new NullPointerException("window can't be null");
  1270. }
  1271. try {
  1272. checkPermission(SecurityConstants.TOPLEVEL_WINDOW_PERMISSION);
  1273. return true;
  1274. } catch (SecurityException se) {
  1275. // just return false
  1276. }
  1277. return false;
  1278. }
  1279. /**
  1280. * Throws a <code>SecurityException</code> if the
  1281. * calling thread is not allowed to initiate a print job request.
  1282. * <p>
  1283. * This method calls
  1284. * <code>checkPermission</code> with the
  1285. * <code>RuntimePermission("queuePrintJob")</code> permission.
  1286. * <p>
  1287. * If you override this method, then you should make a call to
  1288. * <code>super.checkPrintJobAccess</code>
  1289. * at the point the overridden method would normally throw an
  1290. * exception.
  1291. * <p>
  1292. *
  1293. * @exception SecurityException if the calling thread does not have
  1294. * permission to initiate a print job request.
  1295. * @since JDK1.1
  1296. * @see #checkPermission(java.security.Permission) checkPermission
  1297. */
  1298. public void checkPrintJobAccess() {
  1299. checkPermission(new RuntimePermission("queuePrintJob"));
  1300. }
  1301. /**
  1302. * Throws a <code>SecurityException</code> if the
  1303. * calling thread is not allowed to access the system clipboard.
  1304. * <p>
  1305. * This method calls <code>checkPermission</code> with the
  1306. * <code>AWTPermission("accessClipboard")</code>
  1307. * permission.
  1308. * <p>
  1309. * If you override this method, then you should make a call to
  1310. * <code>super.checkSystemClipboardAccess</code>
  1311. * at the point the overridden method would normally throw an
  1312. * exception.
  1313. *
  1314. * @since JDK1.1
  1315. * @exception SecurityException if the calling thread does not have
  1316. * permission to access the system clipboard.
  1317. * @see #checkPermission(java.security.Permission) checkPermission
  1318. */
  1319. public void checkSystemClipboardAccess() {
  1320. checkPermission(SecurityConstants.ACCESS_CLIPBOARD_PERMISSION);
  1321. }
  1322. /**
  1323. * Throws a <code>SecurityException</code> if the
  1324. * calling thread is not allowed to access the AWT event queue.
  1325. * <p>
  1326. * This method calls <code>checkPermission</code> with the
  1327. * <code>AWTPermission("accessEventQueue")</code> permission.
  1328. * <p>
  1329. * If you override this method, then you should make a call to
  1330. * <code>super.checkAwtEventQueueAccess</code>
  1331. * at the point the overridden method would normally throw an
  1332. * exception.
  1333. *
  1334. * @since JDK1.1
  1335. * @exception SecurityException if the calling thread does not have
  1336. * permission to access the AWT event queue.
  1337. * @see #checkPermission(java.security.Permission) checkPermission
  1338. */
  1339. public void checkAwtEventQueueAccess() {
  1340. checkPermission(SecurityConstants.CHECK_AWT_EVENTQUEUE_PERMISSION);
  1341. }
  1342. /*
  1343. * We have an initial invalid bit (initially false) for the class
  1344. * variables which tell if the cache is valid. If the underlying
  1345. * java.security.Security property changes via setProperty(), the
  1346. * Security class uses reflection to change the variable and thus
  1347. * invalidate the cache.
  1348. *
  1349. * Locking is handled by synchronization to the
  1350. * packageAccess/packageDefinition variables. They are only
  1351. * used in this class.
  1352. */
  1353. private static boolean packageAccessValid = false;
  1354. private static String[] packageAccess = new String[0];
  1355. private static boolean packageDefinitionValid = false;
  1356. private static String[] packageDefinition = new String[0];
  1357. private static String[] getPackages(String p) {
  1358. String packages[] = null;
  1359. if (p != null && !p.equals("")) {
  1360. java.util.StringTokenizer tok =
  1361. new java.util.StringTokenizer(p, ",");
  1362. int n = tok.countTokens();
  1363. if (n > 0) {
  1364. packages = new String[n];
  1365. int i = 0;
  1366. while (tok.hasMoreElements()) {
  1367. String s = tok.nextToken().trim();
  1368. packages[i++] = s;
  1369. }
  1370. }
  1371. }
  1372. if (packages == null)
  1373. packages = new String[0];
  1374. return packages;
  1375. }
  1376. /**
  1377. * Throws a <code>SecurityException</code> if the
  1378. * calling thread is not allowed to access the package specified by
  1379. * the argument.
  1380. * <p>
  1381. * This method is used by the <code>loadClass</code> method of class
  1382. * loaders.
  1383. * <p>
  1384. * This method first gets a list of
  1385. * restricted packages by obtaining a comma-separated list from
  1386. * a call to
  1387. * <code>java.security.Security.getProperty("package.access")</code>,
  1388. * and checks to see if <code>pkg</code> starts with or equals
  1389. * any of the restricted packages. If it does, then
  1390. * <code>checkPermission</code> gets called with the
  1391. * <code>RuntimePermission("accessClassInPackage."+pkg)</code>
  1392. * permission.
  1393. * <p>
  1394. * If this method is overridden, then
  1395. * <code>super.checkPackageAccess</code> should be called
  1396. * as the first line in the overridden method.
  1397. *
  1398. * @param pkg the package name.
  1399. * @exception SecurityException if the calling thread does not have
  1400. * permission to access the specified package.
  1401. * @exception NullPointerException if the package name argument is
  1402. * <code>null</code>.
  1403. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1404. * loadClass
  1405. * @see java.security.Security#getProperty getProperty
  1406. * @see #checkPermission(java.security.Permission) checkPermission
  1407. */
  1408. public void checkPackageAccess(String pkg) {
  1409. if (pkg == null) {
  1410. throw new NullPointerException("package name can't be null");
  1411. }
  1412. synchronized (packageAccess) {
  1413. /*
  1414. * Do we need to update our property array?
  1415. */
  1416. if (!packageAccessValid) {
  1417. String tmpPropertyStr =
  1418. (String) AccessController.doPrivileged(
  1419. new PrivilegedAction() {
  1420. public Object run() {
  1421. return java.security.Security.getProperty(
  1422. "package.access");
  1423. }
  1424. }
  1425. );
  1426. packageAccess = getPackages(tmpPropertyStr);
  1427. packageAccessValid = true;
  1428. }
  1429. /*
  1430. * Traverse the list of packages, check for any matches.
  1431. */
  1432. for (int i = 0; i < packageAccess.length; i++) {
  1433. if (pkg.startsWith(packageAccess[i]) ||
  1434. packageAccess[i].equals(pkg + ".")) {
  1435. checkPermission(
  1436. new RuntimePermission("accessClassInPackage."+pkg));
  1437. }
  1438. }
  1439. } /* synchronized */
  1440. }
  1441. /**
  1442. * Throws a <code>SecurityException</code> if the
  1443. * calling thread is not allowed to define classes in the package
  1444. * specified by the argument.
  1445. * <p>
  1446. * This method is used by the <code>loadClass</code> method of some
  1447. * class loaders.
  1448. * <p>
  1449. * This method first gets a list of restricted packages by
  1450. * obtaining a comma-separated list from a call to
  1451. * <code>java.security.Security.getProperty("package.definition")</code>,
  1452. * and checks to see if <code>pkg</code> starts with or equals
  1453. * any of the restricted packages. If it does, then
  1454. * <code>checkPermission</code> gets called with the
  1455. * <code>RuntimePermission("defineClassInPackage."+pkg)</code>
  1456. * permission.
  1457. * <p>
  1458. * If this method is overridden, then
  1459. * <code>super.checkPackageDefinition</code> should be called
  1460. * as the first line in the overridden method.
  1461. *
  1462. * @param pkg the package name.
  1463. * @exception SecurityException if the calling thread does not have
  1464. * permission to define classes in the specified package.
  1465. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1466. * @see java.security.Security#getProperty getProperty
  1467. * @see #checkPermission(java.security.Permission) checkPermission
  1468. */
  1469. public void checkPackageDefinition(String pkg) {
  1470. if (pkg == null) {
  1471. throw new NullPointerException("package name can't be null");
  1472. }
  1473. synchronized (packageDefinition) {
  1474. /*
  1475. * Do we need to update our property array?
  1476. */
  1477. if (!packageDefinitionValid) {
  1478. String tmpPropertyStr =
  1479. (String) AccessController.doPrivileged(
  1480. new PrivilegedAction() {
  1481. public Object run() {
  1482. return java.security.Security.getProperty(
  1483. "package.definition");
  1484. }
  1485. }
  1486. );
  1487. packageDefinition = getPackages(tmpPropertyStr);
  1488. packageDefinitionValid = true;
  1489. }
  1490. /*
  1491. * Traverse the list of packages, check for any matches.
  1492. */
  1493. for (int i = 0; i < packageDefinition.length; i++) {
  1494. if (pkg.startsWith(packageDefinition[i]) ||
  1495. packageDefinition[i].equals(pkg + ".")) {
  1496. checkPermission(
  1497. new RuntimePermission("defineClassInPackage."+pkg));
  1498. }
  1499. }
  1500. } /* synchronized */
  1501. }
  1502. /**
  1503. * Throws a <code>SecurityException</code> if the
  1504. * calling thread is not allowed to set the socket factory used by
  1505. * <code>ServerSocket</code> or <code>Socket</code>, or the stream
  1506. * handler factory used by <code>URL</code>.
  1507. * <p>
  1508. * This method calls <code>checkPermission</code> with the
  1509. * <code>RuntimePermission("setFactory")</code> permission.
  1510. * <p>
  1511. * If you override this method, then you should make a call to
  1512. * <code>super.checkSetFactory</code>
  1513. * at the point the overridden method would normally throw an
  1514. * exception.
  1515. * <p>
  1516. *
  1517. * @exception SecurityException if the calling thread does not have
  1518. * permission to specify a socket factory or a stream
  1519. * handler factory.
  1520. *
  1521. * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) setSocketFactory
  1522. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) setSocketImplFactory
  1523. * @see java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory) setURLStreamHandlerFactory
  1524. * @see #checkPermission(java.security.Permission) checkPermission
  1525. */
  1526. public void checkSetFactory() {
  1527. checkPermission(new RuntimePermission("setFactory"));
  1528. }
  1529. /**
  1530. * Throws a <code>SecurityException</code> if the
  1531. * calling thread is not allowed to access members.
  1532. * <p>
  1533. * The default policy is to allow access to PUBLIC members, as well
  1534. * as access to classes that have the same class loader as the caller.
  1535. * In all other cases, this method calls <code>checkPermission</code>
  1536. * with the <code>RuntimePermission("accessDeclaredMembers")
  1537. * </code> permission.
  1538. * <p>
  1539. * If this method is overridden, then a call to
  1540. * <code>super.checkMemberAccess</code> cannot be made,
  1541. * as the default implementation of <code>checkMemberAccess</code>
  1542. * relies on the code being checked being at a stack depth of
  1543. * 4.
  1544. *
  1545. * @param clazz the class that reflection is to be performed on.
  1546. *
  1547. * @param which type of access, PUBLIC or DECLARED.
  1548. *
  1549. * @exception SecurityException if the caller does not have
  1550. * permission to access members.
  1551. * @exception NullPointerException if the <code>clazz</code> argument is
  1552. * <code>null</code>.
  1553. * @see java.lang.reflect.Member
  1554. * @since JDK1.1
  1555. * @see #checkPermission(java.security.Permission) checkPermission
  1556. */
  1557. public void checkMemberAccess(Class clazz, int which) {
  1558. if (clazz == null) {
  1559. throw new NullPointerException("class can't be null");
  1560. }
  1561. if (which != Member.PUBLIC) {
  1562. Class stack[] = getClassContext();
  1563. /*
  1564. * stack depth of 4 should be the caller of one of the
  1565. * methods in java.lang.Class that invoke checkMember
  1566. * access. The stack should look like:
  1567. *
  1568. * someCaller [3]
  1569. * java.lang.Class.someReflectionAPI [2]
  1570. * java.lang.Class.checkMemeberAccess [1]
  1571. * SecurityManager.checkMemeberAccess [0]
  1572. *
  1573. */
  1574. if ((stack.length<4) ||
  1575. (stack[3].getClassLoader() != clazz.getClassLoader())) {
  1576. checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
  1577. }
  1578. }
  1579. }
  1580. /**
  1581. * Determines whether the permission with the specified permission target
  1582. * name should be granted or denied.
  1583. *
  1584. * <p> If the requested permission is allowed, this method returns
  1585. * quietly. If denied, a SecurityException is raised.
  1586. *
  1587. * <p> This method creates a <code>SecurityPermission</code> object for
  1588. * the given permission target name and calls <code>checkPermission</code>
  1589. * with it.
  1590. *
  1591. * <p> See the documentation for
  1592. * <code>{@link java.security.SecurityPermission}</code> for
  1593. * a list of possible permission target names.
  1594. *
  1595. * <p> If you override this method, then you should make a call to
  1596. * <code>super.checkSecurityAccess</code>
  1597. * at the point the overridden method would normally throw an
  1598. * exception.
  1599. *
  1600. * @param target the target name of the <code>SecurityPermission</code>.
  1601. *
  1602. * @exception SecurityException if the calling thread does not have
  1603. * permission for the requested access.
  1604. * @exception NullPointerException if <code>target</code> is null.
  1605. * @exception IllegalArgumentException if <code>target</code> is empty.
  1606. *
  1607. * @since JDK1.1
  1608. * @see #checkPermission(java.security.Permission) checkPermission
  1609. */
  1610. public void checkSecurityAccess(String target) {
  1611. checkPermission(new SecurityPermission(target));
  1612. }
  1613. private native Class currentLoadedClass0();
  1614. /**
  1615. * Returns the thread group into which to instantiate any new
  1616. * thread being created at the time this is being called.
  1617. * By default, it returns the thread group of the current
  1618. * thread. This should be overridden by a specific security
  1619. * manager to return the appropriate thread group.
  1620. *
  1621. * @return ThreadGroup that new threads are instantiated into
  1622. * @since JDK1.1
  1623. * @see java.lang.ThreadGroup
  1624. */
  1625. public ThreadGroup getThreadGroup() {
  1626. return Thread.currentThread().getThreadGroup();
  1627. }
  1628. }