2016年12月22日木曜日

CodeIgniterのログがSELinuxの制限で書き込まれていない

サーバの移行が完了したものと思っていたが、CodeIgniterのログがSELinuxの制限で書き込まれていない問題があった。

今回はたまたまCodeIgniterのログを見ようと思って発見したのだが、CodeIgniter以外のフレームワーク(CakePHPやlaravel、zendframework、Symfony)でも同様の現象が発生するはず。

検索して調べるとSELinuxを使うのをやめるとか本来の対策になってないことを書いているページばかり・・・

Auditのログファイル「/var/log/audit.log」を見てみる。こんなエラーが出ているが・・・人間には意味不明。logsへのwriteがdeniedなのは判る。

type=AVC msg=audit(1482000000.454:1179): avc:  denied  { write } for  pid=1325 comm="httpd" name="logs" dev="sda2" ino=659616 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:httpd_sys_content_t:s0 tclass=dir
type=SYSCALL msg=audit(1482000000.454:1179): arch=c000003e syscall=21 success=no exit=-13 a0=7f21419efe38 a1=2 a2=0 a3=1 items=0 ppid=1065 pid=1325 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd" subj=system_u:system_r:httpd_t:s0 key=(null)

CentODでSELinuxのエラー情報を「/var/log/message」に表示するための「setroubleshoot」パッケージはインストール済みなので「/var/log/message」を見てみる。以下のようなメッセージが大量に出力されていた。

Dec 22 10:10:46 localhost setroubleshoot: SELinux is preventing httpd from write access on the directory logs. For complete SELinux messages. run sealert -l 972e9c6a-24c8-4621-8153-94c6267ee76e

「run sealert」と言っているのでsealertコマンドを実行する。結果は以下の通り。

# sealert -l 972e9c6a-24c8-4621-8153-94c6267ee76e

sealert -l 972e9c6a-24c8-4621-8153-94c6267ee76e
SELinux is preventing httpd from write access on the directory logs.

*****  Plugin httpd_write_content (92.2 confidence) suggests   ***************

If you want to allow httpd to have write access on the logs directory
Then 'logs' のラベルを変更する必要があります
Do
# semanage fcontext -a -t httpd_sys_rw_content_t 'logs'
# restorecon -v 'logs'

*****  Plugin catchall_boolean (7.83 confidence) suggests   ******************

If allow httpd to unified がしたい
Then 'httpd_unified' boolean を有効にすることにより、 これを SELinux に伝える必 要があります。
詳細情報については、'None' man ページをご覧下さい。
Do
setsebool -P httpd_unified 1

*****  Plugin catchall (1.41 confidence) suggests   **************************

If httpd に、 logs directory の write アクセスがデフォルトで許可されるべきです。
Then バグとして報告してください。
ローカルのポリシーモジュールを生成すると、
 このアクセスを許可することができます。
Do
allow this access for now by executing:
# ausearch -c 'httpd' --raw | audit2allow -M my-httpd
# semodule -i my-httpd.pp


Additional Information:
Source Context                system_u:system_r:httpd_t:s0
Target Context                unconfined_u:object_r:httpd_sys_content_t:s0
Target Objects                logs [ dir ]
Source                        httpd
Source Path                   httpd
Port                          
Host                          localhost.localdomain
Source RPM Packages
Target RPM Packages
Policy RPM                    selinux-policy-3.13.1-102.el7_3.7.noarch
Selinux Enabled               True
Policy Type                   targeted
Enforcing Mode                Enforcing
Host Name                     localhost.localdomain
Platform                      Linux localhost.localdomain 3.10.0-327.el7.x86_64
                              #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64
Alert Count                   24
First Seen                    2016-12-22 10:10:38 JST
Last Seen                     2016-12-22 10:14:00 JST
Local ID                      972e9c6a-24c8-4621-8153-94c6267ee76e

Raw Audit Messages
type=AVC msg=audit(1482000000.769:1679): avc:  denied  { write } for  pid=2794 comm="httpd" name="logs" dev="sda2" ino=787019 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:httpd_sys_content_t:s0 tclass=dir


Hash: httpd,httpd_t,httpd_sys_content_t,dir,write

最初の***** - *****のところのDoに記載されている以下のコマンドを実行する。

# cd /var/www/エラーになっているCodeIgniterの場所
# semanage fcontext -a -t httpd_sys_rw_content_t 'logs'
# restorecon -v 'logs'

Webサイトにアクセスしてみる。CodeIgniterのログが出力されるのを確認。

作業完了。

0 件のコメント:

コメントを投稿