SELinux targetedポリシーで、UserDirやVirtualDomainへのアクセスを許可するメモ

以下、激しくイマサラな個人的なメモなので、検索でここに来ちゃった方は、以下へどうぞm(_._)m

環境は、

  • Xen3のDom-UでCentOS5(yum update済み):2.6.18-53.el5xen
  • httpd-2.2.3-11.el5_1.centos.3
  • SELinuxは:SELINUX=enforcing、SELINUXTYPE=targeted、Policy version: 21

古いWebサーバから、Xen3の仮想サーバへ引越しをしている。

いくつかのユーザが~/public_html 以下にコンテンツを持っていて、バーチャルドメインもいくつか運用している。
まず、/etc/passwdや/etc/shadow, /etc/groupの内容を移し、それからrsyncで/home以下を同期。
面倒なのは、メールが~/Maildir以下にあることだが、今回の件とは関係ないので省略。

設定ファイル

まず、設定ファイル(/etc/httpd/conf/*, /etc/httpd/conf.d/* )を新しい環境にコピーして、/usr/sbin/httpd -S を実行してエラーが出なくなるまで修正。
ありがちなのは、サーバの鍵の設定とか。

ただし、この時点でservice httpd startを実行してもエラーになる。
/var/log/messages を見ると、setroubleshoot が

May 30 01:02:34 webserver setroubleshoot:
SELinux is preventing the /usr/sbin/httpd from using potentially mislabeled files (/home/httpd/html).      
For complete SELinux messages. run sealert -l xxxxxx-xxxx-xxxx-xxx-xxxxx

とかいうメッセージを出している。
で、run sealert -l xxxxxx-xxxx-xxxx-xxx-xxxxxすると、以下のようなメッセージが出てくる

Summary
    SELinux is preventing the /usr/sbin/httpd from using potentially mislabeled
    files (/home/httpd/html).

Detailed Description
    SELinux has denied /usr/sbin/httpd access to potentially mislabeled file(s)
    (/home/httpd/html).  This means that SELinux will not allow /usr/sbin/httpd
    to use these files.  It is common for users to edit files in their home
    directory or tmp directories and then move (mv) them to system directories.
    The problem is that the files end up with the wrong file context which
    confined applications are not allowed to access.

Allowing Access
    If you want /usr/sbin/httpd to access this files, you need to relabel them
    using restorecon -v /home/httpd/html.  You might want to relabel the entire
    directory using restorecon -R -v /home/httpd.

Additional Information

Source Context                root:system_r:httpd_t
Target Context                root:object_r:user_home_t
Target Objects                /home/httpd/html [ dir ]
Affected RPM Packages         httpd-xxxxx
Policy RPM                    selinux-policy-2xxxxx
Selinux Enabled               True
Policy Type                   targeted
MLS Enabled                   True
Enforcing Mode                Permissive
Plugin Name                   plugins.home_tmp_bad_labels
Host Name                     webserver
Platform                      Linux xxxxxxxxxx
                              MMM DDD .....
Alert Count                   4
Line Numbers

Raw Audit Messages

avc: denied { getattr } for comm="httpd" dev=dm-0 egid=0 euid=0
exe="/usr/sbin/httpd" exit=0 fsgid=0 fsuid=0 gid=0 items=0
path="/home/httpd/html" pid=nnnn scontext=root:system_r:httpd_t:s0 sgid=0
subj=root:system_r:httpd_t:s0 suid=0 tclass=dir
tcontext=root:object_r:user_home_t:s0 tty=pts0 uid=0

これで、"Allowing Access"の部分を参考にすれば、なんとかなるケースも多いが、パスが絶対パスでない場合もあったりして、頭を抱えることになることもある。

そこで以下、対処方法を検索した結果です。これで一応httpdは起動するようにはなりましたが、CGIとかphpはまだ動作が不明です。結局はpermissiveにして、様子を見る予定(ぉぃ

ユーザのコンテンツをアクセス可能にする。

FC4/Apache/一般ユーザのウェブ表示 - Pocketstudio.jp Linux Wikiによれば、

Fedora Core 4 では予め /home/ユーザ名/public_html に対するセキュリティコンテキストが設定されています。

ということなので、public_htmlを持っている各ユーザに対して、restoreconコマンドを実行します。

# cd /home
# find . -type d -path "*public_html" | awk -F/ '{print $2}' | while read i
> do
> /sbin/restorecon -R /home/$i
> done

バーチャルドメインのコンテンツをアクセス可能にする

上記の参考URLの通りで、あとは、httpdの設定ファイルから、DocumentRootがある行を検索して、対応するディレクトリをchconします。

# grep [dD]ocument[rR]oot /etc/httpd/conf/設定ファイル | awk '{print $2}' | while read i
> do 
> chcon -R -t httpd_user_content_t $i
> done
# setenforce 1
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]