技术笔记:SELinux详解

简介: 技术笔记:SELinux详解

系统资源都是通过进程来读取更改的,为了保证系统资源的安全,传统的Linux使用用户、文件权限的概念来限制资源的访问,通过对比进程的发起用户和文件权限以此来保证系统资源的安全,这是一种自由访问控制方式(DAC);但是随着系统资源安全性要求提高,出现了在Linux下的一种安全强化机制(SELinux),该机制为进程和文件加入了除权限之外更多的限制来增强访问条件,这种方式为强制访问控制(MAC)。这两种方式最直观的对比就是,采用传统DAC,root可以访问任何文件,而在MAC下,就算是root,也只能访问设定允许的文件。


一、SELinux简介


SELinux(Secure Enhanced Linux)安全增强的Linux是由美国国家安全局NSA针对计算机基础结构安全开发的一个全新的Linux安全策略机制。SELinux可以允许系统管理员更加灵活的来定义安全策略。


SELinux是一个内核级别的安全机制,从Linux2.6内核之后就将SELinux集成在了内核当中,因为SELinux是内核级别的,所以我们对于其配置文件的修改都是需要重新启动操作系统才能生效的。


现在主流发现的Linux版本里面都集成了SELinux机制,CentOS/RHEL都会默认开启SELinux机制。


工作原理如下图:


二、SELinux基本概念


我们知道,操作系统的安全机制其实就是对两样东西做出限制:进程和系统资源(文件、网络套接字、系统调用等)。


在之前学过的知识当中,Linux操作系统是通过用户和组的概念来对我们的系统资源进行限制,我们知道每个进程都需要一个用户才能执行。


在SELinux当中针对这两样东西定义了两个基本概念:域(domin)和上下文(context)。


工作类型


SELinux下存在不同的规则,SELinux根据不同的工作类型对这些规则打开或关闭(on|off),然后通过规则的开启与关闭具体地限制不同进程对文件的读取。


getsebool -a 或者 sestatus -b # 查看当前工作类型下各个规则的开启与否


setsebool -P 规则名称 【0|1】 # 修改当前工作类型下指定规则的开启关闭,-P表示同时修改文件使永久生效


域就是用来对进行进行限制,而上下文就是对系统资源进行限制。


security context介绍


安全上下文存在于进程与文件中,context随进程一起存入内存中,文件的context存放在其对应的inode中,因此进程在访问文件时,要先读取inode,再判断是否能够访问该文件。


ls -Z # 显示文件的安全上下文


ps -eZ # 显示所有进程的安全上下文


我们可以通过 ps -Z 这命令来查看当前进程的域的信息,也就是进程的SELinux信息:


【root@xiaoluo ~】# ps -Z


LABEL PID TTY TIME CMD


unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2503 pts/0 00:00:00 su


unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2511 pts/0 00:00:00 bash


unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 3503 pts/0 00:00:00 ps


通过 ls -Z 命令我们可以查看文件上下文信息,也就是文件的SELinux信息:


【root@xiaoluo ~】# ls -Z


-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg


drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Desktop


-rw-r--r--+ root root system_u:object_r:admin_home_t:s0 install.log


-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log.syslog


在稍后我们来探讨一下这些字段所代表的含义。


三、策略


在SELinux中,我们是通过定义策略来控制哪些域可以访问哪些上下文。


在SELinux中,预置了多种的策略模式,我们通常都不需要自己去定义策略,除非是我们自己需要对一些服务或者程序进行保护


在CentOS/RHEL中,其默认使用的是目标(target)策略,那么何为目标策略呢?


目标策略定义了只有目标进程受到SELinux限制,非目标进程就不会受到SELinux限制,通常我们的网络应用程序都是目标进程,比如httpd、mysqld,dhcpd等等这些网络应用程序。


我们的CentOS的SELinux配置文件是存放在 /etc/sysconfig/ 目录下的 selinux 文件,我们可以查看一下里面的内容:


【root@xiaoluo ~】# cat /etc/sysconfig/selinux


# 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 - No SELinux policy is loaded.


SELINUX=enforcing


# SELINUXTYPE= can take one of these two values:


# targeted - Targeted processes are protected,


# mls - Multi Level Security protection.


SELINUXTYPE=targeted   // 我们的CentOS使用的策略就是目标策略


四、SELinux模式


SELinux的工作模式一共有三种 enforcing、permissive和disabled


①enforcing  强制模式:只要是违反策略的行动都会被禁止,并作为内核信息记录


②permissive  允许模式:违反策略的行动不会被禁止,但是会提示警告信息


③disabled  禁用模式:禁用SELinux,与不带SELinux系统是一样的,通常情况下我们在不怎么了解SELinux时,将模式设置成disabled,这样在访问一些网络应用时就不会出问题了。


