selinux及設置

下面是一些 收集來的 設置命令
===ftp===
//If you want to share files anonymously
chcon -R -t public_content_t /var/ftp
//If you want to setup a directory where you can upload files
chcon -t public_content_rw_t /var/ftp/incoming
You must also turn on the boolean allow_ftpd_anon_write
setsebool -P allow_ftpd_anon_write=1
//If you are setting up this machine as a ftpd server and wish to allow users to access their home directorories
setsebool -P ftp_home_dir 1
//If you want to run ftpd as a daemon
setsebool -P ftpd_is_daemon 1
//You can disable SELinux protection for the ftpd daemon
setsebool -P ftpd_disable_trans 1

===httpd===
//If you want a particular domain to write to the public_content_rw_t domain
setsebool -P allow_httpd_anon_write=1
or
setsebool -P allow_httpd_sys__anon_write=1
//httpd can be setup to allow cgi s to be executed
setsebool -P httpd_enable_cgi 1
//If you want to allow access to users home directories
setsebool -P httpd_enable_homedirs 1
chcon -R -t httpd_sys_content_t ~user/public_html
//httpd is allowed access to the controling terminal
setsebool -P httpd_tty_comm 1
//such that one httpd service can not interfere with another
setsebool -P httpd_unified 0
//loadable modules run under the same context as httpd
setsebool -P httpd_builtin_ing 0
//httpd s are allowed to connect out to the network
setsebool -P httpd_can_network_connect 1
// You can disable suexec transition
setsebool -P httpd_suexec_disable_trans 1
//You can disable SELinux protection for the httpd daemon by executing
setsebool -P httpd_disable_trans 1
service httpd restart

===named===
//If you want to have named update the master zone files
setsebool -P named_write_master_zones 1
//You can disable SELinux protection for the named daemon by executing
setsebool -P named_disable_trans 1
service named restart

===nfs===
//If you want to setup this machine to share nfs partitions read only
setsebool -P nfs_export_all_ro 1
//If you want to share files read/write
setsebool -P nfs_export_all_rw 1
//If you want to use a remote NFS server for the home directories on this machine
setsebool -P use_nfs_home_dirs 1

===samba===
//If you want to share files other than home directorie
chcon -t samba_share_t /directory
//If you want to share files with multiple domains
setsebool -P allow_smbd_anon_write=1
//If you are setting up this machine as a Samba server and wish to share the home directories
setsebool -P samba_enable_home_dirs 1
//If you want to use a remote Samba server for the home directories on this machine
setsebool -P use_samba_home_dirs 1
//You can disable SELinux protection for the samba daemon by executing
setsebool -P smbd_disable_trans 1
service smb restart

===rsync===
//If you want to share files using the rsync daemon
chcon -t public_content_t /directories
//If you want to share files with multiple domains
setsebool -P allow_rsync_anon_write=1
//You can disable SELinux protection for the rsync daemon by executing
setsebool -P rsync_disable_trans 1

===kerberos===
//allow your system to work properly in a Kerberos environment
setsebool -P allow_kerberos 1
//If you are running Kerberos daemons kadmind or krb5kdc
setsebool -P krb5kdc_disable_trans 1
service krb5kdc restart
setsebool -P kadmind_disable_trans 1
service kadmind restart

===nis===
Allow your system to work properly in a NIS environment
setsebool -P allow_ypbind 1

 

