1. 程式人生 > >SELinux簡介以及一些常用命令

SELinux簡介以及一些常用命令

1. 簡介

      SELinux(Security-Enhanced Linux) 是美國國家安全域性「NSA=The National Security Agency」 和SCC(Secure Computing Corporation)開發的 Linux的一個擴張強制訪問控制(MAC)安全模組,是 Linux® 上最傑出的新安全子系統。NSA是在Linux社群的幫助下開發了一種訪問控制體系,在這種訪問控制體系的限制下,程序只能訪問那些在他的任務中所需要檔案

      SELinux是一種基於 域-型別 模型(domain-type)的強制訪問控制(MAC)安全系統,它由NSA編寫並設計成核心模組包含到核心中,相應的某些安全相關的應用也被打了SELinux的補丁,最後還有一個相應的安全策略。

     眾所周知,標準的UNIX安全模型是"任意的訪問控制"DAC。就是說,任何程式對其資源享有完全的控制權。假設某個程式打算把含有潛在重要資訊的檔案扔到/tmp目錄下,那麼在DAC情況下沒人能阻止他!

     而MAC情況下的安全策略完全控制著對所有資源的訪問。這是MAC和DAC本質的區別。

     SELinux提供了比傳統的UNIX許可權更好的訪問控制。

2. 基本概念

2.1 主體

      通常指使用者,或代表使用者意圖執行程序或裝置。主體是訪問操作的主動發起者,它是系統中資訊流的啟動者,可以使資訊流在實體之間流動。

2.2 客體

      通常是指資訊的載體或從其他主體或客體接收資訊的實體。
      主體有時也會成為訪問或受控的物件,如一個主體可以向另一個主體授權,一個程序可能控制幾個子程序等情況,這時受控的主體或子程序也是一種客體。

2.3 訪問控制分類

      客體不受它們所依存的系統的限制,可以包括記錄、資料塊、儲存頁、儲存段、檔案、目錄、目錄樹、庫表、郵箱、訊息、程式等,還可以包括位元位、位元組、字、欄位、變數、處理器、通訊通道、時鐘、網路結點等。

2.3.1 自主訪問控制DAC

      管理的方式不同就形成不同的訪問控制方式。一種方式是由客體的屬主對自己的客體進行管理,由屬主自己決定是否將自己客體的訪問權或部分訪問權授予其他主體,這種控制方式是自主的,我們把它稱為自主訪問控制(Discretionary Access Control——DAC)。在自主訪問控制下,一個使用者可以自主選擇哪些使用者可以共享他的檔案。Linux系統中有兩種自主訪問控制策略,一種是9位許可權碼(User-Group-Other),另一種是訪問控制列表ACL(Access Control List)。

2.3.2  強制訪問控制MAC

      強制訪問控制(Mandatory Access Control——MAC),用於將系統中的資訊分密級和類進行管理,以保證每個使用者只能訪問到那些被標明可以由他訪問的資訊的一種訪問約束機制。通俗的來說,在強制訪問控制下,使用者(或其他主體)與檔案(或其他客體)都被標記了固定的安全屬性(如安全級、訪問許可權等),在每次訪問發生時,系統檢測安全屬性以便確定一個使用者是否有權訪問該檔案。其中多級安全(MultiLevel Secure, MLS)就是一種強制訪問控制策略。

3. 控制切換

    Fedora core 5 裡的/etc/sysconfig/selinux標準設定如下:

[cpp] view plain copy print?
  1. # This file controls the state of SELinux on the system.
  2. # SELINUX= can take one of these three values:
  3. # enforcing - SELinux security policy is enforced.
  4. # permissive - SELinux prints warnings instead of enforcing.
  5. # disabled - SELinux is fully disabled.
  6. SELINUX=enforcing  
  7. # SELINUX=disabled
  8. # SELINUXTYPE= type of policy in use. Possible values are:
  9. # targeted - Only targeted network daemons are protected.
  10. # strict - Full SELinux protection.
  11. SELINUXTYPE=targeted  
