Test that an application can mmap executable code from its APK.

bug: 16727210 16076402

Change-Id: I0c3bf84e4588aa99c95592c26b6113d33808fe29
(cherry picked from commit c3baa19073a5bec63de7aef86eb1a01530ef714b)
diff --git a/tests/tests/security/jni/Android.mk b/tests/tests/security/jni/Android.mk
index 94fd02d..fe47e2e 100644
--- a/tests/tests/security/jni/Android.mk
+++ b/tests/tests/security/jni/Android.mk
@@ -29,7 +29,8 @@
 		android_security_cts_LoadEffectLibraryTest.cpp \
 		android_security_cts_NativeCodeTest.cpp \
 		android_security_cts_SeccompDeathTestService.cpp \
-		android_security_cts_SELinuxTest.cpp
+		android_security_cts_SELinuxTest.cpp \
+		android_security_cts_MMapExecutableTest.cpp
 
 LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
 
diff --git a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
index 9d00ad6..41a3df9 100644
--- a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
+++ b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
@@ -24,6 +24,7 @@
 extern int register_android_security_cts_LoadEffectLibraryTest(JNIEnv*);
 extern int register_android_security_cts_SeccompDeathTestService(JNIEnv*);
 extern int register_android_security_cts_SELinuxTest(JNIEnv*);
+extern int register_android_security_cts_MMapExecutableTest(JNIEnv* env);
 
 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
     JNIEnv *env = NULL;
@@ -60,5 +61,9 @@
         return JNI_ERR;
     }
 
+    if (register_android_security_cts_MMapExecutableTest(env)) {
+        return JNI_ERR;
+    }
+
     return JNI_VERSION_1_4;
 }
diff --git a/tests/tests/security/jni/android_security_cts_MMapExecutableTest.cpp b/tests/tests/security/jni/android_security_cts_MMapExecutableTest.cpp
new file mode 100644
index 0000000..c187526
--- /dev/null
+++ b/tests/tests/security/jni/android_security_cts_MMapExecutableTest.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define LOG_TAG "MMapExecutableTest"
+
+#include <android/log.h>
+#include <cutils/log.h>
+#include <fcntl.h>
+#include <jni.h>
+#include <nativehelper/JNIHelp.h>
+#include <nativehelper/ScopedUtfChars.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+const size_t kOffset = 4096;
+
+// Verify that we can mmap a region of a file with a non-zero offset executable
+static jboolean mmap_executable(JNIEnv *env, jobject, jstring jfilename) {
+    if (jfilename == NULL) {
+        jniThrowNullPointerException(env, NULL);
+        return false;
+    }
+
+    ScopedUtfChars filename(env, jfilename);
+    int fd = open(filename.c_str(), O_RDONLY);
+    if (fd == -1) {
+        ALOGE("open %s: %s", filename.c_str(), strerror(errno));
+        return false;
+    }
+
+    struct stat stat_buf;
+    if (fstat(fd, &stat_buf) == -1) {
+        ALOGE("fstat %s: %s", filename.c_str(), strerror(errno));
+        return false;
+    }
+
+    if (stat_buf.st_size < kOffset) {
+        ALOGE("file %s is too small", filename.c_str());
+        return false;
+    }
+
+    void * mem =
+            mmap(NULL, stat_buf.st_size - kOffset,
+                 PROT_EXEC | PROT_READ, MAP_PRIVATE, fd, kOffset);
+    if (mem == MAP_FAILED) {
+        ALOGE("mmap %s: %s", filename.c_str(), strerror(errno));
+        return false;
+    }
+
+    if (munmap(mem, stat_buf.st_size - kOffset) == -1) {
+        ALOGE("munmap %s: %s", filename.c_str(), strerror(errno));
+        return false;
+    }
+
+    return true;
+}
+
+static JNINativeMethod gMethods[] = {
+    { (char*)"mmapExecutable",
+      (char*)"(Ljava/lang/String;)Z", (void *)mmap_executable }
+};
+
+int register_android_security_cts_MMapExecutableTest(JNIEnv* env) {
+    jclass clazz = env->FindClass("android/security/cts/MMapExecutableTest");
+    return env->RegisterNatives(
+            clazz, gMethods, sizeof(gMethods) / sizeof(JNINativeMethod));
+}
diff --git a/tests/tests/security/src/android/security/cts/MMapExecutableTest.java b/tests/tests/security/src/android/security/cts/MMapExecutableTest.java
new file mode 100644
index 0000000..bc2e115
--- /dev/null
+++ b/tests/tests/security/src/android/security/cts/MMapExecutableTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.security.cts;
+
+import android.test.AndroidTestCase;
+
+/**
+ * Verify that we can mmap executable code from an APK.
+ * Prevent regression on: b/16727210 and b/16076402.
+ */
+public class MMapExecutableTest extends AndroidTestCase {
+    public MMapExecutableTest() {}
+
+    /**
+     * Test that we can mmap the APK file executable.
+     */
+    public void testMMapExecutable() {
+        assertTrue(mmapExecutable(getContext().getApplicationInfo().sourceDir));
+    }
+
+    /**
+     * Attempts to mmap a portion of the specified file executable (PROT_EXEC).
+     * Returns true if successful false otherwise.
+     */
+    public static final native boolean mmapExecutable(String filename);
+
+    static {
+        System.loadLibrary("ctssecurity_jni");
+    }
+}