Android给新增的二进制应用添加selinux权限

I.SELinux文件类型确定

查看文件的安全上下文并做修改

1、到相关目录中去查看

root@Z00T:/cd system/bin/
root@Z00T:/ls -Z | grep demo
  • 1
  • 2
  • 3

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)
  • 1
  • 2
  • 3
  • 4

定义了demo是domain(领域),demo_exec为可执行程序

2)对文件匹配进行设定 
在device/qcom/sepolicy/common/file_contexts中,加入匹配字段

/system/bin/demo u:object_r:demo_exec:s0
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4

一般可以在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;
  • 1
  • 2

在device/qcom/sepolicy/common/property_context上匹配所需要访问的property

adb.on                     u:object_r:demo_prop:s0
  • 1
  • 2

然后再根据第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
  • 1
  • 2

说明mount没有相关的权限,可以在init.te中加入相关的权限。

版权声明: 作者:alex wang版权:本文版权归作者和CSDN共有 转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢 要求:未经作者同意,请保留此段声明;在文章中给出原文连接;否则必究法律责任

转自:http://blog.csdn.net/u013983194/article/details/50462694

I.SELinux文件类型确定

查看文件的安全上下文并做修改

1、到相关目录中去查看

root@Z00T:/cd system/bin/
root@Z00T:/ls -Z | grep demo
  • 1
  • 2
  • 3

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)
  • 1
  • 2
  • 3
  • 4

定义了demo是domain(领域),demo_exec为可执行程序

2)对文件匹配进行设定 
在device/qcom/sepolicy/common/file_contexts中,加入匹配字段

/system/bin/demo u:object_r:demo_exec:s0
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4

一般可以在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;
  • 1
  • 2

在device/qcom/sepolicy/common/property_context上匹配所需要访问的property

adb.on                     u:object_r:demo_prop:s0
  • 1
  • 2

然后再根据第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
  • 1
  • 2

说明mount没有相关的权限,可以在init.te中加入相关的权限。

猜你喜欢

转载自blog.csdn.net/xiao5678yun/article/details/79232453