{"id":949,"date":"2018-12-30T18:02:15","date_gmt":"2018-12-30T08:02:15","guid":{"rendered":"https:\/\/www.airxperts.net\/?p=949"},"modified":"2026-03-18T00:30:39","modified_gmt":"2026-03-17T14:30:39","slug":"troubleshooting-selinux-part-1","status":"publish","type":"post","link":"https:\/\/airxperts.net\/index.php\/2018\/12\/30\/troubleshooting-selinux-part-1\/","title":{"rendered":"Troubleshooting SELinux &#8211; Part 1"},"content":{"rendered":"<h4>Hardening with SELinux<\/h4>\n<p>I finally got around to hardening my web server over the holiday break. Well, hardening more. A very important step in hardening linux is ensuring selinux is both <em>enabled<\/em> and <em>enforcing<\/em>. Of course the risk is that <strong>S<\/strong>ecurity <strong>E<\/strong>nhanced <strong>Linux<\/strong> starts blocking things that should not be blocked&#8230;<!--more--><\/p>\n<h5>Undeserved Reputation?<\/h5>\n<p>I have been reluctant to have SELinux enforcing on all my instances of linux for a long time. It turns out that nowadays, with the maturity of the feature and its associated tools, this is pretty much an indefensible position.<\/p>\n<p>Sure, back in the mid-noughties SELinux had its issues and caused much grief for sysadmins, but things have changed&#8230; a lot. This post documents me catching up, and will hopefully be useful to anyone trying to do the same!<\/p>\n<h5>Why SEliNux?<\/h5>\n<p>SELinux is the most essential security tool available on linux. Period. No other single tool succeeds succeeds so well at securing a system from the inside out.<\/p>\n<p>It originated from a 3-letter agency with a special interest in security. You can read more about it at <a href=\"https:\/\/en.wikipedia.org\/wiki\/Security-Enhanced_Linux\" target=\"_blank\" rel=\"noopener\">wikipedia<\/a>.<\/p>\n<h5>PRE-REQUISITES<\/h5>\n<p>I used Centos 7 for this post. Most of my machines use Centos without a GUI installed. The tools should be identical on Red Hat (RHEL) and very similar on any modern linux distro. Of course the system commands may differ on your platform.<\/p>\n<p>For the rest of the post I&#8217;ll assume you are using Centos 7.<\/p>\n<pre>[sysadmin@imago ~]$ cat \/etc\/centos-release\nCentOS Linux release 7.6.1810 (Core)<\/pre>\n<h4>How SELinux Works<\/h4>\n<p>For those familiar with networking, SELinux works a little like a firewall, in that it controls activity between sources and targets based on a configured policy. That policy may include definitions of the source and target objects and rules about the types of activity allowed between them.<\/p>\n<p>SELinux differs from a firewall in that it is interested in the activity <em>within<\/em> your system, not <em>between<\/em> systems on a network. And it does a very good job of this.<\/p>\n<h5>KEY CONCEPT: Objects<\/h5>\n<p>Objects are those &#8220;sources&#8221; or &#8220;targets&#8221; in our firewall analogy, either performing an action, or having an action performed on them.<\/p>\n<p>Objects include a whole range of linux &#8220;things&#8221;, such as processes, files, directories, devices and sockets. But typically we&#8217;ll be dealing with processes and the filesystem.<\/p>\n<p>SELinux has total visibiility and control of the activity between any of these objects. For example, when a process tries to read a file, or open a network socket, SELinux has the power to permit or deny this action.<\/p>\n<h5>KEY CONCEPT: CONTEXTS<\/h5>\n<p>While SELinux can is very flexible and can get very complicated, the basics are actually quite simple.<\/p>\n<p>Every object on the system is labelled with an SELinux <em>context<\/em>. The context is stored in a <em>label,<\/em> so the terms &#8216;context&#8217; and &#8216;label&#8217; are often used interchangably.<\/p>\n<p>A label has four fields: user, role, type and security level.<\/p>\n<h5>KEY CONCEPT: TYPES<\/h5>\n<p>In this series we will mainly be concerned with the <em>type<\/em> field. The type is what&#8217;s usually used to construct the default policy on most linux distributions. And for most situations that&#8217;s all that&#8217;s required.<\/p>\n<p>The <em>user<\/em> and <em>role<\/em> fields are generally not used except in the most hardened environments. They are there to allow an ever richer set of SELinux policies, termed <em>Role-Based Access Control (RBAC)<\/em>. This sounds similar to the tradional DAC but is much more powerful, and we won&#8217;t cover it in this series.<\/p>\n<h4>Viewing Contexts<\/h4>\n<p>SELinux labels are already there, whether you are using them or not.<\/p>\n<p>If you were to look at the files in a directory with the &#8220;-Z&#8221; option, you will see the SELinux context associated with each object (file or directory), including the type.<\/p>\n<h5>Filesystem CONTEXTS<\/h5>\n<p>Here is how my home directory looks:<\/p>\n<pre>[sysadmin@server ~]$ ls -aZ\ndrwx------. sysadmin sysadmin system_u:object_r:user_home_dir_t:s0 .\ndrwxr-xr-x. root root system_u:object_r:home_root_t:s0 ..\n-rw-------. sysadmin sysadmin system_u:object_r:user_home_t:s0 .bash_history\n-rw-r--r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 .bash_logout\n-rw-r--r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 .bash_profile\n-rw-r--r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 .bashrc\n-rw-------. sysadmin sysadmin system_u:object_r:user_home_t:s0 .lesshst\ndrwx------. sysadmin sysadmin system_u:object_r:ssh_home_t:s0 .ssh\n-rw-rw-r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 tmp1\n-rw-rw-r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 tmp2\n-rw-------. sysadmin sysadmin unconfined_u:object_r:user_home_t:s0 .viminfo\n-rw-r--r--. sysadmin sysadmin system_u:object_r:user_home_t:s0 .vimrc<\/pre>\n<p>The context for this directory is &#8216;system_u:object_r:user_home_dir_t:s0&#8217;, and this contains the four field previously mentioned.<\/p>\n<p>Type are easily spotted by ending in &#8220;_t&#8221;. The type of this directory and most of the files within is &#8220;user_home_t&#8221;.<\/p>\n<h5>Process CONTEXTs<\/h5>\n<p>Processes also have a context and a type, and simillarly, these can be shown with the &#8220;-Z&#8221; switch. Most of the standard utilities that use permissions have adopted this switch:<\/p>\n<pre>[sysadmin@server ~]$ ps -aZ\nLABEL PID TTY TIME CMD\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2744 pts\/0 00:00:00 sudo\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2748 pts\/0 00:00:00 su\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2749 pts\/0 00:00:00 bash\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 3648 pts\/0 00:00:00 tail\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 3649 pts\/0 00:00:00 grep\nunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5524 pts\/1 00:00:00 ps<\/pre>\n<p>Here, all the processes my user has initiated are of type &#8220;unconfined_t&#8221;.<\/p>\n<p>Actually, the correct term for a process is <em>domain<\/em>, not type. But the &#8216;_t&#8217; naming convention is still used. I suspect this is because processes are more complex that files and would be assoicated with particular addresses in RAM for example. But be aware that type and domain could be used interchangably in respect to processes.<\/p>\n<h4>SELinux Policies<\/h4>\n<p>Unlike a network firewall, which comes with either no rules or a &#8216;allow everything&#8217; rule, SELinux comes with a default policy and a rich set of default rules that are a workable starting point for most systems. Remember SELinux is m<em>andatory<\/em> access control. Similar to context labels which are already there, so is a policy.<\/p>\n<p>We&#8217;ll see how to determine which policy is in effect below.<\/p>\n<h5>POLICY Rules<\/h5>\n<p>Rules basically boil down to which type of <em>objects <\/em>can do what <em>actions<\/em> to which types of <em>objects.<\/em><\/p>\n<p>For example can a process of type &#8220;unconfined_t&#8221; write to a directory with the &#8220;user_home_dir_t&#8221; type? Can it read from the same directory? Or modify a particular file in that directory?<\/p>\n<h4>How SELinux\u00a0 Rules Are Applied<\/h4>\n<p>SELinux is loaded into the kernel at boot, so it&#8217;s able to be right at the core of all activity on your system.<\/p>\n<h5>TRADITIONAL SECURITY CONTROLS<\/h5>\n<p>You may already be familiar with the regular user+group filesystem permissions system used on Linux. This system is known as <em>Discretionary Access Control (DAC)<\/em>.<\/p>\n<p>When you are using SELinux, DAC remains in effect. In fact DAC is applied <em>before<\/em> SELinux. If for example DAC blocks an action, then SELinux will not see the attempted action or log it. SELinux does not modify DAC&#8217;s filesystem permissions or behaviour in any way. It runs in series.<\/p>\n<p>SELinux interoperates similarly with other security controls, such as the host firewall.<\/p>\n<p>Generally SELinux does not replace other controls, it supplements them.<\/p>\n<h5>SELinux Benefits<\/h5>\n<p>SELinux is a kind of <em>Mandatory Access Control (MAC)<\/em>. <em>Everything<\/em> is policed by SELinux once it&#8217;s enforcing.<\/p>\n<p>Further, all policies are defined at the administrator level. DAC allows regular users to change permission on files and directories they have write access to.<\/p>\n<p>SELInux improves the separation between processes, so successful privilege escalation attacks on one process are less likley to lead to other parts of the system being compromised.<\/p>\n<p>SELinux is also offer much more fine-grained control, even without using the RBAC features.<\/p>\n<p>The default action is <em>deny<\/em>. If there is no rule explicitly allowing access, actions are blocked. This limits the damage from both malicious and accidental activity.<\/p>\n<h4>In Part 2&#8230;<\/h4>\n<p>And that&#8217;s pretty much all we need to know to get started with some hands-on.<\/p>\n<p>In the next post we will learn how to the determine the status and and current mode of operation on a system, and how to change it.\u00a0 Then we&#8217;ll touch on where and how SELinux logs information.<\/p>\n<p>Join me in <a href=\"https:\/\/www.airxperts.net\/index.php\/2019\/01\/27\/troubleshooting-selinux-part-2\/\">Troubleshooting SELinux \u2013 Part 2<\/a>.<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hardening with SELinux I finally got around to hardening my web server over the holiday break. Well, hardening more. A very important step in hardening linux is ensuring selinux is both enabled and enforcing. Of course the risk is that Security Enhanced Linux starts blocking things that should not be blocked&#8230;<\/p>\n","protected":false},"author":2,"featured_media":983,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[51,49,52,50],"class_list":["post-949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sysadmin","tag-centos","tag-linux","tag-security","tag-selinux"],"_links":{"self":[{"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/posts\/949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/comments?post=949"}],"version-history":[{"count":30,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/posts\/949\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/posts\/949\/revisions\/1008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/media\/983"}],"wp:attachment":[{"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/media?parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/categories?post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/airxperts.net\/index.php\/wp-json\/wp\/v2\/tags?post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}