Description
When a Java application uses the Java Native Interface (JNI) to call code written in another programming language, it can expose the application to weaknesses in that code, even if those weaknesses cannot occur in Java.
Many safety features that programmers may take for granted do not apply for native code, so you must carefully review all such code for potential problems. The languages used to implement native code may be more susceptible to buffer overflows and other attacks. Native code is unprotected by the security features enforced by the runtime environment, such as strong typing and array bounds checking.
Potential Impact
Access Control
Bypass Protection Mechanism
Demonstrative Examples
class Echo {
public native void runEcho();static {
System.loadLibrary("echo");
}public static void main(String[] args) {
new Echo().runEcho();
}
}#include <jni.h>#include "Echo.h"//the java class above compiled with javah#include <stdio.h>
JNIEXPORT void JNICALLJava_Echo_runEcho(JNIEnv *env, jobject obj){char buf[64];gets(buf);printf(buf);}Mitigations & Prevention
Implement error handling around the JNI call.
Do not use JNI calls if you don't trust the native library.
Be reluctant to use JNI calls. A Java API equivalent may exist.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Related Weaknesses
Taxonomy Mappings
- 7 Pernicious Kingdoms: — Unsafe JNI
- The CERT Oracle Secure Coding Standard for Java (2011): SEC08-J — Define wrappers around native methods
- SEI CERT Oracle Coding Standard for Java: JNI01-J — Safely invoke standard APIs that perform tasks using the immediate caller's class loader instance (loadLibrary)
- SEI CERT Oracle Coding Standard for Java: JNI00-J — Define wrappers around native methods
- Software Fault Patterns: SFP3 — Use of an improper API
Frequently Asked Questions
What is CWE-111?
CWE-111 (Direct Use of Unsafe JNI) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. When a Java application uses the Java Native Interface (JNI) to call code written in another programming language, it can expose the application to weaknesses in that code, even if those weaknesses ca...
How can CWE-111 be exploited?
Attackers can exploit CWE-111 (Direct Use of Unsafe JNI) to bypass protection mechanism. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-111?
Key mitigations include: Implement error handling around the JNI call.
What is the severity of CWE-111?
CWE-111 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.