ykominami
7/4/2013 - 11:07 PM

SELinuxの設定

SELinuxの設定 #memo

audit.log

audit2allow
semodule

setsebool -P httpd_enable_homedirs 1
(httpdがユーザのホームディレクトリを読み取ることを許可する)

getsebool -a | grep httpd
(httpdのブール値の設定)
---------------------------------------------

*audit.logからポリシーファイル作成
SELinuxを無効にして動作可能にしてログを採取

# setenforce 0
# getenforce 
Permissive

予めログを削除し,デーモンを再起動

# rm /var/log/audit/audit.log
# /etc/init.d/auditd restart
# /etc/init.d/httpd restart


grep httpd /var/log/audit/audit.log |audit2allow -M passenger

passenger.pp
passenger.te

ポリシー適用
smodule -i passenger.pp

SELinuxを有効にして,デーモン再起動

# setenforce 1
# /etc/init.d/auditd restart
# /etc/init.d/httpd restar

semodule -r passenger

------------------
*audit.log
scontextはプロセスのコンテキスト。tcontextはファイルのコンテキスト。
--------------
Passengerのルートに「httpd_sys_content_t」型のポリシーを適用
---------------------------------------

psコマンドで-Zオプションや--contextオプションを指定すると「ユーザ:ロール:ドメイン」の形式で表示される。


"path-to-passenger-root"以下のディレクトリ、ファイルに"httpd_sys_content_t"タイプを適用
chcon -R -h -t httpd_sys_content_t /path-to-passenger-root

-----------
ファイルのタイプの設定
/etc/selinux/targeted/contexts/files/
                                     file_contexts
                                     file_contexts.homedirs
                                     file_contexts.local

-
ファイルタイプ設定
目的
/home/kominami/htmldocのファイルをhttpdからアクセスできるようにする

file_contextsに以下を追加

/home/kominami/htmlcontents(/.+)? user_u:object_r:httpd_user_content_t


/home/kominami/htmldoc以下のファイルのタイプがhttpd_user_content_tする


ファイル修正後、fixfilesコマンドで反映
/sbin/fixfiles relabel /home/apache
(/home/apache配下のみ再設定する場合)


ポリシーの設定

各プログラムのアクセス制御ポリシーの設定ファイルは以下にある。
/etc/selinux/targeted/src/policy/domains/program/*.te

このファイルを修正後、/etc/selinux/targeted/src/policy配下で
# make reload
とすれば、/etc/selinux/targeted/policy/policy.19(ポリシーをバイナリ化してまとめたファイル)が更新される。


発生しているエラーをモグラ叩きで潰している場合は、audit2allow -dとすることでエラーログから*.teに追加すべきallow文を出力してくれるので、これを該当プログラムの*.teに追加すると楽をできる。-oでファイルに直接書き込むこともできる。ただし、追加するポリシーが妥当なのか(セキュリティ上問題ないのか)の検討は別途必要。
# audit2allow -d
allow fsadm_t ramfs_t:fifo_file write;
allow kudzu_t modules_conf_t:file rename;
allow pppd_t sbin_t:lnk_file read;

allow文で指定できるアクセスベクター(ファイル/ソケット等へのアクセス手段)の一覧は以下のファイルで参照できる。
/etc/selinux/targeted/src/policy/flask/access_vectors

ドメイン/タイプの定義

以下のファイルに定義がある。
/etc/selinux/targeted/src/policy/policy.conf
-------
yum install setools

yum install setroubleshoot

audit.logの例
type=AVC msg=audit(1372976406.921:3198942): avc:  denied  { connectto } for  pid=28555 comm="httpd" path="/var/run/rubygem-passenger/passenger.1.0.28555/generation-0/socket" scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unlabeled_t:s0 tclass=unix_stream_socket

「avc: denied { connectto }」 connecttoアクションがSELinuxで拒否された
scontextがソースプロセスのコンテクスト
httpd_t
tcontextはターゲットファイルのコンテクスト。
unlabeled_t
tclassの右辺部分

適当な作業ディレクトリ上で「local.te」というファイルを作成
(内容)
module local1.0;
 
require{
 type httpd_t;
 type unlabeled_t;
 class unix_stream_socket{connectto};
};
 
allow httpd_t unlabeled_t:unix_stream_socket{connectto};

-
make -f /usr/share/selinux/devel/Makefile

semodule -i local.pp

-----
chcon -R -h -t httpd_sys_content_t /usr/local/rbenv/..(略)../gems/passenger-3.0.15
ls -Z /usr/local/rbenv/..(略)../gems/passenger-3.0.15