多くのパスに対してfcontextルールを追加/更新する最良の方法は何ですか?

多くのパスに対してfcontextルールを追加/更新する最良の方法は何ですか?

多くのパスに対してfcontextを構成するルーチンが必要です。現在、私はこのタスクを実行するためにsemanageを呼び出すbashスクリプトを維持しています。しかし、これはかなり長いようです。ここで主な問題は次のとおりです。

  1. ルールに従ってオブジェクトを更新する単純なコマンドが見つかりません。オブジェクトが存在することを確認するために使用し、semanage fcontext -l | grep <object>そのオブジェクトから選択するか、-a結果-mに依存する必要があります。このコマンドは、呼び出しあたり約0.5〜1秒かかるのに非常に遅いです。ルールの更新を強制する既存のツールはありますか?

  2. Semanage fcontext呼び出しが非常に遅いです。 confファイルを使用してルールを定義する方法はありますか?ファイルがありますが、/etc/selinux/targeted/contexts/files/file_contexts.local手動で編集しないでください。

修正する

カスタムポリシーモジュールは、さまざまなパスのルールを処理する標準的な方法です。しかしsemodule -i、実際にはループセマンティクスコマンドよりはるかに遅いです。

以下はmytest.fcの例です。

/var/run/mytest/afolder(/.*)?  gen_context(system_u:object_r:httpd_sys_content_t, s0)
/var/run/mytest/bfolder(/.*)?  gen_context(system_u:object_r:httpd_sys_rw_content_t, s0)
/var/run/mytest/cfolder(/.*)?  gen_context(system_u:object_r:httpd_sys_content_t, s0)
/var/spool/mytest/alink  gen_context(system_u:object_r:httpd_sys_content_t, s0)
/var/spool/mytest/blink  gen_context(system_u:object_r:httpd_sys_rw_content_t, s0)
/var/spool/mytest/clink  gen_context(system_u:object_r:httpd_sys_content_t, s0)

私のサンプルスクリプトのバージョン:


#!/bin/bash
set -x
httpd_sys_content_paths=(
"/var/spool/mytest/alink"
"/var/spool/mytest/clink"
"/var/run/mytest/afolder(/.*)?"
"/var/run/mytest/cfolder(/.*)?"
)

for _path in "${httpd_sys_content_paths[@]}"
do
    set +e
    semanage fcontext -d "${_path}"
    set -e
    semanage fcontext -a -s system_u -t httpd_sys_content_t "${_path}"
done

httpd_sys_rw_content_paths=(
    "/var/spool/mytest/blink"
    "/var/run/mytest/bfolder(/.*)?"
)

for _path in "${httpd_sys_rw_content_paths[@]}"
do
    set +e
    semanage fcontext -d "${_path}"
    set -e
    semanage fcontext -a -s system_u -t httpd_sys_rw_content_t "${_path}"
done

semodule -i35秒かかります。ループには25秒かかります。

ベストアンサー1

あなたは正しいです、semanage fcontext遅いだけです。ただし、バッチモードで継続的に実行するように設計されていません。

したがって、カスタムSELinuxポリシーを直接作成することをお勧めします。 RHEL派生製品の場合は、policycoreutils-develパッケージのインストールが含まれます(順番にラベル付けされていますselinux-policy-devel)。

ここでは2つの可能性があります。最初のオプションは速くて汚いです。 2番目のオプションはより難しいですが、より多くの力を与えるでしょう。

オプション1 - makeを使用したシンプルな戦略パッケージ

どちらのファイルにも、デフォルトのSELinuxファイルコンテキストを持つ/unixsetestファイルを含むディレクトリがあるとします。testfiledefault_t

$ ls -laZ /unixsetest
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 .
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 testfile

それでは、ファイルコンテキストを取得したいのですが、testfile質問に言及した理由で使用したくないとしましょうadmin_home_tsemanage fcontext

unixsetest.te次の内容で(typeforce)というファイルを作成します。

policy_module(unixsetest, 1.0)

unixsetest.fc次の内容で(ファイルコンテキスト定義)というファイルを作成します。

/unixsetest(/.*)?   --  gen_context(system_u:object_r:admin_home_t,s0)

次に、次のコマンドを実行して、カスタマイズしたSELinuxポリシーにコンパイルします。

