
如何用 magiskpolicy 修改 Android SELinux 策略规则并即时生效【免费下载链接】MagiskThe Magic Mask for Android项目地址: https://gitcode.com/GitHub_Trending/ma/Magisk在 Magisk 环境下开发模块或调试 root 权限时经常需要为某个 domain 添加新的 SELinux 规则让系统行为放行。在 Linux 服务器上管理员会直接修改策略源文件*.te再重新编译sepolicy二进制但 Android 上不同Magisk 提供了magiskpolicy二进制直接修补二进制策略文件或运行时runtime策略。该工具同时被别名为supolicy以兼容 SuperSU 的 sepolicy 工具。本文覆盖从编写策略语句policy statement开始用--live让规则立即加载进内核再到通过模块sepolicy.rule持久化的完整过程。前提是设备已安装 Magisk并且能进入 root shell。修改规则前先弄清 Magisk 自身的 SELinux 上下文避免在 root shell 里做测试后得出错误结论需要先了解 Magisk 自己运行在什么上下文从 Magisk daemon 派生的所有进程包括 root shell 及其所有 fork都运行在u:r:magisk:s0上下文官方文档明确magisk这个 domain 是 effectively permissive新增的magisk_file文件类型被设置为任何 domain 都可以访问见 details.md所有安装了 Magisk 的系统上实际生效的规则可以理解为 stocksepolicy叠加了这条补丁magiskpolicy --magisk allow magisk * * *。也就是说root shell 自身已经处于宽放状态。magiskpolicy要改的是其他 domain 的规则写规则时对象应该是目标进程所在的 domain而不是 magisk 自身。命令形式与策略加载来源官方文档给出的用法见 tools.mdUsage: ./magiskpolicy [--options...] [policy statements...] Options: --help show help message for policy statements --load FILE load monolithic sepolicy from FILE --load-split load from precompiled sepolicy or compile split cil policies --compile-split compile split cil policies --save FILE dump monolithic sepolicy to FILE --live immediately load sepolicy into the kernel --magisk apply built-in Magisk sepolicy rules --apply FILE apply rules from FILE, read and parsed line by line as policy statements (multiple --apply are allowed)关键默认行为如果没有指定--load、--load-split或--compile-split中的任何一个magiskpolicy会从当前 live 策略/sys/fs/selinux/policy加载。配合--liveimmediately load sepolicy into the kernel就构成修改并即时生效的路径取当前内核策略应用你给出的语句再写回内核。修改前可以先导出当前策略留底FILE处的文件名由你自行指定magiskpolicy --save policy_before.ssp策略语句怎么写官方文档对策略语句有明确的三条格式规则一条 policy statement 作为一个参数传入也就是说每条语句都要用引号包起来一条命令可以带多条语句。标注(^)的参数接受一个或多个条目多个条目写成花括号包裹的空白分隔列表例如{ s1 s2 }。标注(*)的参数与(^)相同但额外支持 match-all 操作符*。文档示例allow { s1 s2 } { t1 t2 } class *会被展开为allow s1 t1 class { all-permissions-of-class } allow s1 t2 class { all-permissions-of-class } allow s2 t1 class { all-permissions-of-class } allow s2 t2 class { all-permissions-of-class }s1、s2、t1、t2是文档示例中的占位符写自己的规则时替换为系统里实际的 source type 和 target type 名称。支持的语句形式原文照录allow *source_type *target_type *class *perm_set deny *source_type *target_type *class *perm_set auditallow *source_type *target_type *class *perm_set dontaudit *source_type *target_type *class *perm_set allowxperm *source_type *target_type *class operation xperm_set auditallowxperm *source_type *target_type *class operation xperm_set dontauditxperm *source_type *target_type *class operation xperm_set - The only supported operation is ioctl - xperm_set format is either low-high, value, or *. * will be treated as 0x0000-0xFFFF. All values should be written in hexadecimal. permissive ^type enforce ^type typeattribute ^type ^attribute type type_name ^(attribute) - Argument attribute is optional, default to domain attribute attribute_name type_transition source_type target_type class default_type (object_name) - Argument object_name is optional type_change source_type target_type class default_type type_member source_type target_type class default_type genfscon fs_name partial_path fs_context语句较多时不必全部塞在命令行里--apply FILE会把文件按行读取、逐行解析为 policy statement并且允许传多个--apply。语句写法拿不准时--help会打印 policy statements 的帮助信息供对照。用 --live 让规则即时生效最短路径就是在 root shell 中直接执行下面s1、t1沿用文档示例占位符替换为实际类型名magiskpolicy --live allow s1 t1 class *规则较多时把每条语句写进一个文本文件每行一条格式与语句规则一致再用--apply批量应用magiskpolicy --live --apply rules.txt--live的语义是immediately load sepolicy into the kernel即当前内核中的运行策略立刻按你的语句更新不需要重启设备。核对与持久化规则语法层面可以用文档给出的展开示例自检如果你写了allow { s1 s2 } { t1 t2 } class *工具应将其展开为前面那 4 条allow sX tY class { all-permissions-of-class }规则。展开结果与预期不一致说明语句里的花括号或类型列表写错了。内容层面修改后可以再次导出当前策略与修改前的备份对比确认改动已经落到导出的策略里magiskpolicy --save policy_after.ssp官方文档没有提供专门查询某一条规则是否生效的命令能依据文档做的核对就是上面两种语句展开语义 导出后的策略文件。--live只作用于当前运行的内核策略。需要规则在每次开机后都应用到系统时官方文档给出的载体是模块目录里的sepolicy.rule见 guides.md 的模块结构/data/adb/modules └── $MODID └── sepolicy.rule --- Additional custom sepolicy rules文档对它的说明是如果你的模块需要额外的 sepolicy 补丁把规则写进这个文件文件中的每一行都会被当作一条 policy statement 处理。文件格式与magiskpolicy的策略语句一致例如每行写allow s1 t1 class *限制xperm 系列语句allowxperm等仅支持ioctl操作xperm_set只接受low-high、value或*三种形式且所有值必须以十六进制书写*会被当作0x0000-0xFFFF处理。magiskdomain 已经是 effectively permissiveroot shell 就运行在这个上下文中--live加的规则面向其他 domain 时不要用 root shell 自身能否执行某操作来判断规则效果。完整规则语义和模块sepolicy.rule的说明分别见 magiskpolicy 文档 与 模块开发指南。【免费下载链接】MagiskThe Magic Mask for Android项目地址: https://gitcode.com/GitHub_Trending/ma/Magisk创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考