検索

第50章 SELinux の使用

download PDF
SELinux は、新しいセキュリティーパラダイムと、管理者およびエンドユーザー向けの新しいプラクティスとツールの両方を提供します。本章で説明するツールおよび手法は、エンドユーザー、管理者、およびアナリストが実施する標準操作に重点を置いています。

50.1. SELinux のエンドユーザーコントロール

一般的に、エンドユーザーは、Red Hat Enterprise Linux がターゲットポリシーを実行している場合に、SELinux との相互作用はほとんどありません。これは、ユーザーが unconfined_t のドメインと、ターゲットデーモン 以外 の残りのシステムで実行しているためです。
ほとんどの場合、標準の DAC コントロールは、SELinux が参照される前に、必要なアクセスまたはパーミッションを持たないタスクを実行することを防ぎます。したがって、avc: denied メッセージを生成しない可能性があります。
以下のセクションでは、エンドユーザーが Red Hat Enterprise Linux システムで実行する必要がある可能性のある一般的なタスクおよびプラクティスを説明します。これらのタスクは、エンドユーザーだけでなく、すべての特権レベルのユーザーに適用されます。

50.1.1. ファイルの移動とコピー

ファイルシステムの操作では、セキュリティーコンテキストは、ファイルのラベル、アクセスしているプロセス、および操作が発生したディレクトリーという点として考慮する必要があります。このため、mvcp のあるファイルを移動およびコピーすると、予期しない結果が生じる可能性があります。

ファイルのコピー: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
追加のコマンドライン引数なしで cp コマンドを使用すると、作成プロセスのデフォルトのタイプとターゲットディレクトリーを使用して、新しい場所にファイルのコピーが作成されます。この場合、cp および /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 デーモンは再ラベル付けするまでこれらのファイルを提供できなくなります。ファイルのラベリングの詳細は、「ファイルまたはディレクトリーの再ラベル」 を参照してください。

表50.1 mv コマンドおよび cp コマンドの動作
コマンド 動作
mv ファイルは、元のラベルを保持します。これにより、問題、混乱、またはマイナーなセキュリティーが発生する可能性があります。たとえば、sbin_t ドメインで実行している tmpwatch プログラムは、ファイルのタイプが原因で、/tmp ディレクトリー内の経過時間ファイルを削除できない可能性があります。
cp 作成プロセスのドメイン(cp)とターゲットディレクトリーのタイプに基づくデフォルトの動作を使用して、ファイルのコピーを作成します。
cp -p ファイルのコピーを作成し、可能な場合は指定された属性とセキュリティーコンテキストを保持します。デフォルトの属性は modeownership、および 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 コマンドの動作」 で説明されています。

以下の例は、ps コマンドの出力例を一部示しています。ほとんどのプロセスは 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
id コマンドで -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 以外のディレクトリーで動作するスクリプトを作成する場合は、ファイルの再ラベル付けが必要になる場合があります。
再ラベル付け操作には、以下の 2 つの一般的なタイプがあります。
  • ファイルの型を意図的に変更
  • ポリシーに応じたデフォルトの状態へのファイルの復元
また、管理者が実行する再ラベル付け操作もあります。これらについては、「ファイルシステムの再ラベル付け」 で説明されています。
ヒント
targeted ポリシーにおける SELinux パーミッション制御の大半は、Type Enforcement (TE)です。したがって、通常、セキュリティーラベルのユーザーおよびロール情報を無視し、タイプの変更にフォーカスできます。通常、ファイルのロールとユーザー設定を考慮する必要はありません。
注記
再ラベル付けがデーモンの実行可能ファイルのラベルに影響を及ぼす場合は、デーモンを再起動して、正しいドメインで実行していることを確認する必要があります。たとえば、/usr/sbin/mysqld に誤ったセキュリティーラベルがあり、restorecon などの再ラベル付け操作を使用してこれに対応する場合は、再ラベル付け操作の後に mysqld を再起動する必要があります。実行可能ファイルを正しいタイプ(mysqld_exec_t)に設定すると、起動時に適切なドメインに移行できるようになります。
chcon コマンドを使用して、ファイルを正しいタイプに変更します。このコマンドを使用するには、適用する正しいタイプを知っている必要があります。以下の例のディレクトリーおよびファイルには、/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
特別なユーザーのパブリック 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/
ヒント
カーネルで SELinux が無効になっているときに作成されたファイルなどのラベルがない場合は、chcon system_u:object_r:shlib_t foo.so で完全なラベルを指定する必要があります。そうしないと、部分的なコンテキストをラベル付けされていないファイルに適用するというエラーが発生します。
restorecon コマンドを使用して、ポリシーに従ってファイルをデフォルト値に戻します。ファイルシステム全体で機能するこの操作を実行する方法は、fixfiles または policy relabeling の 2 つがあります。これらの方法にはそれぞれスーパーユーザー権限が必要です。これらの方法の両方に対する注意点は、「ファイルシステムの再ラベル付け」 に記載されています。
以下の例は、デフォルトのユーザーのホームディレクトリーコンテキストを、異なるタイプのファイルセットに復元する方法を示しています。最初の 2 つのファイルセットにはタイプが異なるため、アーカイブのためにディレクトリーに移動されています。これらのコンテキストは相互に異なり、標準のユーザーのホームディレクトリーでは正しくありません。
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/
restorecon コマンドを使用してファイルに再ラベル付けするには、ポリシーで設定されたデフォルトのファイルコンテキストを使用するため、これらのファイルには現在のディレクトリーのデフォルトラベルのラベルが付けられます。
/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. セキュリティーコンテキストを保持するアーカイブの作成

tar ユーティリティーまたは star ユーティリティーを使用して、SELinux セキュリティーコンテキストを保持するアーカイブを作成できます。以下の例は star を使用して、このようなアーカイブを作成する方法を示しています。追加の属性がキャプチャーされ、*.star ファイルのヘッダーが xattrs に完全に対応するタイプになるように、適切な -xattr および -H=exustar オプションを使用する必要があります。これらのオプションおよびその他のオプションの詳細は、の man ページを参照してください。
以下の例は、html ファイルおよびディレクトリーの作成と抽出を示しています。2 つのディレクトリーには異なるラベルがあることに注意してください。ファイルコンテキストの未インポート部分は、印刷目的で省略されています(省略される...で示されます)。
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
以下のコマンドはアーカイブを作成し、すべての SELinux セキュリティーコンテキストを保持します。
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
注意
star を使用してアーカイブを作成するときに絶対パスを使用する場合、アーカイブは同じパスに展開されます。たとえば、このコマンドで作成されたアーカイブは、ファイルを /var/log/httpd/ に復元します。
star -xattr -H=exustar -c -f httpd_logs.star /var/log/httpd/
このアーカイブの拡張を試みると、パス のファイルがアーカイブにあるファイルよりも新しい場合は、警告を発行します。
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.