{"id":585,"date":"2022-03-21T03:07:14","date_gmt":"2022-03-21T10:07:14","guid":{"rendered":"https:\/\/cloudinsidr.com\/content\/?p=585"},"modified":"2022-03-21T09:11:24","modified_gmt":"2022-03-21T16:11:24","slug":"tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system","status":"publish","type":"post","link":"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/","title":{"rendered":"SELinux security contexts: correcting SELinux labels on a file system"},"content":{"rendered":"<p>SELinux can be such a nuisance. In particular, if you have a newly created file system, you will need to add labels to it, also known as SELinux security contexts.<\/p>\n<p><!--more--><\/p>\n<p>Inappropriate SELinux security labels can result in errors such as NGINX 403 Forbidden. The fact that SELinux could be the culprit of a 403 error is usually less than obvious.<\/p>\n<h2>Diagnose the problem<\/h2>\n<p>SELinux logs denials to:<\/p>\n<pre>\/var\/log\/audit\/audit.log<\/pre>\n<p>One of the simplest ways to poke around SELinux involves using the grep utility with audit2why, for example like this:<\/p>\n<pre>grep 1657864453.071:1986 \/var\/log\/audit\/audit.log | audit2why<\/pre>\n<p>But for more comprehensive diagnostics, sealert from setroubleshoot comes in handy:<\/p>\n<pre>sealert -a \/var\/log\/audit\/audit.log<\/pre>\n<h2>Relabel your file system<\/h2>\n<p>In its default configuration, SELinux already has a firm idea on what security contexts to apply when restoring security labels on your system. Restoring those default labels is also pretty much straightforward:<\/p>\n<pre>\/sbin\/restorecon -R -v \/var\/www\/html\/index.html<\/pre>\n<p>If you think you have a badly mislabeled machine, you can fully relabel it using:<\/p>\n<pre>touch \/.autorelabel; reboot<\/pre>\n<p>If no <strong>label changes<\/strong> or <strong>Booleans<\/strong> allow access (getsebool -a), you can use audit2allow to create a local policy module. Use this ability as a measure of last resort, though.<\/p>\n<p><strong>WARNING:<\/strong> Modules created with audit2allow may grant SELinux more leeway than may be healthy for your system. For this reason, Red Hat recommends that any policy created with audit2allow be posted to an SELinux list (such as fedora-selinux-list) for review.<\/p>\n<p>If you still need help, keep reading.<\/p>\n<h2>Restore the default SELinux labels<\/h2>\n<p>The command restorecon restores the security context to the system&#8217;s default based on the default SELinux labels for each location.<\/p>\n<h3>Find\u00a0out the default SELinux labels for NGINX<\/h3>\n<p>To find out the default SELinux labels for various elements of an NGINX installation, use this command:<\/p>\n<pre class=\"\">[root@host]$ grep nginx \/etc\/selinux\/targeted\/contexts\/files\/file_contexts\r\n<\/pre>\n<figure id=\"attachment_587\" aria-describedby=\"caption-attachment-587\" style=\"width: 886px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts.png\" rel=\"attachment wp-att-587\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-587\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts.png\" alt=\"grep SELinux labels\" width=\"886\" height=\"217\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts.png 886w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts-600x147.png 600w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts-300x73.png 300w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/grep_SELINUX_contexts-768x188.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/a><figcaption id=\"caption-attachment-587\" class=\"wp-caption-text\">Showing all SELinux labels (click to enlarge)<\/figcaption><\/figure>\n<p>The output of this command shows default SELinux labels for NGINX installation directories:<\/p>\n<pre>\/etc\/nginx(\/.*)? system_u:object_r:httpd_config_t:s0\r\n\/var\/run\/nginx.* system_u:object_r:httpd_var_run_t:s0\r\n\/var\/lib\/nginx(\/.*)? system_u:object_r:httpd_var_lib_t:s0\r\n\/var\/log\/nginx(\/.*)? system_u:object_r:httpd_log_t:s0\r\n\/var\/opt\/rh\/rh-nginx18\/log(\/.*)? system_u:object_r:httpd_log_t:s0\r\n\/etc\/opt\/rh\/rh-nginx18\/nginx(\/.*)? system_u:object_r:httpd_config_t:s0\r\n\/usr\/lib\/systemd\/system\/nginx.* -- system_u:object_r:httpd_unit_file_t:s0\r\n\/var\/opt\/rh\/rh-nginx18\/lib\/nginx(\/.*)? system_u:object_r:httpd_var_lib_t:s0\r\n\/var\/opt\/rh\/rh-nginx18\/run\/nginx(\/.*)? system_u:object_r:httpd_var_run_t:s0\r\n\/usr\/sbin\/nginx -- system_u:object_r:httpd_exec_t:s0<\/pre>\n<p>The default web directory (\/etc\/nginx\/html), by default,\u00a0is unconfined:<\/p>\n<pre>[root@ip-10-0-0-63 fedora]# ls -laZ \/etc\/nginx\/html\/\r\ntotal 16\r\ndrwxr-xr-x. 2 root root unconfined_u:object_r:etc_t:s0 4096 Jun 13 00:48 .\r\ndrwxr-xr-x. 7 root root unconfined_u:object_r:etc_t:s0 4096 Jun 18 12:28 ..\r\n-rw-r--r--. 1 root root unconfined_u:object_r:etc_t:s0 537 Jun 13 00:48 50x.html\r\n-rw-r--r--. 1 root root unconfined_u:object_r:etc_t:s0 612 Jun 13 00:48 index.html<\/pre>\n<h3>Find\u00a0out the default SELinux labels for a CMS such as WordPress<\/h3>\n<p>You can easily find out the default SELinux security contexts for WordPress on your system using the command:<\/p>\n<pre class=\"\">[root@host]$ grep wordpress \/etc\/selinux\/targeted\/contexts\/files\/file_contexts\r\n\r\n\/usr\/share\/wordpress\/.*\\.php -- system_u:object_r:httpd_sys_script_exec_t:s0\r\n\/usr\/share\/wordpress\/wp-includes\/.*\\.php -- system_u:object_r:httpd_sys_script_exec_t:s0\r\n\/usr\/share\/wordpress-mu\/wp-content(\/.*)? system_u:object_r:httpd_sys_rw_content_t:s0\r\n\/usr\/share\/wordpress\/wp-content\/uploads(\/.*)? system_u:object_r:httpd_sys_rw_content_t:s0\r\n\/usr\/share\/wordpress\/wp-content\/upgrade(\/.*)? system_u:object_r:httpd_sys_rw_content_t:s0\r\n\/usr\/share\/wordpress-mu\/wp-config\\.php -- system_u:object_r:httpd_sys_script_exec_t:s0<\/pre>\n<figure id=\"attachment_599\" aria-describedby=\"caption-attachment-599\" style=\"width: 660px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack.png\" rel=\"attachment wp-att-599\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-599 size-large\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack-1024x147.png\" alt=\"Listing current SELinux security contexts for WordPress\" width=\"660\" height=\"95\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack-1024x147.png 1024w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack-600x86.png 600w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack-300x43.png 300w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack-768x110.png 768w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPressBlack.png 1034w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-599\" class=\"wp-caption-text\">Listing current SELinux security contexts for WordPress<\/figcaption><\/figure>\n<p>The output may reveal some very useful information, for example:<\/p>\n<pre>\/usr\/share\/wordpress-mu\/wp-config\\.php -- system_u:object_r:httpd_sys_script_exec_t:s0<\/pre>\n<p>The above line indicates that the main configuration file of WordPress named wp-config.php should receive the following SELinux security context label:<\/p>\n<pre>system_u:object_r:httpd_sys_script_exec_t:s0<\/pre>\n<p>This SELinux label will only be applied, however, if the file is located in\u00a0\/usr\/share\/wordpress-mu\/, which is almost certainly not the case on your system.<\/p>\n<h3>Adjust SELinux security contexts<\/h3>\n<p>Before implementing permanent changes, it is advisable to try adjusting SELinux security labels\u00a0on the affected directory\u00a0tree and its contents temporarily. By doing so you can verify that your assumptions work and if not, restore the defaults (with restorecon) easily.<\/p>\n<h4>List current SELinux security contexts on files and directories<\/h4>\n<p>Listing current SELinux security contexts involves the command:<\/p>\n<pre class=\"\">ls -laZ\r\n<\/pre>\n<p>The resulting output for an installation of WordPress in the web server document directory www.website1.tld may\u00a0look something like this:<\/p>\n<pre class=\"\">drwxr-x---. owner1 \u00a0nginx unconfined_u:object_r:httpd_sys_content_t:s0 www.website1.tld<\/pre>\n<h4>Adjust the SELinux security contexts without changing defaults<\/h4>\n<p>To (temporarily) adjust the SELinux security contexts for WordPress so that it can run:<\/p>\n<pre class=\"\">chcon -vR system_u:object_r:httpd_sys_content_t:s0 www.website1.tld<\/pre>\n<p>For all .php scripts inside the WordPress installation directory and its subdirectories:<\/p>\n<pre class=\"\">chcon -R -v system_u:object_r:httpd_sys_script_exec_t:s0 *\/*\/*\/*.php<\/pre>\n<p><strong>WordPress will run with these settings, but it will fail to write updates, install themes, and plugins. (It almost makes you wonder why these settings are the defaults.)<\/strong><\/p>\n<p><strong>In order to allow NGINX to write to the WordPress installation, adjust the security context on the entire WordPress installation\u00a0and its subdirectories using this command:<\/strong><\/p>\n<pre class=\"\">chcon -R -v system_u:object_r:httpd_sys_rw_content_t:s0 www.website1.tld\/<\/pre>\n<h4>Save new default SELinux contexts<\/h4>\n<p>Make the desired changes permanent:<\/p>\n<pre class=\"lang:sh decode:true \">semanage fcontext -a -t httpd_sys_rw_content_t -s system_u \"\/var\/www\/www.website1.tld(\/.*)?\"<\/pre>\n<p>Verify that your local contexts are correct:<\/p>\n<pre>cat \/etc\/selinux\/targeted\/contexts\/files\/file_contexts.local<\/pre>\n<p>You can add or\u00a0remove default SELinux contexts as you see fit. To remove a context that is faulty or no longer needed:<\/p>\n<pre>semanage fcontext -d\u00a0\"\/var\/www\/www.domainname.tld(\/.*)?\"<\/pre>\n<h4>Restore SELinux default labels<\/h4>\n<p>Finally, to restore SELinux labels from the defaults, run:<\/p>\n<pre class=\"\">restorecon -RFv \/var\/www\/<\/pre>\n<p>on the webserver document directory (adjust the path as needed).<\/p>\n<p>&#8212;<\/p>\n<p>Here is a primer on how to set up <a href=\"https:\/\/www.cloudinsidr.com\/content\/a-web-server-in-the-aws-cloud-how-to-set-up-a-website-from-scratch-on-a-domain-of-your-choice\/\" target=\"_blank\" rel=\"noopener\">NGINX on EC2 from scratch<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELinux can be such a nuisance. In particular, if you have a newly created file system, you will need to add labels to it, also known as SELinux security contexts.<\/p>\n","protected":false},"author":101012,"featured_media":592,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[143,109,154,1],"tags":[140,70],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SELinux security contexts: correcting SELinux labels on a file system - CloudInsidr<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SELinux security contexts: correcting SELinux labels on a file system - CloudInsidr\" \/>\n<meta property=\"og:description\" content=\"SELinux can be such a nuisance. In particular, if you have a newly created file system, you will need to add labels to it, also known as SELinux security contexts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudInsidr\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-21T10:07:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-21T16:11:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPress.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1023\" \/>\n\t<meta property=\"og:image:height\" content=\"146\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/\",\"name\":\"CloudInsidr\",\"description\":\"Cyber security, infotech\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.cloudinsidr.com\/content\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/01\/SELinux_contexts_forWordPress.png\",\"width\":1023,\"height\":146,\"caption\":\"Listing current SELinux security contexts for WordPress\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/#webpage\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/\",\"name\":\"SELinux security contexts: correcting SELinux labels on a file system - CloudInsidr\",\"isPartOf\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/#primaryimage\"},\"datePublished\":\"2022-03-21T10:07:14+00:00\",\"dateModified\":\"2022-03-21T16:11:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/73723b2da71b6d515d17ca593ea5dc68\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-find-the-correct-selinux-security-contexts-and-adjust-selinux-labels-on-your-linux-system\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/73723b2da71b6d515d17ca593ea5dc68\",\"name\":\"Filipe Martins\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dbac033b4d26da8ca1fbde233e49c8dc?s=96&d=mm&r=g\",\"caption\":\"Filipe Martins\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/585"}],"collection":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/users\/101012"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/comments?post=585"}],"version-history":[{"count":55,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":2842,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/585\/revisions\/2842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media\/592"}],"wp:attachment":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media?parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/categories?post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/tags?post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}