SELinux 导致 PHP 无法使用 fsockopen 连接到 Memcached 服务器

前段时间刚刚写了一篇关于 SELinux 导致 httpd(Apache2) 无法启动 的文章,今天又碰到 SELinux 的问题了。

事情是这样的:

首先是服务器硬盘出问题了:-(,我给换了块硬盘,然后重装系统(CentOS 5.4 i386),然后安装各种程序、还原各种数据。最后一步是使用 memcache.php 来监控 Memcache 状态。然而却发现该工具无法连接上 Memcached 服务器。经检查,Memcached 服务器已经正常启动,使用 telnet 能够正常连接上去,使用 Memcached 的应用程序(PHP程序)也正常工作。查看 memcache.php 代码发现其是使用 fsockopen 来连接 Memcached 服务器,遂怀疑 Socket 扩展的问题。然而,检查发现可以在命令行中使用 fsockopen 连接到任意地址的任意端口,说明 Socket 扩展没问题。但在 httpd 中使用 fsockopen 来就只能连接本机的 80、8080、443 端口,连接其他端口均失败。

检查 httpd 的 log 也没发现任何问题。上网搜索也没发现类似问题,郁闷ing……

于是又想到是否是 SELinux 的问题。grep 了下 /var/log/audit/audit.log,发现以下线索:

[liang@www ~]$ sudo grep denied /var/log/audit/audit.log

type=AVC msg=audit(1280882021.681:780): avc:  denied  { name_connect } for  pid=3822 comm="httpd" dest=11211 scontext=user_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket 
type=AVC msg=audit(1280885410.800:805): avc:  denied  { name_connect } for  pid=3790 comm="httpd" dest=11211 scontext=user_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket

看来又是 SELinux 搞的鬼。继续检查,发现 /var/log/messages 有以下错误信息:

  1. 4 08:11:59 www setroubleshoot: SELinux is preventing the http daemon from connecting to the itself or the relay ports For complete SELinux messages. run sealert -l 23d1381f-9d4b-439a-9ad6-d52f1025f247

果然是 SELinux 引起的问题。根据提示继续检查:

[liang@www ~]$ sealert -l 23d1381f-9d4b-439a-9ad6-d52f1025f247

Summary:

SELinux is preventing the http daemon from connecting to the itself or the relay 
ports

Detailed Description:

SELinux has denied the http daemon from connecting to itself or the relay ports. 
An httpd script is trying to do a network connect to an http/ftp port. If you 
did not setup httpd to network connections, this could signal a intrusion 
attempt.

Allowing Access:

If you want httpd to connect to httpd/ftp ports you need to turn on the 
httpd_can_network_relay boolean: "setsebool -P httpd_can_network_relay=1"

The following command will allow this access:

setsebool -P httpd_can_network_relay=1

Additional Information:

Source Context                user_u:system_r:httpd_t 
Target Context                system_u:object_r:http_cache_port_t 
Target Objects                None [ tcp_socket ]

———————省略若干输出———————

错误信息说得很明了了:SELinux 阻止了 httpd 的连接。修改方式也给出来了,以 root 身份运行以下命令即可:

[liang@www ~]$ sudo /usr/sbin/setsebool -P httpd_can_network_relay=1

注意该命令成功运行后没有任何输出。要检查是否设置成功,可以查看运行 getsebool 命令或者直接查看 log:

[liang@www ~]$ /usr/sbin/getsebool httpd_can_network_relay 
httpd_can_network_relay –> on

[liang@www ~]$ sudo tail /var/log/messages

  1. 4 10:50:23 www setsebool: The httpd_can_network_relay policy boolean was changed to 1 by root

设置成功了。重新刷新下 memcache.php, 发现已经能够正常工作了。job done!

此文纯粹是工作备忘。但希望也能给碰到同样问题的朋友一点帮助。


( ! ) Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 169
Call Stack
#TimeMemoryFunctionLocation
10.0015412728{main}( ).../index.php:0
20.09224263128Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.09224263128Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.384511454304Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.385211478936Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.386111534264Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.386411561664require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.396311961296K2ControllerItem->execute( ).../k2.php:64
90.396311961296K2ControllerItem->display( ).../BaseController.php:710
100.406812612000K2ControllerItem->display( ).../item.php:78
110.406812612000K2ControllerItem->display( ).../controller.php:19
120.411012983040Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.413813003408K2ViewItem->display( ).../ViewController.php:102
140.527115876712K2ViewItem->display( ).../view.html.php:742
150.527115876712K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.529816049648include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701

( ! ) Notice: Only variables should be assigned by reference in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 478
Call Stack
#TimeMemoryFunctionLocation
10.0015412728{main}( ).../index.php:0
20.09224263128Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.09224263128Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.384511454304Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.385211478936Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.386111534264Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.386411561664require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.396311961296K2ControllerItem->execute( ).../k2.php:64
90.396311961296K2ControllerItem->display( ).../BaseController.php:710
100.406812612000K2ControllerItem->display( ).../item.php:78
110.406812612000K2ControllerItem->display( ).../controller.php:19
120.411012983040Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.413813003408K2ViewItem->display( ).../ViewController.php:102
140.527115876712K2ViewItem->display( ).../view.html.php:742
150.527115876712K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.529816049648include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
back to top