• Home
Name
Date
Size
#Lines
LOC

..--

config/12-May-2024-3632

docs/images/12-May-2024-

interfaces/12-May-2024-3,1312,344

scripts/12-May-2024-478359

sepolicy/12-May-2024-28,95925,700

test/12-May-2024-1,327865

.gitignoreD12-May-2024309 4031

BUILD.gnD12-May-202415.4 KiB545508

LICENSED12-May-20249.9 KiB178150

OAT.xmlD12-May-20245.6 KiB8337

README-en.mdD12-May-20245.7 KiB11283

README.mdD12-May-20245 KiB11183

bundle.jsonD12-May-20242.4 KiB8888

selinux.gniD12-May-2024687 2118

README-en.md

1# security_selinux
2
3## Introduction
4
5Security-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.
6
7The flow of access control shown in following figure:
8
9![Overall architecture](docs/images/SELinux.png)
10
11## Directory Structure
12
13```
14.
15├── config                  # Third-party library configuration file of the board.
16├── docs                    # Documents.
17│   └── images
18├── interfaces
19│   ├── policycoreutils     # libload_policy.so and librestorecon.so of the board.
20│   │   ├── include
21│   │   └── src
22│   └── tools               # load_policy and restorecon of the board.
23│       ├── load_policy
24│       └── restorecon
25├── scripts                 # Security policy compilation scripts.
26├── sepolicy                # Security policy files.
27└── test                    # Test program.
28```
29
30## Constraints
31
32Currently, SELinux supports only the RK3568 device.
33
34## Usage
35
36### Compiling Source Code
37
381. Obtain the source code. For details, see [Obtaining Source Code](https://gitee.com/openharmony/docs/blob/master/en/device-dev/quick-start/quickstart-standard-sourcecode-acquire.md).
391. Compile the source code. For details, see [Building](https://gitee.com/openharmony/docs/blob/master/en/device-dev/quick-start/quickstart-standard-running-hi3516-build.md).
40
41### Building the Image
42
43Run the following command to build the image that supports SELinux:
44
45```
46Independent build command of this module:
47./build.sh --product-name=rk3568 -T selinux --ccache
48```
49### Verifying Basic Functions
50
51Burn the image to the development board, start the board, run **shell** through the serial port, and run the following commands:
52
53```
54ls -lZ /          # View the file label.
55ls -lLZ /         # View the link source file label.
56ps -eZ            # View the process label.
57setenforce 1      # Enable the enforcing mode.
58setenforce 0      # Enable the permissive mode, which is the default mode.
59getenforce        # Obtain the SELinux working mode.
60```
61Policy file: **/etc/selinux/targeted/policy/policy.31**
62
63File labeling rule: **/etc/selinux/targeted/policy/file_contexts**
64
65SELinux mode switch: **/etc/selinux/config**
66
67During the verification, you can replace the preceding files separately.
68
69### Log
70
71```
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
74The log information is interpreted as follows:
75open                                # The operation is open.
76pid=1658                            # The process ID is 1658.
77comm="setenforce"                   # The process name is setenforce.
78path="/sys/fs/selinux/enforce"      # The path accessed by the process is /sys/fs/selinux/enforce.
79dev="selinuxfs"                     # The file accessed belongs to the SELinux filesystem (selinuxfs).
80ino=4                               # The file node No. is 4.
81scontext=u:r:hdcd:s0                # The SELinux label of the process is u:r:hdcd:s0.
82tcontext=u:object_r:selinuxfs:s0    # The SELinux label of the accessed file is u:object_r:selinuxfs:s0.
83tclass=file                         # The current alarm is about a file operation.
84permissive=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.
85```
86
87### Writing a Policy Rule
88
89```
90Obtain the access information based on the AVC message.
91Example:
92audit: 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
93The rule is as follows:
94allow hdcd selinuxfs:file open;
95```
96
97## Repositories
98
99The table below lists the repositories involved.
100
101| Repository| Source Code| Description|
102| --- | --- | --- |
103| [security_selinux](https://gitee.com/openharmony/security_selinux.git) | `base/security/selinux/` | Provides policies and self-developed APIs.|
104| [third_party_selinux](https://gitee.com/openharmony/third_party_selinux.git) | `third_party/selinux/` | SELinux main repository.|
105| [productdefine_common](https://gitee.com/openharmony/productdefine_common.git) | `productdefine/common/` | Provides SELinux component definitions.|
106| [third_party_toybox](https://gitee.com/openharmony/third_party_toybox.git) | `third_party/toybox/` | Provides the support for SELinux of `ls`.|
107| [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.|
108| [third_party_FreeBSD](https://gitee.com/openharmony/third_party_FreeBSD.git) | `third_party/FreeBSD/` | Provides the fts library.|
109| [third_party_pcre](https://gitee.com/openharmony/third_party_pcre2.git) | `third_party/pcre/` | Provides the pcre2 library.|
110| [build](https://gitee.com/openharmony/build.git) | `build/` | Provides the code for build.|
111
112

README.md

1# security_selinux
2
3## 简介
4
5SELinux (安全增强式 Linux , Security-Enhanced Linux )是 Linux 历史上杰出的安全组件, 包含一组内核修改和用户空间工具,并提供了基于安全策略的强制访问控制机制( Mandatory Access Control , MAC )。SELinux 已经被添加到各种 Linux 发行版中。其软件架构力图将软件执行与安全策略设计分离。本部件负责对文件,属性,服务等系统资源提供强制访问控制保护。提供neverallow规则限制系统中的高危操作,减少系统安全风险。
6
7访问控制基本流程如下图所示:
8![整体架构](docs/images/update.png)
9
10## 目录
11
12```
13.
14├── config                  # 板侧    三方库配置文件
15├── docs                    #         文档资源
16│   └── images
17├── interfaces
18│   ├── policycoreutils     # 板侧    libload_policy.solibrestorecon.so
19│   │   ├── include
20│   │   └── src
21│   └── tools               # 板侧    load_policy、restorecon
22│       ├── load_policy
23│       └── restorecon
24├── scripts                 # 编译侧  策略编译脚本
25├── sepolicy                # 编译侧  策略文件
26└── test                    #         测试程序
27```
28
29## 约束
30
31目前Selinux只支持RK3568
32
33## 使用说明
34
35### 编译代码
36
371. 根据文档[《获取源码》](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/quick-start/quickstart-standard-sourcecode-acquire.md)获取主线代码。
381. 根据文档[《源码编译》](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/quick-start/quickstart-standard-running-hi3516-build.md)编译主线代码。
39
40### 编译镜像
41
42运行以下命令编译打包支持 SELinux 的镜像:
43
44```
45本模块单独编译命令
46./build.sh --product-name=rk3568 -T selinux --ccache
47```
48### 运行验证
49
50将镜像烧录到开发板上,开机,通过串口拿到 Shell ,在其中执行:
51
52```
53ls -lZ /         # 查看文件标签
54ls -lLZ /        # 查看link源文件标签
55ps -eZ           # 查看进程标签
56setenforce 1     # 使能selinux强制模式
57setenforce 0     # 是能selinux宽容模式,当前默认宽容模式
58getenforce       # 获取selinux工作模式
59```
60策略文件            /etc/selinux/targeted/policy/policy.31
61
62文件标签规则        /etc/selinux/targeted/policy/file_contexts
63
64selinux模式开关     /etc/selinux/config
65
66验证时,可单独替换上述文件。
67
68### 日志信息
69
70```
71audit: 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
72
73日志解读
74open                                #操作为open
75pid=1658                            #访问主体进程号为1658
76comm="setenforce"                   #访问主体进程名为setenforce
77path="/sys/fs/selinux/enforce"      #被访问客体为/sys/fs/selinux/enforce
78dev="selinuxfs"                     #被访问文件属于selinuxfs这一文件系统
79ino=4                               #文件节点编号为4
80scontext=u:r:hdcd:s0                #访问主体selinux标签为u:r:hdcd:s0
81tcontext=u:object_r:selinuxfs:s0   #被访问客体selinux标签为u:object_r:selinuxfs:s0
82tclass=file                         #当前告警属于file类型的操作
83permissive=1                        #当前selinux处于宽容模式,只告警不做访问拦截。强制模式时,做拦截, permissive=0
84```
85
86### 策略编写
87
88```
89根据avc告警,获取访问信息
90如:
91audit: 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
92对应规则为
93allow hdcd selinuxfs:file open;
94```
95
96## 相关仓
97
98涉及到的仓库有以下几个:
99
100| 仓库 | 源码目录 | 说明 |
101| --- | --- | --- |
102| [security_selinux](https://gitee.com/openharmony/security_selinux.git) | `base/security/selinux/` | 策略和一些自研接口 |
103| [third_party_selinux](https://gitee.com/openharmony/third_party_selinux.git) | `third_party/selinux/` | SELinux 的主仓库 |
104| [productdefine_common](https://gitee.com/openharmony/productdefine_common.git) | `productdefine/common/` | 添加 SELinux 组件定义 |
105| [third_party_toybox](https://gitee.com/openharmony/third_party_toybox.git) | `third_party/toybox/` | 完善了 `ls` 的 SELinux 支持 |
106| [startup_init_lite](https://gitee.com/openharmony/startup_init_lite.git) | `base/startup/init_lite/` | 系统启动加载策略并分化服务的标签 |
107| [third_party_FreeBSD](https://gitee.com/openharmony/third_party_FreeBSD.git) | `third_party/FreeBSD/` | 提供 fts 库 |
108| [third_party_pcre](https://gitee.com/openharmony/third_party_pcre2.git) | `third_party/pcre/` | 提供 pcre2 库 |
109| [build](https://gitee.com/openharmony/build.git) | `build/` | 编译控制 |
110
111