Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

SELinux 入門:概念、狀態切換與 Apache 三個實例

SELinux 是什麼

SELinux(Security Enhanced Linux)是實作於內核中的 MAC(Mandatory Access Control,強制存取控制)機制,目的在明確規範「哪個行程可以存取哪些資源」,例如檔案與網路連接埠。它的價值在於提升系統抵禦 0-Day 攻擊的能力,因此並不是防火牆或 ACL 的替代品,用途也不重疊。

DAC 與 MAC 的差別

一般 Linux 採用 DAC(自主存取控制),行程本身幾乎不受約束,能做的事取決於執行者的權限。SELinux 則是 MAC:

  • 規則決定控制的嚴格程度,行程可以被限制
  • 政策定義被限制的行程能使用哪些檔案與連接埠
  • 預設策略是「沒有明確允許就拒絕」

通俗來講,SELinux 為特定程式準備了一個沙箱,並替檔案與行程貼上標籤。主體(subject,即行程)對物件(object,即檔案、目錄、連接埠等)的每一次操作,都由內核中的策略執行伺服器查 AVC(Access Vector Cache)決定放行或拒絕。所有檔案都有 type 標籤,所有行程都有 domain 標籤,domain 能做什麼由策略定義。

一、查詢目前狀態

getenforce

三種回傳值:Disabled 已停用、Permissive 只記錄警告不阻擋、Enforcing 記錄並阻擋。

二、切換狀態

setenforce [ 1 | 0 ]

1 為 Enforcing,0 為 Permissive。兩者互切不需重開機;但涉及 Disabled 的切換必須重啟,且重啟時會為整個檔案系統重建安全標籤:

touch /.autorelabel && reboot

永久設定要改 /etc/sysconfig/selinux。特別注意:只要 grub 參數或設定檔中有一處寫成 disabled,SELinux 就不會啟動。

三、運行模式

  • strict:CentOS 5,所有行程都受管制
  • targeted:只保護常見網路服務(RHEL 4 為 13 個,RHEL 5 為 88 個),是預設值
  • minimum:CentOS 7 上修改過的 targeted
  • mls:提供多級安全機制

minimum 與 mls 穩定性不足,實務上少用,一般維持 targeted 即可。

四、安全標籤

查行程標籤:

ps auxZ | grep lldpad

查檔案標籤(有 . 就代表有 SELinux 標籤):

ls -Z /usr/lib/xulrunner-2/libmozjs.so
-rwxr-xr-x. root root system_u:object_r:lib_t:s0 ...

標籤有多段,重點是第三段 type,策略正是以 type 為基礎運作,多個服務可共用如 public_content_t。

手動改標籤與還原預設:

chcon [-t TYPE] FILE...
chcon [-R] --reference=RFILE FILE...
restorecon [-R] /path/to/somewhere

五、安全上下文管理(semanage)

semanage 來自 policycoreutils-python 套件,CentOS 6 預設未安裝。

semanage fcontext -l                                     # 查看
semanage fcontext -a -t httpd_sys_content_t '/testdir(/.*)?'   # 新增
restorecon -Rv /testdir                                  # 套用
semanage fcontext -d -t httpd_sys_content_t '/testdir(/.*)?'   # 刪除

六、連接埠標籤

semanage port -l
semanage port -a -t http_port_t -p tcp 9527     # 新增
semanage port -m -t http_port_t -p tcp 9527     # 修改
semanage port -d -t http_port_t -p tcp 9527     # 刪除

七、布林值規則

getsebool [-a] [boolean]
semanage boolean -l | grep homedir
setsebool -P httpd_enable_homedirs on

-P 表示寫入政策,重開機後仍有效。

八、日誌排查

yum install setroubleshoot*
grep setroubleshoot /var/log/messages
sealert -a /var/log/audit/audit.log
sealert -l UUID

Apache 實例演練

實例一:把網站根目錄改到 /website

[root@~]# mkdir /website
[root@~]# ll -Z /website -d
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /website
[root@~]# ll -Z /var/www/html/
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html
[root@~]# chcon --reference=/var/www/html /website
[root@~]# vim /etc/httpd/conf/httpd.conf
    DocumentRoot "/website"
[root@~]# service httpd restart

關鍵是讓 /website 擁有 httpd_sys_content_t 標籤,httpd 才讀得到。用 --reference 直接抄一份既有目錄的上下文,比手打 type 更不容易出錯。

實例二:改用連接埠 9527

[root@~]# semanage port -l | grep http_port_t
   http_port_t  tcp  80, 81, 443, 488, 8008, 8009, 8443, 9000
[root@~]# semanage port -a -t http_port_t -p tcp 9527
[root@~]# vim /etc/httpd/conf/httpd.conf
    Listen 9527

只改 httpd.conf 是不夠的,SELinux 會擋下非標籤連接埠,必須同步用 semanage 把 9527 納入 http_port_t。

實例三:用布林值開放使用者家目錄

[root@~]# vim /etc/httpd/conf/httpd.conf
    # UserDir disabled
    UserDir public_html
[root@~]# chmod 711 /home/jay
[root@~]# mkdir /home/jay/public_html
[root@~]# echo "jay home" > /home/jay/public_html/index.html
[root@~]# semanage boolean -l | grep homedir
   httpd_enable_homedirs (off , off)  Allow httpd to read home directories
[root@~]# setsebool -P httpd_enable_homedirs on

這個案例說明:遇到 SELinux 阻擋時,先想「有沒有現成的布林值可以開」,通常比改標籤更乾淨。

延伸閱讀:

SELinux 安全上下文初探 https://www.linuxidc.com/Linux/2014-04/99508.htm

一次由 SELinux 引起的 SSH 公鑰認證失敗問題 https://www.linuxidc.com/Linux/2013-07/87267.htm

SELinux 入門教程 https://www.linuxidc.com/Linux/2013-04/82371.htm

CentOS/RHEL 下如何關閉 SELinux https://www.linuxidc.com/Linux/2011-05/36486.htm

SELinux 入門指南 https://www.linuxidc.com/Linux/2016-05/131162.htm

Copyright © Linux教程網 All Rights Reserved