SELinux 使用一套规则-合称为策略-来授权或禁止操作。这些规则较难创建。幸好,有两种标准策略(针对-targeted 和 严格-strict)可以避免大量的配置工作。
在 SELinux 中,权限的管理完全不同于传统的 Unix 系统。进程的权限取决于它的安全上下文。上下文由唤起该进程的用户身份定义,即该时刻用户具有的角色和域。权限实际上取决于域,但是在域之间转换由角色控制。角色之间的转换取决于身份。
图 14.3. 安全上下文和 Unix 用户
实际上,在登录期间,用户被赋予默认的安全上下文(取决于它们的角色)。这定义了当前域,以及所有新的子进程的域。如果想要改变当前角色和相关联的域,就必须调用 (一个指定角色通常只能被赋予一个域,-t
参数可以忽略)。该命令需要口令授权。这个特性可以禁止程序自动切换角色。在 SELinux 策略中,这种改变只能是显式的。
Obviously the rights do not apply to all objects (files, directories, sockets, devices, etc.). They can vary from object to object. To achieve this, each object is associated to a type (this is known as labeling). Domains’ rights are thus expressed with sets of (dis)allowed operations on those types (and, indirectly, on all objects which are labeled with the given type).
补充域和类型等效
Internally, a domain is just a type, but a type that only applies to processes. That’s why domains are suffixed with _t
just like objects’ types.
By default, a program inherits its domain from the user who started it, but the standard SELinux policies expect many important programs to run in dedicated domains. To achieve this, those executables are labeled with a dedicated type (for example ssh
is labeled with ssh_exec_t
, and when the program starts, it automatically switches to the ssh_t
domain). This automatic domain transition mechanism makes it possible to grant only the rights required by each program. It is a fundamental principle of SELinux.
图 14.4. 域之间的自动转换
实践找到安全上下文
要找到指定进程的安全上下文,就要在 ps
命令中使用 Z
选项。
第一个区段包含了身份,角色,域和 MCS 等级,使用冒号分隔。MCS 等级(多类别安全-Multi-Category Security)是一个参数,它会影响保护策略的机密性,进而基于敏感性管制对文件的访问。本书中,此特性将不做详细解释。
要在命令行中显示当前的安全上下文,应当调用 id -Z
。
$
要显示赋予文件的类型,使用 ls -Z
。
$
注意要将身份和角色赋予不是特别重要的文件(从未使用过),但是为了统一,所有的对象都被赋予完整的安全上下文。
SELinux 支持嵌入到 Debian 提供的标准内核中。Unix 核心工具无需修改就支持 SELinux。因此,启用 SELinux 也相对容易。
The apt install selinux-basics selinux-policy-default
command will automatically install the packages required to configure an SELinux system.
CAUTION Reference policy not in jessie
Unfortunately the maintainers of the refpolicy source package did not handle release critical bugs on their package and the package got removed from jessie. This means that the selinux-policy-* packages are currently not installable in jessie and need to be fetched from another place. Hopefully they will come back in one of the point releases or in jessie-backports. In the meantime, you can grab them from unstable.
This sad situation at least proves that SELinux is not very popular in the set of users/developers who are running the development versions of Debian. Thus, if you opt to use SELinux, you should expect the default policy to not work perfectly and you will have to invest quite some time to make it suitable to your specific needs.
The selinux-policy-default package contains a set of standard rules. By default, this policy only restricts access for a few widely exposed services. The user sessions are not restricted and it is thus unlikely that SELinux would block legitimate user operations. However, this does enhance the security of system services running on the machine. To setup a policy equivalent to the old “strict” rules, you just have to disable the unconfined
module (modules management is detailed further in this section).
The SELinux system is now ready. To enable it, you should add the selinux=1 security=selinux
parameter to the Linux kernel. The audit=1
parameter enables SELinux logging which records all the denied operations. Finally, the enforcing=1
parameter brings the rules into application: without it SELinux works in its default permissive mode where denied actions are logged but still executed. You should thus modify the GRUB bootloader configuration file to append the desired parameters. One easy way to do this is to modify the GRUB_CMDLINE_LINUX
variable in /etc/default/grub
and to run update-grub
. SELinux will be active after a reboot.
注意在下次启动时,用 selinux-activate
脚本自动化这些操作并强制标识(避免无标识的文件在 SELinux 未生效或正在标识的时候被创建)。
SELinux 策略是一系列的模块化规则集合,安装时它会基于已经安装的服务自动探测并启用相关模块。系统立即可操作。然而,如果一个服务是在 SELinux 策略之后安装的,就要手动启用相应模块了。semodule
命令可以实现该目的。还必须用 semanage
命令,定义每个用户可用的角色。
这两个命令可以用于修改当前 SELinux 存储在 /etc/selinux/default/
的配置。不像其他配置文件,可以在 /etc/
中的其他配置文件,这些文件不能手工修改。应当使用相应的程序来修改。
进阶更多文档
由于 NSA 没有提供任何官方文档,社区设置了 wiki 来弥补。里面收集了很多信息,但是要意识到大多数 SELinux 贡献者是 Fedora 用户(其中 SELinux 默认启用)。因此,文档趋于处理该发行版的问题。
→
应该看一看 Debian 中专门的 wiki 页面和 Russell Coker 的博客,他是 Debian 上致力于 SELinux 最活跃的开发者。
→ http://wiki.debian.org/SELinux
→
14.5.3.1. 管理 SELinux 模块
Available SELinux modules are stored in the /usr/share/selinux/default/
directory. To enable one of these modules in the current configuration, you should use semodule -i *module.pp.bz2*
. The pp.bz2 extension stands for policy package (compressed with bzip2).
Removing a module from the current configuration is done with semodule -r *module*
. Finally, the semodule -l
command lists the modules which are currently installed. It also outputs their version numbers. Modules can be selectively enabled with semodule -e
and disabled with semodule -d
.
#
semodule
立即加载新配置,除非使用了 -n
选项。需要注意的是在当前配置默认启用的程序(由在 /etc/selinux/config
文件中的 SELINUXTYPE
变量指明),可以使用 -s
选项来修改和储存其他的。注意不同版本支持的选项可能不同。
14.5.3.2. 管理身份
每次用户登录,就会被赋予一个 SELinux 身份。该身份定义了他们可以使用的角色。这两种映射(从用户到身份,从该身份到角色)使用 semanage
命令配置。
一定要阅读 semanage(8) 手册页,即使命令语法和所管理的所有概念相似。你会发现所有子命令的通用选项:-a
添加,-d
删除, 修改,-l
列表,-t
指明类型(或域)。
semanage login -l
列出当用户身份和 SELinux 身份之间的映射。没有指明登记项的用户会使用 __default__
登记项。 semanage login -a -s user_u *user*
命令会关联 user_u 身份到指定用户。semanage login -d *user*
会移除赋予用户的映射登记项。
semanage user -l
列出 SELinux 用户身份和允许的角色之间的映射。添加一个新的身份需要定义相应的角色和用于给个人文件赋予类型的标识前缀(/home/*user*/*
)。前缀必须从 user
,staff
,和 sysadm
之间挑选。staff
前缀生成 staff_home_dir_t
类型文件。创建新的用户身份使用 semanage user -a -R *roles* -P *prefix* *identity*
。使用 semanage user -d *identity*
移除用户身份。
#
14.5.3.3. 管理文件上下文,端口和布尔值
每个 SELinux 模块提供了一套标识规则,但是也可以添加定制的标识规则来满足特殊情况。例如,如果想要网页服务器能读取 /srv/www/
里面的文件,就要执行 semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
和 restorecon -R /srv/www/
。前者注册新的标识规则,后者根据当前标识规则重置文件类型。
Similarly, TCP/UDP ports are labeled in a way that ensures that only the corresponding daemons can listen to them. For instance, if you want the web server to be able to listen on port 8080, you should run semanage port -m -t http_port_t -p tcp 8080
.
有些 SELinux 模块导出布尔选项,可以用来改变默认规则。getsebool
程序可以用于检查此类选项(getsebool *boolean*
显示一个选项,getsebool -a
显示所有选项)。setsebool *boolean* *value*
命令改变当前布尔选项值。-P
选项使更改永久生效,就意味着新规则成为默认规则,重启之后也会保留。下面的例子授予网页服务器对用户主目录的访问权限(当用户在 ~/public_html/
中有个人网页的时候,这是很有用的)。
#
既然 SELinux 策略是模块化的,那么为(定制化)新应用开发新模块是很有趣的。这些新模块会完善参考策略。
要创建新模块,需要 selinux-policy-dev 软件包,还有 selinux-policy-doc。后者包含了标准规则的文档(/usr/share/doc/selinux-policy-doc/html/
)和可用于创建新规则的模板示例文件。安装这些文件并更深入的研究:
$
The .te
file is the most important one. It defines the rules. The .fc
file defines the “file contexts”, that is the types assigned to files related to this module. The data within the .fc
file are used during the file labeling step. Finally, the .if
file defines the interface of the module: it is a set of “public functions” that other modules can use to properly interact with the module that you’re creating.
14.5.4.1. 写一个 .fc 文件
例 14.2. example.fc
文件
14.5.4.2. 写一个 .if 文件
在下面的例子中,第一个接口(myapp_domtrans
)控制谁可以执行程序。第二个(myapp_read_log
)授予程序日志文件的读权限。
每个接口必须有一套规则并嵌入到 .te
文件。因此必须声明所有使用的类型(使用 gen_require
宏),并使用标准指令授予权限。也可以使用其他模块提供的接口。如何表示这些权限将在下节给出更详细的解释。
例 14.3. example.if
文件
- ## <summary>Myapp example policy</summary>
- ## <desc>
- ## <p>
- ## More descriptive text about myapp. The <desc>
- ## tag can also use <p>, <ul>, and <ol>
- ## html tags for formatting.
- ## </p>
- ## <p>
- ## This policy supports the following myapp features:
- ## <ul>
- ## <li>Feature A</li>
- ## <li>Feature B</li>
- ## <li>Feature C</li>
- ## </ul>
- ## </desc>
- #
- ########################################
- ## <summary>
- ## Execute a domain transition to run myapp.
- ## </summary>
- ## <param name="domain">
- ## Domain allowed to transition.
- ## </param>
- #
- interface(`myapp_domtrans',`
- gen_require(`
- type myapp_t, myapp_exec_t;
- ')
- domtrans_pattern($1,myapp_exec_t,myapp_t)
- ')
- ########################################
- ## <summary>
- ## Read myapp log files.
- ## </summary>
- ## <param name="domain">
- ## Domain allowed to read the log files.
- ## </param>
- #
- interface(`myapp_read_log',`
- gen_require(`
- type myapp_log_t;
- ')
- logging_search_logs($1)
- allow $1 myapp_log_t:file r_file_perms;
- ')
文档关于参考策略的解释
The reference policy evolves like any free software project: based on volunteer contributions. The project is hosted by Tresys, one of the most active companies in the SELinux field. Their wiki contains explanations on how the rules are structured and how you can create new ones.
→
14.5.4.3. 写一个 .te 文件
看一看 example.te
文件:
进阶 宏语言
要恰当的构建策略,SELinux 开发者使用宏命令处理器。他们创建了“宏功能”,而非重复使用很多相似的允许(allow)指令,来使用高级别的逻辑生成更加可读的策略。
实际上,m4
用于编译这些规则。它进行反方向地操作:将所有高级别的逻辑扩展为由允许(allow)指令组成的巨型数据库。
SELinux“接口”只是宏功能,在编译时它会被一套规则替代。类似地,有些权限其实是一套权限集合,它们在编译时会被实际值取代。
- policy_module(myapp,1.0.0)
- ########################################
- #
- # Declarations
- #
- type myapp_t;
- type myapp_exec_t;
- domain_type(myapp_t)
- domain_entry_file(myapp_t, myapp_exec_t)
- type myapp_log_t;
- logging_log_file(myapp_log_t)
- type myapp_tmp_t;
- files_tmp_file(myapp_tmp_t)
- ########################################
- #
- # Myapp local policy
- #
- allow myapp_t myapp_log_t:file { read_file_perms append_file_perms };
- allow myapp_t myapp_tmp_t:file manage_file_perms;
- files_tmp_filetrans(myapp_t,myapp_tmp_t,file)
现在需要找到能确保目标程序或服务正常工作的最小规则集合。要做到这一点,就要很好的理解应用程序是如何工作的,它需要处理或生成什么样的数据。
然而,实验方法也可以。一旦相关的对象被正确标识,可以在许可模式下使用应用程序:应该禁止的操作会被记录但是仍然会运行。通过分析日志,就能识别那些操作应该允许。这里有一个此类日志条目示例:
- avc: denied { read write } for pid=1876 comm="syslogd" name="xconsole" dev=tmpfs ino=5510 scontext=system_u:system_r:syslogd_t:s0 tcontext=system_u:object_r:device_t:s0 tclass=fifo_file permissive=1
为了更好的理解这条信息,我们逐一分析。
表 14.1. SELinux 追踪记录分析
消息 | 描述 |
---|---|
avc: denied | 一个操作被拒绝。 |
{ read write } | 该操作需要 读-read 和 写-write 许可。 |
pid=1876 | 进程号为1876的进程执行的操作(或者试图执行)。 |
comm=”syslogd” | 该进程是 syslogd 程序的一个实例。 |
name=”xconsole” | The target object was named xconsole . Sometimes you can also have a “path” variable — with the full path — instead. |
dev=tmpfs | The device hosting the target object is a tmpfs (an in-memory filesystem). For a real disk, you could see the partition hosting the object (for example: “sda3”). |
ino=5510 | 对象的节点(inode)号码是5510。 |
scontext=system_u:system_r:syslogd_t:s0 | 这是执行操作进程的安全上下文。 |
tcontext=system_u:object_r:device_t:s0 | 这是执行操作对象的安全上下文。 |
tclass=fifo_file | 目标对象是一个 FIFO 文件。 |
通过观察日志条目,就可以创建允许操作的规则。例如:allow syslogd_t device_t:fifo_file { read write }
。这一过程可以自动化,这正是 audit2allow
(在 policycoreutils 软件包)命令所提供的功能。这种方法只在各种对象根据其限制已被正确标识时有用。任何情况下,都要仔细审阅创建的规则并根据对应用程序的认识验证它们。实际上,这种方法通常会授予比实际需求更多的权限。恰当的方案是创建新的类型并且只授予针对这些类型的权限。有时候,拒绝操作对应用程序并不是致命的,这种情况下,可以添加一条“忽略-dontaudit
”规则来避免记入日志。
补充无角色的策略规则
14.5.4.4. 编译文件
Once the 3 files (example.if
, example.fc
, and example.te
) match your expectations for the new rules, just run make NAME=devel
to generate a module in the example.pp
file (you can immediately load it with semodule -i example.pp
). If several modules are defined, make
will create all the corresponding files.