SELinuxコンテキスト
ファイルのSELinuxコンテキストを修正する。
何ができるか
- 一例であるが、SELinux有効の状態で、通常と異なるドキュメントルートのHTTPコンテンツ公開
- SELinuxを無効にするそんな非常識な人はいない大前提
- ファイル個別にSELinuxコンテキストラベルの付与
ファイルのSELinuxコンテキストを修正するステップ
注意:変更した場合は、必ず最後にrestoreconを入れる。
- chconにて設定
chcon -t samba_share_t /etc/file1
- 状態を確認
ls -Z /etc/file1
- 問題なければ、semanageで永続的設定
semanage fcontext -a -t samba_share_t /etc/file1
- restoreconも入れておく(semanageしてなくても、必ず入れる)。
restorecon -v /etc/file1
chcon
一時的な変更(-Rを付けるとフォルダ再帰)
[vagrant@localhost ~]$ ls -Z file1
unconfined_u:object_r:user_home_t:s0 file1
[vagrant@localhost ~]$
[vagrant@localhost ~]$ chcon -t samba_share_t file1
[vagrant@localhost ~]$ ls -Z file1
unconfined_u:object_r:samba_share_t:s0 file1
[vagrant@localhost ~]$
chconを戻す場合
restorecon -v file_name
[vagrant@localhost ~]$ restorecon -v file1
Relabeled /home/vagrant/file1 from unconfined_u:object_r:samba_share_t:s0 to unconfined_u:object_r:user_home_t:s0
[vagrant@localhost ~]$ ls -Z file1
unconfined_u:object_r:user_home_t:s0 file1
[vagrant@localhost ~]$
- restoreconも-Rでフォルダ再帰
- restorecon -vは、show changes in file labels.
semanage
永続的な変更。semanage打鍵後、restoreconすべき。ディレクトリに注意。/の有り無しで、挙動が変わる。
[root@localhost ~]# ls -Z /etc/file1
unconfined_u:object_r:etc_t:s0 /etc/file1
[root@localhost ~]# semanage fcontext -a -t samba_share_t /etc/file1
[root@localhost ~]#
restoreconでも戻っていないことがわかる。
[root@localhost ~]# restorecon -v /etc/file1
Relabeled /etc/file1 from unconfined_u:object_r:etc_t:s0 to unconfined_u:object_r:samba_share_t:s0
[root@localhost ~]#
[root@localhost ~]# ls -Z /etc/file1
unconfined_u:object_r:samba_share_t:s0 /etc/file1
[root@localhost ~]#
検証:エントリーが消えても、ファイルには反映されない
そのためrestoreconまで打鍵する必要がある。
[root@localhost files]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
# This file is auto-generated by libsemanage
# Do not edit directly.
/etc/file1 system_u:object_r:samba_share_t:s0
[root@localhost files]#
[root@localhost files]# semanage fcontext -d /etc/file1
[root@localhost files]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
# This file is auto-generated by libsemanage
# Do not edit directly.
[root@localhost files]# ls -Z /etc/file1
unconfined_u:object_r:samba_share_t:s0 /etc/file1
[root@localhost files]#
/etc/file1削除後、再作成すると、エントリーがある状態でもetc_tになる(親ディレクトリを引き継ぐ)。
# エントリーがある状態で、削除、再作成後、
[root@localhost files]# ls -Z /etc/file1
unconfined_u:object_r:etc_t:s0 /etc/file1
[root@localhost files]#
[root@localhost files]# restorecon -v /etc/file1
Relabeled /etc/file1 from unconfined_u:object_r:etc_t:s0 to unconfined_u:object_r:samba_share_t:s0
[root@localhost files]#
[root@localhost files]# ls -Z /etc/file1
unconfined_u:object_r:samba_share_t:s0 /etc/file1
[root@localhost files]#
# restoreconでエントリーの状態になる
検証:エントリーと異なる状態でリブート
ラベルは戻らない
- 親etc,現在etc,エントリーsamba
- reboot
- →etc(エントリーの状態には戻らない)
ラベルの削除
semanage context -d file/directory
元に戻す場合のコマンド
restorecon -RFv xxx
-R, -r change files and directories file labels recur‐
sively (descend directories).
-F Force reset of context to match file_context for
customizable files, and the default file context,
changing the user, role, range portion as well as
the type.
定義ルール一覧
semanage fcontext -l
コンテキストの調べ方
dnf -y install setools-console
# sesearchで調べられる
sesearch --allow | grep httpd
sesearch -b httpd_can_network_connect --allow
# -b boolean
install semanage command
# semanage for 8, 9
dnf -y install policycoreutils-python-utils
Reference
Red Hat Customer Portal - Access to 24x7 support and knowledge
◀︎ Podmanコマンド・単発実行例 | システム認証設定 ▶︎