操作系統安全—SELinux(嵌入linux內核中)

學習操作系統安全時候遇到的一個擴展tips,當做筆記來用,轉載自簡書

原文鏈接:https://www.jianshu.com/p/eb3b3b41b619

如有侵權請聯繫刪除

linux的訪問控制

  • Discretionary Access Control(DAC)

自主(自由)訪問控制:這種訪問控制下,一個進程如果被劫持,那這個進程就有了運行這個進程用戶的所有訪問權限。如:
一個httpd進程

[root@localhost ~]# ps aux |grep httpd
apache     3937  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache     3938  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache     3939  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
....

httpd進程的屬於apche用戶的,我們來看一下,/etc/passwd文件

[root@localhost ~]# ll /etc/passwd
-rw-r--r--. 1 root root 1204 Oct 26 18:37 /etc/passwd

這裏可以看出/etc/passwd文件是root用戶,root組的,但other用戶是對這個文件是有r權限的,那如果有人劫持了httpd進程,那他就可以訪問到/etc/passwd文件,這種不就知道了系統上有什麼用戶了,就可以有針對性的對用戶進程密碼破解。系統中other用戶對很多的文件都有r權限,這是非常不安全的。

在自主訪問控制下,沒有辦法將一個進程的訪問限制在一個允許的範圍內。如:

httpd進程就只能訪問我們提供給用戶可以訪問的資源,而我們不想用戶看到的資源,就不能訪問。那這樣如httpd進程被劫持了,那也就只能訪問有限的資源,不可能看到其它的資源。

這能不能實現呢?

  • Mandatory Access Control(MAC)

在MAC(強制訪問控制)這種模型裏,管理員管理訪問控制。管理員制定策略,用戶不能改變它。策略定義了哪個主體能訪問哪個對象。這種訪問控制模型可以增加安全級別,因爲它基於策略,任何沒有被顯式授權的操作都不能執行。MAC被開發和實現在最重視保密的系統中,如軍事系統。主體獲得清楚的標記,對象得到分類標記,或稱安全級別。

