android底层开发进阶(3) SELinux报错修改篇
2016-01-05 15:04
399 查看
I.SELinux文件类型确定
查看文件的安全上下文并做修改
1、到相关目录中去查看root@Z00T:/cd system/bin/ root@Z00T:/ls -Z | grep demo
PS:demo替换成所需查看的文件名
一般情况下,由于没有设置demo的selinux权限,一般会默认它为文件系统中的文件
demo u:object_r:system_file:s0
2、添加定义文件类型的策略文件
1)添加所需的策略文件demo.te(这里demo以可执行程序为例)
在device/qcom/sepolicy/common中,新建demo.te文件
type demo, domain; type demo_exec, exec_type, file_type; init_daemon_domain(demo)
定义了demo是domain(领域),demo_exec为可执行程序
2)对文件匹配进行设定
在device/qcom/sepolicy/common/file_contexts中,加入匹配字段
/system/bin/demo u:object_r:demo_exec:s0
PS:上文中的demo均可替换成所需文件名
此时再进行第1步看是否生效,注意demo文件不能使用push进去的文件,以免file_context不识别。这时的kernel所报的log才是需要加权限的log
编译是否生效:查看out/target/product/<>/root/file_contexts文件及 out/target/product/<>/obj/ETC/file_contexts_intermediates/file_contexts文件
PS:如demo仅仅是资源文件,可以直接在file_context做匹配字段,在device/qcom/sepolicy/common/file.te上可以看到所有的文件类型
demo u:object_r:system_file:s0 system_file换成所需文件类型即可
II.SELinux加入权限
根据内核log所报的错误权限信息,加入权限即可。比如内核报这样的错:
# cat /dev/kmsg [ 172.554381] type=1400 audit(22611.739:4): avc: denied { getattr } for pid=257 comm="demo" path="/system/rfs" dev="mmcblk0p42" ino=2070 scontext=u:r:recovery:s0 tcontext=u:object_r:rfs_system_file:s0 tclass=dir permissive=0 [ 173.287498] type=1400 audit(22612.479:5): avc: denied { relabelfrom } for pid=257 comm="demo" name="rfs" dev="mmcblk0p42" ino=2070 scontext=u:r:recovery:s0 tcontext=u:object_r:rfs_system_file:s0 tclass=dir permissive=0
一般可以在comm下面看到domain的信息,知道我们所需要修改的te文件
在相应的te、文件中增加语句,语句格式为
allow sourcecontext targetcontext:class 许可 ;
例如
[ 172.554381] type=1400 audit(22611.739:4): avc: denied { getattr } for pid=257 comm=”update_binary” path=”/system/rfs” dev=”mmcblk0p42” ino=2070 scontext=u:r:recovery:s0 tcontext=u:object_r:rfs_system_file:s0 tclass=dir permissive=0
中
sourcecontext指的是“scontext=u:r:recovery:s0”的recovery,targetcontext 指的是“tcontext=u:object_r:rfs_system_file:s0 ” 中的rfs_system_file, class指的是“tclass=dir”中的dir,许可指的是“{}”中的getattr,
所以增加语句
allow recovery rfs_system_file:dir getattr;
III.解决编译报错问题
访问block device超过权限问题
对block device访问时,会因为neverallow,导致编译不过,此时不可以去修改/external/sepolicy/domain.te文件,这样会使cts跑不过,因而给domain特定的block device访问权限。可以在device/qcom/sepolicy/msm89xx/file_context定义相关block device,如果仍发现编译报错,报错原因是相关的block device没有定义,可以到/external/sepolicy/device.te做定义。然后再根据第II步所示,继续修改。
访问default property超过权限
有时会碰到访问default property的访问权限被neverallow,导致编译不过,同理不可修改domain.te,可以在device/qcom/sepolicy/common/property.te中 定义一类propertytype demo_prop, property_type;
在device/qcom/sepolicy/common/property_context上匹配所需要访问的property
adb.on u:object_r:demo_prop:s0
然后再根据第II步所示,继续修改。
操作访问
当需要手动进行一定的操作的时候,可以 cat /dev/kmsg,看需要什么样的权限,然后找到特定的te文件,进行操作如在mout某分区时
avc: denied { associate } for pid=4256 comm="mount" scontext=u:-Object_r:fac_file:S0 (file:S0) (file:S0 (file:S0)) tcontext=u:-Object_r:fac_file:S0 (file:S0) (file:S0 (file:S0)) tclass=filesystem permissive=0
说明mount没有相关的权限,可以在init.te中加入相关的权限。
相关文章推荐
- Yocto开发笔记之《嵌入式linux libcurl编程》(QQ交流群:519230208)
- 恢复误删除的文件
- Linux内核与根文件系统的关系
- zabbix linux服务端安装
- Linux中的时间和时间管理
- 【Linux】ubuntu下kafka集群环境搭建
- Linux系统ulimit值的正确设置方法
- CentOS 6.5安装KVM虚拟化
- linux panic 问题定位
- Linux下编程获取本地IP地址的常见方法
- Linux环境下Python的安装过程 【内有python各版本的下载链接】
- centos 挂载与卸载硬盘
- linux kernel map
- Centos7编译安装MySQL5.5
- Linux系统下安装rz/sz命令及使用说明
- linux下vi编辑器backspace不能正确删除文字
- 如何在Linux上使用命令行查看硬件信息
- Centos7 创建服务
- linux下安装jdk
- CentOS下安装使用start-stop-daemon