...
Noncompliant Code Example
In this noncompliant code example, the nativeOperation()
method is both native and public; therefore, untrusted callers may invoke it. Native method invocations bypass security manager checks.
This example includes the doOperation()
wrapper method, which invokes the nativeOperation()
native method but fails to provide input validation or security checks.
Code Block | ||
---|---|---|
| ||
public final class NativeMethod {
// public native method
public native void nativeOperation(byte[] data, int offset, int len);
// wrapper method that lacks security checks and input validation
public void doOperation(byte[] data, int offset, int len) {
nativeOperation(data, offset, len);
}
static {
// load native library in static initializer of class
System.loadLibrary("NativeMethodLib");
}
}
|
Compliant Solution
This compliant solution declares the native method private. The doOperation()
wrapper method checks permissions, creates a defensive copy of the mutable input array data
, and checks the ranges of the arguments. The nativeOperation()
method is consequently called with secure inputs. Note that the validation checks must produce outputs that conform to the input requirements of the native methods.
Code Block | ||
---|---|---|
| ||
public final class NativeMethodWrapper {
// private native method
private native void nativeOperation(byte[] data, int offset, int len);
// wrapper method performs SecurityManager and input validation checks
public void doOperation(byte[] data, int offset, int len) {
// permission needed to invoke native method
securityManagerCheck();
if (data == null) {
throw new NullPointerException();
}
// copy mutable input
data = data.clone();
// validate input
if ((offset < 0) || (len < 0) || (offset > (data.length - len))) {
throw new IllegalArgumentException();
}
nativeOperation(data, offset, len);
}
static {
// load native library in static initializer of class
System.loadLibrary("NativeMethodLib");
}
}
|
Exceptions
|
Compliant Solution
Code Block | ||
---|---|---|
| ||
|
Exceptions
SEC08-EX0: Native methods that do not require security manager checks, validation of arguments or return values, or defensive copying of mutable inputs (for example, the standard C function int rand(void)
) do not need to be wrapped.
Risk Assessment
Failure to define wrappers around native methods can allow unprivileged callers to invoke them and exploit inherent vulnerabilities such as buffer overflows in native libraries.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
JNI00JNI01-J | medium | probable | high | P4 | L3 |
Automated Detection
Automated detection is not feasible in the fully general case. However, an approach similar to Design Fragments [Fairbanks 2007] could assist both programmers and static analysis tools.
Related Guidelines
CWE-111. Direct use of unsafe JNI | |
Secure Coding Guidelines for the Java Programming Language, Version 34.0 | Guideline 59-3. Define wrappers around native methods |
Bibliography
...
...
9. Safely invoke standard APIs that perform tasks using the immediate caller's class loader instance |
Bibliography
[JNI 2006] |
| ||
| Section 2.2.3, Interfaces and Architectures |