1c1ed15f1Sopenharmony_ci# selinux_adapter 2c1ed15f1Sopenharmony_ci 3c1ed15f1Sopenharmony_ci## Introduction 4c1ed15f1Sopenharmony_ci 5c1ed15f1Sopenharmony_ciSecurity-Enhanced Linux (SELinux) is an outstanding security module in the history of Linux with a set of kernel modifications and user-space tools supporting mandatory access control (MAC) based on security rules.SELinux has been added to various Linux distributions. The software architecture of SELinux attempts to separate enforcement of security decisions from the security policy and streamlines the amount of software involved with security policy enforcement.This component provide MAC protect for system object like file, parameter, service and so on. Providing neverallow rules to limit high-risk operations in the system and reduce system security risks. 6c1ed15f1Sopenharmony_ci 7c1ed15f1Sopenharmony_ciThe flow of access control shown in following figure: 8c1ed15f1Sopenharmony_ci 9c1ed15f1Sopenharmony_ci 10c1ed15f1Sopenharmony_ci 11c1ed15f1Sopenharmony_ci## Directory Structure 12c1ed15f1Sopenharmony_ci 13c1ed15f1Sopenharmony_ci``` 14c1ed15f1Sopenharmony_ci. 15c1ed15f1Sopenharmony_ci├── config # Third-party library configuration file of the board. 16c1ed15f1Sopenharmony_ci├── docs # Documents. 17c1ed15f1Sopenharmony_ci│ └── images 18c1ed15f1Sopenharmony_ci├── interfaces 19c1ed15f1Sopenharmony_ci│ ├── policycoreutils # libload_policy.so and librestorecon.so of the board. 20c1ed15f1Sopenharmony_ci│ │ ├── include 21c1ed15f1Sopenharmony_ci│ │ └── src 22c1ed15f1Sopenharmony_ci│ └── tools # load_policy and restorecon of the board. 23c1ed15f1Sopenharmony_ci│ ├── load_policy 24c1ed15f1Sopenharmony_ci│ └── restorecon 25c1ed15f1Sopenharmony_ci├── scripts # Security policy compilation scripts. 26c1ed15f1Sopenharmony_ci├── sepolicy # Security policy files. 27c1ed15f1Sopenharmony_ci└── test # Test program. 28c1ed15f1Sopenharmony_ci``` 29c1ed15f1Sopenharmony_ci 30c1ed15f1Sopenharmony_ci## Constraints 31c1ed15f1Sopenharmony_ci 32c1ed15f1Sopenharmony_ciCurrently, SELinux supports only the RK3568 device. 33c1ed15f1Sopenharmony_ci 34c1ed15f1Sopenharmony_ci## Usage 35c1ed15f1Sopenharmony_ci 36c1ed15f1Sopenharmony_ci### Building the Image 37c1ed15f1Sopenharmony_ci 38c1ed15f1Sopenharmony_ciRun the following command to build the image that supports SELinux: 39c1ed15f1Sopenharmony_ci 40c1ed15f1Sopenharmony_ci``` 41c1ed15f1Sopenharmony_ciIndependent build command of this module: 42c1ed15f1Sopenharmony_ci./build.sh --product-name=rk3568 -T selinux_adapter --ccache 43c1ed15f1Sopenharmony_ci``` 44c1ed15f1Sopenharmony_ci### Verifying Basic Functions 45c1ed15f1Sopenharmony_ci 46c1ed15f1Sopenharmony_ciBurn the image to the development board, start the board, run **shell** through the serial port, and run the following commands: 47c1ed15f1Sopenharmony_ci 48c1ed15f1Sopenharmony_ci``` 49c1ed15f1Sopenharmony_cils -lZ / # View the file label. 50c1ed15f1Sopenharmony_cils -lLZ / # View the link source file label. 51c1ed15f1Sopenharmony_cips -eZ # View the process label. 52c1ed15f1Sopenharmony_cisetenforce 1 # Enable the enforcing mode. 53c1ed15f1Sopenharmony_cisetenforce 0 # Enable the permissive mode, which is the default mode. 54c1ed15f1Sopenharmony_cigetenforce # Obtain the SELinux working mode. 55c1ed15f1Sopenharmony_ci``` 56c1ed15f1Sopenharmony_ciPolicy file: **/etc/selinux/targeted/policy/policy.31** 57c1ed15f1Sopenharmony_ci 58c1ed15f1Sopenharmony_ciFile labeling rule: **/etc/selinux/targeted/policy/file_contexts** 59c1ed15f1Sopenharmony_ci 60c1ed15f1Sopenharmony_ciSELinux mode switch: **/etc/selinux/config** 61c1ed15f1Sopenharmony_ci 62c1ed15f1Sopenharmony_ciDuring the verification, you can replace the preceding files separately. 63c1ed15f1Sopenharmony_ci 64c1ed15f1Sopenharmony_ci### Log 65c1ed15f1Sopenharmony_ci 66c1ed15f1Sopenharmony_ci``` 67c1ed15f1Sopenharmony_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 68c1ed15f1Sopenharmony_ci 69c1ed15f1Sopenharmony_ciThe log information is interpreted as follows: 70c1ed15f1Sopenharmony_ciopen # The operation is open. 71c1ed15f1Sopenharmony_cipid=1658 # The process ID is 1658. 72c1ed15f1Sopenharmony_cicomm="setenforce" # The process name is setenforce. 73c1ed15f1Sopenharmony_cipath="/sys/fs/selinux/enforce" # The path accessed by the process is /sys/fs/selinux/enforce. 74c1ed15f1Sopenharmony_cidev="selinuxfs" # The file accessed belongs to the SELinux filesystem (selinuxfs). 75c1ed15f1Sopenharmony_ciino=4 # The file node No. is 4. 76c1ed15f1Sopenharmony_ciscontext=u:r:hdcd:s0 # The SELinux label of the process is u:r:hdcd:s0. 77c1ed15f1Sopenharmony_citcontext=u:object_r:selinuxfs:s0 # The SELinux label of the accessed file is u:object_r:selinuxfs:s0. 78c1ed15f1Sopenharmony_citclass=file # The current alarm is about a file operation. 79c1ed15f1Sopenharmony_cipermissive=1 # The SELinux is running in permissive mode, that is, the system does not deny any operation but only logs Access Vector Cache (AVC) message for troubleshooting or debugging. If permissive is set to 0, the SELinux is running in enforcing mode and denies access based on SELinux policy rules. 80c1ed15f1Sopenharmony_ci``` 81c1ed15f1Sopenharmony_ci 82c1ed15f1Sopenharmony_ci### Writing a Policy Rule 83c1ed15f1Sopenharmony_ci 84c1ed15f1Sopenharmony_ci``` 85c1ed15f1Sopenharmony_ciObtain the access information based on the AVC message. 86c1ed15f1Sopenharmony_ciExample: 87c1ed15f1Sopenharmony_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 88c1ed15f1Sopenharmony_ciThe rule is as follows: 89c1ed15f1Sopenharmony_ciallow hdcd selinuxfs:file open; 90c1ed15f1Sopenharmony_ci``` 91c1ed15f1Sopenharmony_ci 92c1ed15f1Sopenharmony_ci## Repositories 93c1ed15f1Sopenharmony_ci 94c1ed15f1Sopenharmony_ciThe table below lists the repositories involved. 95c1ed15f1Sopenharmony_ci 96c1ed15f1Sopenharmony_ci| Repository| Source Code| Description| 97c1ed15f1Sopenharmony_ci| --- | --- | --- | 98c1ed15f1Sopenharmony_ci| [selinux_adapter](https://gitee.com/openharmony/security_selinux_adapter.git) | `base/security/selinux_adapter/` | Provides policies and self-developed APIs.| 99c1ed15f1Sopenharmony_ci| [third_party_selinux](https://gitee.com/openharmony/third_party_selinux.git) | `third_party/selinux/` | SELinux main repository.| 100c1ed15f1Sopenharmony_ci| [productdefine_common](https://gitee.com/openharmony/productdefine_common.git) | `productdefine/common/` | Provides SELinux component definitions.| 101c1ed15f1Sopenharmony_ci| [third_party_toybox](https://gitee.com/openharmony/third_party_toybox.git) | `third_party/toybox/` | Provides the support for SELinux of `ls`.| 102c1ed15f1Sopenharmony_ci| [startup_init_lite](https://gitee.com/openharmony/startup_init_lite.git) | `base/startup/init_lite/` | Provides the init_lite module, which starts the first application.| 103c1ed15f1Sopenharmony_ci| [third_party_FreeBSD](https://gitee.com/openharmony/third_party_FreeBSD.git) | `third_party/FreeBSD/` | Provides the fts library.| 104c1ed15f1Sopenharmony_ci| [third_party_pcre](https://gitee.com/openharmony/third_party_pcre2.git) | `third_party/pcre/` | Provides the pcre2 library.| 105c1ed15f1Sopenharmony_ci| [build](https://gitee.com/openharmony/build.git) | `build/` | Provides the code for build.| 106c1ed15f1Sopenharmony_ci 107