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-console and audit packages for verification.

Procedure

  1. Open a new .cil file with a text editor, for example:

    # vi <local_module>.cil

    To keep your local modules better organized, use the local_ prefix in the names of local SELinux policy modules.

  2. 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;
  3. Save and close the file.
  4. Install the policy module:

    # semodule -i <local_module>.cil

    If you want to remove a local policy module you created by using semodule -i, refer to the module name without the .cil suffix. To remove a local policy module, use semodule -r <local_module>.

  5. Restart any services related to the rules:

    # systemctl restart <service_name>

Verification

  1. List the local modules installed in your SELinux policy:

    # semodule -lfull | grep "local_"
    400 local_module  cil

    Because local modules have priority 400, you can filter them from the list also by using that value, for example, by using the semodule -lfull | grep -v ^100 command.

  2. 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 read operation.

  3. Verify that the relevant service runs confined by SELinux:

    1. Identify the process related to the relevant service:

      $ systemctl status <service_name>
    2. Check the SELinux context of the process listed in the output of the previous command:

      $ ps -efZ | grep <process_name>
  4. Verify that the service does not cause any SELinux denials:

    # ausearch -m AVC -i -ts recent
    <no matches>

    The -i option interprets the numeric values into human-readable text.

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部