SELinux:如何创build一个新的文件types

在RHEL / CentOS 7上我试图为文件创build一个新的SELinux安全上下文,以支持我正在编写的新服务。

我为我的新服务创build了一个Type Enforcement文件,但是我无法设法创build一个系统将识别为文件types的新types。

我正在基于这个CentOS 5“build立一个本地策略模块”文件 ,它指示我创build一个TE文件,然后使用checkmodulesemodule_package来编译它。

如果我只是写在我的TE文件中:

 type myservice_spool_t; 

那么TE编译好,但是当我尝试semanage fcontext我得到这个:

 $ sudo semanage fcontext -a -t myservice_spool_t "/var/spool/myservice(/.*)?" ValueError: Type myservice_spool_t is invalid, must be a file or device type 

我读过各种教程,但没有find一个可行的例子 – 我所看到的一切都是:

  • 过时 – 例如,这个RHEL 4 SELinux文档页面说应该使用type myservice_spool_t, file_type;checkmodule说: ERROR 'attribute file_type is not declared' checkmodule ERROR 'attribute file_type is not declared'
  • 不完整 – 例如, 这个答案将使我使用macroscheckmodule file_type()checkmodule说: ERROR 'This block has no require section.' at token 'files_type' ERROR 'This block has no require section.' at token 'files_type'
  • 或完全缺失 – 例如,除了使用audit2allow之外,针对RHEL 7的新SELinux指南没有关于如何创build新策略的任何信息。

SELinux项目网站是完全没有用的,因为我找不到一个工作的例子

我会很感激一个简单的例子,介绍如何编写一个TE文件,它引入了一个新的types, semanage fcontext将会批准。

[更新]

我发现这个用于创build策略文件的Gentoo文档 ,里面有一些有用的解释和例子。

您需要将其声明为文件属性的成员,使其具有重新标记特权。

尝试

 type myservice_spool_t; files_type(myservice_spool_t) 

或者在你的情况下更好..

 type myservice_spool_t; files_spool_file(myservice_spool_t) 

鉴于你实际上是一个假脱机文件。 如果在策略中具有“pipe理假脱机”特权,则其他macros可以使用该假脱机工作。

我发现我遇到的问题是因为我没有正确编译模块。 因此,macros可能没有“拿”, checkmodule策略编译器的错误信息并没有真正帮助理解这一点。

要使所有这些macros正确地扩展,需要使用SELinux提供的Makefiles来编译策略 – 使用一个名为myservice_spool.te的TE文件,应该执行:

 make -f /usr/share/selinux/devel/Makefile myservice_spool.pp 

这将创build一个扩展了所有macros的临时TE文件,然后调用相关的编译器来创buildmyservice_spool.pp

OP中链接的Gentoo文档有更多的信息,尽pipeCentOS系统的文件path不正确。

如果您查看tmp目录中生成的TE模板(SELinux makefile有助于离开),您可以看到“attributes”确实是处理将文件types指定为文件的正确方法,但是我们必须require它们获取它们工作 – SELinux TE文件的工作方式是,您不会将任何符号神奇地导入到configuration文件中 – 您必须require使用任何东西。

所以build立一个新的文件types的正确的non-macroified方式是这样的(从TE生成的模板复制):

 type myservice_spool_t; require { attribute spoolfile; attribute file_type, non_security_file_type, non_auth_file_type; } # end require typeattribute myservice_spool_t file_type, non_security_file_type, non_auth_file_type, spoolfile; 

https://selinuxproject.org/page/TypeStatements有正确的答案:

 # Using the typeattribute statement to associate a type of # setroubleshootd_exec_t to two attributes file_type and # non_security_file_type. # These are the previously declared attributes: attribute file_type; attribute non_security_file_type; # The previously declared type: type setroubleshootd_exec_t; # These are the associations using the typeattribute statement: typeattribute setroubleshootd_exec_t file_type, non_security_file_type; 

但是,是的,你的问题提出了一个有趣的观点。

SELinux本身就知道三种语言,并且有一种称为“引用策略”的第三方抽象语言。

  1. 单语策略语言(checkpolicy用于编译policy.conf – man checkpolicy)
  2. 模块策略语言(checkmodule用于编译$ MODULE。{te.fc} – man checkmodule)
  3. 通用中级语言(secilc用来编译$ MODULE.cil – man secilc)

上述每种本地语言都有其自己的特定属性,可能会造成混淆。

参考政策基本上是“模块政策语言”的一个包装,目的是使政策维护更容易。