SELINUX

linux的訪問控制

  • Discretionary Access Control(DAC)

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

[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進(jìn)程的屬于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權(quán)限的,那如果有人劫持了httpd進(jìn)程,那他就可以訪問到/etc/passwd文件,這種不就知道了系統(tǒng)上有什么用戶了,就可以有針對性的對用戶進(jìn)程密碼破解。系統(tǒng)中other用戶對很多的文件都有r權(quán)限,這是非常不安全的。

在自主訪問控制下,沒有辦法將一個進(jìn)程的訪問限制在一個允許的范圍內(nèi)。如:

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

這能不能實(shí)現(xiàn)呢?

  • Mandatory Access Control(MAC)

在MAC(強(qiáng)制訪問控制)這種模型里,管理員管理訪問控制。管理員制定策略,用戶不能改變它。策略定義了哪個主體能訪問哪個對象。這種訪問控制模型可以增加安全級別,因?yàn)樗诓呗?,任何沒有被顯式授權(quán)的操作都不能執(zhí)行。MAC被開發(fā)和實(shí)現(xiàn)在最重視保密的系統(tǒng)中,如軍事系統(tǒng)。主體獲得清楚的標(biāo)記,對象得到分類標(biāo)記,或稱安全級別。

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

  • SELinux

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

規(guī)則定義在什么地方呢?

  • policy(政策)

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

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

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


  • 安全上下文(Security Context)

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

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

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

