1# OpenHarmony SELinux Development
2
3## Policy Directory Structure
4
5The OpenHarmony SELinux policy files are located in the **//base/security/selinux_adapter/sepolicy/ohos_policy** directory. The directory structure is as follows:
6```text
7├── Subsystem
8│   └── Component
9│       ├── public
10│       │   └── type1.te
11│       ├── vendor
12│       │   └── type2.te
13│       └── system
14│           └── type3.te
15```
16The 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.
17
18## Universal Policy and Context Files
19
20The universal policy and context files contain SELinux policies to be configured during the development.
21
22| File Name| Description|
23| -------- | -------- |
24| *.te | SELinux policy source file, which defines the types and **allow** and **neverallow** rules.|
25| file_contexts | Defines the mappings between the paths of physical files and labels (contexts).|
26| virtfs_contexts | Defines the mappings between the paths of virtual files and labels.|
27| sehap_contexts | Defines the mappings between key application information, labels of application processes, and labels of application data directories.|
28| parameter_contexts | Defines the mappings between parameters and labels.|
29| sevice_contexts | Defines the mappings between SAs and labels.|
30| hdf_service_contexts | Defines the mappings between HDF services and labels.|
31
32## SELinux Framework Policy Files
33
34The following table lists the SELinux framework policy files, which should not be modified generally.
35
36| File Name| Description|
37| -------- | -------- |
38| security_classes | Defines the classes.|
39| initial_sids | Defines the SIDs.|
40| access_vectors | Defines the permissions supported by classes.|
41| glb_perm_def.spt | Defines the global macros for classes and permissions. Global macros help simplify policy statements.|
42| glb_never_def.spt | Defines global macros for **neverallow** rules.|
43| mls | Defines the multi-level security (MLS) levels.|
44| glb_te_def.spt | Defines global macros for TE rules.|
45| 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.|
46| glb_roles.spt | Defines roles.|
47| users | Defines users.|
48| initial_sid_contexts | Defines the initial SID contexts.|
49| fs_use | Defines the default labels for different file systems.|
50
51## AVC Log Information
52
53When a system behavior is denied by SELinux, a log in the following format will be generated in the kernel log and hilog.
54```text
55audit: 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
56```
57
58In the log:
59  - **open** indicates the operation denied.
60  - **pid=1658** indicates that the PID of the subject process is **1658**.
61  - **comm="setenforce"** indicates that the subject process is **setenforce**.
62  - **path="/sys/fs/selinux/enforce"** indicates that the object to be accessed is **/sys/fs/selinux/enforce**.
63  - **dev="selinuxfs"** indicates that the object belongs to the SELinux filesystem (selinuxfs).
64  - **ino=4** indicates that the file node ID is **4**.
65  - **scontext=u:r:hdcd:s0** indicates that the SELinux label of the subject is **u:r:hdcd:s0**.
66  - **tcontext=u:object_r:selinuxfs:s0** indicating that the SELinux label of the object is **u:object_r:selinuxfs:s0**.
67  - **tclass=file** indicates the type of the object to be accessed by the subject.
68  - **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.
69
70You 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:
71```text
72audit: 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
73```
74
75Write the TE rule as follows:
76```text
77allow hdcd selinuxfs:file open;
78```
79
80## Policy Format
81
82An 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:
83```text
84allow subject object:class permissions;
85```
86  This rule allows the subject to perform the **permissions** operation on the class of the object. In the rule:
87  - **subject** indicates the subject, which is usually the SELinux type of the process, for example, **init**.
88  - **object** indicates the object, which is usually the SELinux type of a system resource, for example, **data_file**.
89  - **class** indicates the type of the object to access. For example, **file** indicates a file, **dir** indicates a directory, and **socket** indicates a socket.
90  - **permissions** indicates the specific operation to perform, for example, to open, read, or write a file.
91
92Likewise:
93```text
94neverallow subject object:class permissions;
95```
96This rule prohibits the subject from performing the **permissions** operation on the class of the object.
97
98## Policy Macros
99
100To 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:
101```text
102debug_only(`
103    allow ueventd init:fd use;
104')
105```
106
107In 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:
108```text
109developer_only(`
110    allow sh init:fd use;
111')
112```
113
114| Macro| Root Version| Root Version Developer Mode| User Version| User Version Developer Mode|
115| -------- | -------- | -------- | -------- | -------- |
116| Other policies| Valid| Valid| Valid| Valid|
117| Policies controlled by **debug_only**| Valid|Valid| Invalid| Invalid|
118| Policies controlled by **developer_only**| Invalid| Valid| Invalid| Valid|
119