Blame SOURCES/scap-security-guide-0.1.61-grub2_template_fix-PR_8180.patch

12e95e
From 1bd88bbdc7ce8b6e2265f323cd3a777ef2240e6b Mon Sep 17 00:00:00 2001
12e95e
From: Matej Tyc <matyc@redhat.com>
12e95e
Date: Fri, 28 Jan 2022 17:11:56 +0100
12e95e
Subject: [PATCH 1/5] Change the grub2 bootloader argument template
12e95e
12e95e
- Introduce the concept of product-specific bootloader config
12e95e
  properties that determine the check/remediation form.
12e95e
- Expand the RHEL8 remediation with a check for update of
12e95e
  /etc/default/grub contents.
12e95e
- Add a RHEL8 check that looks for kernelopts references in loader entries.
12e95e
- Update tests.
12e95e
---
12e95e
 .../grub2_entries_reference_kernelopts.xml    | 25 +++++
12e95e
 .../ansible.template                          | 35 ++++++-
12e95e
 .../grub2_bootloader_argument/bash.template   | 48 +++++++--
12e95e
 .../grub2_bootloader_argument/oval.template   | 97 +++++++++++++------
12e95e
 .../arg_not_there_etcdefaultgrub.fail.sh      |  2 +-
12e95e
 ....fail.sh => arg_not_there_grubenv.fail.sh} |  0
12e95e
 6 files changed, 164 insertions(+), 43 deletions(-)
12e95e
 create mode 100644 shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
 rename shared/templates/grub2_bootloader_argument/tests/{arg_not_there.fail.sh => arg_not_there_grubenv.fail.sh} (100%)
12e95e
12e95e
diff --git a/shared/checks/oval/grub2_entries_reference_kernelopts.xml b/shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
new file mode 100644
12e95e
index 00000000000..1aec9fe64d2
12e95e
--- /dev/null
12e95e
+++ b/shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
@@ -0,0 +1,25 @@
12e95e
+<def-group>
12e95e
+  <definition class="compliance" id="grub2_entries_reference_kernelopts" version="2">
12e95e
+  {{{ oval_metadata(
12e95e
+	  "Ensure that grubenv-defined kernel options are referenced in individual boot loader entries",
12e95e
+	  title="Use $kernelopts in /boot/loader/entries/*.conf",
12e95e
+	  affected_platforms=["multi_platform_all"]) }}}
12e95e
+    <criteria operator="AND">
12e95e
+        
12e95e
+        comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable." />
12e95e
+    </criteria>
12e95e
+  </definition>
12e95e
+
12e95e
+    
12e95e
+                                comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable."
12e95e
+                                check="all" check_existence="all_exist" version="1">
12e95e
+      <ind:object object_ref="object_grub2_entries_reference_kernelopts" />
12e95e
+    </ind:textfilecontent54_test>
12e95e
+
12e95e
+  <ind:textfilecontent54_object id="object_grub2_entries_reference_kernelopts" version="1">
12e95e
+    <ind:path>/boot/loader/entries/</ind:path>
12e95e
+    <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
12e95e
+    <ind:pattern operation="pattern match">^options .*\b\$kernelopts\b.*$</ind:pattern>
12e95e
+    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>                                                                             
12e95e
+  </ind:textfilecontent54_object>
12e95e
+</def-group>
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/ansible.template b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
index 58d4fab69fa..de970879c8f 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
@@ -4,7 +4,34 @@
12e95e
 # complexity = medium
12e95e
 # disruption = low
12e95e
 
