SELinux : ファイルのタイプを変更する2025/10/22 |
|
ファイルのタイプを変更することにより、個々のファイルに対する SELinux によるアクセス制御設定を変更することができます。 以下では [targeted] ポリシー適用下の環境で例示しています。 |
|
| [1] | ファイルへのラベリングのデフォルトとなる設定はポリシーディレクトリ配下の [contexts/files] 配下にあります。 [***.bin] はバイナリファイルとなっており、[.bin] ではないファイルを開くと、ファイルタイプも含めてどのような設定になっているか参照できます。 |
|
dlp:~ # ll /etc/selinux/targeted/contexts/files total 6036 -rw-r--r--. 1 root root 441732 Oct 22 09:34 file_contexts -rw-r--r--. 1 root root 5407101 Oct 22 09:34 file_contexts.bin -rw-r--r--. 1 root root 16080 Oct 22 09:34 file_contexts.homedirs -rw-r--r--. 1 root root 302194 Oct 22 09:34 file_contexts.homedirs.bin -rw-r--r--. 1 root root 0 Jul 31 23:15 file_contexts.local -rw-r--r--. 1 root root 0 Jul 31 23:15 file_contexts.subs -rw-r--r--. 1 root root 1403 Jul 31 23:15 file_contexts.subs_dist -rw-r--r--. 1 root root 139 Jul 31 23:15 mediadlp:~ # 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 /efi(/.*)? system_u:object_r:boot_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(/[^/]*)? -d system_u:object_r:mnt_t:s0 /mnt(/[^/]*)? -l system_u:object_r:mnt_t:s0 /dev/.* system_u:object_r:device_t:s0 |
| [2] |
例として httpd で CGI を利用する際のファイルタイプについて設定します。
httpd での CGI 実行許可のブール値はデフォルトはオンになっています。 |
|
dlp:~ # semanage boolean -l | grep httpd_enable_cgi httpd_enable_cgi (on , on) Allow httpd to enable cgidlp:~ # grep "cgi" /etc/selinux/targeted/contexts/files/file_contexts | grep "httpd_sys_script_exec_t" /opt/.*\.cgi -- system_u:object_r:httpd_sys_script_exec_t:s0 /usr/.*\.cgi -- system_u:object_r:httpd_sys_script_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 /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:s0dlp:~ # grep -n "^ *ScriptAlias" /etc/apache2/default-server.conf 72:ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/" dlp:~ # semanage fcontext -l | grep /srv/www /srv/www = /var/www
dlp:~ # cat > /srv/www/cgi-bin/index.cgi <<'EOF'
#!/usr/bin/python3
print("Content-type: text/html\n")
print("CGI Script Test Page")
EOF
dlp:~ # chmod 755 /srv/www/cgi-bin/index.cgi dlp:~ # restorecon /srv/www/cgi-bin/index.cgi dlp:~ # curl localhost/cgi-bin/index.cgi CGI Script Test Page |
| しかし、デフォルトの [/srv/www/cgi-bin/] ではないディレクトリで CGI を許可する場合、httpd の設定は正しくとも、以下のようにアクセスは拒否されます。これは、[cgi-bin] ではない場所では CGI の実行を許可するための SELinux コンテキストが正しく割り当てられないためです。 |
|
dlp:~ # curl localhost/cgi-enabled/index.cgi <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Server error!</title> ..... ..... # [httpd_sys_content_t] が割り当てられている dlp:~ # ls -lZ /srv/www/htdocs/cgi-enabled total 4 -rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 84 Oct 22 10:41 index.cgi |
|
以上のような場合、SELinux の設定で CGI を許可しているファイルタイプに変更することで、デフォルトではない場所でも CGI を実行することができます。CGI には [2] で確認した通り [httpd_sys_script_exec_t] を割り当てれば OK です。 |
|
| [3] | タイプを変更する場合は以下のように設定します。 ただし、[chcon] コマンドでの変更の場合、ファイルシステムに対してリラべリングしたり、[restorecon] コマンドでデフォルトの SELinux コンテキストにリストアした場合は、ポリシーディレクトリ配下の [contexts/files] 配下で設定されているデフォルトのタイプに戻ります。 |
|
dlp:~ # chcon -t httpd_sys_script_exec_t /srv/www/htdocs/cgi-enabled/index.cgi dlp:~ # ls -lZ /srv/www/htdocs/cgi-enabled total 4 -rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 84 Oct 22 10:41 index.cgidlp:~ # curl localhost/cgi-enabled/index.cgi CGI Script Test Page # アクセスできた |
| [4] | デフォルトのタイプを変更して、タイプの変更を永続的にする場合は以下のように設定します。 ポリシーディレクトリ配下の [contexts/files] 配下で設定されている設定ファイルに書き込まれます。 |
|
dlp:~ # semanage fcontext -a -t httpd_sys_script_exec_t /var/www/htdocs/cgi-enabled/.*\.cgi dlp:~ # grep "cgi-enabled" /etc/selinux/targeted/contexts/files/file_contexts.local
/var/www/htdocs/cgi-enabled/.*.cgi system_u:object_r:httpd_sys_script_exec_t:s0
# 書き込まれた
dlp:~ # cat > /srv/www/htdocs/cgi-enabled/index2.cgi <<'EOF'
#!/usr/bin/python3
print("Content-type: text/html\n")
print("CGI Script Test Page")
EOF
dlp:~ # chmod 755 /srv/www/htdocs/cgi-enabled/index2.cgi dlp:~ # ls -lZ /srv/www/htdocs/cgi-enabled total 8 -rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 84 Oct 22 10:41 index.cgi -rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 84 Oct 22 10:52 index2.cgi # restotecon で初期化 dlp:~ # restorecon /srv/www/htdocs/cgi-enabled/index2.cgi dlp:~ # ls -lZ /srv/www/htdocs/cgi-enabled
total 8
-rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 84 Oct 22 10:41 index.cgi
-rwxr-xr-x. 1 root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 84 Oct 22 10:52 index2.cgi
# リストアされた
dlp:~ # curl localhost/cgi-enabled/index2.cgi CGI Test Page |
| Sponsored Link |
|
|