Bookmark this page

Guided Exercise: Auditing the SELinux Policy

In this exercise, you will use system tools to examine the system's current SELinux policy, and to interpret whether or not specific SELinux domains used by processes have access to files and ports labeled with specific SELinux types.

Outcomes

You should be able to:

  • Use policy tools to predict what SELinux domain the process will have when it is run, based on the SELinux type on an executable.

  • Determine what SELinux types may be accessed by that process and what access is permitted or blocked.

  • Determine whether or not a particular SELinux domain can transition to unconfined_t, and whether or not unconfined_t can transition to that domain.

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-audit setup to verify that the environment is ready. This script also installs httpd on serverc.

[student@workstation ~]$ lab selinux-audit setup
  1. Use the SELinux policy tools to predict the SELinux domain type for the httpd daemon when systemd starts the service.

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

      [student@workstation ~]$ ssh student@serverc
      [student@serverc ~]$ 
    2. 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 ~]# 
    3. Install the policycoreutils-devel and setools-console packages to provide the sepolicy and sesearch commands, respectively.

      [root@serverc ~]# yum install policycoreutils-devel setools-console
      ...output omitted...
      Is this ok [y/d/N]: y
      ...output omitted...
      Complete!
    4. When using systemctl to start a service, the command forwards the request to the systemd daemon. Use the ps -Z command to retrieve the SELinux domain type of the systemd daemon.

      [root@serverc ~]# ps -Z -C systemd
      LABEL                             PID TTY          TIME CMD
      system_u:system_r:init_t:s0         1 ?        00:00:27 systemd
    5. The systemd daemon starts the service by executing the httpd binary file. Use the ls -Z command to retrieve the SELinux context type of the httpd executable.

      [root@serverc ~]# which httpd
      /sbin/httpd
      [root@serverc ~]# ls -Z /sbin/httpd
      -rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /sbin/httpd
    6. Use the sesearch command to retrieve the SELinux domain transition rule for when a daemon of type init_t executes a program of type httpd_exec_t.

      [root@serverc ~]# sesearch -T -s init_t -t httpd_exec_t
      Found 1 semantic te rules:
         type_transition init_t httpd_exec_t : process httpd_t;

      The SELinux domain type of the resulting process is httpd_t.

    7. Use the sepolicy transition command as another way to list domain transitions.

      [root@serverc ~]# sepolicy transition -s init_t -t httpd_t
      init_t @ httpd_exec_t --> httpd_t
      init_t ... initrc_t @ httpd_exec_t --> httpd_t
      init_t ... initrc_t ... vpnc_t ... ifconfig_t ... iptables_t ... insmod_t ... mount_t ... glusterd_t @ httpd_exec_t --> httpd_t
      init_t ... initrc_t ... vpnc_t ... ifconfig_t ... iptables_t ... insmod_t ... mount_t ... glusterd_t ... udev_t ... cupsd_config_t ... cupsd_t ... logrotate_t @ httpd_exec_t --> httpd_t
      ...output omitted...

      The sepolicy transition command displays all the transition paths between a source and a target domain. The first line indicates a direct transition by execution of a binary with the httpd_exec_t type. Notice that for the sepolicy transition command, the -t option is the target domain type. For the sesearch command, -t is the SELinux type of the program file.

    8. Confirm your observation by starting the httpd service and getting the domain type of the resulting httpd processes.

      [root@serverc ~]# systemctl start httpd
      [root@serverc ~]# ps -Z -C httpd
      LABEL                             PID TTY          TIME CMD
      system_u:system_r:httpd_t:s0     1826 ?        00:00:01 httpd
      system_u:system_r:httpd_t:s0     1827 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1828 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1829 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1830 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1831 ?        00:00:00 httpd
  2. Manually start the httpd daemon, without using systemctl. Observe and explain the resulting SELinux domain type.

    1. Stop the httpd service.

      [root@serverc ~]# systemctl stop httpd
      [root@serverc ~]# 
    2. Directly start the httpd daemon, without using systemctl.

      [root@serverc ~]# httpd
      [root@serverc ~]# 

      Red Hat does not recommend starting services this way. Always use the systemctl command.

    3. Retrieve the SELinux domain type of the httpd daemon.

      [root@serverc ~]# ps -Z -C httpd
      LABEL                             PID TTY          TIME CMD
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1856 ? 00:00:00 httpd
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1857 ? 00:00:00 httpd
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1858 ? 00:00:00 httpd
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1859 ? 00:00:00 httpd
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1860 ? 00:00:00 httpd
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1861 ? 00:00:00 httpd

      The httpd domain type is unconfined_t.

    4. The source domain that started the httpd daemon is your Bash shell. Retrieve the SELinux type of your shell.

      [root@serverc ~]# ps -Z $$
      LABEL                             PID TTY      STAT   TIME COMMAND
      unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1735 pts/0 S   0:00 -bash

      $$ is the PID of the current process.

    5. Use the sesearch command to look for a domain transition rule from the unconfined_t source type to the unconfined_t domain type when executing a program with the httpd_exec_t type.

      [root@serverc ~]# sesearch -T -s unconfined_t -t httpd_exec_t
      
      [root@serverc ~]# 

      There is no such rule. Therefore, the daemon resulting from the execution of /sbin/httpd inherits the domain type of the program that launches the command; your shell is this example.

    6. Clean up by killing the httpd processes and restarting the service with systemctl.

      [root@serverc ~]# pkill httpd
      [root@serverc ~]# systemctl start httpd
      [root@serverc ~]# 
  3. Create a test HTML page, and locate the rule that allows the httpd daemon to read that file.

    1. Create the test page, index.html, in the httpd DocumentRoot directory, /var/www/html/. Use curl to confirm that you can access the new page.

      [root@serverc ~]# cd /var/www/html
      [root@serverc html]# echo "Hello World" > ./index.html
      [root@serverc html]# curl http://localhost/index.html
      Hello World
    2. Retrieve the SELinux domain type of the httpd daemon, and the type of the index.html file.

      [root@serverc html]# ps -Z -C httpd
      LABEL                             PID TTY          TIME CMD
      system_u:system_r:httpd_t:s0     1952 ?        00:00:02 httpd
      system_u:system_r:httpd_t:s0     1953 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1954 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1955 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1956 ?        00:00:00 httpd
      system_u:system_r:httpd_t:s0     1957 ?        00:00:00 httpd
      [root@serverc html]# ls -Z ./index.html
      -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 ./index.html
    3. Use the sesearch command to retrieve the rule that allows the httpd_t domain type to read files with the httpd_sys_content_t type.

      [root@serverc html]# sesearch -A -s httpd_t -t httpd_sys_content_t \
      > -c file
      Found 5 semantic av rules:
         allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;
         allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ;
         allow httpd_t httpdcontent : file { ioctl read write create getattr setattr lock append unlink link rename open } ;
         allow httpd_t httpdcontent : file { read getattr execute open } ;
         allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;
  4. Locate the rule that allows the httpd daemon to execute CGI scripts in the /var/www/cgi-bin/ directory.

    1. Retrieve the SELinux context type of the /var/www/cgi-bin/ directory.

      [root@serverc html]# ls -Zd /var/www/cgi-bin/
      drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/

      CGI scripts in this directory also inherit that context.

    2. Use the sesearch command to retrieve the rule that allows the httpd_t domain type to execute files with the httpd_sys_script_exec_t type.

      [root@serverc html]# sesearch -A -s httpd_t \
      > -t httpd_sys_script_exec_t -c file
      Found 4 semantic av rules:
         allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;
         allow httpd_t httpd_script_exec_type : file { ioctl read getattr lock open } ;
         allow httpd_t httpd_sys_script_exec_t : file { read getattr execute open } ;
         allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;
    3. The previous rule depends on the httpd_enable_cgi Boolean. Run the sesearch command again, but add the -C option to display the Booleans associated with each rule.

      [root@serverc html]# sesearch -A -s httpd_t \
      > -t httpd_sys_script_exec_t -c file -C
      Found 4 semantic av rules:
         allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;
         allow httpd_t httpd_script_exec_type : file { ioctl read getattr lock open } ;
      ET allow httpd_t httpd_sys_script_exec_t : file { read getattr execute open } ; [ httpd_enable_cgi ]
      ET allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ; [ httpd_builtin_scripting ]

      The httpd_enable_cgi Boolean is currently on (E), therefore the rule applies.

  5. Locate the rule that allows the httpd daemon to bind to TCP port 80.

    1. Use the semanage port command to retrieve the SELinux type associated with TCP port 80.

      [root@serverc html]# semanage port -l | grep 80
      ...output omitted...
      http_port_t           tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
      ...output omitted...
    2. Use the sesearch command to get the rule that allows the httpd_t domain type to bind to the ports with the http_port_t type.

      [root@serverc html]# sesearch -A -s httpd_t -t http_port_t
      Found 11 semantic av rules:
         allow httpd_t http_port_t : tcp_socket name_bind ;
      ...output omitted...

Cleanup

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

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

This concludes the guided exercise.

Revision: rh415-7.5-813735c