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

Commit d32b7a0a authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Stop relying on AppOpsManager#checkPackage(), which is deprecated

With this CL,

  InputMethodUtils#checkIfPackageBelongsToUid()

starts using

  PackageManagerInternal#getPackageUid()

instead of

  AppOpsManager#checkPackage(),

which is deprecated [1].

There should be no semantic change in this CL.

 [1]: I90215a638a3c380fcffde2b9209c4386f8fd70ea
      a04b9ab4

Bug: 234882948
Test: presubmit
Change-Id: I069b058b4240fb7ab638915b03d433e32660870f
parent 361ed635
Loading
Loading
Loading
Loading
+4 −7
Original line number Original line Diff line number Diff line
@@ -69,7 +69,6 @@ import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.app.ActivityManagerInternal;
import android.app.AppGlobals;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.KeyguardManager;
import android.app.KeyguardManager;
import android.app.Notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationManager;
@@ -309,7 +308,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    final boolean mHasFeature;
    final boolean mHasFeature;
    private final ArrayMap<String, List<InputMethodSubtype>> mAdditionalSubtypeMap =
    private final ArrayMap<String, List<InputMethodSubtype>> mAdditionalSubtypeMap =
            new ArrayMap<>();
            new ArrayMap<>();
    private final AppOpsManager mAppOpsManager;
    private final UserManager mUserManager;
    private final UserManager mUserManager;
    private final UserManagerInternal mUserManagerInternal;
    private final UserManagerInternal mUserManagerInternal;
    private final InputMethodMenuController mMenuController;
    private final InputMethodMenuController mMenuController;
@@ -1737,7 +1735,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        mInputMethodDeviceConfigs = new InputMethodDeviceConfigs();
        mInputMethodDeviceConfigs = new InputMethodDeviceConfigs();
        mImeDisplayValidator = mWindowManagerInternal::getDisplayImePolicy;
        mImeDisplayValidator = mWindowManagerInternal::getDisplayImePolicy;
        mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
        mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
        mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
        mUserManager = mContext.getSystemService(UserManager.class);
        mUserManager = mContext.getSystemService(UserManager.class);
        mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
        mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
        mAccessibilityManager = AccessibilityManager.getInstance(context);
        mAccessibilityManager = AccessibilityManager.getInstance(context);
@@ -2523,7 +2520,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    null, null, null, selectedMethodId, getSequenceNumberLocked(), null, false);
                    null, null, null, selectedMethodId, getSequenceNumberLocked(), null, false);
        }
        }


        if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.mUid,
        if (!InputMethodUtils.checkIfPackageBelongsToUid(mPackageManagerInternal, cs.mUid,
                editorInfo.packageName)) {
                editorInfo.packageName)) {
            Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
            Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
                    + " uid=" + cs.mUid + " package=" + editorInfo.packageName);
                    + " uid=" + cs.mUid + " package=" + editorInfo.packageName);
@@ -3962,7 +3959,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            return false;
            return false;
        }
        }
        if (getCurIntentLocked() != null && InputMethodUtils.checkIfPackageBelongsToUid(
        if (getCurIntentLocked() != null && InputMethodUtils.checkIfPackageBelongsToUid(
                mAppOpsManager,
                mPackageManagerInternal,
                uid,
                uid,
                getCurIntentLocked().getComponent().getPackageName())) {
                getCurIntentLocked().getComponent().getPackageName())) {
            return true;
            return true;
@@ -4224,8 +4221,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        final int callingUid = Binder.getCallingUid();
        final int callingUid = Binder.getCallingUid();
        final ComponentName imeComponentName =
        final ComponentName imeComponentName =
                imeId != null ? ComponentName.unflattenFromString(imeId) : null;
                imeId != null ? ComponentName.unflattenFromString(imeId) : null;
        if (imeComponentName == null || !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager,
        if (imeComponentName == null || !InputMethodUtils.checkIfPackageBelongsToUid(
                callingUid, imeComponentName.getPackageName())) {
                mPackageManagerInternal, callingUid, imeComponentName.getPackageName())) {
            throw new SecurityException("Calling UID=" + callingUid + " does not belong to imeId="
            throw new SecurityException("Calling UID=" + callingUid + " does not belong to imeId="
                    + imeId);
                    + imeId);
        }
        }
+10 −10
Original line number Original line Diff line number Diff line
@@ -20,12 +20,12 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.UserHandleAware;
import android.annotation.UserHandleAware;
import android.annotation.UserIdInt;
import android.annotation.UserIdInt;
import android.app.AppOpsManager;
import android.content.ContentResolver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.res.Resources;
import android.content.res.Resources;
import android.os.Binder;
import android.os.Binder;
import android.os.Build;
import android.os.Build;
@@ -218,20 +218,20 @@ final class InputMethodUtils {
    /**
    /**
     * Returns true if a package name belongs to a UID.
     * Returns true if a package name belongs to a UID.
     *
     *
     * <p>This is a simple wrapper of {@link AppOpsManager#checkPackage(int, String)}.</p>
     * <p>This is a simple wrapper of
     * @param appOpsManager the {@link AppOpsManager} object to be used for the validation.
     * {@link PackageManagerInternal#getPackageUid(String, long, int)}.</p>
     * @param packageManagerInternal the {@link PackageManagerInternal} object to be used for the
     *                               validation.
     * @param uid the UID to be validated.
     * @param uid the UID to be validated.
     * @param packageName the package name.
     * @param packageName the package name.
     * @return {@code true} if the package name belongs to the UID.
     * @return {@code true} if the package name belongs to the UID.
     */
     */
    static boolean checkIfPackageBelongsToUid(AppOpsManager appOpsManager,
    static boolean checkIfPackageBelongsToUid(PackageManagerInternal packageManagerInternal,
            int uid, String packageName) {
            int uid, String packageName) {
        try {
        // PackageManagerInternal#getPackageUid() doesn't check MATCH_INSTANT/MATCH_APEX as of
            appOpsManager.checkPackage(uid, packageName);
        // writing. So setting 0 should be fine.
            return true;
        return packageManagerInternal.getPackageUid(packageName, 0 /* flags */,
        } catch (SecurityException e) {
                UserHandle.getUserId(uid)) == uid;
            return false;
        }
    }
    }


    /**
    /**