{"id":14677,"date":"2020-09-07T07:00:13","date_gmt":"2020-09-07T05:00:13","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/"},"modified":"2020-09-07T07:00:13","modified_gmt":"2020-09-07T05:00:13","slug":"selinux-for-beginners","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/","title":{"rendered":"SELinux for beginners"},"content":{"rendered":"<p>Do you know the following situation: You are following a step by step tutorial on the web and on your environment does not work as expected because of SELinux. Your looking on search engines command how you can disable the SELinux\u2026 Does that sound familiar?<\/p>\n<p>On this Blog I will explain what SELinux are, where and how to use is. Let\u2019s start!<\/p>\n<p><strong>What is SELinux and why should I not disable it?<\/strong><\/p>\n<p>SE stands for &#8220;<strong>S<\/strong>ecurity <strong>E<\/strong>nhanced&#8221;, which provides administrators strict control over all processes on their system. Processes that are not considered necessary are blocked. Let\u2019s say you have PHP application which is vulnerable by SQL Injection. Now if someone\u2019s find your vulnerability, this person will try to get important files from your server such as the \u2018etc\/passwd\u2019 from the URL. An example SQL Injection attack will look like this: https:\/\/example.com\/file=&#8217;..\/..\/..\/..\/etc\/passwd&#8217;<\/p>\n<p>As you know the passwd file includes all users which has access to your server. The attacker will have more information\u2019s to exploit your environment. By activating SELinux, this could not be happened. SELinux will block this because the apache service is not allowed by default to get files from the root folders. It is only allowed read data from \u2018\/var\/www\/html\u2019 and will return the status 403 Forbidden. It makes sense to restrict access rights even if you actually trust the programs. If the application is hijacked by a third party, much less damage can occur. If programs infected by malware can access all processes and files on a system, they will also take advantage of this. SELinux restricts access and thus also the damage radius.<\/p>\n<p><strong>Summarized<\/strong>: By using SELinux you can define access controls for processes, files and applications. In other words, SELinux is like a watchdog which is watching changes and accesses for every file of your system. Since 2003 its integrated into the Linux upstream kernel.<\/p>\n<p><strong>How works SELinux?<\/strong><\/p>\n<p>SELinux works like a Firewall. By default, everything will be blocked. Once you need to allow something, you need to go ahead and configure it. If a service, program, or user then attempts to access or modify a file or resource that is not required for its function, access is denied, and the action is logged.<\/p>\n<p>Linux uses Discretionary Access Control (DAC) as access control. Users or applications that have the appropriate rights then usually have unrestricted access to the respective files and processes of the operating system. SELinux uses MAC (Mandatory Access Control) as access control. Administrators can precisely define security policies with MAC to define additional attributes under which conditions and in which contexts a rights holder can access certain processes or files of the operating system. If these conditions not match, access will be denied.<\/p>\n<p>Before we go deeper, we should know that SELinux has <strong>three basic modes<\/strong> of operation. By default, it is <strong>Enforcing<\/strong> mode:<\/p>\n<ul>\n<li><strong>Enforcing:<\/strong>The default mode. SELinux is enabled and running in enforce mode, which means all polices is watching and logging actions.<\/li>\n<li><strong>Permissive:<\/strong>In Permissive mode, SELinux is enabled but will not enforce the security policy, only warn and log actions. Permissive mode is useful for troubleshooting SELinux issues.<\/li>\n<li><strong>Disabled:<\/strong>SELinux is turned off<\/li>\n<\/ul>\n<p><strong>How to use SELinux?<\/strong><\/p>\n<p>To check your current mode:<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">sestatus<\/pre>\n<p>If you want to change your mode, you can simple run setenforce 0 or setenforce 1. This will not change it permanently! After a reboot your mode will switch to default. To make changes persistent through a system reboot, edit the &#8216;SELINUX=&#8217; line in\u00a0 \/etc\/selinux\/config\u00a0 for either &#8216;enforcing&#8217;, &#8216;permissive&#8217;, or &#8216;disabled&#8217;.<\/p>\n<p>If you disabled SELinux before, you might be to reset the policy value to default.<\/p>\n<p>First, set enforcing in \u2018etc\/selinux\/config\u2019 and then create in the server root directory .autorelabel and the reboot<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">touch \/.autorelabel\nreboot<\/pre>\n<p>Following labels can be set for each process and each file to define the policies.<\/p>\n<ul>\n<li>User<\/li>\n<li>Role<\/li>\n<li>Type<\/li>\n<li>Level<\/li>\n<\/ul>\n<p>For example, an application may only access folders that have a certain label. The review of the guidelines is called SELinux enforcement<\/p>\n<p><strong>How to set policies?<\/strong><\/p>\n<p>SELinux comes with a lot of policies by default, you need just enable it. To see a list of all built-in policies you can use the getsebool -a command.<\/p>\n<p>For example, to get all http related policies:<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">getsebool -a | grep http<\/pre>\n<p>To enable a policy:<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">setsebool -P httpd_can_sendmail=1<\/pre>\n<p>For sure you can create your own policies. For that there are multiple ways to do that. The easiest way is to check the audit.log file and creating from there the policy.<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">grep nginx \/var\/log\/audit\/audit.log audit | audit2why<\/pre>\n<p>audit2why will explain why something has blocked. If you want to create a custom policy from the audit.log file, you need run following command:<\/p>\n<pre class=\"brush: actionscript3; gutter: true; first-line: 1\">grep nginx \/var\/log\/audit\/audit.log | audit2allow -M nginxpolicy<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Advanced configuration<\/strong><\/p>\n<p>To make even more specific policies, you need understand the targeted SELinux Policy. There are 4 types of targeted:<\/p>\n<ul>\n<li><strong>Type Enforcement (TE):<\/strong>Type Enforcement is the primary mechanism of access control used in the\u00a0<strong>targeted<\/strong>\u00a0policy<\/li>\n<li><strong>Role-Based Access Control (RBAC):<\/strong>Based around SELinux users (not necessarily the same as the Linux user), but not used in the default configuration of the\u00a0<strong>targeted<\/strong>\u00a0policy<\/li>\n<li><strong>Multi-Level Security (MLS):<\/strong>Not commonly used and often hidden in the default\u00a0<strong>targeted<\/strong><\/li>\n<li><strong>Multi-Category Security(MCS):<\/strong>An extension of Multi-Level Security, used in the\u00a0<strong>targeted<\/strong>\u00a0policy to implement compartmentalization of virtual machines and containers through\u00a0<a href=\"https:\/\/access.redhat.com\/documentation\/en-US\/Red_Hat_Enterprise_Linux\/7\/html\/SELinux_Users_and_Administrators_Guide\/chap-Security-Enhanced_Linux-sVirt.html\">sVirt<\/a>.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong>\u00a0The -Z switch will work with most utilities to show SELinux security contexts (e.g, &#8216;ls -Z&#8217;, &#8216;ps axZ&#8217; etc).<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>This Blog has written for SELinux beginners. SELinux can very complicated and the administration could be very time-consuming. My recommendation: If you want to use SELinux, the administrator should document every change. Otherwise the overview can quickly get lost. <strong>But never disable SELinux! Instead set it to permissive mode!<\/strong> Have fun with Security Enhanced \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know the following situation: You are following a step by step tutorial on the web and on your environment does not work as expected because of SELinux. Your looking on search engines command how you can disable the SELinux\u2026 Does that sound familiar? On this Blog I will explain what SELinux are, where [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[42,149],"tags":[],"type_dbi":[],"class_list":["post-14677","post","type-post","status-publish","format-standard","hentry","category-operating-systems","category-security"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SELinux for beginners - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SELinux for beginners\" \/>\n<meta property=\"og:description\" content=\"Do you know the following situation: You are following a step by step tutorial on the web and on your environment does not work as expected because of SELinux. Your looking on search engines command how you can disable the SELinux\u2026 Does that sound familiar? On this Blog I will explain what SELinux are, where [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-07T05:00:13+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\"},\"headline\":\"SELinux for beginners\",\"datePublished\":\"2020-09-07T05:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\"},\"wordCount\":955,\"commentCount\":1,\"articleSection\":[\"Operating systems\",\"Security\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\",\"name\":\"SELinux for beginners - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2020-09-07T05:00:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SELinux for beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SELinux for beginners - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/","og_locale":"en_US","og_type":"article","og_title":"SELinux for beginners","og_description":"Do you know the following situation: You are following a step by step tutorial on the web and on your environment does not work as expected because of SELinux. Your looking on search engines command how you can disable the SELinux\u2026 Does that sound familiar? On this Blog I will explain what SELinux are, where [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/","og_site_name":"dbi Blog","article_published_time":"2020-09-07T05:00:13+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b"},"headline":"SELinux for beginners","datePublished":"2020-09-07T05:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/"},"wordCount":955,"commentCount":1,"articleSection":["Operating systems","Security"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/","url":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/","name":"SELinux for beginners - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2020-09-07T05:00:13+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/selinux-for-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SELinux for beginners"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/14677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=14677"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/14677\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=14677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=14677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=14677"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=14677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}