Merge "CTS test for Android Security b/228314987" into qt-dev am: ec2b2bb799 am: 2a2848cdba am: a921b484ec am: abf11aa13e am: f06afb3187 am: 917be27457

Original change: https://googleplex-android-review.googlesource.com/c/platform/cts/+/20566417

Change-Id: I61c7fe778b6c87907077b6ca894ff2478e7972b1
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/hostsidetests/securitybulletin/src/android/security/cts/CVE_2022_20360.java b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2022_20360.java
new file mode 100644
index 0000000..0568740
--- /dev/null
+++ b/hostsidetests/securitybulletin/src/android/security/cts/CVE_2022_20360.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2022 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 static org.junit.Assume.assumeNoException;
+import static org.junit.Assume.assumeTrue;
+
+import android.platform.test.annotations.AsbSecurityTest;
+
+import com.android.sts.common.tradefed.testtype.NonRootSecurityTestCase;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class CVE_2022_20360 extends NonRootSecurityTestCase {
+
+    // b/228314987
+    // Vulnerable apk       : Settings.apk
+    // Vulnerable module    : com.android.settings
+    // Is Play managed      : No
+    @AsbSecurityTest(cveBugId = 228314987)
+    @Test
+    public void testPocCVE_2022_20360() {
+        final String testPkg = "android.security.cts.CVE_2022_20360";
+        ITestDevice device = null;
+        int currentUser = -1;
+        int newUser = -1;
+        try {
+            device = getDevice();
+
+            // Check if device supports nfc
+            assumeTrue("Device does not support nfc", device.hasFeature("android.hardware.nfc"));
+
+            // Get current user
+            currentUser = device.getCurrentUser();
+
+            // Create new guest user 'CTSUser' for test
+            newUser = device.createUser("CTSUser", true, false);
+
+            // Start new guest user 'CTSUser'
+            assumeTrue("Unable to create new guest user", device.startUser(newUser, true));
+
+            // Switch to new user 'CTSUser'
+            assumeTrue("Unable to switch to guest user", device.switchUser(newUser));
+
+            // Install test-app
+            installPackage("CVE-2022-20360.apk", "--user " + newUser);
+
+            runDeviceTests(testPkg, testPkg + ".DeviceTest", "testSecureNfcPreferenceController");
+        } catch (Exception e) {
+            assumeNoException(e);
+        } finally {
+            try {
+                if (currentUser != -1) {
+                    // Switch back to previous user
+                    device.switchUser(currentUser);
+                }
+                if (newUser != -1) {
+                    // Stop user 'CTSUser'
+                    device.stopUser(newUser);
+
+                    // Remove user 'CTSUser'
+                    device.removeUser(newUser);
+                }
+            } catch (Exception e) {
+                // Ignore exception here
+            }
+        }
+    }
+}
diff --git a/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/Android.bp b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/Android.bp
new file mode 100644
index 0000000..d9f8554
--- /dev/null
+++ b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/Android.bp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2022 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test_helper_app {
+    name: "CVE-2022-20360",
+    defaults: [
+        "cts_support_defaults"
+    ],
+    srcs: [
+        "src/**/*.java"
+    ],
+    test_suites: [
+        "sts",
+    ],
+    static_libs: [
+        "androidx.test.core",
+        "androidx.test.rules",
+    ],
+    platform_apis: true,
+}
diff --git a/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/AndroidManifest.xml b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/AndroidManifest.xml
new file mode 100644
index 0000000..9e2361a
--- /dev/null
+++ b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<!--
+  Copyright 2022 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.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.security.cts.CVE_2022_20360">
+   <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+   <instrumentation
+        android:name="androidx.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android.security.cts.CVE_2022_20360" />
+</manifest>
diff --git a/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/res/values/strings.xml b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/res/values/strings.xml
new file mode 100644
index 0000000..9476f7a
--- /dev/null
+++ b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/res/values/strings.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2022 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.
+-->
+<resources>
+    <string name="defaultSettingsPkg">com.android.settings</string>
+    <string name="disableSecureNfcFailed">Disabling secure NFC failed</string>
+    <string name="key">key</string>
+    <string name="msgDeviceLocked">Device is in sleep or locked mode</string>
+    <string name="msgTestFail"> Device is vulnerable to b/228314987!! Secure nfc can be disabled in
+    guest user via SettingsSlice</string>
+    <string name="secureNfcPreferenceControllerClassName">.nfc.SecureNfcPreferenceController
+    </string>
+    <string name="setCheckedMethod">setChecked</string>
+</resources>
diff --git a/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/src/android/security/cts/CVE_2022_20360/DeviceTest.java b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/src/android/security/cts/CVE_2022_20360/DeviceTest.java
new file mode 100644
index 0000000..9e73804
--- /dev/null
+++ b/hostsidetests/securitybulletin/test-apps/CVE-2022-20360/src/android/security/cts/CVE_2022_20360/DeviceTest.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2022 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.CVE_2022_20360;
+
+import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
+import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assume.assumeFalse;
+import static org.junit.Assume.assumeNoException;
+import static org.junit.Assume.assumeTrue;
+
+import android.app.KeyguardManager;
+import android.app.UiAutomation;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.nfc.NfcAdapter;
+import android.nfc.NfcManager;
+import android.os.PowerManager;
+import android.os.UserManager;
+import android.provider.Settings;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+@RunWith(AndroidJUnit4.class)
+public class DeviceTest {
+
+    @Test
+    public void testSecureNfcPreferenceController() {
+        boolean secureNfcEnabled = false;
+        NfcAdapter nfcAdapter = null;
+        UiAutomation uiAutomation = null;
+        try {
+            Context context = getApplicationContext();
+            NfcManager nfcManager = context.getSystemService(NfcManager.class);
+            nfcAdapter = nfcManager.getDefaultAdapter();
+            uiAutomation = getInstrumentation().getUiAutomation();
+
+            // Secure NFC APIs require device to be unlocked hence check if device is unlocked
+            PowerManager powerManager = context.getSystemService(PowerManager.class);
+            KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
+            assumeTrue(context.getString(R.string.msgDeviceLocked),
+                    powerManager.isInteractive() && !keyguardManager.isKeyguardLocked());
+
+
+            // Save secure NFC state(enabled/disabled) and disable secure NFC for test
+            secureNfcEnabled = nfcAdapter.isSecureNfcEnabled();
+            if (secureNfcEnabled) {
+                nfcAdapter.enableSecureNfc(false);
+            }
+            assumeFalse(context.getString(R.string.disableSecureNfcFailed),
+                    nfcAdapter.isSecureNfcEnabled());
+
+            // Retrieve settings package name dynamically
+            Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
+            ComponentName settingsComponent =
+                    settingsIntent.resolveActivity(context.getPackageManager());
+            String settingsPkgName = settingsComponent != null ? settingsComponent.getPackageName()
+                    : context.getString(R.string.defaultSettingsPkg);
+
+            // Get vulnerable method 'setChecked' using reflection
+            Context settingsContext = context.createPackageContext(settingsPkgName,
+                    Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
+            ClassLoader settingsClassLoader = settingsContext.getClassLoader();
+            Class<?> targetClass = settingsClassLoader.loadClass(settingsPkgName
+                    + context.getString(R.string.secureNfcPreferenceControllerClassName));
+            Constructor<?> targetClassCstr =
+                    targetClass.getConstructor(Context.class, String.class);
+            Object targetClassobject =
+                    targetClassCstr.newInstance(context, context.getString(R.string.key));
+            Method setCheckedMethod = targetClass
+                    .getDeclaredMethod(context.getString(R.string.setCheckedMethod), boolean.class);
+            setCheckedMethod.setAccessible(true);
+
+            // Check if current user is guest user
+            uiAutomation.adoptShellPermissionIdentity(android.Manifest.permission.CREATE_USERS);
+            UserManager userManager = context.getSystemService(UserManager.class);
+            assumeTrue(userManager.isGuestUser());
+
+            // Invoke vulnerable method 'setChecked'
+            boolean retVal = (boolean) setCheckedMethod.invoke(targetClassobject, true);
+            assertFalse(context.getString(R.string.msgTestFail), retVal);
+        } catch (Exception e) {
+            assumeNoException(e);
+        } finally {
+            try {
+                // Disable secure NFC if it was disabled before the test
+                if (!secureNfcEnabled) {
+                    nfcAdapter.enableSecureNfc(false);
+                }
+            } catch (Exception ignored) {
+                // Ignore any exception here
+            } finally {
+                uiAutomation.dropShellPermissionIdentity();
+            }
+        }
+    }
+}