このセクションでは、SELinux を有効にした状態で Redmine を使えるようにすることについて紹介していきます。

特に何もしていないと、SELinux が有効な状態では Redmine 環境が下記エラーとなり利用できません。

対策その1

SELinux で httpdによるファイルへの書き込み許可が on になっているかどうか確認します。

[user@pub-web ~]$ sudo getsebool -a | grep httpd_unified
httpd_unified --> off
[user@pub-web ~]$

httpdによるファイルへの書き込み許可が off の場合は、on に変更します。

[user@pub-web ~]$ sudo setsebool -P httpd_unified on
[user@pub-web ~]$

[user@pub-web ~]$ sudo getsebool -a | grep httpd_unified
httpd_unified --> on
[user@pub-web ~]$

Redmine のルートフォルダの SELinux コンテキストを確認します。httpd_t というラベル付けがされたファイルから読み出される権限がない場合には、その権限を次で適用していきます。

[user@pub-web ~]$ sudo ls -Z /var/lib/redmine/
unconfined_u:object_r:user_home_t:s0 CONTRIBUTING.md
unconfined_u:object_r:user_home_t:s0 Gemfile
unconfined_u:object_r:user_home_t:s0 Gemfile.local
unconfined_u:object_r:user_home_t:s0 Gemfile.lock
unconfined_u:object_r:user_home_t:s0 README.rdoc
unconfined_u:object_r:user_home_t:s0 Rakefile
unconfined_u:object_r:user_home_t:s0 app
unconfined_u:object_r:user_home_t:s0 appveyor.yml
unconfined_u:object_r:user_home_t:s0 bin
unconfined_u:object_r:user_home_t:s0 config
unconfined_u:object_r:user_home_t:s0 config.ru
unconfined_u:object_r:user_home_t:s0 db
unconfined_u:object_r:user_home_t:s0 doc
unconfined_u:object_r:user_home_t:s0 extra
unconfined_u:object_r:user_home_t:s0 files
unconfined_u:object_r:user_home_t:s0 lib
unconfined_u:object_r:user_home_t:s0 log
unconfined_u:object_r:user_home_t:s0 package.json
unconfined_u:object_r:user_home_t:s0 plugins
unconfined_u:object_r:user_home_t:s0 public
unconfined_u:object_r:user_home_t:s0 test
unconfined_u:object_r:user_home_t:s0 tmp
unconfined_u:object_r:user_home_t:s0 vendor
unconfined_u:object_r:user_home_t:s0 yarn.lock
[user@pub-web ~]$

Redmine のルートフォルダに対して httpd_t というラベル付けがされたファイルから読み出される権限を適用していきます。

[user@pub-web ~]$ sudo chcon -R -t httpd_sys_content_t /var/lib/redmine/
[user@pub-web ~]$

[user@pub-web ~]$ sudo ls -Z /var/lib/redmine/
unconfined_u:object_r:httpd_sys_content_t:s0 CONTRIBUTING.md
unconfined_u:object_r:httpd_sys_content_t:s0 Gemfile
unconfined_u:object_r:httpd_sys_content_t:s0 Gemfile.local
unconfined_u:object_r:httpd_sys_content_t:s0 Gemfile.lock
unconfined_u:object_r:httpd_sys_content_t:s0 README.rdoc
unconfined_u:object_r:httpd_sys_content_t:s0 Rakefile
unconfined_u:object_r:httpd_sys_content_t:s0 app
unconfined_u:object_r:httpd_sys_content_t:s0 appveyor.yml
unconfined_u:object_r:httpd_sys_content_t:s0 bin
unconfined_u:object_r:httpd_sys_content_t:s0 config
unconfined_u:object_r:httpd_sys_content_t:s0 config.ru
unconfined_u:object_r:httpd_sys_content_t:s0 db
unconfined_u:object_r:httpd_sys_content_t:s0 doc
unconfined_u:object_r:httpd_sys_content_t:s0 extra
unconfined_u:object_r:httpd_sys_content_t:s0 files
unconfined_u:object_r:httpd_sys_content_t:s0 lib
unconfined_u:object_r:httpd_sys_content_t:s0 log
unconfined_u:object_r:httpd_sys_content_t:s0 package.json
unconfined_u:object_r:httpd_sys_content_t:s0 plugins
unconfined_u:object_r:httpd_sys_content_t:s0 public
unconfined_u:object_r:httpd_sys_content_t:s0 test
unconfined_u:object_r:httpd_sys_content_t:s0 tmp
unconfined_u:object_r:httpd_sys_content_t:s0 vendor
unconfined_u:object_r:httpd_sys_content_t:s0 yarn.lock
[user@pub-web ~]$

対策その1を実施した後に、Redmine のWebページが開くかどうか確認します。開けば対策その1で終わりとなりますが、それでも開けない場合には次の対策その2を実行します。

対策その2

対策その1でも解決できない場合は、SETroubleShoot を使って原因とその対処方法を特定していきます。SETroubleShoot がインストールされていない場合には、SETroubleShoot をインストールしてください。

