SELinux 基础

SELinux 的目的是最小化进程的权限,即让进程在受限环境下运行,限制进程可以访问的资源。在 RHEL/CentOS 系统中默认使用的 targeted 策略为所有进程开放所有权限,仅限制少数服务进程的权限。但软件的开发者或系统管理员也可以人为的增加 SELinux 规则从而限制某些软件的权限,比如 Ceph 的开发者就为 Ceph 增加了很多限制。

SELinux 非常复杂,通常来说我们要解决的多是进程权限不够等问题,比如 nginx 无法访问非特定目录下的文件,无法绑定自定义端口等,因此对 SELinux 并不需要有深入的理解,大概了解 SELinux 的原理就可以了。

名词解释

SELinux

Security-Enhanced Linux;

DAC

Discretionary Access Control,即传统的 rwx 权限控制;

MAC

Mandatory Access Control,SELinux 实现的基于访问策略的权限控制,MAC 的检测在 DAC 检测之后,因此 SELinux 是对传统 DAC 权限访问控制系统的一种补充;

subject

SELinux 中的进程(process);

object

SELinux 中的文件(包括文件夹、设备文件等);

SELinux context

SELinux 对系统中所有的进程和文件都打上标签(即 SELinux context),标签由 SELinux user:role:type:[level] 四个字段组成,SELinux 感知的命令(如 ls、ps 等),可以通过 -Z 选项查看关联的标签,如:

~# ls -d -Z /etc/nginx/
drwxr-xr-x. root root system_u:object_r:httpd_config_t:s0 /etc/nginx/
~# ps auxZ | grep nginx
system_u:system_r:httpd_t:s0    root      1986  0.0  0.0  46308  1116 ?        Ss   Dec15   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
system_u:system_r:httpd_t:s0    nginx     1987  0.0  0.0  49060  2660 ?        S    Dec15   0:00 nginx: worker process
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 27779 0.0  0.0 112664 972 pts/2 S+ 22:14   0:00 grep --color=auto nginx

SELinux context type

SELinux context 中的第三个字段,SELinux 基于 SELinux context type 定义访问策略(允许/拒绝),从而实现对进程与文件间及进程与进程间的访问权限进行控制;

process domain

即进程 SELinux context type;

file type

即文件 SELinux context type;

domain transition

默认情况下子进程继承父进程的 process domain,通过定义 domain transition 规则在运行可执行程序创建子进程时将子进程的 process domain 切换为另一个 domain;

targeted policy

一组 SELinux 策略,RHEL/CentOS 系统默认启用该 SELinux 策略,只有少数的服务进程默认在受限(confined)环境下运行,其它进程都在非受限(unconfined)环境下运行;

SELinux 相关软件包

RHEL/CentOS 默认安装的软件包

policycoreutils

secon, fixfiles, genhomedircon, load_policy, restorecon, semodule, sestatus, setfiles, setsebool

selinux-policy-targeted

SELinux targeted policy

libselinux-utils

avcstat, getenforce, getsebool, matchpathcon, selabel_digest, selabel_lookup, selabel_lookup_best_match, selabel_partial_match, selinux_restorecon, selinuxconlist, selinuxdefcon, selinuxenabled, selinuxexeccon, setenforce

RHEL/CentOS 非默认安装的软件包

setools-console

findcon, sechecker, sediff, seinfo, sesearch

policycoreutils-python

semanage, audit2allow, audit2why, chcat, semodule_package, sandbox

checkpolicy

checkmodule, checkpolicy, sedismod, sedispol

SELinux 文件

所有 SELinux 相关的信息都保存在 /etc/selinux/ 目录及其子目录下。

所有定义的 file context,两者的内容是一样的:

/etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/modules/active/file_contexts

已加载的规则模块:

/etc/selinux/targeted/modules/active/modules/

加载后的模块机器重启后仍然会被加载,注意它与原始的 pp 规则模块的区别,一旦加载之后,会拷贝一份到这个目录,而原始的 pp 规则模块就可以被删除了。

注意:CentOS 从内核 3.10.0-693.21.1 开始 /etc/selinux/targeted 的目录结构有所变化,将原有的 /etc/selinux/targeted/modules/active/ 目录和 /etc/selinux/targeted/active/ 目录进行了合并,原有的 /etc/selinux/targeted/modules/ 目录及其子目录下不再有任何有意义的内容,且以前的 bzip2 压缩的 .pp 文件变成了 bzip2 压缩的 hll 文件,解压之后实质仍然是 sedismod 可以识别的策略文件。

参考文献

SELINUX USER’S AND ADMINISTRATOR’S GUIDE

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/selinux_users_and_administrators_guide/

SELinux cheatsheet

https://www.whitewinterwolf.com/posts/2017/09/08/selinux-cheatsheet/

An Introduction to SELinux on CentOS 7

https://www.digitalocean.com/community/tutorial_series/an-introduction-to-selinux-on-centos-7

A collection of notes on SElinux

http://equivocation.org/selinux

Breaking the Ice with SELinux

http://haifux.org/lectures/200/selinux.pdf

SELINUX FOR MERE MORTALS

https://people.redhat.com/tcameron/Summit2012/SELinux/cameron_w_120_selinux_for_mere_mortals.pdf

SELinux with cp/mv

https://danwalsh.livejournal.com/56534.html

Attributes make writing SELinux policy easier

https://danwalsh.livejournal.com/77728.html

Types and Type Attributes

http://www.cse.psu.edu/~trj1/cse544-s11/slides/06-Types&Attribs.pdf

PolicyLanguage

http://selinuxproject.org/page/PolicyLanguage


最后修改于 2019-01-17