<def-group>
  <definition class="compliance" id="audit_rules_kernel_module_loading" version="1">
    <metadata>
      <title>Audit Kernel Module Loading and Unloading</title>
      <affected family="unix">
        <platform>Red Hat Enterprise Linux 7</platform>
        <platform>multi_platform_fedora</platform>
      </affected>
      <description>The audit rules should be configured to log information about kernel module loading and unloading.</description>
      <reference source="JL" ref_id="RHEL7_20150325" ref_url="test_attestation" />
      <reference source="JL" ref_id="FEDORA20_20150325" ref_url="test_attestation" />
    </metadata>
    <criteria operator="OR">
      <!-- First check the default configuraton: 'augenrules' tool specified in 'ExecStartPost' directive of
           /usr/lib/systemd/system/auditd.service (in other words case when final /etc/audit/audit.rules
           configuration is generated from *.rules files' content present in the /etc/audit/rules.d directory) -->
      <criteria operator="AND">
        <criterion comment="audit augenrules" test_ref="test_audit_rule_kernel_module_loading_augenrules" />
        <criterion comment="audit augenrules insmod" test_ref="test_audit_rule_kernel_module_loading_insmod_augenrules" />
        <criterion comment="audit augenrules rmmod" test_ref="test_audit_rule_kernel_module_loading_rmmod_augenrules" />
        <criterion comment="audit augenrules modprobe" test_ref="test_audit_rule_kernel_module_loading_modprobe_augenrules" />
        <criterion comment="audit augenrules module syscalls" test_ref="test_audit_rule_kernel_module_loading_syscall_augenrules" />
      </criteria>
      <!-- Check the modified configuration: 'auditctl' tool specified in 'ExecStartPost' directive of
           /usr/lib/systemd/system/auditd.service (in other words case when final /etc/audit/audit.rules is
           just loaded by 'auditctl' tool into memory) -->
      <criteria operator="AND">
        <criterion comment="audit auditctl" test_ref="test_audit_rule_kernel_module_loading_auditctl" />
        <criterion comment="audit auditctl insmod" test_ref="test_audit_rule_kernel_module_loading_insmod_auditctl" />
        <criterion comment="audit auditctl rmmod" test_ref="test_audit_rule_kernel_module_loading_rmmod_auditctl" />
        <criterion comment="audit auditctl modprobe" test_ref="test_audit_rule_kernel_module_loading_modprobe_auditctl" />
        <criterion comment="audit auditctl module syscalls" test_ref="test_audit_rule_kernel_module_loading_syscall_auditctl" />
      </criteria>
    </criteria>
  </definition>

  <!-- Test the augenrules case -->
  <ind:textfilecontent54_test check="all" comment="audit augenrules" id="test_audit_rule_kernel_module_loading_augenrules" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_augenrules" version="1">
    <ind:filepath>/usr/lib/systemd/system/auditd.service</ind:filepath>
    <ind:pattern operation="pattern match">^ExecStartPost=\-\/sbin\/augenrules.*$</ind:pattern>
    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit augenrules insmod" id="test_audit_rule_kernel_module_loading_insmod_augenrules" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_insmod_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_insmod_augenrules" version="1">
    <ind:filepath operation="pattern match">/etc/audit/rules\.d/.*\.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w[\s]+/usr/sbin/insmod[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit augenrules rmmod" id="test_audit_rule_kernel_module_loading_rmmod_augenrules" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_rmmod_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_rmmod_augenrules" version="1">
    <ind:filepath operation="pattern match">/etc/audit/rules\.d/.*\.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w[\s]+/usr/sbin/rmmod[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit augenrules modprobe" id="test_audit_rule_kernel_module_loading_modprobe_augenrules" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_modprobe_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_modprobe_augenrules" version="1">
    <ind:filepath operation="pattern match">/etc/audit/rules\.d/.*\.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w\s+/usr/sbin/modprobe[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit augenrules module syscalls" id="test_audit_rule_kernel_module_loading_syscall_augenrules" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_syscall_augenrules" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_syscall_augenrules" version="1">
    <ind:filepath operation="pattern match">/etc/audit/rules\.d/.*\.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-a\s+always,exit\s+(\-F\s+arch=(b64|b32)\s+)?\-S\s+init_module\s+\-S\s+delete_module\s+\-k\s+[-\w]+\s*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>

  <!-- Test the auditctl case -->
  <ind:textfilecontent54_test check="all" comment="audit auditctl" id="test_audit_rule_kernel_module_loading_auditctl" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_auditctl" version="1">
    <ind:filepath>/usr/lib/systemd/system/auditd.service</ind:filepath>
    <ind:pattern operation="pattern match">^ExecStartPost=\-\/sbin\/auditctl.*$</ind:pattern>
    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit auditctl insmod" id="test_audit_rule_kernel_module_loading_insmod_auditctl" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_insmod_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_insmod_auditctl" version="1">
    <ind:filepath>/etc/audit/audit.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w[\s]+/usr/sbin/insmod[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit auditctl rmmod" id="test_audit_rule_kernel_module_loading_rmmod_auditctl" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_rmmod_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_rmmod_auditctl" version="1">
    <ind:filepath>/etc/audit/audit.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w[\s]+/usr/sbin/rmmod[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit auditctl modprobe" id="test_audit_rule_kernel_module_loading_modprobe_auditctl" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_modprobe_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_modprobe_auditctl" version="1">
    <ind:filepath>/etc/audit/audit.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-w\s+/usr/sbin/modprobe[\s]+\-p[\s]+\b([raw]*x[raw]*)\b[\s]+\-k[\s]+[-\w]+[\s]*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>
  <ind:textfilecontent54_test check="all" comment="audit auditctl module syscalls" id="test_audit_rule_kernel_module_loading_syscall_auditctl" version="1">
    <ind:object object_ref="object_audit_rule_kernel_module_loading_syscall_auditctl" />
  </ind:textfilecontent54_test>
  <ind:textfilecontent54_object id="object_audit_rule_kernel_module_loading_syscall_auditctl" version="1">
    <ind:filepath>/etc/audit/audit.rules</ind:filepath>
    <ind:pattern operation="pattern match">^\-a\s+always,exit\s+(\-F\s+arch=(b64|b32)\s+)?\-S\s+init_module\s+\-S\s+delete_module\s+\-k\s+[-\w]+\s*$</ind:pattern>
    <ind:instance datatype="int">1</ind:instance>
  </ind:textfilecontent54_object>

</def-group>
