SElinux详解

来源:互联网 发布:少儿编程哪家好 编辑:程序博客网 时间:2024/06/09 16:15

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 anonymouslychcon -R -t public_content_t /var/ftp//If you want to setup a directory where you can upload fileschcon -t public_content_rw_t /var/ftp/incomingYou must also turn on the boolean allow_ftpd_anon_writesetsebool -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 directororiessetsebool -P ftp_home_dir 1//If you want to run ftpd as a daemonsetsebool -P ftpd_is_daemon 1//You can disable SELinux protection for the ftpd daemonsetsebool -P ftpd_disable_trans 1

httpd相关

//If you want a particular domain to write to the public_content_rw_t domainsetsebool -P allow_httpd_anon_write=1orsetsebool -P allow_httpd_sys__anon_write=1//httpd can be setup to allow cgi s to be executedsetsebool -P httpd_enable_cgi 1//If you want to allow access to users home directoriessetsebool -P httpd_enable_homedirs 1chcon -R -t httpd_sys_content_t ~user/public_html//httpd is allowed access to the controling terminalsetsebool -P httpd_tty_comm 1//such that one httpd service can not interfere with anothersetsebool -P httpd_unified 0//loadable modules run under the same context as httpdsetsebool -P httpd_builtin_ing 0//httpd s are allowed to connect out to the networksetsebool -P httpd_can_network_connect 1// You can disable suexec transitionsetsebool -P httpd_suexec_disable_trans 1//You can disable SELinux protection for the httpd daemon by executingsetsebool -P httpd_disable_trans 1service httpd restart

named相关

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

nfs相关

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

samba相关

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

rsync相关

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

kerberos相关

//allow your system to work properly in a Kerberos environmentsetsebool -P allow_kerberos 1//If you are running Kerberos daemons kadmind or krb5kdcsetsebool -P krb5kdc_disable_trans 1service krb5kdc restartsetsebool -P kadmind_disable_trans 1service kadmind restart

nis相关

Allow your system to work properly in a NIS environmentsetsebool -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

1.3.5 SElinux与公共目录共享
如果ftp,samba,web都访问共享目录的话,该文件的安全上下文应为:
public_content_t
public_content_rw_t
其它各服务的策略的bool值,应根据具体情况做相应的修改。

1.3.6 SElinux配置总结
以上内容的selinux的配置实验还需要进行相关验证,以便在实际环境中能够直接应用,相关的内容还需要继续补充。
对于多于牛毛的策略,可以用过滤还查看一个服务相当开启哪些策略。

原创粉丝点击