linux系統中 SElinux安全子系統

1、SElinux 是什麼?

SElinux(Security-Enhanced Linux)是美國國家安全局在linux開源社區的幫助下開發的一個強制訪問控制(Mandatory Access Control)的安全子系統。使用SElinux技術的目的是為了讓各個服務進程都受到約束,使其僅獲取到本應獲取的資源

 

SElinux能夠從多方面監控違法行為:對服務程式的功能進行限制SElinux域限制可以確保服務程式做不了出格的事情);對文件資源的訪問限制SElinux安全上下文確保文件資源只能被其所屬的服務程式進行訪問)。

 

2、SElinux服務有三種配置模式

  • enforcing:強制啟用安全策略模式,將攔截服務的不合法請求
  • permissinve:遇到服務越權訪問時,只發出警告而不強制攔截
  • disabled:對於越權的行為不警告也不攔截

 

3、 SElinux的配置文件為/etc/selinux/config, 默認的模式為enforcing。

[root@PC1linuxprobe /]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

 

4、為了驗證SElinux效果,首先查看 httpd服務的主配置文件,關注此時的網站數據目錄,及httpd服務程式的默認首頁

[root@PC1linuxprobe /]# vim /etc/httpd/conf/httpd.conf

 

 

查看網站數據目錄下文件:

[root@PC1linuxprobe /]# ls /home/wwwroot/
index.html
[root@PC1linuxprobe /]# cat /home/wwwroot/index.html
xxxxyyyyyyzzzz

 

查看此時httpd服務程式的首頁:

 

 

5、getenforce命令可以用來查看SElinux當前的運行模式,setenforce命令可以修改SElinux當前的運行模式(0為禁用,1未啟用,臨時生效)

[root@PC1linuxprobe /]# getenforce   ## 查看此時SElinux服務模式
Enforcing
[root@PC1linuxprobe /]# setenforce 0  ## 設為禁用
[root@PC1linuxprobe /]# getenforce
Permissive

 

看此時的httpd服務首頁:

 

 

6、解釋

 httpd服務程式的功能是允許用戶訪問網站內容,因此SElinux可定會默認放行用戶對網站的請求操作。但是,我們將網站數據的默認保存目錄修改為了/home/wwwroot,這就產生問題了。 /home目錄是用來存放普通用戶的家目錄數據的,而現在,httpd提供的網站服務卻要去獲取普通用戶家目錄中的數據了,這顯然違反了SElinux的監管原則。

 

7、擁有不同的SElinux安全上下文值?????

[root@PC1linuxprobe /]# setenforce 1
[root@PC1linuxprobe /]# ls -lZ /var/www/html/
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@PC1linuxprobe /]# ls -lZ /home/wwwroot/
-rw-r--r--. root root unconfined_u:object_r:home_root_t:s0 index.html