上面也说了SELinux的主配置文件是 /etc/sysconfig/selinux


【root@xiaoluo ~】# cat /etc/sysconfig/selinux


# 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 - No SELinux policy is loaded.


SELINUX=enforcing  //  我们看到SELinux默认的工作模式是enforcing


# SELINUXTYPE= can take one of these two values:


# targeted - Targeted processes //代码效果参考:http://www.lyjsj.net.cn/wx/art_23200.html

are protected,

# mls - Multi Level Security protection.


SELINUXTYPE=targeted


使用命令修改工作模式只在当前有效,想要开机生效,而且如果想要在disabled和其他两种模式之间切换,只有修改配置文件参数然后重启,该配置文件是/etc/selinux/config,另外也可以通过/etc/sysconfig/selinux文件修改,其实该文件是/etc/selinux/config的软链接文件


我们SELinux默认的工作模式是enforcing,我们可以将其修改为 permissive或者是disabled


我们如果要查看当前SELinux的工作状态,可以使用 getenforce 命令来查看:


【root@xiaoluo //代码效果参考:http://www.lyjsj.net.cn/wx/art_23198.html

~】# getenforce

Enforcing


当前的工作模式是 enforcing,我们如果要设置当前的SELinux工作状态,可以使用 setenforce 【0|1】 命令来修改,setenforce 0表示设置成 permissive,1表示enforcing


【注意:】通过 setenforce 来设置SELinux只是临时修改,当系统重启后就会失效了,所以如果要永久修改,就通过修改SELinux主配置文件


【root@xiaoluo ~】# setenforce 0


【root@xiaoluo ~】# getenforce


Permissive


【root@xiaoluo ~】# setenforce 1


【root@xiaoluo ~】# getenforce


Enforcing


我们可以通过 ls -Z 这个命令来查看我们文件的上下文信息,也就是SELinux信息,


【root@xiaoluo ~】# ls -Z


-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg


drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Desktop


-rw-r--r--+ root root system_u:object_r:admin_home_t:s0 install.log


-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log.syslog


我们发现其比传统的 ls 命令多出来了 system_u:object_r:admin_home_t:s0 //代码效果参考:http://www.lyjsj.net.cn/wx/art_23196.html

这个东西,我们现在就来分析一下这段语句所代表的含义

system_u:object_r:admin_home_t:s0


这条语句通过:划分成了四段,第一段 system_u 代表的是用户,第二段 object_r 表示的是角色,第三段是SELinux中最重要的信息,admin_home 表示的是类型,最后一段 s0 是跟MLS、MCS相关的东西,暂时不需要管


①system_u  指的是SElinux用户,root表示root账户身份,user_u表示普通用户无特权用户,system_u表示系统进程,通过用户可以确认身份类型,一般搭配角色使用。身份和不同的角色搭配时有权限不同,虽然可以使用su命令切换用户但对于SElinux的用户并没有发生改变,账户之间切换时此用户身份不变,在targeted策略环境下用户标识没有实质性作用。


②object_r  object_r一般为文件目录的角色、system_r一般为进程的角色,在targeted策略环境中用户的角色一般为system_r。用户的角色类似用户组的概念,不同的角色具有不同的身份权限,一个用户可以具备多个角色,但是同一时间只能使用一个角色。在targeted策略环境下角色没有实质作用,在targeted策略环境中所有的进程文件的角色都是system_r角色。


③admin_home  文件和进程都有一个类型,SElinux依据类型的相关组合来限制存取权限。


五、实例


下面我们通过一个实例来看一下上下文 context 的值和SELinux的访问控制


比如说我搭建好了一个Web服务器,我们知道www服务器其默认网页存放位置是在 /var/www/html 这个目录下,我们如果在这里新建一个 index.html 测试页面,启动我们的www服务器,刷新就能见到其内容了,这时我们如果是在我们的 /home 目录下建立一个 index.html 页面,然后将其移动到 /var/www/html 这个目录下,再刷新页面,其还会不会正常显示呢?


首先我们启动我们的 httpd 服务:


【root@xiaoluo ~】# service httpd restart


Stopping httpd: 【 OK 】


Starting httpd: httpd: apr_sockaddr_info_get() failed for xiaoluo


httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName


【 OK 】


然后打开浏览器,输入我们的 127.0.0.1 来访问,此时看到的界面是Apache的测试界面:


因为我们此时的 /var/www/html 下还不存在任何页面:


【root@xiaoluo home】# ll /var/www/html/


total 0


接下来我们在 /home 目录下建立一个 index.html 的页面,然后将其移动到我们的 /var/www/html 目录下


【root@xiaoluo home】# vi index.html