save_snippets.png
# 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 - SELinux is fully disabled.
SELINUX=enforcing

# SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted


3.1 SELINUX

      SELINUX 有【disabled】、【permissive】、【enforcing】3種選擇。

      • disabled:不啟用SELINUX功能

      • permissive:SELINUX有效,但是即使你違反了策略,它讓你繼續操作,但是把你的違反的內容記錄下來。在我們開發策略的時候非常的有用。相當於Debug模式。

      • enforcing:當你違反了策略,你就無法繼續操作下去。

3.2 SELINUXTYPE

      目前主要有2大類,【targeted】和【strict】。

      • targeted:它是紅帽子開發的targeted,它只是對於主要的網路服務進行保護,比如apache, sendmail, bind, postgresql等,不屬於那些domain的就都讓他們在unconfined_t裡,可匯入性高,可用性好但是不能對整個系統進行保護。

      • strict:是由NAS開發的,能對整個系統進行保護,但是設定複雜,但是隻要掌握一些基本的知識,還是可以玩得動的。

     我們除了在/etc/sysconfig/selinux設它有效無效外,在啟動的時候,也可以通過傳遞引數selinux給核心來控制它。(Fedora 5預設是有效)

3.3 對檔案系統賦予標籤

[cpp] view plain copy print?
  1. [[email protected] sysconfig]# /usr/sbin/getenforce  
  2. Enforcing  
  3. #確認有效後重新對檔案系統賦予標籤:
  4. [[email protected] sysconfig]# /sbin/fixfiles relabel  
  5. #或者
  6. [[email protected] /]# touch /.autorelabel  
  7. #然後 reboot,你就在secure的Linux環境下工作了。
save_snippets.png
[[email protected] sysconfig]# /usr/sbin/getenforce
Enforcing
#確認有效後重新對檔案系統賦予標籤:
[[email protected] sysconfig]# /sbin/fixfiles relabel
#或者
[[email protected] /]# touch /.autorelabel
#然後 reboot,你就在secure的Linux環境下工作了。

4. 基本操作

    SELinux是個經過安全強化的Linux作業系統,實際上,基本上原來的運用軟體沒有必要修改就能在它上面執行。真正做了特別修改的RPM包只有50 多個。像檔案系統EXT3都經過了擴充套件。對於一些原有的命令也進行了擴充套件,另外還增加了一些新的命令,接下來我們就來看看這些命令。

4.1 檔案操作

4.1.1  ls命令

         在命令後加個 -Z 或者加 -context [cpp] view plain copy print?
  1. [[email protected] azureus]# ls -Z  
  2. -rwxr-xr-x fu fu user_u:object_r:user_home_t azureus  
  3. -rw-r--r-- fu fu user_u:object_r:user_home_t Azureus2.jar  
  4. -rw-r--r-- fu fu user_u:object_r:user_home_t Azureus.png  
save_snippets.png
[[email protected] azureus]# ls -Z
-rwxr-xr-x fu fu user_u:object_r:user_home_t azureus
-rw-r--r-- fu fu user_u:object_r:user_home_t Azureus2.jar
-rw-r--r-- fu fu user_u:object_r:user_home_t Azureus.png

4.1.2 chcon

        更改檔案的標籤

[cpp] view plain copy print?
  1. [[email protected] tmp]# ls --context test.txt  
  2. -rw-r--r-- root root root:object_r:staff_tmp_t test.txt  
  3. [[email protected] tmp]# chcon -t etc_t test.txt  
  4. [[email protected] tmp]# ls -lZ test.txt  
  5. -rw-r--r-- root root root:object_r:etc_t test.txt  
save_snippets.png
[[email protected] tmp]# ls --context test.txt
-rw-r--r-- root root root:object_r:staff_tmp_t test.txt

[[email protected] tmp]# chcon -t etc_t test.txt

[[email protected] tmp]# ls -lZ test.txt
-rw-r--r-- root root root:object_r:etc_t test.txt

