Java permissions and security policy — official document

3 Permissions and Security Policy

3.1 The Permission Classes

The permission classes represent access to system resources. The java. security. Permission class is an abstract class and is subclassed,as appropriate,to represent specific accesses.

As an example of a permission,the following code can be used to produce a permission to read the file named "abc" in the /tmp directory:

New permissions are subclassed either from the Permission class or one of its subclasses,such as java. security. BasicPermission. Subclassed permissions (other than BasicPermission) generally belong to their own packages. Thus,FilePermission is found in the java. io package.

A crucial abstract method that needs to be implemented for each new class of permission is the implies method. Basically,"a implies b" means that if one is granted permission "a",one is naturally granted permission "b". This is important when making access control decisions.

Associated with the abstract class java. security. Permission are the abstract class named java. security. PermissionCollection and the final class java. security. Permissions.

Class java. security. PermissionCollection represents a collection (i.e.,a set that allows duplicates) of Permission objects for a single category (such as file permissions),for ease of grouping. In cases where permissions can be added to the PermissionCollection object in any order,such as for file permissions,it is crucial that the PermissionCollection object ensure that the correct semantics are followed when the implies function is called.

Class java. security. Permissions represents a collection of collections of Permission objects,or in other words,a super collection of heterogeneous permissions.

Applications are free to add new categories of permissions that the system supports. How to add such application-specific permissions is discussed later in this document.

Now we describe the syntax and semantics of all built-in permissions.

3.1. 1 java. security. Permission

This abstract class is the ancestor of all permissions. It defines the essential functionalities required for all permissions.

Each permission instance is typically generated by passing one or more string parameters to the constructor. In a common case with two parameters,the first parameter is usually "the name of the target" (such as the name of a file for which the permission is aimed),and the second parameter is the action (such as "read" action on a file). Generally,a set of actions can be specified together as a comma-separated composite string.

This class holds a homogeneous collection of permissions. In other words,each instance of the class holds only permissions of the same type.

This class is designed to hold a heterogeneous collection of permissions. Basically,it is a collection of java. security. PermissionCollection objects.

Recall that the internal state of a security policy is normally expressed by the permission objects that are associated with each code source. Given the dynamic nature of Java technology,however,it is possible that when the policy is initialized the actual code that implements a particular permission class has not yet been loaded and defined in the Java application environment. For example,a referenced permission class may be in a JAR file that will later be loaded.

The UnresolvedPermission class is used to hold such "unresolved" permissions. Similarly,the class java. security. UnresolvedPermissionCollection stores a collection of UnresolvedPermission permissions.

During access control checking on a permission of a type that was prevIoUsly unresolved,but whose class has since been loaded,the unresolved permission is "resolved" and the appropriate access control decision is made. That is,a new object of the appropriate class type is instantiated,if possible,based on the information in the UnresolvedPermission. This new object replaces the UnresolvedPermission,which is removed.

If the permission is still unresolvable at this time,the permission is considered invalid,as if it is never granted in a security policy.

The targets for this class can be specified in the following ways,where directory and file names are strings that cannot contain white spaces.

Note that "< >" is a special string denoting all files in the system. On a Unix system,this includes all files under the root directory. On an MS-DOS system,this includes all files on all drives.

The actions are: read, write, delete,and execute. Therefore,the following are valid code samples for creating file permissions:

