大网数据广告活动
服务器运维问答

Why does the -bs switch on sendmail cause a SELinux denial from within a PHP app?

· 52CE 我爱测

On a CentOS 7.5 server (LAMP, PHP-FPM) with SELinux enabled, I've encountered a strange issue with a Laraval 5.3 application when it tries to send email. The application's mail config is set to use /usr/sbin/sendmail -bs and when…

问题原文

原帖链接 · jamieburchell · CC BY-SA 4.0

On a CentOS 7.5 server (LAMP, PHP-FPM) with SELinux enabled, I've encountered a strange issue with a Laraval 5.3 application when it tries to send email.

The application's mail config is set to use /usr/sbin/sendmail -bs and when attempting to send an email the application throws a Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""' in vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383

Suspecting SELinux, I dug deeper and audit2why shows:

type=AVC msg=audit(1553606152.177:1240981): avc: denied { execute } for pid=3896 comm="sendmail" name="smtpd" dev="vda1" ino=12904557 scontext=system_u:system_r:system_mail_t:s0 tcontext=system_u:object_r:postfix_smtpd_exec_t:s0 tclass=file

If I disable SELinux, the issue is resolved. If I use the mail driver instead, which I believe instructs SwiftMailer to use PHP's mail() function (and subsequently the sendmail bin with only -t -i switches as per the default php.ini config) it works. If I use the SMTP driver it works and if I change the Laraval mail config sendmail path to /usr/sbin/sendmail -t -i it works.

Why does SELinux complain when using the -bs switch on sendmail?

According to man sendmail:

-bs Stand-alone SMTP server mode. Read SMTP commands from standard input, and write responses to standard output.  In stand-alone SMTP server mode, mail relaying and other access controls are disabled by default. To enable them, run the process as the mail_owner user.

If one were to go ahead and use that configuration (I'm guessing it's there for a reason), is it really expected to compile and install an SELinux module to allow the behaviour? It seems like something that should Just WorkTM.

Bonus question: I was under the impression that the sebool httpd_can_sendmail needed to be 1 for PHP to be able to use mail. Certainly I've found that to be the case on other projects. This flag seems to have no affect whatsoever when using any of the above mail drivers, in that they all still work without it being on (apart from the -bs scenario above). Why?

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

Generally, (web) apps should not be exec-ing smtpd. That's better managed and secured on a central SMTP server. SELinux succeeded at restricting sendmail to be only a mail submission agent.

The alternative sendmail invocation queues mail without starting smtpd, which is allowed under this policy.

Booleans had no effect because this wasn't a transition from httpd context. Note the denial was system_mail_t to postfix_smtpd_exec_t. The default policy doesn't allow sendmail to exec smtpd at all.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。