SELinuxの管理方法

SELinuxについてもうちょっと書きたいと思います。

ここでは、SELinuxのユーザとプロセスやファイルに設定されるアクセス権であるコンテクストの管理について話してみます。

SELinuxのユーザについて

前回お伝えしたようにSELinuxでは独自のユーザが存在しますので、通常のLoginユーザとの関係性について例を交えて説明します。

 

まず、Loginユーザ(userSE)を新規作成して、SELinuxのユーザ(staff_u)マッピングします。

SELinuxのユーザの種類についてはこちらを参照ください。

3.3. 制限のあるユーザーおよび制限のないユーザー - Red Hat Customer Portal

 

[root@host1 ~]# useradd -Z staff_u userSE
[root@host1 ~]#

 

ついでにパスワードも変更
[root@host1 ~]# echo pass123 |passwd --stdin userSE
Changing password for user userSE.
passwd: all authentication tokens updated successfully.

passwdコマンドで--stdin(標準入力)をつけると、echoで出力したpass123の文字列を読み取って設定することができます。

 

作成したユーザとSELinuxのユーザのマッピングを確認します。


[root@host1 ~]# semanage login -l

Login Name   SELinux User    MLS/MCS Range Service

__default__   unconfined_u    s0-s0:c0.c1023      *
root               unconfined_u    s0-s0:c0.c1023       *
system_u      system_u          s0-s0:c0.c1023       *
userSE         staff_u               s0-s0:c0.c1023       *
[root@host1 ~]#

 

一旦、userSEでログインして、idコマンドで表示するとこんな感じです。
SELinuxのユーザはPAM モジュールによって割り当てられるのでSSHでログインしています。


[root@host1 ~]# ssh userSE@localhost

[userSE@host1 ~]$ id -Z
staff_u:staff_r:staff_t:s0-s0:c0.c1023

 

ちなみに、 ユーザに特定のSELinuxユーザをマッピングしない場合は、__default__ の値が設定されます。

例)マッピングされていない既存のユーザ(userSE3)での表示

[root@host1 ~]# ssh userSE3@localhost

[userSE3@host1 ~]$ id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

 

次に、すでに存在するユーザ(userSE2)に、SELinuxのユーザ(user_u)を割り当ててみます。

[root@host1 ~]# semanage login -a -s user_u userSE2
[root@host1 ~]# semanage login -l

Login Name   SELinux User    MLS/MCS Range   Service

__default__    unconfined_u    s0-s0:c0.c1023       *
root                unconfined_u    s0-s0:c0.c1023       *
system_u       system_u          s0-s0:c0.c1023       *
userSE          staff_u               s0-s0:c0.c1023       *
userSE2        user_u               s0                           *
[root@host1 ~]#

 

ファイルのコンテキスト(ラベルの組み合わせ)について

rootユーザでテストファイル(testfile1)を作成して、ラベル(コンテキスト)を確認します。コンテクストに含まれるラベルは、ユーザ(_u)、ロール(_r)、タイプ(_t)、機密レベル(Sx)になります。

[root@host1 ~]# touch /root/testfile1
[root@host1 ~]#
[root@host1 ~]# ls -Z /root/testfile1
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/testfile1
[root@host1 ~]#

SELinuxユーザ(unconfined_u)とファイルのラベルであるタイプ(admin_home_t)のコンテキストが確認できます。

 

ここで、コンテキストを別のユーザ(user_u)とタイプ(public_content_t)へ変更してみます。


[root@host1 ~]# chcon -u user_u -t public_content_t /root/testfile1
[root@host1 ~]#
[root@host1 ~]# ls -Z /root/testfile1
-rw-r--r--. root root user_u:object_r:public_content_t:s0 /root/testfile1
[root@host1 ~]#

 

chconは一時的なコンテキストの変更なので、restoreconを実行すると戻ってしまいます。restoreconコマンドは、Configファイルで予め設定している内容でコンテキストを設定し直します。
※Configファイルはこちら:/etc/selinux/targeted/contexts/files/file_contexts.local

 

[root@host1 ~]# restorecon -vF /root/testfile1
restorecon reset /root/testfile1 context user_u:object_r:public_content_t:s0->system_u:object_r:admin_home_t:s0
[root@host1 ~]#

 

-vオプションを付けるとどこをどのように復元したのかも表示してくれます。
->以前の内容が、chconで一時的に変更された内容で、->以降に表示されているコンテキストがConfigファイルの設定になります。-Fは強制的なrestoreになります。

 

永続的に変更するには、fcontecstのサブコマンドを使用します。

fcontestは、-a(add),-d(delete),-l(list)のオプションがあり、永続的な変更では-aを使用します。前出のConfigファイルに書き込まれます。

 

[root@host1 ~]# semanage fcontext -a -s user_u -t public_content_t /root/testfile1

 

実際は、コンテキストを管理しているファイルにエントリーが追加されますので、追加状況は、-lオプションや直接そのファイルを参照することで確認できます。

[root@host1 ~]# semanage fcontext -l |grep testfile1
/root/testfile1 all files user_u:object_r:public_content_t:s0
[root@host1 ~]#
[root@host1 ~]# grep testfile1 /etc/selinux/targeted/contexts/files/file_contexts.local
/root/testfile1 user_u:object_r:public_content_t:s0
[root@host1 ~]#

 そのままで開くと大量に表示されますので、grepで絞り込むことをおすすめします。

なお、この状態ではまだ、実際に有効になっているコンテキストは変更されていないので、いったんrestoreconを実行してコンテキストを反映させます。

[root@host1 ~]# restorecon -vF /root/testfile1
restorecon reset /root/testfile1 context system_u:object_r:admin_home_t:s0->user_u:object_r:public_content_t:s0
[root@host1 ~]#
[root@host1 ~]# ls -Z /root/testfile1
-rw-r--r--. root root user_u:object_r:public_content_t:s0 /root/testfile1
[root@host1 ~]#