CentOS 6
Sponsored Link

SELinux : ファイルのタイプを変更する2016/07/26

 
ファイルのタイプを変更することにより、個々のファイルに対する SELinux によるアクセス制御設定を変更することができます。
以下では「targeted」ポリシー適用下の環境で例示しています。
[1] ファイルへのラベリングのデフォルトとなる設定はポリシーディレクトリ配下の contexts/files 配下にあります。
「***.bin」はバイナリファイルとなっており、「.bin」ではないファイルを開くと、ファイルタイプも含めてどのような設定になっているか参照できます。
[root@dlp ~]#
ll /etc/selinux/targeted/contexts/files

total 584
-rw-r--r--. 1 root root 291724 Jul 28 11:21 file_contexts
-rw-r--r--. 1 root root   6405 Jul 28 11:21 file_contexts.homedirs
-rw-r--r--. 1 root root 290594 Aug 17  2015 file_contexts.pre
-rw-r--r--. 1 root root    139 May 11 18:45 media

[root@dlp ~]#
head /etc/selinux/targeted/contexts/files/file_contexts

/.*     system_u:object_r:default_t:s0
/[^/]+  --      system_u:object_r:etc_runtime_t:s0
/a?quota\.(user|group)  --      system_u:object_r:quota_db_t:s0
/nsr(/.*)?      system_u:object_r:var_t:s0
/sys(/.*)?      system_u:object_r:sysfs_t:s0
/xen(/.*)?      system_u:object_r:xen_image_t:s0
/mnt(/[^/]*)    -l      system_u:object_r:mnt_t:s0
/mnt(/[^/]*)?   -d      system_u:object_r:mnt_t:s0
/bin/.* system_u:object_r:bin_t:s0
/dev/.* system_u:object_r:device_t:s0
[2]
例として httpd で CGI を利用する際のファイルタイプについて設定します。
httpd での CGI 実行許可のブール値はデフォルトはオンになっています。
また httpd のデフォルトで許可されている CGI の設置場所である "/var/www/cgi-bin/" には、以下のように「httpd_sys_script_exec_t」というタイプがデフォルトで設定されるようになっています。
よって、デフォルトでは "/var/www/cgi-bin/" 配下に設置した CGI であれば SELinux で許可されているため、実行可能となっています。
[root@dlp ~]#
semanage boolean -l | grep httpd_enable_cgi

httpd_enable_cgi               (on   ,   on)  Allow httpd cgi support

[root@dlp ~]#
grep "cgi" /etc/selinux/targeted/contexts/files/file_contexts | grep "httpd"

/usr/lib(64)?/nagios/cgi(/.*)?  system_u:object_r:httpd_nagios_script_exec_t:s0
/usr/lib(64)?/dirsrv/cgi-bin(/.*)?      system_u:object_r:httpd_dirsrvadmin_script_exec_t:s0
/usr/lib(64)?/cgi-bin/nagios(/.+)?      system_u:object_r:httpd_nagios_script_exec_t:s0
/usr/lib(64)?/nagios/cgi-bin(/.*)?      system_u:object_r:httpd_nagios_script_exec_t:s0
/usr/lib(64)?/cgi-bin/netsaint(/.*)?    system_u:object_r:httpd_nagios_script_exec_t:s0
/usr/lib(64)?/dirsrv/dsgw-cgi-bin(/.*)? system_u:object_r:httpd_dirsrvadmin_script_exec_t:s0
/usr/lib(64)?/cgi-bin/(nph-)?cgiwrap(d)?        --      system_u:object_r:httpd_suexec_exec_t:s0
/var/www/[^/]*/cgi-bin(/.*)?    system_u:object_r:httpd_sys_script_exec_t:s0
/var/www/html/[^/]*/cgi-bin(/.*)?       system_u:object_r:httpd_sys_script_exec_t:s0
/var/cache/cgit(/.*)?   system_u:object_r:httpd_git_rw_content_t:s0
/var/www/dspam/.*\.cgi  --      system_u:object_r:httpd_dspam_script_exec_t:s0
/usr/lib/cgi-bin(/.*)?  system_u:object_r:httpd_sys_script_exec_t:s0
/var/www/cgi-bin(/.*)?  system_u:object_r:httpd_sys_script_exec_t:s0
.....
.....

# テストスクリプトを設置してアクセスすると正常に結果が返る

[root@dlp ~]#
curl http://localhost/cgi-bin/index.cgi

CGI Test Page
  しかし、デフォルトではないディレクトリで CGI を許可する場合、httpd の設定は正しくとも、以下のようにアクセスは拒否されます。これは、cgi-bin ではない場所では CGI の実行を許可するための SELinux コンテキストが正しく割り当てられないためです。
[root ~]#
curl http://localhost/cgi-enabled/index.cgi

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
.....
.....

# 「httpd_sys_content_t」が割り当てられている

[root ~]#
ls -Z /var/www/html/cgi-enabled

-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.cgi
 
以上のような場合、SELinux の設定で CGI を許可しているファイルタイプに変更することで、デフォルトではない場所でも CGI を実行することができます。CGI には [2] で確認した通り「httpd_sys_script_exec_t」を割り当てれば OK です。
[3] タイプを変更する場合は以下のように設定します。
ただし、chcon コマンドでの変更の場合、ファイルシステムに対してリラべリングしたり、restorecon コマンドでデフォルトの SELinux コンテキストにリストアした場合は、ポリシーディレクトリ配下の contexts/files 配下で設定されているデフォルトのタイプに戻ります。
[root@dlp ~]#
chcon -t httpd_sys_script_exec_t /var/www/html/cgi-enabled/index.cgi

[root@dlp ~]#
ls -Z /var/www/html/cgi-enabled

-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 index.cgi

[root@dlp ~]#
curl http://localhost/cgi-enabled/index.cgi

CGI Test Page    
# アクセスできた

[4] デフォルトのタイプを変更して、タイプの変更を永続的にする場合は以下のように設定します。
ポリシーディレクトリ配下の contexts/files 配下で設定されている設定ファイルに書き込まれます。
[root@dlp ~]#
semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled/index.cgi

[root@dlp ~]#
grep "cgi-enabled" /etc/selinux/targeted/contexts/files/file_contexts.local

/var/www/html/cgi-enabled/index.cgi    system_u:object_r:httpd_sys_script_exec_t:s0
# 書き込まれた

[root@dlp ~]#
ls -Z /var/www/html/cgi-enabled

-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.cgi

# restotecon で初期化

[root@dlp ~]#
restorecon /var/www/html/cgi-enabled/index.cgi

[root@dlp ~]#
ls -Z /var/www/html/cgi-enabled

-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 index.cgi
# リストアされた

[root@dlp ~]#
curl http://localhost/cgi-enabled/index.cgi

CGI Test Page    
# アクセスできた

関連コンテンツ