SELinux詳解之第一章——基本介紹

SELinux是什麼?

SELinux全稱Security Enhanced Linux(安全性增強Linux),意在現有的Linux系統中額外增加一層安全控制層面,主要就是在解決某某應用是否可訪問某某文件的問題。SELinux通常是和Linux自帶的DAC(目錄權限控制)(也就是平常ls後看到的rwx之類的)一起工作。SELinux實現了MAC(強制訪問控制),即爲系統中每個文件和進程打上標籤SELinux上下文標籤,在進程對文件進行訪問時,對標籤進行檢測,查看是否有可以訪問。

使用SELinux的好處

  • 所有進程和文件均會被SELinuxSELinux策略規則定義了進程與文件的交互方式以及進程之間的交互方式。僅當存在專門允許訪問的SELinux策略規則時,才允許訪問。
  • 提供細粒度的訪問控制。 SELinux訪問決策超越了由用戶自行決定並基於Linux用戶和組ID控制的傳統UNIX權限,它基於所有可用信息,例如SELinux用戶,角色,類型以及(可選)安全級別。
  • SELinux策略是在整個系統範圍內由管理人員定義和實施的。
  • 改進了緩解特權升級攻擊的方法。進程在域中運行,因此彼此分開。
  • SELinux策略規則定義了進程如何訪問文件和其他進程。如果某個進程受到威脅,則攻擊者只能訪問該進程的正常功能,並且可以訪問該進程已配置爲有權訪問的文件。例如,如果Apache HTTP Server受感染,除非添加或配置了特定的SELinux策略規則或允許這種訪問,否則攻擊者無法使用該過程讀取用戶主目錄中的文件。
  • SELinux可用於強制執行數據機密性和完整性,並保護進程免受不受信任的輸入。

注意:SELinux並不是防病毒軟件,密碼管理器,防火牆和其他安全系統的替代品。SELinux旨在增強現有的安全解決方案,而不是替代它們。即使在運行SELinux時,也必須繼續遵循良好的安全慣例,例如使軟件保持最新,使用難以猜測的密碼或防火牆規則。

初步瞭解SELinux

以下操作以Centos 7爲例,其他發行版Linux命令基本一致,只需注意包管理器的安裝命令即可

啓用SELinux

要使用SELinux,首先我們需要確保其是可用的,使用如下命令查看

