SELinuxの設定

 

SELinuxの一時的無効化方法

SELinuxが機能している時、以下の方法で一時的に無効化できます。

# getenforce
Enforcing       ← SELinux機能は有効でアクセス制御も有効。
Permissive      ← SElinuxはwarningを出すが、アクセス制限は行われない。
Disabled        ← SElinux機能・アクセス制御ともに無効。
# setenforce 0 ← EnforcingからPermissiveに変更。Disabledには出来ない。
# setenforce 1 ← 上記のPermissiveからEnforcingに戻す。

注:Disabledの場合、Enforcing、Permissiveに変更はできません。

SELinuxの恒常的変更方法

以下はEnforcingからDisabledに変更した例です。

# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=enforcing
  
SELINUX=disabled  ← disabledにする。
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

# SETLOCALDEFS= Check local definition changes
SETLOCALDEFS=0

変更の反映にはLinuxの再起動が必要です。

SELinuxの各パラメータの設定方法

# getsebool パラメータ名      ← パラメータ名の値を表示する場合。
# getsebool -a               ← 全てのパラメータの値を表示する場合。
# getsebool -a | grep httpd   ← httpdをパラメータ名に持つパラメータの値を表示する場合。
# setsebool パラメータ名=1    ← パラメータ名の値を一時的にonにする。
# setsebool パラメータ名=0    ← パラメータ名の値を一時的にoffにする。
# setsebool -P パラメータ名=1 ← パラメータ名の値を恒常的にonにする。

SELinuxに起因する問題

以前Centos V5.4でシステム構築中(このサイトではありません)にSELinuxに制限されてアプリケーションが動かないという問題が幾つか発生しました。これらは、アプリケーション外のSELinuxの制限によるため、原因発見から解決まで時間がかかります。以下はその内容です。

/selinux/contextへの書込み許可がない。

設定値:SELinuxをPermissive (setenforce 0)
問題の症状:rpmでアプリケーションをインストールしようとした時以下のエラーが出てインストールできない。
(パッケージ作成ツール導入 の場合)
エラー: アーカイブの伸長に失敗: ファイル /selinux/context;4bd5db86: cpio: open失敗 - 許可がありません

samba_enable_home_dirs

設定値:on
問題の症状:Sambaでアクセスできない。

httpd_disable_trans

設定値:on
問題の症状:Ruby on Railsのアプリケーションにブラウザーでアクセスすると、以下のようなdispatch.cgiが無いというメッセージが表示され動かない。


Not Found

The requested URL /redmine/dispatch.cgi was not found on this server.


解析内容:
/var/log/httpd/error_logに以下のようなhttpd起動時にpassengerのエラーがある。

[Sun May 09 18:44:48 2010] [error] *** Passenger could not be initialized because of this
error: Cannot create FIFO file /tmp/passenger.2886/.guard: Permission denied (13)

また、ブラウザーからアプリケーションにアクセスした時は以下のエラーがある。

[Sun May 09 21:07:09 2010] [error] [client 192.168.100.1] script not found or unable to stat: 
/var/www/html/redmine/dispatch.cgi
dispatch.cgi was not found

ps -efで稼動しているプロセスを調べると以下のPassengerのプロセスが無い。
/usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/ApplicationPoolServerExecutable
Passenger spawn server

mysqld_disable_trans

設定値:on
問題の症状:MySQLを初めて起動したときにmysql_install_dbが実行されるが、以下のようなエラーメッセージが表示され初期化できなくて起動に失敗する。

MySQL データベースを初期化中:  Installing MySQL system tables...
   :
   :
100514  2:33:38 [Warning] Can't create test file /apli/db/mysql/data/host.lower-test
/usr/libexec/mysqld: Can't change dir to '/apli/db/mysql/data/' (Errcode: 13)
100514  2:33:38 [ERROR] Aborting
   :
   :
And if you do mail us, you MUST use the /usr/bin/mysqlbug script!
                                                          [失敗]

解析内容:
DB作成ディレクトリーをデフォルト以外に設定するとSELinuxによるMySQLへのアクセス制限のためdata/以下にアクセス出来なくてエラーとなる。
初期化失敗後にデータベースの初期化を再度実行するためには、上記の例の場合は/apli/db/mysql/data/以下のディレクトリー、ファイルを削除する。

 

