tags:

views:

132

answers:

3

While reading Java Security I came across the below sentences but could not get any satisfactory explanation on the Internet. Can anyone please explain

  • Prevents loading of classes with bytecode
  • Prevents loading of in illegal packages
A: 

Source code is compiled to bytecode, which is distributed to users. If the bytecode has been damaged, or was not made by a java compiler, then it may be illegal, meaning that the bytes don't make sense.

Lars D
+3  A: 

We cannot, however, be sure that the class itself is safe. There is still the safety net of the SecurityManager which will prevent the class from accessing protected resources such as network and local hard disk, but that in itself is not enough. The class might contain illegal bytecode, forge pointers to protected memory, overflow or underflow the program stack, or in some other way corrupt the integrity of the JVM. Check Topic The Class File Verifier in [1]: http://medialab.di.unipi.it/doc/JNetSec/jns%5Fch5.htm

valli
The link was very useful.
Geek
+2  A: 

The byte code verifier makes the following checks:

  • Branches must be within the bounds of the code array for the method.
  • The targets of all control-flow instructions are each the start of an instruction. In the case of a wide instruction, the wide opcode is considered the start of the instruction, and the opcode giving the operation modified by that wide instruction is not considered to start an instruction. Branches into the middle of an instruction are disallowed.
  • No instruction can access or modify a local variable at an index greater than or equal to the number of local variables that its method indicates it allocates.
  • All references to the constant pool must be to an entry of the appropriate type. For example: the instruction ldc can be used only for data of type int or float or for instances of class String; the instruction getfield must reference a field.
  • The code does not end in the middle of an instruction.
  • Execution cannot fall off the end of the code.
  • For each exception handler, the starting and ending point of code protected by the handler must be at the beginning of an instruction or, in the case of the ending point, immediately past the end of the code. The starting point must be before the ending point. - The exception handler code must start at a valid instruction, and it may not start at an opcode being modified by the wide instruction.
brianegge