Red Hat Training
A Red Hat training course is available for Red Hat Enterprise Linux
4.7. SELinux コンテキスト - ファイルのラベル付け
SELinux を実行しているシステムでは、セキュリティー関連の情報を表す方法で、すべてのプロセスとファイルにラベルが付けられます。この情報は SELinux コンテキストと呼ばれます。ファイルの場合は、ls -Z コマンドを使用して表示されます。
~]$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
この例では、SELinux はユーザー (
unconfined_u)、ロール (object_r)、タイプ (user_home_t)、およびレベル (s0) を提供します。この情報は、アクセス制御の決定を行うために使用されます。DAC システムでは、アクセスは Linux ユーザーおよびグループ ID に基づいて制御されます。SELinux ポリシールールは、DAC ルールの後にチェックされます。DAC ルールがアクセスを拒否する場合は、SELinux ポリシールールは使用されません。
注記
デフォルトでは、新規作成されたファイルおよびディレクトリーは親ディレクトリーの SELinux タイプを継承します。たとえば、
etc_t タイプのラベルが付けられた /etc ディレクトリーに新しいファイルを作成する場合、新しいファイルは同じタイプを継承します。
~]$ ls -dZ - /etc drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc
~]# touch /etc/file1~]# ls -lZ /etc/file1 -rw-r--r--. root root unconfined_u:object_r:etc_t:s0 /etc/file1
SELinux は、chcon、semanage fcontext、restorecon、および
matchpathcon など、ファイルシステムのラベリングを管理するための複数のコマンドを提供します。
4.7.1. 一時的な変更: chcon
chcon コマンドは、ファイルの SELinux コンテキストを変更します。ただし、chcon コマンドを使用して行った変更は、ファイルシステムのラベル変更や restorecon コマンドの実行後も維持されません。SELinux ポリシーは、ユーザーが任意のファイルの SELinux コンテキストを変更できるかどうかを制御します。chcon を使用すると、ユーザーは変更する SELinux コンテキストのすべてまたは一部を提供します。ファイルタイプは、SELinux がアクセスを拒否する一般的な原因です。
クイックリファレンス
- chcon -t type file-name コマンドを実行して、ファイルタイプを変更します。type は SELinux タイプです。たとえば、
httpd_sys_content_tで、file-name はファイルまたはディレクトリー名になります。~]$chcon -t httpd_sys_content_t file-name - chcon -R -t type directory-name コマンドを実行して、ディレクトリーのタイプとそのコンテンツを変更します。type は SELinux タイプです。たとえば、
httpd_sys_content_tで、directory-name はディレクトリー名になります。~]$chcon -R -t httpd_sys_content_t directory-name
手順4.6 ファイルまたはディレクトリーのタイプの変更
次の手順は、タイプの変更を示しており、SELinux コンテキストの他の属性は示していません。このセクションの例では、
file1 がディレクトリーである場合など、ディレクトリーに対して同じように動作します。
- ホームディレクトリーに移動します。
- 新しいファイルを作成して、その SELinux コンテキストを表示します。
~]$touch file1~]$ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1この例では、file1の SELinux コンテキストには、SELinux のunconfined_uユーザー、object_rロール、user_home_tタイプ、およびs0レベルが含まれます。SELinux コンテキストの各部分の説明は、2章SELinux コンテキスト を参照してください。 - 次のコマンドを実行して、タイプを
samba_share_tに変更します。-tオプションはタイプを変更します。次に、変更を表示します。~]$chcon -t samba_share_t file1~]$ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1 - 以下のコマンドを使用して、
file1ファイルの SELinux コンテキストを復元します。変更を確認するには、-vオプションを使用します。~]$restorecon -v file1 restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0この例では、前のタイプsamba_share_tは正しいタイプuser_home_tタイプに復元されます。Targeted ポリシー (Red Hat Enterprise Linux のデフォルトの SELinux ポリシー) を使用する場合、restorecon コマンドは/etc/selinux/targeted/contexts/files/ディレクトリーのファイルを読み取り、どの SELinux コンテキストファイルが存在するかを確認します。
手順4.7 ディレクトリーとそのコンテンツタイプの変更
以下の例は、新しいディレクトリーを作成し、そのディレクトリーのファイルタイプを Apache HTTP Server が使用するタイプに変更します。この例での設定は、Apache HTTP サーバーが、(
/var/www/html/ の代わりに) 別のドキュメント root を使用する場合に使用します。
- root ユーザーとして、このディレクトリー内に新しい
web/ディレクトリーを作成し、3 つの空のファイル (file1、file2、およびfile3) を作成します。web/ディレクトリーおよびそのファイルには、default_tタイプのラベルが付けられます。~]#mkdir /web~]#touch /web/file{1,2,3}~]#ls -dZ /web drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web~]#ls -lZ /web -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 - root で次のコマンドを実行して、
web/ディレクトリー (とそのコンテンツ) のタイプをhttpd_sys_content_tに変更します。~]#chcon -R -t httpd_sys_content_t /web/~]#ls -dZ /web/ drwxr-xr-x root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/~]#ls -lZ /web/ -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file3 - デフォルトの SELinux コンテキストを復元するには、root で
restoreconユーティリティーを使用します。~]#restorecon -R -v /web/ restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
chcon の詳細は、chcon(1) man ページを参照してください。
注記
Type Enforcement は、SELinux の Targeted ポリシーで使用される主要なパーミッション制御です。ほとんどの場合、SELinux のユーザーとロールは無視できます。