From a1d81b5842e5138f93ea691716031919018b34b2 Mon Sep 17 00:00:00 2001 From: Victor Hsieh <victorhsieh@google.com> Date: Fri, 20 Oct 2023 10:37:07 -0700 Subject: [PATCH] Test to ensure allowlisted non-system app doesn't break Bug: 277347456 Test: TamperedUpdatedSystemPackageTest Change-Id: I0e236e228d88160907f39b54e85c2c06cd868f62 --- .../host/Android.bp | 1 + .../test/TamperedUpdatedSystemPackageTest.kt | 33 ++++++++++++++++--- .../host/test-apps/Generic/Android.bp | 8 +++++ .../AndroidManifestDifferentPkgName.xml | 33 +++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 services/tests/PackageManagerServiceTests/host/test-apps/Generic/AndroidManifestDifferentPkgName.xml diff --git a/services/tests/PackageManagerServiceTests/host/Android.bp b/services/tests/PackageManagerServiceTests/host/Android.bp index 6eacef7670421..c617ec49ab32b 100644 --- a/services/tests/PackageManagerServiceTests/host/Android.bp +++ b/services/tests/PackageManagerServiceTests/host/Android.bp @@ -58,6 +58,7 @@ java_test_host { ":PackageManagerTestOverlayTarget", ":PackageManagerTestOverlayTargetNoOverlayable", ":PackageManagerTestAppDeclaresStaticLibrary", + ":PackageManagerTestAppDifferentPkgName", ":PackageManagerTestAppStub", ":PackageManagerTestAppUsesStaticLibrary", ":PackageManagerTestAppVersion1", diff --git a/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/TamperedUpdatedSystemPackageTest.kt b/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/TamperedUpdatedSystemPackageTest.kt index c4906041ea5de..304f605d5b95a 100644 --- a/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/TamperedUpdatedSystemPackageTest.kt +++ b/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/TamperedUpdatedSystemPackageTest.kt @@ -44,6 +44,10 @@ class TamperedUpdatedSystemPackageTest : BaseHostJUnit4Test() { private const val VERSION_TWO_ALT_KEY = "PackageManagerTestAppVersion2AltKey.apk" private const val VERSION_TWO_ALT_KEY_IDSIG = "PackageManagerTestAppVersion2AltKey.apk.idsig" + + private const val ANOTHER_PKG_NAME = "com.android.server.pm.test.test_app2" + private const val ANOTHER_PKG = "PackageManagerTestAppDifferentPkgName.apk" + private const val STRICT_SIGNATURE_CONFIG_PATH = "/system/etc/sysconfig/preinstalled-packages-strict-signature.xml" private const val TIMESTAMP_REFERENCE_FILE_PATH = "/data/local/tmp/timestamp.ref" @@ -74,6 +78,7 @@ class TamperedUpdatedSystemPackageTest : BaseHostJUnit4Test() { @After fun removeApk() { device.uninstallPackage(TEST_PKG_NAME) + device.uninstallPackage(ANOTHER_PKG_NAME) } @Before @@ -90,7 +95,9 @@ class TamperedUpdatedSystemPackageTest : BaseHostJUnit4Test() { .readText() .replace( "</config>", - "<require-strict-signature package=\"${TEST_PKG_NAME}\"/></config>" + "<require-strict-signature package=\"${TEST_PKG_NAME}\"/>" + + "<require-strict-signature package=\"${ANOTHER_PKG_NAME}\"/>" + + "</config>" ) writeText(newConfigText) } @@ -146,10 +153,7 @@ class TamperedUpdatedSystemPackageTest : BaseHostJUnit4Test() { tempFolder.newFile() ) assertThat(device.installPackage(versionTwoFile, true)).isNull() - val baseApkPath = device.executeShellCommand("pm path ${TEST_PKG_NAME}") - .lineSequence() - .first() - .replace("package:", "") + val baseApkPath = getBaseApkPath(TEST_PKG_NAME) assertThat(baseApkPath).doesNotContain(productPath.toString()) preparer.pushResourceFile(VERSION_TWO_ALT_KEY_IDSIG, baseApkPath.toString() + ".idsig") @@ -175,4 +179,23 @@ class TamperedUpdatedSystemPackageTest : BaseHostJUnit4Test() { assertThat(device.executeShellCommand("pm path ${TEST_PKG_NAME}")) .contains(productPath.toString()) } + + @Test + fun allowlistedPackageIsNotASystemApp() { + // If an allowlisted package isn't a system app, make sure install and boot still works + // normally. + assertThat(device.installJavaResourceApk(tempFolder, ANOTHER_PKG, /* reinstall */ false)) + .isNull() + assertThat(getBaseApkPath(ANOTHER_PKG_NAME)).startsWith("/data/app/") + + preparer.reboot() + assertThat(getBaseApkPath(ANOTHER_PKG_NAME)).startsWith("/data/app/") + } + + private fun getBaseApkPath(pkgName: String): String { + return device.executeShellCommand("pm path $pkgName") + .lineSequence() + .first() + .replace("package:", "") + } } diff --git a/services/tests/PackageManagerServiceTests/host/test-apps/Generic/Android.bp b/services/tests/PackageManagerServiceTests/host/test-apps/Generic/Android.bp index bee7c4019fc16..b826590b74402 100644 --- a/services/tests/PackageManagerServiceTests/host/test-apps/Generic/Android.bp +++ b/services/tests/PackageManagerServiceTests/host/test-apps/Generic/Android.bp @@ -76,3 +76,11 @@ android_test_helper_app { certificate: ":FrameworksServicesTests_keyset_A_cert", v4_signature: true, } + +android_test_helper_app { + name: "PackageManagerTestAppDifferentPkgName", + manifest: "AndroidManifestDifferentPkgName.xml", + srcs: [ + "src/**/*.kt", + ], +} diff --git a/services/tests/PackageManagerServiceTests/host/test-apps/Generic/AndroidManifestDifferentPkgName.xml b/services/tests/PackageManagerServiceTests/host/test-apps/Generic/AndroidManifestDifferentPkgName.xml new file mode 100644 index 0000000000000..0c5d36e389726 --- /dev/null +++ b/services/tests/PackageManagerServiceTests/host/test-apps/Generic/AndroidManifestDifferentPkgName.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2020 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="com.android.server.pm.test.test_app2" + android:versionCode="1" + > + + <permission + android:name="com.android.server.pm.test.test_app.TEST_PERMISSION" + android:protectionLevel="normal" + /> + + <application> + <activity android:name="com.android.server.pm.test.test_app.TestActivity" + android:label="PackageManagerTestApp" /> + </application> + +</manifest> -- GitLab