[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          error (Success)
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31
# 如果沒在運行我們需要設置爲Permissive模式,然後重啓
[root@localhost ~]# setenforce 0
Permissive

爲了避免以下的操作出現,命令找不到的情況,我們先安裝以下幾個軟件包:

[root@localhost ~]# yum install -y setools selinux-policy-devel setroubleshoot-server

然後我們需要確保 auditd and rsyslog守護進程是運行的。

[root@localhost ~]# systemctl enable auditd.service&&systemctl enable auditd.service

這兩個守護進程是負責記錄SELinux在運行過程中文件訪問的AVC(Access Vector Cache)(其實就是被攔截的操作)信息的,攔截的信息一般會被記錄在兩個文件中。

  • /var/log/audit/audit.log,記錄格式如下:
type=AVC msg=audit(1223024155.684:49): avc: denied { getattr } for pid=2000 comm="httpd"
path="/var/www/html/file1" dev=dm-0 ino=399185 scontext=unconfined_u:system_r:httpd_t:s0
tcontext=system_u:object_r:samba_share_t:s0 tclass=file
  • /var/log/message,記錄格式如下:
May 7 18:55:56 localhost setroubleshoot: SELinux is preventing httpd (httpd_t) "getattr" to
/var/www/html/file1 (samba_share_t). For complete SELinux messages. run sealert -l de7e30d6-
5488-466d-a606-92c9f40d316d

關於這個訪問攔截控制,是先進行DAC攔截,再進行MAC攔截的。也就是說進程訪問一個文件時,會先判斷是否具有讀寫權限,然後再判斷是否SELinux上下文中的域是否符合訪問規則。

SELinux的配置詳情

SELinux的配置文件爲/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=disable
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

配置文件很簡單就只有兩個配置項:

  • 運行模式
    • enforcing - 啓用SELinux規則,對不符合訪問策略的行爲進行阻止
    • permissive - 啓用SELinux規則但是對被攔截的操作只進行記錄不阻止進程的訪問.
    • disabled - 不啓用SELinux規則.
  • 安全控制類型
    • targeted - 只對指定目標進行保護
    • minimum -自定義的目標保護規則.
    • mls - 多級安全保護規則,使用這個類型需要安裝selinux-policy-mls纔可使用,這個我會單獨出一篇文章來介紹.

我們可以通過setenforce命令來設置運行模式,重啓後生效。

# 0-permissive,1-enforcing,如需禁用需要修改配置文件
setenforce 0 

注意:請勿在不清楚規則的情況下,啓用enforcing和MLS運行策略,可能會導致系統無法啓動。請勿在生產環境下,進行試驗,在進行策略更改之前請務必對系統進行快照備份,以免出現不可恢復的情況。

SELinux上下文標識

到這裏呢,我們將逐漸進入正題。如何查看上下文標識,如何修改標識,如何查看配置的規則等等,將是這一節的關注點。

在確保按上一節的操作配置SELinux之後,我們將來看看如何查看文件或進程的上下文標識:

# 我們需要先切換至root用戶再進行如下操作:
# 查看文件的上下文標識
[root@localhost /]# ls -Z
lrwxrwxrwx. root root         system_u:object_r:bin_t:s0       bin -> usr/bin
dr-xr-xr-x. root root         system_u:object_r:boot_t:s0      boot
drwxr-xr-x. root root         system_u:object_r:device_t:s0    dev
drwxr-xr-x. sssd pulse-access system_u:object_r:root_t:s0      elk
drwxr-xr-x. root root         system_u:object_r:etc_t:s0       etc
drwxr-xr-x. root root         system_u:object_r:home_root_t:s0 home
lrwxrwxrwx. root root
......
# 查看進行的上下文標識
[root@localhost /]# ps -Z
LABEL                              PID TTY          TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 11360 pts/0 00:00:00 shell
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 11934 pts/0 00:00:00 ps

上下文標識的組成

我們以system_u:object_r:bin_t:s0爲例,通常一個上下文標識的由如下幾部分組成:

user:role:type:level
  • user - 這是一個SELinux的內部用戶對象,每個Linux用戶都會映射到SELinux的內部用戶對象,這可以使Linux用戶可以繼承對SELinux用戶的限制,用如下命令即可查看映射關係:
[root@localhost /]# semanage login -l

登錄名                  SELinux 用戶           MLS/MCS 範圍           服務

__default__          unconfined_u         s0-s0:c0.c1023       *
root                 unconfined_u         s0-s0:c0.c1023       *
system_u             system_u             s0-s0:c0.c1023       *
# 查看當前用戶的上下文標識
[root@localhost /]# id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

默認的新建用戶在未使用useradd -Z參數指定SELinux 用戶時,默認映射到__default__新用戶名上,最終也是映射到unconfined_u

  • role - 這是基於角色的訪問控制(RBAC)安全模型。該角色是RBAC的屬性。 SELinux用戶被授權使用角色,角色被授權用於域。該角色充當域和SELinux用戶之間的中介。利用role綁定到user上可以控制user的訪問權限。最終,它控制可以訪問的對象類型。
# SELinux內置了14個角色
[root@localhost /]# seinfo -r
Roles: 14
   auditadm_r 
   dbadm_r
   guest_r
   staff_r
   user_r
   logadm_r
   object_r
   secadm_r
   sysadm_r
   system_r
   webadm_r
   xguest_r
   nx_server_r
   unconfined_r

這裏對其中幾個角色進行一下說明:

  1. webadm_r - 只能管理與Apache HTTP Server相關的SELinux類型。
  2. dbadm_r - 只能管理與MariaDB數據庫和PostgreSQL數據庫管理系統有關的SELinux類型。
  3. logadm_r - 只能管理與syslog和auditlog進程相關的SELinux類型
  4. secadm_r - 只能管理SELinux
  5. auditadm_r - 只能管理與audit子系統相關的流程。

舉個例子,如果一個進程運行時被綁定的角色爲webadm_r,那麼這個進程在運行時就只能訪問域爲Apache HTTP Server相關的SELinux相關的文件,例如:

[root@localhost /]# ls -dZ /var/www/html
drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html

這個文件夾的域爲httpd_sys_content_t,那麼webadm_r是有權限訪問的,所以訪問該服務程序時是可以正常返回界面的,如果使用如下命令臨時修改了文件的域之後:

[root@localhost /]# chcon -R -t samba_share_t /var/www/html
# 可以使用如下命令恢復默認的域
[root@localhost /]# restorecon -R -v /var/www/html

再次訪問同一樣的網頁就會被拒絕。

  • type - 這個在官方文檔中被稱爲域,是SELinux中最重要的一個概念,不同域的進程和文件。如果沒有指定的規則指明是可以訪問的,那麼該進程對該文件的訪問將被拒絕。
  • level - 這個屬性只有在MLS/MCS的運行策略中才會用到這裏我們可以先不用管

上下文標識的修改

首先,需要注意的是SELinux默認採用的是上下文繼承的方式來生成標識,即新建的文件的標識一般與父目錄的保持一致。

臨時修改文件標識

[root@localhost /]# chcon -R -t (typename) (filepath)

其中-R是針對與目錄使用的,表示遞歸修改

恢復臨時修改文件原有標識

# -R是針對與目錄使用的,表示遞歸修改,-v 表示顯示恢復的記錄
[root@localhost /]# restorecon -R -v (filepath)

永久修改標識

# filepath可以使用正則表達式,-a表示新增一條記錄
#例如semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
[root@localhost /]# semanage fcontext -a -t (typename) (filepath)
# 新增的記錄可以使用如下命令查看
[root@localhost /]# semanage fcontext -C -l

新增的記錄被存儲在/etc/selinux/targeted/contexts/files/file_contexts.local

恢復永久修改文件的原有標識

# 先刪除新添記錄
[root@localhost /]# semanage fcontext -d (filepath)
# 恢復原有標識
# -R是針對與目錄使用的,表示遞歸修改,-v 表示顯示恢復的記錄
[root@localhost /]# restorecon -R -v (filepath)

NFS文件系統掛載

在啓用SELinux之後,如果不爲NFS文件系統指定相應的上下文系統可能會出現被拒絕的情況。下面我們就來看下如何爲新掛載的系統配置上下文標識:

# 覆蓋現有文件系統擴展屬性,或爲不支持擴展屬性的文件系統指定不同的默認上下文。
[root@localhost /]# mount server:/export /local/mount/point -o  context="system_u:object_r:httpd_sys_content_t:s0"

當使用上下文選項掛載文件系統時,禁止用戶和進程更改上下文。例如,在安裝了上下文選項的文件系統上運行chcon命令會導致“操作不受支持”錯誤

#配置默認上下文,與上述配置不同的是,這個只是用作爲未設置上下文的文件配置默認標識,上面的選項是用作覆蓋的
[root@localhost /]# mount /dev/sda2 /test/ -o defcontext="system_u:object_r:samba_share_t:s0"

從同一網絡位置掛載多個卷,使用不同上下文標識:

# 需要指定nosharecache參數,否則第二的卷將無法掛載
[root@localhost /]# mount server:/export/web /local/web -o nosharecache,context="system_u:object_r:httpd_sys_content_t:s0"
[root@localhost /]# mount server:/export/database /local/database -o nosharecache,context="system_u:object_r:mysqld_db_t:s0"

如果想要開機自動掛載的話,只需在/etc/fstab中配置即可,示例如下:

server:/export /local/mount/ nfs context="system_u:object_r:httpd_sys_content_t:s0" 0 0

文件位置改變對上下文標識的影響

文件複製

被複制的新生成的文件,將按照目標文件夾的上下文標識生成。

文件移動

移動文件,將不會改變文件現有的上下文標識,所以儘量使用文件複製,以便SELinux重新標識文件,避免出現某些不可訪問的情況出現。

文件標識檢查

使用如下命令可以檢查目錄下的文件,是否與默認上下文的標識是否一致,通常是某些文件訪問異常時,可以發現問題。

[root@localhost /]# matchpathcon -V /var/www/html/*
/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be
system_u:object_r:httpd_sys_content_t:s0
/var/www/html/file2 verified.
/var/www/html/file3 verified.

發現不吻合的上下文標識後,可以使用restorecon進行恢復

文件的打包和解包

下面以tar爲例,默認程序是不會保存上下文信息的,除非加上--selinux選項,如果需要解壓之後,保持與當前上下文一致需要使用如下語句:

[root@localhost /]# tar --selinux -cf test.tar file{1,2,3}
#恢復至當前默認上下文
[root@localhost /]# tar -xvf archive.tar | restorecon -f -

SELinux信息查詢命令

avcstat

該程序的統計信息來源於/sys/fs/selinux/avc/cache_stats,使用如下:

# 每兩秒查詢一次AVC信息
[root@localhost /]# avcstat 2
lookups       hits     misses     allocs   reclaims      frees
7387467    7360550      26917      27232      26272      26733
1510       1508          2          2          0          0
4196       4196          0          0          0          0

seinfo

查看關於SELinux的概況:

[root@localhost /]# seinfo

Statistics for policy file: /sys/fs/selinux/policy
Policy Version & Type: v.31 (binary, mls)

   Classes:           130    Permissions:       272
   Sensitivities:       1    Categories:       1024
   Types:            4796    Attributes:        255
   Users:               8    Roles:              14
   Booleans:          318    Cond. Expr.:       367
   Allow:          108418    Neverallow:          0
   Auditallow:        158    Dontaudit:       10073
   Type_trans:      18172    Type_change:        74
   Type_member:        35    Role allow:         37
   Role_trans:        415    Range_trans:      5899
   Constraints:       143    Validatetrans:       0
   Initial SIDs:       27    Fs_use:             32
   Genfscon:          103    Portcon:           614
   Netifcon:            0    Nodecon:             0
   Permissives:         0    Polcap:              5
[root@localhost /]# seinfo -u

Users: 8
   sysadm_u
   system_u
   xguest_u
   root
   guest_u
   staff_u
   user_u
   unconfined_u

sesearch

在策略中搜索特定規則、搜索策略源文件或二進制文件。

# 查詢能訪問httpd_sys_content_t的角色 
[root@localhost /]# sesearch --role_allow -t httpd_sys_content_t
Found 37 role allow rules:
   allow system_r xguest_r;
   allow webadm_r system_r;
   allow system_r webadm_r;
   allow system_r user_r;
   allow system_r unconfined_r;
   allow system_r unconfined_r;
   allow system_r unconfined_r;
   allow unconfined_r system_r;
   allow sysadm_r user_r;
   allow sysadm_r staff_r;
   ....

更多工具可以看:SELinux-Tool Github

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章