第50章 SELinux の使用
50.1. SELinux のエンドユーザーコントロール リンクのコピーリンクがクリップボードにコピーされました!
unconfined_t のドメインと、ターゲットデーモン 以外 の残りのシステムで実行しているためです。
avc: denied メッセージを生成しない可能性があります。
50.1.1. ファイルの移動とコピー リンクのコピーリンクがクリップボードにコピーされました!
ファイルのコピー:cp の SELinux オプション
特に指定しない限り、cp は、作成プロセスのドメインとターゲットディレクトリーのタイプに基づいて新しいファイルを作成するデフォルトの動作に従います。ラベルを設定する特定のルールがない限り、ファイルはターゲットディレクトリーからタイプを継承します。
-Z user:role:type オプションを使用して、新規ファイルに必要なラベルを指定します。
-p (または --preserve=mode,ownership,timestamps)オプションは指定された属性を保持し、可能であれば link などの追加属性を保持します。
touch bar foo
ls -Z bar foo
-rw-rw-r-- auser auser user_u:object_r:user_home_t bar
-rw-rw-r-- auser auser user_u:object_r:user_home_t foo
/tmp に適用される特定のルールがないため、新しいファイルには親ディレクトリーのタイプが設定されます。
cp bar /tmp
ls -Z /tmp/bar
-rw-rw-r-- auser auser user_u:object_r:tmp_t /tmp/bar
tmp_t タイプは、一時ファイルのデフォルトタイプです。
-Z オプションを使用して、新規ファイルのラベルを指定します。
cp -Z user_u:object_r:user_home_t foo /tmp
ls -Z /tmp/foo
-rw-rw-r-- auser auser user_u:object_r:user_home_t /tmp/foo
ファイルの移動:mv の SELinux オプション
mv のあるファイルを移動すると、ファイルに関連付けられた元のタイプが保持されます。問題が発生する可能性があるため、このコマンドを使用する場合は注意が必要です。たとえば、タイプ user_home_t を持つファイルを ~/public_html に移動すると、httpd デーモンは再ラベル付けするまでこれらのファイルを提供できなくなります。ファイルのラベリングの詳細は、「ファイルまたはディレクトリーの再ラベル」 を参照してください。
| コマンド | 動作 |
|---|---|
| mv | ファイルは、元のラベルを保持します。これにより、問題、混乱、またはマイナーなセキュリティーが発生する可能性があります。たとえば、sbin_t ドメインで実行している tmpwatch プログラムは、ファイルのタイプが原因で、/tmp ディレクトリー内の経過時間ファイルを削除できない可能性があります。 |
| cp | 作成プロセスのドメイン(cp)とターゲットディレクトリーのタイプに基づくデフォルトの動作を使用して、ファイルのコピーを作成します。 |
| cp -p | ファイルのコピーを作成し、可能な場合は指定された属性とセキュリティーコンテキストを保持します。デフォルトの属性は mode、ownership、および timestamps です。その他の属性は links および all です。 |
| cp -Z <user:role:type> | 指定されたラベルで ファイルのコピーを作成します。-Z オプションは、--context と同義語です。 |
50.1.2. プロセス、ユーザー、またはファイルオブジェクトのセキュリティーコンテキストの確認 リンクのコピーリンクがクリップボードにコピーされました!
プロセス ID の確認
Red Hat Enterprise Linux では、-Z オプションは --context と同等で、ps コマンド、id コマンド、ls コマンド、および cp コマンドで使用できます。SELinux に関する cp コマンドの動作は、表50.1「mv コマンドおよび cp コマンドの動作」 で説明されています。
unconfined_t ドメインで実行されており、いくつかの例外があります。
[user@localhost ~]$ ps auxZ
LABEL USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
system_u:system_r:init_t root 1 0.0 0.1 2032 620 ? Ss 15:09 0:00 init [5]
system_u:system_r:kernel_t root 2 0.0 0.0 0 0 ? S 15:09 0:00 [migration/0]
system_u:system_r:kernel_t root 3 0.0 0.0 0 0 ? SN 15:09 0:00 [ksoftirqd/0]
user_u:system_r:unconfined_t user 3122 0.0 0.6 6908 3232 ? S 16:47 0:01 /usr/libexec/gconfd-2 5
user_u:system_r:unconfined_t user 3125 0.0 0.1 2540 588 ? S 16:47 0:00 /usr/bin/gnome-keyring-daemon
user_u:system_r:unconfined_t user 3127 0.0 1.4 33612 6988 ? Sl 16:47 0:00 /usr/libexec/gnome-settings-daemon
user_u:system_r:unconfined_t user 3144 0.1 1.4 16528 7360 ? Ss 16:47 0:01 metacity --sm-client-id=default1
user_u:system_r:unconfined_t user 3148 0.2 2.9 79544 14808 ? Ss 16:47 0:03 gnome-panel --sm-client-id default2
ユーザー ID の確認
id コマンドで -Z オプションを使用して、ユーザーのセキュリティーコンテキストを判断できます。このコマンドでは、-Z と他のオプションを組み合わせることはできません。
[root@localhost ~]# id -Z
user_u:system_r:unconfined_t
-Z オプションを使用して、別のユーザーのセキュリティーコンテキストを検査できないことに注意してください。つまり、現在ログインしているユーザーのセキュリティーコンテキストのみを表示できます。
[user@localhost ~]$ id
uid=501(user) gid=501(user) groups=501(user) context=user_u:system_r:unconfined_t
[user@localhost ~]$ id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[user@localhost ~]$ id -Z root
id: cannot display context when selinux not enabled or when displaying the id
of a different user
ファイル ID の確認
ls コマンドに -Z オプションを使用して、一般的な長い形式情報をグループ化できます。モード、ユーザー、グループ、セキュリティーコンテキスト、およびファイル名の情報を表示できます。
cd /etc
ls -Z h* -d
drwxr-xr-x root root system_u:object_r:etc_t hal
-rw-r--r-- root root system_u:object_r:etc_t host.conf
-rw-r--r-- root root user_u:object_r:etc_t hosts
-rw-r--r-- root root system_u:object_r:etc_t hosts.allow
-rw-r--r-- root root system_u:object_r:etc_t hosts.canna
-rw-r--r-- root root system_u:object_r:etc_t hosts.deny
drwxr-xr-x root root system_u:object_r:hotplug_etc_t hotplug
drwxr-xr-x root root system_u:object_r:etc_t hotplug.d
drwxr-xr-x root root system_u:object_r:httpd_sys_content_t htdig
drwxr-xr-x root root system_u:object_r:httpd_config_t httpd
50.1.3. ファイルまたはディレクトリーの再ラベル リンクのコピーリンクがクリップボードにコピーされました!
~/public_html ディレクトリーなどのターゲットデーモンに関連する特別なディレクトリーに移動またはコピーする場合、または /home 以外のディレクトリーで動作するスクリプトを作成する場合は、ファイルの再ラベル付けが必要になる場合があります。
- ファイルの型を意図的に変更
- ポリシーに応じたデフォルトの状態へのファイルの復元
/usr/sbin/mysqld に誤ったセキュリティーラベルがあり、restorecon などの再ラベル付け操作を使用してこれに対応する場合は、再ラベル付け操作の後に mysqld を再起動する必要があります。実行可能ファイルを正しいタイプ(mysqld_exec_t)に設定すると、起動時に適切なドメインに移行できるようになります。
/home で作成されたファイルシステムオブジェクトに定義されたデフォルトタイプのラベルが付けられています。
cd ~
ls -Zd public_html/
drwxrwxr-x auser auser user_u:object_r:user_home_t public_html/
ls -Z web_files/
-rw-rw-r-- auser auser user_u:object_r:user_home_t 1.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 2.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 3.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 4.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 5.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t index.html
public_html ディレクトリーに移動すると、元のタイプを保持します。
mv web_files/* public_html/
ls -Z public_html/
-rw-rw-r-- auser auser user_u:object_r:user_home_t 1.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 2.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 3.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 4.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 5.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t index.html
httpd が読み取り権限を持つタイプが必要です。Apache HTTP Server は UserDir に対して設定され、ブール値 httpd_enable_homedirs が有効になります。
chcon -R -t httpd_user_content_t public_html/
ls -Z public_html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t 1.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t 2.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t 3.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t 4.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t 5.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t index.html
ls -Z public_html/ -d
drwxrwxr-x auser auser user_u:object_r:httpd_user_content_t public_html/
ls -Z /tmp/
-rw-rw-r-- auser auser user_u:object_r:tmp_t /tmp/file1
-rw-rw-r-- auser auser user_u:object_r:tmp_t /tmp/file2
-rw-rw-r-- auser auser user_u:object_r:tmp_t /tmp/file3
mv /tmp/{1,2,3} archives/
mv public_html/* archives/
ls -Z archives/
-rw-rw-r-- auser auser user_u:object_r:tmp_t file1
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t file1.html
-rw-rw-r-- auser auser user_u:object_r:tmp_t file2
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t file2.html
-rw-rw-r-- auser auser user_u:object_r:tmp_t file3
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t file3.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t file4.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t file5.html
-rw-rw-r-- auser auser user_u:object_r:httpd_user_content_t index.html
archives/ ディレクトリーには、ユーザーのホームディレクトリーにデフォルトのタイプが作成されているため、すでにデフォルトのタイプがあります。
ls -Zd archives/
drwxrwxr-x auser auser user_u:object_r:user_home_t archives/
/sbin/restorecon -R archives/
ls -Z archives/
-rw-rw-r-- auser auser system_u:object_r:user_home_t file1
-rw-rw-r-- auser auser system_u:object_r:user_home_t file1.html
-rw-rw-r-- auser auser system_u:object_r:user_home_t file2
-rw-rw-r-- auser auser system_u:object_r:user_home_t file2.html
-rw-rw-r-- auser auser system_u:object_r:user_home_t file3
-rw-rw-r-- auser auser system_u:object_r:user_home_t file3.html
-rw-rw-r-- auser auser system_u:object_r:user_home_t file4.html
-rw-rw-r-- auser auser system_u:object_r:user_home_t file5.html
-rw-rw-r-- auser auser system_u:object_r:user_home_t index.html
50.1.4. セキュリティーコンテキストを保持するアーカイブの作成 リンクのコピーリンクがクリップボードにコピーされました!
*.star ファイルのヘッダーが xattrs に完全に対応するタイプになるように、適切な -xattr および -H=exustar オプションを使用する必要があります。これらのオプションおよびその他のオプションの詳細は、の man ページを参照してください。
ls -Z public_html/ web_files/
public_html/:
-rw-rw-r-- auser auser ...httpd_user_content_t 1.html
-rw-rw-r-- auser auser ...httpd_user_content_t 2.html
-rw-rw-r-- auser auser ...httpd_user_content_t 3.html
-rw-rw-r-- auser auser ...httpd_user_content_t 4.html
-rw-rw-r-- auser auser ...httpd_user_content_t 5.html
-rw-rw-r-- auser auser ...httpd_user_content_t index.html
web_files/:
-rw-rw-r-- auser auser user_u:object_r:user_home_t 1.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 2.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 3.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 4.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 5.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t index.html
star -xattr -H=exustar -c -f all_web.star public_html/ web_files/
star: 11 blocks + 0 bytes (total of 112640 bytes = 110.00k).
-Z オプションを指定して ls コマンドを使用して、セキュリティーコンテキストを検証します。
ls -Z all_web.star
-rw-rw-r-- auser auser user_u:object_r:user_home_t \ all_web.star
/tmp にコピーされます。派生一時タイプを作成する特定のポリシーがない場合、デフォルトの動作は tmp_t タイプを取得することです。
cp all_web.star /tmp/ cd /tmp/
ls -Z all_web.star
-rw-rw-r-- auser auser user_u:object_r:tmp_t all_web.star
star -xattr -x -f all_web.star
star: 11 blocks + 0 bytes (total of 112640 bytes = 110.00k).
ls -Z /tmp/public_html/ /tmp/web_files/
/tmp/public_html/:
-rw-rw-r-- auser auser ...httpd_sys_content_t 1.html
-rw-rw-r-- auser auser ...httpd_sys_content_t 2.html
-rw-rw-r-- auser auser ...httpd_sys_content_t 3.html
-rw-rw-r-- auser auser ...httpd_sys_content_t 4.html
-rw-rw-r-- auser auser ...httpd_sys_content_t 5.html
-rw-rw-r-- auser auser ...httpd_sys_content_t index.html
/tmp/web_files/:
-rw-rw-r-- auser auser user_u:object_r:user_home_t 1.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 2.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 3.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 4.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t 5.html
-rw-rw-r-- auser auser user_u:object_r:user_home_t \ index.html
/var/log/httpd/ に復元します。
star -xattr -H=exustar -c -f httpd_logs.star /var/log/httpd/