Bookmark this page

Guided Exercise: Controlling Access with Confined Users

In this exercise, you will configure users as SELinux confined users to limit the mechanisms they may use to access the system and the superuser account.

Outcomes

You should be able to:

  • Prevent Linux users from switching to a different SELinux confined user.

  • Control the methods administrators are permitted to use to switch user with SELinux confined users.

  • Examine the effects of confined SELinux users on sudo, su, ssh, and SUID execution and explore relevant Booleans.

Confirm that the workstation and serverc machines are started.

Log in to workstation as student using student as the password. On workstation, run lab selinux-users setup to verify that the environment is ready. This script also creates the runme test program in /tmp and /home/student.

[student@workstation ~]$ lab selinux-users setup
  1. On serverc, confine all Linux users except root by mapping them to the user_u SELinux user at login.

    1. Log in to serverc as student. No password is required.

      [student@workstation ~]$ ssh student@serverc
      [student@serverc ~]$ 
    2. Display the SELinux user associated with student.

      [student@serverc ~]$ id -Z
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    3. Use the sudo -i command to switch identity to the root user. Use student as the password.

      [student@serverc ~]$ sudo -i
      [sudo] password for student: student
      [root@serverc ~]# 
    4. Confirm that SELinux is in enforcing mode.

      [root@serverc ~]# getenforce
      Enforcing
    5. Retrieve the mapping between the Linux and the SELinux users.

      [root@serverc ~]# 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       *
      system_u             system_u             s0-s0:c0.c1023       *

      Notice that by default, SELinux maps the Linux users to unconfined_u. Also, changing this mapping has no impact on the root user because it has its own rule.

    6. Change the default mapping to map the Linux users to user_u. The semanage command may take up to a minute to complete.

      [root@serverc ~]# semanage login -m -s user_u  -r s0  __default__
      [root@serverc ~]# 
    7. Use the semanage login -l command again to verify your work.

      [root@serverc ~]# semanage login -l
      
      Login Name           SELinux User         MLS/MCS Range        Service
      
      __default__          user_u               s0                   *
      root                 unconfined_u         s0-s0:c0.c1023       *
      system_u             system_u             s0-s0:c0.c1023       *
    8. Log out of serverc, then log in again as student.

      [root@serverc ~]# logout
      [student@serverc ~]$ logout
      [student@workstation ~]$ ssh student@serverc
      [student@serverc ~]$ 
    9. Use the id -Z command to confirm that SELinux maps student to user_u.

      [student@serverc ~]$ id -Z
      user_u:user_r:user_t:s0
    10. Confirm that student cannot use the sudo or su commands anymore. For su, use redhat as the password for the root user.

      [student@serverc ~]$ sudo -i
      sudo: PERM_SUDOERS: setresuid(-1, 1, -1): Operation not permitted
      sudo: no valid sudoers sources found, quitting
      sudo: setresuid() [0, 0, 0] -> [1000, -1, -1]: Operation not permitted
      sudo: unable to initialize policy plugin
      [student@serverc ~]$ su -
      Password: redhat
      su: Authentication failure
  2. Set the user_exec_content SELinux Boolean to off to prevent users mapped to user_u from executing programs in /tmp or in their home directories.

    1. Verify that for now, student can still execute programs in the /tmp directory on serverc.

      [student@serverc ~]$ /tmp/runme
      Tested
      [student@serverc ~]$ ./runme
      Tested

      The runme program was deployed by the exercise setup script.

    2. Because you can no longer use sudo -i to switch from student to root, log out from serverc and log in again as root.

      [student@serverc ~]$ logout
      [student@workstation ~]$ ssh root@serverc
      [root@serverc ~]# 
    3. Set the SELinux user_exec_content Boolean to off. The setsebool command may take up to a minute to complete.

      [root@serverc ~]# setsebool -P user_exec_content off
      [root@serverc ~]# 
    4. Log out of serverc and log in again as student. No password is required.

      [root@serverc ~]# logout
      [student@workstation ~]$ ssh student@serverc
      [student@serverc ~]$ 
    5. Confirm that student can no longer execute programs in /tmp or their home directory.

      [student@serverc ~]$ /tmp/runme
      -bash: /tmp/runme: Permission denied
      [student@serverc ~]$ ./runme
      -bash: ./runme: Permission denied
  3. Create the operator1 Linux user account as follows:

    • The user is a system administrator and must be able to use the sudo and su commands. Map the user to the sysadm_u SELinux user.

    • Add the user to the wheel group, to benefit from the existing sudo rule granting permission to run any command as any user.

    • Set the user's password to redhat.

    • The operator1 user must be able to log in using ssh. The sysadm_u SELinux user does not allow this by default, but you can meet this requirement by allowing users mapped to sysadm_u to use ssh to log in.

    1. Log out of serverc and log in again as root.

      [student@serverc ~]$ logout
      [student@workstation ~]$ ssh root@serverc
      [root@serverc ~]# 
    2. Create the operator1 Linux user account, map it to the sysadm_u SELinux user, and add it to the wheel group. The useradd command may take up to a minute to complete.

      [root@serverc ~]# useradd -G wheel -Z sysadm_u operator1
      [root@serverc ~]# 
    3. Confirm that SELinux maps operator1 to sysadm_u.

      [root@serverc ~]# semanage login -l
      
      Login Name           SELinux User         MLS/MCS Range        Service
      
      __default__          user_u               s0                   *
      operator1            sysadm_u             s0-s0:c0.c1023       *
      root                 unconfined_u         s0-s0:c0.c1023       *
      system_u             system_u             s0-s0:c0.c1023       *
    4. Set the password for operator1 to redhat.

      [root@serverc ~]# echo redhat | passwd --stdin operator1
      Changing password for user operator1.
    5. Log out of serverc and try to use SSH to log in as operator1 user.

      [root@serverc ~]# logout
      [student@workstation ~]$ ssh operator1@serverc
      Unable to get valid context for operator1
      Last login: Mon Jul 23 07:58:37 2018 from workstation.lab.example.com
      Connection to serverc closed.
      [student@workstation ~]$ 

      By default, SELinux denies access to sysadm_u accounts over SSH.

    6. Log in as root and set the ssh_sysadm_login Boolean to on.

      [student@workstation ~]$ ssh root@serverc
      [root@serverc ~]# setsebool -P ssh_sysadm_login on
      [root@serverc ~]# 
    7. Log out of serverc and log in as operator1. This time the connection succeeds.

      [root@serverc ~]# logout
      [student@workstation ~]$ ssh operator1@serverc
      [operator1@serverc ~]$ 
    8. To confirm that operator1 can administer the system, use the sudo -i command to switch identity to the root user and restart the httpd service. Use redhat as the password.

      [operator1@serverc ~]$ sudo -i
      [sudo] password for operator1: redhat
      [root@serverc ~]# systemctl restart httpd
      [root@serverc ~]# systemctl is-active httpd
      active
  4. Create the developer1 Linux user account for your web developer. developer1 is not a system administrator, but they need to be able to restart the httpd service with sudo.

    Create the user account as follows:

    • Map the developer1 user to the staff_u SELinux user. Remember that if you do not explicitly map the user account, it is mapped to user_u, which does not allow sudo.

    • Set the user's password to redhat.

    • Create a new sudo configuration file, /etc/sudoers.d/developers, and add a rule to permit developer1 to run the systemctl restart httpd command as root.

    1. Create the developer1 Linux user account and map it to the staff_u SELinux user. The useradd command may take up to a minute to complete.

      [root@serverc ~]# useradd -Z staff_u developer1
      [root@serverc ~]# 
    2. Confirm that SELinux maps developer1 to staff_u.

      [root@serverc ~]# semanage login -l
      
      Login Name           SELinux User         MLS/MCS Range        Service
      
      __default__          user_u               s0                   *
      developer1           staff_u              s0-s0:c0.c1023       *
      operator1            sysadm_u             s0-s0:c0.c1023       *
      root                 unconfined_u         s0-s0:c0.c1023       *
      system_u             system_u             s0-s0:c0.c1023       *
    3. Set the password for developer1 to redhat.

      [root@serverc ~]# echo redhat | passwd --stdin developer1
      Changing password for user developer1.
    4. Create the /etc/sudoers.d/developers file and add a rule to permit developer1 to run systemctl restart httpd as root.

      [root@serverc ~]# vim /etc/sudoers.d/developers
      developer1  ALL=/bin/systemctl restart httpd
    5. Log out of serverc and log in as developer1. No password is required.

      [root@serverc ~]# logout
      [operator1@serverc ~]$ logout
      [student@workstation ~]$ ssh developer1@serverc
      [developer1@serverc ~]$ 
    6. Use the id -Z command to confirm that SELinux maps developer1 to staff_u.

      [developer1@serverc ~]$ id -Z
      staff_u:staff_r:staff_t:s0-s0:c0.c1023

      Notice that the current role is staff_r.

    7. Only the sysadm_r role allows the execution of commands using sudo. Confirm that the current role, staff_r, does not allow the user to restart the httpd service using sudo. Use redhat as the password.

      [developer1@serverc ~]$ sudo systemctl restart httpd
      [sudo] password for developer1: redhat
      Failed to get D-Bus connection: Operation not permitted
    8. To run commands with sudo, developer1 needs to change its current role to sysadm_r. Log out of serverc and log in as root. Use the semanage user -l command to confirm that SELinux allows staff_u users to change role to sysadm_r.

      [developer1@serverc ~]$ logout
      [student@workstation ~]$ ssh root@serverc
      [root@serverc ~]# semanage user -l
      
                    Labeling  MLS/       MLS/
      SELinux User  Prefix    MCS Level  MCS Range       SELinux Roles
      
      guest_u       user      s0         s0              guest_r
      root          user      s0         s0-s0:c0.c1023  staff_r sysadm_r system_r unconfined_r
      staff_u       user      s0         s0-s0:c0.c1023  staff_r sysadm_r system_r unconfined_r
      sysadm_u      user      s0         s0-s0:c0.c1023  sysadm_r
      system_u      user      s0         s0-s0:c0.c1023  system_r unconfined_r
      unconfined_u  user      s0         s0-s0:c0.c1023  system_r unconfined_r
      user_u        user      s0         s0              user_r
      xguest_u      user      s0         s0              xguest_r
    9. Configure sudo to perform the role change before running the command. Edit the /etc/sudoers.d/developers file and insert the ROLE variable before the command.

      [root@serverc ~]# vim /etc/sudoers.d/developers
      developer1  ALL= ROLE=sysadm_r /bin/systemctl restart httpd
    10. Log out of serverc and log in as developer1.

      [root@serverc ~]# logout
      [student@workstation ~]$ ssh developer1@serverc
      [developer1@serverc ~]$ 
    11. Confirm that developer1 can restart httpd. Use redhat as the password. Log out when done.

      [developer1@serverc ~]$ sudo /bin/systemctl restart httpd
      [sudo] password for developer1: redhat
      [developer1@serverc ~]$ logout
      [student@workstation ~]$ 

      The systemctl restart command does not display anything when successful.

Cleanup

On workstation, run the lab selinux-users cleanup script to clean up this exercise.

[student@workstation ~]$ lab selinux-users cleanup

This concludes the guided exercise.

Revision: rh415-7.5-b847083