SELinux权限学习笔记

转载:SELinux权限学习笔记 - Hello-World3 - 博客园

1. 权限修改

方法1: adb在线修改seLinux

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

方法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

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

6.看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 using “find /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

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

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

(1) 查看某个属性的selinux权限:# getprop -Z vendor.qcom.bluetooth.soc
u:object_r:vendor_bluetooth_prop:s0

给platform_app加此属性的获取权限,在platform_app.te中添加:get_prop(platform_app, vendor_bluetooth_prop)

(2) 查看某个进程的安全上下文:ps -AZ | grep com.android.myapp

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

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值