精通SELinuxhtml
目錄
一、什麼是SELinux
二、SELinux的運行模式
三、安全性本文(Security Context)
四、SELinux的啓動、關閉與查看
五、SELinux網絡服務運行範例
六、SELinux日誌
七、SELinux的策略與規則管理 node
一、什麼是SELinux
SELinux(Security-Enhanced Linux) 是美國國家安全局(NSA)對於強制訪問控制的實現,是Linux歷史上最傑出的新安全子系統。NSA是在Linux社區的幫助下開發了一種訪問控制體系,在這種訪問控制體系的限制下,進程只能訪問那些在他的任務中所須要文件。SELinux默認安裝在Fedora和Red Hat Enterprise Linux上,也能夠做爲其餘發行版上容易安裝的包獲得。
SELinux是2.6版本的Linux內核中提供的強制訪問控制(MAC)系統。對於目前可用的 Linux安全模塊來講,SELinux 是功能最全面,並且測試最充分的,它是在 20 年的 MAC 研究基礎上創建的。SELinux在類型強制服務器中合併了多級安全性或一種可選的多類策略,並採用了基於角色的訪問控制概念。
大部分使用 SELinux 的人使用的都是SELinux就緒的發行版,例如 Fedora、Red Hat Enterprise Linux(RHEL)、Debian或 Centos。它們都是在內核中啓用SELinux的,而且提供一個可定製的安全策略,還提供不少用戶層的庫和工具,它們均可以使用 SELinux 的功能。
SELinux是一種基於 域-類型 模型(domain-type)的強制訪問控制(MAC)安全系統,它由NSA編寫並設計成內核模塊包含到內核中,相應的某些安全相關的應用也被打了SELinux的補丁,最後還有一個相應的安全策略。任何程序對其資源享有徹底的控制權。假設某個程序打算把含有潛在重要信息的文件扔到/tmp目錄下,那麼在DAC狀況下沒人能阻止他。SELinux提供了比傳統的UNIX權限更好的訪問控制linux
二、SELinux的運行模式
● 主體 (Subject):
SELinux 主要想要管理的就是程序,所以你能夠將『主體』跟 process 劃上等號;git
● 目標 (Object):
主體程序可否存取的『目標資源』通常就是文件系統。所以這個目標項目能夠等文件系統劃上等號;swift
● 策略 (Policy):
因爲程序與檔案數量龐大,所以 SELinux 會依據某些服務來制訂基本的存取安全性政策。這些政策內還會有詳細的規則 (rule) 來指定不一樣的服務開放某些資源的存取與否。在目前的 CentOS 6.x 裏面僅有提供兩個主要的政策以下,通常來講,使用預設的 target 政策便可。
targeted:針對網絡服務限制較多,針對本機限制較少,是預設的政策;
strict:完整的 SELinux 限制,限制方面較爲嚴格。瀏覽器
● 安全性本文 (security context):
咱們剛剛談到了主體、目標與政策面,可是主體能不能存取目標除了要符合政策指定以外,主體與目標的安全性本文必須一致纔可以順利存取。 這個安全性本文 (security context) 有點相似文件系統的 rwx 啦!安全性本文的內容與設定是很是重要的! 若是設定錯誤,你的某些服務(主體程序)就沒法存取文件系統(目標資源),固然就會一直出現「權限不符」的錯誤訊息了!
(1)主體程序必需要經過 SELinux 政策內的規則放行後,就能夠與目標資源進行安全性本文的比對。
(2)若比對失敗則沒法存取目標,若比對成功則能夠開始存取目標。安全
三、安全性本文(Security Context)
[root@localhost ~]# ll -Z
total 4
-rw-------. 1 root root system_u:object_r:admin_home_t:s0 1193 Feb 9 09:16 anaconda-ks.cfg服務器
● 身份標識 (Identify): 至關於帳號方面的身份識別!主要的身份識別則有底下三種常見的類型:
root:表示 root 的帳號身份,如同上面的表格顯示的是 root 家目錄下的數據!
system_u:表示系統程序方面的識別,一般就是程序;
user_u:表明的是通常使用者帳號相關的身份。網絡
● 角色 (Role): 透過角色字段,咱們能夠知道這個數據是屬於程序、檔案資源仍是表明使用者。通常的角色有:
object_r:表明的是檔案或目錄等檔案資源,這應該是最多見的;
system_r:表明的就是程序啦!不過,通常使用者也會被指定成爲 system_r !dom
● 類型 (Type): 在預設的 targeted 政策中, Identify 與 Role 字段基本上是不重要的!重要的在於這個類型 (type) 字段! 基本上,一個主體程序能不能讀取到這個檔案資源,與類型字段有關!而類型字段在檔案與程序的定義不太相同,分別是:
type:在檔案資源 (Object) 上面稱爲類型 (Type);
domain:在主體程序 (Subject) 則稱爲領域 (domain) 了!
domain 須要與 type 搭配,則該程序纔可以順利的讀取檔案資源啦!
四、SELinux的啓動、關閉與查看
SELinux主配置文件介紹:
[root@localhost ~]# cat /etc/selinux/config
SELINUX=enforcing
目前SELinux支持三種模式,分別以下:
● enforcing:強制模式:表明SELinux正在運行中,且已經正確開始限制domain/type了。
● permissive:寬容模式:表明SELinux正在運行中,不過僅會有警告信息並不會實際限制domain/type的訪問。這種模式能夠用來做爲SELinux的調試之用。
● disabled:關閉:SELinux並無實際運行。
SELINUXTYPE=targeted
查看SELinux:
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# sestatus [-vb]
參數:
-v:檢查列於/etc/sestatus.conf內的文件與程序的安全上下文內容;
-b:將目前策略的規則布爾值列出,及某些規則(rule)是否要啓動(0/1)之意;
[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: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 31
[root@localhost ~]# setenforce [0|1]
參數:
0:轉成permissive寬容模式;
1:轉成Enforcing強制模式;
啓動selinux:
配置文件中啓動selinux(須要重啓系統)
[root@localhost ~]# cat /etc/selinux/config
SELINUX=enforcing
命令行啓動selinux
[root@localhost ~]# setenforce 1
關閉selinux:
配置文件中關閉selinux(須要重啓系統)
[root@localhost ~]# cat /etc/selinux/config
SELINUX=disabled
命令行啓動selinux
[root@localhost ~]# setenforce 0
五、SELinux網絡服務運行範例
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "123456" > /var/www/html/index.html
[root@localhost ~]# systemctl restart httpd
此時你的瀏覽器會經過httpd這個進程擁有的httpd_t這個domain去讀取/var/www/html/index.html這個文件。
查看文件的權限和SELinux的安全上下文數據:
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 7 Feb 22 20:42 /var/www/html/index.html
建立錯誤的SELinux安全上下文:
[root@localhost ~]# echo "My httpd" > index.html
[root@localhost ~]# rm -rf /var/www/html/index.html
[root@localhost ~]# mv index.html /var/www/html/
查看index.html的權限:
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:admin_home_t:s0 9 Feb 22 20:50 /var/www/html/index.html
修改安全上下文:
chcon命令介紹(修改方式):
[root@localhost ~]# chcon [-R] [-t type] [-u user] [-r role] 文件
[root@localhost ~]# chcon [-R] --reference=範例文件 文件
參數:
-R:連同該目錄下的子目錄也同時修改;
-t:後面接安全上下文的類型字段!例如:httpd_sys_content_t;
-u:後面接身份識別,例如system_u;
-r:後面接角色,例如system_r;
--reference=範例文件:拿某個文件當範例來修改後續接的文件的類型!
範例一:以/etc/passwd爲依據,將index.html修改爲該類型
[root@localhost ~]# ll -Z /etc/passwd
-rw-r--r--. 1 root root system_u:object_r:passwd_file_t:s0 1133 Feb 22 20:42 /etc/passwd
[root@localhost ~]# chcon --reference=/etc/passwd /var/www/html/index.html
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root system_u:object_r:passwd_file_t:s0 9 Feb 22 20:50 /var/www/html/index.html
範例二:將剛纔的index.html類型改成httpd_sys_content_t的類型
[root@localhost ~]# chcon -t httpd_sys_content_t /var/www/html/index.html
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 9 Feb 22 20:50 /var/www/html/index.html
chcon是經過直接指定的方式來處理安全上下文的類型數據,咱們都知道系統默認的目錄都有特殊的SELinux安全上下文,而restorecon就是恢復模式的安全上下文的命令。
restorecon(還原方式)
[root@localhost ~]# restorecon [-Rv] 文件或目錄
參數:
-R:連同子目錄一塊兒修改。
-v:將過程顯示到屏幕上。
範例一:將剛纔錯誤的index.html以默認的安全上下文改正過來
[root@localhost ~]# restorecon -Rv /var/www/html/index.html
Relabeled /var/www/html/index.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 7 Feb 22 21:07 /var/www/html/index.html
六、SELinux日誌
setroubleshoot:將錯誤信息寫入/var/log/message
[root@localhost ~]# cat /var/log/messages | grep setroubleshoot
Feb 22 21:28:27 localhost setroubleshoot[12759]: failed to retrieve rpm info for /var/www/html/index.html
Feb 22 21:28:27 localhost setroubleshoot[12759]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages run: sealert -l cf1dacbd-e06f-4c52-bbd4-abc3d3cde551
[root@localhost ~]# sealert -l cf1dacbd-e06f-4c52-bbd4-abc3d3cde551
SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html.
#<==就是剛纔/var/log/messages的信息
* Plugin restorecon (99.5 confidence) suggests ****
If you want to fix the label.
/var/www/html/index.html default label should be httpd_sys_content_t.
Then you can run restorecon. The access attempt may have been stopped due to insufficient permissions to access a parent directory in which case try to change the following command accordingly.
Do
#<==若要容許訪問,你須要執行如上操做。
* Plugin catchall (1.49 confidence) suggests **
If you believe that httpd should be allowed getattr access on the index.html file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
Additional Information:
Source Context system_u:system_r:httpd_t:s0
Target Context unconfined_u:object_r:admin_home_t:s0
Target Objects /var/www/html/index.html [ file ]
Source httpd
Source Path /usr/sbin/httpd
Port <Unknown>
Host localhost.localdomain
Source RPM Packages
Target RPM Packages
Policy RPM selinux-policy-3.14.1-61.el8.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name localhost.localdomain
Platform Linux localhost.localdomain 4.18.0-80.el8.x86_64
#1 SMP Tue Jun 4 09:19:46 UTC 2019 x86_64 x86_64
Alert Count 10
First Seen 2020-02-22 21:28:26 EST
Last Seen 2020-02-22 21:28:41 EST
Local ID cf1dacbd-e06f-4c52-bbd4-abc3d3cde551
Raw Audit Messages
type=AVC msg=audit(1582424921.167:319): avc: denied { getattr } for pid=11426 comm="httpd" path="/var/www/html/index.html" dev="dm-0" ino=33926150 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:admin_home_t:s0 tclass=file permissive=0
Hash: httpd,httpd_t,admin_home_t,file,getattr
與setroubleshoot不一樣的是,audit會將許多的SELinux信息都記錄下來,不僅是錯誤信息而已,所以日誌文件/var/log/audit/audit.log很是龐大。
[root@localhost ~]# audit2why < /var/log/audit/audit.log
type=USER_AVC msg=audit(1581914928.322:67): pid=783 uid=81 auid=4294967295 ses=4294967295 subj=system_u:system_r:system_dbusd_t:s0-s0:c0.c1023 msg='avc: denied { send_msg } for msgtype=error error_name=org.freedesktop.NetworkManager.AlreadyEnabledOrDisabled dest=:1.16 spid=885 tpid=1415 scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:vmtools_unconfined_t:s0 tclass=dbus permissive=0 exe="/usr/bin/dbus-daemon" sauid=81 hostname=? addr=? terminal=?'UID="dbus" AUID="unset" SAUID="dbus"
Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access.
七、SELinux的策略與規則管理
一個主體進程可否讀取到目標文件資源的重點在於SELinux的策略以及策略內的各項規則,而後再經過該規則的定義去處理各目標文件的安全上下文,尤爲是「類型」的部分。
● 策略查詢
CentOS 8.x默認使用targeted策略,那麼這個策略提供多少相關規則呢?
能夠經過seinfo來查詢。
[root@localhost ~]# yum -y install setools-console.x86_64 #<==安裝命令
[root@localhost ~]# seinfo [-Atrub]
參數:
-A:列出SELinux的狀態、規則布爾值、身份識別、角色、類型等全部信息;
-t:列出SELinux的全部類型(type)種類;
-r:列出SELinux的全部角色(role)種類;
-u:列出SELinux的全部身份標識(user)種類;
-b:列出全部規則的種類(布爾值)。
範例一:列出SELinux在此策略下的統計狀態
[root@localhost ~]# seinfo
Statistics for policy file: /sys/fs/selinux/policy
Policy Version: 31 (MLS enabled) #<==列出策略所在文件與版本
Target Policy: selinux
Handle unknown classes: allow
Classes: 129 Permissions: 452
Sensitivities: 1 Categories: 1024
Types: 4914 Attributes: 251
Users: 8 Roles: 14
Booleans: 324 Cond. Expr.: 370
Allow: 111263 Neverallow: 0
Auditallow: 159 Dontaudit: 10271
Type_trans: 237483 Type_change: 74
Type_member: 35 Range_trans: 6015
Role allow: 39 Role_trans: 424
Constraints: 71 Validatetrans: 0
MLS Constrain: 72 MLS Val. Tran: 0
Permissives: 0 Polcap: 5
Defaults: 7 Typebounds: 0
Allowxperm: 0 Neverallowxperm: 0
Auditallowxperm: 0 Dontauditxperm: 0
Initial SIDs: 27 Fs_use: 33
Genfscon: 105 Portcon: 627
Netifcon: 0 Nodecon: 0
範例二:列出與httpd有關的規則(booleana)有哪些
[root@localhost ~]# seinfo -b | grep httpd
httpd_anon_write
httpd_builtin_scripting
httpd_can_check_spam
httpd_can_connect_ftp
httpd_can_connect_ldap
httpd_can_connect_mythtv
httpd_can_connect_zabbix
......(下面省略)......
若是查詢到相關的類型或者是布爾值後,想要知道詳細的規則時,就得要使用sesearch這個命令了!
[root@localhost ~]# sesearch [-A] [-s 主體類型] [-t 目標類型] [-b 布爾值]
參數:
-A:列出該類型或布爾值的全部相關信息;
-t:後面還要接類型,例如-t httpd_t;
-b:後面還要接布爾值的規則,例如-b httpd_enable_ftp_server;
範例一:找出目標文件資源類型爲httpd_sys_content_t的有關信息
[root@localhost ~]# sesearch -a -t httpd_sys_content_t
範例二:找出主體進程爲httpd_t且目標文件類型爲httpd相關的全部信息
[root@localhost ~]# sesearch -s httpdt -t httpd* -a
範例三:我知道有個布爾值爲httpd_enable_homedirs,請問該布爾值設置了多少規則?
[root@localhost ~]# sesearch -b httpd_enable_homedirs -A
● 布爾值的查詢與修改
上面咱們經過sesearch知道了,其實Subject與Object可否有訪問的權限是與布爾值有關的,那麼系統有多少布爾值能夠經過「seinfo -b」來查詢,可是每一個布爾值是啓動仍是關閉的呢?
這就來查詢一下吧:
[root@localhost ~]# getsebool [-a] [布爾值條款]
參數:
-a:列出目前系統上面的全部布爾值條款設置爲開啓或關閉值
範例一:查詢本系統內全部的布爾值設置情況
[root@localhost ~]# getsebool -a
abrt_anon_write --> off
abrt_handle_event --> off
abrt_upload_watch_anon_write --> on
antivirus_can_scan_system --> off
antivirus_use_jit --> off
auditadm_exec_content --> on
......(下面省略)......
關閉和啓動某個布爾值:
[root@localhost ~]# setsebool [-P] 布爾值=[0|1]
參數:
-P:直接將設置值寫入配置文件,該設置數據未來會生效的!
範例一:查詢httpd_enable_homedirs是否爲關閉,若不爲關閉,請關閉它!
[root@localhost ~]# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> on
[root@localhost ~]# setsebool -P httpd_enable_homedirs=0
[root@localhost ~]# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> off
● 默認目錄的安全上下文查詢與修改
使用semanage查看目錄的默認安全上下文:
[root@localhost ~]# semanage {login,user,port,interface,fcontext,translation} -l
[root@localhost ~]# semanage fcontext -{a|d|m} [-frst] file_spec
參數:
fcontext:主要用在安全上下文方面的用途,-l爲查詢的意思;
-a:增長的意思,你能夠增長一些目錄的默認安全上下文類型設置;
-m:修改的意思;
-d:刪除的意思;
範例一:查詢一下/var/www/html的默認安全上下文設置
[root@localhost ~]# semanage fcontext -l
......(前面省略)......
/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
......(後面省略)......
範例二:利用semanage設置/srv/samba目錄的默認安全上下文爲public_content_t
[root@localhost ~]# mkdir /srv/samba
[root@localhost ~]# ll -Zd /srv/samba/
drwxr-xr-x. 2 root root unconfined_u:object_r:var_t:s0 6 Feb 22 22:46 /srv/samba/
[root@localhost ~]# semanage fcontext -l|grep '/srv'
/srv all files system_u:object_r:var_t:s0
/srv/([^/]/)?ftp(/.)? all files system_u:object_r:public_content_t:s0
/srv/([^/]/)?rsync(/.)? all files system_u:object_r:public_content_t:s0
/srv/([^/]/)?www(/.)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/([^/]/)?www/logs(/.)? all files system_u:object_r:httpd_log_t:s0
/srv/. all files system_u:object_r:var_t:s0
/srv/gallery2(/.)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/gallery2/smarty(/.)? all files system_u:object_r:httpd_sys_rw_content_t:s0
/srv/lib/gitosis(/.)? all files system_u:object_r:gitosis_var_lib_t:s0
/srv/loopback-device(/.)? all files system_u:object_r:swift_data_t:s0
/srv/node(/.)? all files system_u:object_r:swift_data_t:s0
#上面則是默認的/srv下面的安全上下文數據,不過,並無指定到/srv/samba
[root@localhost ~]# semanage fcontext -a -t public_content_t "/srv/samba(/.)?"
[root@localhost ~]# semanage fcontext -l | grep '/srv/samba(/.)?'
/srv/samba(/.*)? all files system_u:object_r:public_content_t:s0
[root@localhost ~]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
/srv/samba(/.*)? system_u:object_r:public_content_t:s0#其實就是寫這個文件[root@localhost ~]# ll -Zd /srv/sambadrwxr-xr-x. 2 root root unconfined_u:object_r:var_t:s0 6 Feb 22 22:46 /srv/samba[root@localhost ~]# restorecon -Rv /srv/samba[root@localhost ~]# ll -Zd /srv/sambadrwxr-xr-x. 2 root root unconfined_u:object_r:public_content_t:s0 6 Feb 22 22:46 /srv/samba#有默認值,之後用restorecon來修改比較簡單!