|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package android.security.cts.CVE_2021_0600; |
| 18 | + |
| 19 | +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assume.assumeNoException; |
| 23 | +import static org.junit.Assume.assumeTrue; |
| 24 | + |
| 25 | +import android.app.Instrumentation; |
| 26 | +import android.content.BroadcastReceiver; |
| 27 | +import android.content.Context; |
| 28 | +import android.content.Intent; |
| 29 | +import android.content.IntentFilter; |
| 30 | +import android.graphics.Rect; |
| 31 | +import android.platform.test.annotations.AsbSecurityTest; |
| 32 | +import android.security.cts.R; |
| 33 | +import android.support.test.uiautomator.By; |
| 34 | +import android.support.test.uiautomator.BySelector; |
| 35 | +import android.support.test.uiautomator.UiDevice; |
| 36 | +import android.support.test.uiautomator.UiObject2; |
| 37 | +import android.support.test.uiautomator.Until; |
| 38 | + |
| 39 | +import androidx.test.runner.AndroidJUnit4; |
| 40 | + |
| 41 | +import com.android.sts.common.util.StsExtraBusinessLogicTestCase; |
| 42 | + |
| 43 | +import java.util.concurrent.CompletableFuture; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | +import java.util.regex.Pattern; |
| 46 | + |
| 47 | +import org.junit.Test; |
| 48 | +import org.junit.runner.RunWith; |
| 49 | + |
| 50 | +@RunWith(AndroidJUnit4.class) |
| 51 | +public class CVE_2021_0600 extends StsExtraBusinessLogicTestCase { |
| 52 | + private static final long TIMEOUT_MS = 5000; |
| 53 | + private CompletableFuture<String> mPocActivityReturn; |
| 54 | + private UiDevice mDevice; |
| 55 | + private Context mContext; |
| 56 | + |
| 57 | + // b/179042963 |
| 58 | + // Vulnerable package : com.android.settings (As per AOSP code) |
| 59 | + // Vulnerable app : Settings.apk (As per AOSP code) |
| 60 | + @AsbSecurityTest(cveBugId = 179042963) |
| 61 | + @Test |
| 62 | + public void testPocCVE_2021_0600() { |
| 63 | + try { |
| 64 | + Instrumentation instrumentation = getInstrumentation(); |
| 65 | + mDevice = UiDevice.getInstance(instrumentation); |
| 66 | + mContext = instrumentation.getContext(); |
| 67 | + |
| 68 | + // Registering a broadcast receiver to receive broadcast from PocActivity. |
| 69 | + mPocActivityReturn = new CompletableFuture<>(); |
| 70 | + BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { |
| 71 | + @Override |
| 72 | + public void onReceive(Context context, Intent intent) { |
| 73 | + try { |
| 74 | + mPocActivityReturn.complete(intent.getStringExtra( |
| 75 | + mContext.getString(R.string.cve_2021_0600_keyException))); |
| 76 | + } catch (Exception e) { |
| 77 | + // ignore. |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + mContext.registerReceiver(broadcastReceiver, |
| 82 | + new IntentFilter(mContext.getString(R.string.cve_2021_0600_action))); |
| 83 | + |
| 84 | + // Launch the PocActivity which in turn starts DeviceAdminAdd activity with normal |
| 85 | + // text as 'explanation'. |
| 86 | + Intent intent = new Intent(mContext, PocActivity.class); |
| 87 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 88 | + intent.putExtra(mContext.getString(R.string.cve_2021_0600_keyHtml), false); |
| 89 | + mContext.startActivity(intent); |
| 90 | + String pocActivityException = mPocActivityReturn.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 91 | + assumeTrue(pocActivityException, pocActivityException.trim() |
| 92 | + .equals(mContext.getString(R.string.cve_2021_0600_noException))); |
| 93 | + |
| 94 | + // Get the height of the normal text with no formatting. Because width is same both |
| 95 | + // with and without fix, height is being used for comparing the with and without |
| 96 | + // fix behaviour. |
| 97 | + int heightWoHtml = getVulnerableUIHeight(); |
| 98 | + assumeTrue(heightWoHtml != -1); |
| 99 | + |
| 100 | + // Launch PocActivity again such that DeviceAdminAdd activity starts with formatted text |
| 101 | + // this time. |
| 102 | + mPocActivityReturn = new CompletableFuture<>(); |
| 103 | + intent.putExtra(mContext.getString(R.string.cve_2021_0600_keyHtml), true); |
| 104 | + mContext.startActivity(intent); |
| 105 | + pocActivityException = mPocActivityReturn.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 106 | + assumeTrue(pocActivityException, pocActivityException |
| 107 | + .equalsIgnoreCase(mContext.getString(R.string.cve_2021_0600_noException))); |
| 108 | + |
| 109 | + // Get the height of HTML text with formatting. |
| 110 | + int heightWithHtml = getVulnerableUIHeight(); |
| 111 | + assumeTrue(heightWithHtml != -1); |
| 112 | + |
| 113 | + // On vulnerable device, the text displayed on the screen will be HTML formatted, so |
| 114 | + // there will be considerable increase in height of the text due to <h1> tag, if there |
| 115 | + // is at least 20% increase in height, the test will fail. |
| 116 | + assertFalse(mContext.getString(R.string.cve_2021_0600_failMsg), |
| 117 | + heightWithHtml > 1.2 * heightWoHtml); |
| 118 | + } catch (Exception e) { |
| 119 | + assumeNoException(e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private int getVulnerableUIHeight() { |
| 124 | + Pattern pattern = Pattern.compile(mContext.getString(R.string.cve_2021_0600_pattern), |
| 125 | + Pattern.CASE_INSENSITIVE); |
| 126 | + BySelector selector = By.text(pattern); |
| 127 | + assumeTrue(mContext.getString(R.string.cve_2021_0600_patternNotFound, pattern), |
| 128 | + mDevice.wait(Until.hasObject(selector), TIMEOUT_MS)); |
| 129 | + UiObject2 obj = mDevice.findObject(selector); |
| 130 | + if (obj != null && obj.getText() != null |
| 131 | + && obj.getText().contains(mContext.getString(R.string.cve_2021_0600_targetText))) { |
| 132 | + Rect bounds = obj.getVisibleBounds(); |
| 133 | + return bounds.bottom - bounds.top; |
| 134 | + } |
| 135 | + return -1; |
| 136 | + } |
| 137 | +} |
0 commit comments