第 4 章 为使用非标准配置的应用程序和服务配置 SELinux

当 SELinux 处于 enforcing 模式时,默认策略是目标(targeted)策略。以下小节提供了有关在更改配置默认值后为各种服务设置和配置 SELinux 策略的信息,如端口、数据库位置或进程的文件系统权限。

您将了解如何为非标准端口更改 SELinux 类型,识别并修复默认目录更改的不正确的标签,以及使用 SELinux 布尔值调整策略。

4.1. 在非标准配置中为 Apache HTTP 服务器自定义 SELinux 策略

您可以将 Apache HTTP 服务器配置为在不同端口中侦听,并在非默认目录中提供内容。要防止 SELinux 拒绝带来的后果,请按照以下步骤调整系统的 SELinux 策略。

先决条件

  • 已安装 httpd 软件包,并将 Apache HTTP 服务器配置为侦听 TCP 端口 3131,并使用 /var/test_www/ 目录而不是默认的 /var/www/ 目录。
  • policycoreutils-python-utilssetroubleshoot-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. 更改 SELinux 类型端口 3131 使其与端口 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/www/html 目录的类型:

    # 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’
    ...

其他资源

  • The semanage(8), matchpathcon(8), 和 sealert(8) man pages.