在“強制訪問控制”模型中,每一個進程能做什麼都有明確的規則,而且遵循沒有明確說明可以訪問的,就不能訪問,這樣能在任何一個進程被劫持也只能訪問我們定義規則中明確說明它能訪問的資源,所能破壞的也在定義的範圍內,(這就是最小訪問原則

  • SELinux

selinux就是一種mac的訪問控制,任何一個進程的訪問都要經過selinux中定義的規則檢查,通過才能進行訪問

規則定義在什麼地方呢?

  • policy(政策)

這就是規則庫,一堆的規則(rule)集合在一起就成了政策(policy),那按照MAC的定義是不是要將系統上所有的程序能做什麼都寫成規則才能使用selinux呢?理論上是的,只有這樣才能做到最小化訪問原則,但每一個系統上運行的程序千千萬萬,光是寫這個規則我想都沒有人再去用selinux了。(而且規則不是普通用戶能寫的)
那怎麼使用呢?還好在Centos 7 中爲我們提供了有policy

[root@localhost ~]# ll /etc/sysconfig/selinux 
lrwxrwxrwx. 1 root root 17 Oct 10 19:24 /etc/sysconfig/selinux -> ../selinux/config

[root@localhost ~]# cat /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 three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

/etc/selinux/conf中系統爲我們提供了三種policy

  • targeted
    默認使用的policy,只能有限的程序受selinux的保護。
  • minimum
    這是targeted的修改版,僅選定的進程受保護。(用的不多)
  • mls
    多級安全保護。(用的不多)

像上面所有的如果要所有的程序都受selinux的控制,這將是一個非常龐大的工程,對用戶來說基本沒有用戶體驗,(我們不能做任何事,除非在policy中添加我們能做什麼的rule,那光寫rule就天荒地老了,還用什麼?)所以只控制有限的程序是一個不錯的選擇


  • 安全上下文(Security Context)

在啓動selinux後,系統中所有的資源都會被打標(label),這就是安全上下文(context)。selinux就是通過這些標記來輔助完成訪問控制的。

規則中不可能寫 httpd進程能訪問/var/www/html/index.html文件,這樣的具體的條目,這樣改一下 httpd的文件名那所有的規則就都要重寫,明顯不能這麼做,那又要怎麼做呢?

先看一下安全上下文長什麼樣:

[root@localhost ~]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /sbin/httpd
[root@localhost ~]# ps -eZ |grep httpd 
system_u:system_r:httpd_t:s0      3936 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3937 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3938 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3939 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3940 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3941 ?        00:00:00 httpd
[root@localhost ~]# ls -Z /var/www/html/
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

看到加上-Z選項後多出來的顯示了嗎?這就是context
system_u:system_r:httpd_t:s0
unconfined_u:object_r:httpd_sys_content_t:s0

這裏有四段:
user:role:type:range

字段 說明
user SELinux用戶身份。這可以與允許SELinux用戶使用的一個或多個角色相關聯。

使用SELinux策略將每個Linux用戶映射到一個SELinux用戶。這使Linux用戶可以繼承對SELinux用戶的限制。映射的SELinux用戶身份在SELinux上下文中用於該會話中的進程,以便定義他們可以輸入哪些角色和級別。
role SELinux角色。這可以與SELinux用戶被允許訪問的一種或多種類型相關聯。

SELinux的一部分是基於角色的訪問控制(RBAC)安全模型。該角色是RBAC的屬性。 SELinux用戶被授權使用角色,角色被授權用於域。該角色充當域和SELinux用戶之間的中介。可以輸入的角色決定了可以輸入哪些域。最終,它控制可以訪問的對象類型。這有助於減少特權升級攻擊的漏洞。
type 當類型與進程相關聯時,它定義SELinux用戶(subject)可以訪問哪些進程(或domain)。
當類型與object相關聯時,它定義SELinux用戶對該object具有什麼訪問權限。

類型是類型強制的屬性。該類型定義進程的域,並定義文件的類型。 SELinux策略規則定義類型如何相互訪問,無論是訪問類型的域還是訪問另一個域的域。僅當存在允許訪問的特定SELinux策略規則時,才允許訪問。
range 此字段也可以稱爲級別,並且僅在策略支持MCS或MLS時才顯示。該條目可以包括:
•單個安全級別,其中包含敏感級別和零個或多個類別
(例如s0,s1:c0,s7:c10.c15)
•由兩個安全級別(低和高)組成的範圍,兩個安全級別之間用連字符分隔
(例如s0-s15:c0.c1023)。

上面的做個瞭解就好,在Centos的targeted中可以不去關心user、role、range,只關心type就好了

類型是類型強制的屬性。該類型定義進程的域,並定義文件的類型。 SELinux策略規則定義類型如何相互訪問,無論是訪問類型的域還是訪問另一個域的域。僅當存在允許訪問的特定SELinux策略規則時,才允許訪問。

type 這個字段,在進程和文件(linux上一切皆文件)上有不同的稱呼
進程上,一般稱作domain / 域
文件上,一般稱作type / 類型

再來看一下上面的安全上下文
system_u:object_r:httpd_exec_t:s0 /sbin/httpd 文件
system_u:system_r:httpd_t:s0 3936 ? 00:00:00 httpd 進程
unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html 文件

我們知道在Centos中httpd服務的默認訪問路徑在/var/www/html/中,我們安裝、啓動一個httpd服務,再在/var/www/html/下創建的html文件,可以直接在瀏覽器中訪問

現在來解釋一下這個上下文,用戶執行了httpd_exec_t類型的httpd程序,可以產生一個工作在httpd_t的進程,這個進程可以訪問httpd_sys_content_t類型的文件/var/www/html/index.html

這些其實都是在policy中有明確的rule定義的

httpd_exec_t 類型(entrypoint)運行進入 httpd_t

[root@localhost ~]# sesearch -A -t httpd_exec_t |grep httpd_t
   allow httpd_t httpd_exec_t : file { ioctl read getattr lock map execute execute_no_trans entrypoint open } ; 
   ...

httpd_t 可以 訪問 httpd_sys_content_t 類型的鏈接文件、目錄、文件

[root@localhost ~]# sesearch -A -s httpd_t |grep httpd_sys_content_t
   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ; 
   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ; 
   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock map open } ; 
   allow httpd_t httpd_sys_content_t : dir { ioctl read write getattr lock add_name remove_name search open } ; 

