Menu Close
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、
match
path con などのファイルシステムのラベリングを管理するための複数のコマンドを提供します。
4.7.1. 一時的な変更: chcon
chcon コマンドは、ファイルの SELinux コンテキストを変更します。ただし、ch con コマンドで行った変更は、ファイルシステムの再ラベルや restorecon コマンドの実行には永続性がありません。SELinux ポリシーは、ユーザーが指定のファイルの SELinux コンテキストを変更できるかどうかを制御します。chcon を使用する場合、ユーザーは SELinux コンテキストすべてまたは一部を提供します。誤ったファイルタイプは、SELinux がアクセスを拒否する一般的な原因です。
クイックリファレンス
- chcon -t type file-name コマンドを実行してファイルの種類を変更します。type は、
httpd_sys_content_t
などの SELinux タイプで、file-name はファイルまたはディレクトリー名です。~]$
chcon -t httpd_sys_content_t file-name - chcon -R -t type directory-name コマンドを実行して、ディレクトリーのタイプとそのコンテンツを変更します。type は、
httpd_sys_content_t
などの SELinux タイプで、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 コンテキストには、SELinuxunconfined_u
ユーザー、object_r
ロール、user_home_t
タイプ、s0
レベルが含まれます。SELinux コンテキストの各部分の説明は、2章SELinux Contexts を参照してください。 - 以下のコマンドを入力して、タイプを
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
タイプに復元されます。ターゲットポリシー(Red Hat Enterprise Linux のデフォルトの SELinux ポリシー)を使用する場合、restorecon コマンドは/etc/selinux/targeted/contexts/files/
ディレクトリーのファイルを読み取り、どの SELinux コンテキストファイルが存在するかを確認します。
手順4.7 ディレクトリーとそのコンテンツタイプの変更
以下の例は、新しいディレクトリーを作成し、そのディレクトリーのファイルタイプを Apache HTTP Server が使用するタイプに変更する方法を示しています。この例の設定は、Apache HTTP Server が異なるドキュメントルート(
/var/www/html/
ではなく)を使用する場合に使用します。
- 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 ページを参照してください。
注記
Enforcement タイプ は、SELinux ターゲットポリシーで使用される主要なパーミッション制御です。ほとんどの部分では、SELinux ユーザーおよびロールを無視できます。