SELinux/Tutorials/How is the policy provided and loaded

How policies are provided and loaded
We have been talking about SELinux policies for a while, and even seen as few of the supported statements (through the output of the sesearch command). Let's have a look at how policies are truly provided and loaded on the system.

SELinux uses policy modules
SELinux borrowed the concept of modules from the Linux kernel and implemented a similar approach for its policies. Just as you can dynamically add in (and remove) driver support in the Linux kernel through kernel modules, you can add in (and remove) policies using SELinux modules.

The list of SELinux modules that are currently loaded on a system can be obtained with semodule -l.

On some distributions, you can even get information about each module through automatically generated man pages, like with man selinux_apache. This isn't the case for Gentoo Linux (yet) though.

Modules can be loaded into and removed from memory using the same semodule tool, which we will see later in this tutorial.

The policy store
SELinux uses a policy store to keep track of its loaded policy modules and related settings. The active policy store name can be obtained from sestatus (look for the Loaded policy name line):

The policy store that should be active upon boot is configured in through the SELINUXTYPE setting.

In the remainder of this tutorial, we will assume that the active policy is called "strict". When you see a command or path that uses "strict" inside, you might need to change that to the policy store name active on your system (which is either targeted, mcs or mls - most non-Gentoo distributions use targeted).

Installing the SELinux policy
Most binary distributions build the SELinux policy in advance, and provide packages that install the binary modules on the file system. Gentoo of course builds the policy through its package management, but in the end, the binary policy modules will be installed on the file system as well. Installing the files on the file system does not enable the policy. Unlike with Linux kernel modules, which can be automatically loaded (if needed) when they are located in the right directory, SELinux requires policy modules to be explicitly loaded (by the administrator or through its configuration).

On most systems, the policy files are stored in. If you take a look in this directory, you will find lots of files with the suffix:

These files are provided by the policy packages (in Gentoo, these are the packages in the sec-policy category). Each of these files might show up in the output of semodule -l except for the file. This is a special one: it is the base policy, comparable to the Linux kernel itself (if we compare modular design) as the kernel itself is also not displayed if you use lsmod.

Loading the SELinux policies
When SELinux is enabled, these files need to be loaded in memory. What happens is that these files are read in (through semodule again) and combined in the active policy. This policy is then loaded in memory (at which point in time the policy takes effect). The full binary policy as it is loaded in memory can be found at and is usually called  where ## is a version telling the management utilities which SELinux features are supported. Most of the time, you don't need to concern you with this version information.

The SELinux modules that are currently loaded are also to be found in the location: they are contained in the  subdirectory. Because they are copied there before loaded, it allows an administrator to verify if the policy modules installed by the package manager (in ) are the ones that are loaded:

Effectively loading the policies in memory is done through the distribution support of SELinux. In Gentoo, this is done at install time of the various sec-policy packages, which calls semodule with the proper arguments.

Similarly, deinstalling the package removes the policy from memory (in this case, the module name is used, not the module file name).

Sometimes an update requires the entire policy (including base policy) to be loaded. SELinux supports full reloads - all you need to do is add in -b base.pp to tell the management utilities where the base policy module can be found. But again, most of the time this is done through the distribution.

Disabling policy modules
You can also opt to disable policy modules. This way, the part of the policy that is provided by the policy module is not active anymore.

What you need to remember
What you should remember from this tutorial is that
 * SELinux uses a modular design for its policies, similar as how the Linux kernel uses modules
 * a policy store is used to keep track of loaded policy modules and settings, and is governed through the SELINUXTYPE setting in
 * the semodule tool is used to load, unload, ... SELinux policies