バーチャルホストのURLがForbiddenで表示できない。

問題の症状:バーチャルホストを設定したが、そのURLにアクセスしたところ403エラー(Forbidden)等が表示されて本来のHPが表示されない。
解析内容:
audit.logに以下のような対象となるバーチャルホストのDocumentRoot(この例の場合/home/user/public_html/index.html)でtype=AVCのエラーがありました。

# view /var/log/audit/audit.log
type=AVC msg=audit(1377959407.008:29): avc:  denied  { getattr } for  pid=1348
 comm="httpd" path="/home/user/public_html/index.html" dev=dm-0 ino=147403
 scontext=system_u:system_r:httpd_t:s0
 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file

これはhttpd_tドメインとして実行されているプロセスがhome_root_tというタイプのファイルである/home/user/public_html/index.htmlにアクセスしようとして拒否されたことを表します。
http_tドメインには、タイプがhttpd_sys_content_tであるファイルやディレクトリについての読み取りは許可されていますが、home_root_tというタイプに対しては許可されていません。
バーチャルホストのDocumentRootのファイルタイプを調べるとhttpd_sys_content_tの設定がありませんでした。

# ls -dZ /home/user/public_html
drwxr-xr-x. apache apache unconfined_u:object_r:home_root_t:s0 /home/user/public_html

設定方法:
semanageコマンドを使用してhttpdに対して、バーチャルホストのDocumentRootへのアクセス許可を与えます。
現在のhttpdへのファイルタイプの設定値を調べてみます。-lオプションはリストの表示です。

# semanage fcontext -l | grep httpd_sys_content_t
/etc/htdig(/.*)?                                   all files          system_u:object_r:httpd_sys_content_t:s0 
/srv/([^/]*/)?www(/.*)?                            all files          system_u:object_r:httpd_sys_content_t:s0 
/srv/gallery2(/.*)?                                all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/drupal.*                                all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/htdig(/.*)?                             all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/icecast(/.*)?                           all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/mythtv/data(/.*)?                       all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/mythweb(/.*)?                           all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/ntop/html(/.*)?                         all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/openca/htdocs(/.*)?                     all files          system_u:object_r:httpd_sys_content_t:s0 
/usr/share/selinux-policy[^/]*/html(/.*)?          all files          system_u:object_r:httpd_sys_content_t:s0 
/var/lib/cacti/rra(/.*)?                           all files          system_u:object_r:httpd_sys_content_t:s0 
/var/lib/graphite-web(/.*)                         all files          system_u:object_r:httpd_sys_content_t:s0 
/var/lib/htdig(/.*)?                               all files          system_u:object_r:httpd_sys_content_t:s0 
/var/lib/trac(/.*)?                                all files          system_u:object_r:httpd_sys_content_t:s0 
/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0 
/var/www/icons(/.*)?                               all files          system_u:object_r:httpd_sys_content_t:s0 
/var/www/svn/conf(/.*)?                            all files          system_u:object_r:httpd_sys_content_t:s0 

上記の中でデフォルトで使用される/var/wwwが設定されていることが分かります。
同様にして/home/user/public_htmlをSELinuxに設定します。

# semanage fcontext -a -t httpd_sys_content_t "/home/user/public_html(/.*)?"

-aオプションは設定の追加、-tオプションは設定するタイプです。
設定値を確認します。lオプションに大文字のCをオプションとして追加指定すると、デフォルトのポリシーに存在しない、追加された定義情報のみを表示します。

# semanage fcontext -lC
SELinux fcontext                                   タイプ                コンテキスト

/home/user/public_html(/.*)?                                 all files          system_u:object_r:httpd_sys_content_t:s0

次にバーチャルホストのDocumentRootへファイルタイプを設定します。

# restorecon -R /home/user/public_html/

設定を確認します。

# ls -dZ /home/user/public_html
drwxr-xr-x. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 /home/user/public_html

この状態で先ほどと同様にindex.htmlへアクセスし、拒否されずに内容が表示されることを確認します。

