Blame SOURCES/scap-security-guide-0.1.60-rhel9_stig_grub-PR_7931.patch

ff1465
commit 2e1eeff365be8fde302620fae6691ccc523f6f9e
ff1465
Author: Gabriel Becker <ggasparb@redhat.com>
ff1465
Date:   Thu Feb 24 18:19:45 2022 +0100
ff1465
ff1465
    Manual edited patch scap-security-guide-0.1.60-rhel9_stig_grub-PR_7931.patch.
ff1465
ff1465
diff --git a/linux_os/guide/system/bootloader-grub2/grub2_kernel_trust_cpu_rng/oval/shared.xml b/linux_os/guide/system/bootloader-grub2/grub2_kernel_trust_cpu_rng/oval/shared.xml
ff1465
index c95f1d4..9035eee 100644
ff1465
--- a/linux_os/guide/system/bootloader-grub2/grub2_kernel_trust_cpu_rng/oval/shared.xml
ff1465
+++ b/linux_os/guide/system/bootloader-grub2/grub2_kernel_trust_cpu_rng/oval/shared.xml
ff1465
@@ -29,11 +29,34 @@
ff1465
     <ind:instance operation="greater than or equal" datatype="int">1</ind:instance>
ff1465
   </ind:textfilecontent54_object>
ff1465
 
ff1465
-
ff1465
-  comment="check forkernel command line parameters random.trust_cpu=off in {{{ grub2_boot_path }}}/grubenv for all kernels"
ff1465
-  check="all" check_existence="all_exist" version="1">
ff1465
-    <ind:object object_ref="object_trust_cpu_rng_boot_param" />
ff1465
-    <ind:state state_ref="state_trust_cpu_rng_boot_param_off" />
ff1465
+  {{% if product in ['rhel9'] %}}
ff1465
+  
ff1465
+                              comment="check kernel command line parameters for the argument for all boot entries."
ff1465
+                              check="all" check_existence="all_exist" version="1">
ff1465
+    <ind:object object_ref="obj_grub2_kernel_trust_cpu_rng_entries"/>
ff1465
+    <ind:state state_ref="state_trust_cpu_rng_boot_param_off"/>
ff1465
+  </ind:textfilecontent54_test>
ff1465
+
ff1465
+  
ff1465
+                              comment="check kernel command line parameters for the argument for all boot entries."
ff1465
+                              check="all" check_existence="all_exist" version="1">
ff1465
+    <ind:object object_ref="obj_grub2_kernel_trust_cpu_rng_entries"/>
ff1465
+    <ind:state state_ref="state_trust_cpu_rng_boot_param_on"/>
ff1465
+  </ind:textfilecontent54_test>
ff1465
+
ff1465
+  <ind:textfilecontent54_object id="obj_grub2_kernel_trust_cpu_rng_entries" version="1">
ff1465
+    <ind:path>/boot/loader/entries/</ind:path>
ff1465
+    <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
ff1465
+    <ind:pattern operation="pattern match">^options (.*)$</ind:pattern>
ff1465
+    <ind:instance datatype="int">1</ind:instance>
ff1465
+  </ind:textfilecontent54_object>
ff1465
+  {{% else %}}
ff1465
+
ff1465
+  
ff1465
+                              comment="check for kernel command line parameters random.trust_cpu=off in {{{ grub2_boot_path }}}/grubenv for all kernels"
ff1465
+                              check="all" check_existence="all_exist" version="1">
ff1465
+    <ind:object object_ref="object_trust_cpu_rng_boot_param"/>
ff1465
+    <ind:state state_ref="state_trust_cpu_rng_boot_param_off"/>
ff1465
   </ind:textfilecontent54_test>
ff1465
 
ff1465
 
ff1465
@@ -50,6 +73,7 @@
ff1465
     <ind:pattern operation="pattern match">^kernelopts=(.*)$</ind:pattern>
ff1465
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
ff1465
   </ind:textfilecontent54_object>
