On a Fedora 18 machine, I have the following SELinux context as a regular user created during installation:
$ sestatus
SELinux status: enabled
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
and
$ id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
I want to run a program with the confined selinux role user_r, instead of my current unconfined_r, so I create a new user with the confined SELinux user user_r:
# useradd -MN -Z user_u johndoe
# echo 'fubar' | passwd johndoe --stdin
Now, I open up a new tty and login as johndoe using agetty. It complains that /home/johndoe does not exist, but essentially, it logs me in. SELinux context:
$ id -Z
user_u:user_r:user_t:s0
user_u:user_r:user_t is perfect and what I want. But I would like to achieve this in a script already running as an unconfined_u and not as a fresh login. I tried with sudo:
If I use sudo -u, I get:
$ sudo -u johndoe id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Which means that the SELinux user doesn't change.
I also tried:
$ sudo -r user_r -u johndoe id -Z
sudo: unconfined_u:user_r:user_t:s0-s0:c0.c1023 is not a valid context
sudo: unable to execute /bin/id: Invalid argument
So, no luck there either.
So how can I transition from the unconfined_r role to user_r, using sudo or su (or anything else)?