1e41f4b71Sopenharmony_ci# OpenHarmony SELinux Check 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciOpenHarmony provides a set of SELinux policy checks to complement the cases that may not be covered by the **neverallow** check and manual review. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## Checking for Regular Expressions in Secondary Directories of /data in file_contexts 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci### When to Use 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciIn the **/data** partition, there are a large number of application files and user files. Frequent access to these files may cause fragmentation. However, labelling the files may comprise system performance. To solve this problem, the secondary directories under **/data** cannot contain regular expressions. The check scans the **file_contexts** file to check for regular expressions. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci### Error Information 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciThe error "Regex is not allowed in the secondary directory under data" will be reported during the compilation if any regular expression is found in the secondary directories of **/data** in **file_contexts**. The error information is as follows: 17e41f4b71Sopenharmony_ci```text 18e41f4b71Sopenharmony_ciRegex is not allowed in the secondary directory under data, check '/data/log(/.*)?' failed in file out/rk3568/obj/base/security/selinux_adapter/file_contexts:214 19e41f4b71Sopenharmony_ci There are two solutions: 20e41f4b71Sopenharmony_ci 1. Add '/data/log(/.*)?' to whitelist file 'data_regex_whitelist.txt' under 'base/security/selinux_adapter/sepolicy'; 21e41f4b71Sopenharmony_ci 2. Modify '/data/log(/.*)?' to remove the regular expression 22e41f4b71Sopenharmony_ci``` 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci### Block Reason 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciThe regular expression **log(/.*)?** is detected in the secondary directory of **/data**. 27e41f4b71Sopenharmony_ci```text 28e41f4b71Sopenharmony_ci/data/log(/.*)? u:object_r:data_log:s0 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### Solution 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- Method 1: Add **/data/log/(.*)** to the **data_regex_whitelist.txt** file in the **//base/security/selinux_adapter/sepolicy/** directory.<br> 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the file, carefully review the modification to avoid security risks. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- Method 2: Modify the regular expression. For example, modify the regular expression as follows: 40e41f4b71Sopenharmony_ci ```text 41e41f4b71Sopenharmony_ci /data/log u:object_r:data_log:s0 42e41f4b71Sopenharmony_ci /data/log/(.*)? u:object_r:data_log:s0 43e41f4b71Sopenharmony_ci ``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## Checking for Partition Labels in file_contexts 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci### When to Use 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciPartition labels are labels of the first-level directory under the root directory. Partition labels include the following: 50e41f4b71Sopenharmony_ci```text 51e41f4b71Sopenharmony_ciu:object_r:dev_file:s0 52e41f4b71Sopenharmony_ciu:object_r:etc_file:s0 53e41f4b71Sopenharmony_ciu:object_r:lib_file:s0 54e41f4b71Sopenharmony_ciu:object_r:config_file:s0 55e41f4b71Sopenharmony_ciu:object_r:updater_file:s0 56e41f4b71Sopenharmony_ciu:object_r:system_file:s0 57e41f4b71Sopenharmony_ciu:object_r:sys_prod_file:s0 58e41f4b71Sopenharmony_ciu:object_r:chip_prod_file:s0 59e41f4b71Sopenharmony_ciu:object_r:vendor_file:s0 60e41f4b71Sopenharmony_ciu:object_r:data_file:s0 61e41f4b71Sopenharmony_ciu:object_r:module_update_file:s0 62e41f4b71Sopenharmony_ci``` 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciAvoid using partition labels for paths in **file_contexts**, which may pose security risks on subdirectories in the root directory. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci### Error Information 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciThe error "partition label is not allow to use" will be reported during the compilation if improper use of partition labels is found in **file_contexts**. The error information is as follows: 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci``` 71e41f4b71Sopenharmony_cipartition label is not allow to use, check '/data/log u:object_r:data_file:s0' failed in file out/rk3568/obj/base/security/selinux_adapter/file_contexts:213 72e41f4b71Sopenharmony_ci There are two solutions: 73e41f4b71Sopenharmony_ci 1. Add '/data/log u:object_r:data_file:s0' to whitelist file 'partition_label_use_whitelist.txt' under 'base/security/selinux_adapter/sepolicy'; 74e41f4b71Sopenharmony_ci 2. Change '/data/log u:object_r:data_file:s0' to avoid using label in ['u:object_r:dev_file:s0', 'u:object_r:etc_file:s0', 'u:object_r:lib_file:s0', 'u:object_r:config_file:s0', 'u:object_r:updater_file:s0', 'u:object_r:system_file:s0', 'u:object_r:sys_prod_file:s0', 'u:object_r:chip_prod_file:s0', 'u:object_r:vendor_file:s0', 'u:object_r:data_file:s0', 'u:object_r:module_update_file:s0'] 75e41f4b71Sopenharmony_ci``` 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### Block Reason 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciThe partition label **u:object_r:data_file:s0** is configured for **/data/log**. 80e41f4b71Sopenharmony_ci```text 81e41f4b71Sopenharmony_ci/data/log u:object_r:data_file:s0 82e41f4b71Sopenharmony_ci``` 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci### Solution 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci- Method 1: Add the path and label **/data/log u:object_r:data_file:s0** to the **partition_label_use_whitelist.txt** file in the **//base/security/selinux_adapter/sepolicy/** directory. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the file, carefully review the modification to avoid security risks. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci- Method 2: Modify the label of **/data/log**. For example, modify the label as follows: 93e41f4b71Sopenharmony_ci ```text 94e41f4b71Sopenharmony_ci /data/log u:object_r:data_log:s0 95e41f4b71Sopenharmony_ci ``` 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci## Checking for High-Risk Permission Groups 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci### When to Use 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciWhen a pair of subject and object have different SELinux permissions, an attack path may be formed. This check is used to check user version policies and developer mode policies. The check items are configured in the **base/security/selinux_adapter/scripts/selinux_check/config/perm_group.json** file. 102e41f4b71Sopenharmony_ci```json 103e41f4b71Sopenharmony_ci{ 104e41f4b71Sopenharmony_ci "check_rules": [ 105e41f4b71Sopenharmony_ci { 106e41f4b71Sopenharmony_ci "name": "execute and execute_no_trans", 107e41f4b71Sopenharmony_ci "description": "process label should transform while execute a file", 108e41f4b71Sopenharmony_ci "perm_group": [ 109e41f4b71Sopenharmony_ci { 110e41f4b71Sopenharmony_ci "tclass": "*", 111e41f4b71Sopenharmony_ci "perm": "execute execute_no_trans" 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci ] 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci ] 116e41f4b71Sopenharmony_ci} 117e41f4b71Sopenharmony_ci``` 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**check_rules** specifies all the check items. Each check item contains the following fields: 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci- **name**: name of the check item. 122e41f4b71Sopenharmony_ci- **description**: description of the check item. 123e41f4b71Sopenharmony_ci- **perm_group**: list of permissions to check. **perm_group** consists of **tclass** and **perm**. **tclass** specifies the type of the object to access. If it is set to **\***, it indicates all types. **perm** specifies the permission of the operation. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci### Error Information 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciThe error "check rule 'xxx' in user mode failed" will be reported during the compilation if incorrect permission configuration is found. 'xxx' indicates the name of the check item. The error information is as follows: 128e41f4b71Sopenharmony_ci```text 129e41f4b71Sopenharmony_ci check rule 'execute and execute_no_trans' in user mode failed, process label should transform while execute a file 130e41f4b71Sopenharmony_ci violation list (scontext tcontext): 131e41f4b71Sopenharmony_ci appspawn appspawn_exec 132e41f4b71Sopenharmony_ci There are two solutions: 133e41f4b71Sopenharmony_ci 1. Add the above list to whitelist file 'perm_group_whitelist.json' under 'base/security/selinux_adapter/sepolicy' in 'user' part of 'execute and execute_no_trans' 134e41f4b71Sopenharmony_ci 2. Change the policy to avoid violating rule 'execute and execute_no_trans' 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci check rule 'execute and execute_no_trans' in developer mode failed, process label should transform while execute a file 137e41f4b71Sopenharmony_ci violation list (scontext tcontext): 138e41f4b71Sopenharmony_ci appspawn appspawn_exec 139e41f4b71Sopenharmony_ci There are two solutions: 140e41f4b71Sopenharmony_ci 3. Add the above list to whitelist file 'perm_group_whitelist.json' under 'base/security/selinux_adapter/sepolicy' in 'developer' part of 'execute and execute_no_trans' 141e41f4b71Sopenharmony_ci 4. Change the policy to avoid violating rule 'execute and execute_no_trans' 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci### Block Reason 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciThe subject **appspawn** and the object **appspawn_exec** have permissions **execute** and **execute_no_trans** in the user version and developer mode policies. 147e41f4b71Sopenharmony_ci```text 148e41f4b71Sopenharmony_ciallow appspawn appspawn_exec:file { execute execute_no_trans }; 149e41f4b71Sopenharmony_ci``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci### Solution 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci- Method 1: Add the subject and object to the **user** or **developer** field in the **perm_group_whitelist.json** file in **//base/security/selinux_adapter/sepolicy/**. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the file, carefully review the modification to avoid security risks. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci The following is an example of the file: 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci ```text 162e41f4b71Sopenharmony_ci { 163e41f4b71Sopenharmony_ci "whitelist": [ 164e41f4b71Sopenharmony_ci { 165e41f4b71Sopenharmony_ci "name": "execute and execute_no_trans", 166e41f4b71Sopenharmony_ci "user": [ 167e41f4b71Sopenharmony_ci "appspawn appspawn_exec" 168e41f4b71Sopenharmony_ci ], 169e41f4b71Sopenharmony_ci "developer": [ 170e41f4b71Sopenharmony_ci ] 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci ] 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci ``` 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci **whitelist** specifies all the permission group check items that are allowed. Each item contains the following fields: 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci - **name**: name of the allowed check item, that is, the name of the check item. 179e41f4b71Sopenharmony_ci - **user**: policy allowed for the user version. 180e41f4b71Sopenharmony_ci - **developer**: policy allowed for the developer mode. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci The following table provides the rules for determine where you should add the subject and object. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci **Table 1** Rules for determining the entry of the subject and object 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci | Violate User Version Policy | Violate Developer Mode Policy | Entry Field | 187e41f4b71Sopenharmony_ci | --------------------------- | ----------------------------- | ------------------------------------------------------------ | 188e41f4b71Sopenharmony_ci | Yes | Yes | user | 189e41f4b71Sopenharmony_ci | No | Yes | developer | 190e41f4b71Sopenharmony_ci | Yes | No | user. In addition, delete the subject and object from the **developer** field. | 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci- Method 2: Modify the policy to avoid the violation. 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci### Deleting Redundant Policies from a Whitelist File 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciIf the whitelist file still contains the policy that has been deleted, the error "remove the following unnecessary whitelists in rule 'xxx' part 'user'" will be reported in the compilation. 'xxx' indicates the name of the check item intercepted. The error information is as follows: 197e41f4b71Sopenharmony_ci```text 198e41f4b71Sopenharmony_ci check rule 'execute and execute_no_trans' failed in whitelist file 'perm_group_whitelist.json' 199e41f4b71Sopenharmony_ci remove the following unnecessary whitelists in rule 'execute and execute_no_trans' part 'user': 200e41f4b71Sopenharmony_ci appspawn appspawn_exec 201e41f4b71Sopenharmony_ci check rule 'execute and execute_no_trans' failed in whitelist file 'perm_group_whitelist.json' 202e41f4b71Sopenharmony_ci remove the following unnecessary whitelists in rule 'execute and execute_no_trans' part 'developer': 203e41f4b71Sopenharmony_ci appspawn appspawn_exec 204e41f4b71Sopenharmony_ci``` 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ciTo solve the problem, you need to delete the redundant policy from the **perm_group_whitelist.json** file in **//base/security/selinux_adapter/sepolicy/**. The following is an example of the file: 207e41f4b71Sopenharmony_ci```text 208e41f4b71Sopenharmony_ci{ 209e41f4b71Sopenharmony_ci "whitelist": [ 210e41f4b71Sopenharmony_ci { 211e41f4b71Sopenharmony_ci "name": "execute and execute_no_trans", 212e41f4b71Sopenharmony_ci "user": [ 213e41f4b71Sopenharmony_ci "appspawn appspawn_exec" 214e41f4b71Sopenharmony_ci ], 215e41f4b71Sopenharmony_ci "developer": [ 216e41f4b71Sopenharmony_ci ] 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci ] 219e41f4b71Sopenharmony_ci} 220e41f4b71Sopenharmony_ci``` 221e41f4b71Sopenharmony_ciIn the **perm_group_whitelist.json** file, locate **execute and execute_no_trans**, and delete **appspawn appspawn_exec** from the **user** field. 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**Table 2** Rules for deleting redundant policy from the whitelist file 224e41f4b71Sopenharmony_ci| User Version Whitelist Redundant | Developer Mode Whitelist Redundant | Field to Delete | 225e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 226e41f4b71Sopenharmony_ci| Yes | Yes | user | 227e41f4b71Sopenharmony_ci| No | Yes | developer | 228e41f4b71Sopenharmony_ci| Yes | No | user | 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci## Checking for Invalid Policy Baseline of Critical Processes 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci### When to Use 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ciThe SELinux policies of OpenHarmony critical processes, such as shell and console, must be protected against unauthorized deletion and addition, which may cause system unavailability or security risks. The baseline policies of the critical process *xx* are defined in the *xx***.baseline** file in **//base/security/selinux_adapter/sepolicy/**. The following is an example of the **sh** (SElinux label) baseline: 236e41f4b71Sopenharmony_ci```text 237e41f4b71Sopenharmony_ci(allow sh vendor_file (dir (search))) 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_cideveloper_only(` 240e41f4b71Sopenharmony_ci(allow sh system_lib_file (dir (search))) 241e41f4b71Sopenharmony_ci') 242e41f4b71Sopenharmony_ci``` 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ciThe policy enclosed in **developer_only** applies to the developer mode only. Other policies apply to both the user version and the developer mode. 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci### Error Information 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ciThe error "check 'xxx' baseline in user mode failed" will be reported in the compilation if a policy is added or deleted for a critical process. 'xxx' indicates the SELinux label of the process. The error information is as follows: 249e41f4b71Sopenharmony_ci```text 250e41f4b71Sopenharmony_ci check 'sh' baseline in user mode failed 251e41f4b71Sopenharmony_ci expect rule: (allow sh vendor_file (dir ())); actual rule: (allow sh vendor_file (dir (search))) 252e41f4b71Sopenharmony_ci There are two solutions: 253e41f4b71Sopenharmony_ci 1. Add the above actual rule to baseline file 'sh.baseline' under 'base/security/selinux_adapter/sepolicy' 254e41f4b71Sopenharmony_ci 2. Change the policy to satisfy expect rule 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci check 'sh' baseline in developer mode failed 257e41f4b71Sopenharmony_ci expect rule: (allow sh vendor_file (dir ())); actual rule: (allow sh vendor_file (dir (search))) 258e41f4b71Sopenharmony_ci There are two solutions: 259e41f4b71Sopenharmony_ci 1. Add the above actual rule to baseline file 'sh.baseline' under 'base/security/selinux_adapter/sepolicy' and add developer_only 260e41f4b71Sopenharmony_ci 2. Change the policy to satisfy expect rule 261e41f4b71Sopenharmony_ci``` 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci### Block Reason 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ciThe policy **allow sh vendor_file:dir search;**, that is, **(allow sh vendor_file (dir (search)))** in the CIL format, is added for the shell process (labelled **sh**), which violates the process baseline for the user version and developer mode. The expected baseline in the CIL format is **(allow sh vendor_file (dir ()))**. 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci### Solution 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci- Method 1: Add the policy (in the CLI format) indicated by **actual rule** in the error message to the baseline file *xx*.**baseline** in **//base/security/selinux_adapter/sepolicy/** as a new baseline. In the file name, *xx* indicates the process label. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the baseline file, carefully review the modification to avoid security risks. 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci The following table provides the rules for modifying the policy baseline. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci **Table 3** Rules for modifying the policy baseline for critical processes 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci | User Version Baseline Error | Developer Mode Baseline Error | Update Policy in developer_only | 280e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 281e41f4b71Sopenharmony_ci | Yes | Yes | No | 282e41f4b71Sopenharmony_ci | No | Yes | Yes | 283e41f4b71Sopenharmony_ci | Yes | No | Move the policy out of **developer_only**. | 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci- Method 2: Modify the policy to avoid baseline violation. 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci### Deleting Redundant Policies from a Baseline File 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ciIf the baseline file still contains the policy that has been deleted, the error "check 'xx' baseline in user mode failed" will be reported in the compilation. 'xx' indicates the process label intercepted. The error information is as follows: 290e41f4b71Sopenharmony_ci``` 291e41f4b71Sopenharmony_ci check 'sh' baseline in user mode failed 292e41f4b71Sopenharmony_ci expect rule: (allow sh rootfs (dir (search))); actual rule: (allow sh rootfs (dir ())) 293e41f4b71Sopenharmony_ci There are two solutions: 294e41f4b71Sopenharmony_ci 1. Add the above actual rule to baseline file 'sh.baseline' under 'base/security/selinux_adapter/sepolicy' 295e41f4b71Sopenharmony_ci 2. Change the policy to satisfy expect rule 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci check 'sh' baseline in developer mode failed 298e41f4b71Sopenharmony_ci expect rule: (allow sh rootfs (dir (search))); actual rule: (allow sh rootfs (dir ())) 299e41f4b71Sopenharmony_ci There are two solutions: 300e41f4b71Sopenharmony_ci 1. Add the above actual rule to baseline file 'sh.baseline' under 'base/security/selinux_adapter/sepolicy' and add developer_only 301e41f4b71Sopenharmony_ci 2. Change the policy to satisfy expect rule 302e41f4b71Sopenharmony_ci``` 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ciDelete the policy from the baseline file, for example, **sh.baseline** in **//base/security/selinux_adapter/sepolicy/**. 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ciDelete the redundant policy, for example, **(allow sh rootfs (dir (search)))**, and ensure that the actual policy is the same as the expected one. The following table provides the rules for deleting redundant policies from the baseline file. 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci**Table 4** Rules for deleting redundant policies from the baseline file 309e41f4b71Sopenharmony_ci| User Version Baseline Redundant | Developer Mode Baseline Redundant | Field to Delete | 310e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 311e41f4b71Sopenharmony_ci| Yes | Yes | Policy outside **developer_only** | 312e41f4b71Sopenharmony_ci| No | Yes | Policy in **developer_only** | 313e41f4b71Sopenharmony_ci| Yes | No | Policy outside **developer_only** | 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci## Checking ioctl Permission Policies 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci### When to Use 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciFor **ioctl**, you need to restrict **ioctlcmd** based on AVC logs in addition to configuring **allow** rules. Granting all permissions for **ioctlcmd** violates the least privilege principle. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci### Error Information 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ciThe error "check ioctl rule in user mode failed." will be reported during the compilation if the **allow** rule contains the access permission to **ioctl** but the **ioctl** permissions are not restricted. 324e41f4b71Sopenharmony_ci```text 325e41f4b71Sopenharmony_ci check ioctl rule in user mode failed. 326e41f4b71Sopenharmony_ci violation list (allow scontext tcontext:tclass ioctl) 327e41f4b71Sopenharmony_ci allow wifi_host data_service_el1_file:file ioctl; 328e41f4b71Sopenharmony_ci allow wifi_host dev_hdfwifi:chr_file ioctl; 329e41f4b71Sopenharmony_ci allow write_updater updater_block_file:blk_file ioctl; 330e41f4b71Sopenharmony_ci please add "allowxperm" rule based on the above list. 331e41f4b71Sopenharmony_ci``` 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci### Block Reason 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ciThe **allow scontext tcontext:tclass ioctl** rule allows all **ioctl** permissions for **tcontext:tclass**, which violates the least privilege principle. Specific **allowxperm** rules need to be added to implement fine-grained control over the **ioctl** permissions. 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci### Solution 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 340e41f4b71Sopenharmony_ci- Method 1: Restrict **ioctlcmd** of **ioctl** based on the AVC log. For example, the AVC log is as follows: 341e41f4b71Sopenharmony_ci ```text 342e41f4b71Sopenharmony_ci #avc: denied { ioctl } for pid=1 comm="init" path="/data/app/el1/bundle/public" dev="mmcblk0p11" ino=652804 ioctlcmd=0x6613 scontext=u:r:init:s0 tcontext=u:object_r:data_app_el1_file:s0 tclass=dir permissive=0 343e41f4b71Sopenharmony_ci ``` 344e41f4b71Sopenharmony_ci Based on the AVC log, set the SELinux policy as follows: 345e41f4b71Sopenharmony_ci ```text 346e41f4b71Sopenharmony_ci allow init data_app_el1_file:dir { ioctl }; 347e41f4b71Sopenharmony_ci ``` 348e41f4b71Sopenharmony_ci In addition, based on "ioctlcmd=0x6613" in the AVC log, add **allowxperm** to further restrict the **ioctl** permissions for the same user or developer mode. 349e41f4b71Sopenharmony_ci ```text 350e41f4b71Sopenharmony_ci allowxperm init data_app_el1_file:dir ioctl { 0x6613 }; 351e41f4b71Sopenharmony_ci ``` 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci- Method 2: Add "scontext tcontext tclass" to the **whitelist ioctl_xperm_whitelist.json** file in **//base/security/selinux_adapter/sepolicy/**. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the whitelist file, carefully review the modification to avoid security risks. 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci In the AVC log, **user mode** indicates that the policy is the baseline shared by the user and developer modes, and **developer mode** indicates that the policy is used only as the baseline in developer mode and is added to the whitelist. 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci ```text 360e41f4b71Sopenharmony_ci { 361e41f4b71Sopenharmony_ci "whitelist": { 362e41f4b71Sopenharmony_ci "user": [ 363e41f4b71Sopenharmony_ci "wifi_host data_service_el1_file file" 364e41f4b71Sopenharmony_ci ], 365e41f4b71Sopenharmony_ci "developer": [ 366e41f4b71Sopenharmony_ci ] 367e41f4b71Sopenharmony_ci } 368e41f4b71Sopenharmony_ci } 369e41f4b71Sopenharmony_ci ``` 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci## Checking Permissions on the Permissive Subject Type 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci### When to Use 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ciThe **permissive** subject type allows access to all objects, which violates the least privilege principle. 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci### Error Information 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ciThe error "check permissive rule in user mode failed." will be reported during the compilation if the policy file contains "permissive scontext;". 381e41f4b71Sopenharmony_ci```text 382e41f4b71Sopenharmony_ci check permissive rule in user mode failed. 383e41f4b71Sopenharmony_ci violation list (scontext): 384e41f4b71Sopenharmony_ci sa_subsys_dfx_service 385e41f4b71Sopenharmony_ci There are two solutions: 386e41f4b71Sopenharmony_ci 1. Add the above list to whitelist file 'permissive_whitelist.json' under 'base/security/selinux_adapter/sepolicy' in 'user' mode. 387e41f4b71Sopenharmony_ci 2. Change the policy to avoid violating rule. 388e41f4b71Sopenharmony_ci``` 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci### Block Reason 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ciThe rules contain new **permissive** subject type. 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci### Solution 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ciYou can use either of the following methods to solve the problem: 397e41f4b71Sopenharmony_ci- Method 1: Delete unnecessary **permissive** definitions. 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci- Method 2: Add the subject type **scontext** to the **permissive_whitelist.json** file in **//base/security/selinux_adapter/sepolicy/**. 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci > **NOTE**<br>Before modifying the whitelist file, carefully review the modification to avoid security risks. 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci In the AVC log, **user mode** indicates that the policy is the baseline shared by the user and developer modes, and **developer mode** indicates that the policy is used only as the baseline in developer mode and is added to the whitelist. 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci ```text 406e41f4b71Sopenharmony_ci { 407e41f4b71Sopenharmony_ci "whitelist": { 408e41f4b71Sopenharmony_ci "user": [ 409e41f4b71Sopenharmony_ci "sa_subsys_dfx_service" 410e41f4b71Sopenharmony_ci ], 411e41f4b71Sopenharmony_ci "developer": [ 412e41f4b71Sopenharmony_ci ] 413e41f4b71Sopenharmony_ci } 414e41f4b71Sopenharmony_ci } 415e41f4b71Sopenharmony_ci ``` 416