Cockpit SELinux 策略开发指南修改、重建与快速迭代 cockpit 的 SELinux 策略模块【免费下载链接】cockpitCockpit is a web-based graphical interface for servers.项目地址: https://gitcode.com/gh_mirrors/co/cockpitCockpit 作为面向服务器的 Web 管理界面其登录、会话与 Web 服务组件都运行在 SELinux 强制访问控制MAC环境之下需要一套专门定制的策略模块来约束cockpit-ws与cockpit-session进程。本文以仓库内 selinux/HACKING.md 为骨架完整讲解如何修改这套策略、如何重新构建 RPM 与测试镜像以及如何用最小代价在本地快速编译、部署并热加载策略模块适合为 Cockpit 贡献 SELinux 策略改动或需要自定义策略的开发与运维人员阅读。策略模块概览Cockpit 的 SELinux 三件套Cockpit 的 SELinux 策略以标准 refpolicy 模块形式组织全部位于仓库的selinux/目录下由三个核心文件构成selinux/cockpit.teType Enforcement 文件定义类型domain/type与访问许可规则是策略的逻辑主体selinux/cockpit.fcFile Context 文件定义文件路径与 SELinux 标签类型的映射关系selinux/cockpit.ifInterface 文件定义可供其他策略模块复用的接口供第三方模块与 Cockpit 策略集成。此外 selinux/cockpit_ws_selinux.8cockpit 与 selinux/cockpit_session_selinux.8cockpit 是由sepolicy manpage自动生成的两份手册页分别描述cockpit_ws_t与cockpit_session_t两个域随模块一起安装到man8。它们在 selinux/Makefile.am 中被列为SELINUX_POLICY_MANPAGES与策略文件一起被打进cockpit-ws-selinux子包详见下文“打包与安装”。修改策略的两种工作流selinux/HACKING.md 给出了两种工作流一种是“干净路线”适合最终提交与全量验证另一种是“快速迭代路线”适合开发过程中的短周期试错。工作流一编辑策略后重建 RPM 与测试镜像干净路线# 编辑 selinux/cockpit.te、selinux/cockpit.fc、selinux/cockpit.if 后 test/image-prepare -q fedora-XXtest/image-prepare -q fedora-XX-q为 quiet 模式fedora-XX为测试镜像版本会执行完整的构建链先打源码包再在构建虚拟机中用rpmbuild生成 RPM见 test/image-prepare 中的build_rpms其流程为rpmbuild --define _topdir /var/tmp/build -ts生成 SRPM再--rebuild出二进制包随后把 RPM 上传到测试镜像并部署最终产出一个已经装有最新策略的、可直接运行验证测试套件的虚拟机镜像。这条路线最接近真实发布流程能同时验证策略在打包、安装、%post脚本触发 relabel/module 加载等环节的表现适合在改动合入前做最终验证。工作流二本地快速编译并热加载迭代路线make cockpit.pp scp cockpit.pp c:/tmp/ ssh c semodule -i /tmp/cockpit.pp此流程依赖两个前提本地 configure 时开启策略构建。必须在构建树中执行./configure --enable-selinux-policytargeted--enable-selinux-policy需要携带一个具体的策略类型参数如targeted该参数在 configure.ac 中解析如果传入裸的yes会直接报错--enable-selinux-policy requires a type (eg: targeted)解析结果通过AM_CONDITIONAL(SELINUX_POLICY_ENABLED, ...)与AC_SUBST(SELINUX_POLICY_TYPE, ...)传给构建系统最终由顶层 Makefile.am 以include selinux/Makefile.am的形式纳入构建。c是可用的 SSH 目标。c是 Cockpit 测试套件中测试虚拟机的 SSH 别名对应仓库测试体系中的目标机器你需要能通过 SSH 免密访问该机器。命令链做了三件事make cockpit.pp依据 selinux/Makefile.am 中的规则调用系统 SELinux 开发模板make -sf /usr/share/selinux/devel/Makefile cockpit.pp把.te/.fc/.if编译为二进制策略模块cockpit.ppscp cockpit.pp c:/tmp/把编译产物传到目标机的/tmpssh c semodule -i /tmp/cockpit.pp通过semodule -i把模块直接安装进运行中的 SELinux 策略无需重启机器或服务改动即刻生效。迭代路线省掉了 RPM 打包与镜像重建改一次策略编译一次、安装一次是开发期最推荐的循环方式。注意该路线不会自动 relabel 已有文件系统若只改了.fc文件上下文规则需要额外执行restorecon才能把新标签落到磁盘上的既有文件。策略源码解析Type Enforcementcockpit.teselinux/cockpit.te 定义了 8 个主要类型可以分成两组看类型用途关键声明cockpit_ws_tcockpit-ws/cockpit-tls进程域init_daemon_domaininit_nnp_daemon_domaincockpit_ws_exec_t上述可执行文件的入口文件类型can_exec(cockpit_ws_t, cockpit_ws_exec_t)cockpit_session_tcockpit-session进程域负责登录会话domain_typedomain_entry_filecockpit_session_exec_t/usr/libexec/cockpit-session的入口文件类型can_exec(init_t, cockpit_session_exec_t)cockpit_tmp_t/tmp下的临时文件files_tmp_filecockpit_tmpfs_ttmpfs 上的临时文件userdom_user_tmp_filecockpit_var_run_t/run下的 PID 文件与 socketfiles_pid_filesystemd_private_tmpcockpit_var_lib_t/var/lib/cockpit数据目录files_typecockpit_unit_file_tsystemd unit 文件systemd_unit_file其中cockpit_ws_t域的入口点不止cockpit-ws一个。根据文件上下文 selinux/cockpit.fc 与手册页 selinux/cockpit_ws_selinux.8cockpit/usr/libexec/cockpit-tls和/usr/libexec/cockpit-wsinstance-factory同样被标记为cockpit_ws_exec_t即它们与cockpit-ws共享同一进程域——这是因为三者构成 WS 层的同一信任边界。cockpit_ws_t 的关键访问规则cockpit_ws_t的策略围绕“Web 服务进程”职责展开几个有代表性的规则网络端口corenet_tcp_bind_websm_port(cockpit_ws_t)允许绑定websm_port_t端口Cockpit 默认 9090 端口的 SELinux 端口类型测试用例 test/verify/check-connection 中也有semanage port -m -t websm_port_t -p tcp 443的操作佐证corenet_tcp_connect_ssh_port(cockpit_ws_t)允许向外建立 SSH 连接用于跳板/隧道场景进程派生链can_exec(cockpit_ws_t, cockpit_session_exec_t)允许cockpit-ws执行cockpit-session并通过cockpit_session_domtrans(cockpit_ws_t)完成域转换can_exec(cockpit_ws_t, ssh_exec_t)则允许经 beiboot/ferny 启动真正的ssh进程间通信allow cockpit_ws_t cockpit_session_t:unix_stream_socket connectto与反向的rw_stream_socket_perms定义了 WS 与 session 之间的 Unix socket 通道cockpit-tls与cockpit-ws之间则通过allow cockpit_ws_t cockpit_ws_t:unix_stream_socket ...自域通信系统状态读取kernel_read_system_state、dev_read_sysfs、init_read_state、kernel_read_network_state等宏支撑仪表盘对 CPU/内存/网络状态的采集临时与运行文件对cockpit_tmp_t、cockpit_tmpfs_t、cockpit_var_run_t的 manage 模式配合files_tmp_filetrans、fs_tmpfs_filetrans、files_pid_filetrans完成文件自动打标服务管理allow cockpit_ws_t cockpit_unit_file_t:service manage_service_perms配合optional_policy中的systemd_exec_systemctl允许在页面中启停 systemd 服务Kerberos 与证书optional_policy内的kerberos_use、kerberos_etc_filetrans_keytab、miscfiles_dontaudit_map_generic_certs等保证在使用 keytab 和通用证书目录时不被误拦截。cockpit_session_t 的关键访问规则cockpit-session的任务是把用户的 Web 登录转成真正的系统登录会话因此它获得的权限更接近登录程序login切换用户身份userdom_spec_domtrans_all_users与userdom_noatsecure_login_userdomain允许它以任意受限用户身份运行cockpit-bridge且不设置 AT_SECURE见 src/session 相关实现unconfined_domtrans处理无限制用户的转换完整 PAM 栈auth_login_pgm_domain、auth_manage_passwd、auth_manage_shadow、auth_write_login_records、usermanage_read_crack_db等宏覆盖 pam 认证、改密、写 wtmp/btmp 等需求auth_manage_passwd/auth_manage_shadow对应“重置过期密码”场景账号锁定与双因子pam_faillock需要把状态文件 chown 给目标用户故有self:capability { chown fowner dac_override ... setuid setgid }userdom_manage_user_home_content则让pam_google_authenticator能在用户家目录创建/重命名文件对应手册页中auth_home_t下的.google_authenticator等路径与受限用户的会话通信allow { user_t sysadm_t } cockpit_session_t:unix_stream_socket rw_stream_socket_perms允许受限用户在既有 socket 连接上继续收发数据pidfs 兼容针对 systemd 258 以后的改动以optional_policy形式对pidfs_t:filesystem getattr放行同时注明 RHEL 9 尚无此类型体现了策略对不同发行版年代的兼容处理手法。optional_policy是这套策略反复使用的技巧把对可选功能hostname、Kerberos、SSSD、GNOME keyring、pidfs 等的授权包在optional_policy块中使模块在缺少相应类型的系统上仍能正常加载这正是 HACKING 文档所强调的“可移植的策略模块”写法。文件上下文cockpit.fc与标签映射selinux/cockpit.fc 定义了策略涉及的文件标签核心映射如下路径模式类型/usr/lib/systemd/system/cockpit.*、/etc/systemd/system/cockpit.*cockpit_unit_file_t/usr/libexec/cockpit-ws、cockpit-tls、cockpit-wsinstance-factorycockpit_ws_exec_t/usr/libexec/cockpit-sessioncockpit_session_exec_t/usr/share/cockpit/issue/update-issueshell_exec_t作为可执行脚本/var/lib/cockpit(/.*)?cockpit_var_lib_t/var/run/cockpit(/.*)?、/run/cockpit(/.*)?、/var/run/cockpit-ws(/.*)?、/run/cockpit-ws(/.*)?cockpit_var_run_t/etc/cockpit/ws-certs\.d(/.*)?cert_t证书目录复用系统证书类型注意.fc中的--普通文件与(/.*)?目录及其内容修饰符含义不同改文件上下文规则时需保持这两种模式并用前者精确匹配单个文件后者匹配目录及递归内容。cert_t的复用说明策略刻意把 Web 服务证书交给系统标准类型管理避免重复造类型。手册页 selinux/cockpit_ws_selinux.8cockpit 的 FILE CONTEXTS 一节还补充说明临时改标签用chcon永久改标签需semanage fcontext配合restorecon落盘。接口模块cockpit.if对外开放的策略接口selinux/cockpit.if 面向其他策略模块作者定义了十几个带interface()的复用接口例如cockpit_ws_domtrans($1)/cockpit_session_domtrans($1)允许指定域转换进 Cockpit 的 WS 或 session 域cockpit_manage_unix_stream_sockets($1)允许指定域创建/连接cockpit_ws_t的 Unix stream socketcockpit_read_lib_files($1)/cockpit_manage_lib_files($1)/cockpit_manage_lib_dirs($1)对/var/lib/cockpit的读/管理授权cockpit_read_pid_files($1)/cockpit_manage_pid_files($1)/cockpit_manage_pid_dirs($1)对/var/run/cockpit下 PID 文件与目录的授权cockpit.te中即用cockpit_read_pid_files(local_login_t)让 login 域读取 PID 文件cockpit_systemctl($1)/cockpit_admin($1)面向管理域的一站式管理接口cockpit_admin还内置了对deny_ptracetunable 的处理以及systemd_passwd_agent_exec等可选依赖。这套接口遵循 refpolicy 惯例每个接口都以gen_require声明所需类型既保证编译期可校验也方便其他模块以最小依赖接入。构建与打包Makefile 与 RPM 子包构建规则selinux/Makefile.amselinux/Makefile.am 是策略构建的核心关键规则cockpit.pp: $(SELINUX_POLICY_FILES) $(AM_V_GEN) make -sf /usr/share/selinux/devel/Makefile cockpit.pp cockpit.pp.bz2: cockpit.pp $(AM_V_GEN) bzip2 -9 $ $.tmp mv $.tmp $编译依赖系统提供的/usr/share/selinux/devel/Makefile由selinux-policy-devel包提供因此本地快速迭代需要先安装该开发包分发用.pp.bz2压缩格式安装到$(datadir)/selinux/packages/$(SELINUX_POLICY_TYPE)/即targeted类型下install-selinux目标会在$(sharedstatedir)/selinux/$(SELINUX_POLICY_TYPE)/active/modules/200/cockpit建立目录权限 700对应运行时模块存储路径。该文件整体被if SELINUX_POLICY_ENABLED条件包裹只有当--enable-selinux-policy非no时才参与构建。RPM 打包tools/cockpit.specRPM 规范文件 tools/cockpit.spec 展示了策略如何被组织成子包定义%define selinuxtype targeted并把--enable-selinux-policy%{selinuxtype}作为 configure 参数传入主包构建ws-selinux子包第 485 行起声明Requires(post): selinux-policy-%{selinuxtype}与libselinux-utils随包交付%{_datadir}/selinux/packages/%{selinuxtype}/%{name}.pp.bz2与两份 man8 手册页生命周期脚本使用 RPM 的 SELinux 宏族%pre中%selinux_relabel_pre%post中%selinux_modules_install -s %{selinuxtype} ...pp.bz2安装模块并%selinux_relabel_post%postun中%selinux_modules_uninstall -s %{selinuxtype} %{name}卸载并 relabel。这也解释了为什么 Debian/Ubuntu 等平台没有cockpit-selinux——RPM 系的 Fedora/RHEL 使用 SELinux enforcing 作为默认安全模型而测试套件中 test/verify/check-selinux 也明确以testlib.skipImage(No cockpit-selinux, debian-*, ubuntu-*, arch, ...)跳过非 RPM 发行版。selinux/目录同时被两个顶层文件引用构建通过 Makefile.am 的include selinux/Makefile.am接入发行打包通过 tools/cockpit.spec 与 selinux/Makefile.am 的dist_noinst_DATA一起进入源码发行包。验证、排错与常见操作策略改动后建议按以下顺序验证检查域归属ps -eZ | grep cockpit_ws_t与ps -eZ | grep cockpit_session_t确认进程运行在预期域中见两份手册页的 DESCRIPTION 一节查看文件标签ls -Z /usr/libexec/cockpit-ws、ls -Zd /run/cockpit确认.fc规则已生效改过.fc后执行restorecon -Rv /usr/libexec /run/cockpit /var/lib/cockpit应用新标签临时放行调试semanage permissive -a cockpit_ws_t可将某域临时置于 permissive 模式AVC 拒绝仍会记录但不再拦截定位问题后务必semanage permissive -d cockpit_ws_t撤销查看拒绝日志用ausearch -m AVC或journalctl | grep denied定位 AVC 拒绝结合 test/common/testlib.py 中audit_messages(14)的审计消息处理方式理解拒绝事件跑回归测试仓库提供 test/verify/check-selinux 浏览器端测试覆盖 SELinux 页面与告警交互改动涉及 UI 时以它为回归基线涉及连接/端口时可参考 test/verify/check-connection 中semanage port相关断言。若页面端还需要展示 SELinux 状态与告警对应实现位于 pkg/selinuxselinux.js、setroubleshoot-view.jsx等它与本策略模块同属cockpit-selinux交付内容改动策略时可一并关注。小结修改 Cockpit 的 SELinux 策略并非难事日常开发用--enable-selinux-policytargetedmake cockpit.ppsemodule -i的短循环快速试错提交前用test/image-prepare -q fedora-XX走完整的 RPM 构建与镜像部署验证理解 selinux/cockpit.te 中cockpit_ws_t/cockpit_session_t两大域的授权模型、selinux/cockpit.fc 的标签映射与 selinux/cockpit.if 的对外接口就能安全地对访问控制边界做最小改动并借助 AVC 日志与回归测试确保策略在 enforcing 模式下稳定可用。【免费下载链接】cockpitCockpit is a web-based graphical interface for servers.项目地址: https://gitcode.com/gh_mirrors/co/cockpit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考