Selinux blocks the crontab command from php

独自空忆成欢 提交于 2021-01-28 03:27:47

问题


There are Fedora 25 and apache on our server.
I want to do so that the php script on our web site can change crontab settings.

I created the following test php script:

<?php
system("echo '*/2 * * * * date > /var/www/logs/testlog.txt' | crontab - 2>&1");

But it did not work. I got the message:

/var/spool/cron/#tmp.mh203-95.XXXXG0KrFF: Permission denied

I looked at output of sealert -a /var/log/audit/audit.log and found:

SELinux is preventing crontab from write access on the directory /var/spool/cron.

Okay. It sounds like apache is not allowed the write access to /var/spool/cron because that directory has not the httpd_sys_rw_content_t label. So I executed the command: chcon -v -R -t httpd_sys_rw_content_t /var/spool/cron

My php script begun to work. The crontab -l command gave normal output.
But the new problem appeared. :( The cron tasks was not executed.

In the /var/log/cron I saw the error:

Mar 23 18:05:01 mh203-95 crond[1653]: (apache) Unauthorized SELinux context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 file_context=system_u:object_r:httpd_sys_rw_content_t:s0 (/var/spool/cron/apache)
Mar 23 18:05:01 mh203-95 crond[1653]: (apache) FAILED (loading cron table)

After many time of research... I found that the /var/spool/cron must have the user_cron_spool_t label. So I executed: chcon -v -R -t user_cron_spool_t /var/spool/cron.

The cron tasks begun to works. But my php script did not work again. The same problem as at the beginning.

sealert suggested the commands like:
ausearch -c 'crontab' --raw | audit2allow -M my-crontab
semodule -X 300 -i my-crontab.pp
But it did not help.

What am I missing? How to solve the problem? Can I somehow combine two labels user_cron_spool_t and httpd_sys_rw_content_t for /var/spool/cron directory?


回答1:


I had solved the problem.

The reason was in this: sealert generates the same politic name my-crontab in all suggested commands. The new politic overwrote the old.
It is just needed to change this name slightly.

So i executed:

ausearch -c 'crontab' --raw | audit2allow -M my-crontab
semodule -X 300 -i my-crontab.pp

ausearch -c 'crontab' --raw | audit2allow -M my-crontab2
semodule -X 300 -i my-crontab2.pp

ausearch -c 'crontab' --raw | audit2allow -M my-crontab3
semodule -X 300 -i my-crontab3.pp
...

Before every ausearch ... I executed:
echo -n "" > /var/log/audit/audit.log
My php script.
sealert -a /var/log/audit/audit.log



来源:https://stackoverflow.com/questions/42984667/selinux-blocks-the-crontab-command-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!