1.1 SElinux概述
SELinux(Security-Enhanced Linux) 是美國國家安全局(NAS)對於強制訪問控 制的實現,在這種訪問控制體系的限制下,進程只能訪問那些在他的任務中所需要文件。大部分使用 SELinux 的人使用的都是SELinux就緒的發行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它們都是在內核中啓用 SELinux 的,並且提供一個可定製的安全策略,還提供很多用戶層的庫和工具,它們都可以使用 SELinux 的功能。
1.1.1 SElinux特點
1.MAC
對訪問的控制徹底化,對所有的文件、目錄、端口的訪問都是基於策略設定的,可由管理員時行設定。
2.RBAC
對於用戶只賦予最小權限。用戶被劃分成了一些role(角色),即使是root用戶,如果不具有sysadm_r角色的話,也不是執行相關的管理。哪裏role可以執行哪些domain,也是可以修改的。
3.安全上下文
當啓動selinux的時候,所有文件與對象都有安全上下文。進程的安全上下文是域,安全上下文由用戶:角色:類型表示。
(1)系統根據pam子系統中的pam_selinux.so模塊設定登錄者運行程序的安全上下文
(2)rpm包安裝會根據rpm包內記錄來生成安全上下文,
(3)如果是手工他建的,會根據policy中規定來設置安全上下文,
(4)如果是cp,會重新生成安全上下文。
(5)如果是mv,安全上下文不變。
1.1.2 安全上下文格式
安全上下文由user:role:type三部分組成,下面分別說明其作用:
1.user identity:類似linux系統中的UID,提供身份識別,安全上下文中的一部分。
三種常見的user:
user_u-:   普通用戶登錄系統後預設;
system_u-:開機過程中系統進程的預設;
root-:    root登錄後預設;
在targeted policy中users不是很重要;
在strict policy中比較重要,的有預設的selinux users都以 "_u"結尾,root除外。
2.role
件與目錄的role,通常是object_r;
程序的role,通常是system_r;
用戶的role,targeted policy爲system_r;
strict policy爲sysadm_r,staff_r,user_r
用戶的role,類似於系統中的GID,不同的角色具備不同的權限;用戶可以具備多個role;但是同一時間內只能使用一role;
role是RBAC的基礎;
3.type
type:用來將主體與客體劃分爲不同的組,組每個主體和系統中的客體定義了一個類型;爲進程運行提供最低的權限環境。
當一個類型與執行的進程關聯時,該type也稱爲domain,也叫安全上下文。
域或安全上下文是一個進程允許操作的列表,決字一個進程可以對哪種類型進行操作。
1.1.3 SElinux配置文件
vi /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 - 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
#SELINUX有「disabled」「permissive」,「enforcing」3種選擇。
1.模式的設置
enforcing:強制模式,只要selinux不允許,就無法執行
permissive:警告模式,將該事件記錄下來,依然允許執行
disabled:關閉selinux;停用,啓用需要重啓計算機。

2.策略的設置
targeted:保護常見的網絡服務,是selinux的默認值;
stric:提供RBAC的policy,具備完整的保護功能,保護網絡服務,一般指令及應用程序。
策略改變後,需要重新啓動計算機。
也可以通過命令來修改相關的具體的策略值,也就是修改安全上下文,來提高策略的靈活性。

3.策略的位置
/etc/selinux/<策略名>/policy/

