0%

Android SELinux 调试小记

获取 SELinux 日志

adb shell logcat | findstr "avc:"

或者

adb shell dmesg | findstr "avc:"

临时开关 SELinux 功能

# 查看状态
adb shell getenforce
# 开启
adb shell setenforce 1
# 关闭
adb shell setenforce 0

查看进程绑定的上下文

adb shell ps -efZ | findstr {keywords}

查看文件绑定的上下文

adb shell ls -lhZ {path}

临时改变文件绑定的上下文

# 临时设定上下文
adb shell chcon {new_context} {path}
# 还原/重新应用原始上下文
adb shell restorecon {path}

单编 SELinux 策略

# vendor 和 system 都编译
make selinux_policy -j24
# 只编译 vendor 的 sepolicy
make selinux_policy_nonsystem -j24

产物根据修改范围大致在以下路径:
- /system/etc/selinux/ (新版本Android已经没有这个路径了, 在 system_ext 下面)
- /system_ext/etc/selinux/
- /vendor/etc/selinux/

验证策略

根据修改范围, 将编译后的产物 push 到设备, 即可验证:

adb root
adb remount
adb shell setenforce 0

# system 的 sepolicy
adb push {out_path}/system_ext/etc/selinux /system_ext/etc/
# vendor 的 sepolicy
adb push {out_path}/vendor/etc/selinux /vendor/etc/

adb shell setenforce 1

其他

  • 策略适配原则:最小授权
  • 绑定对象不能重复, 支持正则表达式
  • domain 不能重复
  • 链接文件和原始文件策略通常不能共享, 需要分别设置
  • 本文作者: 6x
  • 本文链接: https://6xyun.cn/article/222
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-ND 许可协议。转载请注明出处!