12e95e
-{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{#
12e95e
+   See the OVAL template for more comments.
12e95e
+   Product-specific categorization should be synced across all template content types
12e95e
+-#}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
+
12e95e
+{{% if product in ["rhel9"] %}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel8"] %}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if system_with_kernel_options_in_etc_default_grub -%}}
12e95e
 - name: Check {{{ ARG_NAME }}} argument exists
12e95e
   command: grep 'GRUB_CMDLINE_LINUX.*{{{ ARG_NAME }}}=' /etc/default/grub
12e95e
   failed_when: False
12e95e
@@ -27,7 +54,9 @@
12e95e
 - name: Update bootloader menu
12e95e
   command: /sbin/grubby --update-kernel=ALL --args="{{{ ARG_NAME_VALUE }}}"
12e95e
 
12e95e
-{{% else %}}
12e95e
+{{%- endif %}}
12e95e
+
12e95e
+{{% if system_with_kernel_options_in_grubenv -%}}
12e95e
 
12e95e
 - name: Get current kernel parameters
12e95e
   ansible.builtin.shell:
12e95e
@@ -50,4 +79,4 @@
12e95e
   when:
12e95e
     - kernelopts.rc != 0
12e95e
 
12e95e
-{{% endif %}}
12e95e
+{{%- endif %}}
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/bash.template b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
index 631e686897e..817fd1fde23 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/bash.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
@@ -1,6 +1,41 @@
12e95e
 # platform = multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv,multi_platform_ubuntu,multi_platform_sle
12e95e
+{{#
12e95e
+   See the OVAL template for more comments.
12e95e
+   Product-specific categorization should be synced across all template content types
12e95e
+-#}}
12e95e
 
12e95e
-{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
+
12e95e
+{{% if product in ["rhel9"] %}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel8"] %}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
+{{% endif -%}}
12e95e
+
12e95e
+{{% macro update_etc_default_grub(arg_name_value) %}}
12e95e
+{{% if 'ubuntu' in product %}}
12e95e
+update-grub
12e95e
+{{% else %}}
12e95e
+grubby --update-kernel=ALL --args="{{{ arg_name_value }}}"
12e95e
+{{% endif %}}
12e95e
+{{% endmacro -%}}
12e95e
+
12e95e
+{{% if system_with_kernel_options_in_etc_default_grub %}}
12e95e
 {{% if '/' in ARG_NAME %}}
12e95e
 {{{ raise("ARG_NAME (" + ARG_NAME + ") uses sed path separator (/) in " + rule_id) }}}
12e95e
 {{% elif '/' in ARG_NAME_VALUE %}}
12e95e
@@ -14,14 +49,11 @@ else
12e95e
 	# no {{{ ARG_NAME }}}=arg is present, append it
12e95e
 	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/'  '/etc/default/grub'
12e95e
 fi
12e95e
-
12e95e
-{{% if 'ubuntu' in product %}}
12e95e
-update-grub
12e95e
-{{% else %}}
12e95e
-# Correct the form of kernel command line for each installed kernel in the bootloader
12e95e
-grubby --update-kernel=ALL --args="{{{ ARG_NAME_VALUE }}}"
12e95e
 {{% endif %}}
12e95e
-{{% else %}}
12e95e
+
12e95e
+{{{ update_etc_default_grub(ARG_NAME_VALUE) }}}
12e95e
+
12e95e
+{{% if system_with_kernel_options_in_grubenv -%}}
12e95e
 # Correct grub2 kernelopts value using grub2-editenv
12e95e
 existing_kernelopts="$(grub2-editenv - list | grep kernelopts)"
12e95e
 if ! printf '%s' "$existing_kernelopts" | grep -qE '^kernelopts=(.*\s)?{{{ ARG_NAME_VALUE }}}(\s.*)?$'; then
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
index 3ea8acb2910..24258a3bcbd 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/oval.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
@@ -1,15 +1,53 @@
12e95e
+{{#-
12e95e
+  We set defaults to "off", and products should enable relevant ones depending on how the product configures grub.
12e95e
+   - /boot/loader/entries/* may not exist don't exist
12e95e
+   - If they exist, they can reference variables defined in grubenv, or they can contain literal args
12e95e
+   - The grub cfg may either use those loader entries, or it can contain literal values as well
12e95e
+   - Kernel opts can be stored in /etc/default/grub so they are persistent between kernel upgrades
12e95e
+-#}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
+
12e95e
+{{% if product in ["rhel9"] -%}}
12e95e
+{{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
+{{%- endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel8"] -%}}
12e95e
+{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
12e95e
+{{% set system_with_kernel_options_in_grubenv = true %}}
12e95e
+{{%- endif -%}}
12e95e
+
12e95e
+{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product -%}}
12e95e
+{{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
+{{%- endif -%}}
12e95e
+
12e95e
+{{%- if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
+{{%- endif -%}}
12e95e
+
12e95e
 <def-group>
12e95e
   <definition class="compliance" id="{{{ _RULE_ID }}}" version="2">
12e95e
     {{{ oval_metadata("Ensure " + ARG_NAME_VALUE + " is configured in the kernel line in /etc/default/grub.") }}}
12e95e
     <criteria operator="AND">
12e95e
-      {{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
12e95e
-        {{% if product in ['rhel9'] %}}
12e95e
+      {{% if system_with_kernel_options_in_grubenv -%}}
12e95e
+        
12e95e
+        comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_boot_path }}}/grubenv" />
12e95e
+      {{%- endif %}}
12e95e
+      {{% if system_with_referenced_kernel_options_in_loader_entries -%}}
12e95e
+        <extend_definition comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable" definition_ref="grub2_entries_reference_kernelopts" />
12e95e
+      {{%- endif %}}
12e95e
+      {{% if system_with_expanded_kernel_options_in_loader_entries -%}}
12e95e
           
12e95e
                      comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the /boot/loader/entries/*.conf" />
12e95e
-        {{% else %}}
12e95e
+      {{%- endif %}}
12e95e
+      {{% if system_with_expanded_kernel_options_in_grub_cfg -%}}
12e95e
           
12e95e
           comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the {{{ grub2_boot_path }}}/grub.cfg for all kernels" />
12e95e
-        {{% endif %}}
12e95e
+      {{%- endif %}}
12e95e
+      {{% if system_with_kernel_options_in_etc_default_grub -%}}
12e95e
         <criteria operator="OR">
12e95e
           
12e95e
           comment="check for {{{ ARG_NAME_VALUE }}} in /etc/default/grub via GRUB_CMDLINE_LINUX" />
12e95e
@@ -20,14 +58,11 @@
12e95e
             comment="Check GRUB_DISABLE_RECOVERY=true in /etc/default/grub" />
12e95e
           </criteria>
12e95e
         </criteria>
12e95e
-      {{% else %}}
12e95e
-        
12e95e
-        comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_boot_path }}}/grubenv" />
12e95e
-      {{% endif %}}
12e95e
+      {{%- endif %}}
12e95e
     </criteria>
12e95e
   </definition>
12e95e
 
12e95e
-{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
12e95e
+{{%- if system_with_kernel_options_in_etc_default_grub %}}
12e95e
   
12e95e
   comment="check for {{{ ARG_NAME_VALUE }}} in /etc/default/grub via GRUB_CMDLINE_LINUX"
12e95e
   check="all" check_existence="all_exist" version="1">
12e95e
@@ -54,8 +89,25 @@
12e95e
     <ind:pattern operation="pattern match">^\s*GRUB_CMDLINE_LINUX_DEFAULT="(.*)"$</ind:pattern>
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
   </ind:textfilecontent54_object>
12e95e
+{{%- endif %}}
12e95e
+
12e95e
+{{%- if system_with_kernel_options_in_grubenv %}}
12e95e
+  
12e95e
+  comment="check for kernel command line parameters {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grubenv for all kernels"
12e95e
+  check="all" check_existence="all_exist" version="1">
12e95e
+    <ind:object object_ref="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_env" />
12e95e
+    <ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
12e95e
+  </ind:textfilecontent54_test>
12e95e
 
12e95e
-  {{% if product in ["rhel9"] %}}
12e95e
+  
12e95e
+  version="1">
12e95e
+    <ind:filepath>{{{ grub2_boot_path }}}/grubenv</ind:filepath>
12e95e
+    <ind:pattern operation="pattern match">^kernelopts=(.*)$</ind:pattern>
12e95e
+    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
+  </ind:textfilecontent54_object>
12e95e
+{{%- endif %}}
12e95e
+
12e95e
+{{%- if system_with_expanded_kernel_options_in_loader_entries %}}
12e95e
     
12e95e
                                 comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} for all boot entries."
12e95e
                                 check="all" check_existence="all_exist" version="1">
12e95e
@@ -69,7 +121,9 @@
12e95e
     <ind:pattern operation="pattern match">^options (.*)$</ind:pattern>
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
   </ind:textfilecontent54_object>
12e95e
-  {{% else %}}
12e95e
+{{%- endif %}}
12e95e
+
12e95e
+{{%- if system_with_expanded_kernel_options_in_grub_cfg %}}
12e95e
   
12e95e
   comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grub.cfg for all kernels"
12e95e
   check="all" check_existence="all_exist" version="1">
12e95e
@@ -87,26 +141,7 @@
12e95e
     {{% endif %}}
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
   </ind:textfilecontent54_object>
12e95e
-
12e95e
-  {{% endif %}}
12e95e
-
12e95e
-{{% else %}}
12e95e
-
12e95e
-  
12e95e
-  comment="check forkernel command line parameters {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grubenv for all kernels"
12e95e
-  check="all" check_existence="all_exist" version="1">
12e95e
-    <ind:object object_ref="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_env" />
12e95e
-    <ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
12e95e
-  </ind:textfilecontent54_test>
12e95e
-
12e95e
-  
12e95e
-  version="1">
12e95e
-    <ind:filepath>{{{ grub2_boot_path }}}/grubenv</ind:filepath>
12e95e
-    <ind:pattern operation="pattern match">^kernelopts=(.*)$</ind:pattern>
12e95e
-    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
-  </ind:textfilecontent54_object>
12e95e
-
12e95e
-{{% endif %}}
12e95e
+{{%- endif %}}
12e95e
 
12e95e
   
12e95e
   version="1">
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
12e95e
index a56e6d09235..a270be45952 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
12e95e
+++ b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
12e95e
@@ -1,6 +1,6 @@
12e95e
 #!/bin/bash
12e95e
 
12e95e
-# platform = Red Hat Enterprise Linux 7,Red Hat Enterprise Linux 9
12e95e
+# platform = Red Hat Enterprise Linux 7,Red Hat Enterprise Linux 8,Red Hat Enterprise Linux 9
12e95e
 
12e95e
 # Removes argument from kernel command line in /etc/default/grub
12e95e
 if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=.*"'  '/etc/default/grub' ; then
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/tests/arg_not_there.fail.sh b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_grubenv.fail.sh
12e95e
similarity index 100%
12e95e
rename from shared/templates/grub2_bootloader_argument/tests/arg_not_there.fail.sh
12e95e
rename to shared/templates/grub2_bootloader_argument/tests/arg_not_there_grubenv.fail.sh
12e95e
12e95e
From 0d10bf751d5e1d7f024cd7301f8b02b38c0e3b9c Mon Sep 17 00:00:00 2001
12e95e
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
12e95e
Date: Wed, 9 Feb 2022 11:19:06 +0100
12e95e
Subject: [PATCH 2/5] Change the default product setting
12e95e
12e95e
Assume that every product stores kernel opts in the /etc/default/grub
12e95e
---
12e95e
 shared/templates/grub2_bootloader_argument/ansible.template | 6 +-----
12e95e
 shared/templates/grub2_bootloader_argument/bash.template    | 6 +-----
12e95e
 shared/templates/grub2_bootloader_argument/oval.template    | 6 +-----
12e95e
 3 files changed, 3 insertions(+), 15 deletions(-)
12e95e
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/ansible.template b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
index de970879c8f..46de9b465c2 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
@@ -11,7 +11,7 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
 
12e95e
 {{% if product in ["rhel9"] %}}
12e95e
@@ -27,10 +27,6 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
 {{% endif -%}}
12e95e
 
12e95e
-{{% if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
 {{% if system_with_kernel_options_in_etc_default_grub -%}}
12e95e
 - name: Check {{{ ARG_NAME }}} argument exists
12e95e
   command: grep 'GRUB_CMDLINE_LINUX.*{{{ ARG_NAME }}}=' /etc/default/grub
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/bash.template b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
index 817fd1fde23..b188d1e3689 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/bash.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
@@ -7,7 +7,7 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
 
12e95e
 {{% if product in ["rhel9"] %}}
12e95e
@@ -23,10 +23,6 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
 {{% endif -%}}
12e95e
 
12e95e
-{{% if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
 {{% macro update_etc_default_grub(arg_name_value) %}}
12e95e
 {{% if 'ubuntu' in product %}}
12e95e
 update-grub
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
index 24258a3bcbd..88fa7b7a3ee 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/oval.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
@@ -8,7 +8,7 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
 {{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = false -%}}
12e95e
+{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
 
12e95e
 {{% if product in ["rhel9"] -%}}
12e95e
@@ -24,10 +24,6 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
 {{%- endif -%}}
12e95e
 
12e95e
-{{%- if product in ["rhel7", "ol7", "rhel8", "ol8", "rhel9"] or 'ubuntu' in product %}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = true %}}
12e95e
-{{%- endif -%}}
12e95e
-
12e95e
 <def-group>
12e95e
   <definition class="compliance" id="{{{ _RULE_ID }}}" version="2">
12e95e
     {{{ oval_metadata("Ensure " + ARG_NAME_VALUE + " is configured in the kernel line in /etc/default/grub.") }}}
12e95e
12e95e
From fac0aeb351d7acab1112482d11a0be73df662496 Mon Sep 17 00:00:00 2001
12e95e
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
12e95e
Date: Fri, 11 Feb 2022 14:55:53 +0100
12e95e
Subject: [PATCH 3/5] Improve the template further
12e95e
12e95e
- Fix the $kernelopts regex - $ is not a word char.
12e95e
- Use grubby exclusively on RHEL systems and structure remediations differently than OVAL checks
12e95e
- Exclude the rescue.conf loader entry from checks, as it is not a boot entry for general use.
12e95e
---
12e95e
 .../grub2_entries_reference_kernelopts.xml    |  2 +-
12e95e
 .../ansible.template                          | 72 +------------------
12e95e
 .../grub2_bootloader_argument/bash.template   | 67 +++++------------
12e95e
 .../grub2_bootloader_argument/oval.template   |  7 +-
12e95e
 .../tests/invalid_rescue.pass.sh              |  6 ++
12e95e
 tests/test_rule_in_container.sh               |  2 +-
12e95e
 6 files changed, 33 insertions(+), 123 deletions(-)
12e95e
 create mode 100644 shared/templates/grub2_bootloader_argument/tests/invalid_rescue.pass.sh
12e95e
12e95e
diff --git a/shared/checks/oval/grub2_entries_reference_kernelopts.xml b/shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
index 1aec9fe64d2..30f3965a037 100644
12e95e
--- a/shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
+++ b/shared/checks/oval/grub2_entries_reference_kernelopts.xml
12e95e
@@ -19,7 +19,7 @@
12e95e
   <ind:textfilecontent54_object id="object_grub2_entries_reference_kernelopts" version="1">
12e95e
     <ind:path>/boot/loader/entries/</ind:path>
12e95e
     <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
12e95e
-    <ind:pattern operation="pattern match">^options .*\b\$kernelopts\b.*$</ind:pattern>
12e95e
+    <ind:pattern operation="pattern match">^options(?:\s+.*)?\s+\$kernelopts\b.*$</ind:pattern>
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>                                                                             
12e95e
   </ind:textfilecontent54_object>
12e95e
 </def-group>
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/ansible.template b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
index 46de9b465c2..db3b4430d4b 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/ansible.template
12e95e
@@ -4,75 +4,5 @@
12e95e
 # complexity = medium
12e95e
 # disruption = low
12e95e
 
12e95e
-{{#
12e95e
-   See the OVAL template for more comments.
12e95e
-   Product-specific categorization should be synced across all template content types
12e95e
--#}}
12e95e
-{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
-{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
-{{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
-{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
-
12e95e
-{{% if product in ["rhel9"] %}}
12e95e
-{{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% if product in ["rhel8"] %}}
12e95e
-{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
12e95e
-{{% set system_with_kernel_options_in_grubenv = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
12e95e
-{{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% if system_with_kernel_options_in_etc_default_grub -%}}
12e95e
-- name: Check {{{ ARG_NAME }}} argument exists
12e95e
-  command: grep 'GRUB_CMDLINE_LINUX.*{{{ ARG_NAME }}}=' /etc/default/grub
12e95e
-  failed_when: False
12e95e
-  register: argcheck
12e95e
-
12e95e
-- name: Replace existing {{{ ARG_NAME }}} argument
12e95e
-  replace:
12e95e
-      path: /etc/default/grub
12e95e
-      regexp: '{{{ ARG_NAME }}}=\w+'
12e95e
-      replace: '{{{ ARG_NAME_VALUE }}}'
12e95e
-  when: argcheck.rc == 0
12e95e
-
12e95e
-- name: Add {{{ ARG_NAME }}} argument
12e95e
-  replace:
12e95e
-      path: /etc/default/grub
12e95e
-      regexp: '(GRUB_CMDLINE_LINUX=.*)"'
12e95e
-      replace: '\1 {{{ ARG_NAME_VALUE }}}"'
12e95e
-  when: argcheck.rc != 0
12e95e
-
12e95e
-- name: Update bootloader menu
12e95e
+- name: Update grub defaults and the bootloader menu
12e95e
   command: /sbin/grubby --update-kernel=ALL --args="{{{ ARG_NAME_VALUE }}}"
12e95e
-
12e95e
-{{%- endif %}}
12e95e
-
12e95e
-{{% if system_with_kernel_options_in_grubenv -%}}
12e95e
-
12e95e
-- name: Get current kernel parameters
12e95e
-  ansible.builtin.shell:
12e95e
-    cmd: '/usr/bin/grub2-editenv - list | grep "kernelopts="'
12e95e
-  register: kernelopts
12e95e
-  ignore_errors: yes
12e95e
-  changed_when: False
12e95e
-
12e95e
-- name: Update the bootloader menu
12e95e
-  command: /usr/bin/grub2-editenv - set "{{ item }} {{{ ARG_NAME_VALUE }}}"
12e95e
-  with_items: "{{ kernelopts.stdout_lines | select('match', '^kernelopts.*') | list }}"
12e95e
-  when:
12e95e
-    - kernelopts.rc == 0
12e95e
-    - kernelopts.stdout_lines is defined
12e95e
-    - kernelopts.stdout_lines | length > 0
12e95e
-    - kernelopts.stdout | regex_search('^kernelopts=(?:.*\s)?{{{ ARG_NAME_VALUE }}}(?:\s.*)?$', multiline=True) is none
12e95e
-
12e95e
-- name: Update the bootloader menu when there are no entries previously set
12e95e
-  command: /usr/bin/grub2-editenv - set "kernelopts={{{ ARG_NAME_VALUE }}}"
12e95e
-  when:
12e95e
-    - kernelopts.rc != 0
12e95e
-
12e95e
-{{%- endif %}}
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/bash.template b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
index b188d1e3689..5f97efd498f 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/bash.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/bash.template
12e95e
@@ -4,59 +4,28 @@
12e95e
    Product-specific categorization should be synced across all template content types
12e95e
 -#}}
12e95e
 
12e95e
-{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
12e95e
-{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
12e95e
-{{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
-{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
-{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
+{{% set grub_helper_executable = "grubby" -%}}
12e95e
+{{% set grub_helper_args = ["--update-kernel=ALL", "--args=" ~ ARG_NAME_VALUE] -%}}
12e95e
 
12e95e
-{{% if product in ["rhel9"] %}}
12e95e
-{{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% if product in ["rhel8"] %}}
12e95e
-{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
12e95e
-{{% set system_with_kernel_options_in_grubenv = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
12e95e
-{{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
-{{% endif -%}}
12e95e
-
12e95e
-{{% macro update_etc_default_grub(arg_name_value) %}}
12e95e
-{{% if 'ubuntu' in product %}}
12e95e
-update-grub
12e95e
-{{% else %}}
12e95e
-grubby --update-kernel=ALL --args="{{{ arg_name_value }}}"
12e95e
-{{% endif %}}
12e95e
-{{% endmacro -%}}
12e95e
-
12e95e
-{{% if system_with_kernel_options_in_etc_default_grub %}}
12e95e
-{{% if '/' in ARG_NAME %}}
12e95e
-{{{ raise("ARG_NAME (" + ARG_NAME + ") uses sed path separator (/) in " + rule_id) }}}
12e95e
-{{% elif '/' in ARG_NAME_VALUE %}}
12e95e
-{{{ raise("ARG_NAME_VALUE (" + ARG_NAME_VALUE + ") uses sed path separator (/) in " + rule_id) }}}
12e95e
-{{% endif %}}
12e95e
+{{%- macro update_etc_default_grub_manually() -%}}
12e95e
 # Correct the form of default kernel command line in GRUB
12e95e
 if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ ARG_NAME }}}=.*"'  '/etc/default/grub' ; then
12e95e
-	# modify the GRUB command-line if an {{{ ARG_NAME }}}= arg already exists
12e95e
-	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/'  '/etc/default/grub'
12e95e
+       # modify the GRUB command-line if an {{{ ARG_NAME }}}= arg already exists
12e95e
+       sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/'  '/etc/default/grub'
12e95e
 else
12e95e
-	# no {{{ ARG_NAME }}}=arg is present, append it
12e95e
-	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/'  '/etc/default/grub'
12e95e
+       # no {{{ ARG_NAME }}}=arg is present, append it
12e95e
+       sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/'  '/etc/default/grub'
12e95e
 fi
12e95e
-{{% endif %}}
12e95e
+{{%- endmacro %}}
12e95e
+
12e95e
+{{% if 'ubuntu' in product %}}
12e95e
+{{{ update_etc_default_grub_manually() }}}
12e95e
+{{% set grub_helper_executable = "update-grub" -%}}
12e95e
+{{% endif -%}}
12e95e
 
12e95e
-{{{ update_etc_default_grub(ARG_NAME_VALUE) }}}
12e95e
+{{% if product in ["rhel8", "ol8"] %}}
12e95e
+{{# Suppress the None output of append -#}}
12e95e
+{{{ grub_helper_args.append("--env=/boot/grub2/grubenv") or "" -}}}
12e95e
+{{% endif -%}}
12e95e
 
12e95e
-{{% if system_with_kernel_options_in_grubenv -%}}
12e95e
-# Correct grub2 kernelopts value using grub2-editenv
12e95e
-existing_kernelopts="$(grub2-editenv - list | grep kernelopts)"
12e95e
-if ! printf '%s' "$existing_kernelopts" | grep -qE '^kernelopts=(.*\s)?{{{ ARG_NAME_VALUE }}}(\s.*)?$'; then
12e95e
-  if test -n "$existing_kernelopts"; then
12e95e
-    grub2-editenv - set "$existing_kernelopts {{{ ARG_NAME_VALUE }}}"
12e95e
-  else
12e95e
-    grub2-editenv - set "kernelopts={{{ ARG_NAME_VALUE }}}"
12e95e
-  fi
12e95e
-fi
12e95e
-{{% endif %}}
12e95e
+{{{ grub_helper_executable }}} {{{ " ".join(grub_helper_args) }}}
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
index 88fa7b7a3ee..6981cc14045 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/oval.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
@@ -1,6 +1,6 @@
12e95e
 {{#-
12e95e
   We set defaults to "off", and products should enable relevant ones depending on how the product configures grub.
12e95e
-   - /boot/loader/entries/* may not exist don't exist
12e95e
+   - /boot/loader/entries/* may not exist.
12e95e
    - If they exist, they can reference variables defined in grubenv, or they can contain literal args
12e95e
    - The grub cfg may either use those loader entries, or it can contain literal values as well
12e95e
    - Kernel opts can be stored in /etc/default/grub so they are persistent between kernel upgrades
12e95e
@@ -116,7 +116,12 @@
12e95e
     <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
12e95e
     <ind:pattern operation="pattern match">^options (.*)$</ind:pattern>
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
+    <filter action="exclude">state_grub2_rescue_entry_for_{{{ _RULE_ID }}}</filter>
12e95e
   </ind:textfilecontent54_object>
12e95e
+
12e95e
+  <ind:textfilecontent54_state id="state_grub2_rescue_entry_for_{{{ _RULE_ID }}}" version="1">
12e95e
+    <ind:filename>rescue.conf</ind:filename>
12e95e
+  </ind:textfilecontent54_state>
12e95e
 {{%- endif %}}
12e95e
 
12e95e
 {{%- if system_with_expanded_kernel_options_in_grub_cfg %}}
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/tests/invalid_rescue.pass.sh b/shared/templates/grub2_bootloader_argument/tests/invalid_rescue.pass.sh
12e95e
new file mode 100644
12e95e
index 00000000000..ee6e2c67f34
12e95e
--- /dev/null
12e95e
+++ b/shared/templates/grub2_bootloader_argument/tests/invalid_rescue.pass.sh
12e95e
@@ -0,0 +1,6 @@
12e95e
+# platform = Red Hat Enterprise Linux 7,Red Hat Enterprise Linux 9
12e95e
+# packages = grub2,grubby
12e95e
+
12e95e
+{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
12e95e
+
12e95e
+echo "I am an invalid boot entry, but nobody should care, because I am rescue" > /boot/loader/entries/trololol-rescue.conf
12e95e
diff --git a/tests/test_rule_in_container.sh b/tests/test_rule_in_container.sh
12e95e
index 395fc4e856c..a8691ca7463 100755
12e95e
--- a/tests/test_rule_in_container.sh
12e95e
+++ b/tests/test_rule_in_container.sh
12e95e
@@ -221,7 +221,7 @@ additional_args=()
12e95e
 test "$_arg_dontclean" = on && additional_args+=(--dontclean)
12e95e
 
12e95e
 # Don't act on the default value.
12e95e
-test -n "$_arg_scenarios" && additional_args+=(--scenario "'$_arg_scenarios'")
12e95e
+test -n "$_arg_scenarios" && additional_args+=(--scenario "$_arg_scenarios")
12e95e
 
12e95e
 test -n "$_arg_datastream" && additional_args+=(--datastream "$_arg_datastream")
12e95e
 
12e95e
12e95e
From 8dda6030dea885c7c7e7e8f1024f5f2edf5bc36c Mon Sep 17 00:00:00 2001
12e95e
From: Matej Tyc <matyc@redhat.com>
12e95e
Date: Mon, 14 Feb 2022 13:45:09 +0100
12e95e
Subject: [PATCH 4/5] Add support for checks of both BIOS/UEFI systems
12e95e
12e95e
---
12e95e
 .../grub2_bootloader_argument/oval.template   | 57 +++++++++++++++----
12e95e
 1 file changed, 46 insertions(+), 11 deletions(-)
12e95e
12e95e
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
index 6981cc14045..71367465663 100644
12e95e
--- a/shared/templates/grub2_bootloader_argument/oval.template
12e95e
+++ b/shared/templates/grub2_bootloader_argument/oval.template
12e95e
@@ -10,6 +10,7 @@
12e95e
 {{% set system_with_kernel_options_in_grubenv = false -%}}
12e95e
 {{% set system_with_kernel_options_in_etc_default_grub = true -%}}
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
12e95e
+{{% set system_with_bios_and_uefi_support = false -%}}
12e95e
 
12e95e
 {{% if product in ["rhel9"] -%}}
12e95e
 {{% set system_with_expanded_kernel_options_in_loader_entries = true %}}
12e95e
@@ -24,13 +25,25 @@
12e95e
 {{% set system_with_expanded_kernel_options_in_grub_cfg = true %}}
12e95e
 {{%- endif -%}}
12e95e
 
12e95e
+{{% if grub2_uefi_boot_path and grub2_uefi_boot_path != grub2_boot_path -%}}
12e95e
+{{% set system_with_bios_and_uefi_support = true %}}
12e95e
+{{%- endif -%}}
12e95e
+
12e95e
 <def-group>
12e95e
   <definition class="compliance" id="{{{ _RULE_ID }}}" version="2">
12e95e
     {{{ oval_metadata("Ensure " + ARG_NAME_VALUE + " is configured in the kernel line in /etc/default/grub.") }}}
12e95e
     <criteria operator="AND">
12e95e
       {{% if system_with_kernel_options_in_grubenv -%}}
12e95e
+      {{% if system_with_bios_and_uefi_support -%}}
12e95e
+      <criteria operator="OR">
12e95e
+      {{%- endif %}}
12e95e
         
12e95e
         comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_boot_path }}}/grubenv" />
12e95e
+      {{% if system_with_bios_and_uefi_support -%}}
12e95e
+        
12e95e
+        comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_uefi_boot_path }}}/grubenv" />
12e95e
+      </criteria>
12e95e
+      {{%- endif %}}
12e95e
       {{%- endif %}}
12e95e
       {{% if system_with_referenced_kernel_options_in_loader_entries -%}}
12e95e
         <extend_definition comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable" definition_ref="grub2_entries_reference_kernelopts" />
12e95e
@@ -40,8 +53,16 @@
12e95e
                      comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the /boot/loader/entries/*.conf" />
12e95e
       {{%- endif %}}
12e95e
       {{% if system_with_expanded_kernel_options_in_grub_cfg -%}}
12e95e
+          {{% if system_with_bios_and_uefi_support -%}}
12e95e
+          <criteria operator="OR">
12e95e
+          {{%- endif %}}
12e95e
           
12e95e
           comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the {{{ grub2_boot_path }}}/grub.cfg for all kernels" />
12e95e
+          {{% if system_with_bios_and_uefi_support -%}}
12e95e
+          
12e95e
+          comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the {{{ grub2_uefi_boot_path }}}/grub.cfg for all kernels" />
12e95e
+          </criteria>
12e95e
+          {{%- endif %}}
12e95e
       {{%- endif %}}
12e95e
       {{% if system_with_kernel_options_in_etc_default_grub -%}}
12e95e
         <criteria operator="OR">
12e95e
@@ -88,19 +109,26 @@
12e95e
 {{%- endif %}}
12e95e
 
12e95e
 {{%- if system_with_kernel_options_in_grubenv %}}
12e95e
-  
12e95e
-  comment="check for kernel command line parameters {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grubenv for all kernels"
12e95e
+{{%- macro test_and_object_for_kernel_options_grub_env(base_name, path) %}}
12e95e
+  
12e95e
+  comment="check for kernel command line parameters {{{ ARG_NAME_VALUE }}} in {{{ path }}} for all kernels"
12e95e
   check="all" check_existence="all_exist" version="1">
12e95e
-    <ind:object object_ref="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_env" />
12e95e
+    <ind:object object_ref="object_{{{ base_name }}}" />
12e95e
     <ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
12e95e
   </ind:textfilecontent54_test>
12e95e
 
12e95e
-  
12e95e
+  
12e95e
   version="1">
12e95e
-    <ind:filepath>{{{ grub2_boot_path }}}/grubenv</ind:filepath>
12e95e
+    <ind:filepath>{{{ path }}}</ind:filepath>
12e95e
     <ind:pattern operation="pattern match">^kernelopts=(.*)$</ind:pattern>
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
   </ind:textfilecontent54_object>
12e95e
+{{%- endmacro %}}
12e95e
+
12e95e
+{{{ test_and_object_for_kernel_options_grub_env("grub2_" ~ SANITIZED_ARG_NAME ~ "_argument_grub_env", grub2_boot_path ~ "/grubenv") }}}
12e95e
+{{% if system_with_bios_and_uefi_support -%}}
12e95e
+{{{ test_and_object_for_kernel_options_grub_env("grub2_" ~ SANITIZED_ARG_NAME ~ "_argument_grub_env_uefi", grub2_uefi_boot_path ~ "/grubenv") }}}
12e95e
+{{%- endif %}}
12e95e
 {{%- endif %}}
12e95e
 
12e95e
 {{%- if system_with_expanded_kernel_options_in_loader_entries %}}
12e95e
@@ -120,21 +148,22 @@
12e95e
   </ind:textfilecontent54_object>
12e95e
 
12e95e
   <ind:textfilecontent54_state id="state_grub2_rescue_entry_for_{{{ _RULE_ID }}}" version="1">
12e95e
-    <ind:filename>rescue.conf</ind:filename>
12e95e
+    <ind:filename operation="pattern match">.*rescue.conf$</ind:filename>
12e95e
   </ind:textfilecontent54_state>
12e95e
 {{%- endif %}}
12e95e
 
12e95e
 {{%- if system_with_expanded_kernel_options_in_grub_cfg %}}
12e95e
-  
12e95e
-  comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grub.cfg for all kernels"
12e95e
+{{%- macro test_and_object_for_kernel_options_grub_cfg(base_name, path) %}}
12e95e
+  
12e95e
+  comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} in {{{ path }}} for all kernels"
12e95e
   check="all" check_existence="all_exist" version="1">
12e95e
-    <ind:object object_ref="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_cfg" />
12e95e
+    <ind:object object_ref="object_{{{ base_name }}}" />
12e95e
     <ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
12e95e
   </ind:textfilecontent54_test>
12e95e
 
12e95e
-  
12e95e
+  
12e95e
   version="1">
12e95e
-    <ind:filepath>{{{ grub2_boot_path }}}/grub.cfg</ind:filepath>
12e95e
+    <ind:filepath>{{{ path }}}</ind:filepath>
12e95e
     {{% if product in ["rhel7"] or 'ubuntu' in product %}}
12e95e
       <ind:pattern operation="pattern match">^.*/vmlinuz.*(root=.*)$</ind:pattern>
12e95e
     {{% else %}}
12e95e
@@ -142,6 +171,12 @@
12e95e
     {{% endif %}}
12e95e
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
12e95e
   </ind:textfilecontent54_object>
12e95e
+{{%- endmacro %}}
12e95e
+
12e95e
+{{{ test_and_object_for_kernel_options_grub_cfg("grub2_" + SANITIZED_ARG_NAME + "_argument_grub_cfg", grub2_boot_path ~ "/grub.cfg") }}}
12e95e
+{{% if system_with_bios_and_uefi_support -%}}
12e95e
+{{{ test_and_object_for_kernel_options_grub_cfg("grub2_" + SANITIZED_ARG_NAME + "_argument_grub_cfg_uefi", grub2_uefi_boot_path ~ "/grub.cfg") }}}
12e95e
+{{%- endif %}}
12e95e
 {{%- endif %}}
12e95e
 
12e95e
   
12e95e
12e95e
From df44b5d7017328d05c0671397edcfed019a2a448 Mon Sep 17 00:00:00 2001
12e95e
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
12e95e
Date: Mon, 14 Feb 2022 14:49:34 +0100
12e95e
Subject: [PATCH 5/5] Correct test scenario metadata
12e95e
12e95e
- Grubenv doesn't relate to anything else than RHEL8
12e95e
- The grubby remediation has different behavior in corner-cases
12e95e
  that are technically unsupported, so the corresponding test scenario has been dropped.
12e95e
---
12e95e
 .../grub2_audit_argument/tests/blank_grubenv_rhel8.fail.sh      | 1 +
12e95e
 .../auditing/grub2_audit_argument/tests/correct_grubenv.pass.sh | 2 +-
12e95e
 2 files changed, 2 insertions(+), 1 deletion(-)
12e95e
12e95e
diff --git a/linux_os/guide/system/auditing/grub2_audit_argument/tests/blank_grubenv_rhel8.fail.sh b/linux_os/guide/system/auditing/grub2_audit_argument/tests/blank_grubenv_rhel8.fail.sh
12e95e
index 5af2acc317e..956c8ac79fd 100644
12e95e
--- a/linux_os/guide/system/auditing/grub2_audit_argument/tests/blank_grubenv_rhel8.fail.sh
12e95e
+++ b/linux_os/guide/system/auditing/grub2_audit_argument/tests/blank_grubenv_rhel8.fail.sh
12e95e
@@ -1,6 +1,7 @@
12e95e
 #!/bin/bash
12e95e
 
12e95e
 # platform = Red Hat Enterprise Linux 8
12e95e
+# remediation = none
12e95e
 
12e95e
 # Removes audit argument from kernel command line in /boot/grub2/grubenv
12e95e
 file="/boot/grub2/grubenv"
12e95e
diff --git a/linux_os/guide/system/auditing/grub2_audit_argument/tests/correct_grubenv.pass.sh b/linux_os/guide/system/auditing/grub2_audit_argument/tests/correct_grubenv.pass.sh
12e95e
index 0ec9a1d6e38..9823b08dff9 100644
12e95e
--- a/linux_os/guide/system/auditing/grub2_audit_argument/tests/correct_grubenv.pass.sh
12e95e
+++ b/linux_os/guide/system/auditing/grub2_audit_argument/tests/correct_grubenv.pass.sh
12e95e
@@ -1,4 +1,4 @@
12e95e
 #!/bin/bash
12e95e
-# platform = Red Hat Enterprise Linux 8,Red Hat Enterprise Linux 9
12e95e
+# platform = Red Hat Enterprise Linux 8
12e95e
 
12e95e
 grub2-editenv - set "$(grub2-editenv - list | grep kernelopts) audit=1"