我试图build立一个自定义的SELinux
策略,但是当应用程序二进制文件在/bin
, /usr/bin
等标准二进制文件目录之外时,我却遇到了域转换问题。应用程序二进制文件位于这些目录之外。
出于testing目的,我有一个程序,只是打印当前的SELinux
域。 我有一个域my_domain_t
,和一个typesmy_domain_exec_t
。 当unconfined_t
域中的进程使用my_domain_exec_t
上下文执行文件时,将设置到my_domain_exec_t
。
如果我将testing二进制文件复制到/data
目录,设置它的上下文,并执行它,域不会改变。
$ cp getcontext /data/getcontext $ chcon -t my_domain_exec_t /data/getcontext $ /data/getcontext unconfined_t:unconfined_r:unconfined_t:s0-s0:c0.c1023
如果我将testing二进制文件复制到/usr/bin
目录,设置它的上下文,然后执行它,则域转换正确发生。
$ sudo cp getcontext /usr/bin/getcontext $ sudo chcon -t my_domain_exec_t /usr/bin/getcontext $ /usr/bin/getcontext unconfined_t:unconfined_r:my_domain_t:s0-s0:c0.c1023
通过testing,我相信我已经消除了与运行文件的用户,文件的所有权或父目录的安全上下文有关的任何事情。 还有什么可能导致这种差异?
编辑: /data
不是NFS挂载的。 /data
的上下文是system_u:object_r:default_t:s0
。 在另一个我正在testing的系统上,我也看到/data
标签为system_u:object_r:etc_runtime_t:s0
。
policy_module(my_domain, 1.0.0) require { type unconfined_t; } type my_domain_t; type my_domain_exec_t; role unconfined_r types my_domain_t; allow unconfined_t my_domain_exec_t:file *; allow my_domain_t my_domain_exec_t:file { rx_file_perms() }; allow unconfined_t my_domain_t:process { transition siginh setexec }; type_transition unconfined_t my_domain_exec_t: process my_domain_t;
这是我创build的近20个版本的策略中最简单的一个,它显示了所描述的行为。 我也尝试了domain_trans
, domain_auto_trans
和domain_auto_trans
macros,以及使用sepolgen
工具生成策略,该工具使用了domtrans_pattern
macros。
SElinux并不太在乎文件path。
我无法复制您的问题。 我在Fedora 20上使用了staff_r
angular色和staff_t
types,但是这似乎有效地复制了您的问题。
我的程序:
#include <stdio.h> #include <stdlib.h> #include <selinux/selinux.h> int main() { security_context_t con; if (getcon(&con) < 0) { perror("Cannot getcon"); return 1; } printf("%s\n", con); freecon(con); return 0; }
我的政策。
policy_module(getcon, 1.0.0) require { role staff_r; type staff_t; } ## Type defs type getcon_exec_t; type getcon_t; application_domain(getcon_t, getcon_exec_t) ## Role permits role staff_r types getcon_t; ## Transitions domain_auto_transition_pattern(staff_t, getcon_exec_t, getcon_t); ## Access vectors allow getcon_t staff_t:process sigchld; userdom_use_inherited_user_ptys(getcon_t);
编译程序:
gcc -o getcon getcon.c -lselinux
编译和加载策略:
make -f /usr/share/selinux/devel/Makefile load
运行时不改变上下文
$ ./getcon staff_u:staff_r:staff_t:s0-s0:c0.c1023
更改上下文:
# chcon -t getcon_exec_t getcon $ ./getcon staff_u:staff_r:getcon_t:s0-s0:c0.c1023
将父目录更改为default_t:
# chcon -t . default_t; chcon -t getcon_exec_t getcon $ ./getcon staff_u:staff_r:getcon_t:s0-s0:c0.c1023
如果你喜欢,你可以尝试使用我的政策作为基础。 但是这似乎并没有影响到我。