{"id":9709,"date":"2025-02-26T01:02:00","date_gmt":"2025-02-26T06:02:00","guid":{"rendered":"https:\/\/www.both.org\/?p=9709"},"modified":"2025-02-21T13:37:57","modified_gmt":"2025-02-21T18:37:57","slug":"fix-selinux-problems-with-this-little-known-option","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=9709","title":{"rendered":"Fix SELinux problems with this little-known option"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"9709\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>SELinux is a security subsystem that works to prevent files and code from running from places they don&#8217;t belong. It&#8217;s a big complex system that&#8217;s integrated into the very kernel, and it has governance over every file on your computer. Fortunately, you can benefit from SELinux and even interact with it without understanding how or why it works. Here&#8217;s how I manage SELinux on my system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understand enforcement<\/h2>\n\n\n\n<p>The most basic security toggle on your Linux computer is the <code>setenforce<\/code> command. Using just a single <code>setenforce<\/code> instruction, you can configure SELinux to allow a violation it would normally prevent. There are two states: Enabled and Permissive. By default, SELinux is <code>Enabled<\/code> (also represented as <code>1<\/code> when using Boolean values). To set SELinux to permissive mode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo setenforce Permissive<\/code><\/pre>\n\n\n\n<p>Try that the next time you&#8217;re working on something at the system-level, using root or <code>sudo<\/code> permissions, and it doesn&#8217;t work.<\/p>\n\n\n\n<p>When something works in Permissive mode, you&#8217;ve successfully identified the symptom, but you haven&#8217;t fixed the problem yet. Activate Enforcing mode again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo setenforce Enforcing<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Check the status of SELinux<\/h2>\n\n\n\n<p>You can check the state of SELinux at any time using the <code>sestatus<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sestatus\nSELinux status:                 enabled\nSELinuxfs mount:                \/sys\/fs\/selinux\nSELinux root directory:         \/etc\/selinux\nLoaded policy name:             targeted\nCurrent mode:                   enforcing\n&#91;...]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Look at labels and contexts<\/h2>\n\n\n\n<p>If you have a running Linux system, then you have an example of what SELinux requires for normal operation. You don&#8217;t have to learn about security contexts or memorize labels. For most anything you try to do on your computer, there are likely already files doing something similar. Use those files as templates.<\/p>\n\n\n\n<p>You can look at the security labels of any file you have access to by using the <code>-Z<\/code> (that&#8217;s a capital Z) option of <code>ls<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ touch hello\n$ ls -Z hello \nunconfined_u:object_r:user_home_t:s0 hello<\/code><\/pre>\n\n\n\n<p>An empty file created by a user in the user&#8217;s own home directory has, as you might expect, a very specific security profile. Even with the executable bit set, that file would not be permitted to run as a systemwide service. It just doesn&#8217;t have the correct security context.<\/p>\n\n\n\n<p>If you use an <code>ll<\/code> alias, try adding the <code>-Z<\/code> option to its option list so you get used to seeing SELinux labels. The more you see what labels exist on your system, and how they relate to various system roles, you&#8217;re more likely to recognize when they&#8217;re wrong.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy contexts<\/h2>\n\n\n\n<p>Suppose you were developing a custom SELinux service for your laptop. You&#8217;ve written a shell script, a service file, and you&#8217;ve placed them in the appropriate system locations. You&#8217;re also careful to set ownership and permissions correctly. But no matter what you do, you get errors when attempting to start the service.<\/p>\n\n\n\n<p>You suspect that SELinux might be preventing an unrecognized service from running. That would normally be appreciated, but in this case you want to make an exception.<\/p>\n\n\n\n<p>First, confirm that the service runs successfully with SELinux in Permissive mode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo setenforce Permissive\n$ sestatus | grep Current\nCurrent mode:                 permissive\n$ sudo systemctl start hello.service || echo \"fail\"\n$ \n$ sudo setenforce Enforcing<\/code><\/pre>\n\n\n\n<p>Then look at the files you&#8217;ve created using the <code>-Z<\/code> and compare them with other files that you know to be working properly. Note the differences:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -Z \/usr\/lib\/systemd\/system\/hello.service \nunconfined_u:object_r:systemd_unit_file_t:s0\n$ ls -Z \/usr\/lib\/systemd\/system\/rdisc.service \nsystem_u:object_r:rdisc_unit_file_t:s0<\/code><\/pre>\n\n\n\n<p>The working service (<code>rdisc.service<\/code> in this example, chosen at random) features the <code>system_u<\/code> label as well as a special <code>rdisc_unit_file_t<\/code> label. Suppose you know from previous experience with <code>ls -Z<\/code> that a common SELinux label for systemd service files is <code>systemd_unit_file_t<\/code> so you ignore that difference. However, <code>unconfined_u<\/code> and <code>system_u<\/code> seem to be important.<\/p>\n\n\n\n<p>Use the <code>chcon<\/code> (&#8220;change context&#8221;) command to change the security context of your service file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo chcon system_u:object_r:systemd_unit_file_t:s0 \\\n\/usr\/lib\/systemd\/system\/hello.service \n\n$ ls -Z \/usr\/lib\/systemd\/system\/hello.service \nsystem_u:object_r:systemd_unit_file_t:s0<\/code><\/pre>\n\n\n\n<p>Your systemd service is probably triggering some executable file on your system. If you created that yourself, it probably also has the incorrect security context. Comparing it to a known working script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -Z \/usr\/bin\/example.sh \nunconfined_u:object_r:gconf_home_t:s0\n$ ls -Z \/usr\/bin\/brltty-prologue.sh\nsystem_u:object_r:bin_t:s0<\/code><\/pre>\n\n\n\n<p>Again, there&#8217;s one obvious difference, which you can correct with <code>chcon<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chcon system_u:object_r:bin_t:s0 \\\n\/usr\/bin\/example.sh <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Remember the foundations of Linux<\/h2>\n\n\n\n<p>By accident or by design, one of the foundational principles of Linux is that you can learn Linux by exploring your Linux system. This holds true whether you&#8217;re reading source code, reading FAQs buried in a documentation folder, or just compulsively looking at security labels. After you get to know what Linux looks like when it&#8217;s working, it&#8217;s easy to spot problems when something&#8217;s not working. Use the <code>-Z<\/code> option with <code>ls<\/code> or <code>ll<\/code>, and start getting used to what SELinux expects. The next time something fails and you suspect SELinux, think of files that fill a similar role to what you&#8217;re trying to do, and check you work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELinux is a security subsystem that works to prevent files and code from running from places they don&rsquo;t<\/p>\n","protected":false},"author":31,"featured_media":2700,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,75],"tags":[722],"class_list":["post-9709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-security","tag-selinux"],"modified_by":"Seth Kenlon","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9709"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9709\/revisions"}],"predecessor-version":[{"id":9730,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9709\/revisions\/9730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2700"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}