SELinux 介绍

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

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

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

SELinux 基本概念

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

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

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

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

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

[root@localhost docker-web-app]# ps -Z
LABEL                              PID TTY          TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1692 pts/0 00:00:00 bash
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 92021 pts/0 00:00:00 ps

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

[root@localhost /]# ls -Z
lrwxrwxrwx. root root system_u:object_r:bin_t:s0       bin -> usr/bin
dr-xr-xr-x. root root system_u:object_r:boot_t:s0      boot
drwxr-xr-x. root root system_u:object_r:default_t:s0   data
drwxr-xr-x. root root system_u:object_r:device_t:s0    dev
drwxr-xr-x. root root system_u:object_r:etc_t:s0       etc
drwxr-xr-x. root root system_u:object_r:home_root_t:s0 home
lrwxrwxrwx. root root system_u:object_r:lib_t:s0       lib -> usr/lib
lrwxrwxrwx. root root system_u:object_r:lib_t:s0       lib64 -> usr/lib64
drwxr-xr-x. root root system_u:object_r:mnt_t:s0       media
drwxr-xr-x. root root system_u:object_r:mnt_t:s0       mnt
drwxr-xr-x. root root system_u:object_r:usr_t:s0       opt
dr-xr-xr-x. root root system_u:object_r:proc_t:s0      proc
dr-xr-x---. root root system_u:object_r:admin_home_t:s0 root
drwxr-xr-x. root root system_u:object_r:var_run_t:s0   run
lrwxrwxrwx. root root system_u:object_r:bin_t:s0       sbin -> usr/sbin
drwxr-xr-x. root root system_u:object_r:var_t:s0       srv
dr-xr-xr-x. root root system_u:object_r:sysfs_t:s0     sys
drwxrwxrwt. root root system_u:object_r:tmp_t:s0       tmp
drwxr-xr-x. root root system_u:object_r:usr_t:s0       usr
drwxr-xr-x. root root system_u:object_r:var_t:s0       var
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依据类型的相关组合来限制存取权限

SELinux 策略

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

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

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

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

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

[root@localhost /]# 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 three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELinux 模式

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

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

  • permissive(允许模式):违反策略的行动不会被禁止,但是会提示警告信息(前期配置服务策略时主要用的是这个模式,等策略配置好后则开启强制模式)

  • disabled(禁用模式):目前市面上的云服务器都是这个模式,很容易导致某个服务被黑后整个系统被接管,禁用SELinux,与不带SELinux系统是一样的

查看 SELinux 的当前模式

[root@localhost /]# getenforce
Enforcing

临时开启/关闭 SELinux

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

setenforce [0|1]
  • setenforce 0表示设置成 permissive
  • setenforce 1表示设置成 enforcing
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部