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

Commit 70447223 authored by Gavin Corkery's avatar Gavin Corkery
Browse files

Add isSameApp to PackageManagerInternal

Adds a new method to PackageManagerInternal that checks
if a given package belongs to the calling uid. This logic also
accounts for calling uids in the sdk sandbox range.
Deduplicates two methods in NotificationManager and ActivityTaskManager
which have implemented this logic.

Bug: 219750831
Test: atest NotificationManagerServiceTest
Test: Manual test using Sdk Sandbox test app
Change-Id: I1c566f75c81112dfeeb664827dcb27768959c377
parent c4293b56
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -194,6 +194,19 @@ public abstract class PackageManagerInternal {
     */
    public abstract boolean isPermissionsReviewRequired(String packageName, int userId);


    /**
     * Gets whether a given package name belongs to the calling uid. If the calling uid is an
     * {@link Process#isSdkSandboxUid(int) sdk sandbox uid}, checks whether the package name is
     * equal to {@link PackageManager#getSdkSandboxPackageName()}.
     *
     * @param packageName The package name to check.
     * @param callingUid The calling uid.
     * @param userId The user under which to check.
     * @return True if the package name belongs to the calling uid.
     */
    public abstract boolean isSameApp(String packageName, int callingUid, int userId);

    /**
     * Retrieve all of the information we know about a particular package/application.
     * @param filterCallingUid The results will be filtered in the context of this UID instead
+2 −21
Original line number Diff line number Diff line
@@ -9650,27 +9650,8 @@ public class NotificationManagerService extends SystemService {
        if (uid == Process.ROOT_UID && ROOT_PKG.equals(pkg)) {
            return;
        }
        try {
            ApplicationInfo ai = mPackageManager.getApplicationInfo(
                    pkg, 0, userId);
            if (ai == null) {
                throw new SecurityException("Unknown package " + pkg);
            }
            if (!UserHandle.isSameApp(ai.uid, uid)) {
                throw new SecurityException("Calling uid " + uid + " gave package "
                        + pkg + " which is owned by uid " + ai.uid);
            }
        } catch (RemoteException re) {
            throw new SecurityException("Unknown package " + pkg + "\n" + re);
        }
    }
    private boolean isCallerSameApp(String pkg) {
        try {
            checkCallerIsSameApp(pkg);
            return true;
        } catch (SecurityException e) {
            return false;
        if (!mPackageManagerInternal.isSameApp(pkg, uid, userId)) {
            throw new SecurityException("Package " + pkg + " is not owned by uid " + uid);
        }
    }
+13 −0
Original line number Diff line number Diff line
@@ -6942,6 +6942,19 @@ public class PackageManagerService extends IPackageManager.Stub
            return PackageManagerService.this.getKnownPackageNamesInternal(knownPackage, userId);
        }

        @Override
        public boolean isSameApp(@Nullable String packageName, int callingUid, int userId) {
            if (packageName == null) {
                return false;
            }

            if (Process.isSdkSandboxUid(callingUid)) {
                return packageName.equals(getSdkSandboxPackageName());
            }
            int uid = getPackageUid(packageName, 0, userId);
            return UserHandle.isSameApp(uid, callingUid);
        }

        @Override
        public boolean isResolveActivityComponent(ComponentInfo component) {
            return mResolveActivity.packageName.equals(component.packageName)
+2 −12
Original line number Diff line number Diff line
@@ -2232,18 +2232,8 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
     * Return true if callingUid is system, or packageName belongs to that callingUid.
     */
    private boolean isSameApp(int callingUid, @Nullable String packageName) {
        try {
        if (callingUid != 0 && callingUid != SYSTEM_UID) {
                if (packageName == null) {
                    return false;
                }
                final int uid = AppGlobals.getPackageManager().getPackageUid(packageName,
                        PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
                        UserHandle.getUserId(callingUid));
                return UserHandle.isSameApp(callingUid, uid);
            }
        } catch (RemoteException e) {
            // Should not happen
            return mPmInternal.isSameApp(packageName, callingUid, UserHandle.getUserId(callingUid));
        }
        return true;
    }
+5 −0
Original line number Diff line number Diff line
@@ -420,6 +420,11 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                    return getApplicationInfo((String) args[0], mUid);
                });
        when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid);
        when(mPackageManagerInternal.isSameApp(anyString(), anyInt(), anyInt())).thenAnswer(
                (Answer<Boolean>) invocation -> {
                    Object[] args = invocation.getArguments();
                    return (int) args[1] == mUid;
                });
        final LightsManager mockLightsManager = mock(LightsManager.class);
        when(mockLightsManager.getLight(anyInt())).thenReturn(mock(LogicalLight.class));
        when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
Loading