[LINUX] DB access fails on PHP webpage, but CLI succecess on rhel (by selinux)

 

증상


PHP스크립트 내에 DB(mysql) 커넥션을 수행시 오류 발생

터미널 상에서 CLI를 이용한 실행 : OK
웹페이지(웹서버)를 통한 실행 : FAIL

CLI실행시 문제가 없음 → PHP 코드 내의 “DB접속정보 (host/id/pass/dbname)”는 오류가 없음

 

 

 

원인 및 문제 여부 확인


SELinux 정책에 의한 문제
기본적으로 제한되어 있는것으로 보여짐

결과적으로 문제는 발생하였으나, 이것이 httpd, php 측에서 발생한 에러가 아니다보니
웹서버 및 브라우저 측면에서는 정확하거나 신속하게 확인이 되지 않음
때문에 “감사시스템로그(audit.log)” 에서 확인해야 함

1. 로그

file : /var/log/audit/audit.log


# cat audit.log
type=AVC msg=audit(1725337229.836:16002687): avc: denied { name_connect } for pid=1549 comm="httpd" dest=3306 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:mysqld_port_t:s0 tclass=tcp_socket
type=SYSCALL msg=audit(1725337229.836:16002687): arch=c000003e syscall=42 success=no exit=-13 a0=f a1=7fe352b59090 a2=10 a3=40 items=0 ppid=1547 pid=1549 auid=0 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=992527 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)

 

 

2. 로그확인

exec : audit2why


# audit2why < audit.log
type=AVC msg=audit(1725337229.836:16002687): avc: denied { name_connect } for pid=1549 comm="httpd" dest=3306 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:mysqld_port_t:s0 tclass=tcp_socket

Was caused by:
One of the following booleans was set incorrectly.
Description:
Allow HTTPD scripts and modules to connect to the network using TCP.

Allow access by executing:
# setsebool -P httpd_can_network_connect 1
Description:
Allow HTTPD scripts and modules to connect to databases over the network.

Allow access by executing:
# setsebool -P httpd_can_network_connect_db 1

 

 

 

해결


 

1. SELinux 의 정책 설정 변경

exec : setsebool

  • setsebool -P httpd_can_network_connect 1
  • setsebool -P httpd_can_network_connect_db 1

 

httpd_can_network_connect : HTTP 스크립트 및 모듈의 네트워크 또는 원격 포트에 대한 연결 허용 여부
httpd_can_network_connect_db : HTTP 스크립트 및 모듈의 DB서버에 대한 연결 허용 여부

httpd_can_network_connect 항목을 활성화 해주면
httpd_can_network_connect_db 항목이 “off” 상태로 되어 있어도
DB커넥션은 정상적으로 수행됨

 

 

2. 처리 내용 확인

exec : sestatus

  • -v Verbose check of process and file contexts.
  • -b Display the current state of booleans.

# sestatus -b|grep httpd_can_network
httpd_can_network_connect on
httpd_can_network_connect_cobbler off
httpd_can_network_connect_db off
httpd_can_network_memcache off
httpd_can_network_relay off

 

 

About KENNETH 19693 Articles
지락문화예술공작단

Be the first to comment

Leave a Reply

Your email address will not be published.


*


이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.