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 searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Direct Use of Unsafe JNI
This weakness occurs when a Java application directly calls native code through the Java Native Interface (JNI), exposing the entire application to security risks present in that external code, even…
What is CWE-111?
Real-world CVEs caused by CWE-111
No public CVE references are linked to this CWE in MITRE's catalog yet.
Step-by-step attacker path
- 1
The following code defines a class named Echo. The class declares one native method (defined below), which uses C to echo commands entered on the console back to the user. The following C code defines the native method implemented in the Echo class:
- 2
Because the example is implemented in Java, it may appear that it is immune to memory issues like buffer overflow vulnerabilities. Although Java does do a good job of making memory operations safe, this protection does not extend to vulnerabilities occurring in source code written in other languages that are accessed using the Java Native Interface. Despite the memory protections offered in Java, the C code in this example is vulnerable to a buffer overflow because it makes use of gets(), which does not check the length of its input.
- 3
The Sun Java(TM) Tutorial provides the following description of JNI [See Reference]: The JNI framework lets your native method utilize Java objects in the same way that Java code uses these objects. A native method can create Java objects, including arrays and strings, and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code. A native method can even update Java objects that it created or that were passed to it, and these updated objects are available to the Java application. Thus, both the native language side and the Java side of an application can create, update, and access Java objects and then share these objects between them.
- 4
The vulnerability in the example above could easily be detected through a source code audit of the native method implementation. This may not be practical or possible depending on the availability of the C source code and the way the project is built, but in many cases it may suffice. However, the ability to share objects between Java and native methods expands the potential risk to much more insidious cases where improper data handling in Java may lead to unexpected vulnerabilities in native code or unsafe operations in native code corrupt data structures in Java. Vulnerabilities in native code accessed through a Java application are typically exploited in the same manner as they are in applications written in the native language. The only challenge to such an attack is for the attacker to identify that the Java application uses native code to perform certain operations. This can be accomplished in a variety of ways, including identifying specific behaviors that are often implemented with native code or by exploiting a system information exposure in the Java application that reveals its use of JNI [See Reference].
Vulnerable Java
The following code defines a class named Echo. The class declares one native method (defined below), which uses C to echo commands entered on the console back to the user. The following C code defines the native method implemented in the Echo class:
class Echo {
public native void runEcho();
static {
System.loadLibrary("echo");
}
public static void main(String[] args) {
new Echo().runEcho();
}
} Secure pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
const safe = validateAndEscape(input);
return executeWithGuards(safe);
} How to prevent CWE-111
- Implementation Implement error handling around the JNI call.
- Implementation Do not use JNI calls if you don't trust the native library.
- Implementation Be reluctant to use JNI calls. A Java API equivalent may exist.
How to detect CWE-111
Plexicus auto-detects CWE-111 and opens a fix PR in under 60 seconds.
Codex Remedium scans every commit, identifies this exact weakness, and ships a reviewer-ready pull request with the patch. No tickets. No hand-offs.
Frequently asked questions
What is CWE-111?
This weakness occurs when a Java application directly calls native code through the Java Native Interface (JNI), exposing the entire application to security risks present in that external code, even if Java itself is safe from those specific flaws.
How serious is CWE-111?
MITRE has not published a likelihood-of-exploit rating for this weakness. Treat it as medium-impact until your threat model proves otherwise.
What languages or platforms are affected by CWE-111?
MITRE lists the following affected platforms: Java.
How can I prevent CWE-111?
Implement error handling around the JNI call. Do not use JNI calls if you don't trust the native library.
How does Plexicus detect and fix CWE-111?
Plexicus's SAST engine matches the data-flow signature for CWE-111 on every commit. When a match is found, our Codex Remedium agent opens a fix PR with the corrected code, tests, and a one-line summary for the reviewer.
Where can I learn more about CWE-111?
MITRE publishes the canonical definition at https://cwe.mitre.org/data/definitions/111.html. You can also reference OWASP and NIST documentation for adjacent guidance.
Weaknesses related to CWE-111
Use of Low-Level Functionality
This vulnerability occurs when code bypasses high-level framework controls by directly using low-level system functions, violating the…
J2EE Bad Practices: Direct Management of Connections
This vulnerability occurs when a J2EE application handles database connections directly instead of using the container's built-in…
J2EE Bad Practices: Direct Use of Sockets
This vulnerability occurs when a J2EE application creates network sockets directly, bypassing the container-managed communication…
J2EE Bad Practices: Direct Use of Threads
Creating or managing threads directly within a J2EE application is a risky practice that violates the platform's standards and often leads…
EJB Bad Practices: Use of Synchronization Primitives
This vulnerability occurs when an Enterprise JavaBeans (EJB) component improperly uses thread synchronization primitives, violating the…
EJB Bad Practices: Use of AWT Swing
This vulnerability occurs when an Enterprise JavaBeans (EJB) component incorrectly uses AWT or Swing UI toolkits, violating the EJB…
EJB Bad Practices: Use of Java I/O
This vulnerability occurs when an Enterprise JavaBeans (EJB) component incorrectly uses Java I/O (java.io) operations to access the file…
Further reading
- MITRE — official CWE-111 https://cwe.mitre.org/data/definitions/111.html
- Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors https://samate.nist.gov/SSATTM_Content/papers/Seven%20Pernicious%20Kingdoms%20-%20Taxonomy%20of%20Sw%20Security%20Errors%20-%20Tsipenyuk%20-%20Chess%20-%20McGraw.pdf
- Fortify Descriptions http://vulncat.fortifysoftware.com
- The Java(TM) Tutorial: The Java Native Interface http://www.eg.bucknell.edu/~mead/Java-tutorial/native1.1/index.html
Don't Let Security
Weigh You Down.
Stop choosing between AI velocity and security debt. Plexicus is the only platform that runs Vibe Coding Security and ASPM in parallel — one workflow, every codebase.