天天看點

SElinux詳解

setsebool指令

配置VSFTPD時本地使用者無法切換

用到指令

setsebool -P allow_ftpd_anon_write=1 allow_ftpd_full_access=1 ftp_home_dir=1 ftpd_disable_trans=1 ftpd_is_daemon=1

setsebool指令查詢了相關的資料

是selinux的相關設定項

這是SELinux的設定指令.

其實 man 一下setsebool也知道了:

NAME

setsebool – set SELinux boolean value

在不熟悉SELnux前,把SELinux關掉也可以的。有時間研究下SELinux。

Redhat從FC3開始,就使用了SELinux來增強安全,但是使用起來有時候太繁瑣,就想關閉它,但是如果安裝伺服器的時候開始沒有圖形界面,還真很難找到地方可以關閉。

正确方法如下:修改/etc/selinux/config檔案中的SELINUX=”” 為 disabled ,然後重新開機。

2009年8。22更新

下面是一些收集來的設定指令:

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      

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      

注:使用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 /*是否允許ftp匿名通路*/ 
allow_ftpd_full_access --> off 
httpd_disable_trans --> off /*隻要有disable_trans關閉保護*/      

說明: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

繼續閱讀