這裏要說明一個,在開啓了selinux,並執行targeted 策略後,系統爲我們定義了一堆的rule,來控制常見的網絡服務,如:httpd,vsftp,samba等等

math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7Brule%E4%B8%AD%E6%B2%A1%E6%9C%89%E6%98%8E%E7%A1%AE%E5%AE%9A%E4%B9%89%E5%85%81%E8%AE%B8%E7%9A%84%EF%BC%8C%E5%B0%B1%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7Brule%E4%B8%AD%E6%B2%A1%E6%9C%89%E6%98%8E%E7%A1%AE%E5%AE%9A%E4%B9%89%E5%85%81%E8%AE%B8%E7%9A%84%EF%BC%8C%E5%B0%B1%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7Brule%E4%B8%AD%E6%B2%A1%E6%9C%89%E6%98%8E%E7%A1%AE%E5%AE%9A%E4%B9%89%E5%85%81%E8%AE%B8%E7%9A%84%EF%BC%8C%E5%B0%B1%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消\color{rgb(255,0,0)}{rule中沒有明確定義允許的,就不能訪問}

math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%A6%82%E5%B0%86%2Fvar%2Fwww%2Fhtml%2F%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6type%E6%94%B9%E6%8E%89%EF%BC%8C%E9%82%A3%E7%BD%91%E7%AB%99%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%A6%82%E5%B0%86%2Fvar%2Fwww%2Fhtml%2F%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6type%E6%94%B9%E6%8E%89%EF%BC%8C%E9%82%A3%E7%BD%91%E7%AB%99%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%A6%82%E5%B0%86%2Fvar%2Fwww%2Fhtml%2F%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6type%E6%94%B9%E6%8E%89%EF%BC%8C%E9%82%A3%E7%BD%91%E7%AB%99%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%7Duploading.4e448015.gif轉存失敗重新上傳取消\color{rgb(255,0,0)}{常見舉例1:如將/var/www/html/目錄下的文件type改掉,那網站將不能訪問}

[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@localhost html]# chcon -t admin_home_t index.html 
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html

這是因爲運行在httpd_t域的httpd,不能read(讀) admin_home_t類型的文件

math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%A6%82%E5%B0%86httpd%E7%9A%84DocumentRoot%20%22%2Fvar%2Fwww%2Fhtml%22%E4%BF%AE%E6%94%B9%E4%BD%8D%E7%BD%AE%EF%BC%8C%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%E7%BD%91%E7%AB%99%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%A6%82%E5%B0%86httpd%E7%9A%84DocumentRoot%20%22%2Fvar%2Fwww%2Fhtml%22%E4%BF%AE%E6%94%B9%E4%BD%8D%E7%BD%AE%EF%BC%8C%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%E7%BD%91%E7%AB%99%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(255%2C0%2C0)%7D%7B%E5%B8%B8%E8%A7%81%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%A6%82%E5%B0%86httpd%E7%9A%84DocumentRoot%20%22%2Fvar%2Fwww%2Fhtml%22%E4%BF%AE%E6%94%B9%E4%BD%8D%E7%BD%AE%EF%BC%8C%E5%B0%86%E4%B8%8D%E8%83%BD%E8%AE%BF%E9%97%AE%E7%BD%91%E7%AB%99%7Duploading.4e448015.gif轉存失敗重新上傳取消\color{rgb(255,0,0)}{常見舉例2:如將httpd的DocumentRoot "/var/www/html"修改位置,將不能訪問網站}

[root@localhost html]# mkdir -pv /service/http/
mkdir: created directory ‘/service’
mkdir: created directory ‘/service/http/’
[root@localhost html]# ls -dZ /service/http/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /service/http/

這是因爲policy中定義/var/www/html目錄和其下的目錄、文件的type爲httpd_sys_content_t,這樣運行在httpd_t域的httpd,就可以訪問了,但將DocumentRoot重新指定位置後,在policy中就沒有我們指定位置的相關定義,不會將指定的目錄的context設置爲typehttpd_sys_content_t,這樣httpd進程也就不能正常訪問了

以上都可以將httpd服務的網站 目錄、文件(object)類型修改爲httpd_sys_content_t就可以正常訪問了


其實還有一種修改方法,但不建議在實際中使用

