SELinux 文件路径上下文不适用于正则表达式

Tay*_*wee 4 linux selinux regex

我已经根据评论中的建议重新格式化以提高可读性。

我有一个使用 google 身份验证器的 RADIUS 服务器,SELinux 阻止 RADIUS 访问用户主目录中的 .google_authenticator 文件(这些也是 winbind 用户,主目录在/home/API)。我尝试通过构建以下策略文件来授予它访问权限:

$ 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")
Run Code Online (Sandbox Code Playgroud)

安装这些会显示 中的路径semanage fcontext -l,但它不起作用:

 $ 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
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我更改它以使路径完全匹配(即使有转义期)时,它的工作原理是:

$ 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
Run Code Online (Sandbox Code Playgroud)

更奇怪的是,正如 Iain 的回答中所建议的那样,当我直接使用 semanage 而不是策略文件添加路径时,正则表达式会起作用(首先删除策略路径,以免发生冲突):

$ 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
Run Code Online (Sandbox Code Playgroud)

我还使用 HOME_DIR 尝试了各种设置,但也没有运气。

Mat*_*Ife 6

尝试使用

HOME_DIR/\.google_authenticator -- gen_context(system_u:object_r:radiusd_google_authenticator_t,s0)
Run Code Online (Sandbox Code Playgroud)

反而。主目录不一定在 /home 中,这在您重建策略时充当宏。

编辑

所以我检查了源代码,它提供了以下文本,可能表明这里发生了什么。

label_file.c
680     /*
681      * Check for matching specifications in reverse order, so that
682      * the last matching specification is used.
683      */
Run Code Online (Sandbox Code Playgroud)

最后一场比赛的正则表达式,获胜。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
Run Code Online (Sandbox Code Playgroud)

在您的案例中获胜的匹配正则表达式是这个:

grep user_home_t /etc/selinux/targeted/contexts/files/file_contexts.homedirs
/home/[^/]+/.+  user_u:object_r:user_home_t:s0
Run Code Online (Sandbox Code Playgroud)

通过将条目添加为本地上下文,它是从这个文件中获取的:/etc/selinux/targeted/contexts/files/file_contexts.local它出现当前匹配的文件之后

因此,为了解决这个问题(这基本上是一个小技巧),您可以将条目添加为本地覆盖。

或者,我尝试将其添加为 HOME_DIR 覆盖(就像我最初建议的那样,但使​​用 audio_home_t 来测试主体)执行以下操作:

HOME_DIR/(tcr)?/\.google_authenticator          --      gen_context(system_u:object_r:audio_home_t)
Run Code Online (Sandbox Code Playgroud)

这对我有用,因为它将条目添加到后面的文件中,并且当我这样做时,“最后一个正则表达式将获胜”。

它实际上将文件中的正则表达式更改为:

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
Run Code Online (Sandbox Code Playgroud)

我建议首先尝试 HOME_DIR 选项(这是您应该实施它的策略中的实际方式),或者使用本地覆盖。