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