SELinux – Why Do SELinux Policies Apply to Commands from Cronjobs but Not from Command Line?

logrotateselinux

Why do selinux policies apply to commands (e.g: logrotate) running from cronjobs, but not when run directly from command line?

When I run logrotate manually from the command line, it works perfectly. But when it runs from the cronjob, I get an error in the audit.log alerting me that selinux prevented access to www, etc.

Why is that? And how can I simulate it running from the cronjob to test?

Best Answer

When cron runs logrotate, SELinux confines it to a logrotate_t "type". That "type" is restricted from modifying other file types (aka "escaping the confinement").

When you run logrotate, you're (most likely) starting from an "unconfined" type, which means what it says -- the logrotate process is permitted to modify files. You might also want logrotate to restart or signal processes (via postrotate, for example); that activity may also be confined by SELinux.

My suggestion here is to tell SELinux to allow ("permit") the logrotate_t type to escape the confinement, with:

semanage permissive -a logrotate_t

Doing so is a moderate solution, in-between turning SELinux off and fine-tuning a policy that allows exactly the confinement escapes that you need (perhaps with custom labeling). To revert this change, use semanage permissive -d logrotate_t.

The best way to simulate a cron-initiated process is to put the jobs into cron. Alternatively, I'm aware of runcon, although I wasn't able to use it successfully.

Related Question