Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be722d13 authored by Thiébaud Weksteen's avatar Thiébaud Weksteen Committed by Android (Google) Code Review
Browse files

Merge "Transition PlatformCompat to @EnforcePermission"

parents 4ec356ec f68263fc
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ interface IPlatformCompat {
     * @param appInfo  representing the affected app
     * @throws SecurityException if logging is not allowed
     */
    @EnforcePermission("LOG_COMPAT_CHANGE")
    void reportChange(long changeId, in ApplicationInfo appInfo);

    /**
@@ -60,6 +61,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app in question
     * @throws SecurityException if logging is not allowed
     */
    @EnforcePermission("LOG_COMPAT_CHANGE")
    void reportChangeByPackageName(long changeId, in String packageName, int userId);

    /**
@@ -72,6 +74,7 @@ interface IPlatformCompat {
     * @param uid      the UID of the app in question
     * @throws SecurityException if logging is not allowed
     */
    @EnforcePermission("LOG_COMPAT_CHANGE")
    void reportChangeByUid(long changeId, int uid);

    /**
@@ -90,6 +93,7 @@ interface IPlatformCompat {
     * @return {@code true} if the change is enabled for the current app
     * @throws SecurityException if logging or reading compat confis is not allowed
     */
    @EnforcePermission(allOf={"LOG_COMPAT_CHANGE", "READ_COMPAT_CHANGE_CONFIG"})
    boolean isChangeEnabled(long changeId, in ApplicationInfo appInfo);

    /**
@@ -115,6 +119,7 @@ interface IPlatformCompat {
     * @return {@code true} if the change is enabled for the current app
     * @throws SecurityException if logging or reading compat confis is not allowed
     */
    @EnforcePermission(allOf={"LOG_COMPAT_CHANGE", "READ_COMPAT_CHANGE_CONFIG"})
    boolean isChangeEnabledByPackageName(long changeId, in String packageName, int userId);

    /**
@@ -140,6 +145,7 @@ interface IPlatformCompat {
     * @return {@code true} if the change is enabled for the current app
     * @throws SecurityException if logging or reading compat confis is not allowed
     */
    @EnforcePermission(allOf={"LOG_COMPAT_CHANGE", "READ_COMPAT_CHANGE_CONFIG"})
    boolean isChangeEnabledByUid(long changeId, int uid);

    /**
@@ -151,6 +157,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app whose changes will be overridden
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    void setOverrides(in CompatibilityChangeConfig overrides, in String packageName);

    /**
@@ -171,6 +178,7 @@ interface IPlatformCompat {
     *                           on specific apps by their package name
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD")
    void putAllOverridesOnReleaseBuilds(in CompatibilityOverridesByPackageConfig overridesByPackage);

    /**
@@ -190,6 +198,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app whose changes will be overridden
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD")
    void putOverridesOnReleaseBuilds(in CompatibilityOverrideConfig overrides, in String packageName);

    /**
@@ -201,6 +210,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app whose changes will be overridden
     * @throws SecurityException if overriding changes is not permitted.
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    void setOverridesForTest(in CompatibilityChangeConfig overrides, in String packageName);

    /**
@@ -213,6 +223,7 @@ interface IPlatformCompat {
     * @return {@code true} if an override existed
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    boolean clearOverride(long changeId, String packageName);

    /**
@@ -225,6 +236,7 @@ interface IPlatformCompat {
     * @return {@code true} if an override existed
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    boolean clearOverrideForTest(long changeId, String packageName);

    /**
@@ -245,6 +257,7 @@ interface IPlatformCompat {
     *                                   removed for specific apps by their package name
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD")
    void removeAllOverridesOnReleaseBuilds(in CompatibilityOverridesToRemoveByPackageConfig overridesToRemoveByPackage);

    /**
@@ -266,6 +279,7 @@ interface IPlatformCompat {
     *                            default behaviour
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD")
    void removeOverridesOnReleaseBuilds(in CompatibilityOverridesToRemoveConfig overridesToRemove, in String packageName);

    /**
@@ -280,6 +294,7 @@ interface IPlatformCompat {
     * @return The number of changes that were enabled.
     * @throws SecurityException if overriding changes is not permitted.
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    int enableTargetSdkChanges(in String packageName, int targetSdkVersion);

    /**
@@ -294,6 +309,7 @@ interface IPlatformCompat {
     * @return the number of changes that were disabled
     * @throws SecurityException if overriding changes is not permitted.
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    int disableTargetSdkChanges(in String packageName, int targetSdkVersion);

    /**
@@ -304,6 +320,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app whose overrides will be cleared
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    void clearOverrides(in String packageName);

    /**
@@ -314,6 +331,7 @@ interface IPlatformCompat {
     * @param packageName the package name of the app whose overrides will be cleared
     * @throws SecurityException if overriding changes is not permitted
     */
    @EnforcePermission("OVERRIDE_COMPAT_CHANGE_CONFIG")
    void clearOverridesForTest(in String packageName);

    /**
@@ -323,6 +341,7 @@ interface IPlatformCompat {
     * @return a {@link CompatibilityChangeConfig}, representing whether a change is enabled for
     * the given app or not
     */
    @EnforcePermission(allOf={"LOG_COMPAT_CHANGE", "READ_COMPAT_CHANGE_CONFIG"})
    CompatibilityChangeConfig getAppConfig(in ApplicationInfo appInfo);

    /**
@@ -330,6 +349,7 @@ interface IPlatformCompat {
     *
     * @return an array of {@link CompatibilityChangeInfo} known to the service
     */
    @EnforcePermission("READ_COMPAT_CHANGE_CONFIG")
    CompatibilityChangeInfo[] listAllChanges();

    /**
@@ -338,10 +358,12 @@ interface IPlatformCompat {
     *
     * @return an array of {@link CompatibilityChangeInfo}
     */
    @RequiresNoPermission
    CompatibilityChangeInfo[] listUIChanges();

    /**
     * Gets an instance that can determine whether a changeid can be overridden for a package name.
     */
    @RequiresNoPermission
    IOverrideValidator getOverrideValidator();
}
+28 −71
Original line number Diff line number Diff line
@@ -20,9 +20,9 @@ import static android.Manifest.permission.LOG_COMPAT_CHANGE;
import static android.Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG;
import static android.Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD;
import static android.Manifest.permission.READ_COMPAT_CHANGE_CONFIG;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Process.SYSTEM_UID;