◈ 一直說targeted 只控制了部分的進程,那就將系統中的進程分爲了confined(受控) 和 unconfined(不受控),我們是不是可以將httpd調整爲 不受控 呢?

[root@localhost html]# systemctl stop httpd
[root@localhost html]# ls -Z /bin/cat
-rwxr-xr-x. root root system_u:object_r:bin_t:s0       /bin/cat
[root@localhost html]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /sbin/httpd
[root@localhost html]# chcon -t bin_t /sbin/httpd
[root@localhost html]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:bin_t:s0       /sbin/httpd
[root@localhost html]# systemctl start httpd
[root@localhost html]# ps -eZ |grep httpd
system_u:system_r:unconfined_service_t:s0 4517 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4518 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4519 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4520 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4521 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4522 ? 00:00:00 httpd

可以看到現在httpd進程的domain變爲了unconfined_service_t,現在再來訪問網站就不再受控於selinux了,你再像上面的例子調整網站文件的位置,type都可以正常訪問了,(當然DAC還是要遵守的)。


  • BOOLEANS(布爾值)

在targeted中,關於context的規則我們沒有能力去修改(能力有限啊,聽說寫規則是一項專業的技能),但有着一些boolean我們是可以去改變的

布爾值允許在運行時更改SELinux策略的某些部分,而無需任何SELinux策略編寫知識。這允許進行更改,例如允許服務訪問NFS卷,而無需重新加載或重新編譯SELinux策略。

這個值一般是讓我們可以改變服務的運行特性
如:

[root@localhost html]# semanage boolean -l
SELinux boolean                State  Default Description

httpd_enable_cgi               (on   ,   on)  Allow httpd to enable cgi
httpd_use_nfs                  (off  ,  off)  Allow httpd to use nfs
httpd_anon_write               (off  ,  off)  Allow httpd to anon write
...
ftpd_anon_write                (off  ,  off)  Allow ftpd to anon write
...
samba_enable_home_dirs         (off  ,  off)  Allow samba to enable home dirs
samba_create_home_dirs         (off  ,  off)  Allow samba to create home dirs
samba_share_nfs                (off  ,  off)  Allow samba to share nfs
...

這裏定義了targeted認爲不安全的一些服務運行特性,如:httpd的匿名寫操作,ftpd的匿名寫,samba的用戶家目錄,這些默認都是off,不允許的。但在實際中有可能我們又要用到這些功能,那可以在boolean中進行on,放行。


現在我們對Centos 7 上的targeted策略有了一定的瞭解,在使用中用到最多的:

  • type的修改
  • boolean的修改

至於:什麼 "多級安全(MLS) " ,"限制用戶"等等的諸多功能,我們就不討論了,自行查看官方文檔
SELinux User's and Administrator's Guide


下面來說一說selinux的相關配置和命令:

  • 1. SELinux 的開啓,關閉

    • 關閉
      在/etc/selinux/config文件中修改SELINUX=disabled即可
    • 開啓
      selinux的開啓有兩種模式:enforcingpermissive
      • enfocing
        這是強制模式,所有接受selinux控制的進程對object的訪問都要在rule中有明確的定義,沒有就不能訪問,並記錄日誌
      • permissive
        這是寬容模式,所有接受selinux控制的進程對object的訪問都要在rule中有明確的定義,沒有也可以訪問,但會記錄到日誌中(常用於排錯中)
  • 2. SELinux 的日誌

SELinux決定(例如允許或禁止訪問)被緩存。此緩存稱爲訪問向量緩存(AVC)。 SELinux拒絕訪問時,將記錄拒絕消息。這些拒絕也稱爲“ AVC拒絕”,並根據運行的守護程序記錄到其他位置:

服務 Log 位置
auditd on /var/log/audit/audit.log
auditd off; rsyslogd on /var/log/messages
setroubleshootd, rsyslogd, and auditd on /var/log/audit/audit.log
易於閱讀的拒絕消息也發送至 /var/log/messages
  • 3.SELinux 的命令

getenfoce
  查看當前的工作模式
setenfoce [0|1]
  改變當前的工作模式
  0: permissive
  1: enfocing

