1e41f4b71Sopenharmony_ci# OpenHarmony SELinux Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Policy Directory Structure 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe OpenHarmony SELinux policy files are located in the **//base/security/selinux_adapter/sepolicy/ohos_policy** directory. The directory structure is as follows: 6e41f4b71Sopenharmony_ci```text 7e41f4b71Sopenharmony_ci├── Subsystem 8e41f4b71Sopenharmony_ci│ └── Component 9e41f4b71Sopenharmony_ci│ ├── public 10e41f4b71Sopenharmony_ci│ │ └── type1.te 11e41f4b71Sopenharmony_ci│ ├── vendor 12e41f4b71Sopenharmony_ci│ │ └── type2.te 13e41f4b71Sopenharmony_ci│ └── system 14e41f4b71Sopenharmony_ci│ └── type3.te 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ciThe system-related policy files are located in the **system** directory, the chipset-related policy files are located in the **vendor** directory, and the policy files for both the system and chipsets are located in the **public** directory. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## Universal Policy and Context Files 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciThe universal policy and context files contain SELinux policies to be configured during the development. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| File Name| Description| 23e41f4b71Sopenharmony_ci| -------- | -------- | 24e41f4b71Sopenharmony_ci| *.te | SELinux policy source file, which defines the types and **allow** and **neverallow** rules.| 25e41f4b71Sopenharmony_ci| file_contexts | Defines the mappings between the paths of physical files and labels (contexts).| 26e41f4b71Sopenharmony_ci| virtfs_contexts | Defines the mappings between the paths of virtual files and labels.| 27e41f4b71Sopenharmony_ci| sehap_contexts | Defines the mappings between key application information, labels of application processes, and labels of application data directories.| 28e41f4b71Sopenharmony_ci| parameter_contexts | Defines the mappings between parameters and labels.| 29e41f4b71Sopenharmony_ci| sevice_contexts | Defines the mappings between SAs and labels.| 30e41f4b71Sopenharmony_ci| hdf_service_contexts | Defines the mappings between HDF services and labels.| 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## SELinux Framework Policy Files 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciThe following table lists the SELinux framework policy files, which should not be modified generally. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| File Name| Description| 37e41f4b71Sopenharmony_ci| -------- | -------- | 38e41f4b71Sopenharmony_ci| security_classes | Defines the classes.| 39e41f4b71Sopenharmony_ci| initial_sids | Defines the SIDs.| 40e41f4b71Sopenharmony_ci| access_vectors | Defines the permissions supported by classes.| 41e41f4b71Sopenharmony_ci| glb_perm_def.spt | Defines the global macros for classes and permissions. Global macros help simplify policy statements.| 42e41f4b71Sopenharmony_ci| glb_never_def.spt | Defines global macros for **neverallow** rules.| 43e41f4b71Sopenharmony_ci| mls | Defines the multi-level security (MLS) levels.| 44e41f4b71Sopenharmony_ci| glb_te_def.spt | Defines global macros for TE rules.| 45e41f4b71Sopenharmony_ci| attributes | Defines universal sets of attributes (access control rules). When defining a policy type, you can specify attributes. Then, the policy type inherits the permissions of the attributes.| 46e41f4b71Sopenharmony_ci| glb_roles.spt | Defines roles.| 47e41f4b71Sopenharmony_ci| users | Defines users.| 48e41f4b71Sopenharmony_ci| initial_sid_contexts | Defines the initial SID contexts.| 49e41f4b71Sopenharmony_ci| fs_use | Defines the default labels for different file systems.| 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci## AVC Log Information 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ciWhen a system behavior is denied by SELinux, a log in the following format will be generated in the kernel log and hilog. 54e41f4b71Sopenharmony_ci```text 55e41f4b71Sopenharmony_ciaudit: type=1400 audit(1502458430.566:4): avc: denied { open } for pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1 56e41f4b71Sopenharmony_ci``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciIn the log: 59e41f4b71Sopenharmony_ci - **open** indicates the operation denied. 60e41f4b71Sopenharmony_ci - **pid=1658** indicates that the PID of the subject process is **1658**. 61e41f4b71Sopenharmony_ci - **comm="setenforce"** indicates that the subject process is **setenforce**. 62e41f4b71Sopenharmony_ci - **path="/sys/fs/selinux/enforce"** indicates that the object to be accessed is **/sys/fs/selinux/enforce**. 63e41f4b71Sopenharmony_ci - **dev="selinuxfs"** indicates that the object belongs to the SELinux filesystem (selinuxfs). 64e41f4b71Sopenharmony_ci - **ino=4** indicates that the file node ID is **4**. 65e41f4b71Sopenharmony_ci - **scontext=u:r:hdcd:s0** indicates that the SELinux label of the subject is **u:r:hdcd:s0**. 66e41f4b71Sopenharmony_ci - **tcontext=u:object_r:selinuxfs:s0** indicating that the SELinux label of the object is **u:object_r:selinuxfs:s0**. 67e41f4b71Sopenharmony_ci - **tclass=file** indicates the type of the object to be accessed by the subject. 68e41f4b71Sopenharmony_ci - **permissive=1** indicates that SELinux runs in permissive mode, where violations are logged but allowed. If **permissive** is set to **0**, SELinux runs in enforcing mode, where violations are denied and logged. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciYou can use the keyword "avc denied" to search for access denial logs, and configure SELinux policies based on the AVC alarms that affect services. For example, if the log information is as follows: 71e41f4b71Sopenharmony_ci```text 72e41f4b71Sopenharmony_ciaudit: type=1400 audit(1502458430.566:4): avc: denied { open } for pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ciWrite the TE rule as follows: 76e41f4b71Sopenharmony_ci```text 77e41f4b71Sopenharmony_ciallow hdcd selinuxfs:file open; 78e41f4b71Sopenharmony_ci``` 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci## Policy Format 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciAn SELinux policy, also called an SELinux rule, usually starts with **allow** or **neverallow** to define a behavior allowed or prohibited. If SELinux is enabled on a device, SELinux denies all behaviors that are not allowed. You can configure **allow** rules to allow behaviors and **neverallow** rules to prohibit risky behaviors. The following is an example of an **allow** rule: 83e41f4b71Sopenharmony_ci```text 84e41f4b71Sopenharmony_ciallow subject object:class permissions; 85e41f4b71Sopenharmony_ci``` 86e41f4b71Sopenharmony_ci This rule allows the subject to perform the **permissions** operation on the class of the object. In the rule: 87e41f4b71Sopenharmony_ci - **subject** indicates the subject, which is usually the SELinux type of the process, for example, **init**. 88e41f4b71Sopenharmony_ci - **object** indicates the object, which is usually the SELinux type of a system resource, for example, **data_file**. 89e41f4b71Sopenharmony_ci - **class** indicates the type of the object to access. For example, **file** indicates a file, **dir** indicates a directory, and **socket** indicates a socket. 90e41f4b71Sopenharmony_ci - **permissions** indicates the specific operation to perform, for example, to open, read, or write a file. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciLikewise: 93e41f4b71Sopenharmony_ci```text 94e41f4b71Sopenharmony_cineverallow subject object:class permissions; 95e41f4b71Sopenharmony_ci``` 96e41f4b71Sopenharmony_ciThis rule prohibits the subject from performing the **permissions** operation on the class of the object. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci## Policy Macros 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciTo facilitate your app experience without compromising device security, OpenHarmony SELinux provides macros to apply security policies on different versions. You can use the macro **debug_only** to apply the policies customized for the root version for debugging. To enable this macro, specify **--build-variant root** in the version build command. In the user version for commercial release, specify **--build-variant user** in the version build command to disable this macro. To use this macro, do as follows: 101e41f4b71Sopenharmony_ci```text 102e41f4b71Sopenharmony_cidebug_only(` 103e41f4b71Sopenharmony_ci allow ueventd init:fd use; 104e41f4b71Sopenharmony_ci') 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ciIn addition, you can use the **developer_only** macro to apply the policies customized for the developer mode. These policies are used for debugging of the user version. This macro is enabled by default. To use the **developer_only** macro, do as follows: 108e41f4b71Sopenharmony_ci```text 109e41f4b71Sopenharmony_cideveloper_only(` 110e41f4b71Sopenharmony_ci allow sh init:fd use; 111e41f4b71Sopenharmony_ci') 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci| Macro| Root Version| Root Version Developer Mode| User Version| User Version Developer Mode| 115e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 116e41f4b71Sopenharmony_ci| Other policies| Valid| Valid| Valid| Valid| 117e41f4b71Sopenharmony_ci| Policies controlled by **debug_only**| Valid|Valid| Invalid| Invalid| 118e41f4b71Sopenharmony_ci| Policies controlled by **developer_only**| Invalid| Valid| Invalid| Valid| 119