SELinux is an upgrade to the file system that creates labels to enhance the policies for groups and users.
Created by Red Hat and the NSA it also comes natively in CentOS and Fedora but can be installed in any other Linux and Unix distributions because it uses Kernel security modules.
Install the packets:
sudo apt update sudo apt install policycoreutils selinux-utils selinux-basics -y
Check the status and activate:
sestatus sudo selinux-activate sudo reboot
Then it will reboot automatically one more time.
By default, it will be permissive when enabled.
The permissive mode will allow the applications to access the file even if it is not labeled accordingly but it will log everything.
Enforcing will restrict access based on the labeling policy of SELinux.
sudo selinux-config-enforcing sestatus
The following commands can get the current mode and set the current mode:
getenforce setenforce setenforce 0
The mode can also be defined in the configuration file:
sudo nano /etc/selinux/config
The configuration file can be also modified by following the commands:
sudo sed -i 's/SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
sudo sed -i 's/SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
By listing the content of the directory the labels can be seen for each file:
Change the label:
semanage fcontext -a -t FILE_TYPE "/web"
For a webserver, the FILE_TYPE would be httpd_sys_content_t.
Apply the changes:
restorecon -Rv /web
Checking the log messages:
grep AVC /var/log/messages
AVC (Access Vector Cache) can be understood as Access Violation.
Another way is to look for Alerts on the same file:
grep sealert /var/log/messages
Then copy and execute the command relative to the alert you want more details, for example:
sealert -l askjc1c63deb-2af3-9d23-a3247a234ab34
Note that files created will inheritance the labels from the parent directory. In the case of moving files, they will maintain the labels and will require to be re-labeled.
Keep the system in permissive mode, check the logs, and apply all the necessary labels according to the applications running.
In conclusion, SELinux requires a lot of work labeling all the file-system before enabling enforcing mode. Otherwise, it will crash many applications and possibly the system itself.