This article covers Files and Processes in SELinux. Basically, managing file and process context are at the heart of a successful SELinux implementation.
With SELinux, a process or application will have only the rights it needs to function and NOTHING more. The SELinux policy for the application will determine what types of files it needs access to and what processes it can transition to.
SELinux policies are written by app developers and shipped with the Linux distribution that supports it. A policy is basically a set of rules that maps processes and users to their rights.
SELinux enforces something we can term as “context inheritance”. What this means is that unless specified by the policy, processes and files are created with the contexts of their parents.
So if we have a process called “proc_a” spawning another process called “proc_b”, the spawned process will run in the same domain as “proc_a” unless specified otherwise by the SELinux policy.
SELinux in Action: Testing a File Context Error
1. First, let's create a directory named www under the root. We will also create a folder called html under www:
mkdir -p /www/html
2. If we run the ls -Z command, we will see these directories have been created with the default_t context:
ls -Z /www/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 html
3. Next we copy the contents of the /var/www/html directory to /www/html:
cp /var/www/html/index.html /www/html/
The copied file will have a context of default_t. That's the context of the parent directory.
We now edit the httpd.conf file to point to this new directory as the web site's root folder.
i. We will also have to relax the access rights for this directory.
ii. First we comment out the existing location for document root and add a new DocumentRoot directive to /www/html:
# DocumentRoot "/var/www/html"
iii. We also comment out the access rights section for the existing document root and add a new section:
# AllowOverride None
# Allow open access:
# Require all granted
# Allow open access:
Require all granted
We leave the location of the cgi-bin directory as it is. We are not getting into detailed Apache configuration here; we just want our site to work for SELinux purposes.
iv. Finally, restart the httpd daemon:
service httpd restart
Once the server has been restarted, accessing the web page will give us the same “403 Forbidden” error (or default “Testing 123” page) we saw before.
The error is happening because the index.html file's context changed during the copy operation. It needs to be changed back to its original context (httpd_sys_content_t).