文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx

2023-08-31 14:05

关注

带android:sharedUserId=“android.uid.system” 发送广播时,会出现 Sending non-protected broadcast 异常提醒;

原因:
Ams在发送广播时,对于systemApp(系统应用),会要求发送广播必须是声明在frameworks\base\core\res\AndroidManifest.xml里面的protected-broadcast。这是为了提醒 系统应用开发者要将 broadcast 添加到protected-broadcast,因为非 protexted-broadcast 广播是可以被三方应用发送的。定义为 proected-broadcast 可以避免三方垃圾应用也发送这些广播来捣蛋。

查找问题

首先去搜索问题出现的位置 ,去AndroidXRef网站搜索下

定位到frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

类中,在checkBroadcastFromSystem中抛出该异常。

checkBroadcastFromSytem是用于检测系统应用发出的广播是否安全,如果发出的广播不安全,就会抛出no-protected broadcast异常,提醒系统应用开发者。

为什么要有这个机制?

这是为了提醒 系统应用开发者去将 broadcast添加为protected-broadcast,因为非 protected-broadcast 广播是可以被三方应用发送的。 而定义为 proected-broadcast 就能防止恶意的三方应用模仿系统应用去发出该广播。

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)                || TelephonyIntents.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());        }    }

如上代码, 对于广播,checkBroadcastFromSystem方法有三种检测。

  1. 如果Action 是Shell 发出的,则直接返回,不发出警告

  2. 如果Action 是protected-broadcast广播或一些指定的Action,不发出警告

  3. 如果Action 是指定了包名或者Compononent信息,只有特定应用能接收,则当所有receiver 均根据receiverPermisson 对权限进行了顾虑,则不发出警告

如果系统应用不满足以上三种情况,则发送公共广播时会被警告

如何解决 non-protected broadcast警告

从系统源码层面

在checkBroadcastFromSystem 处修改

 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)                || TelephonyIntents.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;        }复制代码

在/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java#isProtectedBroadcast方法处修改

在/frameworks/base/core/res/AndroidManifest.xml 处增加 protected-broadcast 标签

在应用层面修改

  1. 在系统应用的 AndroidManifest.xml 处增加 protected-broadcast 标签,且apk放在 /system/priv-app 目录下

  2. 指定接收者.获取所有接收该广播的ResolveInfo,发送广播。

来源地址:https://blog.csdn.net/xiaowang_lj/article/details/128828286

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