Red Hat Training

A Red Hat training course is available for RHEL 8

第4章 非標準設定でのアプリケーションとサービスの SELinux 設定

SELinux が Enforcing モードの場合、デフォルトのポリシーはターゲットポリシーになります。以下のセクションでは、ポート、データベースの場所、プロセスのファイルシステムパーミッションなどの設定のデフォルトを変更した後に、さまざまなサービスに対して SELinux ポリシーを設定および設定する方法を説明します。

非標準ポート用に SELinux タイプを変更し、デフォルトディレクトリーの変更に関する間違ったラベルを特定して修正し、SELinux ブール値を使用してポリシーを調整する方法を説明します。

4.1. 非標準設定での Apache HTTP サーバーの SELinux ポリシーのカスタマイズ

Apache HTTP サーバーを設定して、別のポートでリッスンし、デフォルト以外のディレクトリーにコンテンツを提供できます。SELinux の拒否を防止するには、以下の手順に従い、システムの SELinux ポリシーを調整します。

前提条件

  • httpd パッケージがインストールされ、Apache HTTP サーバーが TCP ポート 3131 をリッスンし、デフォルトの /var/www/ ディレクトリーの代わりに /var/test_www/ ディレクトリーを使用するように設定されています。
  • policycoreutils-python-utils パッケージおよび setroubleshoot-server パッケージがシステムにインストールされている。

手順

  1. httpd サービスを起動して、ステータスを確認します。

    # systemctl start httpd
    # systemctl status httpd
    ...
    httpd[14523]: (13)Permission denied: AH00072: make_sock: could not bind to address [::]:3131
    ...
    systemd[1]: Failed to start The Apache HTTP Server.
    ...
  2. SELinux ポリシーは、httpd がポート 80 で実行していることを前提としています。

    # semanage port -l | grep http
    http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
    http_cache_port_t              udp      3130
    http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t            tcp      5988
    pegasus_https_port_t           tcp      5989
  3. ポート 3131 の SELinux タイプを、ポート 80 に一致させるように変更します。

    # semanage port -a -t http_port_t -p tcp 3131
  4. httpd を再開します。

    # systemctl start httpd
  5. ただし、コンテンツにはアクセスできません。

    # wget localhost:3131/index.html
    ...
    HTTP request sent, awaiting response... 403 Forbidden
    ...

    sealert ツールの理由を確認します。

    # sealert -l "*"
    ...
    SELinux is preventing httpd from getattr access on the file /var/test_www/html/index.html.
    ...
  6. matchpathcon ツールを使用して、標準パスと新規パスの SELinux タイプを比較します。

    # matchpathcon /var/www/html /var/test_www/html
    /var/www/html       system_u:object_r:httpd_sys_content_t:s0
    /var/test_www/html  system_u:object_r:var_t:s0
  7. 新しい /var/test_www/html/ コンテンツディレクトリーの SELinux タイプを、デフォルトの /var/ ディレクトリーのタイプに変更します。

    # semanage fcontext -a -e /var/www /var/test_www
  8. 再帰的に、/var ディレクトリーのラベルを再設定します。

    # restorecon -Rv /var/
    ...
    Relabeled /var/test_www/html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
    Relabeled /var/test_www/html/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

検証

  1. httpd サービスが実行していることを確認します。

    # systemctl status httpd
    ...
    Active: active (running)
    ...
    systemd[1]: Started The Apache HTTP Server.
    httpd[14888]: Server configured, listening on: port 3131
    ...
  2. Apache HTTP サーバーが提供するコンテンツがアクセスできることを確認します。

    # wget localhost:3131/index.html
    ...
    HTTP request sent, awaiting response... 200 OK
    Length: 0 [text/html]
    Saving to: ‘index.html’
    ...

関連情報

  • man ページの semanage(8)matchpathcon(8)、および sealert(8)