Postfixでバーチャルメールボックスに設定したディレクトリーにpostfixがMaildirディレクトリーを作成できない。

問題の症状:Postfixでバーチャルメールボックスを設定したが、メールボックスの同期をした時にエラーとなり、メールの受信ができていませんでした。
解析内容:
dovecotのログに以下のようにSpool下にHome Directoryを作成しようとして、Permission deniedで失敗していました。

# view /var/log/dovecot.log
Jan 24 13:56:49 imap-login: Info: Login: user=<admin@centos6.com>, method=PLAIN, rip=192.168.100.1, lip=192.168.100.136, mpid=25438
Jan 24 13:56:49 imap(admin@centos6.com): Debug: Effective uid=10000, gid=10000, home=/var/spool/vmail/centos6.com/admin
Jan 24 13:56:49 imap(admin@centos6.com): Debug: Home dir not found: /var/spool/vmail/centos6.com/admin
Jan 24 13:56:49 imap(admin@centos6.com): Debug: maildir++: root=/var/spool/vmail/centos6.com/admin/Maildir, index=, control=, inbox=/var/spool/vmail/centos6.com/admin/Maildir
Jan 24 13:56:49 imap(admin@centos6.com): Debug: Namespace : Permission lookup failed from /var/spool/vmail/centos6.com/admin/Maildir
Jan 24 13:56:49 imap(admin@centos6.com): Debug: Namespace : Using permissions from /var/spool/vmail/centos6.com/admin/Maildir: mode=0700 gid=-1
Jan 24 13:56:49 imap(admin@centos6.com): Error: user admin@centos6.com: Initialization failed: Initializing mail storage from mail_location setting failed: mkdir(/var/spool/vmail/centos6.com/admin/Maildir) failed: Permission denied (euid=10000(vmail) egid=10000(vmail) missing +w perm: /var/spool/vmail, euid is not dir owner)
Jan 24 13:56:49 imap(admin@centos6.com): Error: Invalid user settings. Refer to server log for more information.

これはVirtualユーザー用のアカウントvmail(euid=10000 egid=10000)でspool下にメールユーザーのMaildirディレクトリーを作成しようとして、拒否されたことを表します。
現在のdovecotへのファイルタイプの設定値を調べてみます。-lオプションはリストの表示です。dovecotについて以下のようなファイルタイプが表示されました。

# semanage fcontext -l | grep dovecot
/etc/dovecot(/.*)?                                 all files          system_u:object_r:dovecot_etc_t:s0
/etc/dovecot\.conf.*                               all files          system_u:object_r:dovecot_etc_t:s0
/etc/dovecot\.passwd.*                             all files          system_u:object_r:dovecot_passwd_t:s0
/etc/pki/dovecot(/.*)?                             all files          system_u:object_r:dovecot_cert_t:s0
/etc/rc\.d/init\.d/dovecot                         regular file       system_u:object_r:dovecot_initrc_exec_t:s0
/usr/libexec/dovecot/auth                          regular file       system_u:object_r:dovecot_auth_exec_t:s0
/usr/libexec/dovecot/deliver                       regular file       system_u:object_r:dovecot_deliver_exec_t:s0
/usr/libexec/dovecot/dovecot-auth                  regular file       system_u:object_r:dovecot_auth_exec_t:s0
/usr/libexec/dovecot/dovecot-lda                   regular file       system_u:object_r:dovecot_deliver_exec_t:s0
/usr/sbin/dovecot                                  regular file       system_u:object_r:dovecot_exec_t:s0
/usr/share/ssl/certs/dovecot\.pem                  regular file       system_u:object_r:dovecot_cert_t:s0
/usr/share/ssl/private/dovecot\.pem                regular file       system_u:object_r:dovecot_cert_t:s0
/var/lib/dovecot(/.*)?                             all files          system_u:object_r:dovecot_var_lib_t:s0
/var/log/dovecot(/.*)?                             all files          system_u:object_r:dovecot_var_log_t:s0
/var/log/dovecot\.log.*                            all files          system_u:object_r:dovecot_var_log_t:s0
/var/run/dovecot(-login)?(/.*)?                    all files          system_u:object_r:dovecot_var_run_t:s0
/var/run/dovecot/login/ssl-parameters.dat          regular file       system_u:object_r:dovecot_var_lib_t:s0
/var/spool/dovecot(/.*)?                           all files          system_u:object_r:dovecot_spool_t:s0