$ make -f /usr/share/selinux/devel/Makefile
Compiling targeted unixsetest module
/usr/bin/checkmodule:  loading policy configuration from tmp/unixsetest.tmp
/usr/bin/checkmodule:  policy configuration loaded
/usr/bin/checkmodule:  writing binary representation (version 19) to tmp/unixsetest.mod
Creating targeted unixsetest.pp policy package
rm tmp/unixsetest.mod.fc tmp/unixsetest.mod

そしてインストールしてください:

$ semodule -i unixsetest.pp

負荷確認:

$ semodule -l | grep unixsetest
unixsetest      1.0

ファイルコンテキストがファイルコンテキスト定義データベースにロードされていることを確認します。

$ semanage fcontext -l | grep admin_home_t
/root(/.*)?                                        all files          system_u:object_r:admin_home_t:s0
/unixsetest(/.*)?                                  regular file       system_u:object_r:admin_home_t:s0

(最初の行はオペレーティングシステムのデフォルトのSElinuxポリシーの一部であり、2行目はカスタムポリシーファイルを介して追加された部分です。)

次に、次のコマンドを使用してファイルコンテキストを適用しますrestorecon

$ restorecon -Rv /unixsetest/
restorecon reset /unixsetest/testfile context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:admin_home_t:s0

効率的な:

 ls -laZ /unixsetest
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 .
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 testfile

オプション2 - sepolicy生成ツールセットの複雑なポリシー(RPM)パッケージの使用

これでsepolicy-generateツールにアクセスできます。実行man sepolicy-generateして何ができるかを確認してください。 RPMベースのインストール用に設計されており、ポリシーインストールとパッケージインストールを緊密に統合します。

sepolicy generate実行するアクションを指定する適切なパラメータを使用してテンプレートを実行して、テンプレートからポリシーを作成できます。

       Confined Applications

       sepolicy generate --application [-n NAME] [-u USER ]command [-w WRITE_PATH ]
       sepolicy generate --cgi [-n NAME] command [-w WRITE_PATH ]
       sepolicy generate --dbus [-n NAME] command [-w WRITE_PATH ]
       sepolicy generate --inetd [-n NAME] command [-w WRITE_PATH ]
       sepolicy generate --init [-n NAME] command [-w WRITE_PATH ]

       Confined Users

       sepolicy generate --admin_user [-r TRANSITION_ROLE] -n NAME
       sepolicy generate --confined_admin -n NAME [-a ADMIN_DOMAIN] [-u USER] [-n NAME] [-w WRITE_PATH]
       sepolicy generate --desktop_user -n NAME [-w WRITE_PATH]
       sepolicy generate --term_user -n NAME [-w WRITE_PATH]
       sepolicy generate --x_user -n NAME [-w WRITE_PATH]

       Miscellaneous Policy

       sepolicy generate --customize -d DOMAIN -n NAME [-a ADMIN_DOMAIN]
       sepolicy generate --newtype -t type -n NAME
       sepolicy generate --sandbox -n NAME

ポリシーパッケージには、次の5つのファイルが含まれています。

       Type Enforcing File NAME.te
       This file can be used to define all the types rules for a particular domain.

       Note: Policy generated by sepolicy generate will automatically add a permissive DOMAIN  to
       your  te  file.   When  you  are  satisfied that your policy works, you need to remove the
       permissive line from the te file to run your domain in enforcing mode.

       Interface File NAME.if
       This file defines the interfaces for the types generated in the te file, which can be used
       by other policy domains.

       File Context NAME.fc
       This file defines the default file context for the system, it takes the file types created
       in the te file and associates file paths to the types.  Tools like restorecon and RPM will
       use these paths to put down labels.

       RPM Spec File NAME_selinux.spec
       This  file  is  an  RPM  SPEC  file  that  can be used to install the SELinux policy on to
       machines and setup the labeling. The spec file also installs the interface file and a  man
       page  describing  the  policy.   You  can use sepolicy manpage -d NAME to generate the man
       page.

       Shell File NAME.sh
       This is a helper shell script to compile, install  and  fix  the  labeling  on  your  test
       system.   It  will also generate a man page based on the installed policy, and compile and
       build an RPM suitable to be installed on other machines

もちろん、.fcファイルに特に興味があるでしょう。

あなたを助ける良いチュートリアルがあります。特にRH文書をお勧めします:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/writing-a-custom-selinux-policy_using-selinuxそしてhttps://linuxconcept.com/tutorial/extending-generated-selinux-policies/

おすすめ記事