日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

SELinux基本概念及基本配置

發(fā)布時間:2024/1/23 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SELinux基本概念及基本配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SELinux從出現(xiàn)至今,已經(jīng)走過將近13年歷史,然而在Linux相關QQ技術群或者Linux相關論壇,經(jīng)常有人遇到問題問題都歸咎與 SELinux,如httpd各項配置都正常,但客戶就是無法訪問;又比如vsftpd配置均正常,但客戶端訪問提示無法轉換家目錄。于是乎很多人都 對SELinux有了極大的偏見,認為SELinux帶來的似乎只有麻煩,于是很多人選擇在安裝系統(tǒng)第一件事就是將SELinux設置為禁用。不過隨著日 益增長的 0-day 安全漏洞,SELinux在很多時候給了我們關鍵的保障。

什么是SELinux?

SELinux 全稱 Security Enhanced Linux (安全強化 Linux),是美國國家安全局2000年以 GNU GPL 發(fā)布,是 MAC (Mandatory Access Control,強制訪問控制系統(tǒng))的一個實現(xiàn),目的在于明確的指明某個進程可以訪問哪些資源(文件、網(wǎng)絡端口等)。強制訪問控制系統(tǒng) 的用途在于增強系統(tǒng)抵御 0-Day 攻擊(利用尚未公開的漏洞實現(xiàn)的攻擊行為)的能力。所以它不是網(wǎng)絡防火墻或 ACL 的替代品,在用途上也 不重復。在目前的大多數(shù)發(fā)行版中,已經(jīng)默認在內(nèi)核集成了SELinux。

舉例來說,系統(tǒng)上的 Apache 被發(fā)現(xiàn)存在一個漏洞,使得某遠程用戶可以訪問系統(tǒng)上的敏感文件(比如 /etc/passwd 來獲得系統(tǒng)已存在用戶) ,而修復該安全漏洞的 Apache 更新補丁尚未釋出。此時 SELinux 可以起到彌補該漏洞的緩和方案。因為 /etc/passwd 不具有 Apache 的 訪問標簽,所以 Apache 對于 /etc/passwd 的訪問會被 SELinux 阻止。 相比其他強制性訪問控制系統(tǒng),SELinux 有如下優(yōu)勢:

  • 控制策略是可查詢而非程序不可見的。
  • 可以熱更改策略而無需重啟或者停止服務。
  • 可以從進程初始化、繼承和程序執(zhí)行三個方面通過策略進行控制。
  • 控制范圍覆蓋文件系統(tǒng)、目錄、文件、文件啟動描述符、端口、消息接口和網(wǎng)絡接口。

那么 SELinux 對于系統(tǒng)性能有什么樣的影響呢?根據(jù) Phoronix 在 2009 年使用 Fedora 11 所做的橫向比較來看,開啟 SELinux 僅在少數(shù) 情況下導致系統(tǒng)性能約 5% 的降低。

SELinux 是不是會十分影響一般桌面應用及程序開發(fā)呢?原因是,因為 SELinux 的策略主要針對服務器環(huán)境。但隨著 SELinux 13年來的廣泛 應用,目前 SELinux 策略在一般桌面及程序開發(fā)環(huán)境下依然可以同時滿足安全性及便利性的要求。以剛剛發(fā)布的 Fedora 15 為例,筆者在 搭建完整的娛樂(包含多款第三方原生 Linux 游戲及 Wine 游戲)及開發(fā)環(huán)境(Android SDK + Eclipse)過程中,只有 Wine 程序的首次運行 時受到 SELinux 默認策略的阻攔,在圖形化的“SELinux 故障排除程序”幫助下,點擊一下按鈕就解決了。

了解和配置 SELinux

1. 獲取當前 SELinux 運行狀態(tài)

getenforce

可能返回結果有三種:Enforcing、Permissive 和 Disabled。Disabled 代表 SELinux 被禁用,Permissive 代表僅記錄安全警告但不阻止 可疑行為,Enforcing 代表記錄警告且阻止可疑行為。

目前常見發(fā)行版中,RHEL、CentOS、Fedora 等默認設置為 Enforcing,其余的如 openSUSE 等為 Permissive。

2. 改變 SELinux 運行狀態(tài)

setenforce [ Enforcing | Permissive | 1 | 0 ]

該命令可以立刻改變 SELinux 運行狀態(tài),在 Enforcing 和 Permissive 之間切換,結果保持至關機。一個典型的用途是看看到底是不是 SELinux 導致某個服務或者程序無法運行。若是在 setenforce 0 之后服務或者程序依然無法運行,那么就可以肯定不是 SELinux 導致的。

若是想要永久變更系統(tǒng) SELinux 運行環(huán)境,可以通過更改配置文件 /etc/selinux/config 實現(xiàn)。注意當從 Disabled 切換到 Permissive 或者 Enforcing 模式后需要重啟計算機并為整個文件系統(tǒng)重新創(chuàng)建安全標簽(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 的值實現(xiàn),該值有兩種可能: targeted 代表僅針對預制的幾種網(wǎng)絡服務和訪問請求使用 SELinux 保護,strict 代表所有網(wǎng)絡服務和訪問請求都要經(jīng)過 SELinux。

RHEL、CentOS、Fedora 等默認設置為 targeted,包含了對幾乎所有常見網(wǎng)絡服務的 SELinux 策略配置,已經(jīng)默認安裝并且可以無需修改直接使用。 若是想自己編輯 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服務的網(wǎng)頁目錄或文件的上 下文要為httpd_sys_content_t,否則客戶端無法訪問。

5.1使用chcon修改httpd目錄或文件安全上下文:

如nagios服務器的網(wǎng)頁目錄上下文默認為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服務,客戶端就已經(jīng)可以訪問。

5.2使用semanage工具,讓httpd支持非標準端口:

semanage工具非常強大,基本能實現(xiàn)所有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

根據(jù)提示,運行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。

根據(jù)提示,運行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端口已經(jīng)加入
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

重啟服務:

[root@web2 ~]# service httpd start
正在啟動 httpd: ? ? ? ? ? ?[確定]

5.3 修改selinux布爾值,允許創(chuàng)建私人網(wǎng)站

若是希望用戶可以通過在 ~/www/ 放置文件的方式創(chuàng)建自己的個人網(wǎng)站的話,那么需要在 Apache 策略中允許該操作執(zhí)行。使用:

[root@web2 ~]# setsebool httpd_enable_homedirs 1

默認情況下 setsebool 的設置只保留到下一次重啟之前,若是想永久生效的話,需要添加 -P 參數(shù),比如:

[root@web2 ~]# setsebool -P httpd_enable_homedirs 1

setsebool 是用來切換由布爾值控制的 SELinux 策略的,當前布爾值策略的狀態(tài)可以通過 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基本概念及基本配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。