dovecotのspool用のファイルタイプは最後のdovecot_spool_tのようです。

設定方法:
semanageコマンドを使用してdovecotに対して、バーチャルメールボックスのspoolディレクトリーへのアクセス許可を与えます。
-aオプションは設定の追加、-tオプションは設定するタイプです。

# semanage fcontext -a -t dovecot_spool_t "/var/spool/vmail(/.*)?"

設定値を確認します。lオプションに大文字のCをオプションとして追加指定すると、デフォルトのポリシーに存在しない、追加された定義情報のみを表示します。

# semanage fcontext -lC
SELinux fcontext                                   タイプ                コンテキスト
/var/spool/vmail(/.*)?                             all files          system_u:object_r:dovecot_spool_t:s0

現在の設定を確認します。

# ls -dZ /var/spool/vmail
drwx------. root root unconfined_u:object_r:var_spool_t:s0 /var/spool/vmail

次にバーチャルメールボックスディレクトリーへファイルタイプを設定します。

# restorecon -R /var/spool/vmail

設定結果を確認します。

# ls -dZ /var/spool/vmail
drwx------. root root unconfined_u:object_r:dovecot_spool_t:s0 /var/spool/vmail

Postfixがバーチャルメールボックスに受信メールのファイルを作成できない。

問題の症状:
メールをバーチャルメールアドレスに送信しクライアント側にはエラーが無かったのですが、メールを送信できていませんでした。
解析内容:
maillogに以下のPermission deniedエラーがありました。

# view /var/log/maillog
Jan 14 20:37:21 acompass postfix/virtual[12796]: warning: maildir access problem for UID/GID=1000/1000: create maildir file /var/spool/vmail/sub.acompass.com/user/Maildir/tmp/1418557041.P12796.acompass.acompass.net: Permission denied
Jan 14 20:37:21 acompass postfix/virtual[12796]: warning: perhaps you need to create the maildirs in advance

そしてaudit.logに以下のようなdenied { create }のavcエラーがありました。

# view /var/log/audit/audit.log
Jan 14 20:46:31 acompass kernel: type=1400 audit(1418557591.569:267052): avc:  denied  { create } for  pid=12841 comm="virtual" name="sub.acompass.net" scontext=system_u:system_r:postfix_virtual_t:s0 tcontext=system_u:object_r:dovecot_spool_t:s0 tclass=dir

前述で設定したコンテキストタイプdovecot_spool_tに対して、コンテキストタイプpostfix_virtual_tの権限の設定が必要なようです。
対応内容:
audit2allowでSELinuxのPolicy packageを作成します。 先ず、audit2whyで調べます。grepのキーワードは何でもよいのですがここでは"12841"を使用しています。

# grep 12871  /var/log/audit/audit.log | audit2why
Jan 14 20:56:31 acompass kernel: type=1400 audit(1418558191.243:267053): avc:  denied  { create } for  pid=12871 comm="virtual" name="sub.acompass.net" scontext=system_u:system_r:postfix_virtual_t:s0 tcontext=system_u:object_r:dovecot_spool_t:s0 tclass=dir

        Was caused by:
                Missing type enforcement (TE) allow rule.

                You can use audit2allow to generate a loadable module to allow this access.

audit2allowでPolicy packageを作成します。ここではPackage名をvmailとしています。

# grep 12871  /var/log/audit/audit.log | audit2allow -M vmail

******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i vmail.pp

teファイルの内容は以下の通りでした。

# view vmail.te
module vmail 1.0;

require {
        type postfix_virtual_t;
        type dovecot_spool_t;
        class dir create;
}

#============= postfix_virtual_t ==============
allow postfix_virtual_t dovecot_spool_t:dir create;

作成されたPolicy package vmail.ppをインストールします。

# semodule -i vmail.pp

PostgreSQLでdataのディレクトリーをDefaultから変更

問題の症状:PostgreSQLでdataのディレクトリーをDefaultから変更したところ、initdbでFAILEDとなる。
解析内容:
postgresqlのログに以下のようなPermissionエラーがある。

