Disabling AppArmor
Since AppArmor is enabled by default on Ubuntu systems, you must stop its service to avoid conflicts with SELinux:Installing SELinux and AuditD
To get started with SELinux, install the SELinux utilities along with the AuditD package. The SELinux package includes essential tools and default security policies, and AuditD logs system events—a critical resource when building custom SELinux policies. Below is a sample installation output. Your output may vary:Verifying and Activating SELinux
To verify whether SELinux is enabled on your system, run:Configuring the Bootloader
After activating SELinux, verify the bootloader settings. In the/etc/default/grub file, you should see the following line instructing the kernel to load SELinux:
Understanding SELinux Modes
SELinux operates in two primary modes:- Permissive: In this mode, SELinux logs policy violations without enforcing them. It is ideal for “training” your policies.
- Enforcing: Here, SELinux actively enforces policies and may deny actions that do not comply with the defined rules.
Although similar audit entries might be repeated, a single occurrence generally provides enough insight into the issue.
Examining Process and File Contexts
You can verify SELinux contexts for running processes and files. For example, to inspect the contexts of SSH daemons, run:sshd_exec_t is executed, SELinux transitions the process into the sshd_t domain, where its type enforcement rules become active.
Generating and Loading a Custom SELinux Policy
After operating in permissive mode and gathering necessary logs, you can generate a custom SELinux policy module from the audit logs. Run:If you see a warning such as “libsemanage.add_user: user sddm not in password file”, it is a known issue. You can safely ignore it if you are not using sddm.
Switching to Enforcing Mode
To temporarily switch SELinux from permissive to enforcing mode, execute:/etc/selinux/config.
Open the configuration file with:
Reviewing the Custom Policy Module
After loading the custom module, two files are created:- A binary package (
mymodule.pp) - A human-readable policy file (
mymodule.te)
.te file contains the type enforcement rules. An excerpt might look like:
Understanding and Reviewing SELinux Type Enforcement
The custom module includes specific rules for thesshd_t domain. For instance, it allows the SSH daemon to access files labeled with var_log_t:
sshd_t domain, such as the SSH daemon, can write to log files like /var/log/auth.log, which are labeled as var_log_t.
You can verify these contexts with the following commands:
Changing SELinux File Contexts with chcon and restorecon
A file’s SELinux context is composed of three parts: user, role, and type. To manually change a file’s context, use thechcon command. For example, to view and modify the context of /var/log/auth.log:
Manual changes made with
chcon may be overridden during a complete filesystem relabel.restorecon command. For example, to fix the context of /var/log/auth.log based on /var/log/dmesg as a reference:
Fixing Contexts for Web Content
If you create a new directory for your website under/var/www and add files, they may initially have an incorrect SELinux type (such as var_t). To correct this recursively:
restorecon -R /var/www/, the files should be relabeled correctly (for example, as httpd_sys_content_t), which is appropriate for web content. Note that restorecon by default only restores the type, not the user or role. If needed, use the force option to restore all parts of the label.
To permanently assign a default label to a specific file (e.g., /var/www/10), add a rule with semanage:
SELinux Booleans and Port Bindings
SELinux booleans act as switches to enable or disable related security policies. To list supported booleans and their current settings, run:virt_use_nfs boolean, execute:
Any warnings regarding the user sddm may be safely ignored if you are not using sddm.