RHEL SELinux 阻止 Apache 访问 PostgreSQL

Geo*_*off 5 postgresql redhat selinux django apache-2.2

我正在运行一个使用 PostgreSQL 的 Django 应用程序。服务器正在运行带有 SELinux 的 RHEL 6.5。我遇到了 Django 应用程序无法连接到数据库的问题,我认为这是因为 SELinux 阻止了它。这是我在 Django 中看到的错误:

could not connect to server: Permission denied Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

我怎样才能解决这个问题?我遇到了这篇文章,但我不确定如何将解决方案 ( chcon -t postgresql_exec_t /path/to/pgbouncer) 应用于我的问题。

谢谢!

[编辑]
这是/var/log/audit/audit.log我尝试访问该站点时的样子:

type=AVC msg=audit(1396289984.549:9245): avc:  denied  { write } for  pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1396289984.549:9245): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625273aa0 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1396289984.756:9246): avc:  denied  { write } for  pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1396289984.756:9246): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe624d87890 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1396289984.757:9247): avc:  denied  { write } for  pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1396289984.757:9247): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625342c20 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1396289984.758:9248): avc:  denied  { write } for  pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1396289984.758:9248): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625603ac0 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)
Run Code Online (Sandbox Code Playgroud)

[编辑2]

以下是我启用的一些相关 SELinux 选项。

-bash-4.1$ sudo getsebool -a | grep httpd_can_network_connect_db httpd_can_network_connect_db --> on -bash-4.1$ sudo getsebool -a | grep allow_user_postgresql_connect allow_user_postgresql_connect --> on

JJC*_*JJC 10

对于未来的读者,对我来说,只需设置 bool 以允许 httpd 建立数据库连接就足够了;IE:

setsebool -P httpd_can_network_connect_db 1
Run Code Online (Sandbox Code Playgroud)

您可以检查/验证设置是否通过以下方式设置:

getsebool httpd_can_network_connect_db
Run Code Online (Sandbox Code Playgroud)

应该返回'... => on'

之后,如果您 tail -f /var/log/audit/audit.log 并重新尝试您的操作,它应该可以工作。


Geo*_*off 1

好的,在系统管理员的帮助下,问题现已解决。事实证明,分配给二进制文件的 SELinux 上下文/usr/pgsql-9.3/bin是错误的。解决这个问题所需要的只是chcon -t postgresql_exec_t /usr/pgsql-9.3/bin。要更改符号链接的上下文,只需添加-h.