Show Table of Contents
このページには機械翻訳が使用されている場合があります (詳細はこちら)。
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 とグループ 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
といった複数のコマンドがあります。
4.7.1. 一時的な変更: chcon
chcon
コマンドは、ファイルの SELinux コンテキストを変更します。ただし、chcon
コマンドによる変更は、ファイルシステムの再ラベル付けや 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 コンテキスト」を参照してください。 - 以下のコマンドを実行して、タイプを
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 ページを参照してください。
注記
Type Enforcement は、SELinux ターゲットポリシーで使われる主要なパーミッション制御です。ほとんどの場合、SELinux ユーザーとロールは無視することができます。
4.7.2. 永続的な変更: semanage fcontext
semanage fcontext
コマンドは、ファイルの SELinux コンテキスト変更に使用します。新規作成ファイルおよびディレクトリーのコンテキストを表示するには、root で以下のコマンドを実行します。
~]# semanage fcontext -C -l
これらのファイルは、2 つのユーティリティーが読み込みます。ファイルシステムのラベル変更には
setfiles
ユーティリティーを使用し、デフォルトの SELinux コンテキストを復元するには restorecon
ユーティリティーを使用します。つまり、ファイルシステムのラベル変更が行われても、semanage fcontext
による変更は維持されます。SELinux ポリシーは、ユーザーが特定ファイルの SELinux コンテキストを修正できるかどうかを制御します。
クイックリファレンス
ファイルシステムのラベル変更が行われても SELinux コンテキストの変更が維持されるようにするには、以下の手順を実行します。
- 以下のコマンドを実行します。ファイルまたはディレクトリーの完全パスを使用します。
~]#
semanage fcontext -a options file-name|directory-name
restorecon
ユーティリティーを使用してコンテキスト変更を適用します。~]#
restorecon -v file-name|directory-name
手順4.8 ファイルまたはディレクトリーのタイプ変更
以下ではファイルのタイプを変更し、SELinux コンテキストの他の属性はそのままにしておく例を示しています。このセクションの例は、ディレクトリーにも適用できます。例えば、
file1
をディレクトリーに置き換えます。
- root ユーザーとして、
/etc
ディレクトリー内に新規ファイルを作成します。デフォルトでは、/etc
ディレクトリー内の新規作成ファイルにはetc_t
タイプのラベルが付けられます。~]#
touch /etc/file1
~]$
ls -Z /etc/file1
-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1ディレクトリーの情報を確認するには、以下のコマンドを実行します。~]$
ls -dZ directory_name
- root で以下のコマンドを実行し、
file1
のタイプをsamba_share_t
に変更します。-a
オプションは新規レコードを追加し、-t
オプションはタイプ (samba_share_t
) を定義します。このコマンドを実行しても、直ちにタイプが変更されるわけではないことに留意してください。file1
にはetc_t
タイプのラベルが付けられたままです。~]#
semanage fcontext -a -t samba_share_t /etc/file1
~]#
ls -Z /etc/file1
-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1~]$
semanage fcontext -C -l
/etc/file1 unconfined_u:object_r:samba_share_t:s0 - root で
restorecon
ユーティリティーを使用してタイプを変更します。semanage
が/etc/file1
のエントリーをfile_contexts.local
に追加したので、restorecon
によりタイプがsamba_share_t
に変更されます。~]#
restorecon -v /etc/file1
restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0
手順4.9 ディレクトリーおよびコンテンツタイプの変更
以下の例では、新規ディレクトリーの作成と、そのディレクトリーのファイルタイプをそのコンテンツとともに 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
に変更します。-a
オプションは新規レコードを追加し、-t
オプションはタイプ (httpd_sys_content_t
) を定義します。"/web(/.*)?"
の正規表現を使うことで、semanage
が変更をweb/
とその中のファイルに適用します。このコマンドを実行しても、直接にはタイプを変更しないことに留意してください。web/
およびその中のファイルはdefault_t
タイプのラベルが付けられたままです。~]#
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
~]$
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 file3semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
コマンドが以下のエントリーを/etc/selinux/targeted/contexts/files/file_contexts.local
に追加します。/web(/.*)? system_u:object_r:httpd_sys_content_t:s0
- root で
restorecon
ユーティリティーを使用してweb/
とその中のすべてのファイルのタイプを変更します。-R
オプションは再帰的なので、web/
ディレクトリー下のすべてのファイルとディレクトリーがhttpd_sys_content_t
タイプでラベル付けされます。semanage
で/web(/.*)?
のエントリーをfile.contexts.local
に追加したので、restorecon
によりhttpd_sys_content_t
にタイプが変更されます。~]#
restorecon -R -v /web
restorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0デフォルトでは、新規作成のファイルおよびディレクトリーは、親ディレクトリーの SELinux タイプを引き継ぎます。
手順4.10 追加されたコンテキストの削除
以下では、SELinux コンテキストの追加と削除の例を示しています。
/web(/.*)?
のようにコンテキストが正規表現の一部である場合、正規表現の前後に引用符を使います。
~]#
semanage fcontext -d "/web(/.*)?"
- コンテキストを削除するには、root ユーザーで以下のコマンドを実行します。ここでの file-name|directory-name は、
file_contexts.local
の最初の部分です。~]#
semanage fcontext -d file-name|directory-name
以下は、file_contexts.local
内のコンテキスト例です。/test system_u:object_r:httpd_sys_content_t:s0
最初の部分は/test
になっています。restorecon
実行後もしくはファイルシステムのラベル交換後に/test/
ディレクトリーへのhttpd_sys_content_t
のラベル付けを防ぐには、root で以下のコマンドを実行してfile_contexts.local
からコンテキストを削除します。~]#
semanage fcontext -d /test
- root で
restorecon
ユーティリティーを使用してデフォルトの SELinux コンテキストを復元します。
semanage
についての詳細は、semanage(8) の man ページを参照してください。
重要
semanage fcontext -a
で SELinux のコンテキストを変更する場合、ファイルシステムの再ラベル付け後もしくは restorecon
コマンド実行後におけるファイルの誤ったラベル付けを避けるために、ファイルもしくはディレクトリーへの完全パスを使用してください。
このページには機械翻訳が使用されている場合があります (詳細はこちら)。