1.2 SElinux命令介紹
1.2.1 查詢SElinux狀態命令
1.查詢selinux狀態
[root@redhat ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 21
Policy from config file:        targeted

2.查詢selinux激活狀態
[root@redhat ~]# selinuxenabled
[root@redhat ~]# echo $?
0
如果爲-256爲非激活狀態。
1.2.2 切換SElinux類型
1.切換成警告模式
[root@redhat ~]# setenforce 0或setenforce permissive
[root@redhat ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive
Mode from config file:          enforcing
Policy version:                 21
Policy from config file:        targeted

[root@redhat ~]# getenforce
Permissive

2.切換成強制模式
[root@redhat ~]# setenforce 1
[root@redhat ~]# getenforce
Enforcing
注:使用setenforce切換enforcing與permissive模式不需要重啓計算機。
1.2.3 檢查安全上下文
1.檢查帳號的安全上下文
[root@redhat ~]# id -Z
root:system_r:unconfined_t:SystemLow-SystemHigh

2.檢查進程的安全上下文
[root@redhat home]# ps -Z
LABEL                             PID TTY          TIME CMD
root:system_r:unconfined_t:SystemLow-SystemHigh 2383 pts/0 00:00:00 bash
root:system_r:unconfined_t:SystemLow-SystemHigh 2536 pts/0 00:00:00 ps

3.檢查文件與目錄的安全上下文
[root@redhat home]# ls -Z
drwx------  tom  tom  system_u:object_r:user_home_dir_t tom
1.2.4 修改文件/目錄安全上下文與策略
1.chcon命令
chcon -u [user]  對象
      -r [role]
      -t [type]
      -R 遞歸
示例:
chcon -R -t samba_share_t /tmp/abc
:安全上下文的簡單理解說明,受到selinux保護的進程只能訪問標識爲自己只夠訪問的安全上下文的文件與目錄。
例如:上面解釋爲使用smb進程能夠訪問/tmp/abc目錄而設定的安全上下文。

2.getsebool命令
獲取本機selinux策略值,也稱爲bool值。
getsebool -a  命令同sestatus -b
[root@redhat files]# getsebool -a
NetworkManager_disable_trans --> off
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
allow_daemons_use_tty --> off
allow_execheap --> off
allow_execmem --> on
allow_execmod --> off
allow_execstack --> on
allow_ftpd_anon_write --> off  
allow_ftpd_full_access --> off
...
httpd_disable_trans --> off   

說明:selinux的設置一般通過兩個部分完成的,一個是安全上下文,另一個是策略,策略值是對安全上下文的補充。

3.setsebool命令
setsebool -P allow_ftpd_anon_write=1
-P 是永久性設置,否則重啓之後又恢復預設值。
例:
[root@redhat files]# setsebool -P allow_ftpd_anon_write=1
[root@redhat files]# getsebool allow_ftpd_anon_write
allow_ftpd_anon_write --> on
說明:如果僅僅是安全上下文中設置了vsftpd進程對某一個目錄的訪問,配置文件中也允許可寫,但是selinux中策略中不允許可寫,仍然不可寫。所以基於selinux保護的服務中,安全性要高於很多。

1.3 SElinux應用
selinux的設置分爲兩個部分,修改安全上下文以及策略,下面收集了一些應用的安全上下文,供配置時使用,對於策略的設置,應根據服務應用的特點來修改相應的策略值。
1.3.1 SElinux與samba
1.samba共享的文件必須用正確的selinux安全上下文標記。
chcon -R -t samba_share_t /tmp/abc
果共享/home/abc,需要設置整個主目錄的安全上下文。
chcon -R -r samba_share_t /home
2.修改策略(只對主目錄的策略的修改)
setsebool -P samba_enable_home_dirs=1
setsebool -P allow_smbd_anon_write=1
getsebool 查看
samba_enable_home_dirs -->on
allow_smbd_anon_write --> on

1.3.2 SElinux與nfs
selinux對nfs的限制好像不是很嚴格,默認狀態下,不對nfs的安全上下文進行標記,而且在默認狀態的策略下,nfs的目標策略允許nfs_export_all_ro
nfs_export_all_ro
nfs_export_all_rw值爲0
所以說默認是允許訪問的。
但是如果共享的是/home/abc的話,需要打開相關策略對home的訪問。
setsebool -P use_nfs_home_dirs boolean 1
getsebool use_nfs_home_dirs

1.3.3 SElinux與ftp
1果ftp爲匿名用戶共享目錄的話,應修改安全上下文。
chcon -R -t public_content_t /var/ftp
chcon -R -t public_content_rw_t /var/ftp/incoming

2.策略的設置
setsebool -P allow_ftpd_anon_write =1
getsebool allow_ftpd_anon_write
allow_ftpd_anon_write--> on

1.3.4 SElinux與http
apache的主目錄如果修改爲其它位置,selinux就會限制客戶的訪問。
1.修改安全上下文:
chcon -R -t httpd_sys_content_t /home/html
由於網頁都需要進行匿名訪問,所以要許匿名訪問。
2.改策略:
setsebool -P allow_ftpd_anon_write = 1
setsebool -P allow_httpd_anon_write = 1
setsebool -P allow_<協議名>_anon_write = 1
閉selinux對httpd的保護
httpd_disable_trans=0

1.3.5 SElinux與公共目錄共享
如果ftp,samba,web都訪問共享目錄的話,該文件的安全上下文應爲:
public_content_t
public_content_rw_t
其它各服務的策略的bool值,應根據具體情況做相應的修改。

1.3.6 SElinux配置總結
以上內容的selinux的配置實驗還需要進行相關驗證,以便在實際環境中能夠直接應用,相關的內容還需要繼續補充。
對於多於牛毛的策略,可以用過濾還查看一個服務相當開啓哪些策略。
發佈了1 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章