Zabbix 5.0 with SELinux on CentOS 8

 article  Comments Off on Zabbix 5.0 with SELinux on CentOS 8
Mar 142021
 

SELinux環境下で動かそうとしたらいろいろあったので覚書。

前提

環境

  • CentOS 8.3
  • Zabbix 5.0.9
  • PostgreSQL 10.15
  • Apache 2.4

利用テンプレート

  • Template App Zabbix Server
  • Template App Apache by HTTP
  • Template App PHP-FPM by HTTP
  • Template OS Linux by Zabbix agent
  • Template DB PostgreSQL

適用したSELinuxブール値

  • httpd_can_connect_zabbix –> on
  • httpd_can_network_connect_db –> off
  • zabbix_can_network –> on

作成したSELinuxポリシーなど

Zabbix Server用

module zabbix-server_local 1.0;

require {
        type zabbix_t;
        type zabbix_var_run_t;
        type krb5_keytab_t;
        type var_log_t;
        class sock_file create;
        class sock_file unlink;
        class unix_stream_socket connectto;
        class dir search;
        class file open;
        class file read;
}

#============= zabbix_t ==============
allow zabbix_t zabbix_var_run_t:sock_file create;
allow zabbix_t zabbix_var_run_t:sock_file unlink;
allow zabbix_t self:unix_stream_socket connectto;
allow zabbix_t krb5_keytab_t:dir search;
allow zabbix_t var_log_t:file open;
allow zabbix_t var_log_t:file read;

上記ポリシーに加えてdac_overrideも要求されるが、/run/zabbixの所有者権限がzabbix:zabbix 700となっており、厳密なDACとしてはrootでの書き込みが許可されていないため発生している。
dac_overrideは許可したくないので/run/zabbixの権限をzabbix:root 775に変更してrootにも書き込み許可を与えるようにした。サービスユニットファイルを以下のとおり修正している。

[Service]
ExecStartPre=/usr/bin/mkdir -p /run/zabbix
ExecStartPre=/usr/bin/chgrp root /run/zabbix
ExecStartPre=/usr/bin/chmod g+w  /run/zabbix

Apache用

httpd_can_network_connect_dbをoffにしたので、PostgreSQLへの接続のみを明示的に許可している。

module httpd_local 1.0;

require {
        type httpd_t;
        type postgresql_port_t;
        class tcp_socket name_connect;
}

#============= httpd_t ==============
allow httpd_t postgresql_port_t:tcp_socket name_connect;

Zabbix Agent監視用

PostgreSQL監視用に/var/lib/zabbix/.pgpass読み込み(zabbix_var_lib_t)と、system.sw.packagesキーでrpm一覧を取得するためのrpm_exec_t実行権限やvar_lib_rpm_tの読み込み権限が必要だった。

module zabbix-agent_local 1.0;

require {
        type zabbix_agent_t;
        type zabbix_var_lib_t;
        type rpm_exec_t;
        type rpm_var_lib_t;
        type devlog_t;
        type initctl_t;
        type proc_kcore_t;
        class file open;
        class file read;
        class file execute;
        class file execute_no_trans;
        class file map;
        class sock_file getattr;
        class fifo_file getattr;
        class file getattr;
}

#============= zabbix_agent_t ==============
allow zabbix_agent_t zabbix_var_lib_t:file open;
allow zabbix_agent_t zabbix_var_lib_t:file read;
allow zabbix_agent_t rpm_exec_t:file execute;
allow zabbix_agent_t rpm_exec_t:file execute_no_trans;
allow zabbix_agent_t rpm_exec_t:file map;
allow zabbix_agent_t rpm_var_lib_t:file open;
allow zabbix_agent_t rpm_var_lib_t:file read;
allow zabbix_agent_t devlog_t:sock_file getattr;
allow zabbix_agent_t initctl_t:fifo_file getattr;
allow zabbix_agent_t proc_kcore_t:file getattr;

Zabbix 4.0.9 SELinux設定

 article  Comments Off on Zabbix 4.0.9 SELinux設定
Jun 132019
 

SELinuxを有効にしたCentOS 7にZabbix 4.0.9を導入した際のSELinux設定メモです。

CentOS 7にZabbix 4.0.9、MariaDB、Apache HTTP Serverを導入し、SELinux無効化状態であれば起動するところまで設定済みなところから始めます。

まずはApacheからZabbix Serverへの接続許可、Zabbixからのネットワーク接続を許可するbool値を設定します。httpd_can_network_connect_dbというのもありますが、今回ApacheからMySQLへの接続はUNIXドメインソケット経由となり、ネットワーク経由ではありませんので不要です。

# setsebool -P httpd_can_connect_zabbix=on
# setsebool -P zabbix_can_network=on

ポリシー作成に必要なパッケージをインストールします。

# yum install -y policycoreutils-python selinux-policy-devel

単純にZabbix Serverを起動すると、/var/run/zabbix/配下に各種ソケットファイルが作成できないとのエラーが発生しますので、そちらを許可するポリシーを作成します。

# cat zabbix-server_local.te
module zabbix-server_local 1.0;

require {
        type zabbix_var_run_t;
        type zabbix_t;
        class sock_file create;
        class sock_file unlink;
        class unix_stream_socket connectto;
}

#============= zabbix_t ==============
allow zabbix_t zabbix_var_run_t:sock_file create;
allow zabbix_t self:unix_stream_socket connectto;
allow zabbix_t zabbix_var_run_t:sock_file unlink;

作成したポリシーファイルをビルドしてインストールします。

# make -f /usr/share/selinux/devel/Makefile zabbix-server_local.pp
# semodule -i zabbix-server_local.pp

通常はここまで実施すればZabbix Server自身の稼働には問題ないのですが、Template DB MySQLを適用してMariaDBの状態を監視しようとするとエラーになります。

具体的にはmysql UNIXドメインソケットに接続できない、my.cnfファイルが読み込めずにmysqlコマンドを実行できないため、これらを許可するポリシーを作成します。

# cat zabbix-agentd_local.te
module zabbix-agentd_local 1.0;

require {
        type mysqld_etc_t;
        type mysqld_home_t;
        type mysqld_t;
        type zabbix_agent_t;
        class unix_stream_socket connectto;
        class file open;
        class file read;
}

#============= zabbix_agent_t ==============
allow zabbix_agent_t mysqld_etc_t:file open;
allow zabbix_agent_t mysqld_etc_t:file read;
allow zabbix_agent_t mysqld_home_t:file open;
allow zabbix_agent_t mysqld_home_t:file read;
allow zabbix_agent_t mysqld_t:unix_stream_socket connectto;

また、zabbixユーザーが使用する.my.cnfは/var/lib/zabbix/.my.cnfに配置しますので、こちらのファイルタイプがmysqld_home_tになるようにファイルコンテキストも作成します。

# cat zabbix-agentd_local.fc
/var/lib/zabbix/\.my\.cnf -- unconfined_u:object_r:mysqld_home_t:s0

teファイル、fcファイル両方揃ったところでビルドしインストールします。

# make -f /usr/share/selinux/devel/Makefile zabbix-agentd_local.pp
# semodule -i zabbix-agentd_local.pp

これでMySQL監視もおこなえるようになりました。

導入したポリシーモジュールは以下のプライオリティ400のものになります。

# semodule -lfull|grep zabbix
400 zabbix-agentd_local pp
400 zabbix-server_local pp
100 zabbix              pp