SELinux

2025-10-09

SELinux

SELinux(Security-Enhanced Linux)是Linux内核的一个模块,也是Linux的一个安全子系统。SELinux的实现了强制访问控制MAC(Mandatory Access Control ),每个进程和系统资源都有一个特殊的安全标签,资源能否被访问除了DAC规定的原则外,还需要判断每一类进程是否拥有对某一类资源的访问权限。

SELinux 运行模式

  • SELinux分为三种模式

permissive:SELinux仅打印告警而不强制执行。 enforcing:SELinux安全策略被强制执行。 disabled:不加载SELinux安全策略。

  • 更改SELinux模式
    //查看当前SELinux运行模式
    adb shell getenforce
    //设置为Permissive
    adb shell setenforce 0
    //设置为Enforcing
    adb shell setenforce 1
    

SELinux 调试

调试确认SELinux问题

> adb shell getenforce

Enforcing

//临时禁用selinux ,重启失效

> adb shell setenforce 0

> adb shell getenforce

Permissive

如果问题消失了,基本可以确认是SELinux造成的权限问题,需要通过正规的方式来解决权限问题。

通过命令过滤avc denied

dmesg | grep avc 或者 cat /proc/kmsg | grep avc

具体案例分析

解决原则是:缺什么权限补什么,一步一步补到没有avc denied为止。

解决权限问题需要修改的权限文件如下位置,以.te结尾 A:Android/xxx/xxx-device-common/sepolicy/*.te

B:Android/external/sepolicy/*.te

case 1

audit(0.0:67): avc: denied { write } for path=”/dev/block/vold/93:96” dev=”tmpfs” ino=/1263 scontext=u:r:kernel:s0 tcontext=u:object_r:block_device:s0 tclass=blk_file permissive=0

分析过程:

缺少什么权限:{ write }权限,

谁缺少权限:scontext=u:r:kernel:s0

对哪个文件缺少权限:tcontext=u:object_r:block_device

什么类型的文件:tclass=blk_file

完整的意思:kernel进程对block_device类型的blk_file缺少write权限。

解决方法:在上文A位置,找到kernel.te这个文件,加入以下内容:

allow  kernel  block_device:blk_file  write;

case 2

audit(0.0:53): avc: denied { execute } for path=”/data/data/com.mofing/qt-reserved-files/plugins/platforms/libgnustl_shared.so” dev=”nandl” ino=115502 scontext=u:r:platform_app:s0 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=0

分析过程:

缺少什么权限:{ execute}权限,

谁缺少权限:scontext = u:r:platform_app:s0

对哪个文件缺少权限:tcontext = u:object_r:app_data_file

什么类型的文件:tclass= file

完整的意思:platform_app进程对app_data_file类型的file缺少execute权限。

解决方法:在上文A位置,找到platform_app.te这个文件,加入以下内容:

allow  platform_app  app_data_file:file  execute;

感谢阅读!