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

Commit b1b35679 authored by Jordan Liu's avatar Jordan Liu
Browse files

Use checkpackage instead of comparing UID

Based on b/150979331 it seems this is the safer option.

Test: atest SmsPermissionsTest
Bug: 152347586
Change-Id: I0980775d4fba3f1e036361c22f3d29eec9527292
parent d1e49fb7
Loading
Loading
Loading
Loading
+9 −27
Original line number Diff line number Diff line
@@ -21,10 +21,8 @@ import android.app.AppOpsManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.service.carrier.CarrierMessagingService;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.telephony.Rlog;
@@ -84,21 +82,11 @@ public class SmsPermissions {
     * </ul>
     */
    public void enforceCallerIsImsAppOrCarrierApp(String message) {
        int callingUid = Binder.getCallingUid();
        String carrierImsPackage = CarrierSmsUtils.getCarrierImsPackageForIntent(mContext, mPhone,
                new Intent(CarrierMessagingService.SERVICE_INTERFACE));
        try {
            if (carrierImsPackage != null
                    && callingUid == mContext.getPackageManager().getPackageUid(
                    carrierImsPackage, 0)) {
        String carrierImsPackage = CarrierSmsUtils.getCarrierImsPackageForIntent(mContext,
                mPhone, new Intent(CarrierMessagingService.SERVICE_INTERFACE));
        if (carrierImsPackage != null && packageNameMatchesCallingUid(carrierImsPackage)) {
            return;
        }
        } catch (PackageManager.NameNotFoundException e) {
            if (Rlog.isLoggable("SMS", Log.DEBUG)) {
                loge("Cannot find configured carrier ims package");
            }
        }

        TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
                mContext, mPhone.getSubId(), message);
    }
@@ -187,19 +175,13 @@ public class SmsPermissions {
    @VisibleForTesting
    public boolean packageNameMatchesCallingUid(String packageName) {
        try {
            if (Binder.getCallingUid()
                    != mContext.getPackageManager().getPackageUid(packageName, 0)) {
                Log.e(LOG_TAG, "packageNameMatchesCallingUid: " + packageName + " uid "
                        + mContext.getPackageManager().getPackageUid(packageName, 0)
                        + " does not match calling uid " + Binder.getCallingUid());
                return false;
            }
        } catch (PackageManager.NameNotFoundException ex) {
            Log.e(LOG_TAG, "packageNameMatchesCallingUid: packageName " + packageName
                    + " not found");
            ((AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE))
                    .checkPackage(Binder.getCallingUid(), packageName);
            // If checkPackage doesn't throw an exception then we are the given package
            return true;
        } catch (SecurityException e) {
            return false;
        }
        return true;
    }

    @UnsupportedAppUsage
+7 −20
Original line number Diff line number Diff line
@@ -21,19 +21,17 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

import android.Manifest;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import org.junit.After;
import org.junit.Before;
@@ -259,28 +257,17 @@ public class SmsPermissionsTest extends TelephonyTest {

    @Test
    public void testPackageNameMatchesCallingUid() {
        PackageManager mockPackageManager = mock(PackageManager.class);
        doReturn(mockPackageManager).when(mMockContext).getPackageManager();
        AppOpsManager mockAppOpsManager = mock(AppOpsManager.class);
        Mockito.when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(
                mockAppOpsManager);

        // test matching case
        try {
            doReturn(Binder.getCallingUid()).when(mockPackageManager)
                    .getPackageUid(eq(PACKAGE), anyInt());
        } catch (Exception e) {
            Log.e(TAG, "testPackageNameMatchesCallingUid: unable to setup mocks");
            fail();
        }
        assertTrue(new SmsPermissions(mMockPhone, mMockContext, mMockAppOps)
                .packageNameMatchesCallingUid(PACKAGE));

        // test mis-match case
        try {
            doReturn(Binder.getCallingUid() + 1).when(mockPackageManager)
                    .getPackageUid(eq(PACKAGE), anyInt());
        } catch (Exception e) {
            Log.e(TAG, "testPackageNameMatchesCallingUid: unable to setup mocks");
            fail();
        }
        SecurityException e = new SecurityException("Test exception");
        doThrow(e).when(mockAppOpsManager).checkPackage(anyInt(), anyString());
        assertFalse(new SmsPermissions(mMockPhone, mMockContext, mMockAppOps)
                .packageNameMatchesCallingUid(PACKAGE));
    }