SELinux
SELinux (Security-Enhanced Linux) は、システムにアクセスできるユーザーを管理者がより詳細に制御できるようにする Linux® システム 用のセキュリティ・アーキテクチャです。もともとは、Linux Security Modules (LSM) を使用した Linux カーネルへの一連のパッチとして、アメリカ国家安全保障局 (NSA) によって開発されました。
参照:https://www.redhat.com/ja/topics/linux/what-is-selinux
RBAC(ロールベースアクセス制御)
ロールといわれるアクセス権をユーザに設定することで、ユーザに対しての必要最小限の権限を設定できます。
MAC(強制アクセス制御)
ファイルの所有者ではなく、システムの管理者のみがファイルのアクセス権限を設定できる制御方法。
そのため、アクセス権限を一元管理することが出来る。
ファイル所有者がアクセス権限を設定する制御方式をDAC(任意アクセス制御)
セキュリティコンテキスト
SElinuxは、ユーザやプロセスから利用できるファイルをルールベースで集中管理することが出来る。
ユーザ・プロセス・ファイルにセキュリティコンテキスト
と呼ばれるラベルを付与して管理する。
# ユーザ識別子:ロール識別子:タイプ識別子 が表示される。
[admin@almalinux9 ~]$ id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# 項目の「LABEL」の部分がセキュリティコンテキストに当たる。
[admin@almalinux9 ~]$ ps -C sshd -Z
LABEL PID TTY TIME CMD
system_u:system_r:sshd_t:s0-s0:c0.c1023 82253 ? 00:00:00 sshd
system_u:system_r:sshd_t:s0-s0:c0.c1023 102471 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 102475 ? 00:00:00 sshd
system_u:system_r:sshd_t:s0-s0:c0.c1023 116999 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 117003 ? 00:00:00 sshd
[admin@almalinux9 ~]$ ls -lZ /etc/ssh
合計 600
-rw-r--r--. 1 root root system_u:object_r:etc_t:s0 578094 9月 27 05:21 moduli
-rw-r--r--. 1 root root system_u:object_r:etc_t:s0 1921 9月 27 05:21 ssh_config
drwxr-xr-x. 2 root root system_u:object_r:etc_t:s0 28 2月 8 00:57 ssh_config.d
-rw-r-----. 1 root ssh_keys system_u:object_r:sshd_key_t:s0 492 2月 8 01:12 ssh_host_ecdsa_key
-rw-r--r--. 1 root root system_u:object_r:sshd_key_t:s0 162 2月 8 01:12 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys system_u:object_r:sshd_key_t:s0 387 2月 8 01:12 ssh_host_ed25519_key
-rw-r--r--. 1 root root system_u:object_r:sshd_key_t:s0 82 2月 8 01:12 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys system_u:object_r:sshd_key_t:s0 2578 2月 8 01:12 ssh_host_rsa_key
-rw-r--r--. 1 root root system_u:object_r:sshd_key_t:s0 554 2月 8 01:12 ssh_host_rsa_key.pub
-rw-------. 1 root root system_u:object_r:etc_t:s0 3667 9月 27 05:21 sshd_config
drwx------. 2 root root system_u:object_r:etc_t:s0 28 2月 8 00:58 sshd_config.d
モードの変更
SElinuxには3つのモードがある。
モード | 内容 |
---|---|
Enforcing | SELinuxが有効な状態 |
Permissive | SELinuxは有効だが、アクセス拒否は実施しない |
Disabled | SELimuxが無効な状態 |
警告
SELINUX=disabled だと、メモリーリーク・カーネルパニックが発生する可能性があるため、Permissiveに設定することが推奨されています。
# 現在のSELinuxのモード確認
[admin@almalinux9 ~]$ getenforce
Enforcing
# Permissiveモードに変更
[admin@almalinux9 ~]$ sudo setenforce Permissive
# Enforcingモードに変更
[admin@almalinux9 ~]$ sudo setenforce Enforcing
SELinuxのブールパラメータ設定
SELinuxの設定には管理の知識を必要としますが、ブールパラメータを使用すると関連する設定をまとめて行うことが出来るので、比較的簡単に設定変更することが出来る。
# ブールパラメータの一覧表示
[admin@almalinux9 ~]$ getsebool -a
abrt_anon_write --> off
abrt_handle_event --> off
abrt_upload_watch_anon_write --> on
antivirus_can_scan_system --> off
antivirus_use_jit --> off
(以下省略)
# ブールパラメータの設定値の変更
# ユーザーホームディレクトリ (/home/*/ のコンテンツなど) へのhttpdアクセスを許可
[admin@almalinux9 ~]$ sudo setsebool httpd_enable_homedirs on
# 再起動後も同じ設定を維持する。-Pオプションを付与する。
[admin@almalinux9 ~]$ sudo setsebool -P httpd_enable_homedirs on
ファイルコンテキストの変更
ファイル・ディレクトリを作成した後に移動させると、元のコンテキストを引き継ぐことがあるので、
標準的なコンテキストに変更する対応を行う場合がある。
# /var/ftpに移動して、ファイルを作成する。
[admin@almalinux9 ftp]$ sudo touch sefile.txt
[admin@almalinux9 ftp]$ ls -lZ
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 2月 9 02:27 sefile.txt
# 標準のコンテキストに変更 -Rオプションで再帰的に変更する
[admin@almalinux9 ~]$ sudo restorecon sefile.txt
# 確認コマンド 左から3番目の対応識別子が変更されている!
[admin@almalinux9 ~]$ ls -lZ
-rw-r--r--. 1 root root unconfined_u:object_r:user_home_t:s0 0 2月 9 02:27 sefile.txt
semanageコマンド
新たなディレクトリにhttpdがアクセスできるようにするなど、標準と異なる設定にするにはsemanage
コマンドで設定を行う。
[admin@almalinux9 ~]$ sudo semanage fcontext -l
SELinux fcontext タイプ コンテキスト
/ directory system_u:object_r:root_t:s0
/.* all files system_u:object_r:default_t:s0
/[^/]+ regular file system_u:object_r:etc_runtime_t:s0
/\.autofsck regular file system_u:object_r:etc_runtime_t:s0
/\.autorelabel regular file system_u:object_r:etc_runtime_t:s0
/\.ismount-test-file regular file system_u:object_r:sosreport_tmp_t:s0
/\.journal all files <<None>>
/\.snapshots(/.*)? all files system_u:object_r:snapperd_data_t:s0
/\.suspended regular file system_u:object_r:etc_runtime_t:s0
(以下省略)
今回は、/var/ftp/incomingというディレクトリを作成し、配下のディレクトリ・ファイルに
public_content_rw_tというタイプを設定します。
# 設定の追加
[admin@almalinux9 ~]$ sudo semanage fcontext -a -t public_content_rw_t "/var/ftp/incoming(/.)?"
# セキュリティコンテキストを反映
[admin@almalinux9 ~]$ sudo restorecon -R /var/ftp/incoming/
# 設定の削除
[admin@almalinux9 ~]$ sudo semanage fcontext -d -t public_content_rw_t "/var/ftp/incoming(/*)?"