前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >S004Define a SELinux domain for Service

S004Define a SELinux domain for Service

作者头像
上善若水.夏
发布2018-09-28 11:11:57
1.1K0
发布2018-09-28 11:11:57
举报
文章被收录于专栏:上善若水上善若水

实现的目标:在源码中,init.rc 启动一个原生的服务

一、适用情景

当在init.rc中新增service:

代码语言:javascript
复制
service ro_isn /system/bin/isn.sh 
    class late_start 
    user root 
    oneshot

kernel log会打印以下log:

代码语言:javascript
复制
Warning!  Service ro_isn needs a SELinux domain defined; please fix!

这是因为Service ro_isn没有在SELinux的监控之下,这种情况会提示你定义一个SELinux。

在这种情况下,你可以:

  1. 无视该条log,Service功能不受影响。各种权限不受限制。但是这样做会有风险。
  2. 为Service ro_isn定义一个SELinux domain,仅添加需要的权限,未允许的权限操作会被拒绝。具体方法请参照下节。

二、解决方法

1.在devices/qcom/sepolicy/common/目录下新增ro_isn.te文件,内容如下:

代码语言:javascript
复制
type ro_isn, domain; 

type ro_isn_exec, exec_type, file_type; 

2.在devices/qcom/sepolicy/Android.mk中添加ro_isn.te文件,内容如下:

代码语言:javascript
复制
BOARD_SEPOLICY_UNION := 

... 

    hostapd.te 

    ro_isn.te

3.在devices/qcom/sepolicy/common/file_contexts中增加如下内容:

代码语言:javascript
复制
###################################

# System files

#

...

/system/vendor/bin/slim_ap_daemon               u:object_r:location_exec:s0

/system/bin/isn.sh                       u:object_r:ro_isn_exec:s0

4.在init.rc中service ro_isn下添加secure context by seclabel

代码语言:javascript
复制
service ro_isn /system/bin/isn.sh 
    class late_start 
    user root 
    oneshot 
    seclabel u:r:ro_isn:s0 

5.编译并烧录bootimage 6.如果编译不成功,失败原因如下:

Error while expanding policy

libsepol.check_assertion_helper: neverallow on line 233 of external/sepolicy/domain.te (or line 5194 of policy.conf) violated by allow ro_isn system_file:file { entrypoint };

make: *** [out/target/product/msm8226/obj/ETC/sepolicy_intermediates/sepolicy] 错误 1

这是因为系统在domain.te中定义了全局的neverallow策略,与ro_isn.te中allow的策略有冲突:

allow ro_isn system_file:file { entrypoint };

neverallow domain { file_type -exec_type }:file entrypoint;

请确定自己的service有必要需要这个权限。如无必要,请在自己的code中删除掉相关操作;如必要,可以在external/sepolicy/domain.te中冲突的neverallow语句中添加自己为例外:

neverallow {

代码语言:javascript
复制
domain

-ro_isn

} { file_type -exec_type }:file entrypoint;

7.在service ro_isn运行时,搜索关于“ro_isn”的avc: denied log

代码语言:javascript
复制
<6>[ 13.547188](CPU:0-pid:320:logd.auditd) type=1400 audit(17468992.410:7): avc: denied { entrypoint } for pid=272 comm="init" path="/system/bin/isn.sh" dev="mmcblk0p38" ino=631 scontext=u:r:ro_isn:s0 tcontext=u:object_r:system_file:s0 tclass=file 

8.按照如下规则在ro_isn.te添加权限

SELinux规则语句一般如下: allow A B:C D; 可以从log中分别获取ABCD四个参数。

比如这行warning log:

代码语言:javascript
复制
avc: denied { entrypoint } for pid=272 comm="init" path="/system/bin/isn.sh" dev="mmcblk0p38" ino=631 scontext=u:r:ro_isn:s0 tcontext=u:object_r:system_file:s0 tclass=file 

那么我们就得出最后的规则是:

代码语言:javascript
复制
allow qcomsysd  block_device:dir { search };

allow ro_isn system_file:file { entrypoint }; 

9.重复步骤5-8,直到没有关于“ro_isn”的avc: denied log

参考链接

  1. Define a SELinux domain for Service
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.10.12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、适用情景
  • 二、解决方法
  • 参考链接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档