android-jni-security
Installation
SKILL.md
This skill provides rules and code patterns to secure Android JNI boundaries. Follow these patterns to prevent buffer overflows, pinned memory leaks, and Time-of-Check to Time-of-Use (TOCTOU) file races. They also prevent hardcoded native secrets, cross-thread reference crashes, and unhandled pending JVM exceptions.
Glossary
- JNI (Java Native Interface): The platform bridge connecting ART bytecode in Kotlin or Java with C/C++
.solibraries. - JNIEnv: A thread-local pointer to the JNI function table passed as the first argument to every native method. A
JNIEnv*pointer must NEVER be shared or cached across threads. - JavaVM: The process-wide virtual machine pointer used to attach and detach native threads (
AttachCurrentThread,DetachCurrentThread). - Local reference: A thread-scoped reference (
jobject,jclass,jstring,jarray) valid only during the active native call. - Global reference: A cross-thread reference (
NewGlobalRef) that keeps a Java object reachable across threads and calls until freed withDeleteGlobalRef. - TOCTOU (Time-of-Check to Time-of-Use): A race condition when managed code checks a file path before C/C++ re-opens that path string. An attacker can swap the file or symlink before
fopenoropenruns. - Pending JVM exception state: The state after a JNI call throws a managed exception. Native code must not call most JNI functions while an exception is pending. Check
ExceptionCheck(), then free resources and return, or clear and handle the failure.