> The implies method in this class correctly interprets the file system. For example,FilePermission("/-",execute") implies FilePermission("/home/gong/public_html/index.html","read"),and FilePermission("bin/*","execute") implies FilePermission("bin/emacs19.31","execute").

Note: Most of these strings are given in platform-dependent format. For example,to represent read access to the file named "foo" in the "temp" directory on the C drive of a Windows system,you would use

The double backslashes are necessary to represent a single backslash because the strings are processed by a tokenizer (java.io.StreamTokenizer),which allows "\" to be used as an escape string (e.g.,"\n" to indicate a new line) and which thus requires two backslashes to indicate a single backslash. After the tokenizer has processed the above FilePermission target string,converting double backslashes to single backslashes,the end result is the actual path

It is necessary that the strings be given in platform-dependent format until there is a universal file description language. Note also that the use of meta symbols such as "*" and "-" prevents the use of specific file names. We think this is a small limitation that can be tolerated for the moment. Finally,note that "/-" and "< >" are the same target on Unix systems in that they both refer to the entire file system. (They can refer to multiple file systems if they are all available). The two targets are potentially different on other operating systems,such as MS Windows and MacOS.

Also note that a target name that specifies just a directory,with a "read" action,as in

means you are only giving permission to list the files in that directory,not read any of them. To allow read access to files,you must specify either an explicit file name,or an "*" or "-",as in

And finally,note that code always automatically has permission to read files from its same (URL) location,and subdirectories of that location; it does not need explicit permission to do so.

This class represents access to a network via sockets. The target for this class can be given as "hostname:port_range",where hostname can be given in the following ways:

That is,the host is expressed as a DNS name,as a numerical IP address,as "localhost" (for the local machine) or as "" (which is equivalent to specifying "localhost").

The wildcard "*" may be included once in a DNS name host specification. If it is included,it must be in the leftmost position,as in "*.sun.com".

The port_ range can be given as follows:

Here N,N1,and N2 are non-negative integers ranging from 0 to 65535 (2^16-1).

The actions on sockets are accept, connect, listen,and resolve (which is basically DNS lookup). Note that implicitly,the action "resolve" is implied by "accept","connect",and "listen" -- i.e.,those who can listen or accept incoming connections from or initiate out-going connections to a host should be able to look up the name of the remote host.

Below are some examples of socket permissions.

import java.net.socketPermission;

SocketPermission p = new SocketPermission("java.example.com","accept");
p = new SocketPermission("192.0.2.99","accept");
p = new SocketPermission(".com","connect");
p = new SocketPermission("
.example.com:80","accept");
p = new SocketPermission(".example.com:-1023","accept");
p = new SocketPermission("
.example.com:1024-","connect");
p = new SocketPermission("java.example.com:8000-9000","connect,accept");
p = new SocketPermission("localhost:1024-","accept,connect,listen");

Note that SocketPermission("java.example.com:80,8080","accept") and SocketPermission("java.example.com,javasun.example.com","accept") are not valid socket permissions.

Moreover,because listen is an action that applies only to ports on the local host,whereas accept is an action that applies to ports on both the local and remote host,both actions are necessary.

The BasicPermission class extends the Permission class. It can be used as the base class for permissions that want to follow the same naming convention as BasicPermission (see below).

The name for a BasicPermission is the name of the given permission (for example,"exitVM","setFactory","queuePrintJob",etc). The naming convention follows the hierarchical property naming convention. An asterisk may appear at the end of the name,following a ".", or by itself,to signify a wildcard match. For example: "java.*" or "*" is valid,"*java" or "a*b" is not valid.

The action string (inherited from Permission) is unused. Thus,BasicPermission is commonly used as the base class for "named" permissions (ones that contain a name but no actions list; you either have the named permission or you don't.) Subclasses may implement actions on top of BasicPermission,if desired.

Some of the BasicPermission subclasses are java. lang.RuntimePermission,java. security. SecurityPermission,java. util. PropertyPermission,and java. net. NetPermission.

The targets for this class are basically the names of Java properties as set in various property files. Examples are the "java.home" and "os.name" properties. Targets can be specified as "*" (any property),"a.*" (any property whose name has a prefix "a."),"a.b.*",and so on. Note that the wildcard can occur only once and can only be at the rightmost position.

This is one of the BasicPermission subclasses that implements actions on top of BasicPermission. The actions are read and write. Their meaning is defined as follows: "read" permission allows the getProperty method in java. lang.System to be called to get the property value,and "write" permission allows the setProperty method to be called to set the property value.

The target for a RuntimePermission can be represented by any string,and there is no action associated with the targets. For example,RuntimePermission("exitVM") denotes the permission to exit the Java Virtual Machine.

The target names are:

This is in the same spirit as the RuntimePermission; it's a permission without actions. The targets for this class are:

This class contains the following targets and no actions:

This is the Permission class for reflective operations. A ReflectPermission is a named permission (like RuntimePermission) and has no actions. The only name currently defined is

which allows suppressing the standard Java programming language access checks -- for public,default (package) access,protected,and private members -- performed by reflected objects at their point of use.

This class contains the following targets and no actions:

SecurityPermissions control access to security-related objects,such as Security,Policy,Provider,Signer,and Identity objects. This class contains the following targets and no actions:

This permission implies all permissions. It is introduced to simplify the work of system administrators who might need to perform multiple tasks that require all (or numerous) permissions. It would be inconvenient to require the security policy to iterate through all permissions. Note that AllPermission also implies new permissions that are defined in the future.

Clearly much caution is necessary when considering granting this permission.

3.1. 16 javax. security. auth. AuthPermsision

AuthPermission handles authentication permissions and authentication-related object such as Subject,SubjectDomainCombiner,LoginContext,and Configuration. This class contains the following targets and no actions:

Recall that permissions are often compared against each other,and to facilitate such comparisons,we require that each permission class defines an implies method that represents how the particular permission class relates to other permission classes. For example,java. io. FilePermission("/tmp/*","read") implies java. io. FilePermission("/tmp/a.txt","read") but does not imply any java. net. NetPermission.

There is another layer of implication that may not be immediately obvIoUs to some readers. Suppose that one applet has been granted the permission to write to the entire file system. This presumbly allows the applet to replace the system binary,including the JVM runtime environment. This effectively means that the applet has been granted all permissions.

Another example is that if an applet is granted the runtime permission to create class loaders,it is effectively granted many more permissions,as a class loader can perform sensitive operations.

Other permissions that are "dangerous" to give out include those that allow the setting of system properties,runtime permissions for defining packages and for loading native code libraries (because the Java security architecture is not designed to and does not prevent malicIoUs behavior at the level of native code),and of course the AllPermission.

For more information about permissions,including tables enumerating the risks of assigning specific permissions as well as a table of all the Java built-in methods that require permissions,see .

It is essential that no one except Sun Microsystems should extend the permissions that are built into the Java 2 SDK,either by adding new functionality or by introducing additional target keywords into a class such as java. lang.RuntimePermission. This maintains consistency.

To create a new permission,the following steps are recommended,as shown by an example. Suppose an application developer from company ABC wants to create a customized permission to "watch TV".

First,create a new class com. abc. Permission that extends the abstract class java. security. Permission (or one of its subclasses),and another new class com. abc. TVPermission that extends the com. abc. Permission. Make sure that the implies method,among others,is correctly implemented. (Of course,com.abc.TVPermission can directly extend java.security.Permission; the intermediate com.abc.Permission is not required.)

The following figure shows the subclass relationship.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>