SELinux

Short information about SELinux

SELinux

Basic commands

  • To check ithe mode status:
    • getenforce
  • To check if enabled and more details:
    • sestatus
  • We can temporarily set the current status of SELinux:
    • setenforce 0 / setenforce permisive:
      • Changes SELinux to permissive mode
      • Policy violations are only logged, but otherwise ignored
    • setenforce 1 / setenforce enforing:
      • Changes SELinux to enforcing mode
      • Policy violations are logged, and prevented

Important

  • setenforce 0 / setenforce disabled:
    • If we run into problems (policy violations) due to SELinux, this command would always solve them
    • We should never do this for this reason!
    • We should never do this for this reason!
    • This command should only be used for collecting data in order to adjus SELinux permissions and / or our policy (more on that later)

Permanently changing status

  • configuration can be changed in the file /etc/selinux/config
  • here we can set if the SELinux should be enabled:
    • SELINUX=enforcing:
      • SELinux should enforce the policy (and log violations)
    • SELINUX=permissive:
      • SELinux should only log policy violations
    • SELINUX=disabled:
      • SELinux is disabled

File contexts

  • Are a way of labeling files
  • This will allow SELinux to enforce its policy and to check if access to a certain file / resource is allowed

File contexts in SELinux

  • Files on SELinux systems are labeled
  • Those labels are completely independent from the normal unix permissions
  • **We can have a look at those labels with the ls command:
    • ls -Z
[tomik@almarouter ~]$ ls -Z
unconfined_u:object_r:user_home_t:s0 bel.txt    unconfined_u:object_r:user_home_t:s0 numbers.txt     unconfined_u:object_r:user_home_t:s0 scripts
unconfined_u:object_r:user_home_t:s0 json.json  unconfined_u:object_r:user_home_t:s0 permission.txt  unconfined_u:object_r:user_home_t:s0 test
unconfined_u:object_r:user_home_t:s0 nohup.out  unconfined_u:object_r:user_home_t:s0 population.txt  unconfined_u:object_r:user_home_t:s0 website
[tomik@almarouter ~]$
  • What does it mean?
    • unconfined_u:object_r:user_home_t:s0 website
  • The syntax is as follows:
    • [SELinux suer]:[SELlinux role]:[SELinux type]:[SELinux level]
  • Here in this case: The SELinux user is: unconfined_u The SELinux role is: oject_r The SELinux type is: user_home_t The SELinux levelis: s0
  • In SELinux, best-practice is as follows:
    • All SELinux users end with _u, roles end with _r, and types end with _u
  • Based on the Context, SELinux will decide if access should be granted
  • For us, the type will be the most important part of the SELinux context

How does SELinux protect us?

  • The type allows the SELinux policy to allow access to just certain parts of the filesystem
  • Example:
    • A webserver should only be allowed to access httpd_sys_content_t,httpd_config_t,etc_t (and few others)
    • If it tries to access any file with another type - this access is not allowed and thus prevented ( if SELinux is enforcing its policy)

What happens when we create a new file?

  • By default:
    • If we create a file, it will inherit the SELinux context from its parent directory
    • Except when the SELinux policy includes a rule that overrides this default behavior (type transition)

Changing the context (temporarily)

  • We can change the SELinux labesl with the chcon command:
    • chcon [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] [-R] [-v] FILE
    • -R: Recursive
    • -v: Verbose (print which files have been changed)
    • Example:
      • chcon -t user_home_t -R -v folder

Resetting the context

  • If we want to reset the file context to the default, we can use the following command:
    • restorecon -R -v folder/file
    • this will reset the SELinux type for a given file or folder
    • Important options:
      • -R: Recursive
      • -v: Print all changes
      • -F: By default, only the SELinux type is reset, By adding this option, it will also reset the SELinux user & role

How do default contexts work?

  • SELinux somehow used a default context for many files
  • How does it work?
  • Those defaults are defined in the following folders:
    • /etc/selinux/[policy]/contexts
    • We can have a look at those, but we should never edit those files directly
    • Example:
      • cat /etc/selinux/targeted/contexts/files/file_context
    • It’s best to use the following command:
      • semanage fcontext -l
    • If we want to change the default context:
      • semanage fcontext -a -t httpd_sys_content_t ‘/public(/.*)?’
      • -a:
        • we want to add a default rule (-d would remove one)
      • -t https_sys_content_t:
        • This is the SELinux type that we want to be applied
      • ‘/public(/.*)?
        • Absolute path, with regular expression to define which files should be affected
      • Now we need to restorecon setting yet with command:
        • restorecon -R -v /public
    • Or we create a mapping to an existing context of another folder:
      • semanage fcontext -a -e /usr/share/nginx/html /public
      • -e: Defines the mapping
      • The same rules that are applied to share/nginx/html should be applied also to public folder

