linux 修改文件名报错,android底层开发进阶(3) SELinux报错修改篇

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中 定义一类property

type 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中加入相关的权限。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值