SELinux

【SELinux】chcon/restorecon/semanage fcontext の使い方とオプション

SELinux のラベリングルール

SELinux が有効な状態でどこかのディレクトリに file/dir を作成すると、その file/dir には「コンテキスト」と呼ばれる拡張属性が自動で付与されます。どのコンテキストが付与されるかは、「ラベリングルール」と呼ばれる SELinux ポリシーによって決まっています。具体的には「このディレクトリで生成した file/dir はこのコンテキストを付与する」といったルールが書かれています。

chcon はラベリングルールを無視してコンテキストを変更するコマンド、restorecon はラベリングルールの通りに変更するコマンド、semanage fcontext コマンドはラベリングルールを確認したりルールを変更したりするコマンドです。

なお、SELinux のデフォルト設定ではポリシーが "targeted" というモードで動作しており、この場合はコンテキストの [ユーザ:ロール:タイプ:レベル] の4つの属性のうち、3つ目の「タイプ」しかアクセス制御に利用されませんので、このデフォルト設定においてはユーザやロールを変更することは意味のないものだと理解してください。

chcon の使い方、オプション

chcon は SELinuxのコンテキストを、自分の好きなコンテキストに変更できます。例えば testuser がホームディレクトリ /home/testuser で test というディレクトリと test.txt というファイルを作り、それを root権限で /root 配下に mv した時、以下のようになります。

[root@localhost ~]# ls -Z
-rw-------. root     root     system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. testuser testuser unconfined_u:object_r:user_home_t:s0 test
-rw-rw-r--. testuser testuser unconfined_u:object_r:user_home_t:s0 test.txt

anaconda-ks.cfg は /root 配下に自動で生成されるファイルですので、コンテキストのSELiuxユーザはシステムユーザを表す system_u となっています。一方、testuser 等のシェル上でユーザが作成したファイルは unconfined_u というSELinuxユーザになります。これを system_uに変えたい!という場合は以下のようにchconコマンドを使います。

[root@localhost ~]# chcon -u system_u test.txt
[root@localhost ~]# ls -Z
-rw-------. root     root     system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. testuser testuser unconfined_u:object_r:user_home_t:s0 test
-rw-rw-r--. testuser testuser system_u:object_r:user_home_t:s0 test.txt

(前述の通り、デフォルトの targeted ポリシー環境においては意味はありませんが、)ロールやタイプも変更したい場合は以下のようにします。

[root@localhost ~]# chcon -u unconfined_u -r unconfined_r -t admin_home_t test.txt
[root@localhost ~]# ls -Z
-rw-------. root     root     system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. testuser testuser unconfined_u:object_r:user_home_t:s0 test
-rw-rw-r--. testuser testuser unconfined_u:unconfined_r:admin_home_t:s0 test.txt

restorecon の使い方、オプション

restorecon は SELinux のコンテキストをラベリングルールの定義通りに変更するコマンドです。例えば先程 /home/testuser から mv してきた test というディレクトリをrestorecon で修正します。

[root@localhost ~]# ls -Z
drwxr-xr-x. testuser testuser unconfined_u:object_r:user_home_t:s0 test
[root@localhost ~]# restorecon test
[root@localhost ~]# ls -Z
drwxr-xr-x. testuser testuser unconfined_u:object_r:admin_home_t:s0 test

user_home_t が admin_home_t に変更されました。これは /home/testuser配下で作成されるファイルのコンテキストタイプは user_home_t となる定義ルールがある一方、/root配下で作成されるファイルのコンテキストタイプは admin_home_t となる定義ルールがあるからです。定義ルールは semanage fcontext -l で確認できます。

[root@localhost ~]# semanage fcontext -l | grep user_home_t
/home/[^/]+/.+                                     all files          unconfined_u:object_r:user_home_t:s0
[root@localhost ~]# semanage fcontext -l | grep admin_home_t
/root(/.*)?                                        all files          system_u:object_r:admin_home_t:s0
[root@localhost ~]#

(前述の通り、デフォルトの targeted ポリシー環境においては意味はありませんが、) /root 配下のコンテキストのユーザは system_u になっていますが、 restorecon コマンドで戻っていません。オプション無しではタイプしかリセットされないためです。ユーザやロールもリセットしたい場合は -F を付けます。

[root@localhost ~]# restorecon -F test
[root@localhost ~]# ls -Z
-rw-------. root     root     system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. testuser testuser system_u:object_r:admin_home_t:s0 test
-rw-rw-r--. testuser testuser unconfined_u:object_r:user_home_t:s0 test.txt
[root@localhost ~]#