SELinux: processes

  • All processes on our system run with a specific SELinux security context
  • This includes:
    • A SELinux user
    • A SELIinux role
    • A SELinux type
  • This security context is used to define which files a process can access
  • This provides an additional layer of security
  • When a process is started:
    • The policy defines the transition into a different SELinux context:
      • Example:
        • systemd (as SELinux typy init_t) can start nginx as system_u::system_r:httpd_t
  • How can we see the SELinux security context of a process?
    • We could use the Activity Monitor on a Desktop system to show it
    • Or on the command line, we can use program ps:
      • ps -Z
      • ps -efZ
    • For htop, we need to enable the following column:
      • SECATTR
      • for this the htop program must be installed and repository epel-release have to be enabled for CentOS)

SELinux: The targeted policy

  • By default (targeted policy):
    • SELinux is only enforcing its typing policy for system processes
    • The goal here:
      • SELinux should protect our servcices
      • But it should not protect us against a compromised user
    • We can find out our users SELinux domain by using program id for this:
      • id -Z
    • Or through the semanage command:
      • semanage login -l
    • in my case, it’s unconfined_u
    • Thus, the SELinux rules allow pretty much everything for our normal user
      • Most processes that our interacitve user starts will be runnung as the SELinux user unconfined_u
        • The SELinux policy can override the SELinux context for certain processes, even if we start them as unconfined_u

SELinux: Booleans

  • The SELinux policy needs to be adjusted to our applications needs
  • This is what makes a system with SELinux more secure
  • For adjusting this, we want to have a simple way to confifure our system
  • Luckily, we can use SELinux Booleans for this. We can toggle policy rules.
    • To hev a look at the available booleans:
      • getsebool -a
    • To also get an explanation about them:
      • semanage boolean -l
    • Example of enable boolean via setsebool command
      • Temporarily:
        • setsebool httpd_read_user_content on
      • Permanently:
        • setsebool -P httpd_read_user_content on

SELinux: Auditing policy violations

  • Whenever a SELinux policy violation happens:
    • It will (usually) ber recorded in the audit log of SELinux:
      • /var/log/audit/audit.log
      • We can query this log file with the ausearch command:
        • ausearch -ts recent
        • By default, ausearch would display all violations of today
        • But with -ts recent, only the last 10 minutes will be shown
      • Once we found an entry, we an query journalctl:
        • journalctl -t setroubleshoot –since=14:20
      • And then we can run sealert as shown in the journalctl output to display additional info
        • sealert -l <long hexadecimal code>
  • Usually, this is because:
    • incorrectly applied SELinux labels for files or directories
    • We have changed the configuration of the application
    • Confined processes act in a different way than their policy
    • Our application or the policy is faulty

SELinux: Managing ports

  • If an application wants to listen on a specific port, SELinux must allow this too
  • We can have a look at the mapping with the following command:
    • semanage port -l
  • We can add an additional port:
    • semanage port -a -t http_port_t -p tcp 8888
      • -a: we want to add a port
      • -t http_port_t: for the type http_port_t
      • -p tcp: for the protocol TCP
      • 8888: The port we want to add (8888)

SELinux: Disabling during boot

  • Sometimes, enforcing strict policy rules might prevent our system from booting up
  • If the GRUB boot menu is enabled:
    • We can disable SELinux for a single boot:
      • selinux=0
    • Or set SELinux to permissive for a single boot:
      • selinux=1 enforcing=0
    • During GRUB menu press e to edit and write in the end of line (one line above initrd) i.e. selinux=1 enforcing=0
    • our example of line: - rd,lvm.lv=cs_cenos/swap rhgb quiet selinux=1 enforcing=0
    • If the GRUB is disabled:
      • We would need to change our GRUB configuration
      • We have had a look at this in the chapter about boot process and systemd