SELinux policy相关问题的总结

版权声明:转载请注明博客地址 https://blog.csdn.net/jinron10/article/details/86574711

在开发Android系统的时候或多或少遇到一些Selinux的相关的问题,在这里进行一些总结和整理,内容大部分来源网络.

1、了解SELinux基本概念,这个网上资料很多,具体参考:

http://jingpin.jikexueyuan.com/article/55398.html
http://blog.csdn.net/innost/article/details/19299937/ 
http://blog.csdn.net/luoshengyang/article/details/37613135 
http://www.2cto.com/kf/201504/390742.html 
http://www.th7.cn/system/lin/201512/147098.shtml 

2、确认Selinux的状态

2.1 可以通过手动关闭selinux,确认问题是否是权限引起的,具体方法: 
查看SELinux状态: 
1) /usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 
SELinux status:                 enabled 
2) getenforce                 ##也可以用这个命令检查 

2.2 关闭SELinux: 
临时关闭(不用重启机器): 
setenforce 0                  ##设置SELinux 成为permissive模式 
                              ##setenforce 1 设置SELinux 成为enforcing模式 

3、抓dmesg log,转换出系统缺少的policy 

dmesg log: 
dmesg | grep rpcServer 
[ 8.132704] init: Starting service 'rpcServer'... 
[ 12.936186] type=1400 audit(1449623772.120:13): avc: denied { read } for pid=174 comm="rpcServer" name="mmcblk0p1" dev="tmpfs" ino=8105 scontext=u:r:rpcServer:s0 tcontext=u:object_r:block_device:s0 tclass=blk_file permissive=0 
比如上面这个log,你需要能清楚这句话的意思:原进程(scontext)需要目标(tcontext)进程tclass属性的read权限 
我们可以通过ls -Z、ps -Z、ls -l等查看具体设备文件相关属性 

4、如何添需要的权限(这个可以分3类来处理)

1)第一类比较简单,通过audit2allow等工具转换出对应模块缺少的policy(.te文件) 
添加到sepolicy/rpcServer.te:
allowrpcServer block_device:blk_file read ;  分别对应上面的allow scontext tcontext:tclass   read 

2)第二类,有一些policy加进来,编译的时候系统会报错,原因是我们上面转换的policy一般都是某个进程对所以的的设备文件具有相关权限,这个权限过大,会和google的安全策略(external/sepolicy下neverallow)想冲突。这种情况下,我们需要找出原进程究竟需要哪个具体的设备文件的相关权限,就可以解决 
添加: 
先在sepolicy/file_contexts定义mmcblk0p1 设备节点:/dev/block/mmcblk0p1 u:object_r:rpc_block_device:s0 
在sepolicy/rpcServer.te添加该设备节点的权限:allow rpcServer rpc_block_device:blk_file read; 

3)

原进程的属性(root、radio、system等)与目标设备文件的属性不匹配,这种情况下我们映射会不起作用。这样的话,需要在init.rc 和ueventd.rc里面修改相应的属性:
我这边例子是查下来问题原因: 
vmodem driver is not able openthe NVM partitions so default NVM values are expected. Issue is with permissionmis-match: 
vmodem driver context is “radio”and block device nodes are “root” 
由于mmcblk0p1是modem使用的Nvm Calculate Static,rpcServer无权限挂载,先将rpcServer和设备mmcblk0p1都设置设置成一样的radio属性: 
ueventd.rc文件中: 
/dev/block/mmcblk0p1 0660 radio radio 
/dev/vmodem 0660 radio radio 
init.rc和init.ptest.rc文件中,system/bin/rpcServer 的user、group的root属性改成radio: 
chown radio radio /sys/class/misc/vmodem/modem_state 
service rpcServer /system/bin/rpcServer 
socket msmSock stream 660 system radio 
class core 
user radio 
group radio 
然后添加kernel和rpcServer对mmcblk0p1 设备的权限: 
在sepolicy/file_contexts定义mmcblk0p1 设备节点:/dev/block/mmcblk0p1 
u:object_r:rpc_block_device:s0 
在sepolicy/kernel.te:allowkernel rpc_block_device:blk_file read; 
在sepolicy/rpcServer.te:allow 
rpcServer rpc_block_device:blk_file read; 

其中上面两种情况解决套路是一样的,而第三种,需要根据不同的平台,对应的设备和进程属性来具体分析。以下是在本地添加一个服务,因selinux的原因导致无法运行,如下:

1、在init.rc 中添加:

service xxx /system/bin/xxx 
    class main 
    user root 

编译boot后烧到机器中,发现服务xxx无法启动,kernel log中有如下提示:
[   20.076354s][pid:1,cpu7,init]init: Service xxx does not have a SELinux domain defined. 

该提示说明没有定义SELinux domain,导致服务xxx无法自启动。为了解决这个问题我们按如下方式修改或添加sepolicy文件:

修改seplicy/file_contexts文件,添加以下内容: 
/system/bin/xxx     u:object_r:xxx_exec:s0 
新增xxx.te文件,并在其中添加如下内容: 
需要为新增的进程增加域、执行权限 
type xxx, domain; 
type xxx_exec, exec_type, file_type; 
然后启用这个域 
init_daemon_domain(xxx) 
验证,原则上修改SELinux的问题需要全编译,为了节省时间可以使用以下方法调试 
编译bootimage 
烧录bootimage 
执行adb remount 
执行adb shell restorecon system/bin/xxx 
重启机器,看是否在kernel log中是否成功启动xxx服务


 

猜你喜欢

转载自blog.csdn.net/jinron10/article/details/86574711