Ansible を使用して SELinux ステータス、コンテキスト、ポート、ブール値を管理する |
SELinux とは何ですか?なぜ重要ですか?
SELinux (Security Enhanced Linux) は、Linux のセキュリティ アーキテクチャです。 Linux システム内のアプリケーション、ファイル、プロセスへのアクセスを制御します。アクセスできるものとできないものを定義するセキュリティ ポリシーを使用します。たとえば、プロセスまたはアプリケーション (サブジェクトと呼ばれる) がファイルなどのオブジェクトにアクセスしたい場合、SELinux は対応するルールをチェックし、アクセスを許可するか拒否するかを決定します。
従来の Linux セキュリティでは、ユーザー、グループなどが所有および操作できるものを定義する任意のアクセス制御が使用されており、ユーザーはいつでもこれらのアクセス許可を変更できます。ただし、SELinux は、いわゆる強制アクセス制御を使用します。任意のアクセス制御によって一部の権限が定義されている場合でも、同じ権限が SELinux によって許可されていない場合、その権限は許可されません。これは、ポリシーが管理的に設定され、固定されているためです。これは、外部ユーザーがファイルへのアクセス許可を変更しようとすると、そのようなアクセスを防止する SELinux の設定によってアクセスが禁止され、システムが通常は保存されることを意味します。そうは言っても、SElinux を強制モードに保ちながら、さまざまなポリシーを管理する方法を学ぶことが重要です。
SELinux はどのように機能しますか?
デフォルトでは、SELinux は「対象を絞ったポリシー」を使用し、常に「強制」します。ターゲットとは、特定の (ターゲットの) プロセスのみが SELinux によって保護されることを意味します。ステータスを強制するとは、SELinux ポリシーがそれに応じて適用されることを意味します。これらの設定は /etc/selinux/config ファイルで変更できます。その他のステータス オプションは次のとおりです。
- 寛容 – SElinux ルールは適用されませんが、違反があった場合に備えて操作が記録されます。
- 無効 – SELinux ポリシーは適用されません
SElinux はラベル付けと強制を使用します。プロセス、ファイルなどには SELinux コンテキストのラベルが付けられます。ファイルとディレクトリのラベルは拡張属性としてファイル システムに保存されますが、プロセスとポートのラベルはカーネルによって管理されます。ラベルを付けると、同じラベルを持つリソースは相互にアクセスできますが、異なるラベルを持つリソースにはアクセスできないように、リソースがグループ化されます。たとえば、ファイルまたはプロセスの SELinux コンテキストを変更するか、ブール ステータスを変更することで、アクセスの有効化を回避できます。ブール値は基本的に SELinux のオン/オフ設定です。
SELinux の管理
ターミナル上で適切なコマンドを直接実行することで、Linux システム上の SELinux を手動で管理できます。 SELinux 管理に必要なほとんどの Linux パッケージはデフォルトでインストールされますが、使用するために他のパッケージを手動でインストールする必要がある場合があります。デフォルトでインストールされるパッケージには次のものがあります。
- policycoreutils は、SELinux を操作および管理するための restorecon、secon、setfiles、semodule、load_policy、setsebool などのユーティリティを提供します。
- selinux-policy は、基本的なディレクトリ構造、selinux-policy.conf ファイル、および RPM マクロを提供します。
- selinux-policy-targeted は、SELinux を対象としたポリシーを提供します。
- libselinux – SELinux アプリケーション用の API を提供します。
- libselinux-utils は、avcstat、getenforce、getsebool、matchpathcon、selinuxconlist、selinuxdefcon、selinuxenabled、setenforce ユーティリティを提供します。
- libselinux-python は、SELinux アプリケーションを開発するための Python バインディングを提供します。
手動でインストールする必要があるオプションのパッケージの一部を以下に示します。 「sudo yum install <パッケージ名>」を実行するだけです。
- selinux-policy-devel は、カスタム SELinux ポリシーとポリシー モジュールを作成するためのユーティリティを提供します。
- selinux-policy-doc には、SELinux をさまざまなサービスとともに構成する方法を説明したマニュアル ページが用意されています。
- selinux-policy-mls は、MLS (マルチレベル セキュリティ) SELinux ポリシーを提供します。
- Settroubleshot と setroubleshoot-server は、SELinux によってアクセスが拒否されたときに生成される拒否メッセージを、このパッケージで提供されている Sealert ユーティリティで表示できる詳細な説明に変換します。
- mcstrans は、s0-s0:c0.c1023 などのレベルを、SystemLow-SystemHigh などの読みやすい形式に変換します。
- policycoreutils-python は、SELinux を操作および管理するための semanage、audit2allow、audit2why、chcat などのユーティリティを提供します。
- policycoreutils-gui は、SELinux を管理するためのグラフィカル ユーティリティである system-config-selinux を提供します。
実行できることには、SELinux 構成設定の変更、Boolen のオンまたはオフの設定、ファイル/ディレクトリのコンテキストの変更などが含まれます。便利なコマンドには次のようなものがあります。
# Edit SELinux Configuration file
sudo vim /etc/selinux/config
# List all Booleans
getsebool -a
# Get a specific Boolean
getsebool <Boolean>
# Set a Boolean on or off
setsebool <Boolean> <on|off>
setsebool -P <Boolean> <1|0>
# Get SELinux contect of a file
ls -lZ <path-to-file>
# Change context of a file
chcon -t <context-type> <path-to-file>
# Or simply reference a known good file
chcon –reference <path-to-good-file> <path-to-ile-to-be-changed>
#Restore a directory and all its content to the default context
restorecon -vR <directory-path>
# Set the file context of a file/directory by telling SELinux what it needs to do with the file before enabling the setting with restorecon
semanage fcontext -a -e <existing-directory> <new-directory>
Ansible を使用した SELinux の管理
Ansible は、複数のリモート ホストを同時に操作する場合に役立つ IT 管理ツールです。 Ansible を使用すると、管理するすべてのリモート ホストと、ホスト上で実行するコマンドを含む Playbook を定義する単一のファイルを作成できます。 Ansible は ssh を使用してリモートホストにアクセスします。 Ansible を使用した SELinux の管理に役立つさまざまな Playbook を見てみましょう。
Ansible を使用した SELinux の状態の変更
SElinux の状態とポリシーを変更できるようにするには、Ansible サーバーにansible.posix.selinux プラグインをインストールする必要があります。このプラグインは ansible.posix コレクションの一部です。
$ ansible-galaxy collection install ansible.posix
Process install dependency map
Starting collection install process
Installing 'ansible.posix:1.2.0' to '/home/lorna/.ansible/collections/ansible_collections/ansible/posix'
次に、SELinux を Permissive モードに設定する Playbook を作成します。管理対象のリモート ホスト用のホスト ファイルがすでにあることを前提としています。
---
- hosts: all
tasks:
- name: Set SELinux to Permissive mode
ansible.posix.selinux:
policy: targeted
state: permissive
プレイブックを実行する
$ ansible-playbook playbook.yml
PLAY [all] ***********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [Set SELinux to Permissive mode] ***********************************************************************************************************************************************************************************************************************
changed: [192.168.100.161]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
192.168.100.161 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
強制モードに戻すには、次のようにします。
---
- hosts: all
tasks:
- name: Set SELinux to Enforcing Mode
ansible.posix.selinux:
policy: targeted
state: enforcing
Ansible を使用して SELinux を無効にするには:
---
- hosts: all
tasks:
- name: Disable SELinux
ansible.posix.selinux:
state: disabled
Ansible を使用した SELinux ファイルコンテキストの管理
ファイルコンテキストは、chcon、restorecon、semanage を使用して更新できます。ただし、Semanage は、ファイル/ディレクトリに変更があった場合にファイル/ディレクトリに対して何を行う必要があるかを SElinux に伝えるだけですが、実際の変更はrestorecon が行います。手動で更新する場合は、semanage fcontext を使用してから restorecon を使用して変更を適用します。ターゲット ポリシーを使用する場合、ファイル コンテキストの変更は /etc/selinux/targeted/files に書き込まれます。libselinux-python と policycoreutils が必要です。 -python は、「semanage」を使用できるようにリモートホストにインストールされます。 Ansible サーバーに community.general.sefcontext プラグインをインストールします。
$ ansible-galaxy collection install community.general
Process install dependency map
Starting collection install process
Installing 'community.general:3.4.0' to '/home/lorna/.ansible/collections/ansible_collections/community/general'
私たちのプレイブックでは、必要なパッケージをインストールし、Apache が /srv/git_repos 内のファイルを変更できるようにする例を見ていきます。
---
- hosts: all
become: yes
tasks:
- name: install Required Packages
yum:
name:
- "python3-libselinux"
- "policycoreutils-python-utils"
state: present
- name: Allow Apache to modify files in /srv/git_repos/
community.general.sefcontext:
target: '/srv/git_repos(/.*)?'
setype: httpd_sys_rw_content_t
state: present
- name: Apply SELinux file context
ansible.builtin.command: restorecon -irv /srv/git_repos
プレイブックを実行します
$ ansible-playbook playbook.yml
PLAY [all] ***********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [install Required Packages] *************************************************************************************************************************************************************************************************************
changed: [192.168.100.161]
TASK [Allow Apache to modify files in /srv/git_repos/] ***************************************************************************************************************************************************************************************
changed: [192.168.100.161]
TASK [Apply SELinux file context] ************************************************************************************************************************************************************************************************************
changed: [192.168.100.161]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
192.168.100.161 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible を使用した SELinux ネットワーク ポート タイプ定義の管理
管理ポート定義を変更できるようにするには、community.general コレクションの一部である community.general.seport プラグインが必要です。 ansible-galaxy を使用して、community.general コレクションをインストールする方法をすでに示しました。 libselinux-python モジュールと policycoreutils python モジュールもインストールする必要があります。以下は、ポートを有効にするさまざまな例を示すプレイブックです。
---
- hosts: all
become: yes
tasks:
- name: install Required Packages
yum:
name:
- "python3-libselinux"
- "policycoreutils-python-utils"
state: present
- name: Allow sshd to listen on tcp port 20222
community.general.seport:
ports: 20222
proto: tcp
setype: ssh_port_t
state: present
- name: Allow memcached to listen on a range of ports
community.general.seport:
ports: 10000-10100,10112
proto: tcp
setype: memcache_port_t
state: present
プレイブックを実行してください
$ ansible-playbook playbook.yml
PLAY [all] ***********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [install Required Packages] *************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [Allow sshd to listen on tcp port 20222] ************************************************************************************************************************************************************************************************
changed: [192.168.100.161]
TASK [Allow memcached to listen on a range of ports] *****************************************************************************************************************************************************************************************
changed: [192.168.100.161]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
192.168.100.161 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible を使用した SELinux ブール値の管理
ブール値は、SELinux ポリシーのオン/オフ設定です。 getsebool -a コマンドを使用すると、すべてのブール値とそのステータスを手動で表示できます。ステータスを変更する必要があるブール値を特定したら、setsebool
$ ansible-galaxy collection install ansible.posix
Process install dependency map
Starting collection install process
Skipping 'ansible.posix' as it is already installed
また、libselinux-pyth と libsemanage-python をインストールする必要があります。以下の Playbook は、必要なパッケージをインストールし、ブール値の httpd_can_connect_ftp を「ON」に設定して、再起動時に永続化できるようにします。
---
- hosts: all
become: yes
tasks:
- name: Install Required Packages
yum:
name:
- "python3-libselinux"
- "python3-libsemanage"
state: present
- name: Set httpd_can_connect_ftp flag ON and enable it to persist on reboot
ansible.posix.seboolean:
name: httpd_can_connect_ftp
state: yes
persistent: yes
プレイブックの実行時:
$ ansible-playbook playbook.yml
PLAY [all] ***********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [install Required Packages] *************************************************************************************************************************************************************************************************************
ok: [192.168.100.161]
TASK [Set httpd_can_connect_ftp flag ON and enable it to persist on reboot] ******************************************************************************************************************************************************************
changed: [192.168.100.161]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
192.168.100.161 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
このガイドでは、SELinux とは何か、それを強制し続けることが重要である理由を学び、SELinux がどのように機能するかについても説明しました。運用中のほとんどの場合、Linux の運用に干渉しないように SELinux を無効にしますが、それが実際に Linux の重要なセキュリティ面であることはあまり知られていません。 SElinx を完全に無効にするのではなく、SElinx を管理する方法を理解することが重要です。
Ansible 自動化を学ぶのに最適なコース:
- Ansible を理解する – Ansible の初心者からエキスパートまで
- Ansible Essentials とハンズオン ラボ
- Ansible をマスターする
- Ansible Advanced – ハンズオン – DevOps
- まったくの初心者のための Ansible – ハンズオン – DevOps
- DevOps 初心者とシステム管理者のための Ansible
このガイドが SELinux の重要性を理解するのに役立ち、セットアップのニーズに合わせて SELinux ポリシーを活用する方法を説明できたことを願っています。以下のさらに興味深いガイドをチェックしてください。
- Podman カスタム グラフルート ディレクトリの SELinux コンテキスト ラベルを設定する
- RHEL 8|CentOS 8 で SELinux を無効にする方法
- SELinux マニュアルページの設定
- SELinux 強制を使用した CentOS/RHEL および Fedora の SSH ポートの変更