This is a test about SELinux


【root@xiaoluo home】# mv index.html /var/www/html/


【root@xiaoluo html】# cd /var/www/html/


【root@xiaoluo html】# ls


index.html


此时,按照正常情况,因为html目录下存在了一个index.html的页面,我们此时如果刷新浏览器页面,应该会跳转到index.html页面的


但是事实我们发现,页面还是在这个测试页面,到底是为什么呢?这个就跟我们的SELinux的安全策略有关系了,我们可以去 /var/log/audit 这个目录下查看 audit.log 这个文件,从中找出错误信息


【root@xiaoluo html】# tail /var/log/audit/audit.log


type=CRED_DISP msg=audit(1369575601.957:289): user pid=3637 uid=0 auid=0 ses=44 subj=system_u:system_r:crond_t:s0-s0:c0.c1023 msg='op=PAM:setcred acct="root" exe="/usr/sbin/crond" hostname=? addr=? terminal=cron res=success'


type=USER_END msg=audit(1369575601.957:290): user pid=3637 uid=0 auid=0 ses=44 subj=system_u:system_r:crond_t:s0-s0:c0.c1023 msg='op=PAM:session_close acct="root" exe="/usr/sbin/crond" hostname=? addr=? terminal=cron res=success'


type=AVC msg=audit(1369575729.534:291): avc: denied { getattr } for pid=3619 comm="httpd" path="/var/www/html/index.html" dev=sda2 ino=538738 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file


type=SYSCALL msg=audit(1369575729.534:291): arch=c000003e syscall=4 success=no exit=-13 a0=7f34198634f8 a1=7fffbc87bee0 a2=7fffbc87bee0 a3=7f341985ff60 items=0 ppid=3612 pid=3619 auid=500 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)


type=AVC msg=audit(1369575729.535:292): avc: denied { getattr } for pid=3619 comm="httpd" path="/var/www/html/index.html" dev=sda2 ino=538738 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file


type=SYSCALL msg=audit(1369575729.535:292): arch=c000003e syscall=6 success=no exit=-13 a0=7f34198635c8 a1=7fffbc87bee0 a2=7fffbc87bee0 a3=1 items=0 ppid=3612 pid=3619 auid=500 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)


type=AVC msg=audit(1369575736.549:293): avc: denied { getattr } for pid=3618 comm="httpd" path="/var/www/html/index.html" dev=sda2 ino=538738 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file


type=SYSCALL msg=audit(1369575736.549:293): arch=c000003e syscall=4 success=no exit=-13 a0=7f34198634f8 a1=7fffbc87bee0 a2=7fffbc87bee0 a3=7f341985ff60 items=0 ppid=3612 pid=3618 auid=500 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)


type=AVC msg=audit(1369575736.549:294): avc: denied { getattr } for pid=3618 comm="httpd" path="/var/www/html/index.html" dev=sda2 ino=538738 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file


type=SYSCALL msg=audit(1369575736.549:294): arch=c000003e syscall=6 success=no exit=-13 a0=7f34198635c8 a1=7fffbc87bee0 a2=7fffbc87bee0 a3=1 items=0 ppid=3612 pid=3618 auid=500 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)


从这个日志文件中,我们就可以看到刷新页面不出来index.html的原因就是因为我们的SELinux安全策略所导致的


我们通过 ls -Z 命令先来看看刚移动过来的 index.html 的上下文信息


【root@xiaoluo html】# ls -Z


-rw-r--r--. root root unconfined_u:object_r:home_root_t:s0 index.html


我们发现其第三个字段的类型是 homeroot

相关文章
|
4月前
|
安全 Ubuntu Linux
SELinux 简介
前段时间的工作遇到了一些关于 SELinux 的问题,初次接触不熟悉此概念,导致当时配置策略时束手束脚,焦头烂额,为此去系统的学习了下 SELinux 的东西。聊 SELinux 之前,先来看看什么叫做访问控制。
31 3
SELinux 简介
|
安全 Shell Linux
SELinux 入门 pt.2
SELinux 入门 pt.2
|
安全 Unix Linux
SELinux 入门 pt.1
SELinux 入门 pt.1
|
运维 安全 关系型数据库
是不是都把SELinux给忘了?
是不是都把SELinux给忘了?
|
Web App开发 监控 安全
SELinux初学者指南
SELinux(Security Enhanced Linux)是美国国家安全局2000年发布的一种高级MAC(Mandatory Access Control,强制访问控制)机制,用来预防恶意入侵。SELinux在DAC(Discretionary Access Control,自主访问控制)的基础上实现了强制访问控制,比如读、写和执行权限。
1193 0
|
Web App开发 安全 Linux
|
安全 开发工具 网络安全