4.1.3 restorecon

        當這個檔案在策略裡有定義時,可以恢復原來的 檔案標籤。

4.1.4 setfiles

        跟chcon一樣可以更改一部分檔案的標籤,不需要對整個檔案系統重新設定標籤。

4.1.5 fixfiles

         一般是對整個檔案系統的, 後面一般跟 relabel,對整個系統 relabel後,一般我們都重新啟動。如果,在根目錄下有.autorelabel空檔案的話,每次重新啟動時都呼叫 fixfiles relabel

4.1.6 star

        就是tar在SELinux下的互換命令,能把檔案的標籤也一起備份起來。

4.1.7 cp

        可以跟 -Z,--context=CONTEXT 在拷貝的時候指定目的地檔案的security context

4.1.8 find

        可以跟 –context 查特定的type的檔案。例子如下:
[cpp] view plain copy print?
  1. find /home/fu/ --context fu:fu_r:amule_t -exec ls -Z {} \:  
save_snippets_01.png
find /home/fu/ --context fu:fu_r:amule_t -exec ls -Z {} \:

4.1.9 run_init

         在sysadm_t裡手動啟動一些如Apache之類的程式,也可以讓它正常進行,domain遷移。

4.2 程序domain的確認

      程式現在在那個domain裡執行,我們可以在ps 命令後加 -Z進行檢視:

[cpp] view plain copy print?
  1. [[email protected] /]# ps -eZ  
  2.     LABEL                 PID TTY TIME CMD  
  3. system_u:system_r:init_t   1  00:00:00 init  
  4. system_u:system_r:kernel_t 2  00:00:00 ksoftirqd/0  
  5. system_u:system_r:kernel_t 3  00:00:00 watchdog/0  
save_snippets.png
[[email protected] /]# ps -eZ
    LABEL                 PID TTY TIME CMD
system_u:system_r:init_t   1  00:00:00 init
system_u:system_r:kernel_t 2  00:00:00 ksoftirqd/0
system_u:system_r:kernel_t 3  00:00:00 watchdog/0

4.3 ROLE的確認和變更

     命令id能用來確認自己的 security context。

[cpp] view plain copy print?
  1. [[email protected] ~]# id  
  2. uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:staff_r:staff_t  
