SELinux 阻止 php 的 exec('kill pid') 而日志中没有任何错误

SELinux blocking php's exec('kill pid') without any error in log

我正在尝试获取进程 PID 并使用以下代码将其终止:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$_script_path = "/path/to/scriptname.php";

$cmd_find_process = "ps aux | grep '[p]hp -f ".$_script_path."'";

echo $cmd_find_process.PHP_EOL;
echo exec($cmd_find_process);
echo PHP_EOL.PHP_EOL;

$cmd = "kill $(".$cmd_find_process." | awk '{print }')";
echo $cmd;
echo exec($cmd);
?>

最初我无法列出进程,我通过编译自定义 SELinux 模块修复了这个问题,selinux-httpd-allow-ps-aux.te :

policy_module(myhttpd,1.0.0)

gen_require(`
    type httpd_t;
')

domain_read_all_domains_state(httpd_t);

我已经禁用了 dontaudit 语句:

semodule -DB

但我无法终止我之前由同一用户启动的任何进程:apache。 /var/log/audit/audit.log 文件中没有记录错误。

为了完整理解,我试图杀死的 PHP 脚本是使用以下命令执行的:

su -s /bin/sh apache -c php -f /path/to/scriptname.php

我知道是 SELinux 因为用

关闭了 SELinux
echo 0 > /selinux/enforce

会成功的。

显然我必须重新启动 auditd 才能显示错误。

service auditd restart

这是错误:

type=AVC msg=audit(1459790992.546:15889813): avc:  denied  { signal } for  pid=25478 comm="sh" scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:system_r:initrc_t:s0 tclass=process
    Was caused by:
        Missing type enforcement (TE) allow rule.

        You can use audit2allow to generate a loadable module to allow this access.

我能够通过 audit2allow 工具解决问题。这是解决问题的生成的自定义模块。

module selinux-httpd-allow-signal 1.0;

require {
        type httpd_t;
        type initrc_t;
        class process signal;
}

#============= httpd_t ==============
allow httpd_t initrc_t:process signal;