前言:
OpenHarmony 上某个进程向samgr注册SA服务,其他进程在与该进程进行IPC通信之前,需要获取该SA服务,SA提供方需要为该SA配置SELinux标签,否则该SA会被SELinux配置为u:object_r:default_service:s0标签,配置访问default_service的策略会被neverallow禁止,本文将介绍如何为新增的SA配置SELinux策略。
一、SELinux简介
SELinux (安全增强式 Linux , Security-Enhanced Linux )是 Linux 的安全组件,包含一组内核修改和用户空间工具,并提供了基于安全策略的强制访问控制机制( Mandatory Access Control ,MAC )。SELinux 已经被添加到各种 Linux 发行版中,其软件架构力图将软件执行与安全策略设计分离。本部件负责对文件、属性、服务等系统资源提供强制访问控制保护。提供neverallow规则限制系统中的高危操作,减少系统安全风险。
访问控制基本流程如下图所示:
1.1 整体架构
1.2 策略目录结构
OpenHarmony SELinux策略文件存放在//base/security/selinux_adapter/sepolicy/ohos_policy
路径下,在该目录下按以下规范存放:
├── 子系统
│ └── 部件
│ ├── public
│ │ └── type1.te
│ ├── vendor
│ │ └── type2.te
│ └── system
│ └── type3.te
其中,系统相关策略存放在system目录下,芯片相关策略存放在vendor目录下,系统和芯片共用的策略存放在public目录下。
1.3 通用策略文件
通用策略文件,是指设备开发者进行SELinux策略配置时,涉及修改的文件。
1.4 基础策略文件
基础策略文件,是指SELinux基础框架的SELinux策略文件,一般不涉及修改。
1.5 SELinux模式开关
将镜像烧录到开发板上,开机,通过串口拿到 Shell ,在其中执行:
ls -lZ / # 查看文件标签 ls -lLZ / # 查看link源文件标签 ps -eZ # 查看进程标签 setenforce 1 # 使能selinux强制模式(enforcing) setenforce 0 # 是能selinux宽容模式,当前默认宽容模式(permissive) getenforce # 获取selinux工作模式 |
selinux模式开关配置文件 :开发板 /etc/selinux/config
二、SELinux策略
2.1 avc日志信息
当系统行为被SELinux拦截时,会在内核日志和hilog中打印相关的avc denied
日志,格式如下:
//使用dmesg查看日志 audit: type=1400 audit(1502458430.566:4): avc: denied { open } for pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1//关键字:avc: denied//日志解读 open #操作为open pid=1658 #访问主体进程号为1658 comm="setenforce" #访问主体进程名为setenforce path="/sys/fs/selinux/enforce" #被访问客体为/sys/fs/selinux/enforce dev="selinuxfs" #被访问文件属于selinuxfs这一文件系统 ino=4 #文件节点编号为4 scontext=u:r:hdcd:s0 #访问主体selinux标签为u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 #被访问客体selinux标签为u:object_r:selinuxfs:s0 tclass=file #当前告警属于file类型的操作 permissive=1 #当前selinux处于宽容模式,只告警不做访问拦截。强制模式时,做拦截, permissive=0//分析: 缺少啥权限:denied { open } 谁缺少权限:scontext=u:r:hdcd:s0 对哪个文件缺少:tcontext=u:object_r:selinuxfs:s0 什么类型:tclass=file 通用公式为:allow {scontext} {tcontext}:{tclass} {denied权限} |
开发者可以使用关键字avc denied
来过滤日志,对于影响业务的avc告警,可以利用告警提供的信息来编写相应的SELinux策略,例如:
上述avc日志对应的TE规则为
allow hdcd selinuxfs:file open; |
2.2 策略格式
SELinux策略,又称SELinux规则,通常以allow或neverallow开头,表示允许或禁止某种行为。在设备上使能SELinux时,SELinux会拦截所有未经allow规则授权的行为,配置allow规则可以放行,neverallow规则主要是拦截危险规则的配置。通常allow规则如下:
allow subject object:class permissions; |
表示允许subject
对object
进行class
中的permissions
操作,其中:
subject
表示主体,通常为进程的SELinux类型,如init
。object
表示客体,通常为系统资源的SELinux类型,如data_file
。class
表示要执行的操作的类型,如文件操作file、目录操作dir、套接字操作socket。permissions
表示要执行的具体操作,如对文件file的open、read、write。
同理,
neverallow subject object:class permissions; |
表示不允许subject
对object
进行class
中的permissions
操作。
2.3 策略宏隔离
在考虑设备开发者便利的同时,需要兼顾商用设备安全性,因此OpenHarmony SELinux提供了策略宏隔离,决定在不同版本上策略是否生效。OpenHarmony SELinux中支持对仅在root版本生效的策略做宏隔离,宏名称为debug_only
。在用于设备开发者调试的root版本中,也就是在版本编译命令中指定--build-variant root
时,宏开启。在用于商用发布的user版本中,也就是在版本编译命令中指定--build-variant user
时,宏关闭。该宏的使用方法参考如下:
debug_only(`allow ueventd init:fd use; ') |
另外,OpenHarmony SELinux中也支持对开发者模式的策略做宏隔离,宏名称为developer_only
,该宏默认开启。开发者模式策略是指,为便于使用user版本进行调试开发的开发者,需要开放的一些用于调试的SELinux策略。开发者模式宏的使用方法参考如下:
developer_only(`allow sh init:fd use; ') |
三、配置SELinux策略
此处以开发activation_sa激活服务为例,详细介绍如何为sa服务配置selinux策略。
3.1 配置service_contexts
在sepolicy/base/public/service_contexts文件内,新增SAID与SA标签的映射关系:(SAID在activation_sa服务中定义,如foundation/activation/activation/sa_profile/BUILD.gn)
10 u:object_r:sa_render_service:s0 ... ... 7002 u:object_r:sa_ui_appearance:s0 8001 u:object_r:sa_ca_daemon_service:s0 10000 u:object_r:sa_activation_sa:s0 |
在service.te中定义对应的sa_activation_sa,使对应的标签合法:
type sa_activation_sa, sa_service_attr |
3.2 配置type.te
由init通过cfg文件孵化的SA服务进程,需要在sa服务的本进程cfg文件中新增secon字段(本例子为 foundation/activation/activation/etc/activation_sa.cfg),建立进程与标签的映射关系。如果未配置secon字段,进程在SELinux使能状态会被拦截启动。
{"services" : [{"name" : "activation_sa","path" : ["/system/bin/sa_main", "/system/profile/activation_sa.json"],"uid" : "system","gid" : ["system", "shell"],"start-mode" : "boot","secon" : "u:r:activation_sa:s0"}] } |
因此,需要在type.te中定义SELinux标签u:r:activation_sa:s0中的SELinux类型activation_sa,使u:r:activation_sa:s0是合法的:
type activation_sa, sadomain, domain; |
此外,需要调用binder_call,在activation_sa.te中添加
binder_call(activation_sa, samgr); allow activation_sa sa_activation_sa:samgr_class { get add }; |
3.3 查看avc日志信息
dmesg |grep activation_sa [ 11.766938] [pid=1][Init][INFO][init_service_manager.c:1088]Start service activation_sa [ 11.768130] [pid=1][Init][INFO][init_common_service.c:567]Service activation_sa(pid 606) started [ 34.494845] audit: type=1400 audit(1502631529.323:301): avc: denied { getattr } for pid=1395 comm="ps" path="/proc/606" dev="proc" ino=27789 scontext=u:r:sh:s0 tcontext=u:r:activation_sa:s0 tclass=dir permissive=1 [ 45.994459] audit: type=1400 audit(1502631540.823:317): avc: denied { read } for pid=1426 comm="ps" name="stat" dev="proc" ino=30816 scontext=u:r:sh:s0 tcontext=u:r:activation_sa:s0 tclass=file permissive=1 [ 45.994494] audit: type=1400 audit(1502631540.823:318): avc: denied { open } for pid=1426 comm="ps" path="/proc/606/stat" dev="proc" ino=30816 scontext=u:r:sh:s0 tcontext=u:r:activation_sa:s0 tclass=file permissive=1 [ 51.714670] audit: type=1400 audit(1502631546.543:334): avc: denied { getattr } for pid=1443 comm="ps" path="/proc/606" dev="proc" ino=27789 scontext=u:r:sh:s0 tcontext=u:r:activation_sa:s0 tclass=dir permissive=1 [ 51.714712] audit: type=1400 audit(1502631546.543:335): avc: denied { search } for pid=1443 comm="ps" name="606" dev="proc" ino=27789 scontext=u:r:sh:s0 tcontext=u:r:activation_sa:s0 tclass=dir permissive=1 |
根据日志配置te策略,新增sepolicy/ohos_policy/activation/test/system/sh.te
allow sh activation_sa:dir { getattr search }; allow sh activation_sa:file { read open getattr }; |
注意:随着策略不断配置,还会出现很多新的avc日志,要全部配置完全。
3.4 其他
如果配置完上述策略后,应用程序还是无法调用activation_sa中的方法,那么还需在对应等级的应用te文件(如settings应用权限等级为system_basic,那么对应新增system_basic_hap.te)中添加:
allow system_basic_hap_attr activation_sa:binder { call transfer }; allow system_basic_hap_attr sa_activation_sa:samgr_class { get }; |
通过上述一系列配置,新增的activation_sa服务能够在selinux开启强制模式下正常启动,且应用程序能够调用到activation_sa服务中定义的接口。