|
| 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 |
| 18 | + |
| 19 | +import android.content.ComponentName |
| 20 | +import android.content.Intent |
| 21 | +import android.platform.test.annotations.AsbSecurityTest |
| 22 | +import android.service.quicksettings.TileService |
| 23 | +import android.util.Log |
| 24 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 25 | +import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation |
| 26 | +import com.android.compatibility.common.util.BlockingBroadcastReceiver |
| 27 | +import com.android.compatibility.common.util.SystemUtil |
| 28 | +import com.android.sts.common.util.StsExtraBusinessLogicTestCase |
| 29 | +import org.junit.After |
| 30 | +import org.junit.Assert.fail |
| 31 | +import org.junit.Assume.assumeTrue |
| 32 | +import org.junit.Before |
| 33 | +import org.junit.Test |
| 34 | +import org.junit.runner.RunWith |
| 35 | + |
| 36 | +@RunWith(AndroidJUnit4::class) |
| 37 | +class Bug_300903792 : StsExtraBusinessLogicTestCase() { |
| 38 | + |
| 39 | + @Before |
| 40 | + fun setUp() { |
| 41 | + assumeTrue(TileService.isQuickSettingsSupported()) |
| 42 | + installPackage(TILE_SERVICE_APP_LOCATION) |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + fun tearDown() { |
| 47 | + SystemUtil.runShellCommand(REMOVE_TILE_COMMAND) |
| 48 | + Log.d("TestRunner", "Uninstalling $TILE_SERVICE_PACKAGE") |
| 49 | + uninstallPackage(TILE_SERVICE_PACKAGE) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @AsbSecurityTest(cveBugId = [300903792]) |
| 54 | + fun testPocBug_300903792() { |
| 55 | + val context = getInstrumentation().context |
| 56 | + val nullBindingReceiver = BlockingBroadcastReceiver(context, ON_NULL_BINDING) |
| 57 | + // First we add the tile, we should receive a broadcast once it has bound. |
| 58 | + // We expect that the tile will be bound to notify `onTileAdded` and onNullBinding will |
| 59 | + // happen. |
| 60 | + try { |
| 61 | + nullBindingReceiver.register() |
| 62 | + SystemUtil.runShellCommand(ADD_TILE_COMMAND) |
| 63 | + nullBindingReceiver.awaitForBroadcast(ONE_MINUTE_IN_MILLIS) |
| 64 | + } finally { |
| 65 | + nullBindingReceiver.unregisterQuietly() |
| 66 | + } |
| 67 | + |
| 68 | + val backgroundActivityStarted = |
| 69 | + BlockingBroadcastReceiver(context, BACKGROUND_ACTIVITY_STARTED) |
| 70 | + // We start an activity that will schedule another activity to start and then go home |
| 71 | + // (putting itself in the background). We expect that the backgroundActivity is not started, |
| 72 | + // but if the security issue is not patched, it will. |
| 73 | + try { |
| 74 | + backgroundActivityStarted.register() |
| 75 | + context.startActivity( |
| 76 | + Intent() |
| 77 | + .setComponent(ACTIVITY_STARTER_COMPONENT) |
| 78 | + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 79 | + ) |
| 80 | + val intent = backgroundActivityStarted.awaitForBroadcast(ONE_MINUTE_IN_MILLIS) |
| 81 | + if (intent != null) { |
| 82 | + fail("Vulnerable to b/300903792! Activity started from the background") |
| 83 | + } |
| 84 | + } finally { |
| 85 | + backgroundActivityStarted.unregisterQuietly() |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private fun installPackage(apkPath: String) { |
| 90 | + val result = SystemUtil.runShellCommand("pm install -r $apkPath") |
| 91 | + Log.d("security", "Install result: $result") |
| 92 | + } |
| 93 | + |
| 94 | + private fun uninstallPackage(packageName: String) { |
| 95 | + SystemUtil.runShellCommand("pm uninstall $packageName") |
| 96 | + } |
| 97 | + |
| 98 | + companion object { |
| 99 | + private const val TILE_SERVICE_APP_LOCATION = |
| 100 | + "/data/local/tmp/cts/security/TileServiceNullBindingTestApp.apk" |
| 101 | + private const val TILE_SERVICE_PACKAGE = "android.security.cts.tileservice" |
| 102 | + private const val TILE_SERVICE_NAME = ".NullBindingTileService" |
| 103 | + private const val ACTIVITY_STARTER_NAME = ".ActivityStarterActivity" |
| 104 | + |
| 105 | + private val TILE_SERVICE_COMPONENT = |
| 106 | + ComponentName.createRelative(TILE_SERVICE_PACKAGE, TILE_SERVICE_NAME) |
| 107 | + private val ACTIVITY_STARTER_COMPONENT = |
| 108 | + ComponentName.createRelative(TILE_SERVICE_PACKAGE, ACTIVITY_STARTER_NAME) |
| 109 | + |
| 110 | + private const val BACKGROUND_ACTIVITY_STARTED = |
| 111 | + "android.security.cts.tileservice.BACKGROUND_ACTIVITY_STARTED" |
| 112 | + |
| 113 | + private const val ON_NULL_BINDING = "android.security.cts.tileservice.ON_NULL_BINDING" |
| 114 | + |
| 115 | + private val ADD_TILE_COMMAND = |
| 116 | + "cmd statusbar add-tile ${TILE_SERVICE_COMPONENT.flattenToString()}" |
| 117 | + private val REMOVE_TILE_COMMAND = |
| 118 | + "cmd statusbar remove-tile ${TILE_SERVICE_COMPONENT.flattenToString()}" |
| 119 | + |
| 120 | + private const val ONE_MINUTE_IN_MILLIS = 60 * 1000L |
| 121 | + } |
| 122 | +} |
0 commit comments