[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選項(xiàng)后多出來的顯示了嗎?這就是context
system_u:system_r:httpd_t:s0
unconfined_u:object_r:httpd_sys_content_t:s0

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

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

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

SELinux的一部分是基于角色的訪問控制(RBAC)安全模型。該角色是RBAC的屬性。 SELinux用戶被授權(quán)使用角色,角色被授權(quán)用于域。該角色充當(dāng)域和SELinux用戶之間的中介??梢暂斎氲慕巧珱Q定了可以輸入哪些域。最終,它控制可以訪問的對象類型。這有助于減少特權(quán)升級攻擊的漏洞。
type 當(dāng)類型與進(jìn)程相關(guān)聯(lián)時,它定義SELinux用戶(subject)可以訪問哪些進(jìn)程(或domain)。
當(dāng)類型與object相關(guān)聯(lián)時,它定義SELinux用戶對該object具有什么訪問權(quán)限。

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

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

類型是類型強(qiáng)制的屬性。該類型定義進(jìn)程的域,并定義文件的類型。 SELinux策略規(guī)則定義類型如何相互訪問,無論是訪問類型的域還是訪問另一個域的域。僅當(dāng)存在允許訪問的特定SELinux策略規(guī)則時,才允許訪問。

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

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

我們知道在Centos中httpd服務(wù)的默認(rèn)訪問路徑在/var/www/html/中,我們安裝、啟動一個httpd服務(wù),再在/var/www/html/下創(chuàng)建的html文件,可以直接在瀏覽器中訪問

現(xiàn)在來解釋一下這個上下文,用戶執(zhí)行了httpd_exec_t類型的httpd程序,可以產(chǎn)生一個工作在httpd_t的進(jìn)程,這個進(jìn)程可以訪問httpd_sys_content_t類型的文件/var/www/html/index.html

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

httpd_exec_t 類型(entrypoint)運(yùn)行進(jìn)入 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,并執(zhí)行targeted 策略后,系統(tǒng)為我們定義了一堆的rule,來控制常見的網(wǎng)絡(luò)服務(wù),如:httpd,vsftp,samba等等

\color{rgb(255,0,0)}{rule中沒有明確定義允許的,就不能訪問}

\color{rgb(255,0,0)}{常見舉例1:如將/var/www/html/目錄下的文件type改掉,那網(wǎng)站將不能訪問}

[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

這是因?yàn)檫\(yùn)行在httpd_t域的httpd,不能read(讀) admin_home_t類型的文件

\color{rgb(255,0,0)}{常見舉例2:如將httpd的DocumentRoot "/var/www/html"修改位置,將不能訪問網(wǎng)站}

[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/

這是因?yàn)閜olicy中定義/var/www/html目錄和其下的目錄、文件的type為httpd_sys_content_t,這樣運(yùn)行在httpd_t域的httpd,就可以訪問了,但將DocumentRoot重新指定位置后,在policy中就沒有我們指定位置的相關(guān)定義,不會將指定的目錄的context設(shè)置為typehttpd_sys_content_t,這樣httpd進(jìn)程也就不能正常訪問了

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


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

? 一直說targeted 只控制了部分的進(jìn)程,那就將系統(tǒng)中的進(jìn)程分為了confined(受控) 和 unconfined(不受控),我們是不是可以將httpd調(diào)整為 不受控 呢?

[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

可以看到現(xiàn)在httpd進(jìn)程的domain變?yōu)榱藆nconfined_service_t,現(xiàn)在再來訪問網(wǎng)站就不再受控于selinux了,你再像上面的例子調(diào)整網(wǎng)站文件的位置,type都可以正常訪問了,(當(dāng)然DAC還是要遵守的)。


  • BOOLEANS(布爾值)

在targeted中,關(guān)于context的規(guī)則我們沒有能力去修改(能力有限啊,聽說寫規(guī)則是一項(xiàng)專業(yè)的技能),但有著一些boolean我們是可以去改變的

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

這個值一般是讓我們可以改變服務(wù)的運(yùn)行特性
如:

[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認(rèn)為不安全的一些服務(wù)運(yùn)行特性,如:httpd的匿名寫操作,ftpd的匿名寫,samba的用戶家目錄,這些默認(rèn)都是off,不允許的。但在實(shí)際中有可能我們又要用到這些功能,那可以在boolean中進(jìn)行on,放行。


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

  • type的修改
  • boolean的修改

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


下面來說一說selinux的相關(guān)配置和命令:

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

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

服務(wù) 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
易于閱讀的拒絕消息也發(fā)送至 /var/log/messages
  • 3.SELinux 的命令

getenfoce
??查看當(dāng)前的工作模式
setenfoce [0|1]
??改變當(dāng)前的工作模式
??0: permissive
??1: enfocing
?
setenfoce可是在系統(tǒng)運(yùn)行時臨時切換開啟狀態(tài)的selinux的運(yùn)行模式,但不能將selinux從disable(關(guān)閉),切換到0或1中來,selinux從關(guān)閉開啟需要重啟系統(tǒng),而selinux在系統(tǒng)重啟過程中將根據(jù)policy對所有文件(object)進(jìn)行打標(biāo)

[root@localhost html]# getenforce 
Enforcing

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

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

chcon - 改變object上下文
?? -u, --user=USER 在目標(biāo)安全上下文中設(shè)置用戶USER
?? -r, --role=ROLE 在目標(biāo)安全上下文中設(shè)置角色ROLE
?? -t, --type=TYPE 在目標(biāo)安全上下文中設(shè)置類型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的相當(dāng)信息
??-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
??可以顯示更多、更詳細(xì)的信息,如: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
????搜索允許規(guī)則。
??EXPRESSIONS
??-s NAME, --source=NAME
????查找以類型/屬性名稱為源的規(guī)則。(subject domain)
??-t NAME, --target=NAME
????查找以類型/屬性NAME為目標(biāo)的規(guī)則。(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              導(dǎo)入本地自定義
    export              輸出本地自定義
    login               管理linux用戶和SELinux受限用戶之間的登錄映射
    user                管理SELinux受限用戶(SELinux用戶的角色和級別)
    port                管理網(wǎng)絡(luò)端口類型定義
    ibpkey              Manage infiniband ibpkey type definitions
    ibendport           Manage infiniband end port type definitions
    interface           管理網(wǎng)絡(luò)接口類型定義
    module              管理SELinux策略模塊
    node                 管理網(wǎng)絡(luò)節(jié)點(diǎn)類型定義
    fcontext             管理文件上下文映射定義
    boolean             管理布爾值以有選擇地啟用功能
    permissive          管理流程類型強(qiáng)制模式
    dontaudit           禁用/啟用策略中的dontaudit規(guī)則

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

查看時常用 "-l"來顯示相關(guān)信息,如:
[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服務(wù)器>可以<訪問><用戶主目錄中的文件>嗎?


圖1.1

圖1.1 SELinux允許以httpd_t運(yùn)行的Apache進(jìn)程訪問/var/www/html/目錄,并且它拒絕同一進(jìn)程訪問/data/mysql/目錄,因?yàn)閷τ趆ttpd_t和mysqld_db_t類型上下文沒有允許規(guī)則。另一方面,以mysqld_t身份運(yùn)行的MariaDB進(jìn)程能夠訪問/data/mysql/目錄,而SELinux也正確拒絕mysqld_t類型的進(jìn)程來訪問標(biāo)記為httpd_sys_content_t的/var/www/html/目錄。


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

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

上述兩個標(biāo)準(zhǔn)說明:
?

  1. 文件的所有權(quán)的優(yōu)先級高于訪問權(quán)限
    ?
    1)文件的所有者即便沒有任何權(quán)限,也可以在為自己分配權(quán)限之后獲得訪問文件的能力。
    ?
    2)非文件的所有者即便已經(jīng)獲得了訪問權(quán)限,也可能會被所有者隨時收回,從而導(dǎo)致無權(quán)訪問該文件。
    ?
  2. 權(quán)限是文件訪問的關(guān)鍵
    ?
    1)無論是不是文件的所有者,關(guān)系到使用者能否訪問文件的最直接的因素是其所對應(yīng)的用戶是否獲得了可訪問該文件的權(quán)限
    ?
    2)使用者被分配的何種權(quán)限,就只能以該權(quán)限所規(guī)定的操作來訪問文件,無法越權(quán)。

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

