天天看点

禁止静态广播发送给第三方应用

frameworks/base / services/core/java/com/android/server/am/BroadcastQueue.java

 final void processNextBroadcast(boolean fromMsg) {

 int perm = mService.checkComponentPermission(info.activityInfo.permission,

                    r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,

                    info.activityInfo.exported);

            if (!skip && perm != PackageManager.PERMISSION_GRANTED) {

                if (!info.activityInfo.exported) {

                    Slog.w(TAG, "Permission Denial: broadcasting "

                            + r.intent.toString()

                            + " from " + r.callerPackage + " (pid=" + r.callingPid

                            + ", uid=" + r.callingUid + ")"

                            + " is not exported from uid " + info.activityInfo.applicationInfo.uid

                            + " due to receiver " + component.flattenToShortString());

                } else {

                    Slog.w(TAG, "Permission Denial: broadcasting "

                            + r.intent.toString()

                            + " from " + r.callerPackage + " (pid=" + r.callingPid

                            + ", uid=" + r.callingUid + ")"

                            + " requires " + info.activityInfo.permission

                            + " due to receiver " + component.flattenToShortString());

                }

                skip = true;

            } else if (!skip && info.activityInfo.permission != null) {

                final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission);

                if (opCode != AppOpsManager.OP_NONE

                        && mService.mAppOpsService.noteOperation(opCode, r.callingUid,

                                r.callerPackage) != AppOpsManager.MODE_ALLOWED) {

                    Slog.w(TAG, "Appop Denial: broadcasting "

                            + r.intent.toString()

                            + " from " + r.callerPackage + " (pid="

                            + r.callingPid + ", uid=" + r.callingUid + ")"

                            + " requires appop " + AppOpsManager.permissionToOp(

                                    info.activityInfo.permission)

                            + " due to registered receiver "

                            + component.flattenToShortString());

                    skip = true;

                }

            }

            if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&

                r.requiredPermissions != null && r.requiredPermissions.length > 0) {

                for (int i = 0; i < r.requiredPermissions.length; i++) {

                    String requiredPermission = r.requiredPermissions[i];

                    try {

                        perm = AppGlobals.getPackageManager().

                                checkPermission(requiredPermission,

                                        info.activityInfo.applicationInfo.packageName,

                                        UserHandle

                                                .getUserId(info.activityInfo.applicationInfo.uid));

                    } catch (RemoteException e) {

                        perm = PackageManager.PERMISSION_DENIED;

                    }

                    if (perm != PackageManager.PERMISSION_GRANTED) {

                        Slog.w(TAG, "Permission Denial: receiving "

                                + r.intent + " to "

                                + component.flattenToShortString()

                                + " requires " + requiredPermission

                                + " due to sender " + r.callerPackage

                                + " (uid " + r.callingUid + ")");

                        skip = true;

                        break;

                    }

                    int appOp = AppOpsManager.permissionToOpCode(requiredPermission);

                    if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp

                            && mService.mAppOpsService.noteOperation(appOp,

                            info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)

                            != AppOpsManager.MODE_ALLOWED) {

                        Slog.w(TAG, "Appop Denial: receiving "

                                + r.intent + " to "

                                + component.flattenToShortString()

                                + " requires appop " + AppOpsManager.permissionToOp(

                                requiredPermission)

                                + " due to sender " + r.callerPackage

                                + " (uid " + r.callingUid + ")");

                        skip = true;

                        break;

                    }

                }

            }

            if (!skip && r.appOp != AppOpsManager.OP_NONE

                    && mService.mAppOpsService.noteOperation(r.appOp,

                    info.activityInfo.applicationInfo.uid, info.activityInfo.packageName)

                    != AppOpsManager.MODE_ALLOWED) {

                Slog.w(TAG, "Appop Denial: receiving "

                        + r.intent + " to "

                        + component.flattenToShortString()

                        + " requires appop " + AppOpsManager.opToName(r.appOp)

                        + " due to sender " + r.callerPackage

                        + " (uid " + r.callingUid + ")");

                skip = true;

            }

            if (!skip) {

                skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,

                        r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);

            }

            boolean isSingleton = false;

            try {

                isSingleton = mService.isSingleton(info.activityInfo.processName,

                        info.activityInfo.applicationInfo,

                        info.activityInfo.name, info.activityInfo.flags);

            } catch (SecurityException e) {

                Slog.w(TAG, e.getMessage());

                skip = true;

            }

            if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {

                if (ActivityManager.checkUidPermission(

                        android.Manifest.permission.INTERACT_ACROSS_USERS,

                        info.activityInfo.applicationInfo.uid)

                                != PackageManager.PERMISSION_GRANTED) {

                    Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()

                            + " requests FLAG_SINGLE_USER, but app does not hold "

                            + android.Manifest.permission.INTERACT_ACROSS_USERS);

                    skip = true;

                }

            }

            if (!skip && info.activityInfo.applicationInfo.isInstantApp()

                    && r.callingUid != info.activityInfo.applicationInfo.uid) {

                Slog.w(TAG, "Instant App Denial: receiving "

                        + r.intent

                        + " to " + component.flattenToShortString()

                        + " due to sender " + r.callerPackage

                        + " (uid " + r.callingUid + ")"

                        + " Instant Apps do not support manifest receivers");

                skip = true;

            }

            if (!skip && r.callerInstantApp

                    && (info.activityInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_INSTANT_APP) == 0

                    && r.callingUid != info.activityInfo.applicationInfo.uid) {

                Slog.w(TAG, "Instant App Denial: receiving "

                        + r.intent

                        + " to " + component.flattenToShortString()

                        + " requires receiver have visibleToInstantApps set"

                        + " due to sender " + r.callerPackage

                        + " (uid " + r.callingUid + ")");

                skip = true;

            }

            if (!skip) {

                r.manifestCount++;

            } else {

                r.manifestSkipCount++;

            }

            if (r.curApp != null && r.curApp.crashing) {

                // If the target process is crashing, just skip it.

                Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r

                        + " to " + r.curApp + ": process crashing");

                skip = true;

            }

            if (!skip) {

                boolean isAvailable = false;

                try {

                    isAvailable = AppGlobals.getPackageManager().isPackageAvailable(

                            info.activityInfo.packageName,

                            UserHandle.getUserId(info.activityInfo.applicationInfo.uid));

                } catch (Exception e) {

                    // all such failures mean we skip this receiver

                    Slog.w(TAG, "Exception getting recipient info for "

                            + info.activityInfo.packageName, e);

                }

                if (!isAvailable) {

                    if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,

                            "Skipping delivery to " + info.activityInfo.packageName + " / "

                            + info.activityInfo.applicationInfo.uid

                            + " : package no longer available");

                    skip = true;

                }

            }

            // If permissions need a review before any of the app components can run, we drop

            // the broadcast and if the calling app is in the foreground and the broadcast is

            // explicit we launch the review UI passing it a pending intent to send the skipped

            // broadcast.

            if (mService.mPermissionReviewRequired && !skip) {

                if (!requestStartTargetPermissionsReviewIfNeededLocked(r,

                        info.activityInfo.packageName, UserHandle.getUserId(

                                info.activityInfo.applicationInfo.uid))) {

                    skip = true;

                }

            }

            // This is safe to do even if we are skipping the broadcast, and we need

            // this information now to evaluate whether it is going to be allowed to run.

            final int receiverUid = info.activityInfo.applicationInfo.uid;

            // If it's a singleton, it needs to be the same app or a special app

            if (r.callingUid != Process.SYSTEM_UID && isSingleton

                    && mService.isValidSingletonCall(r.callingUid, receiverUid)) {

                info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);

            }

            String targetProcess = info.activityInfo.processName;

            ProcessRecord app = mService.getProcessRecordLocked(targetProcess,

                    info.activityInfo.applicationInfo.uid, false);

            if (!skip) {

                final int allowed = mService.getAppStartModeLocked(

                        info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,

                        info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false);

                if (allowed != ActivityManager.APP_START_MODE_NORMAL) {

                    // We won't allow this receiver to be launched if the app has been

                    // completely disabled from launches, or it was not explicitly sent

                    // to it and the app is in a state that should not receive it

                    // (depending on how getAppStartModeLocked has determined that).

                    if (allowed == ActivityManager.APP_START_MODE_DISABLED) {

                        Slog.w(TAG, "Background execution disabled: receiving "

                                + r.intent + " to "

                                + component.flattenToShortString());

                        skip = true;

                    } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)

                            || (r.intent.getComponent() == null

                                && r.intent.getPackage() == null

                                && ((r.intent.getFlags()

                                        & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)

                                && !isSignaturePerm(r.requiredPermissions))) {

                        mService.addBackgroundCheckViolationLocked(r.intent.getAction(),

                                component.getPackageName());

                        Slog.w(TAG, "Background execution not allowed: receiving "

                                + r.intent + " to "

                                + component.flattenToShortString());

                        skip = true;

                    }

                }

            }

 //#ifedf 1

            //james add avoid third part app auto start via broadcast

            if(app==null&&((info.activityInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM)==0)) {

                Slog.w(TAG, "skip delivery "+r+" to third part app :"+info.activityInfo.packageName);

                skip = true;

            }

            //#endif

继续阅读