SELinux에서 "권한 거부" 오류가 발생하여 PHP가 PostgreSQL에 연결할 수 없습니다.

SELinux에서 "권한 거부" 오류가 발생하여 PHP가 PostgreSQL에 연결할 수 없습니다.

내 PHP 앱은 SELinux를 끄면 연결할 수 있지만 SELinux를 켠 상태에서는 연결할 수 없습니다.

setenforce 0; curl -I http://domain.com; setenforce 1

에서는 오류가 발생하지 않습니다 /var/log/httpd/error_log. 그러나 이 기능을 사용하면 다음 오류가 발생합니다.

PHP 경고: pg_connect(): PostgreSQL 서버에 연결할 수 없습니다. 서버에 연결할 수 없습니다. 권한이 거부되었습니다. 서버가 로컬로 실행되고 Unix 도메인 소켓 "/tmp/.s.PGSQL.5432"에서 연결을 허용합니까?

난 노력 했어

# restorecon -R -v /home/domain/public_html
#  chcon -R -t httpd_sys_rw_content_t /home/domain/public_html/
#  chcon -v --type=httpd_sys_content_t /home/domain/public_html
# semanage fcontext -a -t httpd_sys_content_t "/home/domain/public_html(/.*)?"
# service httpd restart

SELinux를 켜면 다음과 같은 작업을 수행할 수 있습니다.

# php -a
Interactive shell

php > $connection = pg_connect ("dbname=domain user=domain password=xxxxxx") or die(pg_last_error());
php > echo $connection;
Resource id #1

다음은 오류입니다 /var/log/audit/audit.log.

type=AVC msg=audit(1404684735.513:97245): avc:  denied  { write } for  pid=3594 comm="httpd" name=".s.PGSQL.5432" dev=xvde ino=2552 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1404684735.513:97245): arch=c000003e syscall=42 success=no exit=-13 a0=b a1=7f40ae4fd640 a2=6e a3=0 items=0 ppid=26231 pid=3594 auid=0 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=2700 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)

센트OS 6.5

답변1

httpd가 다음을 통해 DB 연결을 만들 수 있도록 허용하는 SELinux 정책 부울을 설정해 보셨습니까?

setsebool -P httpd_can_network_connect_db 1

이것은 나에게 효과적이었습니다. 다음을 통해 설정이 지정되었는지 확인/검증할 수 있습니다.

getsebool httpd_can_network_connect_db

'... => on'을 반환해야 합니다.

그런 다음 tail -f /var/log/audit/audit.log 작업을 다시 시도하면 제대로 작동합니다.

답변2

더 많은 정보가 필요합니다:

avc 거부에 대해서는 /var/log/audit/audit.log를 확인해야 합니다.

또는 다음이 설치되어 있는지 확인하십시오.

  1. 문제 해결 서버 설정
  2. 감사
  3. 메시지 버스

메시지는 /var/log/messages에 나타납니다.

추가 참조:http://danwalsh.livejournal.com/7995.html

PHP 대화형 셸이 작동하는 이유는 애플리케이션이 Apache 컨텍스트로 실행되는 사용자 컨텍스트에서 실행되기 때문입니다.

답변3

내가 고쳤어! 여기서 해결책을 찾았습니다.https://bugzilla.redhat.com/show_bug.cgi?id=772084#c8

내가 한 일은 audit.log에 기록된 두 줄을 가져와서 에 파이프하는 것이었습니다. audit2allow그러면 바이너리와 텍스트라는 두 개의 파일이 생성됩니다. 그런 다음 해당 파일을 semodule. 그런데 파일이 이해가 안 되네요. httpd 오류를 캡처했는지 확인하세요.

tail -2 /var/log/audit/audit.log | audit2allow -M mypol
semodule -i mypol.pp # takes a while

생성된 실제 텍스트 파일은 다음과 같습니다.mypol.te

module mypol 1.0;

require {
        type httpd_t;
        type initrc_t;
        class unix_stream_socket connectto;
}

#============= httpd_t ==============
allow httpd_t initrc_t:unix_stream_socket connectto;

관련 정보