配置文件 /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關(guān)閉

修改配置文件 SELINUX=disabled,并重啟主機(jī)

SELinux啟用后,可以有兩種運(yùn)行模式:強(qiáng)制(enforcing)或許可(permissive)。

\color{rgb(0,1,255)}{狀態(tài)查看}

~]$ 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
    當(dāng)SELinux在強(qiáng)制模式下運(yùn)行時,它將強(qiáng)制執(zhí)行SELinux策略,并根據(jù)SELinux策略規(guī)則拒絕訪問。在Red Hat Enterprise Linux中,當(dāng)系統(tǒng)最初與SELinux一起安裝時,默認(rèn)情況下啟用強(qiáng)制模式。

  • Permissive Mode
    當(dāng)SELinux在許可模式下運(yùn)行時,不會強(qiáng)制執(zhí)行SELinux策略。系統(tǒng)保持運(yùn)行狀態(tài),SELinux不會拒絕任何操作,而只會記錄AVC消息,然后將其用于故障排除,調(diào)試和SELinux策略改進(jìn)。在這種情況下,每個AVC僅記錄一次。

改變selinux的Mode

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

在啟動時,您可以設(shè)置幾個內(nèi)核參數(shù)來更改SELinux的運(yùn)行方式:
enforcing=0
設(shè)置此參數(shù)會使計算機(jī)以許可模式啟動,這在解決問題時很有用。如果文件系統(tǒng)太損壞,則使用許可模式可能是檢測問題的唯一選擇。此外,在許可模式下,系統(tǒng)會繼續(xù)正確創(chuàng)建標(biāo)簽。在此模式下創(chuàng)建的AVC消息可以與在強(qiáng)制模式下不同。在寬松模式下,僅報告第一個拒絕。但是,在強(qiáng)制模式下,您可能會拒絕讀取目錄,并且應(yīng)用程序停止。在許可模式下,您將收到相同的AVC消息,但是應(yīng)用程序?qū)⒗^續(xù)讀取目錄中的文件,此外,您還將獲得每個拒絕的AVC。

selinux=0
此參數(shù)導(dǎo)致內(nèi)核不加載SELinux基礎(chǔ)結(jié)構(gòu)的任何部分。初始化腳本會注意到系統(tǒng)使用selinux=0參數(shù)啟動并touch /.autorelabel文件。這將導(dǎo)致系統(tǒng)在您下次啟用SELinux時引導(dǎo)時自動重新標(biāo)記。

強(qiáng)制系統(tǒng)重新標(biāo)記,以下命令:

~]# touch /.autorelabel
~]# reboot

在運(yùn)行SELinux的系統(tǒng)上,所有進(jìn)程和文件都以表示安全相關(guān)信息的方式標(biāo)記。此信息稱為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系統(tǒng)上,訪問是根據(jù)Linux用戶和組ID控制的。在DAC規(guī)則之后檢查SELinux策略規(guī)則。如果DAC規(guī)則首先拒絕訪問,則不使用SELinux策略規(guī)則。

默認(rèn)情況下,新創(chuàng)建的文件和目錄繼承其父目錄的SELinux類型。例如,當(dāng)在/ etc目錄中創(chuàng)建一個標(biāo)有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命令進(jìn)行的更改,在重新標(biāo)記或執(zhí)行restorecon命令后將被重置,不能持續(xù)存在。
~]$ touch file1
~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1

將類型更改為samba_share_t。 -t選項(xiàng)僅更改類型。然后查看更改:

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

恢復(fù)file1文件的SELinux上下文。使用-v選項(xiàng)查看更改:

~]$ 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/目錄中的文件,以查看文件應(yīng)具有的SELinux上下文。

更改目錄及其內(nèi)容類型

~]# 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進(jìn)行的更改由以下實(shí)用程序使用。當(dāng)重新標(biāo)記文件系統(tǒng)并且restorecon實(shí)用工具恢復(fù)默認(rèn)的SELinux上下文時,將使用setfiles實(shí)用工具。這意味著即使重新標(biāo)記了文件系統(tǒng),semanage fcontext所做的更改也將保持不變。

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

~]# semanage fcontext -C -l

快速參考

要在文件系統(tǒng)中保留SELinux上下文更改,請重新標(biāo)記:

  • 輸入以下命令,記住要使用文件或目錄的完整路徑:
    ~]# semanage fcontext -a options file-name|directory-name
  • 使用restorecon實(shí)用工具應(yīng)用上下文更改:
    ~]# 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
更改目錄及其內(nèi)容類型
~]# 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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容