1e41f4b71Sopenharmony_ci# Configuring an OpenHarmony SELinux Policy for a File
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## File in a Read-Only Partition
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci**Scenario**
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciIn 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci**Procedure**
10e41f4b71Sopenharmony_ci1. In **file_contexts**, define the mapping between the absolute path of the file and the label. Regular expressions can be used.
11e41f4b71Sopenharmony_ci    ```text
12e41f4b71Sopenharmony_ci    /system/lib(/.*)?    u:object_r:system_lib_file:s0
13e41f4b71Sopenharmony_ci    ```
14e41f4b71Sopenharmony_ci2. Define **system_lib_file** in **type.te** to make **u:object_r:system_lib_file:s0** valid.
15e41f4b71Sopenharmony_ci    ```text
16e41f4b71Sopenharmony_ci    type system_lib_file, system_file_attr, file_attr;
17e41f4b71Sopenharmony_ci    ```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## File in a Read/Write Partition
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**Scenario**
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciThe 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.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**Procedure**
27e41f4b71Sopenharmony_ci1. In **file_contexts**, define the mapping between the absolute path of the file and the label. Regular expressions can be used.
28e41f4b71Sopenharmony_ci    ```text
29e41f4b71Sopenharmony_ci    /data/service/el0(/.*)?    u:object_r:data_service_el0_file:s0
30e41f4b71Sopenharmony_ci    ```
31e41f4b71Sopenharmony_ci    
32e41f4b71Sopenharmony_ci2. Define **data_service_el0_file** in **type.te** to make **u:object_r:data_service_el0_file:s0** valid.
33e41f4b71Sopenharmony_ci    ```text
34e41f4b71Sopenharmony_ci    type data_service_el0_file, file_attr, data_file_attr;
35e41f4b71Sopenharmony_ci    ```
36e41f4b71Sopenharmony_ci    
37e41f4b71Sopenharmony_ci  3. Make the label take effect. 
38e41f4b71Sopenharmony_ci      
39e41f4b71Sopenharmony_ci      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.
40e41f4b71Sopenharmony_ci      
41e41f4b71Sopenharmony_ci      | Scenario| Label Update|
42e41f4b71Sopenharmony_ci      | -------- | -------- |
43e41f4b71Sopenharmony_ci| 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.|
44e41f4b71Sopenharmony_ci      | 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.|
45e41f4b71Sopenharmony_ci      | The directory or file is created by a process in running.| Use **Restorecon** to update the label after the directory or file is created.|
46e41f4b71Sopenharmony_ci      
47e41f4b71Sopenharmony_ci      For details about the APIs, see [OpenHarmony SELinux APIs](subsys-security-selinux-func.md).
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci## File in a Virtual File System
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**Scenario**
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciFor the files in a virtual file system, for example, the files in **/proc** and **/sys**, you can configure labels in the **virtfs_contexts** file.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**Procedure**
57e41f4b71Sopenharmony_ci1. In **virtfs_contexts**, configure the file-label mapping using the **genfscon** syntax.
58e41f4b71Sopenharmony_ci    ```text
59e41f4b71Sopenharmony_ci    genfscon  proc /iomem  u:object_r:proc_iomem_file:s0
60e41f4b71Sopenharmony_ci    ```
61e41f4b71Sopenharmony_ci2. Define **proc_iomem_file** in **type.te** to make **u:object_r:proc_iomem_file:s0** valid.
62e41f4b71Sopenharmony_ci    ```text
63e41f4b71Sopenharmony_ci    type proc_iomem_file, fs_attr, proc_attr;
64e41f4b71Sopenharmony_ci    ```
65