Android7.1 Selinux使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xzx208/article/details/85375403

讲Selinux的原理的文章太多了,讲实际使用也是有一些,但是都不够完整,这里举一个在Android7.1下的服务的例子。

服务第一步,我们会在init.rc下增加服务。 
因为我的设备是freesale sabresd 所以路径是: 
device/fsl/sabresd_6dq/init.rc

service crondd /system/bin/crondd
    class late_start
    user root
1
2
3
4
接着我把重新编译过boot.img镜像烧录进设备,我使用命令 
start crondd 毫无疑问系统提示 
init: Service crondd does not have a SELinux domain defined.

接着我们需要给我们的crondd 增加一下domain:

这里我的增加的目录为: 
device/fsl/imx6/sepolicy/crondd.te 
或者这里也行: 
system/sepolicy/ 
通用的目录是,另外包含的路径可以参考一下system/sepolicy/Android.mk

具体什么意思就不解释了。

type crondd, domain, domain_deprecated;
type crondd_exec, exec_type, file_type;
type crondd_device, dev_type;
type crondd_data_file, file_type, data_file_type;
init_daemon_domain(crondd)
allow crondd crondd_data_file:dir rw_dir_perms;
allow crondd crondd_data_file:file create_file_perms;

然后重新编译boot.img 我们再将服务启动起来,这时候我们能看到一些selinux打印出来的信息。

现在我们关闭掉selinux: 
setenforce 0

然后把所有的selinux收集起来: 
dmesg | grep avc > /data/avc_log.txt

这些信息我们可以用工具翻译出来,如果一句句自己翻译估计要崩溃了。 
ubuntu下: 
sudo apt install policycoreutils

然后: 
audit2allow -i avc_log.txt

#============= crondd ==============
allow crondd busybox_exec:file { read getattr open execute execute_no_trans };
allow crondd dex2oat:dir { getattr search };
allow crondd dex2oat:file { read open };
allow crondd platform_app:dir { getattr search };
allow crondd platform_app:file read;
allow crondd priv_app:dir { getattr search };
allow crondd priv_app:file read;
allow crondd untrusted_app:dir { getattr search };
allow crondd untrusted_app:file read;

很简单,把这些全部拷贝进去就可以了。 
这个工作很有可能重复很多次,因为有些命令是因为selinux 或者权限的问题执行失败,要一步一步的加。

其中有一种情况,用selinux的工具似乎是没法生成,这个只好自己了解一下生成的规则自己凑一下了。 
allow surfaceflinger system_prop:property_service set;

还有一种: 
type=1400 audit(1420226774.850:4466): avc: denied { read } for pid=3978 comm=”busybox” 
scontext=u:r:logdumpd:s0 tcontext=u:r:platform_app:s0:c512,c768 tclass=file permissive=0

需要把.te的第一行修改成:mlstrustedsubject。 
type logdumpd , domain,mlstrustedsubject

关于规则和一些说明可以参考如下网址: 
http://blog.csdn.net/Innost/article/details/19299937 
http://blog.csdn.net/xbalien29/article/details/19505575
--------------------- 
作者:守望尼罗河畔的初心 
来源:CSDN 
原文:https://blog.csdn.net/zmnqazqaz/article/details/76130202 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/xzx208/article/details/85375403