4.9. SELinux ラベルの維持
4.9.1. ファイルおよびディレクトリーのコピー
user_home_t タイプのラベルが付けられます。
~]$touch file1
~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
/etc という別のディレクトリーにコピーされたとすると、この新しいファイルは /etc のデフォルトのラベル付けルールにしたがって作成されます。追加オプションなしでファイルをコピーすると、オリジナルのコンテキストは保持されない可能性があります。
~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
~]#cp file1 /etc/
~]$ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1
/etc/file1 が存在しない状況で、file1 が /etc/ にコピーされると、/etc/file1 は新規ファイルとして作成されます。上の例にあるように、/etc/file1 はデフォルトのラベル付けルールにしたがって、etc_t タイプでラベル付けされます。
--preserve=context などの cp オプションを指定してオリジナルファイルのコンテキストを維持しない限り、既存ファイルのコンテキストが維持されます。SELinux ポリシーは、コピー時にコンテキストの維持を妨げる場合があります。
手順4.11 SELinux コンテキストを維持せずにコピーする
cp コマンドでオプションなしでファイルをコピーすると、ターゲットの親ディレクトリーからタイプを継承することを示しています。
- ユーザーのホームディレクトリーでファイルを作成します。ファイルは
user_home_tタイプでラベル付けされます。~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 - 以下のコマンドで示すように、
/var/www/html/ディレクトリーはhttpd_sys_content_tタイプでラベル付けされています。~]$ls -dZ /var/www/html/drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ file1が/var/www/html/にコピーされると、httpd_sys_content_tタイプを継承します。~]#cp file1 /var/www/html/~]$ls -Z /var/www/html/file1-rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1
手順4.12 SELinux コンテキストを維持してコピーする
--preserve=context オプションを使用してコピー時にコンテキストを維持する方法を示しています。
- ユーザーのホームディレクトリーでファイルを作成します。ファイルは
user_home_tタイプでラベル付けされます。~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 - 以下のコマンドで示すように、
/var/www/html/ディレクトリーはhttpd_sys_content_tタイプでラベル付けされています。~]$ls -dZ /var/www/html/drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ --preserve=contextオプションを使うと、コピー時に SELinux コンテキストが維持されます。以下で示すように、file1のuser_home_tタイプは、このファイルを/var/www/html/にコピーしても維持されます。~]#cp --preserve=context file1 /var/www/html/~]$ls -Z /var/www/html/file1-rw-r--r-- root root unconfined_u:object_r:user_home_t:s0 /var/www/html/file1
手順4.13 コンテキストのコピーおよび変更
--context オプションを使ってコピー先のコンテキストを変更する方法を示しています。以下の例は、ユーザーのホームディレクトリーで行われています。
- ユーザーのホームディレクトリーでファイルを作成します。ファイルは
user_home_tタイプでラベル付けされます。~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 --contextオプションを使って SELinux コンテキストを定義します。~]$cp --context=system_u:object_r:samba_share_t:s0 file1 file2--contextを使用しないと、file2はunconfined_u:object_r:user_home_tコンテキストでラベル付けされます。~]$ls -Z file1 file2-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 -rw-rw-r-- user1 group1 system_u:object_r:samba_share_t:s0 file2
手順4.14 既存ファイル上へのファイルのコピー
- root で新規ファイル
file1を/etcディレクトリーに作成します。以下のように、このファイルはetc_tタイプでラベル付けされます。~]#touch /etc/file1~]$ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1 - 別のファイル
file2を/tmpディレクトリーに作成します。以下のように、このファイルはuser_tmp_tタイプでラベル付けされます。~]$touch /tmp/file2~$ls -Z /tmp/file2-rw-r--r-- root root unconfined_u:object_r:user_tmp_t:s0 /tmp/file2 file1をfile2で上書きします。~]#cp /tmp/file2 /etc/file1- コピー後に以下のコマンドを実行すると、
file1はetc_tタイプでラベル付けされており、/etc/file1を上書きした/tmp/file2のuser_tmp_tタイプではないことが分かります。~]$ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1
重要
4.9.2. ファイルおよびディレクトリーの移動
/var/www/html/ ディレクトリーに移動します。ファイルは移動されたため、正しい SELinux コンテキストを継承しません。
手順4.15 ファイルおよびディレクトリーの移動
- ユーザーのホームディレクトリーに移動して、ファイルを作成します。ファイルは
user_home_tタイプでラベル付けされます。~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 - 以下のコマンドを実行して、
/var/www/html/ディレクトリーの SELinux コンテキストを表示します。~]$ls -dZ /var/www/html/drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/デフォルトでは、/var/www/html/にはhttpd_sys_content_tタイプがラベル付けされています。/var/www/html/下で作成されたファイルおよびディレクトリーはこのタイプを継承するため、このタイプでラベル付けされます。 - root で
file1を/var/www/html/に移動します。このファイルは移動したので、現行のuser_home_tタイプを維持します。~]#mv file1 /var/www/html/~]#ls -Z /var/www/html/file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 /var/www/html/file1
user_home_t タイプでラベル付けされたファイルを読み取れません。Web ページを構成するすべてのファイルが user_home_t タイプ、もしくは Apache HTTP Server が読み取り不可能な別のタイプでラベル付けされている場合、それらに Mozilla Firefox のような Web ブラウザーを使用してアクセスしようとすると、パーミッションは拒否されます。
重要
mv コマンドで移動すると、誤った SELinux コンテキストとなり、Apache HTTP Server や Samba などのプロセスがそれらのファイルやディレクトリーにアクセスできなくなる可能性があります。
4.9.3. デフォルト SELinux コンテキストのチェック
matchpathcon ユーティリティーを使ってチェックします。このユーティリティーは、システムポリシーにクエリを行い、ファイルパスに関連するデフォルトのセキュリティーコンテキストを提供します[8]。以下の例では、matchpathcon を使って /var/www/html/ ディレクトリーのファイルが正しくラベル付けされているかを検証しています。
手順4.16 matchpathcon を使ってデフォルトの SELinux コンテキストをチェックする
- root ユーザーとして
/var/www/html/ディレクトリーに 3 つのファイルを作成します (file1、file2、file3)。これらのファイルは/var/www/html/からhttpd_sys_content_tタイプを継承します。~]#touch /var/www/html/file{1,2,3}~]#ls -Z /var/www/html/-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 - root で
file1のタイプをsamba_share_tに変更します。Apache HTTP Server は、samba_share_tタイプでラベル付けされたファイルやディレクトリーを読み取れないことに注意してください。~]#chcon -t samba_share_t /var/www/html/file1 matchpathcon-Vオプションは、現行の SELinux コンテキストを SELinux ポリシーの正しいデフォルトのコンテキストと比較します。以下のコマンドを実行すると、/var/www/html/ディレクトリー内の全ファイルをチェックします。~]$matchpathcon -V /var/www/html/*/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0 /var/www/html/file2 verified. /var/www/html/file3 verified.
matchpathcon コマンドの出力は、file1 は samba_share_t タイプでラベル付けされていますが、httpd_sys_content_t タイプでラベル付けされるべきであることを示しています。
/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0
file1 にアクセスできるようにするには、root で restorecon ユーティリティーを使用します。
~]#restorecon -v /var/www/html/file1restorecon reset /var/www/html/file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:httpd_sys_content_t:s0
4.9.4. tar を使ったファイルのアーカイブ作成
tar ユーティリティーはデフォルトでは拡張属性を維持しません。SELinux コンテキストは拡張属性に保存されるので、ファイルをアーカイブするとコンテキストは失われます。コンテキストを維持するアーカイブを作成し、アーカイブからファイルを復元するには、tar --selinux を使います。tar アーカイブに拡張属性のないファイルが含まれる、もしくはシステムデフォルトに拡張属性を適合させたい場合は、restorecon ユーティリティーを使用します。
~]$tar -xvf archive.tar | restorecon -f -
restorecon を実行する必要があることもあります。
tar アーカイブの作成方法を説明します。
手順4.17 tar アーカイブを作成する
/var/www/html/ディレクトリーに移動し、その SELinux コンテキストを確認します。~]$cd /var/www/html/html]$ls -dZ /var/www/html/drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .- root ユーザーとして
/var/www/html/ディレクトリーに 3 つのファイルを作成します (file1、file2、file3)。これらのファイルは/var/www/html/からhttpd_sys_content_tタイプを継承します。html]#touch file{1,2,3}html]$ls -Z /var/www/html/-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 - root で以下のコマンドを実行し、
test.tarという名前のtarアーカイブを作成します。SELinux コンテキストを保持するには、--selinuxを使用します。html]#tar --selinux -cf test.tar file{1,2,3} - root で
test/という名前の新規ディレクトリーを作成し、全ユーザーに完全アクセスを許可します。~]#mkdir /test~]#chmod 777 /test/ test.tarファイルをtest/にコピーします。~]$cp /var/www/html/test.tar /test/test/ディレクトリーに移動し、以下のコマンドを実行してtarアーカイブを抽出します。--selinuxオプションを指定してください。これを行わないと、SELinux コンテキストがdefault_tに変更されます。~]$cd /test/test]$tar --selinux -xvf test.tar- SELinux コンテキストを確認します。
httpd_sys_content_tタイプが維持されたことが分かります。--selinuxを使用していなければ、default_tに変更されていました。test]$ls -lZ /test/-rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file3 -rw-r--r-- user1 group1 unconfined_u:object_r:default_t:s0 test.tar test/ディレクトリーが不要になったら、root で以下のコマンドを実行し、ディレクトリーとその中の全ファイルを削除します。~]#rm -ri /test/
--xattrs オプションなどの tar に関する詳細情報は、tar(1) man ページを参照してください。
4.9.5. star を使ったファイルのアーカイブ作成
star ユーティリティーは、デフォルトでは拡張属性を維持しません。SELinux コンテキストは拡張属性に保存されるので、ファイルをアーカイビングするとコンテキストは失われます。コンテキストを維持するアーカイブを作成するには、star -xattr -H=exustar コマンドを使用します。star パッケージはデフォルトではインストールされません。star をインストールするには、yum install star コマンドを root ユーザーで実行します。
star アーカイブの作成方法を説明します。
手順4.18 star アーカイブを作成する
- root で
/var/www/html/ディレクトリーに 3 つのファイルを作成します (file1、file2、file3)。これらのファイルは/var/www/html/からhttpd_sys_content_tタイプを継承します。~]#touch /var/www/html/file{1,2,3}~]#ls -Z /var/www/html/-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 /var/www/html/ディレクトリーに移動し、root で以下のコマンドを実行してtest.starという名前のstarアーカイブを作成します。~]$cd /var/www/htmlhtml]#star -xattr -H=exustar -c -f=test.star file{1,2,3}star: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).- root で
test/という名前の新規ディレクトリーを作成し、全ユーザーに完全アクセスを許可します。~]#mkdir /test~]#chmod 777 /test/ - 以下のコマンドを実行して、
test.starファイルをtest/にコピーします。~]$cp /var/www/html/test.star /test/ test/ディレクトリーに移動し、以下のコマンドを実行してstarアーカイブを抽出します。~]$cd /test/test]$star -x -f=test.starstar: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).- SELinux コンテキストを確認します。
httpd_sys_content_tタイプが維持されたことが分かります。-xattr -H=exustarオプションを使用していなければ、default_tに変更されていました。~]$ls -lZ /test/-rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file3 -rw-r--r-- user1 group1 unconfined_u:object_r:default_t:s0 test.star test/ディレクトリーが不要になったら、root で以下のコマンドを実行し、ディレクトリーとその中の全ファイルを削除します。~]#rm -ri /test/starが不要になったら、root でパッケージを削除します。~]#yum remove star
star についての詳細は、star(1) の man ページを参照してください。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.