# view /home/postgres/pgstartup.log
could not change directory to "/home/postgres"
initdb: could not access directory "/data/pgsql": Permission denied
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

semanageコマンドでpostgresqlのファイルタイプを調べます。

# semanage fcontext -l | grep postgresql | grep data
/var/lib/pgsql/data(/.*)?                          all files          system_u:object_r:postgresql_db_t:s0

上記よりファイルタイプはpostgresql_db_tです。

設定方法:
semanageコマンドを使用してpostgresqlのDataのディレクトリーへファイルタイプを設定します。
-aオプションは設定の追加、-tオプションは設定するタイプです。

# semanage fcontext -a -t postgresql_db_t "/data/pgsql(/.*)?"

設定値を確認します。

# semanage fcontext -lC
SELinux fcontext                                   type               Context

/data/pgsql(/.*)?                                  all files          system_u:object_r:postgresql_db_t:s0

現在の設定を確認します。

# ls -dZ /data/pgsql
drwxr-xr-x. postgres postgres unconfined_u:object_r:default_t:s0 /data/pgsql

次にバーチャルメールボックスディレクトリーへファイルタイプを設定します。

# restorecon -R /data/pgsql

設定結果を確認します。

# ls -dZ /data/pgsql
drwxr-xr-x. postgres postgres unconfined_u:object_r:postgresql_db_t:s0 /data/pgsql

httpd_can_network_connect_db

設定値:on
問題の症状:ApacheからPostgreSQL/MySQLに接続できない。
解析内容:
SELinuxのログに以下のようなacv:deniedがある。

# view /var/log/messages
Jan 01 00:00:00 Hostname kernel: type=1400 audit(1405261224.356:11619): avc:  denied  {
name_connect } for  pid=12345 comm="httpd" dest=5432 scontext=system_u:system_r:httpd_t:s0
 tcontext=system_u:object_r:postgresql_port_t:s0 tclass=tcp_socket

PostgreSQLでPortをDefaultから変更

問題の症状:PostgreSQLでPortをDefaultから変更したところ、起動後停止する。
解析内容:
postgresqlのログに以下のようなPortのPermissionエラーがある。

# view /home/postgres/pgstartup.log
LOG:  could not bind IPv6 socket: Permission denied
HINT:  Is another postmaster already running on port 5433? If not, wait a few seconds and
retry.
LOG:  could not bind IPv4 socket: Permission denied
HINT:  Is another postmaster already running on port 5433? If not, wait a few seconds and
retry.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets

semanageコマンドでpostgresqlに許可されたportを調べます。

# semanage port -l | grep postgresql
postgresql_port_t              tcp      5432

上記よりPostgreSQLに許可されているPortはDefaultの5432であることが分かります。

設定方法:
前提:iptablesで該当のPortが開いていること
ここの例でのPort No. = 5433
semanageコマンドを使用してpostgresqlに許可するPortを追加します。
-aオプションは設定の追加、-tオプションは設定するタイプです。

# semanage port -a -t postgresql_port_t -p tcp 5433

設定値を確認します。

# semanage port -l | grep postgres
postgresql_port_t              tcp      5433, 5432

PostgreSQLを起動します。

# service postgresql start
Starting postgresql service:                               [  OK  ]

PostgreSQLの起動を確認します。

# ps -ef | postgres
postgres  7736     1  0 Jan01 ?        00:00:00 /usr/bin/postmaster -p 5433 -D /data/pgsql
postgres  7742  7736  0 Jan01 ?        00:00:00 postgres: logger process
postgres  7744  7736  0 Jan01 ?        00:00:00 postgres: writer process
postgres  7745  7736  0 Jan01 ?        00:00:00 postgres: wal writer process
postgres  7746  7736  0 Jan01 ?        00:00:00 postgres: autovacuum launcher process
postgres  7747  7736  0 Jan01 ?        00:00:00 postgres: stats collector process
root      7767  7692  0 00:03 pts/0    00:00:00 grep postgres

最終更新のRSS
Last-modified: 2014-12-16 (火) 15:15:26 (3417d)