1# OpenHarmony SELinux编译与加载
2
3## 策略编译与加载
4
5OpenHarmony SELinux策略编译流程在编译机中进行,加载流程在init进程启动后执行,总体流程如图:
6
7**图1** OpenHarmony SELinux编译和加载流程图
8
9![selinux_structure](./figures/SELinux-policy.png)
10### 策略编译
11OpenHarmony SELinux策略编译主要包括以下流程:
121. 按`security_classes, initial_sids, access_vectors, glb_perm_def.spt, glb_never_def.spt, mls, policy_cap, glb_te_def.spt, attributes, .te, glb_roles.spt, users, initial_sid_contexts, fs_use, virtfs_contexts`顺序遍历策略目录`//base/security/selinux_adapter/sepolicy/`,得到父目录为system和public的策略文件列表,使用m4宏处理器将策略文件列表拼接成`system.conf`中间文件,这里会决定是否展开隔离宏。同理,遍历得到父目录为vendor和public的策略文件列表,使用m4宏处理器将策略文件列表拼接成`vendor.conf`中间文件,这里也会决定是否展开隔离宏。
132. 使用开源软件checkpolicy将conf中间文件编译为.cil明文策略文件,得到`system.cil`和`vendor.cil`。
143. 使用开源软件secilc将`system.cil`和`vendor.cil`进行合并,编译为二进制策略文件,编译时会进行neverallow检查,违反neverallow时会编译报错,参考[OpenHarmony SELinux常见问题](subsys-security-selinux-faq.md)。
154. 编译后的二进制策略文件会归档到`system.img`中,位于`/system/etc/selinux/targeted/policy/policy.31`。
16
17### 策略加载
18OpenHarmony SELinux策略加载主要经过以下流程:
191. init进程在启动后,通过开源软件libselinux提供的用户态操作内核态的接口将selinux二进程策略文件加载到内核。
202. 然后根据配置文件`/system/etc/selinux/config`中SELINUX字段的值设置运行模式,`SELINUX=enforcing`时,设置为强制模式,`SELINUX=permissive`时,设置为宽容模式。从OpenHarmony 3.2开始,OpenHarmony SELinux默认以强制模式运行。
21
22## contexts编译与加载
23OpenHarmony SELinux contexts包括`file_contexts`、`hdf_service_contexts`、`sevice_contexts`、`parameter_contexts`、`sehap_contexts`。这些文件不随策略编译而编译,需要单独归档。总体流程如图:
24
25**图2** OpenHarmony SELinux contexts编译和加载流程图
26
27![selinux_contexts](./figures/SELinux-contexts.png)
28
29### contexts编译
30OpenHarmony SELinux contexts编译主要包括以下流程:
311. 分别遍历各类型的contexts文件,得到每个类型contexts的文件列表。
322. 对列表分别拼接去重,生成contexts中间文件。
333. 对contexts中间文件中使用的SELinux标签校验合法性,得到contexts文件。
344. 将contexts归档到`system.img`,位于`/system/etc/selinux/targeted/contexts/`。
35
36### contexts加载
37OpenHarmony SELinux contexts加载按类型不同,加载方式不同:
38| contexts类型 | 加载方 | 用途 |
39| -------- | -------- | -------- |
40| file_contexts | 需要更新文件标签的进程 | 更新文件标签 |
41| hdf_service_contexts | hdf_devmgr |管控HDF注册与获取 |
42| sevice_contexts | samgr | 管控SA注册与获取 |
43| parameter_contexts | init | 管控参数设置 |
44| sehap_contexts | appspawn<br>installs | appspawn:设置应用标签<br>installs:设置应用数据标签 |