...
Non-Compliant Code Example
SecurityManager Security manager checks are not conducted in case of native method invocations. Additionally, as demonstrated in the non-compliant code example, it is easy to overlook proper input validation before the call. The doOperation
method invokes the nativeOperation
native method but fails on multiple validation angles. Also, the access specifier of the native method is public
which raises risks associated with untrusted callers. (Note that native methods may even increase susceptibility to non-Java specific vulnerabilities, such as buffer overflows)
...
This compliant solution defines a public wrapper that calls securityManagerCheck()
which in turn performs routine permission checks to determine if the succeeding operations can continue. This is followed by input range checking and creation of a copy of the mutable input array, data. Finally the nativeOperation
method is called with sanitized inputs. Ensure that the validation checks produce outputs that are coherent with the input requirements of the native implementations/libraries.
...