SELinuxのCIL (Part3) — | サイオスOSS | サイオステクノロジー

SELinuxのCIL (Part3)
こんにちは。SIOS OSSエバンジェリスト/セキュリティ担当の面です。
またまた前回(Reference Policyでのポリシの作成)からかなり時間が経ってしまいましたが、今回は、いよいよCILでポリシの作成を行います。
CILに関しては、SELinux CILリファレンスガイドの翻訳を行いましたので、興味のある方は是非目を通してみてください。
また、今回からは環境としては最新のFedora25Betaを使います。
サンプルプログラムを動かす独自ドメインを作ってみる
今回、サンプルプログラムとして、第一回・第二回と同じくsend_configを使用します(起動ポートをPort80からPort65000に変更しました)。
下記のプログラムをコンパイルし、/opt/test_dir/bin/send_configとしてコピーします。
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include #include #include #include #include #include <sys/types.h> int main(int argc, char *argv[]) { int listen_fd = 0, connect_fd = 0; struct sockaddr_in serv_addr; char sendBuff[1025]; FILE *fpin; char s[256]; listen_fd = socket(AF_INET, SOCK_STREAM, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(65000); bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listen_fd, 10); while(1) { connect_fd = accept(listen_fd, (struct sockaddr*)NULL, NULL); if ((fpin = fopen("/etc/fstab","r")) == NULL) { printf("Input file open error!!\n"); exit(1); } while (fgets(s, 256, fpin) != NULL) { snprintf(sendBuff, sizeof(sendBuff), "%s", s); write(connect_fd, sendBuff, strlen(sendBuff)); } fclose(fpin); if ((fpin = fopen("/proc/cpuinfo","r")) == NULL) { printf("Input file open error!!\n"); exit(1); } while (fgets(s, 256, fpin) != NULL) { snprintf(sendBuff, sizeof(sendBuff), "%s", s); write(connect_fd, sendBuff, strlen(sendBuff)); } fclose(fpin); close(connect_fd); sleep(1); } }
また、systemdで起動するように、下記の記述の/etc/systemd/system/send_config.serviceを登録します。
Description = Send Config to remote [Service] ExecStart = /opt/test_dir/bin/send_config Restart = always Type = simple [Install] WantedBy = multi-user.target
独自ドメイン用のポリシを作成する
1. Type・ドメインをどうするか考える
send_configプログラムを動かす上で、Typeとドメインをどうするか考えます。
今回は
-
プロセス(send_config)のドメイン:sendconfig_t
-
プロセスを起動するためのプログラムのType:sendconfig_exec_t
-
プログラムのコンテキスト:system_u:object_r:sendconfig_exec_t
とします。
2. CILポリシ(例)を見てみる
CILで記述されているポリシは"/var/lib/selinux/targeted/active/modules/[module名]/cil"として配置されています。このファイルはbzip2形式で圧縮されています。
サンプルとして、apacheのポリシを見てみましょう。/var/lib/selinux/targeted/active/modules/apache/cilをコピーし、bzip2コマンドで解凍してみます。
[root@fedora25b tmp]# cp /var/lib/selinux/targeted/active/modules/targeted/active/modules/apache/cil /tmp/apache_sample.cil.bz2 [root@fedora25b tmp]# bzip2 -d /tmp/apache_sample.cil.bz2
apache_sample.cilを抜粋します。
apackeサンプルCIL(抜粋): (filecon "/var/www/cgi-bin(/.*)?" any (system_u object_r httpd_sys_script_exec_t ((s0) (s0)))) (filecon "/var/www/html/[^/]*/cgi-bin(/.*)?" any (system_u object_r httpd_sys_script_exec_t ((s0) (s0)))) (filecon "/var/www/html(/.*)?/sites/default/settings\.php" file (system_u object_r httpd_sys_rw_content_t ((s0) (s0)))) (filecon "/var/www/html(/.*)?/sites/default/files(/.*)?" any (system_u object_r httpd_sys_rw_content_t ((s0) (s0)))) (filecon "/var/www/html/configuration\.php" any (system_u object_r httpd_sys_rw_content_t ((s0) (s0)))) (typealiasactual httpd_sys_content_rw_t httpd_sys_rw_content_t) (typealias httpd_sys_content_ra_t) (typealiasactual httpd_sys_content_ra_t httpd_sys_ra_content_t) (allow httpd_t httpd_exec_t (file (entrypoint))) (allow httpd_t httpd_exec_t (file (ioctl read getattr lock execute execute_no_trans open))) (allow httpd_suexec_t httpd_suexec_exec_t (file (entrypoint))) (allow httpd_suexec_t httpd_suexec_exec_t (file (ioctl read getattr lock execute execute_no_trans open)))
見てお分かりの通り、CILの特徴は下記のようなものになります:
-
見てすぐわかる通り、CILではS式を用いて構成されています。従って、丸括弧"()"で各項目が区切られています。例えば
(typeattributeset cil_gen_require var_t)
のように一組の丸括弧で囲われている単純なものから(allow httpd_suexec_t httpd_log_t (file (create getattr open)))
のように、括弧でネストされて使われているものまであります。 -
LISPを元に文法が構成されており
-
シンボル - スペースを含まない文字列(例えば、type, httpd_t, user_rなど)
-
クォートされた文字列 - クォートで閉じられた文字列(例えば、"^/usr/shared/*.?"など)
-
IPv4とIPv6アドレス - v4/v6のIPアドレス(例えば、127.0.0.1など)
-
数字 - 数字は単純な整数(例えば、443など)
のパーツによって構成されています。
-
3. まずは作成してみる
上述のapacheのCILポリシファイルを参考にし、下記のようなポリシファイルを/root/customized_policies/sendconfigとして作成しました。
; Declare a sendconfig related type (type sendconfig_t) (type sendconfig_exec_t) ; Assign the type to the object_r role (roletype system_r sendconfig_t) (roletype object_r sendconfig_exec_t) ; Assign the right set of attributes to the port (typeattributeset entry_type sendconfig_exec_t) (typeattributeset exec_type sendconfig_exec_t) (typeattributeset file_type sendconfig_exec_t) (typeattributeset non_security_file_type sendconfig_exec_t) (typeattributeset non_auth_file_type sendconfig_exec_t) ; Assign type entry point (allow sendconfig_t sendconfig_exec_t (file (entrypoint))) (allow sendconfig_t sendconfig_exec_t (file (ioctl read getattr lock execute exe cute_no_trans open))) (typetransition initrc_domain sendconfig_exec_t process sendconfig_t) (allow sendconfig_t sendconfig_exec_t (file (ioctl read getattr lock execute exe cute_no_trans open))) (allow svc_run_t sendconfig_exec_t (file (read getattr execute open))) (allow svc_run_t sendconfig_t (process (transition))) (typetransition svc_run_t sendconfig_exec_t process sendconfig_t) (filecon "/opt/test_dir/bin/send_config" file (system_u object_r sendconfig_exec _t ((s0) (s0))))
4. モジュールをコンパイルしてロードする
モジュールをコンパイルしてロードするには、semoduleコマンドを使用します。
semodule -l: ロードされているモジュールの一覧を表示
semodule -i: 引数で与えられたファイルをコンパイルしてモジュールとしてロードする。
semodule -e: 引数で与えられたファイルモジュールロードする。
semodule -r: 引数で与えられたファイルモジュールを削除する。
となっていますので、上述の"-i"オプションを使用して
semodule -i sendconfig
を実行します。
4. コンテキストを付け直す
/opt/test_dir/bin/sendconfig ファイルのコンテキストをsystem_u:object_r:sendconfig_exec_t:s0に変更します。
[root@fedora25b sendconfig]# ls -lZ /opt/test_dir/bin/ |sed s/omok/sios/g 合計 24 -rwxrwxr-x. 1 sios sios system_u:object_r:sendconfig_exec_t:s0 9336 10月 17 11:47 send_config
5.SELinuxをPermissiveモードにする。
SELinuxのモードを
[root@fedora25b sendconfig]# setenforce 0
として、Permissiveモードにしておきます。
6. サービスを立ち上げ直して、監査ログ(/var/log/audit以下)を確認する。
サービスの立ち上げ直しは、
systemctl stop send_config systemctl start send_config
で大丈夫です。
7. プロセスのドメインを確認する。
send_config サービスが起動したら、"ps axZ"などで確認します。
[root@fedora25b sendconfig]# ps axZ|grep send system_u:system_r:sendconfig_t:s0 1730 ? Ss 0:00 /opt/test_dir/bin/send_config
まとめ
今回は、sendconfig_tドメインで起動したところまでで一旦休憩です。次回は後半戦として、このポリシをログからカスタマイズしていきます。
[セミナー告知]
11/30(水)に「OSSセキュリティナイター vol.3」と題して、セキュリティのセミナーを行います。
この回では、世界で最も利用されているオープンソースデータベースであるMySQLを取り上げ、『MySQLデータベースのセキュリティを考える 〜 重要データを守るために必要なこと〜』と題してセミナーを開催します。
今回も、前回に引き続き、ゲスト講師としてMySQLスペシャリストをお招きし講演をいただきます。
http://connpass.com/event/44819/がプログラム内容と申し込みの詳細になりますので、是非お申し込み下さい。
OSSに関するお困りごとは サイオス OSSよろず相談室まで
サイオスOSSよろず相談室 では、OSS を利用する中で発生する問題に対し、長年培ってきた技術力・サポート力をもって企業の OSS活用を強力に支援します。Red Hat Enterprise Linux のほか、CentOS をご利用され ている環境でのサポートも提供いたします。
- セキュリティ分野別一覧
- 最新の記事
- Linux Kernel 等の脆弱性(CVE-2018-8897, CVE-2018-1087) 2018年05月09日
- 389-ds-baseの脆弱性(CVE-2018-1089) 2018年05月09日
- wgetの脆弱性(CVE-2018-0494) 2018年05月07日
- Ansible Towerの脆弱性(CVE-2018-1101) 2018年05月03日
- python-paramikoの脆弱性(CVE-2018-7750) 2018年05月03日
- OpenShiftの脆弱性(CVE-2018-1102) 2018年05月02日
- PHPの複数の脆弱性(CVE-2018-10546, CVE-2018-10547, CVE-2018-10548, CVE-2018-10549) 2018年05月02日
- Linux Kernelの脆弱性(CVE-2018-1000199) 2018年05月02日
- GlusterFSサーバの脆弱性(CVE-2018-1112) 2018年04月27日
- nmapの脆弱性(CVE-2018-1000161) 2018年04月25日
- PackageKitの脆弱性(CVE-2018-1106) 2018年04月25日
- phpMyAdminの脆弱性(CVE-2018-10188) 2018年04月25日
- Linux Kernelに複数の脆弱性(CVE-2018-10322, CVE-2018-10323) 2018年04月25日
- Linux Kernelの脆弱性(CVE-2018-8781) 2018年04月25日
- GlusterFSの脆弱性(CVE-2018-1088) 2018年04月19日
- MySQLの脆弱性(Oracle Critical Patch Update Advisory - Apr 2018) 2018年04月19日
- Oracle Javaの脆弱性(Oracle Critical Patch Update Advisory - Apr 2018) 2018年04月18日
- Linux Kernelの脆弱性 (CVE-2018-10124) 2018年04月17日
- OpenSSLの脆弱性情報 ( CVE-2018-0737 : Low) 2018年04月17日
- GNU Patchの脆弱性(CVE-2018-1000156) 2018年04月08日
フォローしませんか?