在我的 selinux 模块文件中设置端口号

在我的 selinux 模块文件中设置端口号

我正在尝试组装一个小型的 selinux 模块,以进一步了解 selinux。我定义了一个小型的守护进程类应用程序类型,它有自己的 port_type。以下代码是精简版,用 httpd_t 替换了我自己的应用程序类型:

module mymodule 1.0;

require {
        type httpd_t;
        attribute port_type;
        class tcp_socket name_bind;
}

#============= my_port_t ==============

type my_port_t, port_type;
allow httpd_t my_port_t:tcp_socket name_bind;

我现在想将 my_port_t 的实际端口号放入模块(或生成的包)中。目的是避免像这样的显式 semanage 调用:

semanage port -a -t my_port_t -p tcp 9011

我知道 semanage 会使端口号永久不变,但我更希望有一个文件来处理所有事情。这样,将模块分发到许多系统会容易得多。

我该怎么做?到目前为止我还没有找到解决方案。我是不是用错了方法?

答案1

portcon是出于这个目的的指令,但根据在可加载策略模块中标记端口文章中提到,在基础模块之外是不可能实现的。官方SELinux portcon 文档说同样的话(“模块政策:否”)。

然而,这仅适用于传统.te模块。SELinux 现在也支持 CIL(通用中间语言),其中它受到支持.据说,你需要使用 EL 7.3 或更高版本,或者 Fedora 23 或更高版本

因此,就您而言,端口定义本身应如下所示:

; Declare a my_port_t type
(type my_port_t)
; Assign the type to the object_r role
(roletype object_r my_port_t)

; Assign the right set of attributes to the port
(typeattributeset defined_port_type my_port_t)
(typeattributeset port_type my_port_t)

; Declare tcp:9011 as my_port_t
(portcon tcp 9011 (system_u object_r my_port_t ((s0) (s0))))

但这只解决了端口类型定义的问题。根据SELinux 基本概念,该政策的其余部分将非常简单:

; Allow httpd_t to bind to my_port_t
(allow httpd_t my_port_t (tcp_socket (name_bind)))

如果您只想向现有端口类型添加其他端口,则要简单得多:

(portcon tcp 922 (system_u object_r ssh_port_t ((s0)(s0))))

您可以将此文件保存为,foobar-ssh.cil然后可以像这样安装并验证结果:

# semodule -i foobar-ssh.cil
# semodule -l | grep foobar
foobar-ssh
# semanage port -l | grep ssh
ssh_port_t                     tcp      22, 922

相关内容