SELinux开关
adb在线修改seLinux(也可以改配置文件彻底关闭)
$ getenforce; //获取当前seLinux状态,Enforcing(表示已打开),Permissive(表示已关闭)
$ setenforce 1; //打开seLinux
$ setenforce 0; //关闭seLinux
命令查看selinux权限
查看客体的安全上下文:客体是死的,如文件、属性、套接字等 ls -Z 查看安全上下文
查看主体的安全上下文:主体是指进程,是活的,是安全行为的发起者 ps -Z 查看安全上下文
标志性log 格式:
avc: denied { 操作权限 } for pid=7201 comm=“进程名” scontext=u:r:源类型:s0 tcontext=u:r:目标类型:s0 tclass=访问类型 permissive=0
在相应源类型.te文件,添加如下格式的一行语句:
格式:allow 源类型 目标类型:访问类型 {操作权限};
示例:
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:s0
什么类型:tclass=blk_file
kernel.te: allow kernel block_device:blk_file write;
(写操作一般还伴随open、append等,所以一般使用w_file_perms宏替代单一的write)(如需同时申请多个权限可用花括号包起来放句尾,如 {read write} )
常修改的文件位置
/system/sepolicy/下面的public/是平台公共策略,private/是平台私有策略,vendor/是供应商策略
也有直接在/vendor/下的,也有在/device/下的
selinux编译产物在AndroidR上的位置
system/etc/selinux system_ext/etc/selinux product/etc/selinux vendor/etc/selinux odm/etc/selinux
安装 policycoreutils工具使用 :
1.linux or 虚拟机 2. su切进root 3.sudo apt install policycoreutils 4.su切出root 5.policycoreutils 的audit2allow命令可以将selinux报错信息转换为配置,从而解决问题。
示例:
# audit2allow -i selog.txt
#============= init ==============
allow init sysfs:file write;
#============= priv_app ==============
allow priv_app oplus_nw_exported_system_prop:file read;
(selog.txt中存放的是“avc: denied”报错打印,audit2allow不但会给出权限配置,而且还会给出配置的位置)
开发和调试时的分析
- 主要步骤为| 确认–添加–验证 |如下:
- 确认:关闭selinux再运行确认是否是selinux引起 或者 看kernellog、eventlog、mainlog中“avc”log且“permissive=0”的log。(因为有时只是报log而已,permissive=1是selinux并未阻止的)
- 添加:从github上可下载sepolicy-inject安装进手机直接使用命令动态注入怀疑缺少的权限,快速验证。
- 验证:验证是否问题还出现。
- 可以根据 ino 这个打印出来的 inode number 号来查看是报那个节点没有权限
# ls -i /sys/devices/system/cpu/cpu0/online //查看文件的 inode number
35941 /sys/devices/system/cpu/cpu0/online
# ls -i /proc/uptime
4026532103 /proc/uptime
/sys/devices # find ./ -inum 35941 //根据 inode number 查找对应的文件
./system/cpu/cpu0/online
/proc # find ./ -inum 4026532103
./uptime
- 查看文件的selinux属性
/sys/class/wakeup # ls -la -Z
total 0
drwxr-xr-x 2 root root u:object_r:sysfs_wakeup:s0 0 1970-01-01 08:00 . //selinux配置使用sysfs_wakeup
drwxr-xr-x 112 root root u:object_r:sysfs:s0 0 1970-01-01 08:00 .. //selinux配置使用sysfs
lrwxrwxrwx 1 root root u:object_r:sysfs:s0 0 2020-08-28 20:04 wakeup0 -> ../../devices/platform/.../c440000.qcom,spmi:qcom,pm8150@0:qcom,power-on@800/wakeup/wakeup0
常用通配符
-表示除去某项内容
*表示全部
~表示取反除了此项之外的所有
selinux文件说明和语法
- 所有客体类别和客体操作都在android/system/sepolicy/private/security_classes和access_vectors中有罗列。
- file_context文件是设置节点或文件的selinux属性类型的,.te文件是设置访问权限的。通俗的理解,file_context是定义属性的类型,.te文件是使用属性类型。
(1)file_contexts 给所有的自定义变量(包括文件节点、设备节点、可执行程序)赋了具体的值
(2)device.te 声明了file_contexts中为设备节点的变量
(3)file.te 声明了file_contexts中为文件类型的变量
(4)很多.te文件都是以进程名来命名的
- file.te语法
type httpd_user_content_t, file_type, httpdcontent;
分成两条语句进行表述:
#定义 httpd_user_content_t :
type httpd_user_content_t;
#关联属性:
typeattribute httpd_user_content_t file_type, httpdcontent;