1e41f4b71Sopenharmony_ci# Compiling and Loading OpenHarmony SELinux 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Compiling and Loading Policies 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe OpenHarmony SELinux policies are compiled by the compiler, and loaded after the init process is started. The following figure shows the compilation and loading process. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci**Figure 1** OpenHarmony SELinux compilation and loading process 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci### Compiling Policies 11e41f4b71Sopenharmony_ciThe OpenHarmony SELinux policy compilation process is as follows: 12e41f4b71Sopenharmony_ci1. Traverse the **//base/security/selinux_adapter/sepolicy/** directory to obtain system-related policy files (files in **/system** and **/public**) and chipset-related policy files (files in **/vendor** and **/public**). Then, use m4 (a macro processor) to concatenate the system-related policy file lists into the **system.conf** file, and the chipset-related policy file lists into the **vendor.conf** file. You also need to determine whether to enable the macro to apply the policies here. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci The **//base/security/selinux_adapter/sepolicy/** directory is traversed in the following sequence: 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 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 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci2. Use checkpolicy (open-source software) to compile the **system.conf** and **vendor.conf** files into **system.cil** and **vendor.cil**, respectively. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci3. Use secilc (open-source software) to combine **system.cil** and **vendor.cil** and compile them into a binary policy file. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci During the compilation process, **neverallow** check is performed. If any **neverallow** violation is found, an error will be reported. For details about how to resolve the problem, see [OpenHarmony SELinux FAQs](subsys-security-selinux-faq.md). 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci4. Archive the binary policy file to **system.img** in **/system/etc/selinux/targeted/policy/policy.31**. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci### Loading Policies 27e41f4b71Sopenharmony_ciThe OpenHarmony SELinux policy loading process is as follows: 28e41f4b71Sopenharmony_ci1. After started, the init process loads the SELinux binary policy file to the kernel by using the API provided by libselinux. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci2. The SELinux running mode is set based on the value of the **SELINUX** field in the **/system/etc/selinux/config** file. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci The running mode can be enforcing mode (where violations are denied and logged) or permissive mode (where violations are logged but allowed). Since OpenHarmony 3.2, OpenHarmony SELinux runs in enforcing mode by default. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## Compiling and Loading Contexts 35e41f4b71Sopenharmony_ciOpenHarmony SELinux contexts include **file_contexts**, **hdf_service_contexts**, **sevice_contexts**, **parameter_contexts**, and **sehap_contexts**. Instead of being compiled with policies, these context files must be archived separately. The following figure shows the overall process. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Figure 2** OpenHarmony SELinux context compilation and loading process 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci### Compiling Contexts 42e41f4b71Sopenharmony_ciThe OpenHarmony SELinux context compilation process is as follows: 43e41f4b71Sopenharmony_ci1. Traverse the context files of each type to obtain a list of context files separately. 44e41f4b71Sopenharmony_ci2. Combine and deduplicate the file lists to generate a temporary context file of each type. 45e41f4b71Sopenharmony_ci3. Verify the SELinux labels in each context file and generate the final context files. 46e41f4b71Sopenharmony_ci4. Archive the final context files to **system.img** in **/system/etc/selinux/targeted/contexts/**. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci### Loading Contexts 49e41f4b71Sopenharmony_ciThe loading of OpenHarmony SELinux contexts varies with the context type. 50e41f4b71Sopenharmony_ci| Context Type| Loaded By| Loaded To| 51e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 52e41f4b71Sopenharmony_ci| file_contexts | Process that needs to update the file labels| Update file labels.| 53e41f4b71Sopenharmony_ci| hdf_service_contexts | hdf_devmgr |Control HDF service registration and obtaining.| 54e41f4b71Sopenharmony_ci| sevice_contexts | Samgr | Control SA registration and obtaining. | 55e41f4b71Sopenharmony_ci| parameter_contexts | init | Control parameter setting.| 56e41f4b71Sopenharmony_ci| sehap_contexts | appspawn<br>installs | Set application labels.<br>Set application data labels.| 57