save_snippets.png
[[email protected] ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:staff_r:staff_t
         這裡,雖然是root使用者,但也只是在一般的ROLE在staff_t裡執行,如果在enforcing模式下,這時的root對於系統管理工作來說,是什麼也幹不了。
[cpp] view plain copy print?
  1. [[email protected] ~]# newrole -r sysadm_r  
  2. Authenticating root.  
  3. 口令:  
  4. [[email protected] ~]# id  
  5. uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:sysadm_r:sysadm_t  
save_snippets.png
[[email protected] ~]# newrole -r sysadm_r
Authenticating root.
口令:
[[email protected] ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:sysadm_r:sysadm_t

4.4 模式切換

4.4.1 getenforce

       得到當前的SELINUX值。
[cpp] view plain copy print?
  1. [[email protected] bin]# getenforce  
  2. Permissive  
save_snippets.png
[[email protected] bin]# getenforce
Permissive

4.4.2 setenforce

        更改當前的SELINUX值 ,後面可以跟 enforcing,permissive 或者 1,0。 [cpp] view plain copy print?
  1. [[email protected] bin]# setenforce permissive  
save_snippets.png
[[email protected] bin]# setenforce permissive

4.4.3 sestatus

        顯示當前的 SELinux的資訊。
[cpp] view plain copy print?
  1. [[email protected] bin]# sestatus -v  
  2. SELinux status: enabled  
  3. SELinuxfs mount: /selinux  
  4. Current mode: permissive  
  5. Mode from config file: permissive  
  6. Policy version: 20  
  7. Policy from config file: refpolicy  
  8. Process contexts:  
  9. Current context: user_u:user_r:user_t  
  10. Init context: system_u:system_r:init_t  
  11. /sbin/mingetty system_u:system_r:getty_t  
  12. /usr/sbin/sshd system_u:system_r:sshd_t  
  13. File contexts:  
  14. Controlling term: user_u:object_r:user_devpts_t  
  15. /etc/passwd system_u:object_r:etc_t  
  16. /etc/shadow system_u:object_r:shadow_t  
  17. /bin/bash system_u:object_r:shell_exec_t  
  18. /bin/login system_u:object_r:login_exec_t  
  19. /bin/sh system_u:object_r:bin_t -> system_u:object_r:shell_exec_t  
  20. /sbin/agetty system_u:object_r:getty_exec_t  
  21. /sbin/init system_u:object_r:init_exec_t  
  22. /sbin/mingetty system_u:object_r:getty_exec_t  
save_snippets.png
[[email protected] bin]# sestatus -v
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: permissive
Policy version: 20
Policy from config file: refpolicy
Process contexts:
Current context: user_u:user_r:user_t
Init context: system_u:system_r:init_t
/sbin/mingetty system_u:system_r:getty_t
/usr/sbin/sshd system_u:system_r:sshd_t
File contexts:
Controlling term: user_u:object_r:user_devpts_t
/etc/passwd system_u:object_r:etc_t
/etc/shadow system_u:object_r:shadow_t
/bin/bash system_u:object_r:shell_exec_t
/bin/login system_u:object_r:login_exec_t
/bin/sh system_u:object_r:bin_t -> system_u:object_r:shell_exec_t
/sbin/agetty system_u:object_r:getty_exec_t
/sbin/init system_u:object_r:init_exec_t
/sbin/mingetty system_u:object_r:getty_exec_t

4.5 其他重要命令

4.5.1 audit2allow

        很重要的一個以python寫的命令,主要用來處理日誌,把日誌中的違反策略的動作的記錄,轉換成 access vector,對開發安全策略非常有用。在refpolicy裡,它的功能比以前有了很大的擴充套件。 [cpp] view plain copy print?
  1. [[email protected] log]# cat dmesg | audit2allow -m local > local.te  
save_snippets_01.png
[[email protected] log]# cat dmesg | audit2allow -m local > local.te

4.5.2 checkmodule

         編譯模組。

[cpp] view plain copy print?
  1. [[email protected] log]# checkmodule -m -o local.mod local.te  
  2. checkmodule: loading policy configuration from local.te  
  3. checkmodule: policy configuration loaded  
  4. checkmodule: writing binary representation (version 5) to local.mod  
save_snippets.png
[[email protected] log]# checkmodule -m -o local.mod local.te
checkmodule: loading policy configuration from local.te
checkmodule: policy configuration loaded
checkmodule: writing binary representation (version 5) to local.mod

4.5.3 semodule_package

         建立新的模組。
[cpp] view plain copy print?
  1. [[email protected] log]# semodule_package -o local.pp -m local.mod  
save_snippets_01.png
[[email protected] log]# semodule_package -o local.pp -m local.mod

4.5.4 semodule

可以顯示、載入、刪除模組,載入的例子:
[cpp] view plain copy print?
  1. [[email protected] log]# semodule -i local.pp  
save_snippets_01.png
[[email protected] log]# semodule -i local.pp

4.5.5 semanage

        這是一個功能強大的策略管理工具,有了它即使沒有策略的原始碼,也是可以管理安全策略的。因為本文主要是介紹用原始碼來修改策略的,詳細用法大家可以參考它的man頁。

5. 定製策略

    FC4,RHEL4等都是採用策略1.X版本的,並且是提供策略原始碼的RPM包。從FC5開始策略的版本從1.X 升級到2.X。2.X版本的refpolicy(reference policy)最大的一個變化就是引進模組(module)這個概念,同一套策略原始碼就可以支援Multi-LevelSecurity(MLS)和non-MLS。

    標準的FC5裡不提供原始碼的RPM包。FC5提供的audit2allow,semanage,semodule也是可以開發一些簡單的策略模組的。但是,要是做策略模組的開發,增加一個ROLE之類的,最好還是下載refpolicy的原始碼。

5.1 策略原始檔的安裝

      從CVS伺服器下載的原始碼是最新的,如果make時出錯,最好把你係統裡和SELinux有關的那些包更新到最新的狀態。
      每一個模組有3個檔案構成,比如上圖的sudo.fc 就是和 命令sudo相關的檔案的定義標籤,(file context rabel),sudo.te是Type Enforcement定義,包括TE訪問規則等,sudo.if是一個外部模組呼叫這個模組的介面定義。

5.2 給程式定製domain

     開發程式策略的一般步驟
     1) 給檔案,埠之類的object賦予type 標籤
     2) 設定 Type Enforcement (Domain 遷移,訪問許可)
     3) 策略載入
     4) permissive模式下執行程式
     5) 確認日誌,用audit2allow生成訪問許可
     6) 重複1-5動作,直到沒有違反的日誌出現
     7) 切換到enforcing模式,正式運用

     因為我們所常用的那些服務的策略模組都已經有了,修改的時候也比較簡單。在這裡我就舉個一般的例子。用點對點下載的朋友估計都跟我一樣,在Linux上用 azureus,Amule來下載東西吧。

    接下來以azureus為例,介召如何在FC5裡追加一個azureus.pp模組。我們在追加azureus.pp模組之前,azureus是在系統給使用者設好的user_t domain裡執行。

