Skip to main content
 首页 » 编程设计

Android SELinux权限学习笔记

2022年07月19日317thcjp

1. 权限修改

方法1: adb在线修改seLinux

$ getenforce;     //获取当前seLinux状态,Enforcing(表示已打开),Permissive(表示已关闭) 
$ setenforce 1;   //打开seLinux 
$ setenforce 0;   //关闭seLinux

注:setenforce 0 后,虽然内核中还会报avc deny错误,但是已经不影响访问了。

方法2: 从kernel中彻底关闭

修改LINUX/android/kernel/arch/arm64/configs/xxx_defconfig文件, 去掉CONFIG_SECURITY_SELINUX=y 的配置项

方法3: sepolicy中添加权限

修改依据: 通过指令cat /proc/kmsg | grep denied,或者kernel的Log中定位到标志性log。

修改步骤: 找相应的源类型.te文件,此文件可能的存放路径 (其中源类型见下方的标志性log格式) :

LINUX/android/external/sepolicy
LINUX/android/device/qcom/sepolicy/common

标志性log 格式:

avc: denied  { 操作权限  }  for pid=7201  comm=“进程名”  scontext=u:r:源类型:s0  tcontext=u:r:目标类型:s0  tclass=访问类型 permissive=0

在相应源类型.te文件,添加如下格式的一行语句:(结尾别忘了分号)

格式:allow  源类型 目标类型:访问类型 {操作权限};

实例:

Kernel Log:

avc: denied {getattr read} for pid=7201 comm="xxx.xxx" scontext=u:r:system_app:s0 tcontext=u:r:shell_data_file:s0 tclass=dir permissive=0

修改方案

在system_app.te文件中,添加下面语句:

allow system_app shell_data_file:dir{getattr read};

scontext: start context,发起方
tcontext:target context, 接受方

若发起方访问接受方没有权限,一般权限配置在发起方,但是由权限接受方模块负责人配置。

2. 将selinux报错信息转换为配置

# audit2allow -i selicx.txt

selicx.txt中存放的是“avc: denied”报错打印,audit2allow不但会给出权限配置,而且还会给出配置的位置。

3.audit2why工具

~/tmp/selinux$ cat se_error.txt  
avc: denied { write } for comm="init" name="iostats" dev="sysfs" ino=67139 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 
avc: denied { write } for comm="init" name="scheduler" dev="sysfs" ino=67122 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 
~/tmp/selinux$  
~/tmp/selinux$ audit2why -i se_error.txt  
avc: denied { write } for comm="init" name="scheduler" dev="sysfs" ino=67122 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 
 
        Was caused by: 
                Missing type enforcement (TE) allow rule. 
 
                You can use audit2allow to generate a loadable module to allow this access.

解释avc报错原因。

注:可以根据 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

4.查看文件的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

file_context文件是设置节点或文件的selinux属性类型的,.te文件是设置访问权限的。通俗的理解,file_context是定义属性的类型,.te文件是使用属性类型。

5.看selinux报的是哪个文件的权限错误

As an example ,  
In the following path ino , 82228 / 69931 / 49391  
08-03 00:22:47.555 684 684 W Binder:684_2: type=1400 audit(0.0:3709): avc: denied { read } for name="wakeup58" dev="sysfs" ino=82228 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0  
08-03 00:22:47.555 684 684 W Binder:684_2: type=1400 audit(0.0:3710): avc: denied { read } for name="wakeup48" dev="sysfs" ino=69931 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0  
 
We have to see which path its pointing to usingfind /sys -inum <number that is showing in the denials >this will change in each run where running with the right number will give you the path it was trying for . 

举例:

log: 
01-01 00:00:53.725  1373  1373 W vendor-oplus-ha: type=1400 audit(0.0:1866): avc: denied { read } for name="u:object_r:system_prop:s0" dev="tmpfs" ino=13478 scontext=u:r:usage_hidl:s0 tcontext=u:object_r:system_prop:s0 tclass=file permissive=0 
130|OP4AF7:/ # find /sys/ -inum 13478 
/sys/firmware/devicetree/base/soc/xxx/port@4/endpoint/slave-mode

注:se context 属性是 per inode, 链接的节点相当于新的节点了, 取决于是否对此节点是否赋予新的context. 

6. 查看文件、进程、属性的selinux安全上下文

(1) 查看某个属性的selinux权限:

# getprop -Z vendor.qcom.bluetooth.soc 
u:object_r:vendor_bluetooth_prop:s0

(2) 查看某个app的selinux权限:

# ps -AZ | grep com.android.chrome 
u:r:isolated_app:s0:c512,c768  u0_i65         553 20623 1352256  74060 do_epoll_wait       0 S com.android.chrome:sandboxed_process0:org.chromium.content.app.SandboxedProcessService0:9 
u:r:app_zygote:s0:c512,c768    u0_a114      20623   728 1682068  35660 poll_schedule_timeout 0 S com.android.chrome_zygote 
u:r:untrusted_app:s0:c114,c256,c512,c768 u0_a114 31990 728 1495908 194232 do_epoll_wait    0 S com.android.chrome 
u:r:isolated_app:s0:c512,c768  u0_i56       32031 20623 1354404  78800 do_epoll_wait       0 S com.android.chrome:sandboxed_process0:org.chromium.content.app.SandboxedProcessService0:0 
u:r:untrusted_app:s0:c114,c256,c512,c768 u0_a114 32084 728 1354112 89860 do_epoll_wait     0 S com.android.chrome:privileged_process0

(3) 查看某个进程的selinux权限:

# ps -AZ | grep system_server 
u:r:system_server:s0           system        1564   727 13590372 391196 do_epoll_wait      0 S system_server

(3) 查看某个文件的安全上下文:ls -laZ

# ls -laZ 
drwxr-xr-x   2 root root u:object_r:sysfs_cm_mgr:s0              0 2022-06-02 20:50 cm_mgr 
-r--r--r--   1 root root u:object_r:sysfs_kernel_notes:s0       48 2022-06-02 20:50 notes

9. 案例

案例1:avc: denied { write }之类的缺少权限

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

语法:rule_name source_type target_type : class perm_set**

万能公式:

缺少什么权限:{ 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

TODO: 解释剩余命令的作用

~/tmp/selinux$ audi [tab]
audispd audit2allow audit2why auditctl auditd

TODO: 优秀博文

https://blog.csdn.net/yanjun821126/article/details/80828908
https://blog.csdn.net/shichaog/article/details/53728893
https://blog.csdn.net/xct841990555/article/details/82714322
https://blog.csdn.net/w2064004678/article/details/105515244/

selinux属性模糊配置:
https://blog.csdn.net/iaMay_____/article/details/80344592

参考:

SELinux 权限问题: http://gityuan.com/2015/06/13/SEAndroid-permission/

属性问题展开的selinux权限介绍:https://www.jianshu.com/p/88a92d101532

selinux操作:https://www.cnblogs.com/hellokitty2/p/10088128.html


本文参考链接:https://www.cnblogs.com/hellokitty2/p/12264337.html