LoginSignup
18
13

More than 5 years have passed since last update.

CentOS7/RHEL7でむやみにSELinuxポリシー追加せずにnginx apacheをEnforcingのまま動かす

Last updated at Posted at 2016-11-16

パーミッションは正しいのにページが表示されない。
setenforce 0 で動いた。

どうする?

  • SELinuxをPermissiveにしよう!
  • audit2allowで弾かれたログからポリシー作って追加しよう!

というのを良く見かけるけど、セキュリティレベルを落としたり、むやみにポリシーが追加されるのも避けたい…

ってことで調べてみました。

とりあえずやり方

デフォルトのままの状態

sestatusで確認してみます。

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

インストールしたばかりの環境ではtargetedポリシーがenforcingモードにっています。
なので、nginx/apacheのプロセスと、セキュリティラベルのターゲットが適切であればよさそうです。

ここのへんを参考に、コンテンツファイルのラベルを変更するのと、Booleanのon/offでなんとかなりそうです。

プロセスのターゲットを確認する

psコマンドに-Zオプションを追加して、セキュリティラベルを表示します。
nginxの場合

$ ps -eZ | grep nginx
system_u:system_r:httpd_t:s0     8461 ?        00:00:00 nginx
system_u:system_r:httpd_t:s0     8488 ?        00:00:06 nginx
system_u:system_r:httpd_t:s0     8489 ?        00:00:16 nginx

末尾に_tが付いているhttpd_tがターゲットです。

ファイルのセキュリティラベルを確認する

/usr 配下の場合

$ cd /usr/share/nginx
$ ll -Z
-rw-r--r--. root root system_u:object_r:usr_t:s0       404.html
-rw-r--r--. root root system_u:object_r:usr_t:s0       50x.html
-rw-r--r--. root root system_u:object_r:usr_t:s0       index.html
-rw-r--r--. root root system_u:object_r:usr_t:s0       nginx-logo.png
-rw-r--r--. root root system_u:object_r:usr_t:s0       poweredby.png

末尾に_tが付いているusr_tがターゲットです。

コンテンツのファイルに適切なターゲットを設定

httpd_tがラベル付けされたプロセスに許可されているターゲットのラベルをファイルに付けてあげます。

読み取りはhttpd_sys_content_t

読み書き(アップロードフォルダなど)はhttpd_sys_rw_content_t
を設定します。

ターゲットを変更するには

$ sudo chcon -t httpd_sys_content_t ファイル

その他

ポートを80以外にしたいなど、デフォルト以外の設定を入れ込むたびにSELinuxに引っかかることがあります。

$ sudo tail /var/log/audit/audit.log | grep denied

などで拒否されたログを確認し、以下を参照しながら適切なラベル付けをしていってください。

参考

RedHat: Apache HttpServer
RedHat: ターゲット ポリシー

18
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
13