監査システムのログ(audit.log)で 処理が拒否されていないかどうか確認します。私の環境では、Nginx と unicornソケット間での書き込みを拒否しているようなログが出力されていました。おそらく、書き込みが拒否されていることが原因と思われる結果です。

[user@pub-web ~]$ sudo ls -la /var/log/audit/audit.log
--(省略)--
type=AVC msg=audit(1693455808.897:14696): avc:  denied  { write } for  pid=82872 comm="nginx" name="unicorn.sock" dev="tmpfs" ino=9198 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:var_run_t:s0 tclass=sock_file permissive=0

次に、messages ログで SELinux で拒否された原因と問題解決の方法を確認します。
SETroubleShoot を使用すると、SELinux で拒否された原因と問題解決の方法を messages ログに出力してくれるため、SELinux に関わるトラブルシューティングの参考となります。

ログを確認した結果、/usr/sbin/nginx から sock_file unicorn.sock への書き込みアクセスが拒否されてしまっていることが原因のようでした。解決方法は、For complete SELinux messages run: に記載されているので、そのコマンドを次に実行していきます。

[user@pub-web ~]$ sudo tail -F /var/log/messages
--(省略)--
Aug 31 00:24:19 pub-web setroubleshoot[83941]: SELinux is preventing /usr/sbin/nginx from write access on the sock_file unicorn.sock. For complete SELinux messages run: sealert -l fb3e06d8-74b4-4a55-9dbe-982e10f98d70
Aug 31 00:24:19 pub-web setroubleshoot[83941]: SELinux is preventing /usr/sbin/nginx from write access on the sock_file unicorn.sock.#012#012*****  Plugin catchall (100. confidence) suggests   **************************#012#012If you believe that nginx should be allowed write access on the unicorn.sock sock_file by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'nginx' --raw | audit2allow -M my-nginx#012# semodule -X 300 -i my-nginx.pp#012

sealert -l … コマンドを実行すると詳細情報を確認できます。
allow this access for now by executing: にアクセスを許可するためのコマンドが記載されているので、そのコマンドを実行し拒否されているアクセスを許可していきます。

[user@pub-web ~]$ sudo sealert -l fb3e06d8-74b4-4a55-9dbe-982e10f98d70
SELinux is preventing /usr/sbin/nginx from write access on the sock_file unicorn.sock.

*****  Plugin catchall (100. confidence) suggests   **************************

If you believe that nginx should be allowed write access on the unicorn.sock sock_file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'nginx' --raw | audit2allow -M my-nginx
# semodule -X 300 -i my-nginx.pp


Additional Information:
Source Context                system_u:system_r:httpd_t:s0
Target Context                system_u:object_r:var_run_t:s0
Target Objects                unicorn.sock [ sock_file ]
Source                        nginx
Source Path                   /usr/sbin/nginx
Port                          <Unknown>
Host                          pub-web
Source RPM Packages           nginx-1.24.0-1.el9.ngx.x86_64
Target RPM Packages
SELinux Policy RPM            selinux-policy-targeted-38.1.11-2.el9_2.3.noarch
Local Policy RPM              selinux-policy-targeted-38.1.11-2.el9_2.3.noarch
Selinux Enabled               True
Policy Type                   targeted
Enforcing Mode                Enforcing
Host Name                     pub-web
Platform                      Linux pub-web 5.14.0-284.25.1.el9_2.x86_64 #1 SMP
                              PREEMPT_DYNAMIC Tue Aug 8 04:07:27 EDT 2023 x86_64
                              x86_64
Alert Count                   5
First Seen                    2023-08-31 00:22:02 EDT
Last Seen                     2023-08-31 00:26:07 EDT
Local ID                      fb3e06d8-74b4-4a55-9dbe-982e10f98d70

Raw Audit Messages
type=AVC msg=audit(1693455967.546:14714): avc:  denied  { write } for  pid=82872 comm="nginx" name="unicorn.sock" dev="tmpfs" ino=9198 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:var_run_t:s0 tclass=sock_file permissive=0


type=SYSCALL msg=audit(1693455967.546:14714): arch=x86_64 syscall=connect success=no exit=EACCES a0=9 a1=55b1040edc98 a2=6e a3=7fffc8a6dc7c items=0 ppid=82735 pid=82872 auid=4294967295 uid=991 gid=991 euid=991 suid=991 fsuid=991 egid=991 sgid=991 fsgid=991 tty=(none) ses=4294967295 comm=nginx exe=/usr/sbin/nginx subj=system_u:system_r:httpd_t:s0 key=(null)

Hash: nginx,httpd_t,var_run_t,sock_file,write

[user@pub-web ~]$

アクセスを許可するためのコマンドを実行します。

[user@pub-web ~]$ sudo ausearch -c 'nginx' --raw | audit2allow -M my-nginx
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i my-nginx.pp

[user@pub-web ~]$

[user@pub-web ~]$ sudo semodule -X 300 -i my-nginx.pp
[user@pub-web ~]$

対策その2実行後に、もう一度Redmine のWebページにアクセスし開くかどうか確認します。正常であればこの時点で SELinux が有効な状態でも Redmine が開けるようになります。