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

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