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:
This blog mainly introduces the following ". class file Verifier", introduces its rationale, and several other security mechanisms are introduced in subsequent blogs.
Introduction
The JVM's. class File Checker is used to check if the. class file has a legitimate memory structure, which is necessary because Java. class files may come from the computer or from the network, either by your own compiled files or by someone else's tampered files. For the JVM, a. class file is a sequence of bytes that does not interfere with the origin of the sequence of bytes, but verifies that the structure of the byte sequence is correct.
The. class file Inspector's security measures are to verify the robustness of the. class file bytecode, such as a . class file that has been maliciously tampered with. this. class file contains a method that has a goto instruction that jumps directly outside the method to execute the unknown code, and if the method is executed, it is likely to cause the JVM to crash. Therefore, it is necessary to check the robustness of the bytecode by the. class File Checker.
although the. class File Checker checks the bytecode to ensure the robustness of the program, which is then sacrificed some performance, in order to minimize this impact, the. class file Checker does most of the testing before bytecode execution, which means The . class File Checker checks before bytecode execution and not execution, and this check only occurs once. For example, each time a jump command is encountered . The class file inspector confirms that the jump instruction jumps to another legitimate instruction, which is in the same method's byte stream.
The. class file inspector will perform four independent scans to ensure the legitimacy of the bytecode. The first scan takes place during class loading, this time checking the internal structure of the. class file to ensure it can be safely compiled; the second and third scans are made at the time of the connection, The data definition of the. class file is then checked for the semantics of the Java language, and the integrity of the bytecode is checked , and the IV scan is performed when parsing the symbol reference, which checks to see if the fields, methods, and classes referenced by the. class file exist.
First scan
The first scan occurs when the. class file is loaded, and the . class file checker checks each byte sequence to see if it conforms to the basic structure of the Java class file. First, the checker checks to see if the. class file starts with the magic number 0XCAFEBABY, which is intended to distinguish between some obviously wrong or corrupted . class files. And then The verifier checks the major and minor version numbers of the. class file, which must be within the scope supported by the JVM, such as the. class file that I compiled with JAVA8 compilation will be thrown in the Java6 JVM Java.lang.UnsupportedClassVersionErro R, because JAVA6 does not support some of the new features of java8 such as lambda expressions, it is not possible to execute JAVA8 compiler-compiled bytecode, and Java is generally backwards compatible, such as Java8 's JVM is capable of executing JAVA6-compiled. class files. The first scan is primarily to check if the. class file conforms to the fixed format of the Java class file, so that bytecode can be compiled into internal data structures within the method area.
Second scan
The second scan will perform a semantic check of the data type, and it will not check the binary data of the. class file, but will examine the data structure defined in the method area. class, the field defined in the Methods and method descriptors are stored as a string in the. class file, and the verifier checks to see if the strings conform to the Java specification, and the verifier checks to see if the class itself conforms to the Java language specification, such as the Java language rules that all classes must have a parent class except for the object class; The inspector also checks for final repair If the decorated class (for example, String.class) is inherited, it also checks whether the method that was final modified is overwritten and throws Java.lang.VerifyError if an error occurs. The verifier also checks to see if the constants defined in the constant pool are valid, and it checks that the index of the constants points to the correct constant pool entry.
Third trip scan
The third scan will be byte-code verification, and bytecode checking confirms the correctness of the bytecode opcode, ensures that the operand stack contains the correct values and types, and checks that the class's methods are called with the correct arguments and parameter types. The inspector will perform a large number of operations on the first three scans, such as verifying that all opcode have a valid operand, etc., after this scan, it is necessary to ensure that the. class file's byte stream can be executed securely by the JVM. Then the checker does not check out all the security issues, such as the "downtime problem" that it cannot check out. The downtime problem is a well-known computer domain problem, that is, you cannot write a program that uses it to determine whether a program that is being entered will be down during execution.
Four-trip scan
The four-pass scan is checked on the dynamic connection of the symbol, and the checker checks to see if the symbol reference is legal; Because this scan needs to check the classes referenced by the class, it is possible to load new classes, but in order to save memory and ensure program correctness, the JVM uses deferred-loaded policies to load classes, That is, it is not loaded until the class is actually used by the program. If a JVM implementation preloaded a class in order to speed up the load, it would still behave as a deferred load. For example, when a JVM is preloaded with a class, it finds that the class does not exist, and it does not throw noclassdeffounderror immediately, but only throws an exception if the missing class is used by the program. If the JVM is pre-connected, the fourth scan is immediately followed by a third scan, and if the JVM is delayed (that is, when a reference is first executed), then the fourth scan may take a long time to execute (or not even execute) after the three-pass scan.
Java Security Sandbox (ii). class File Verifier