setenfoce可是在系統運行時臨時切換開啓狀態的selinux的運行模式,但不能將selinux從disable(關閉),切換到0或1中來,selinux從關閉開啓需要重啓系統,而selinux在系統重啓過程中將根據policy對所有文件(object)進行打標

[root@localhost html]# getenforce 
Enforcing

getsebool
  查看boolean設定
  -a 查看全部
  boolean 查看指定boolean的值
setsebool [ -P] boolean value
  設置boolean值[0|1] [off|on]
  如果指定了-P選項,則值都將寫入磁盤上的策略文件。重新啓動後保持不變。

[root@localhost html]# getsebool -a |grep tftp
tftp_anon_write --> off
tftp_home_dir --> off

chcon - 改變object上下文
   -u, --user=USER 在目標安全上下文中設置用戶USER
   -r, --role=ROLE 在目標安全上下文中設置角色ROLE
   -t, --type=TYPE 在目標安全上下文中設置類型TYPE
   -R, --recursive 遞歸操作文件和目錄

[root@localhost html]# chcon -t admin_home_t index.html 
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html

restorecon pathname - 還原pathname本來的context
   -v 顯示過程


 

sestatus [-v | -b]
  顯示selinux的相當信息
  -v: 顯示更多的信息
  -b: 顯示boolean值信息

[root@localhost html]# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

seinfo
  可以顯示更多、更詳細的信息,如:user, role, type
   -t 顯示類型
   -r 顯示角色
   -u 顯示用戶
   -b 顯示布爾值

[root@localhost html]# seinfo 

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

   Classes:           129    Permissions:       267
   Sensitivities:       1    Categories:       1024
   Types:            4774    Attributes:        258
   Users:               8    Roles:              14
   Booleans:          315    Cond. Expr.:       361
   Allow:          106707    Neverallow:          0
   Auditallow:        155    Dontaudit:       10045
   Type_trans:      18058    Type_change:        74
   Type_member:        35    Role allow:         39
   Role_trans:        416    Range_trans:      5899
   Constraints:       143    Validatetrans:       0
   Initial SIDs:       27    Fs_use:             32
   Genfscon:          102    Portcon:           613
   Netifcon:            0    Nodecon:             0
   Permissives:         0    Polcap:              5

sesearch
  SELinux策略查詢工具
sesearch [OPTIONS] RULE_TYPE [RULE_TYPE ...] [EXPRESSION] [POLICY ...]
  OPTIONS
  -A, --allow
    搜索允許規則。
  EXPRESSIONS
  -s NAME, --source=NAME
    查找以類型/屬性名稱爲源的規則。(subject domain)
  -t NAME, --target=NAME
    查找以類型/屬性NAME爲目標的規則。(object type)

[root@localhost ~]# sesearch -A -t httpd_exec_t |grep httpd_t
   allow httpd_t httpd_exec_t : file { ioctl read getattr lock map execute execute_no_trans entrypoint open } ; 

semanage