ユーザも system_u にリセットされました。ところで、test 配下のファイル等はリセットされていません。

[root@localhost test]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:user_home_t:s0 test1.txt
-rw-r--r--. root root unconfined_u:object_r:user_home_t:s0 test2.txt
[root@localhost test]#

再帰的にリセットする場合は -R を使います。

[root@localhost test]# restorecon -FR ../test
[root@localhost test]# ls -Z
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test1.txt
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test2.txt
[root@localhost test]#

配下のファイルもリセットされました。

semanage fcontext の使い方

chcon の効果を永続させたいときはラベリングルールを変える必要があります。ラベリングルールを変えるコマンドは semanage fcontext です。

前述の通り、ポリシーで使われているラベリングルール一覧を表示するのは -l オプションです。

[root@localhost ~]# semanage fcontext -l | more
SELinux fcontext               タイプ              コンテキスト

/.*                            all files          system_u:object_r:default_t:s0
/[^/]+                         regular file       system_u:object_r:etc_runtime_t:s0
/a?quota\.(user|group)         regular file       system_u:object_r:quota_db_t:s0
/nsr(/.*)?                     all files          system_u:object_r:var_t:s0
/sys(/.*)?                     all files          system_u:object_r:sysfs_t:s0
/xen(/.*)?                     all files          system_u:object_r:xen_image_t:s0
/mnt(/[^/]*)?                  directory          system_u:object_r:mnt_t:s0
/mnt(/[^/]*)?                  symbolic link      system_u:object_r:mnt_t:s0
/bin/.*                        all files          system_u:object_r:bin_t:s0
/dev/.*                        all files          system_u:object_r:device_t:s0
/srv/.*                        all files          system_u:object_r:var_t:s0
/var/.*                        all files          system_u:object_r:var_t:s0
/usr/.*                        all files          system_u:object_r:usr_t:s0
~~~

自分でルールを作成する場合は -a を使います。例えば /home/webmaster を含む、配下の全ファイルのコンテキストタイプを httpd_sys_content_t にしたい場合は以下のように設定します。

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/home/webmaster(/.*)?'
[root@localhost ~]#

-a はルール追加(add)、-t はタイプです。自分で設定したルールだけを見たい場合は -E を使います。

[root@localhost ~]# semanage fcontext -E
fcontext -a -f a -t httpd_sys_content_t '/home/webmaster(/.*)?'

"-f a" が自動で追加されています。-f はファイルタイプ、 a は全てのタイプのファイル(all files)を意味します。ファイルタイプは他に通常のファイル(regular file)/ディレクトリ(directory)/シンボリックリンク(symbolic link)等があります。

デフォルトのtargeted ポリシーではアクセス制御にはタイプしか使わないのであまり意味のないことですが、タイプ以外のコンテキスト属性がどうなっているかが分かりません。具体的にどのようなルールになっているかは -l で直接見ます。

[root@localhost ~]# semanage fcontext -l | grep webmaster
/home/webmaster(/.*)?          all files        system_u:object_r:httpd_sys_content_t:s0

ユーザは system_u 、ロールは object_r になっています。これはデフォルト値と考えればよいでしょう。(意味はありませんが)もしこれを変更したいのであれば -e でコンテキスト全てを指定します。

[root@localhost ~]# semanage fcontext -a -e "unconfined_u:unconfined_r:httpd_sys_content_t:s0" '/home/webmaster(/.*)?'
[root@localhost ~]# semanage fcontext -E
fcontext -a -e unconfined_u:unconfined_r:httpd_sys_content_t:s0 /home/webmaster(/.*)?
[root@localhost ~]# semanage fcontext -l | grep webmaster
/home/webmaster(/.*)? = unconfined_u:unconfined_r:httpd_sys_content_t:s0
[root@localhost ~]#

自分で作ったルールを全て削除したい場合は -d を使います。

[root@localhost ~]# semanage fcontext -d -t httpd_sys_content_t '/home/webmaster(/.*)?'
[root@localhost ~]# semanage fcontext -E
[root@localhost ~]#

もしくは --deleteall で自作ルール全てを削除することもできます。

[root@localhost ~]# semanage fcontext --deleteall
[root@localhost ~]# semanage fcontext -E
[root@localhost ~]#

コメント

タイトルとURLをコピーしました