Just to add on top of the good answer by @JeffSchaller, with regard to the specific question:
how can I simulate it running from the cronjob to test?
I can share a workable alternative to having to run test commands from a cronjob.
To start, it may be worth mentioning a subtle detail:
In a traditional non-SELinux environment, when you operate as root (effective UID = 0) you are normally free to switch to whatever other UID you like, to "gain" that UID's restrictions.
That is quite unlike SELinux's contexts: being root (effective UID = 0) typically does place you in "unconfined" context, but does not automatically allow you to switch to other contexts freely. You rather need an explicit SELinux policy rule that grants the "unconfined" context the clearance to perform that specific "transition" operation. After you have installed such a rule, the runcon
command will succeed in putting you in that context.
In practical terms, for your case you probably need a SELinux policy like a simple:
allow unconfined_t crond_t:process transition;
(assuming crond_t
is the SELinux domain your crond
daemon runs under)
Such rule as a full module to be compiled and installed is:
module unconfined-trans-crond 1.0;
require {
type unconfined_t;
type crond_t;
class process transition;
}
allow unconfined_t crond_t:process transition;
After having compiled and installed that module, a runcon
targeting the crond_t
domain from unconfined_t
would work. For instance:
# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# runcon $(ps -q $(pgrep crond) -o context --no-header) sh
# id
uid=0(root) gid=0(root) groups=0(root) context=system_u:system_r:crond_t:s0
#
Note also that you are not required to be UID 0 to use runcon
, you "only" need the correct policy rule that allows the transition from your current SELinux context to the target SELinux context.
In fact, the full command I would normally run as root to test a daemon's (such as crond
) reach is:
runuser $(ps -q $(pgrep crond) -o user --no-header) -c 'runcon $(ps -q $(pgrep crond) -o context --no-header) /bin/bash'
which gives a shell running with crond
's user as well as its SELinux context.
To undo that policy once you've finished your testing just remove the custom module.
This of course does not take into account possible other restrictions such as different namespaces views, resource limits (ulimits), cgroups, or Linux Capabilities. These would require additional commands to be run in sequence in order to recreate these additional restrictions, unless the PAM configuration for runuser
is in some way consistent at re-creating them for you.
HTH