From 2e1245013f5bdfcad8a8f93cc67e6bdaf70b804e Mon Sep 17 00:00:00 2001 From: Jackal Guo Date: Tue, 13 Dec 2022 15:06:00 +0800 Subject: [PATCH] Handle update-ownership tag For update ownership enforcement, using the config to enable the enforcement for the system apps. A config is expected to have "update-ownership" tags per package: ``` ``` Bug: 244413073 Test: atest FrameworksServicesTests:SystemConfigTest Change-Id: I38e996eb885a130f72f656b27731d9f31b16b366 --- .../java/com/android/server/SystemConfig.java | 25 +++++++++ .../server/systemconfig/SystemConfigTest.java | 52 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/services/core/java/com/android/server/SystemConfig.java b/services/core/java/com/android/server/SystemConfig.java index 75788f219d589..2fc07129435be 100644 --- a/services/core/java/com/android/server/SystemConfig.java +++ b/services/core/java/com/android/server/SystemConfig.java @@ -329,6 +329,8 @@ public class SystemConfig { private final Set mInstallConstraintsAllowlist = new ArraySet<>(); private String mModulesInstallerPackageName; + // Update ownership for system applications and the installers eligible to update them. + private final ArrayMap mUpdateOwnersForSystemApps = new ArrayMap<>(); /** * Map of system pre-defined, uniquely named actors; keys are namespace, @@ -475,6 +477,13 @@ public class SystemConfig { return mModulesInstallerPackageName; } + /** + * Gets the update owner of the given package from "update-ownership" tags in sysconfig. + */ + public @Nullable String getSystemAppUpdateOwnerPackageName(@NonNull String packageName) { + return mUpdateOwnersForSystemApps.get(packageName); + } + public ArraySet getAppDataIsolationWhitelistedApps() { return mAppDataIsolationWhitelistedApps; } @@ -1405,6 +1414,22 @@ public class SystemConfig { } XmlUtils.skipCurrentTag(parser); } break; + case "update-ownership": { + final String packageName = parser.getAttributeValue(null /* namespace */, + "package"); + final String installerName = parser.getAttributeValue(null /* namespace */, + "installer"); + if (TextUtils.isEmpty(packageName)) { + Slog.w(TAG, "<" + name + "> without valid package in " + permFile + + " at " + parser.getPositionDescription()); + } else if (TextUtils.isEmpty(installerName)) { + Slog.w(TAG, "<" + name + "> without valid installer in " + permFile + + " at " + parser.getPositionDescription()); + } else { + mUpdateOwnersForSystemApps.put(packageName, installerName); + } + XmlUtils.skipCurrentTag(parser); + } break; default: { Slog.w(TAG, "Tag " + name + " is unknown in " + permFile + " at " + parser.getPositionDescription()); diff --git a/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigTest.java b/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigTest.java index 16c213c77d108..d073f5bfebe47 100644 --- a/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigTest.java +++ b/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigTest.java @@ -594,6 +594,58 @@ public class SystemConfigTest { assertFooIsOnlySharedLibrary(); } + /** + * Tests that readPermissions works correctly for the tag: {@code update-ownership}. + */ + @Test + public void readPermissions_updateOwnership_successful() throws IOException { + final String contents = + "\n" + + " \n" + + ""; + final File folder = createTempSubfolder("folder"); + createTempFile(folder, "update_ownership.xml", contents); + + readPermissions(folder, /* Grant all permission flags */ ~0); + + assertThat(mSysConfig.getSystemAppUpdateOwnerPackageName("com.foo")) + .isEqualTo("com.bar"); + } + + /** + * Tests that readPermissions works correctly for the tag: {@code update-ownership}. + */ + @Test + public void readPermissions_updateOwnership_noPackage() throws IOException { + final String contents = + "\n" + + " \n" + + ""; + final File folder = createTempSubfolder("folder"); + createTempFile(folder, "update_ownership.xml", contents); + + readPermissions(folder, /* Grant all permission flags */ ~0); + + assertThat(mSysConfig.getSystemAppUpdateOwnerPackageName("com.foo")).isNull(); + } + + /** + * Tests that readPermissions works correctly for the tag: {@code update-ownership}. + */ + @Test + public void readPermissions_updateOwnership_noInstaller() throws IOException { + final String contents = + "\n" + + " \n" + + ""; + final File folder = createTempSubfolder("folder"); + createTempFile(folder, "update_ownership.xml", contents); + + readPermissions(folder, /* Grant all permission flags */ ~0); + + assertThat(mSysConfig.getSystemAppUpdateOwnerPackageName("com.foo")).isNull(); + } + private void parseSharedLibraries(String contents) throws IOException { File folder = createTempSubfolder("permissions_folder"); createTempFile(folder, "permissions.xml", contents);