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

Commit 3a70f3b3 authored by rambowang's avatar rambowang
Browse files

Introduce utility methods to check system/phone process for multi-user

Telephony components have multiple user cases to check if the current
process is a system/phone process, or root/shell process.

The utility methods correctly take muliti-user cases into account and
help to reduce the duplication.

Bug: 328511085
Test: atest framewoksTelephonyTests
Flag: com.android.internal.telephony.flags.support_phone_uid_check_for_multiuser
Change-Id: I880262d637e55b1cc0aa6c95f487585dc935d731
parent db3e4d9d
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.flags.Flags;

import java.util.HashMap;
import java.util.HashSet;
@@ -716,10 +717,11 @@ public final class TelephonyPermissions {
    }

    private static int getCarrierPrivilegeStatus(Context context, int subId, int uid) {
        if (uid == Process.SYSTEM_UID || uid == Process.PHONE_UID) {
        if (isSystemOrPhone(uid)) {
            // Skip the check if it's one of these special uids
            return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
        }

        final long identity = Binder.clearCallingIdentity();
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
@@ -926,4 +928,30 @@ public final class TelephonyPermissions {
                || checkCallingOrSelfReadPhoneNumber(context, subId, callingPackage,
                callingFeatureId, message));
    }

    /**
     * @return true if the specified {@code uid} is for a system or phone process, no matter if runs
     * as system user or not.
     */
    public static boolean isSystemOrPhone(int uid) {
        if (Flags.supportPhoneUidCheckForMultiuser()) {
            return UserHandle.isSameApp(uid, Process.SYSTEM_UID) || UserHandle.isSameApp(uid,
                    Process.PHONE_UID);
        } else {
            return uid == Process.SYSTEM_UID || uid == Process.PHONE_UID;
        }
    }

    /**
     * @return true if the specified {@code uid} is for a ROOT or SHELL process, no matter if runs
     * as system user or not.
     */
    public static boolean isRootOrShell(int uid) {
        if (Flags.supportPhoneUidCheckForMultiuser()) {
            return UserHandle.isSameApp(uid, Process.ROOT_UID) || UserHandle.isSameApp(uid,
                    Process.SHELL_UID);
        } else {
            return uid == Process.ROOT_UID || uid == Process.SHELL_UID;
        }
    }
}