基于评论中的build议,我已经根据可读性进行了重新格式化。
我有一个使用google authenticator的RADIUS服务器,SELinux阻止了RADIUS访问用户主目录(这些也是winbind用户, /home/API dir在/home/API )中的.google_authenticator文件。 我试图通过build立以下策略文件来访问它:
$ cat ./google-authenticator.fc /home/API(/.*)?/\.google_authenticator gen_context(system_u:object_r:radiusd_google_authenticator_t,s0) $ cat ./google-authenticator.te policy_module(radiusd_google_authenticator, 0.0.10) require { type radiusd_t; type user_home_dir_t; type admin_home_t; } type radiusd_google_authenticator_t; role object_r types radiusd_google_authenticator_t; allow radiusd_t radiusd_google_authenticator_t:file { rename create unlink rw_file_perms }; files_type(radiusd_google_authenticator_t) filetrans_pattern(radiusd_t, user_home_dir_t, radiusd_google_authenticator_t, file, ".google_authenticator") filetrans_pattern(radiusd_t, admin_home_t, radiusd_google_authenticator_t, file, ".google_authenticator")
安装这些显示在semanage fcontext -lpath,但它不起作用:
$ sudo semanage fcontext -l | grep google_authenticator /home/API(/.*)?/\.google_authenticator all files system_u:object_r:radiusd_google_authenticator_t:s0 $ matchpathcon /home/API/tcr/.google_authenticator /home/API/tcr/.google_authenticator unconfined_u:object_r:user_home_t:s0
奇怪的是,当我改变它,以便path完全匹配(即使与逃脱期间),它的工作原理:
$ sudo semanage fcontext -l | grep google_authenticator /home/API/tcr/\.google_authenticator all files system_u:object_r:radiusd_google_authenticator_t:s0 $ matchpathcon /home/API/tcr/.google_authenticator /home/API/tcr/.google_authenticator system_u:object_r:radiusd_google_authenticator_t:s0
即使是更奇怪的,正则expression式的工作原理,正如Iain的答案中所提到的,我直接使用semanage添加path而不是策略文件(首先删除策略path,以免冲突):
$ sudo semanage fcontext -l | grep google_authenticator $ matchpathcon /home/API/tcr/.google_authenticator /home/API/tcr/.google_authenticator unconfined_u:object_r:user_home_t:s0 $ sudo semanage fcontext -a -t radiusd_google_authenticator_t '/home/API(/.*)?/\.google_authenticator' $ sudo semanage fcontext -l | grep google_authenticator /home/API(/.*)?/\.google_authenticator all files system_u:object_r:radiusd_google_authenticator_t:s0 $ matchpathcon /home/API/tcr/.google_authenticator /home/API/tcr/.google_authenticator system_u:object_r:radiusd_google_authenticator_t:s0
我也尝试了使用HOME_DIR的各种设置,但也没有运气。
尝试使用
HOME_DIR/\.google_authenticator -- gen_context(system_u:object_r:radiusd_google_authenticator_t,s0)
代替。 主目录不一定在/ home中,当你重build策略时,它就像一个macros一样。
编辑
所以我检查了源代码,它提供了以下文本,可能指示这里正在发生什么。
label_file.c 680 /* 681 * Check for matching specifications in reverse order, so that 682 * the last matching specification is used. 683 */
正则expression式匹配最后,赢。 SELinux库使用以下文件查找顺序进行匹配:
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist /etc/selinux/targeted/contexts/files/file_contexts.subs /etc/selinux/targeted/contexts/files/file_contexts /etc/selinux/targeted/contexts/files/file_contexts.homedirs /etc/selinux/targeted/contexts/files/file_contexts.local
在你的情况下获胜的匹配正则expression式是这样的:
grep user_home_t /etc/selinux/targeted/contexts/files/file_contexts.homedirs /home/[^/]+/.+ user_u:object_r:user_home_t:s0
通过添加条目作为本地上下文,它将从该文件中获取: /etc/selinux/targeted/contexts/files/file_contexts.local ,该文件出现在当前匹配的文件之后 。
所以,为了解决这个问题(这基本上是一个黑客),你可以添加条目作为本地覆盖。
另外我试着把它作为一个HOME_DIR覆盖(像我最初的build议,但使用audio_home_t来testing主体),做如下:
HOME_DIR/(tcr)?/\.google_authenticator -- gen_context(system_u:object_r:audio_home_t)
这对我来说是有效的,因为它为后面的文件添加了条目,当我这样做的时候,“最后的正则expression式赢了”。
它实际上改变了正则expression式在这个文件中:
grep tcr /etc/selinux/targeted/contexts/files/* /etc/selinux/targeted/contexts/files/file_contexts.homedirs:/home/[^/]+/(tcr)?/\.google_authenticator -- user_u:object_r:audio_home_t:s0
我build议首先尝试HOME_DIR选项(这是您应该实现它的策略的实际方法),或者使用本地覆盖。
我不完全确定你想要什么,但是如果它有API之后的通配符,例如
/home/API/one/.google_authenticator /home/API/two/.google_authenticator ...
都有typesradiusd_google_authenticator_t然后这似乎工作
sudo semanage fcontext -a -t mysqld_db_t "/home/API(/.*)?/.google_authenticator"
注意我已经使用mysqld_db_t,因为我没有radiusd_google_authenticator_t
matchpathcon /home/API/one/.google_authenticator /home/API/one/.google_authenticator system_u:object_r:mysqld_db_t:s0 matchpathcon /home/API/two/.google_authenticator /home/API/two/.google_authenticator system_u:object_r:mysqld_db_t:s0