[root@localhost html]# semanage -h
usage: semanage [-h]
                
                {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
                ...

semanage is used to configure certain elements of SELinux policy with-out
requiring modification to or recompilation from policy source.

positional arguments:
  {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
    import              導入本地自定義
    export              輸出本地自定義
    login               管理linux用戶和SELinux受限用戶之間的登錄映射
    user                管理SELinux受限用戶(SELinux用戶的角色和級別)
    port                管理網絡端口類型定義
    ibpkey              Manage infiniband ibpkey type definitions
    ibendport           Manage infiniband end port type definitions
    interface           管理網絡接口類型定義
    module              管理SELinux策略模塊
    node                 管理網絡節點類型定義
    fcontext             管理文件上下文映射定義
    boolean             管理布爾值以有選擇地啓用功能
    permissive          管理流程類型強制模式
    dontaudit           禁用/啓用策略中的dontaudit規則

[root@localhost html]# semanage user -h  "使用-h 獲取幫助"

查看時常用 "-l"來顯示相關信息,如:
[root@localhost html]# semanage user -l

                Labeling   MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

guest_u         user       s0         s0                             guest_r
root            user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
staff_u         user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
sysadm_u        user       s0         s0-s0:c0.c1023                 sysadm_r
system_u        user       s0         s0-s0:c0.c1023                 system_r unconfined_r
unconfined_u    user       s0         s0-s0:c0.c1023                 system_r unconfined_r
user_u          user       s0         s0                             user_r
xguest_u        user       s0         s0                             xguest_r



以下參考

=========================

May <subject> do <action> to <object>
May a web server access files in users' home directories?
<Web服務器>可以<訪問><用戶主目錄中的文件>嗎?

 

圖1.1

圖1.1 SELinux允許以httpd_t運行的Apache進程訪問/var/www/html/目錄,並且它拒絕同一進程訪問/data/mysql/目錄,因爲對於httpd_t和mysqld_db_t類型上下文沒有允許規則。另一方面,以mysqld_t身份運行的MariaDB進程能夠訪問/data/mysql/目錄,而SELinux也正確拒絕mysqld_t類型的進程來訪問標記爲httpd_sys_content_t的/var/www/html/目錄。

 

Linux Discretionary Access Control(DAC)
DAC主要的內容包括以下幾個概念:主體、客體、權限(rwx)、所有權(ugo)。
 
在這個模型中,主體是用戶的身份,客體是資源或者說是文件(切記:一切皆文件)。由客體的屬主對自己的客體進行管理,由主體自己決定是否將自己的客體訪問權限或部分訪問權限授予其他主體,這種控制方式是自主的。也就是說,在自主訪問控制下,用戶可以按自己的意願,有選擇地與其他用戶共享他的文件。
 
我們所定義的DAC系統有兩個至關重要的標準:
 

  1. 文件的所有權:系統中的每個文件(一些特殊文件可能沒有,如塊設備文件等)都有所有者。在DAC系統中,文件的所有者是創建這個文件的計算機的使用者(或事件,或另一個文件)。那麼此文件的自主訪問控制權限由它的創建者來決定如何設置和分配;
     
  2. 訪問權限:文件的所有者擁有訪問權限,並且可以將訪問權限分配給自己及其他用戶。

上述兩個標準說明:
 

  1. 文件的所有權的優先級高於訪問權限
     
    1)文件的所有者即便沒有任何權限,也可以在爲自己分配權限之後獲得訪問文件的能力。
     
    2)非文件的所有者即便已經獲得了訪問權限,也可能會被所有者隨時收回,從而導致無權訪問該文件。
     
  2. 權限是文件訪問的關鍵
     
    1)無論是不是文件的所有者,關係到使用者能否訪問文件的最直接的因素是其所對應的用戶是否獲得了可訪問該文件的權限
     
    2)使用者被分配的何種權限,就只能以該權限所規定的操作來訪問文件,無法越權。

======================

配置文件 /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

SELinux關閉

修改配置文件 SELINUX=disabled,並重啓主機

SELinux啓用後,可以有兩種運行模式:強制(enforcing)或許可(permissive)。

math?formula=%5Ccolor%7Brgb(0%2C1%2C255)%7D%7B%E7%8A%B6%E6%80%81%E6%9F%A5%E7%9C%8B%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(0%2C1%2C255)%7D%7B%E7%8A%B6%E6%80%81%E6%9F%A5%E7%9C%8B%7Duploading.4e448015.gif轉存失敗重新上傳取消math?formula=%5Ccolor%7Brgb(0%2C1%2C255)%7D%7B%E7%8A%B6%E6%80%81%E6%9F%A5%E7%9C%8B%7Duploading.4e448015.gif轉存失敗重新上傳取消\color{rgb(0,1,255)}{狀態查看}

~]$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      30
  • Enforcing Mode
    當SELinux在強制模式下運行時,它將強制執行SELinux策略,並根據SELinux策略規則拒絕訪問。在Red Hat Enterprise Linux中,當系統最初與SELinux一起安裝時,默認情況下啓用強制模式。

  • Permissive Mode
    當SELinux在許可模式下運行時,不會強制執行SELinux策略。系統保持運行狀態,SELinux不會拒絕任何操作,而只會記錄AVC消息,然後將其用於故障排除,調試和SELinux策略改進。在這種情況下,每個AVC僅記錄一次。

改變selinux的Mode

~]# getenforce
Enforcing
~]# setenforce 0
~]# getenforce
Permissive
~]# setenforce 1
~]# getenforce
Enforcing

