5.10. SELinux ラベルの維持
5.10.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 ポリシーは、コピー中にコンテキストの保持を妨げる可能性があります。
cp コマンドでオプションなしでファイルをコピーすると、ターゲットの親ディレクトリーからタイプを継承します。
~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 ~]$ls -dZ /var/www/html/drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ ~]#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
file1 がユーザーのホームディレクトリーに作成され、user_home_t タイプのラベル付けがされています。/var/www/html/ ディレクトリーは ls -dZ /var/www/html/ コマンドで表示されているように、httpd_sys_content_t タイプでラベル付けされています。file1 を /var/www/html/ にコピーすると、ls -Z /var/www/html/file1 コマンドにあるように httpd_sys_content_t タイプを継承します。
コピー時に cp --preserve=context コマンドを使うと、コンテキストを保持します。
~]$touch file1~]$ls -Z file1-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 ~]$ls -dZ /var/www/html/drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /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
file1 がユーザーのホームディレクトリーに作成され、user_home_t タイプのラベル付けがされています。/var/www/html/ ディレクトリーは ls -dZ /var/www/html/ コマンドで表示されているように、httpd_sys_content_t タイプでラベル付けされています。--preserve=context オプションを使うと、コピー作業中に SELinux コンテキストを保持します。ls -Z /var/www/html/file1 コマンドにあるように、ファイルが /var/www/html/ にコピーされた際に、file1 user_home_t タイプは保持されています。
cp -Z コマンドを使ってコピー先のコンテキストを変更します。以下の例は、ユーザーのホームディレクトリーで行われています。
~]$touch file1~]$cp -Z system_u:object_r:samba_share_t:s0 file1 file2~]$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 ~]$rm file1 file2
-Z オプションで定義されています。-Z オプションなしだと、file2 は unconfined_u:object_r:user_home_t コンテキストでラベル付けされます。
既存ファイル上にファイルをコピーすると、(オプションを使ってコンテキストを維持しない限り) 既存ファイルのコンテキストが保持されます。例えば、
~]#touch /etc/file1~]#ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1 ~]#touch /tmp/file2~]#ls -Z /tmp/file2-rw-r--r-- root root unconfined_u:object_r:user_tmp_t:s0 /tmp/file2 ~]#cp /tmp/file2 /etc/file1~]#ls -Z /etc/file1-rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1
etc_t タイプのラベル付けがされた /etc/file1 と、user_tmp_t タイプのラベル付けがされた /tmp/file2 の 2 つのファイルが作成されます。cp /tmp/file2 /etc/file1 コマンドは、file1 を file2 で上書きします。コピー後に ls -Z /etc/file1 コマンドは、file1 が /etc/file1 を置き換えた /tmp/file2 からの user_tmp_t タイプではなく、etc_t タイプでラベル付けされていることを示しています。
重要

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.