'Why does SELinux label files unexpectedly when created?
On a private Centos 8 development server, while working on code, access is lost to certain files. Investigating the matter, I found it's caused by the SELinux label these files receive. It only seems to happen when these files are created from the shell utilities like touch, mkdir and through input output piping. When the same file is for instance uploaded through a cifs share, the label is as expected.
According to my knowledge, new files and directories inherit the SElinux context from their parent or when a rule matches from what is defined through semanage in /etc/selinux/targeted/contexts. However something else seems to be also at play.
Setup a test to reproduce the issue
# mkdir /home/user/testdir/
# ls -Zd /home/user/testdir/
unconfined_u:object_r:user_home_t:s0 /home/user/testdir
# semanage fcontext -a -t httpd_user_content_t '/home/user/testdir(/.*)?'
# restorecon -R /home/user/testdir/
# setsebool -P samba_enable_home_dirs 1
# setsebool -P httpd_enable_homedirs 1
# ls -Zd /home/user/testdir/
unconfined_u:object_r:httpd_user_content_t:s0 /home/user/testdir/
Create a file through the CLI. Expected behaviour is that the file would be labelled as httpd_user_content_t like the parent and the above set rule.
# ls -Z /home/user/testdir/.htaccess
ls: cannot access '/home/user/testdir/.htaccess': No such file or directory
# touch /home/user/testdir/.htaccess
# ls -Z /home/user/testdir/.htaccess
unconfined_u:object_r:httpd_user_htaccess_t:s0 /home/user/testdir/.htaccess
The tools supplied with SELinux seem to agree with me:
# matchpathcon -V /home/user/testdir/.htaccess
/home/user/testdir/.htaccess has context unconfined_u:object_r:httpd_user_htaccess_t:s0, should be system_u:object_r:httpd_user_content_t:s0
restorecon denies restoring the context to default as its overridden by the admin?
# restorecon -vv /home/user/testdir/.htaccess
/home/user/testdir/.htaccess not reset as customized by admin to unconfined_u:object_r:httpd_user_htaccess_t:s0
There is exactly one rule present that labels files with the httpd_user_htaccess_t type. But that rule does NOT match our files, as our files are not within a www, web or public_html directory. If I would read the pattern wrongly, both matchpathcon and restorecon should have already told me that.
# semanage fcontext -l | grep httpd_user_htaccess_t
/home/[^/]+/((www)|(web)|(public_html))(/.*)?/\.htaccess regular file unconfined_u:object_r:httpd_user_htaccess_t:s0
Apparently I thought I knew how SELinux worked or I'm unaware of some part of SELinux or I'm unaware of some other process that comes with Centos 8.
Of course I can chcon all the affected files and directories each time, or I can write a custom policy to also allow access to httpd_user_htaccess_t, httpd_user_ra_content_t and httpd_user_rw_content_t. But I would rather understand what's going on here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
