Sending non-protected broadcast问题

时代在发展科技在进步

1.问题简述

我们公司是做智能电话的,随着我们自己的应用增多,还有方便读取外置SD卡,都给了所有的程序系统权限,我们进程间的通讯基本上都是使用的广播,可以说是广播满天飞的状态,同时打印的日志也是非常多的,这时候发现在打印的日志中存在Sending non-protected broadcast的Error信息,但是不会导致程序crash掉,因为打印的非常多,因此想把它给解决掉不再让他提示
还有发现就是非系统级应用给系统级应用发送广播后,不会存在此提示


Sending non-protected broadcast

2.查找问题原因

查看源码在ActivityManagerService中有个checkBroadcastFromSystem方法

    private void checkBroadcastFromSystem(Intent intent, ProcessRecord callerApp,
            String callerPackage, int callingUid, boolean isProtectedBroadcast, List receivers) {
        if ((intent.getFlags() & Intent.FLAG_RECEIVER_FROM_SHELL) != 0) {
            // Don't yell about broadcasts sent via shell
            return;
        }

        final String action = intent.getAction();
        if (isProtectedBroadcast
                || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
                || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MEDIA_BUTTON.equals(action)
                || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
                || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MASTER_CLEAR.equals(action)
                || Intent.ACTION_FACTORY_RESET.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
                || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
                || TelephonyManager.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
                || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)
                || AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION.equals(action)
                || AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION.equals(action)) {
            // Broadcast is either protected, or it's a public action that
            // we've relaxed, so it's fine for system internals to send.
            return;
        }

        // This broadcast may be a problem...  but there are often system components that
        // want to send an internal broadcast to themselves, which is annoying to have to
        // explicitly list each action as a protected broadcast, so we will check for that
        // one safe case and allow it: an explicit broadcast, only being received by something
        // that has protected itself.
        if (intent.getPackage() != null || intent.getComponent() != null) {
            if (receivers == null || receivers.size() == 0) {
                // Intent is explicit and there's no receivers.
                // This happens, e.g. , when a system component sends a broadcast to
                // its own runtime receiver, and there's no manifest receivers for it,
                // because this method is called twice for each broadcast,
                // for runtime receivers and manifest receivers and the later check would find
                // no receivers.
                return;
            }
            boolean allProtected = true;
            for (int i = receivers.size()-1; i >= 0; i--) {
                Object target = receivers.get(i);
                if (target instanceof ResolveInfo) {
                    ResolveInfo ri = (ResolveInfo)target;
                    if (ri.activityInfo.exported && ri.activityInfo.permission == null) {
                        allProtected = false;
                        break;
                    }
                } else {
                    BroadcastFilter bf = (BroadcastFilter)target;
                    if (bf.requiredPermission == null) {
                        allProtected = false;
                        break;
                    }
                }
            }
            if (allProtected) {
                // All safe!
                return;
            }
        }

        // The vast majority of broadcasts sent from system internals
        // should be protected to avoid security holes, so yell loudly
        // to ensure we examine these cases.
        if (callerApp != null) {
            Log.wtf(TAG, "Sending non-protected broadcast " + action
                            + " from system " + callerApp.toShortString() + " pkg " + callerPackage,
                    new Throwable());
        } else {
            Log.wtf(TAG, "Sending non-protected broadcast " + action
                            + " from system uid " + UserHandle.formatUid(callingUid)
                            + " pkg " + callerPackage,
                    new Throwable());
        }
    }

其中的一段话描述


image.png

大致的意思我理解为,系统级应用发送的广播应该受到保护,以防止有居心不良之人,利用这些广播找到漏洞
然后在看框架层代码中显示如下
frameworks\base\core\res\AndroidManifest.xml


image.png

在这个Manifest文件中存在了很多系统层的广播,如对package的处理等
由此可以看此google把一些有安全隐患的广播都放到了这里

3.解决方法

目前从看到的方法有

方案1.修改源码

从frameworks层找到ActivityManagerService的checkBroadcastFromSystem这个函数,修改这个函数,彻底解除系统的这个消息

方案2.修改框架层的AndroidManifest

将自定义添加的广播,添加至frameworks\base\core\res\AndroidManifest.xml 文件中,这样也不会报错
格式为<protected-broadcast android:name="com.pzdf.action_checkNetState" />

方案3.修改自己应用的AndroidManifest

同上一个方法一样,也可以将广播添加到自己系统级应用的AndroidManifest.xml文件中,效果与上一个方法一致

方案4.给广播添加上权限

AndroidManifest.xml添加权限
<permission android:name="com.android.permission" />
<uses-permission android:name="com.my.permission" />
发送接收广播地方
sendBroadcast(intent, "com.android.permission")
registerReceiver(receiver, filter,"com.android.permission", null)

4.题外话

在使用方案2或者方案3的时候会有个问题
非系统权限的应用给系统级应用发送广播会报出安全异常
如下


Permission Denial
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容