SELinux是 Security Enhanced Linux 的缩写,字面上的意义就是安全强化的 Linux 之意,其使用所谓的委任式访问控制 (Mandatory Access Control, MAC) ,当初设计的目标:避免资源的误用,他可以针对特定的程序与特定的档案资源来进行权限的控管! 也就是说,即使你是 root ,那么在使用不同的程序时,你所能取得的权限并不一定是 root ,而得要看当时该程序的设定而定。
<code> SELinux 的运行模式:</code>
主体 (Subject):
SELinux 主要想要管理的就是程序,因此你可以将主体与 process 划上等号;
目标 (Object):
主体程序能否存取的目标资源一般就是文件系统。因此这个目标项目可以等文件系统划上等号;
政策 (Policy):
由於程序与文件数量庞大,因此 SELinux 会依据某些服务来制订基本的存取安全性政策。这些政策内还会有详细的守则 (rule) 来指定不同的服务开放某些资源的存取与否。在目前的 CentOS 5.x 里面仅有提供两个主要的政策,分别是:
targeted:针对网络服务限制较多,针对本机限制较少,是默认的政策;
strict:完整的 SELinux 限制,限制方面较为严格。
centos6.X默认使用的是 targeted 政策。
安全上下文 (security context):
有点类似rwx权限一样的东西,主体与目标的安全上下文一致才能访问,意即程序与文件的安全上下文一致才能执行文件。
使用ls -Z可以查看文件的安全上下文:
<code> [root@www www]# ls -lZ drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 manual </code>
安全上下文主要分为这三个:
Identify:role:type
身份识别:角色:类型
身份识别 (Identify):
相当于帐号方面的身份识别!主要的身份识别则有底下三种常见的类型:
<code> root:表示 root 的帐号身份,如同上面的表格显示的是 root 家目录下的数据啊! system_u:表示系统程序方面的识别,通常就是程序罗; user_u:代表的是一般使用者帐号相关的身份。 </code>
还有其他的一些,这个不太重要!
系统上面大部分的数据都会是 system_u 或 root ,如果是在 /home 底下的数据,那么大部分应该就会是 user_u !
角色 (Role):
透过角色栏位,我们可以知道这个数据是属於程序、文件资源还是代表使用者。一般的角色有:
object_r:代表的是文件或目录等文件资源,这应该是最常见的罗;
system_r:代表的就是程序啦!不过,一般使用者也会被指定成为 system_r 喔!
你也会发现角色的栏位最后面使用 _r 来结尾!因为是 role 的意思嘛!
类型 (Type) :(最重要!)
在默认的 targeted 政策中, Identify 与 Role 栏位基本上是不重要的!重要的在於这个类型 (type) 栏位! 基本上,一个主体程序能不能读取到这个文件资源,与类型栏位有关!而类型栏位在文件与程序的定义不太相同,分别是:
type:在文件资源 (Object) 上面称为类型 (Type);
domain:在主体程序 (Subject) 则称为领域 (domain) 了!
domain 需要与 type 搭配,则该程序才能够顺利的读取文件资源啦!domain与type就相当于省与市的概念啦!!!
所以上面的安全上下文可以这么理解,身份都为system_u,所以都是系统用户,角色都为 object_r,都是文件,而cgi-bin的类型为httpd_sys_script_exec_t:s0,其他文件的类型都为httpd_sys_content_t:s0 ,他们都是httpd这个域可以读取的类型。
<code> SELinux有三种模式: enforcing:强制模式,代表 SELinux 运行中,且已经正确的开始限制 domain/type 了; permissive:宽容模式:代表 SELinux 运行中,不过仅会有警告信息并不会实际限制 domain/type 的存取。这种模式可以运来作为 SELinux 的 debug 之用; disabled:关闭,SELinux 并没有实际运行。 </code>
与SELinux服务相关的命令都是以se开头的。
<code> [root@www selinux]# sestatus #查看selinux的状态 SELinux status: enabled SELinuxfs mount: /selinux Current mode: permissive Mode from config file: permissive Policy version: 24 Policy from config file: targeted [root@www selinux]# setenforce 1 #开启selinux,0为宽容模式,1为强制模式 [root@www selinux]# getenforce #查看selinux, 为开启状态 Enforcing [root@www selinux]# setenforce 0 [root@www selinux]# getenforce Permissive </code>
上面的设置只是临时有效,要想永久生效,则需编辑其配置文件/etc/selinux/config,
<code> [root@www selinux]# vim /etc/selinux/config 1 2 # This file controls the state of SELinux on the system. 3 # SELINUX= can take one of these three values: 4 # enforcing - SELinux security policy is enforced. 5 # permissive - SELinux prints warnings instead of enforcing. 6 # disabled - No SELinux policy is loaded. 7 SELINUX=permissive -------->selinux状态 8 # SELINUXTYPE= can take one of these two values: 9 # targeted - Targeted processes are protected, 10 # mls - Multi Level Security protection. 11 SELINUXTYPE=targeted ------->selinu默认政策 12 [root@www ~]# ls -Z feiyu.sql -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 feiyu.sql [root@www ~]# cp feiyu.sql /tmp/ [root@www ~]# ls -ldZ /tmp/ /tmp/feiyu.sql drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp/ -rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/feiyu.sql [root@www ~]# mv /tmp/feiyu.sql /etc/ [root@www ~]# ls -ldZ /etc/ /etc/feiyu.sql drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc/ -rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /etc/feiyu.sql </code>
以上例子证明,使用cp会改变安全上下文的类型,但是使用mv会将安全上下文一起移动。
使用chcon修改安全上下文的类型:
<code> [root@www ~]# chcon [-R] [-t type] [-u user] [-r role] 文件 [root@www ~]# chcon [-R] --reference=范例档 文件 选项与参数: -R :连同该目录下的次目录也同时修改; -t :后面接安全性本文的类型栏位!例如 httpd_sys_content_t ; -u :后面接身份识别,例如 system_u; -r :后面街角色,例如 system_r; --reference=范例档:拿某个文件当范例来修改后续接的文件的类型! [root@www ~]# ls -ldZ /tmp/ /tmp/feiyu.sql drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp/ -rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/feiyu.sql [root@www ~]# chcon -t tmp_t /tmp/feiyu.sql [root@www ~]# ls -ldZ /tmp/ /tmp/feiyu.sql drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp/ -rw-r--r--. root root unconfined_u:object_r:tmp_t:s0 /tmp/feiyu.sql </code>
使用restorecon恢复默认的安全上下文:
[root@www ~]# restorecon [-Rv] 文件或目录
选项与参数:
-R :连同次目录一起修改;
-v :将过程显示到萤幕上
<code> [root@www ~]# ls -lZ feiyu.sql -rw-r--r--. root root unconfined_u:object_r:tmp_t:s0 feiyu.sql [root@www ~]# restorecon feiyu.sql [root@www ~]# ls -lZ feiyu.sql -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 feiyu.sql </code>
政策查询:
[root@www ~]# seinfo [-Atrub]
选项与参数:
-A :列出 SELinux 的状态、守则布尔值、身份识别、角色、类别等所有资讯
-t :列出 SELinux 的所有类别 (type) 种类
-r :列出 SELinux 的所有角色 (role) 种类
-u :列出 SELinux 的所有身份识别 (user) 种类
-b :列出所有守则的种类 (布尔值)
<code> [root@www ~]# seinfo Statistics for policy file: /etc/selinux/targeted/policy/policy.21 Policy Version & Type: v.21 (binary, MLS) <==列出政策所在档与版本 Classes: 61 Permissions: 220 Types: 1521 Attributes: 155 Users: 3 Roles: 6 Booleans: 213 Cond. Expr.: 190 Sensitivities: 1 Categories: 1024 Allow: 86561 Neverallow: 0 Auditallow: 34 Dontaudit: 5460 Role allow: 5 Role trans: 0 </code>
# 从上面我们可以看到这个政策是 targeted ,此政策的安全性本文类别有 1521 个;
# 而针对网络服务的守则 (Booleans) 共制订了 213 条守则!
<code> [root@www ~]# seinfo -b | grep httpd #查询与httpd相关的安全上下文 Rule loading disabled allow_httpd_mod_auth_pam allow_httpd_bugzilla_script_anon_write httpd_enable_ftp_server </code>
布尔值的查询与修改:
通过上面知道了,其实 Subject 与 Object 能否有存取的权限,是与布尔值有关的, 那么系统有多少布尔值可以透过 seinfo -b 来查询,但,每个布尔值是启动的还是关闭的呢?这就来查询看看吧:
<code> [root@www ~]# getsebool [-a] [布尔值条款] </code>
选项与参数:
-a :列出目前系统上面的所有布尔值条款配置为开启或关闭值
<code> [root@www ~]# getsebool -a NetworkManager_disable_trans --> off allow_console_login --> off allow_cvs_read_shadow --> off allow_daemons_dump_core --> on </code>
[root@www ~]# setsebool [-P] 布林值=[0|1]
选项与参数:
-P :直接将配置值写入配置档,该配置数据未来会生效的!
范例一:查询 httpd_enable_homedirs 是否为关闭,若不为关闭,请关闭他!
<code> [root@www ~]# getsebool httpd_enable_homedirs httpd_enable_homedirs --> on <==结果是 on ,依题意给他关闭! [root@www ~]# setsebool -P httpd_enable_homedirs=0 [root@www ~]# getsebool httpd_enable_homedirs httpd_enable_homedirs --> off</code>
这个 setsebool 最好记得一定要加上 -P 的选项!因为这样才能将此配置写入配置档! 这是非常棒的工具组!你一定要知道如何使用 getsebool 与 setsebool 才行!