import android.annotation.EnforcePermission;
import android.annotation.RequiresNoPermission;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.IActivityManager;
@@ -93,15 +93,15 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(LOG_COMPAT_CHANGE)
    public void reportChange(long changeId, ApplicationInfo appInfo) {
        checkCompatChangeLogPermission();
        reportChangeInternal(changeId, appInfo.uid, ChangeReporter.STATE_LOGGED);
    }

    @Override
    @EnforcePermission(LOG_COMPAT_CHANGE)
    public void reportChangeByPackageName(long changeId, String packageName,
            @UserIdInt int userId) {
        checkCompatChangeLogPermission();
        ApplicationInfo appInfo = getApplicationInfo(packageName, userId);
        if (appInfo != null) {
            reportChangeInternal(changeId, appInfo.uid, ChangeReporter.STATE_LOGGED);
@@ -109,8 +109,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(LOG_COMPAT_CHANGE)
    public void reportChangeByUid(long changeId, int uid) {
        checkCompatChangeLogPermission();
        reportChangeInternal(changeId, uid, ChangeReporter.STATE_LOGGED);
    }

@@ -119,15 +119,15 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(allOf = {LOG_COMPAT_CHANGE, READ_COMPAT_CHANGE_CONFIG})
    public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
        checkCompatChangeReadAndLogPermission();
        return isChangeEnabledInternal(changeId, appInfo);
    }

    @Override
    @EnforcePermission(allOf = {LOG_COMPAT_CHANGE, READ_COMPAT_CHANGE_CONFIG})
    public boolean isChangeEnabledByPackageName(long changeId, String packageName,
            @UserIdInt int userId) {
        checkCompatChangeReadAndLogPermission();
        ApplicationInfo appInfo = getApplicationInfo(packageName, userId);
        if (appInfo == null) {
            return mCompatConfig.willChangeBeEnabled(changeId, packageName);
@@ -136,8 +136,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(allOf = {LOG_COMPAT_CHANGE, READ_COMPAT_CHANGE_CONFIG})
    public boolean isChangeEnabledByUid(long changeId, int uid) {
        checkCompatChangeReadAndLogPermission();
        String[] packages = mContext.getPackageManager().getPackagesForUid(uid);
        if (packages == null || packages.length == 0) {
            return mCompatConfig.defaultChangeIdValue(changeId);
@@ -197,8 +197,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public void setOverrides(CompatibilityChangeConfig overrides, String packageName) {
        checkCompatChangeOverridePermission();
        Map<Long, PackageOverride> overridesMap = new HashMap<>();
        for (long change : overrides.enabledChanges()) {
            overridesMap.put(change, new PackageOverride.Builder().setEnabled(true).build());
@@ -213,8 +213,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public void setOverridesForTest(CompatibilityChangeConfig overrides, String packageName) {
        checkCompatChangeOverridePermission();
        Map<Long, PackageOverride> overridesMap = new HashMap<>();
        for (long change : overrides.enabledChanges()) {
            overridesMap.put(change, new PackageOverride.Builder().setEnabled(true).build());
@@ -228,9 +228,9 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD)
    public void putAllOverridesOnReleaseBuilds(
            CompatibilityOverridesByPackageConfig overridesByPackage) {
        checkCompatChangeOverrideOverridablePermission();
        for (CompatibilityOverrideConfig overrides :
                overridesByPackage.packageNameToOverrides.values()) {
            checkAllCompatOverridesAreOverridable(overrides.overrides.keySet());
@@ -239,16 +239,16 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD)
    public void putOverridesOnReleaseBuilds(CompatibilityOverrideConfig overrides,
            String packageName) {
        checkCompatChangeOverrideOverridablePermission();
        checkAllCompatOverridesAreOverridable(overrides.overrides.keySet());
        mCompatConfig.addPackageOverrides(overrides, packageName, /* skipUnknownChangeIds= */ true);
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public int enableTargetSdkChanges(String packageName, int targetSdkVersion) {
        checkCompatChangeOverridePermission();
        int numChanges =
                mCompatConfig.enableTargetSdkChangesForPackage(packageName, targetSdkVersion);
        killPackage(packageName);
@@ -256,8 +256,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public int disableTargetSdkChanges(String packageName, int targetSdkVersion) {
        checkCompatChangeOverridePermission();
        int numChanges =
                mCompatConfig.disableTargetSdkChangesForPackage(packageName, targetSdkVersion);
        killPackage(packageName);
@@ -265,36 +265,36 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public void clearOverrides(String packageName) {
        checkCompatChangeOverridePermission();
        mCompatConfig.removePackageOverrides(packageName);
        killPackage(packageName);
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public void clearOverridesForTest(String packageName) {
        checkCompatChangeOverridePermission();
        mCompatConfig.removePackageOverrides(packageName);
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public boolean clearOverride(long changeId, String packageName) {
        checkCompatChangeOverridePermission();
        boolean existed = mCompatConfig.removeOverride(changeId, packageName);
        killPackage(packageName);
        return existed;
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
    public boolean clearOverrideForTest(long changeId, String packageName) {
        checkCompatChangeOverridePermission();
        return mCompatConfig.removeOverride(changeId, packageName);
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD)
    public void removeAllOverridesOnReleaseBuilds(
            CompatibilityOverridesToRemoveByPackageConfig overridesToRemoveByPackage) {
        checkCompatChangeOverrideOverridablePermission();
        for (CompatibilityOverridesToRemoveConfig overridesToRemove :
                overridesToRemoveByPackage.packageNameToOverridesToRemove.values()) {
            checkAllCompatOverridesAreOverridable(overridesToRemove.changeIds);
@@ -303,27 +303,28 @@ public class PlatformCompat extends IPlatformCompat.Stub {
    }

    @Override
    @EnforcePermission(OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD)
    public void removeOverridesOnReleaseBuilds(
            CompatibilityOverridesToRemoveConfig overridesToRemove,
            String packageName) {
        checkCompatChangeOverrideOverridablePermission();
        checkAllCompatOverridesAreOverridable(overridesToRemove.changeIds);
        mCompatConfig.removePackageOverrides(overridesToRemove, packageName);
    }

    @Override
    @EnforcePermission(allOf = {LOG_COMPAT_CHANGE, READ_COMPAT_CHANGE_CONFIG})
    public CompatibilityChangeConfig getAppConfig(ApplicationInfo appInfo) {
        checkCompatChangeReadAndLogPermission();
        return mCompatConfig.getAppConfig(appInfo);
    }

    @Override
    @EnforcePermission(READ_COMPAT_CHANGE_CONFIG)
    public CompatibilityChangeInfo[] listAllChanges() {
        checkCompatChangeReadPermission();
        return mCompatConfig.dumpChanges();
    }

    @Override
    @RequiresNoPermission
    public CompatibilityChangeInfo[] listUIChanges() {
        return Arrays.stream(listAllChanges()).filter(this::isShownInUI).toArray(
                CompatibilityChangeInfo[]::new);
@@ -362,11 +363,15 @@ public class PlatformCompat extends IPlatformCompat.Stub {
        if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, "platform_compat", pw)) {
            return;
        }
        checkCompatChangeReadAndLogPermission();
        mContext.enforceCallingOrSelfPermission(
                READ_COMPAT_CHANGE_CONFIG, "Cannot read compat change");
        mContext.enforceCallingOrSelfPermission(
                LOG_COMPAT_CHANGE, "Cannot read log compat change usage");
        mCompatConfig.dumpConfig(pw);
    }

    @Override
    @RequiresNoPermission
    public IOverrideValidator getOverrideValidator() {
        return mCompatConfig.getOverrideValidator();
    }
@@ -414,49 +419,6 @@ public class PlatformCompat extends IPlatformCompat.Stub {
        }
    }

    private void checkCompatChangeLogPermission() throws SecurityException {
        // Don't check for permissions within the system process
        if (Binder.getCallingUid() == SYSTEM_UID) {
            return;
        }
        if (mContext.checkCallingOrSelfPermission(LOG_COMPAT_CHANGE) != PERMISSION_GRANTED) {
            throw new SecurityException("Cannot log compat change usage");
        }
    }

    private void checkCompatChangeReadPermission() {
        // Don't check for permissions within the system process
        if (Binder.getCallingUid() == SYSTEM_UID) {
            return;
        }
        if (mContext.checkCallingOrSelfPermission(READ_COMPAT_CHANGE_CONFIG)
                != PERMISSION_GRANTED) {
            throw new SecurityException("Cannot read compat change");
        }
    }

    private void checkCompatChangeOverridePermission() {
        // Don't check for permissions within the system process
        if (Binder.getCallingUid() == SYSTEM_UID) {
            return;
        }
        if (mContext.checkCallingOrSelfPermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
                != PERMISSION_GRANTED) {
            throw new SecurityException("Cannot override compat change");
        }
    }

    private void checkCompatChangeOverrideOverridablePermission() {
        // Don't check for permissions within the system process
        if (Binder.getCallingUid() == SYSTEM_UID) {
            return;
        }
        if (mContext.checkCallingOrSelfPermission(OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD)
                != PERMISSION_GRANTED) {
            throw new SecurityException("Cannot override compat change");
        }
    }

    private void checkAllCompatOverridesAreOverridable(Collection<Long> changeIds) {
        for (Long changeId : changeIds) {
            if (isKnownChangeId(changeId) && !mCompatConfig.isOverridable(changeId)) {
@@ -466,11 +428,6 @@ public class PlatformCompat extends IPlatformCompat.Stub {
        }
    }

    private void checkCompatChangeReadAndLogPermission() {
        checkCompatChangeReadPermission();
        checkCompatChangeLogPermission();
    }

    private boolean isShownInUI(CompatibilityChangeInfo change) {
        if (change.getLoggingOnly()) {
            return false;