1# Configuring an OpenHarmony SELinux Policy for a File
2
3## File in a Read-Only Partition
4
5**Scenario**
6
7In OpenHarmony, some image files, such as **system.img** and **vendor.img**, are mounted as read-only to prevent them from being corrupted. Generally, these files are assigned with an SELinux label in the compilation process.
8
9**Procedure**
101. In **file_contexts**, define the mapping between the absolute path of the file and the label. Regular expressions can be used.
11    ```text
12    /system/lib(/.*)?    u:object_r:system_lib_file:s0
13    ```
142. Define **system_lib_file** in **type.te** to make **u:object_r:system_lib_file:s0** valid.
15    ```text
16    type system_lib_file, system_file_attr, file_attr;
17    ```
18
19## File in a Read/Write Partition
20
21**Scenario**
22
23The read/write partitions in OpenHarmony store the files generated dynamically and accessed by applications. For example, the **/data** directory holds user data and data generated and accessed by applications. By default, these files inherit the label of their parent directory. For security purposes, you can configure a label after the file is generated.
24
25
26**Procedure**
271. In **file_contexts**, define the mapping between the absolute path of the file and the label. Regular expressions can be used.
28    ```text
29    /data/service/el0(/.*)?    u:object_r:data_service_el0_file:s0
30    ```
31
322. Define **data_service_el0_file** in **type.te** to make **u:object_r:data_service_el0_file:s0** valid.
33    ```text
34    type data_service_el0_file, file_attr, data_file_attr;
35    ```
36
37  3. Make the label take effect.
38
39      The **file_contexts** file only defines the mapping between the file path and the label. The label cannot be automatically assigned to the file. The process needs to proactively trigger the label update. The label update operation varies depending on how the file is generated.
40
41      | Scenario| Label Update|
42      | -------- | -------- |
43| The directory or file is created by **mkdir** in the .cfg file after the process is started by **init** from .cfg.| The **init** used to parse the **mkdir** command in the .cfg file has integrated the **Restorecon** API. Therefore, **mkdir** updates the file label.|
44      | The directory or file is created by **mkdir** in the .cfg file during the startup process.| The **init** used to parse the **mkdir** command in the .cfg file has integrated the **Restorecon** API. Therefore, **mkdir** updates the file label.|
45      | The directory or file is created by a process in running.| Use **Restorecon** to update the label after the directory or file is created.|
46
47      For details about the APIs, see [OpenHarmony SELinux APIs](subsys-security-selinux-func.md).
48
49## File in a Virtual File System
50
51**Scenario**
52
53For the files in a virtual file system, for example, the files in **/proc** and **/sys**, you can configure labels in the **virtfs_contexts** file.
54
55
56**Procedure**
571. In **virtfs_contexts**, configure the file-label mapping using the **genfscon** syntax.
58    ```text
59    genfscon  proc /iomem  u:object_r:proc_iomem_file:s0
60    ```
612. Define **proc_iomem_file** in **type.te** to make **u:object_r:proc_iomem_file:s0** valid.
62    ```text
63    type proc_iomem_file, fs_attr, proc_attr;
64    ```
65