在啓動時,您可以設置幾個內核參數來更改SELinux的運行方式:
enforcing=0
設置此參數會使計算機以許可模式啓動,這在解決問題時很有用。如果文件系統太損壞,則使用許可模式可能是檢測問題的唯一選擇。此外,在許可模式下,系統會繼續正確創建標籤。在此模式下創建的AVC消息可以與在強制模式下不同。在寬鬆模式下,僅報告第一個拒絕。但是,在強制模式下,您可能會拒絕讀取目錄,並且應用程序停止。在許可模式下,您將收到相同的AVC消息,但是應用程序將繼續讀取目錄中的文件,此外,您還將獲得每個拒絕的AVC。

selinux=0
此參數導致內核不加載SELinux基礎結構的任何部分。初始化腳本會注意到系統使用selinux=0參數啓動並touch /.autorelabel文件。這將導致系統在您下次啓用SELinux時引導時自動重新標記。

強制系統重新標記,以下命令:

~]# touch /.autorelabel
~]# reboot

在運行SELinux的系統上,所有進程和文件都以表示安全相關信息的方式標記。此信息稱爲SELinux上下文。對於文件,可以使用ls -Z命令查看:

~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1

在此示例中,SELinux提供了一個用戶(unconfined_u),一個角色(object_r),一個類型(user_home_t)和一個級別(s0)。此信息用於制定訪問控制決策。在DAC系統上,訪問是根據Linux用戶和組ID控制的。在DAC規則之後檢查SELinux策略規則。如果DAC規則首先拒絕訪問,則不使用SELinux策略規則。

默認情況下,新創建的文件和目錄繼承其父目錄的SELinux類型。例如,當在/ etc目錄中創建一個標有etc_t類型的新文件時,新文件將繼承相同的類型:

~]$ ls -dZ - /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0       /etc
~]# touch /etc/file1
~]# ls -lZ /etc/file1
-rw-r--r--. root root unconfined_u:object_r:etc_t:s0   /etc/file1
  • chcon
    Chcon命令更改文件的SELinux上下文。但是,使用chcon命令進行的更改,在重新標記或執行restorecon命令後將被重置,不能持續存在。
~]$ touch file1
~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1

將類型更改爲samba_share_t。 -t選項僅更改類型。然後查看更改:

~]$ chcon -t samba_share_t file1
~]$ ls -Z file1 
-rw-rw-r--  user1 group1 unconfined_u:object_r:samba_share_t:s0 file1

恢復file1文件的SELinux上下文。使用-v選項查看更改:

~]$ restorecon -v file1
restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0

restorecon命令讀取/etc/selinux/targeted/contexts/files/目錄中的文件,以查看文件應具有的SELinux上下文。

更改目錄及其內容類型

~]# mkdir /web
~]# touch /web/file{1,2,3}
~]# ls -dZ /web
drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
~]# ls -lZ /web
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
~]# chcon -R -t httpd_sys_content_t /web/
~]# ls -dZ /web/
drwxr-xr-x  root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/
~]# ls -lZ /web/
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  • semanage fcontext
    通過semanage fcontext進行的更改由以下實用程序使用。當重新標記文件系統並且restorecon實用工具恢復默認的SELinux上下文時,將使用setfiles實用工具。這意味着即使重新標記了文件系統,semanage fcontext所做的更改也將保持不變

要顯示新創建的文件和目錄的上下文,請以超級用戶身份輸入以下命令

~]# semanage fcontext -C -l

快速參考

要在文件系統中保留SELinux上下文更改,請重新標記:

  • 輸入以下命令,記住要使用文件或目錄的完整路徑:
    ~]# semanage fcontext -a options file-name|directory-name
  • 使用restorecon實用工具應用上下文更改:
    ~]# restorecon -v file-name|directory-name

修改文件

~]# touch /etc/file1
~]$ ls -Z /etc/file1
-rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
~]# semanage fcontext -a -t samba_share_t /etc/file1
~]# ls -Z /etc/file1
-rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
~]$ semanage fcontext -C -l
/etc/file1    unconfined_u:object_r:samba_share_t:s0
~]# restorecon -v /etc/file1
restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0

更改目錄及其內容類型

~]# mkdir /web
~]# touch /web/file{1,2,3}
~]# ls -dZ /web
drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
~]# ls -lZ /web
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3

 

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