コンテナバインドマウントでのSELinux z/Zオプション設定のホスト全体の結果

コンテナバインドマウントでのSELinux z/Zオプション設定のホスト全体の結果

ボリュームマウントを使用してPodmanコンテナを実行する場合は、通常、マウントパスに:z(または)パラメータを追加する必要があります。:ZこれはSELinuxとそのタイプの強制によるものです。このパラメータの目的は、インストールされているファイルの種類を変更することです。私はこれが実際には非常に危険な作業だと思いますが、他のSELinuxタイプを適用する必要があるいくつかのファイルをマウントすると、いくつかの問題が発生する可能性があります。 Apache httpd サーバーの例を考えてみましょう。私が知っている限り、httpdは適切にタグ付けされたディレクトリにのみファイルを提供できます(たとえば、httpd_file_t何でも構いません)。 Podmanコンテナを起動してwwwhttpdのディレクトリをここにマウントすると、:zどうなりますか?コンテキストがcontainer_file_tタイプに変わり、サーバーが機能しなくなると思います(サーバー上のファイルへのアクセスが失われますwww)。

私の理解は正しいですか?

ベストアンサー1

あなたの理解は正しいです。 Dockerマニュアルで(デフォルトではpodmanと同じ):

Configure the selinux label

If you use selinux you can add the z or Z options to modify the selinux label of the host file or directory being mounted into the container. This affects the file or directory on the host machine itself and can have consequences outside of the scope of Docker.

    The z option indicates that the bind mount content is shared among multiple containers.
    The Z option indicates that the bind mount content is private and unshared.

Use extreme caution with these options. Bind-mounting a system directory such as /home or /usr with the Z option renders your host machine inoperable and you may need to relabel the host machine files by hand.

:Zつまり、ホストディレクトリをDockerにバインドマウントすると、SELinuxコンテキストラベルが変更され、特定のディレクトリがその特定のコンテナ()またはすべてのコンテナ(:z)からのみアクセスできるようになります。

これはホストファイルシステム自体で行われるため、結果が発生します。コンテナ仮想化を超えて。あなたの例は良い例です。ホスト Web ルートをバインドマウントすると、標準がhttpd_sys_(rw_)content_t置き換えられます。これにより、ホストのApacheはWebルートへのサービスを拒否します。

幸いなことに、この問題はカスタム戦略モジュールを使用して簡単に克服できます。

Docker/podman を使用:zまたは:Z開始します。動作していることを確認してください。ここで Set SELinux を permissive として使用しますsetenforce 0。その後、Apacheを起動します。 SELinux は許可されているため動作しますが、AVC 拒否は監査ログに記録されます。

# use the output of ausearch to create a policy that allows this. This is the dry-run version
ausearch -m avc -ts recent | audit2allow -a
# If you're satisfied that this is indeed a module you want to add to the SELinux system,
# pick a policyname (use a custom prefix like mnj-) and run
ausearch -m avc -ts recent | audit2allow -a -M mnj-[policyname]

これにより、ポリシーモジュールファイルが生成されます/etc/selinux/targeted/modules/active/modules

このモジュールをインストールして有効にするには、次の手順を実行します。

semodule -i mnj-[policyname].pp

これでSELinuxを再度有効にしますsetenforce 1。これで、ApacheはWebrootを再提供できるようになりました。

おすすめ記事