Let update owners can relinquish update ownership (5/n)

Add a new API to let the update owner can relinquish update ownership.
This API can be only used by the the current update owner.

Bug: 244413073
Test: atest CtsPackageInstallTestCases:UpdateOwnershipEnforcementTest
Change-Id: I8dda2c2502b9dc8db6b23be2b766dc738a9b6742
This commit is contained in:
Jackal Guo
2022-12-30 09:30:33 +08:00
parent d378c630bb
commit ec1c3c0193
7 changed files with 73 additions and 0 deletions

View File

@@ -12258,6 +12258,7 @@ package android.content.pm {
method @NonNull public java.util.List<android.content.pm.PackageManager.Property> queryProviderProperty(@NonNull String);
method @NonNull public java.util.List<android.content.pm.PackageManager.Property> queryReceiverProperty(@NonNull String);
method @NonNull public java.util.List<android.content.pm.PackageManager.Property> queryServiceProperty(@NonNull String);
method public void relinquishUpdateOwnership(@NonNull String);
method @Deprecated public abstract void removePackageFromPreferred(@NonNull String);
method public abstract void removePermission(@NonNull String);
method @RequiresPermission(value="android.permission.WHITELIST_RESTRICTED_PERMISSIONS", conditional=true) public boolean removeWhitelistedRestrictedPermission(@NonNull String, @NonNull String, int);

View File

@@ -3890,4 +3890,14 @@ public class ApplicationPackageManager extends PackageManager {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.SHOW_NEW_APP_INSTALLED_NOTIFICATION_ENABLED, 0) == 1;
}
@Override
public void relinquishUpdateOwnership(String targetPackage) {
Objects.requireNonNull(targetPackage);
try {
mPM.relinquishUpdateOwnership(targetPackage);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}

View File

@@ -214,6 +214,8 @@ interface IPackageManager {
@UnsupportedAppUsage
void setInstallerPackageName(in String targetPackage, in String installerPackageName);
void relinquishUpdateOwnership(in String targetPackage);
void setApplicationCategoryHint(String packageName, int categoryHint, String callerPackageName);
/** @deprecated rawr, don't call AIDL methods directly! */

View File

@@ -10866,4 +10866,16 @@ public abstract class PackageManager {
throw new UnsupportedOperationException(
"isShowNewAppInstalledNotificationEnabled not implemented in subclass");
}
/**
* Attempt to relinquish the update ownership of the given package. Only the current
* update owner of the given package can use this API or a SecurityException will be
* thrown.
*
* @param targetPackage The installed package whose update owner will be changed.
*/
public void relinquishUpdateOwnership(@NonNull String targetPackage) {
throw new UnsupportedOperationException(
"relinquishUpdateOwnership not implemented in subclass");
}
}

View File

@@ -6012,6 +6012,42 @@ public class PackageManagerService implements PackageSender, TestUtilityService
}
}
@Override
public void relinquishUpdateOwnership(String targetPackage) {
final int callingUid = Binder.getCallingUid();
final int callingUserId = UserHandle.getUserId(callingUid);
final Computer snapshot = snapshotComputer();
final PackageStateInternal targetPackageState =
snapshot.getPackageStateForInstalledAndFiltered(targetPackage, callingUid,
callingUserId);
if (targetPackageState == null) {
throw new IllegalArgumentException("Unknown target package: " + targetPackage);
}
final String targetUpdateOwnerPackageName =
targetPackageState.getInstallSource().mUpdateOwnerPackageName;
final PackageStateInternal targetUpdateOwnerPkgSetting =
targetUpdateOwnerPackageName == null ? null
: snapshot.getPackageStateInternal(targetUpdateOwnerPackageName);
if (targetUpdateOwnerPkgSetting == null) {
return;
}
final int callingAppId = UserHandle.getAppId(callingUid);
final int targetUpdateOwnerAppId = targetUpdateOwnerPkgSetting.getAppId();
if (callingAppId != Process.SYSTEM_UID
&& callingAppId != Process.SHELL_UID
&& callingAppId != targetUpdateOwnerAppId) {
throw new SecurityException("Caller is not the current update owner.");
}
commitPackageStateMutation(null /* initialState */, targetPackage,
state -> state.setUpdateOwner(null /* updateOwnerPackageName */));
scheduleWriteSettings();
}
@Override
public boolean setInstantAppCookie(String packageName, byte[] cookie, int userId) {
if (HIDE_EPHEMERAL_APIS) {

View File

@@ -291,6 +291,15 @@ public class PackageStateMutator {
return this;
}
@NonNull
@Override
public PackageStateWrite setUpdateOwner(@NonNull String updateOwnerPackageName) {
if (mState != null) {
mState.setUpdateOwnerPackage(updateOwnerPackageName);
}
return this;
}
private static class UserStateWriteWrapper implements PackageUserStateWrite {
@Nullable

View File

@@ -57,4 +57,7 @@ public interface PackageStateWrite {
@NonNull
PackageStateWrite setInstaller(@Nullable String installerPackageName, int installerPackageUid);
@NonNull
PackageStateWrite setUpdateOwner(@Nullable String updateOwnerPackageName);
}