SELinux基本概念及基本配置
SELinux從出現至今,已經走過將近13年歷史,然而在Linux相關QQ技術群或者Linux相關論壇,經常有人遇到問題問題都歸咎與 SELinux,如httpd各項配置都正常,但客戶就是無法訪問;又比如vsftpd配置均正常,但客戶端訪問提示無法轉換家目錄。于是乎很多人都 對SELinux有了極大的偏見,認為SELinux帶來的似乎只有麻煩,于是很多人選擇在安裝系統第一件事就是將SELinux設置為禁用。不過隨著日 益增長的 0-day 安全漏洞,SELinux在很多時候給了我們關鍵的保障。
什么是SELinux?
SELinux 全稱 Security Enhanced Linux (安全強化 Linux),是美國國家安全局2000年以 GNU GPL 發布,是 MAC (Mandatory Access Control,強制訪問控制系統)的一個實現,目的在于明確的指明某個進程可以訪問哪些資源(文件、網絡端口等)。強制訪問控制系統 的用途在于增強系統抵御 0-Day 攻擊(利用尚未公開的漏洞實現的攻擊行為)的能力。所以它不是網絡防火墻或 ACL 的替代品,在用途上也 不重復。在目前的大多數發行版中,已經默認在內核集成了SELinux。
舉例來說,系統上的 Apache 被發現存在一個漏洞,使得某遠程用戶可以訪問系統上的敏感文件(比如 /etc/passwd 來獲得系統已存在用戶) ,而修復該安全漏洞的 Apache 更新補丁尚未釋出。此時 SELinux 可以起到彌補該漏洞的緩和方案。因為 /etc/passwd 不具有 Apache 的 訪問標簽,所以 Apache 對于 /etc/passwd 的訪問會被 SELinux 阻止。 相比其他強制性訪問控制系統,SELinux 有如下優勢:
- 控制策略是可查詢而非程序不可見的。
- 可以熱更改策略而無需重啟或者停止服務。
- 可以從進程初始化、繼承和程序執行三個方面通過策略進行控制。
- 控制范圍覆蓋文件系統、目錄、文件、文件啟動描述符、端口、消息接口和網絡接口。
那么 SELinux 對于系統性能有什么樣的影響呢?根據 Phoronix 在 2009 年使用 Fedora 11 所做的橫向比較來看,開啟 SELinux 僅在少數 情況下導致系統性能約 5% 的降低。
SELinux 是不是會十分影響一般桌面應用及程序開發呢?原因是,因為 SELinux 的策略主要針對服務器環境。但隨著 SELinux 13年來的廣泛 應用,目前 SELinux 策略在一般桌面及程序開發環境下依然可以同時滿足安全性及便利性的要求。以剛剛發布的 Fedora 15 為例,筆者在 搭建完整的娛樂(包含多款第三方原生 Linux 游戲及 Wine 游戲)及開發環境(Android SDK + Eclipse)過程中,只有 Wine 程序的首次運行 時受到 SELinux 默認策略的阻攔,在圖形化的“SELinux 故障排除程序”幫助下,點擊一下按鈕就解決了。
了解和配置 SELinux
1. 獲取當前 SELinux 運行狀態
getenforce
可能返回結果有三種:Enforcing、Permissive 和 Disabled。Disabled 代表 SELinux 被禁用,Permissive 代表僅記錄安全警告但不阻止 可疑行為,Enforcing 代表記錄警告且阻止可疑行為。
目前常見發行版中,RHEL、CentOS、Fedora 等默認設置為 Enforcing,其余的如 openSUSE 等為 Permissive。
2. 改變 SELinux 運行狀態
setenforce [ Enforcing | Permissive | 1 | 0 ]
該命令可以立刻改變 SELinux 運行狀態,在 Enforcing 和 Permissive 之間切換,結果保持至關機。一個典型的用途是看看到底是不是 SELinux 導致某個服務或者程序無法運行。若是在 setenforce 0 之后服務或者程序依然無法運行,那么就可以肯定不是 SELinux 導致的。
若是想要永久變更系統 SELinux 運行環境,可以通過更改配置文件 /etc/selinux/config 實現。注意當從 Disabled 切換到 Permissive 或者 Enforcing 模式后需要重啟計算機并為整個文件系統重新創建安全標簽(touch /.autorelabel && reboot)。
?[root@web2 ~]# vim /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,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
3. SELinux 運行策略
配置文件 /etc/selinux/config 還包含了 SELinux 運行策略的信息,通過改變變量 SELINUXTYPE 的值實現,該值有兩種可能: targeted 代表僅針對預制的幾種網絡服務和訪問請求使用 SELinux 保護,strict 代表所有網絡服務和訪問請求都要經過 SELinux。
RHEL、CentOS、Fedora 等默認設置為 targeted,包含了對幾乎所有常見網絡服務的 SELinux 策略配置,已經默認安裝并且可以無需修改直接使用。 若是想自己編輯 SELinux 策略,也提供了命令行下的策略編輯器 seedit 以及 Eclipse 下的編輯插件 eclipse-slide 。
4. coreutils 工具的 SELinux 模式
常見的屬于 coreutils 的工具如 ps、ls 等等,可以通過增加 Z 選項的方式獲知 SELinux 方面的信息。
4.1使用ps獲取:
[barlow@web1 ~]$ ps -auxZ |grep httpd |head -5
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ unconfined_u:system_r:httpd_t:s0 apache 1393 0.0 0.2 279648 2272 ? S Jun27 0:01 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 1395 0.0 1.5 272964 15528 ? S Jun27 0:02 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 1399 0.0 1.7 272964 17828 ? S Jun27 0:02 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 1405 0.0 1.2 272964 12732 ? S Jun27 0:02 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 1409 0.0 1.4 272968 14784 ? S Jun27 0:03 /usr/sbin/httpd
4.2使用ls獲取
[barlow@web1 ~]$ ls -Z /var/www/ drwxrwxrwx. apache barlow unconfined_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin drwxrwxrwx. apache barlow unconfined_u:object_r:httpd_sys_content_t:s0 error drwxrwxrwx. apache barlow unconfined_u:object_r:httpd_sys_content_t:s0 html drwxrwxrwx. apache barlow unconfined_u:object_r:httpd_sys_content_t:s0 icons drwxrwxrwx. apache barlow system_u:object_r:httpd_sys_content_t:s0 lost+found
以此類推,Z 選項可以應用在幾乎全部 coreutils 工具里。
5、常用修改有關httpd服務的SELinux策略方法:
如上,ls -Z方法查詢到的文件SELinux上下文跟默認要求的不匹配,則服務無法正常使用,如SELinux要求httpd服務的網頁目錄或文件的上 下文要為httpd_sys_content_t,否則客戶端無法訪問。
5.1使用chcon修改httpd目錄或文件安全上下文:
如nagios服務器的網頁目錄上下文默認為unconfined_u:object_r:usr_t:s0,則客戶端無法訪問:
[root@nagios ~]# ll -Z /usr/local/nagios/share/
-rwxrwxr-x. nagios apache system_u:object_r:usr_t:s0 config.inc.php
drwxrwxr-x. nagios apache unconfined_u:object_r:usr_t:s0 contexthelp
drwxrwxr-x. nagios apache unconfined_u:object_r:usr_t:s0 docs
drwxrwxr-x. nagios apache unconfined_u:object_r:usr_t:s0 images
drwxrwxr-x. nagios apache unconfined_u:object_r:usr_t:s0 includes
-rwxrwxr-x. nagios apache system_u:object_r:usr_t:s0 index.html
-rwxrwxr-x. nagios apache system_u:object_r:usr_t:s0 index.php
……以下略……
?
使用chcon修改/usr/local/nagios/share/目錄及其下所有文件安全上下文為unconfined_u:object_r:httpd_sys_content_t
[root@nagios ~]# chcon -R unconfined_u:object_r:httpd_sys_content_t:s0 /usr/local/nagios/share/
查詢結果:
[root@nagios ~]# ls -Z /usr/local/nagios/share/
-rwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 config.inc.php
drwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 contexthelp
drwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 docs
drwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 images
drwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 includes
-rwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 index.html
-rwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 index.php
drwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 locale
-rwxrwxr-x. nagios apache unconfined_u:object_r:httpd_sys_content_t:s0 main.html
不用重啟httpd服務,客戶端就已經可以訪問。
5.2使用semanage工具,讓httpd支持非標準端口:
semanage工具非常強大,基本能實現所有SELinux配置,但很多時候我們并不知道SELinux錯在哪里,在圖形界面下有圖形化的分析工具,在 終端界面下也有一個功能非常強大的分析工具sealert,但默認情況下,這兩個工具都沒有被安裝,需要先安裝semanage和sealert工具:
[root@web2 ~]# yum -y install policycoreutils-python setroubleshoot
注:semanage的使用也可以參見我的另外一篇博文:Selinux管理工具semanage。
默認情況下 Apache 只偵聽 80、443等幾個端口,若是直接指定其偵聽 808 端口的話,會在 service httpd restart 的時候報錯:
[root@web2 ~]# service httpd start
正在啟動 httpd:(13)Permission denied: make_sock: could not bind to address 0.0.0.0:808
no listening sockets available, shutting down
Unable to open logs ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??[失敗]
?
查看/var/log/messages 文件,可以看到如下這樣的錯誤:
[root@web2 ~]# tail /var/log/messages
Jun 29 10:30:51 web2 setroubleshoot: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket . For complete SELinux messages. run sealert -l 2ad073a4-7c64-4175-93ff-9d78f75133af
根據提示,運行sealert -l 2ad073a4-7c64-4175-93ff-9d78f75133af,生成SELinux報告如下:
[root@web2 ~]# sealert -l 2ad073a4-7c64-4175-93ff-9d78f75133af
# semanage port -a -t PORT_TYPE -p tcp 808
其中 PORT_TYPE 是以下之一:ntop_port_t, http_cache_port_t, http_port_t, puppet_port_t, jboss_messaging_port_t, jboss_management_port_t。
根據提示,運行semanage port -a -t PORT_TYPE -p tcp 808,此處需將PORT_TYPE替換為?http_port_t
[root@web2 ~]# semanage port -a -t http_port_t -p tcp 808
查詢結果:
[root@web2 ~]# semanage port -l|grep http
http_cache_port_t tcp 3128, 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 808, 80, 443, 488, 8008, 8009, 8443 ? ##可以看到808端口已經加入
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
重啟服務:
[root@web2 ~]# service httpd start
正在啟動 httpd: ? ? ? ? ? ?[確定]
5.3 修改selinux布爾值,允許創建私人網站
若是希望用戶可以通過在 ~/www/ 放置文件的方式創建自己的個人網站的話,那么需要在 Apache 策略中允許該操作執行。使用:
[root@web2 ~]# setsebool httpd_enable_homedirs 1
默認情況下 setsebool 的設置只保留到下一次重啟之前,若是想永久生效的話,需要添加 -P 參數,比如:
[root@web2 ~]# setsebool -P httpd_enable_homedirs 1
setsebool 是用來切換由布爾值控制的 SELinux 策略的,當前布爾值策略的狀態可以通過 getsebool 來獲知。 查看與httpd相關的布爾值:
[root@web2 ~]# getsebool -a |grep http
allow_httpd_anon_write --> off
allow_httpd_mod_auth_ntlm_winbind --> off
allow_httpd_mod_auth_pam --> off
allow_httpd_sys_script_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> on
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> on
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> on
httpd_execmem --> off
httpd_manage_ipa --> off
httpd_read_user_content --> off
httpd_run_stickshift --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_tmp_exec --> off
httpd_tty_comm --> on
httpd_unified --> on
httpd_use_cifs --> off
httpd_use_gpg --> off
httpd_use_nfs --> on
httpd_use_openstack --> off
httpd_verify_dns --> off
named_bind_http_port --> off
以上列舉了httpd服務常見selinux配置,其他服務(如vsftpd)也可以采用相同的辦法處理。
總結
以上是生活随笔為你收集整理的SELinux基本概念及基本配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux生成免密码登录
- 下一篇: scala中akka actor例子