ff1465
+  {{% endif %}}
ff1465
 
ff1465
   
ff1465
   version="1">
ff1465
@@ -61,5 +85,4 @@
ff1465
     <ind:subexpression datatype="string" operation="pattern match">^(?:.*\s)?random\.trust_cpu=off(?:\s.*)?$</ind:subexpression>
ff1465
   </ind:textfilecontent54_state>
ff1465
 
ff1465
-
ff1465
 </def-group>
ff1465
diff --git a/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/rule.yml b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/rule.yml
ff1465
index dae640f..b8ff66c 100644
ff1465
--- a/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/rule.yml
ff1465
+++ b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/rule.yml
ff1465
@@ -1,6 +1,6 @@
ff1465
 documentation_complete: true
ff1465
 
ff1465
-prodtype: fedora,rhcos4,ol7,ol8,rhel7,rhel8,rhv4,sle15
ff1465
+prodtype: fedora,ol7,ol8,rhcos4,rhel7,rhel8,rhel9,rhv4,sle15
ff1465
 
ff1465
 title: 'Ensure IPv6 is disabled through kernel boot parameter'
ff1465
 
ff1465
diff --git a/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/arg_not_there_rhel9.fail.sh b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/arg_not_there_rhel9.fail.sh
ff1465
new file mode 100644
ff1465
index 0000000..fc649d7
ff1465
--- /dev/null
ff1465
+++ b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/arg_not_there_rhel9.fail.sh
ff1465
@@ -0,0 +1,10 @@
ff1465
+#!/bin/bash
ff1465
+# platform = Red Hat Enterprise Linux 9
ff1465
+
ff1465
+# Removes ipv6.disable argument from kernel command line in //boot/loader/entries/*.conf
ff1465
+
ff1465
+for file in /boot/loader/entries/*.conf ; do
ff1465
+  if grep -q '^.*ipv6\.disable=.*'  "$file" ; then
ff1465
+    sed -i 's/\(^.*\)ipv6\.disable=[^[:space:]]*\(.*\)/\1 \2/'  "$file"
ff1465
+  fi
ff1465
+done
ff1465
diff --git a/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/wrong_value_rhel9.fail.sh b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/wrong_value_rhel9.fail.sh
ff1465
new file mode 100644
ff1465
index 0000000..3c1cde1
ff1465
--- /dev/null
ff1465
+++ b/linux_os/guide/system/network/network-ipv6/disabling_ipv6/grub2_ipv6_disable_argument/tests/wrong_value_rhel9.fail.sh
ff1465
@@ -0,0 +1,14 @@
ff1465
+#!/bin/bash
ff1465
+# platform = Red Hat Enterprise Linux 9
ff1465
+
ff1465
+# Break the ipv6.disable argument in kernel command line in /boot/loader/entries/*.conf
ff1465
+
ff1465
+for file in /boot/loader/entries/*.conf ; do
ff1465
+  if grep -q '^.*ipv6\.disable=.*'  "$file" ; then
ff1465
+    # modify the GRUB command-line if an ipv6.disable= arg already exists
ff1465
+    sed -i 's/\(^.*\)ipv6\.disable=[^[:space:]]*\(.*\)/\1 ipv6\.disable=0 \2/'  "$file"
ff1465
+  else
ff1465
+    # no ipv6.disable=arg is present, append it
ff1465
+    sed -i 's/\(^.*\(vmlinuz\|kernelopts|options\).*\)/\1 ipv6\.disable=0/'  "$file"
ff1465
+  fi
ff1465
+done
ff1465
diff --git a/shared/macros-bash.jinja b/shared/macros-bash.jinja
ff1465
index b5f55ae..3eebbd9 100644
ff1465
--- a/shared/macros-bash.jinja
ff1465
+++ b/shared/macros-bash.jinja
ff1465
@@ -684,3 +684,43 @@ dpkg-query --show --showformat='${db:Status-Status}\n' "{{{ pkgname }}}" 2>/dev/
ff1465
 rpm --quiet -q "{{{ pkgname }}}"
ff1465
 {{%- endif -%}}
ff1465
 {{%- endmacro -%}}
ff1465
+
ff1465
+{{#
ff1465
+
ff1465
+    Remediation for grub2 bootloader arguments
ff1465
+#}}
ff1465
+{{% macro grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) %}}
ff1465
+{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
ff1465
+{{% if '/' in ARG_NAME %}}
ff1465
+{{{ raise("ARG_NAME (" + ARG_NAME + ") uses sed path separator (/) in " + rule_id) }}}
ff1465
+{{% elif '/' in ARG_NAME_VALUE %}}
ff1465
+{{{ raise("ARG_NAME_VALUE (" + ARG_NAME_VALUE + ") uses sed path separator (/) in " + rule_id) }}}
ff1465
+{{% endif %}}
ff1465
+# Correct the form of default kernel command line in GRUB
ff1465
+if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ ARG_NAME }}}=.*"'  '/etc/default/grub' ; then
ff1465
+	# modify the GRUB command-line if an {{{ ARG_NAME }}}= arg already exists
ff1465
+	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/'  '/etc/default/grub'
ff1465
+else
ff1465
+	# no {{{ ARG_NAME }}}=arg is present, append it
ff1465
+	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/'  '/etc/default/grub'
ff1465
+fi
ff1465
+
ff1465
+{{% if 'ubuntu' in product %}}
ff1465
+update-grub
ff1465
+{{% else %}}
ff1465
+# Correct the form of kernel command line for each installed kernel in the bootloader
ff1465
+grubby --update-kernel=ALL --args="{{{ ARG_NAME_VALUE }}}"
ff1465
+{{% endif %}}
ff1465
+{{% else %}}
ff1465
+# Correct grub2 kernelopts value using grub2-editenv
ff1465
+existing_kernelopts="$(grub2-editenv - list | grep kernelopts)"
ff1465
+if ! printf '%s' "$existing_kernelopts" | grep -qE '^kernelopts=(.*\s)?{{{ ARG_NAME_VALUE }}}(\s.*)?$'; then
ff1465
+  if test -n "$existing_kernelopts"; then
ff1465
+    grub2-editenv - set "$existing_kernelopts {{{ ARG_NAME_VALUE }}}"
ff1465
+  else
ff1465
+    grub2-editenv - set "kernelopts={{{ ARG_NAME_VALUE }}}"
ff1465
+  fi
ff1465
+fi
ff1465
+{{% endif %}}
ff1465
+
ff1465
+{{% endmacro %}}
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/bash.template b/shared/templates/grub2_bootloader_argument/bash.template
ff1465
index cecd1f9..fd75db4 100644
ff1465
--- a/shared/templates/grub2_bootloader_argument/bash.template
ff1465
+++ b/shared/templates/grub2_bootloader_argument/bash.template
ff1465
@@ -1,6 +1,6 @@
ff1465
 # platform = multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv,multi_platform_ubuntu
ff1465
 
ff1465
-{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
ff1465
+{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
ff1465
 {{% if '/' in ARG_NAME %}}
ff1465
 {{{ raise("ARG_NAME (" + ARG_NAME + ") uses sed path separator (/) in " + rule_id) }}}
ff1465
 {{% elif '/' in ARG_NAME_VALUE %}}
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
ff1465
index e8da1fe..3ea8acb 100644
ff1465
--- a/shared/templates/grub2_bootloader_argument/oval.template
ff1465
+++ b/shared/templates/grub2_bootloader_argument/oval.template
ff1465
@@ -2,9 +2,14 @@
ff1465
   <definition class="compliance" id="{{{ _RULE_ID }}}" version="2">
ff1465
     {{{ oval_metadata("Ensure " + ARG_NAME_VALUE + " is configured in the kernel line in /etc/default/grub.") }}}
ff1465
     <criteria operator="AND">
ff1465
-      {{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
ff1465
-        
ff1465
-        comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the {{{ grub2_boot_path }}}/grub.cfg for all kernels" />
ff1465
+      {{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
ff1465
+        {{% if product in ['rhel9'] %}}
ff1465
+          
ff1465
+                     comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the /boot/loader/entries/*.conf" />
ff1465
+        {{% else %}}
ff1465
+          
ff1465
+          comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the {{{ grub2_boot_path }}}/grub.cfg for all kernels" />
ff1465
+        {{% endif %}}
ff1465
         <criteria operator="OR">
ff1465
           
ff1465
           comment="check for {{{ ARG_NAME_VALUE }}} in /etc/default/grub via GRUB_CMDLINE_LINUX" />
ff1465
@@ -22,7 +27,7 @@
ff1465
     </criteria>
ff1465
   </definition>
ff1465
 
ff1465
-{{% if product in ["rhel7", "ol7"] or 'ubuntu' in product %}}
ff1465
+{{% if product in ["rhel7", "ol7", "rhel9"] or 'ubuntu' in product %}}
ff1465
   
ff1465
   comment="check for {{{ ARG_NAME_VALUE }}} in /etc/default/grub via GRUB_CMDLINE_LINUX"
ff1465
   check="all" check_existence="all_exist" version="1">
ff1465
@@ -50,6 +55,21 @@
ff1465
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
ff1465
   </ind:textfilecontent54_object>
ff1465
 
ff1465
+  {{% if product in ["rhel9"] %}}
ff1465
+    
ff1465
+                                comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} for all boot entries."
ff1465
+                                check="all" check_existence="all_exist" version="1">
ff1465
+      <ind:object object_ref="obj_grub2_{{{ SANITIZED_ARG_NAME }}}_entries" />
ff1465
+      <ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
ff1465
+    </ind:textfilecontent54_test>
ff1465
+
ff1465
+  <ind:textfilecontent54_object id="obj_grub2_{{{ SANITIZED_ARG_NAME }}}_entries" version="1">
ff1465
+    <ind:path>/boot/loader/entries/</ind:path>
ff1465
+    <ind:filename operation="pattern match">^.*\.conf$</ind:filename>
ff1465
+    <ind:pattern operation="pattern match">^options (.*)$</ind:pattern>
ff1465
+    <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
ff1465
+  </ind:textfilecontent54_object>
ff1465
+  {{% else %}}
ff1465
   
ff1465
   comment="check kernel command line parameters for {{{ ARG_NAME_VALUE }}} in {{{ grub2_boot_path }}}/grub.cfg for all kernels"
ff1465
   check="all" check_existence="all_exist" version="1">
ff1465
@@ -68,6 +88,8 @@
ff1465
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
ff1465
   </ind:textfilecontent54_object>
ff1465
 
ff1465
+  {{% endif %}}
ff1465
+
ff1465
 {{% else %}}
ff1465
 
ff1465
   
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/template.py b/shared/templates/grub2_bootloader_argument/template.py
ff1465
index 7c32daa..60951cf 100644
ff1465
--- a/shared/templates/grub2_bootloader_argument/template.py
ff1465
+++ b/shared/templates/grub2_bootloader_argument/template.py
ff1465
@@ -6,6 +6,7 @@ def preprocess(data, lang):
ff1465
     if lang == "oval":
ff1465
         # escape dot, this is used in oval regex
ff1465
         data["escaped_arg_name_value"] = data["arg_name_value"].replace(".", "\\.")
ff1465
+        data["escaped_arg_name"] = data["arg_name"].replace(".", "\\.")
ff1465
         # replace . with _, this is used in test / object / state ids
ff1465
         data["sanitized_arg_name"] = ssg.utils.escape_id(data["arg_name"])
ff1465
     return data
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/tests/arg_not_there.fail.sh b/shared/templates/grub2_bootloader_argument/tests/arg_not_there.fail.sh
ff1465
new file mode 100644
ff1465
index 0000000..fdf2a5d
ff1465
--- /dev/null
ff1465
+++ b/shared/templates/grub2_bootloader_argument/tests/arg_not_there.fail.sh
ff1465
@@ -0,0 +1,10 @@
ff1465
+#!/bin/bash
ff1465
+
ff1465
+# platform = Red Hat Enterprise Linux 8
ff1465
+
ff1465
+# Removes audit argument from kernel command line in /boot/grub2/grubenv
ff1465
+file="/boot/grub2/grubenv"
ff1465
+if grep -q '^.*{{{ARG_NAME}}}=.*'  "$file" ; then
ff1465
+	sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/'  "$file"
ff1465
+fi
ff1465
+
ff1465
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
ff1465
new file mode 100644
ff1465
index 0000000..a56e6d0
ff1465
--- /dev/null
ff1465
+++ b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
ff1465
@@ -0,0 +1,9 @@
ff1465
+#!/bin/bash
ff1465
+
ff1465
+# platform = Red Hat Enterprise Linux 7,Red Hat Enterprise Linux 9
ff1465
+
ff1465
+# Removes argument from kernel command line in /etc/default/grub
ff1465
+if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=.*"'  '/etc/default/grub' ; then
ff1465
+	sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/'  '/etc/default/grub'
ff1465
+fi
ff1465
+
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh b/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh
ff1465
new file mode 100644
ff1465
index 0000000..b6454a9
ff1465
--- /dev/null
ff1465
+++ b/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh
ff1465
@@ -0,0 +1,3 @@
ff1465
+# platform = multi_platform_all
ff1465
+
ff1465
+{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/tests/wrong_value.fail.sh b/shared/templates/grub2_bootloader_argument/tests/wrong_value.fail.sh
ff1465
new file mode 100644
ff1465
index 0000000..5a97ec2
ff1465
--- /dev/null
ff1465
+++ b/shared/templates/grub2_bootloader_argument/tests/wrong_value.fail.sh
ff1465
@@ -0,0 +1,12 @@
ff1465
+#!/bin/bash
ff1465
+# platform = Red Hat Enterprise Linux 8
ff1465
+
ff1465
+# Break the argument in kernel command line in /boot/grub2/grubenv
ff1465
+file="/boot/grub2/grubenv"
ff1465
+if grep -q '^.*{{{ARG_NAME}}}=.*'  "$file" ; then
ff1465
+	# modify the GRUB command-line if the arg already exists
ff1465
+	sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 {{{ARG_NAME}}}=wrong \2/'  "$file"
ff1465
+else
ff1465
+	# no arg is present, append it
ff1465
+	sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME}}}=wrong/'  "$file"
ff1465
+fi
ff1465
diff --git a/shared/templates/grub2_bootloader_argument/tests/wrong_value_entries.fail.sh b/shared/templates/grub2_bootloader_argument/tests/wrong_value_entries.fail.sh
ff1465
new file mode 100644
ff1465
index 0000000..09861aa
ff1465
--- /dev/null
ff1465
+++ b/shared/templates/grub2_bootloader_argument/tests/wrong_value_entries.fail.sh
ff1465
@@ -0,0 +1,14 @@
ff1465
+#!/bin/bash
ff1465
+# platform = Red Hat Enterprise Linux 9
ff1465
+
ff1465
+# Removes argument from kernel command line in /boot/loader/entries/*.conf
ff1465
+
ff1465
+for file in /boot/loader/entries/*.conf ; do
ff1465
+  if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=.*'  "$file" ; then
ff1465
+      # modify the GRUB command-line if an audit= arg already exists
ff1465
+	    sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 {{{ARG_NAME}}}=wrong \2/'  "$file"
ff1465
+    else
ff1465
+	    # no audit=arg is present, append it
ff1465
+	    sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME}}}=wrong/'  "$file"
ff1465
+  fi
ff1465
+done