Java Security Sandbox (iv) Security Manager and Java API

Source: Internet
Author: User

Java is a type-safe language that has four types of security mechanisms known as the security sandbox mechanism to guarantee the security of the language, and these four types of security sandboxes are:

  • Class loading System

  • . class File Verifier

  • Security features built into Java virtual machines (and languages)

  • Security Manager and Java Api < span>

This blog mainly introduces the basic principle of "class security manager and Java API", if you need to know the other types of security mechanisms can be accessed through the above Blog link .

Brief introduction

The first three classes of the Java Security Sandbox guarantee the integrity of the programs that the JVM runs so that the JVM does not cause unexpected state by running vulnerable or malicious code. The fourth sandbox model is the class security manager and Java API, which protects the JVM from running vulnerable or malicious code without damaging external resources. Java guarantees this type of security through a class of APIs called the security Manager.

Security Policy Files

The security policy file is introduced first, and if Security Manager is enabled, the security policy file that comes with the JRE is used by default $JAVA _home/jre/lib/security/ Java.policy to specify permissions to access external resources, which can also be specified through the JVM parameter -djava.security.policy.

The main format of the policy file is as follows:

KeyStore "Some_keystore_url", "Keystore_type";
Grant [Signedby "Signer_names"] [, CodeBase "URL"] {Permission permission_class_name ["target_name"] [, "Action"] [, Sig    Nedby "Signer_names"]; ... ...};

  1. "KeyStore" record

    A keystore is a private keys database and the corresponding digital signature, such as the certificate of A/s. The policy file may have only one keystore record (or possibly no record), which can appear anywhere other than the grant record in the file. The keystores specified in the policy configuration file is used to find the public key of the signer specified in the grant record, if any of the grant records specify the signer (signer_names), then The KeyStore record must appear in the policy configuration file.

    "Some_keystore_url" refers to the URL location of KeyStore, "Keystore_type" refers to the type of KeyStore. The second option is optional, and if not specified, the type is assumed to be determined by the "Keystore.type" property in the Security properties file (java.security). The KeyStore type defines the storage and data format of the KeyStore information, which is used to protect the private key in KeyStore and the KeyStore integrity algorithm. The default type supported by Sun Microsystems is "JKS".

  2. "Grant" record

    in thePolicyeach of the files in aGrantA record contains aCodesource(specified code) and itsPermission (License). Policyeach article in the fileGrantThe records follow the following format to preserve the word"Grant"start, which indicates the beginning of a new record,"Permission"is another reserved word that is used in the record to mark the beginning of a new license. Each oneGrantrecords are granted to a specified code (CodeBase) A set of licenses (Permissions). Permission_class_namemust be a qualified and existing fully qualified class name, such asjava.io.FilePermission.

    The target_name is used to specify the location of the target class, which specifies the permissions that the target class has. Target_name can specify the class name directly (either absolute or relative path), directory name, or use wildcards/////.

    directory/represents all. class files under the directory directory, excluding. jar files

    directory/* represents all the. class and. jar files in the directory directory

    directory/-represents all the. class and. jar files in the directory directory, including subdirectories

Here is a demo of the policy file:

Grant CodeBase "file:${{java.ext.dirs}}/*" {permission java.security.AllPermission;};        Grant {Permission java.lang.RuntimePermission "Stopthread";                Permission Java.net.SocketPermission "localhost:1099", "Listen";        Permission Java.util.PropertyPermission "Java.version", "read"; ... ...};

For example: for java.net.SocketPermission , Action can be: Listen , Accept , Connect , Read , write; for java.io.filepermission,action can be: Read, write, delete, and execute.

Security Manager

The Java Security Manager can be customized, You can also use the default implementation of the JDK, Java.lang.SecurityManager, to start the security manager in two ways, one is to start hard-coded, and the other is to start with the JVM parameter-djava.security.manager.

The following test cases use the default policy file configuration of the JRE:

Grant {... permission java.util.PropertyPermission "Java.version", "read"; ... ...};

The policy file specifies Read permissions for "Java.version", and then does not specify write permissions. Refer to the following test cases:

public static void Main (string ... args) {string Javaversion=system.getproperty ("Java.version");    System.err.println (javaversion);    System.setproperty ("Java.version", "1.7.0_45");    String javanewversion=system.getproperty ("java.version");  System.err.println (javanewversion); }

First read the "Java.version" property, then rewrite the property to 1.7.0_45, and then read it and print it, the output is:

1.8.0_451.7.0_45

You can see that the default JDK version is 1.8.0_45 (1.8.0 is the major version of Java, and 45 is the minor version number).

Then the previous policy file only specified the Read permission, why is this write successful? That's because Java does not start the security Manager by default, and you can use hard-coded System.setsecuritymanager () to start the security manager, referring to the following test cases:

public static void Main (String ... args) {//Enable Security Manager System.setsecuritymanager (New SecurityManager ());    String javaversion=system.getproperty ("java.version");    System.err.println (javaversion);    System.setproperty ("Java.version", "1.7.0_45");    String javanewversion=system.getproperty ("java.version");  System.err.println (javanewversion); }

The output at this time is:

1.8.0_45exception in thread "main" Java.security.AccessControlException:access denied ("Java.util.PropertyPermission "Java.version" "write") at Java.security.AccessControlContext.checkPermission (accesscontrolcontext.java:457) at Java.security.AccessController.checkPermission (accesscontroller.java:884) at Java.lang.SecurityManager.checkPermission (securitymanager.java:549) at Java.lang.System.setProperty (System.java : 792) at Test. Test.main (Test.java:9)

The result is clear: it can be read, but not written, and we can modify the policy file so that it supports the "java.version" write operation:

Grant {... permission java.util.PropertyPermission "Java.version", "read";        Permission Java.util.PropertyPermission "Java.version", "write"; ... ...};

At this point, the above use case is executed and the output is:

1.8.0_451.7.0_45

You can see that you can support the write operation for "Java.version" at this time.

In addition , the JVM parameter-djava.security.manager enables the Security Manager to be enabled, at which time the JVM starts with the "Java.security.manager" system property as an empty string "", which starts Sun.misc.Launcher when initializing the security Manager, view The source code of Sun.misc.Launcher:

Public launcher () {        extclassloader extclassloader;         try {             extclassloader = extclassloader.getextclassloader ();         } catch (ioexception ioexception)  {             throw new internalerror ("could not create  Extension class loader ",  ioexception);        }         try {             loader = appclassloader.getappclassloader (Extclassloader);         } catch (Ioexception ioexception1)  {            &nBsp;throw new internalerror ("Could not create application class loader",  ioexception1);        }         thread.currentthread (). Setcontextclassloader (loader);         string s = system.getproperty ("Java.security.manager");         if (s != null)  {             SecurityManager securitymanager = null;             if ("". Equals (s)  | |   "Default". Equals (s))                  securitymanager = new securitymanager ();             else                try {                     securitymanager =  (SecurityManager ) Loader.loadclass (s). newinstance ();                 }                 catch (illegalaccessexception illegalaccessexception)  { }                 catch (InstantiationException  Instantiationexception)  { }                 catch (classnotfoundexception classnotfoundexception)  { }                 catch (ClassCastException  classcastexception)   { }            if (securitymanager !=  null)                  System.setsecuritymanager (SecurityManager);             else                 Throw new internalerror ((New stringbuilder ()). Append ("could not create  securitymanager:  "). Append (s). toString ());        }     }

You can see that the "Java.security.manager" system property is an empty string "" when the default security manager SecurityManager for JDK is enabled.

Java API

Java security mechanism Most of the API is under the Java.security package, because the source of a lot of, it is not posted out, we are interested in the words can be studied under. The following is an introduction to some of the commonly used classes of APIs:

    • Java.security.AccessControlContext: Makes system resource access decisions based on the context it encapsulates, which is most commonly used to mark code as "privileged."

    • Java.security.AccessController: Used for operations and decisions related to access control. Java.security.SecureClassLoader This class extends ClassLoader and supports the use of related code sources and permission definition classes, which are obtained by default by System policy.

    • Java.security.Provider: This class represents the Java security API "Provider", where Provider implements part or all of Java security.

    • Java.security.Permission: Represents an abstract class that accesses system resources. All permissions have a name (their interpretation depends on the subclass) and an abstract method that defines the semantics of a particular Permission subclass.


Java Security Sandbox (iv) Security Manager and Java API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.