newrole -r role_r -t domain_t
(一个指定角色通常只能被赋予一个域,-t
参数可以忽略)。该命令需要口令授权。这个特性可以禁止程序自动切换角色。在 SELinux 策略中,这种改变只能是显式的。
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.
aptitude install selinux-basics selinux-policy-default
命令会自动安装配置 SELinux 系统所需要的软件包。
unconfined
module (modules management is detailed further in this section).
fixfiles relabel
命令手工启动。
selinux=1
参数来启用它。audit=1
参数会使 SELinux 记录所有被拒绝的操作。enforcing=1
参数使规则起作用:不使用该参数的话,SELinux 工作在默认许可模式,该模式下拒绝行为会被记录但是仍然可以执行。可以修改 GRUB 启动加载器的配置文件来附加需要的参数。一个简单的方法是修改 /etc/default/grub
文件中的 GRUB_CMDLINE_LINUX
变量并运行 update-grub
。在重启之后 SELinux 就生效了。
selinux-activate
脚本自动化这些操作并强制标识(避免无标识的文件在 SELinux 未生效或正在标识的时候被创建)。
semodule
命令可以实现该目的。还必须用 semanage
命令,定义每个用户可用的角色。
/etc/selinux/default/
的配置。不像其他配置文件,可以在 /etc/
中的其他配置文件,这些文件不能手工修改。应当使用相应的程序来修改。
/usr/share/selinux/default/
目录。要在当前配置中启用其中的一个模块,使用 semodule -i module.pp
命令。pp 扩展名代表 策略软件包-policy package。
semodule -r module
命令。semodule -l
列出当前已经启用的模块清单。同时也输出版本号。
#
semodule -i /usr/share/selinux/default/aide.pp
#
semodule -l
aide 1.4.0 apache 1.10.0 apm 1.7.0 [...]
#
semodule -r aide
#
semodule -l
apache 1.10.0 apm 1.7.0 [...]
semodule
立即加载新配置,除非使用了 -n
选项。需要注意的是在当前配置默认启用的程序(由在 /etc/selinux/config
文件中的 SELINUXTYPE
变量指明),可以使用 -s
选项来修改和储存其他的。注意不同版本支持的选项可能不同。
semanage
命令配置。
-a
添加,-d
删除,-m
修改,-l
列表,-t
指明类型(或域)。
semanage login -l
列出当用户身份和 SELinux 身份之间的映射。没有指明登记项的用户会使用 __default__
登记项。 semanage login -a -s user_u user
命令会关联 user_u 身份到指定用户。semanage login -d user
会移除赋予用户的映射登记项。
#
semanage login -a -s user_u rhertzog
#
semanage login -l
Login Name SELinux User MLS/MCS Range __default__ unconfined_u s0-s0:c0.c1023 rhertzog user_u None root unconfined_u s0-s0:c0.c1023 system_u system_u s0-s0:c0.c1023 #
semanage login -d rhertzog
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
移除用户身份。
#
semanage user -a -R 'staff_r user_r' -P staff test_u
#
semanage user -l
Labeling MLS/ MLS/ SELinux User Prefix MCS Level MCS Range SELinux Roles root sysadm s0 s0-s0:c0.c1023 staff_r sysadm_r system_r staff_u staff s0 s0-s0:c0.c1023 staff_r sysadm_r sysadm_u sysadm s0 s0-s0:c0.c1023 sysadm_r system_u user s0 s0-s0:c0.c1023 system_r test_u staff s0 s0 staff_r user_r unconfined_u unconfined s0 s0-s0:c0.c1023 system_r unconfined_r user_u user s0 s0 user_r #
semanage user -d test_u
/srv/www/
里面的文件,就要执行 semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
和 restorecon -R /srv/www/
。前者注册新的标识规则,后者根据当前标识规则重置文件类型。
semanage port -m -t http_port_t -p tcp 8080
。
getsebool
程序可以用于检查此类选项(getsebool boolean
显示一个选项,getsebool -a
显示所有选项)。setsebool boolean value
命令改变当前布尔选项值。-P
选项使更改永久生效,就意味着新规则成为默认规则,重启之后也会保留。下面的例子授予网页服务器对用户主目录的访问权限(当用户在 ~/public_html/
中有个人网页的时候,这是很有用的)。
#
getsebool httpd_enable_homedirs
httpd_enable_homedirs --> off #
setsebool -P httpd_enable_homedirs on
#
getsebool httpd_enable_homedirs
httpd_enable_homedirs --> on
/usr/share/doc/selinux-policy-doc/html/
)和可用于创建新规则的模板示例文件。安装这些文件并更深入的研究:
$
zcat /usr/share/doc/selinux-policy-doc/Makefile.example.gz >Makefile
$
zcat /usr/share/doc/selinux-policy-doc/example.fc.gz >example.fc
$
zcat /usr/share/doc/selinux-policy-doc/example.if.gz >example.if
$
cp /usr/share/doc/selinux-policy-doc/example.te ./
.te
文件是最重要的一个。它定义规则。.fc
文件定义“文件上下文”,即与模块相关的文件被赋予的类型。.fc
文件里的数据将在文件标识时使用。.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> ## </p> ## </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; ')
example.te
文件:
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)
模块必须通过名字和版本号标识。此指令是必须的。
| |
如果模块引入了新类型,它们必须使用类似的指令声明。不要害怕需要创建的类型过多,这总胜过赋予过多无用权限。
| |
这些接口定义 myapp_t 类型为进程域,它可以被标识为 myapp_exec_t 的可执行程序使用。这隐式地给这些对象添加 exec_type 属性,反过来允许其他模块授予权限执行这些程序:例如,userdomain 模块允许具有 user_t ,staff_t ,和 sysadm_t 域的进程执行。其他受限域将无权运行,除非规则赋予它们类似的权限(在此例中,dpkg 和域 dpkg_t )。
| |
logging_log_file 是一个有参考策略提供的接口。它指明具有某些标识的文件是日志文件并从相关联的规则中获取权限(例如赋予 logrotate 权限,以便于处理它们)。
| |
allow 指令是授予操作权限的基本指令。第一个参数是允许执行操作的进程域。第二个定义前面的域进程可以操作的对象。参数的形式是:“type:class”此处 type 是 SELinux 类型,class 描述对象(文件,目录,套接字,fifo,等等)属性。最后的参数描述许可权限(即允许的操作)。
许可权限由一套允许操作集合组成,并按照这个模板: { operation1 operation2 } 。可以使用宏来代表最常用的许可。/usr/share/selinux/default/include/support/obj_perm_sets.spt 中一一列举。
后面的网页提供了一个比较详尽的对象类列表,以及可以授予的权限。
|
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
表 14.1. SELinux 追踪记录分析
消息 | 描述 |
---|---|
avc: denied | 一个操作被拒绝。 |
{ read write } | 该操作需要 读-read 和 写-write 许可。 |
pid=1876 | 进程号为1876的进程执行的操作(或者试图执行)。 |
comm="syslogd" | 该进程是 syslogd 程序的一个实例。 |
name="xconsole" | 目标对象名称是 xconsole 。 |
dev=tmpfs | 承载目标对象的设备是 tmpfs (一个内存文件系统)。对于真实磁盘,可以看到承载对象的分区(例如:“hda3”)。 |
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
”规则来避免记入日志。