5.4. Creating a local SELinux policy module
Adding specific SELinux policy modules to an active SELinux policy can fix certain problems with the SELinux policy. You can use this procedure to fix a specific Known Issue described in Red Hat release notes or to implement a particular Red Hat Solution.
Use only rules provided by Red Hat. Red Hat does not support creating SELinux policy modules with custom rules because this falls outside of the Production Support Scope of Coverage. If you are not an expert, contact your Red Hat sales representative and request consulting services.
Prerequisites
-
The
setools-consoleandauditpackages for verification.
Procedure
Open a new
.cilfile with a text editor, for example:# vi <local_module>.cilTo keep your local modules better organized, use the
local_prefix in the names of local SELinux policy modules.Insert the custom rules from a Known Issue or a Red Hat Solution.
중요Do not write your own rules. Use only the rules provided in a specific Known Issue or Red Hat Solution.
For example, to resolve SELinux denies cups-lpd read access to cups.sock in RHEL, insert the following rule:
(allow cupsd_lpd_t cupsd_var_run_t (sock_file (read)))The example solution has been fixed permanently for {ProductShortName} in RHBA-2021:4420. Therefore, the parts of this procedure specific to this solution do not affect updated {ProductShortName} 8 and 9 systems, and are included only as examples of syntax.
You can use either of the two SELinux rule syntaxes, Common Intermediate Language (CIL) and m4. For example,
(allow cupsd_lpd_t cupsd_var_run_t (sock_file (read)))in CIL is equivalent to the following in m4:module local_cupslpd-read-cupssock 1.0; require { type cupsd_var_run_t; type cupsd_lpd_t; class sock_file read; } #============= cupsd_lpd_t ============== allow cupsd_lpd_t cupsd_var_run_t:sock_file read;
- Save and close the file.
Install the policy module:
# semodule -i <local_module>.cilIf you want to remove a local policy module you created by using
semodule -i, refer to the module name without the.cilsuffix. To remove a local policy module, usesemodule -r <local_module>.Restart any services related to the rules:
# systemctl restart <service_name>
Verification
List the local modules installed in your SELinux policy:
# semodule -lfull | grep "local_" 400 local_module cilBecause local modules have priority
400, you can filter them from the list also by using that value, for example, by using thesemodule -lfull | grep -v ^100command.Search the SELinux policy for the relevant allow rules:
# sesearch -A --source=<SOURCENAME> --target=<TARGETNAME> --class=<CLASSNAME> --perm=<P1>,<P2>Where
<SOURCENAME>is the source SELinux type,<TARGETNAME>is the target SELinux type,<CLASSNAME>is the security class or object class name, and<P1>and<P2>are the specific permissions of the rule.For example, to resolve SELinux denies cups-lpd read access to cups.sock in RHEL:
# sesearch -A --source=cupsd_lpd_t --target=cupsd_var_run_t --class=sock_file --perm=read allow cupsd_lpd_t cupsd_var_run_t:sock_file { append getattr open read write };The last line should now include the
readoperation.Verify that the relevant service runs confined by SELinux:
Identify the process related to the relevant service:
$ systemctl status <service_name>Check the SELinux context of the process listed in the output of the previous command:
$ ps -efZ | grep <process_name>
Verify that the service does not cause any SELinux denials:
# ausearch -m AVC -i -ts recent <no matches>The
-ioption interprets the numeric values into human-readable text.