{"id":895,"date":"2019-10-03T10:23:27","date_gmt":"2019-10-03T10:23:27","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=895"},"modified":"2024-07-05T16:48:38","modified_gmt":"2024-07-05T16:48:38","slug":"selinux","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=895","title":{"rendered":"SELINUX"},"content":{"rendered":"<p>SELinux is a set of security rules using which we can control the processes\/application that can access specific files, folders and ports. These processes, files, folders and ports has a special security label called SELinux context. The label called context can be:<\/p>\n<ul>\n<li>user<\/li>\n<li>role<\/li>\n<li>type<\/li>\n<li>sensitivity.<\/li>\n<\/ul>\n<p>Installing the below packages can help in troubleshoot SElinux related issues<\/p>\n<p style=\"padding-left: 30px;\"><strong>[root@centos9vm ~]#\u00a0<\/strong>dnf install policycoreutils-python-utils.noarch<\/p>\n<p style=\"padding-left: 30px;\"><strong>[root@centos9vm ~]#\u00a0<\/strong>dnf install\u00a0setroubleshoot.x86_64<\/p>\n<p><a href=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2019\/10\/SElinux.jpg\" rel=\"attachment wp-att-1004\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1004\" src=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2019\/10\/SElinux.jpg\" alt=\"SElinux\" width=\"411\" height=\"161\" srcset=\"http:\/\/shijuvarghese.com\/wp-content\/uploads\/2019\/10\/SElinux.jpg 411w, http:\/\/shijuvarghese.com\/wp-content\/uploads\/2019\/10\/SElinux-300x118.jpg 300w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/a><\/p>\n<p>The SELinux policy determines if a process or application such as httpd, etc can access a files, folders or port. By default the SELinux policy blocks all access to files, folders and ports.<\/p>\n<p>SELinux application specific policies is defined by the application developers. These policies define what actions are allowed for execution, and what access is allowed.\u00a0The default policy enabled in RHEL\/CentOS called &#8220;targeted policy&#8221;.<\/p>\n<p>Selinux Context (label) has the below parameters:<\/p>\n<ul>\n<li>SElinux user<\/li>\n<li>Role<\/li>\n<li><strong>Type<\/strong><\/li>\n<li>Level<\/li>\n<\/ul>\n<p>If we take example of a Apache web service, the httpd process usually access the folders &#8220;\/var\/ww\/html&#8221;, &#8220;\/var\/tmp&#8221;, &#8220;\/tmp&#8221;, etc.<\/p>\n<p>The <em>Type<\/em> context associated with httpd service is <strong>httpd_t<\/strong>. The Type context associated with the folder &#8220;\/var\/ww\/html&#8221; is &#8220;<strong>httpd_sys_content_t<\/strong>&#8220;, the port is &#8220;<strong>http_port_t<\/strong>&#8220;, etc.<\/p>\n<p>The SELinux context can be displayed when using commands such as ps, ls, cp, mkdir. etc by using the &#8220;<strong>-Z<\/strong>&#8221; switch. For example:<\/p>\n<p><strong>[root@shiju-test ~]# ps -axZ | grep http<\/strong><br \/>\n<em>system_u:system_r:httpd_t:s0 11675 ? Ss 0:00 \/usr\/sbin\/httpd -DFOREGROUND<\/em><br \/>\n<em>system_u:system_r:httpd_t:s0 11676 ? S 0:00 \/usr\/sbin\/httpd -DFOREGROUND<\/em><\/p>\n<p><strong>[root@shiju-test ~]# ls -Z \/var\/www<\/strong><br \/>\n<em>drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin<\/em><br \/>\n<em>drwxr-xr-x. root root system_u:object_r:<strong>httpd_sys_content_t<\/strong>:s0 html<\/em><\/p>\n<p style=\"text-align: center;\"><strong><span style=\"text-decoration: underline;\">Copying and Moving files<\/span><\/strong><\/p>\n<p style=\"text-align: left;\">When moving a file or folder, the SELinux context is\u00a0<strong>preserved<\/strong> by default.<\/p>\n<p style=\"text-align: left; padding-left: 30px;\"><strong>[root@shiju-test ~]#\u00a0<\/strong>mv \/tmp\/shiju.txt \/var\/www\/html<\/p>\n<p style=\"text-align: left;\">When copying a file or folder, the SELinux is NOT preserved by default. The switch &#8220;-p&#8221; is required to carry the SELinux context.<\/p>\n<p style=\"text-align: left;\"><strong>[root@shiju-test ~]# <\/strong>cp\u00a0\/tmp\/shiju.txt \/var\/www\/html<\/p>\n<p style=\"text-align: left;\"><strong>[root@shiju-test ~]#\u00a0<\/strong>ls -Z \/var\/www\/html<\/p>\n<p style=\"text-align: left;\"><strong>[root@shiju-test ~]#\u00a0<\/strong>cp =p \/tmp\/shiju.txt \/var\/www\/html<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>SELinux configuration file<\/strong><\/span><\/p>\n<p>Configuration file for SELinux is &#8220;<em>\/etc\/selinux\/config<\/em>&#8221;<\/p>\n<p>SELinux can be configured in three modes:<\/p>\n<ul>\n<li><strong>Enforced :<\/strong> Policies are enforced and logging is enabled<\/li>\n<li><strong>Permissive :<\/strong> Policies are actually disabled but logging is enabled as the policies are enabled<\/li>\n<li><strong>Disabled :<\/strong> Policies are disabled and logging is also not enabled.<\/li>\n<\/ul>\n<p>Command to verify the mode in which SELinux is running:<br \/>\n<strong>[root@shiju-test ~]#\u00a0<\/strong>getenforce<\/p>\n<p>Command to switch to disabled mode:<br \/>\n<strong>[root@shiju-test ~]#\u00a0<\/strong>setenforce 0<\/p>\n<p>Command to switch to permissive mode:<br \/>\n<strong>[root@shiju-test ~]#\u00a0<\/strong>setenforce 1<\/p>\n<p>&nbsp;<\/p>\n<p>In RHEL, the SElinux can be fully disabled during the boot process itself if needed. To do that, enter the grub prompt during the bootup, and in the Kernel parameter add one of the below value:<\/p>\n<ul>\n<li>selinux=0<\/li>\n<li>enforcing=0<\/li>\n<li>enforcing=1<\/li>\n<\/ul>\n<p>Save changes and procced with the booting process.<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>SELinux Logs<\/strong><\/span><\/p>\n<p>SELinux logs useful information such as info about access denied, etc. The default log file is:<br \/>\n<em>\/var\/log\/audit\/audit.log<\/em><\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>An example to understand SELinux context<\/strong><\/span><\/p>\n<ul>\n<li>Use a system in which SELinux is enabled. The command &#8220;getenforce&#8221; will show the present SELinux setting.<\/li>\n<li>Install httpd\u00a0server and ensure firewalld is disabled in the host .<\/li>\n<li>Create an html file name <strong>index.html<\/strong> displaying a line &#8220;<em>Hello World &#8211; var &#8211; html<\/em>&#8220;<\/li>\n<li>Allow everyone to access the page by issuing the command:\n<ul>\n<li>chmod 755 \/var\/www\/html\/index.html<\/li>\n<\/ul>\n<\/li>\n<li>Verify if everyone can see the web page by accessing the server via a web browser. It should work while selinux is also enabled.<\/li>\n<\/ul>\n<ul>\n<li>Create a new folder named &#8220;\/<strong>virtual&#8221;<\/strong><\/li>\n<li>Create an html file name <strong>\/virtual\/index.html<\/strong> displaying a line &#8220;<em>Hello World &#8211; virtual &#8211; html<\/em>&#8220;.<\/li>\n<li>Add the following lines in your apache webserver&#8217;s configuration file httpd.conf:<\/li>\n<\/ul>\n<p style=\"padding-left: 90px;\">&lt;Directory &#8220;\/virtual&#8221;&gt;<\/p>\n<p style=\"padding-left: 120px;\">AllowOverride None<br \/>\n# Allow open access:<br \/>\nRequire all granted<\/p>\n<p style=\"padding-left: 90px;\">&lt;\/Directory&gt;<\/p>\n<ul>\n<li>In the <strong>httpd.conf<\/strong> file, edit the line that starts with &#8220;<strong>DocumentRoot<\/strong>&#8221; so that it looks like:<br \/>\n<strong>DocumentRoot &#8220;\/virtual&#8221;<\/strong><\/li>\n<li>Restart apache by running the command &#8220;systemctl restart httpd&#8221;<\/li>\n<li>The service may fail<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>You may not be able to see the web page is accessed via browser since SELinux may be blocking access to &#8220;<strong>\/virtual\/index.html<\/strong>&#8221;<\/p>\n<p>Check the log file &#8220;<strong>\/var\/log\/audit\/audit.log<\/strong>&#8221; for any entry related to it like:<\/p>\n<ul>\n<li>type=AVC msg=audit(1523351308.296:598): avc: denied { getattr } for pid=1247 comm=&#8221;httpd&#8221; path=&#8221;\/virtual\/index.html&#8221; dev=&#8221;xvda2&#8243; ino=25270803 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file<\/li>\n<\/ul>\n<p>If you disable SELinux temporarily by issuing the command &#8220;<strong>setenforce 0<\/strong>&#8221; or permanently by editing the file &#8220;<strong>\/etc\/selinux\/config<\/strong>&#8221; you will see that the file gets displayed.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">How to get the new page displayed with SELinux?<\/span><\/strong><\/p>\n<p>We can change the context of a file by using either:<\/p>\n<ol>\n<li>Temporary method\n<ul>\n<li>chcon<\/li>\n<\/ul>\n<\/li>\n<li>Permanent method\n<ul>\n<li>fcontext<\/li>\n<li>restorecon<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>To make the webpage &#8220;<em>\/virtual\/index.html<\/em>&#8221; accessible by http server while SELinux is enforced, run the following command:<\/p>\n<p><strong>[root@shiju-test ~]#\u00a0<\/strong>chcon -R -t httpd_sys_content_t \/virtual\/<\/p>\n<p>Check the result by running the following command:<\/p>\n<p style=\"padding-left: 30px;\"><strong>[root@shiju-test ~]#\u00a0<\/strong>ls -Z \/virtual<\/p>\n<p style=\"padding-left: 30px;\">-rwxr-xr-x. 1 root root system_u:object_r:<strong>httpd_sys_content_t<\/strong>:s0 43 Feb 12 19:59 index.html<\/p>\n<p>Now the new webpage should get displayed via the browser<\/p>\n<p>The set of files that holds the default context of files and folder are located in &#8220;<strong>\/etc\/selinux\/targeted\/contexts\/files\/<\/strong>&#8221;<\/p>\n<p>As the chcon command is a temporary way to change context permission, restarting the node or issuing the\u00a0<strong>restorecon<\/strong> command will revert context attached to a file to the value mentioned in the above files.<\/p>\n<p>To restore the context to default settings as mentioned in SELinux policy use the following command:<\/p>\n<p style=\"padding-left: 30px;\"><strong>root@shiju-test ~]#\u00a0<\/strong>restorecon -Rv \/virtual\/<br \/>\nRelabeled \/virtual from system_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:default_t:s0<br \/>\nRelabeled \/virtual\/index.html from system_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:default_t:s0<\/p>\n<p>Command to view the default SELinux policy settings is as below. You may use &#8220;grep&#8221; function to display selective results<\/p>\n<p style=\"padding-left: 30px;\"><strong>root@shiju-test ~]#\u00a0<\/strong>ls -lZ \/virtual\/<br \/>\n-rwxr-xr-x. 1 root root system_u:object_r:default_t:s0 43 Feb 12 19:59 index.html<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Semanage commands<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">The below command lists all context policies that will be read by default<\/p>\n<p style=\"text-align: left; padding-left: 30px;\"><strong>[root@shiju-test ~]#\u00a0semanage fcontext -l<\/strong><\/p>\n<p>We can add an SELinux Policy by using <strong>semanage fcontext<\/strong> commands so that the same can be used when &#8220;<strong>restorecon<\/strong>&#8221; command is used.<\/p>\n<p>Mentioned below command changes the context of all files in &#8220;\/virtual&#8221; folder<\/p>\n<p style=\"padding-left: 30px;\"><strong>[root@shiju-test ~]#\u00a0<\/strong>semanage fcontext -a -t httpd_sys_content_t &#8216;\/virtual(\/.*)?&#8217;<\/p>\n<p style=\"padding-left: 30px;\"><strong>root@shiju-test ~]#\u00a0<\/strong>restorecon -Rv \/virtual\/<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Listening ports associated with a service<\/strong><\/span><\/p>\n<p>Troubleshooting SELinux is easy by using the &#8220;setroubleshoot-server&#8221; that can be installed using the command &#8220;yum install setroubleshoot-server&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>SELinux is a set of security rules using which we can control the processes\/application that can access specific files, folders and ports. These processes, files, <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=895\" title=\"SELINUX\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":252,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,31,30,14],"tags":[],"class_list":["post-895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-rhel-4","category-rhel-9","category-security"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=895"}],"version-history":[{"count":23,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/895\/revisions"}],"predecessor-version":[{"id":1572,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/895\/revisions\/1572"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/252"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=895"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}