大网数据广告活动
服务器运维问答

SELinux: How to create a new file type

· 52CE 我爱测

On RHEL/CentOS 7 I'm trying to create a new SELinux security context for files to support a new service that I'm writing. I've created a Type Enforcement file for my new service, but I can't manage to create a new type that the s…

问题原文

原帖链接 · Guss · CC BY-SA 3.0

On RHEL/CentOS 7 I'm trying to create a new SELinux security context for files to support a new service that I'm writing.

I've created a Type Enforcement file for my new service, but I can't manage to create a new type that the system will recognize as a file type.

I'm working based on this CentOS 5 "Building a local policy module" document, which instructs me to create a TE file then use checkmodule and semodule_package to compile it.

If I just write in my TE file:

type myservice_spool_t;

then the TE compiles fine, but when I try to semanage fcontext I get this:

$ 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

I've read various tutorials but failed to find an example that works - everything I look at is either:

  • outdated - e.g. This RHEL 4 SELinux documentation page says that one should use type myservice_spool_t, file_type; but checkmodule says: ERROR 'attribute file_type is not declared'
  • incomplete - e.g. this answer will have me use the macro file_type() but checkmodule says: ERROR 'This block has no require section.' at token 'files_type'
  • or completely missing - e.g. the new SELinux guide for RHEL 7 does not have any information on how to create new policies, except using audit2allow.

The SELinux project website is completely useless as I failed to locate a single working example

I'd appreciate a simple concise example of how to write a TE file that introduces a new type that semanage fcontext will approve of.

[Update]

I found this Gentoo documentation for creating policy files, which some useful explanations and examples.

已采纳解答

原帖链接 · Guss · CC BY-SA 3.0

I found out that the problem I was having is because I didn't compile the module correctly. As a result the macros probably didn't "take" and the checkmodule policy compiler error messages didn't really help to understand that.

To get all these macros to expand properly, one needs to compile the policy using the Makefiles provided by SELinux - with a TE file called myservice_spool.te, one should execute:

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

This will create a temporary TE file with all macros expanded, then call the relevant compilers to create myservice_spool.pp.

The Gentoo documentation linked in the OP has slightly more information, though the file paths are not correct for CentOS systems.

If you review the generated TE template in the tmp directory (that the SELinux makefile helpfully leaves in place), you can see that "attributes" is indeed the correct way to handle specifying a type as a file, butwe must require them to get them to work - the way SELinux TE files appear to work is that you don't get any symbols magically imported into the configuration file - you have to require anything you use.

So the correct non-macroified way to set up a new file type is something like this (copied from the TE generated template):

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;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。