[cpp] view plain copy print?
  1. [[email protected] azureus]$ ps -efZ|grep azureus  
  2. user_u:user_r:user_t fu 1751 1732 0 22:28 pts/3 00:00:00 /bin/bash ./azureus  
save_snippets.png
[[email protected] azureus]$ ps -efZ|grep azureus
user_u:user_r:user_t fu 1751 1732 0 22:28 pts/3 00:00:00 /bin/bash ./azureus
     接下來我們在追加3個檔案。
1) azureus.fc
     在這裡我只定義一個檔案,實際要是真的用的,還要定義azureus_t能寫的目錄等。

[cpp] view plain copy print?
  1. [[email protected] apps]# more azureus.fc  
  2. /home/fu/azureus -- gen_context(user_u:object_r:azureus_exec_t,s0)  
save_snippets.png
[[email protected] apps]# more azureus.fc
/home/fu/azureus -- gen_context(user_u:object_r:azureus_exec_t,s0)
2) azureus.te
[cpp] view plain copy print?
  1. [[email protected] apps]# more azureus.te  
  2. policy_module(azureus,1.0.0)  
  3. type azureus_t;  
  4. type azureus_exec_t;  
  5. role user_r types azureus_t;  
  6. require {  
  7. type user_t;  
  8. };  
  9. domain_type(azureus_t)  
  10. domain_entry_file(azureus_t,azureus_exec_t)  
  11. domain_auto_trans(user_t,azureus_exec_t,azureus_t)  
save_snippets.png
[[email protected] apps]# more azureus.te
policy_module(azureus,1.0.0)
type azureus_t;
type azureus_exec_t;
role user_r types azureus_t;
require {
type user_t;
};
domain_type(azureus_t)
domain_entry_file(azureus_t,azureus_exec_t)
domain_auto_trans(user_t,azureus_exec_t,azureus_t)
3) azureus.if
實際上沒有別的模組要呼叫azureus,所以這個檔案就是空檔案也不要緊。

[cpp] view plain copy print?
  1. [[email protected] apps]# more azureus.if
  2. # policy/modules/apps/azureus.if
  3. ## Myapp example policy
  4. ##
  5. ## Execute a domain transition to run azureus.
  6. ##
  7. ##
save_snippets.png
[[email protected] apps]# more azureus.if
# policy/modules/apps/azureus.if
## Myapp example policy
##
## Execute a domain transition to run azureus.
##
##