Getting "sudo: unconfined_u:sysadm_r:sysadm_t:s0-s0:c0.c1023 is not a valid context" error message when sudo'ing
Environment
- Red Hat Enterprise Linux
- SELinux
Issue
-
After adding the following configuration line for users in wheel group, sudo'ing doesn't work anymore
# grep ^%wheel /etc/sudoers %wheel ALL=(ALL) TYPE=sysadm_t ROLE=sysadm_r ALLAs a user in wheel group:
$ sudo -i sudo: unconfined_u:sysadm_r:sysadm_t:s0-s0:c0.c1023 is not a valid context
Resolution
Follow the procedure in the Diagnostic Steps section; if this is a match, proceed further.
This is expected behaviour when the user sudo'ing is mapped to unconfined_u SELinux user.
Having the user mapped to unconfined_u SELinux user is not expected, usually the user has to be mapped to staff_u or sysadm_u SELinux users.
-
Collect the login mapping
# yum -y install policycoreutils-python-utils # semanage login -l Login Name SELinux User MLS/MCS Range Service __default__ unconfined_u s0-s0:c0.c1023 * root unconfined_u s0-s0:c0.c1023 * staff staff_u s0-s0:c0.c1023 * sysadm sysadm_u s0-s0:c0.c1023 * system_u system_u s0-s0:c0.c1023 *In the example above, the staff login is mapped to
staff_uSELinux user, so is sysadm login respectively mapped tosysadm_uSELinux user.
However all other logins (including admin used in our example) is mapped tounconfined_u(see the default entry above), which is not correct. -
Change the mapping for default logins to some confined user
# semanage login -m -s staff_u __default__In the example above, we map default logins to
staff_uSELinux user, which enables sudo'ing (hence may not be wise).Usually to restrict the usage of sudo command to "power users" in wheel group, the "power users" should be mapped one by one or by group, as shown in the example below where our admin login is mapped to
staff_u, all users in wheel group are mapped tostaff_uas well and the normal users are mapped touser_u:# semanage login -m -s user_u __default__ # semanage login -a -s staff_u admin # semanage login -a -s staff_u %wheel -
Log in as the user that needs to sudo and verify its context
$ id uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel) context=staff_u:staff_r:staff_t:s0-s0:c0.c1023 -
Verify that the user can now sudo
$ sudo -i # id -Z staff_u:sysadm_r:sysadm_t:s0-s0:c0.c1023
Root Cause
It's a configuration error to have unconfined users when such sudo rule is applied.
Diagnostic Steps
-
As the user trying to sudo, get the login attributes
$ id uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023In the example above, the admin user is part of the wheel group and is executing in
unconfined_u:unconfined_r:unconfined_tcontext. -
As root, check the sudo configuration for automatic role and type switching for the specific user of wheel group
# egrep "^(admin|%wheel)" /etc/sudoers /etc/sudoers.d/* /etc/sudoers:%wheel ALL=(ALL) TYPE=sysadm_t ROLE=sysadm_r ALLIn the example above, there is a rule to automatically switch to
sysadm_rrole andsysadm_ttype for all users in wheel group.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments