Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance the grub2_argument template to cover more use cases #12375

Merged

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

72 changes: 62 additions & 10 deletions shared/templates/grub2_bootloader_argument/oval.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
- Kernel opts can be stored in /etc/default/grub so they are persistent between kernel upgrades
-#}}
{{% set system_with_expanded_kernel_options_in_loader_entries = false -%}}
{{% set system_with_referenced_kernel_options_in_loader_entries = false -%}}
{{% set system_with_kernel_options_in_grubenv = false -%}}
{{% set system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv = false -%}}
{{% set system_with_kernel_options_in_etc_default_grub = true -%}}
{{% set system_with_kernel_options_in_etc_default_grub_d = false -%}}
{{% set system_with_expanded_kernel_options_in_grub_cfg = false -%}}
Expand All @@ -18,8 +18,7 @@
{{%- endif -%}}

{{% if product in ["ol8", "rhel8"] -%}}
{{% set system_with_referenced_kernel_options_in_loader_entries = true %}}
{{% set system_with_kernel_options_in_grubenv = true %}}
{{% set system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv = true -%}}
{{%- endif -%}}

{{% if product in ["ol7"] or 'ubuntu' in product -%}}
Expand All @@ -34,11 +33,31 @@
{{% set system_with_bios_and_uefi_support = true %}}
{{%- endif -%}}


<def-group>
<definition class="compliance" id="{{{ _RULE_ID }}}" version="2">
{{{ oval_metadata("Ensure " + ARG_NAME_VALUE + " is configured in the kernel line in /etc/default/grub.") }}}
<criteria operator="AND">
{{% if system_with_kernel_options_in_grubenv -%}}
{{% if system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv %}}
<criterion test_ref="test_grub2_{{{ SANITIZED_ARG_NAME }}}_entries_expanded_or_referenced"
comment="Check /boot/loader/entries/*.conf files if they contain direct reference to {{{ ARG_NAME_VALUE }}} or if they contain $kernelopts" />
<criteria operator="OR"
comment="Expressing implication">
<criterion test_ref="test_grub2_{{{ SANITIZED_ARG_NAME }}}_at_least_one_entry_referenced" negate="true"
comment="Negate the result of the test if there exists at least one $kernelopts in /boot/loader/entries" />
{{% if system_with_bios_and_uefi_support -%}}
<criteria operator="OR">
{{%- endif %}}
<criterion test_ref="test_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_env"
comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_boot_path }}}/grubenv" />
{{% if system_with_bios_and_uefi_support -%}}
<criterion test_ref="test_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_grub_env_uefi"
comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_uefi_boot_path }}}/grubenv" />
</criteria>
{{%- endif %}}
</criteria>
{{% elif system_with_kernel_options_in_grubenv -%}}
<extend_definition comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable" definition_ref="grub2_entries_reference_kernelopts" />
{{% if system_with_bios_and_uefi_support -%}}
<criteria operator="OR">
{{%- endif %}}
Expand All @@ -49,11 +68,7 @@
comment="Check if {{{ ARG_NAME_VALUE }}} is present in the GRUB2 environment variable block in {{{ grub2_uefi_boot_path }}}/grubenv" />
</criteria>
{{%- endif %}}
{{%- endif %}}
{{% if system_with_referenced_kernel_options_in_loader_entries -%}}
<extend_definition comment="check kernel command line parameters for referenced boot entries reference the $kernelopts variable" definition_ref="grub2_entries_reference_kernelopts" />
{{%- endif %}}
{{% if system_with_expanded_kernel_options_in_loader_entries -%}}
{{% elif system_with_expanded_kernel_options_in_loader_entries -%}}
<criterion test_ref="test_grub2_{{{ SANITIZED_ARG_NAME }}}_entries"
comment="Check if {{{ ARG_NAME_VALUE }}} is present in the boot parameters in the /boot/loader/entries/*.conf" />
{{%- endif %}}
Expand Down Expand Up @@ -96,6 +111,36 @@
</criteria>
</definition>

{{% if system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv %}}
<ind:textfilecontent54_test id="test_grub2_{{{ SANITIZED_ARG_NAME }}}_entries_expanded_or_referenced"
comment="check all /boot/loader/entries/*.conf for expanded entries of {{{ ARG_NAME_VALUE }}}. Leave out rescue boot entries. Accept also references to $kernelopts."
state_operator="OR" check="all" check_existence="all_exist" version="1">
<ind:object object_ref="obj_grub2_{{{ SANITIZED_ARG_NAME }}}_entries_expanded_or_referenced" />
<ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" />
<ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_is_kernelopts" />
</ind:textfilecontent54_test>

<ind:textfilecontent54_object id="obj_grub2_{{{ SANITIZED_ARG_NAME }}}_entries_expanded_or_referenced" version="1">
<ind:path>/boot/loader/entries/</ind:path>
<ind:filename operation="pattern match">^.*\.conf$</ind:filename>
<ind:pattern operation="pattern match">^options (.*)$</ind:pattern>
<ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
<filter action="exclude">state_grub2_rescue_entry_for_{{{ _RULE_ID }}}</filter>
</ind:textfilecontent54_object>

<ind:textfilecontent54_state id="state_grub2_rescue_entry_for_{{{ _RULE_ID }}}" version="1">
<ind:filename operation="pattern match">.*rescue\.conf$</ind:filename>
</ind:textfilecontent54_state>

<ind:textfilecontent54_test id="test_grub2_{{{ SANITIZED_ARG_NAME }}}_at_least_one_entry_referenced"
comment="check all /boot/loader/entries/*.conf files if there is at least one entry referencing $kernelopts. Leave out rescue entries."
check="all" check_existence="at_least_one_exists" version="1">
<ind:object object_ref="obj_grub2_{{{ SANITIZED_ARG_NAME }}}_entries_expanded_or_referenced" />
<ind:state state_ref="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_is_kernelopts" />
</ind:textfilecontent54_test>
{{% endif %}}


{{%- if system_with_kernel_options_in_etc_default_grub %}}
<ind:textfilecontent54_test id="test_grub2_{{{ SANITIZED_ARG_NAME }}}_argument"
comment="check for {{{ ARG_NAME_VALUE }}} in /etc/default/grub via GRUB_CMDLINE_LINUX"
Expand Down Expand Up @@ -154,7 +199,7 @@
</ind:textfilecontent54_object>
{{%- endif %}}

{{%- if system_with_kernel_options_in_grubenv %}}
{{%- if system_with_kernel_options_in_grubenv or system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv %}}
{{%- macro test_and_object_for_kernel_options_grub_env(base_name, path) %}}
<ind:textfilecontent54_test id="test_{{{ base_name }}}"
comment="check for kernel command line parameters {{{ ARG_NAME_VALUE }}} in {{{ path }}} for all kernels"
Expand Down Expand Up @@ -225,6 +270,13 @@
{{%- endif %}}
{{%- endif %}}

{{% if system_with_expanded_kernel_options_in_loader_entries_or_with_options_in_grubenv %}}
<ind:textfilecontent54_state id="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_is_kernelopts"
version="1">
<ind:subexpression datatype="string" operation="pattern match">^(?:.*\s)?\$kernelopts(?:\s.*)?$</ind:subexpression>
</ind:textfilecontent54_state>
{{% endif %}}

{{% if ARG_VALUE %}}
<ind:textfilecontent54_state id="state_grub2_{{{ SANITIZED_ARG_NAME }}}_argument"
version="1">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# platform = multi_platform_fedora,multi_platform_rhel
# packages = grub2,grubby

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes argument from kernel command line in /boot/loader/entries/*.conf

for file in /boot/loader/entries/*.conf ; do
if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi
# ensure that grubenv is not referenced
if grep -q '\$kernelopts' "$file"; then
sed -i 's/^\(options.*\)\$kernelopts\(.*\)$/\1\2/' "$file"
fi
done
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/bin/bash

# platform = multi_platform_fedora,multi_platform_ol,multi_platform_rhel,multi_platform_ubuntu
{{%- if 'ubuntu' in product %}}
# packages = grub2
{{%- else %}}
# packages = grub2,grubby
{{%- endif %}}
# platform = multi_platform_all

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes argument from kernel command line in /etc/default/grub
if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=.*"' '/etc/default/grub' ; then
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
fi

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# platform = multi_platform_all

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes the argument from kernel command line in /etc/default/grub
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ARG_NAME}}}=.*"' '/etc/default/grub' ; then
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
fi

# removing the parameter from the no recovery kernel parameters as well
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'

# disabling recovery
sed -i 's/\(^.*GRUB_DISABLE_RECOVERY=\).*/\1true/' '/etc/default/grub'

#if the line is not present at all, add it
if ! grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*$' '/etc/default/grub'; then
echo 'GRUB_CMDLINE_LINUX_DEFAULT=""' >> /etc/default/grub
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# platform = Oracle Linux 8,Red Hat Enterprise Linux 8
# packages = grub2,grubby

{{%- if ARG_VARIABLE %}}
# variables = {{{ ARG_VARIABLE }}}=correct_value
{{%- set ARG_NAME_VALUE= ARG_NAME ~ "=correct_value" %}}
{{%- endif %}}

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
# ensure that the grubenv entry is not referenced
# also in RHEL 8, after performing previous steps, the only option is $kernelopts
# so add the arg=value there so that the test passes
for entry in /boot/loader/entries/*.conf; do
if grep -q '\$kernelopts' "$entry"; then
sed -i 's/^\(options.*\)\$kernelopts\(.*\)$/\1\2 mock {{{ ARG_NAME_VALUE }}}/' "$entry"
fi
done
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# platform = Oracle Linux 8,Red Hat Enterprise Linux 8
# packages = grub2,grubby

source common.sh

for entry in /boot/loader/entries/*.conf; do
if ! grep -q '\$kernelopts' "$entry"; then
sed -i 's/^\(options.*\)$/\1 \$kernelopts/' "$entry"
fi
done

This file was deleted.

21 changes: 9 additions & 12 deletions shared/templates/grub2_bootloader_argument/tests/common.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#!/bin/bash

# We need to mock these files if they do not exist yet
# Because:
# - it is not possible to setup kernel / grub env properly in containerized env
# - even if it would be possible, it takes long time

# this file prepares unified test environment used by other scenarios
# These should be tuned per product to match defaults

{{% if product in ["ol8", "rhel8"] -%}}
mkdir -p /boot/grub2
if [ ! -f /boot/grub2/grubenv ]; then
rm -f /boot/grub2/grubenv
# File size must be exactly 1KiB
{
echo '# GRUB Environment Block'
Expand All @@ -18,10 +15,11 @@ if [ ! -f /boot/grub2/grubenv ]; then
dd if=/dev/zero bs=1K count=1 | tr '\0' '#'
} > /boot/grub2/grubenv
truncate --size=1K /boot/grub2/grubenv
fi
{{% endif %}}


mkdir -p /etc/default
if [ ! -f /etc/default/grub ]; then
rm -f /etc/default/grub
{
echo 'GRUB_CMDLINE_LINUX="rhgb ro ouiet"'
echo 'GRUB_CMDLINE_LINUX_DEFAULT="rhgb ro quiet mock"'
Expand All @@ -31,12 +29,11 @@ if [ ! -f /etc/default/grub ]; then
echo 'GRUB_ENABLE_BLSCFG=true'
echo 'GRUB_TIMEOUT=30'
} > /etc/default/grub
fi

{{% if product in ["fedora", "ol8", "ol9", "rhel8", "rhel9", "rhel10"] -%}}
mkdir -p /boot/loader/entries
shopt -s nullglob
boot_loader_entries=(/boot/loader/entries/*.conf)
if [ "${#boot_loader_entries[@]}" -eq 0 ]; then
rm -f /boot/loader/entries/*.conf
{
echo 'title OS 1'
echo 'version 5.0'
Expand All @@ -46,4 +43,4 @@ if [ "${#boot_loader_entries[@]}" -eq 0 ]; then
echo 'grub_users $grub_users'
echo 'grub_arg --unrestricted'
} > /boot/loader/entries/mock.conf
fi
{{%- endif -%}}
Loading
Loading