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.

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,14 @@
#!/bin/bash

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

source common.sh

# 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
done
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/bin/bash

# platform = Oracle Linux 7
# Removes audit argument from kernel command line in /etc/default/grub
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*audit=.*"' '/etc/default/grub' ; then
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)audit=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
# platform = multi_platform_all

source common.sh

# 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=".*\)audit=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/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

# Removes audit argument from kernel command line in /boot/grub2/grubenv
file="/boot/grub2/grubenv"
if grep -q '^.*{{{ARG_NAME}}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi

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

# remediate with grubby, this ill ensure expanded options are in /boot/loader/entries/*.conf
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ if grep -q '^.*{{{ARG_NAME}}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# platform = multi_platform_all

{{%- if 'ubuntu' in product %}}
# packages = grub2
{{%- else %}}
# packages = grub2,grubby
{{%- endif %}}

source common.sh

# configure the argument in kernel command line in /boot/grub2/grubenv
#file="/boot/grub2/grubenv"
#if grep -q '^.*{{{ARG_NAME}}}=.*' "$file"; then
# modify the GRUB command-line if the arg already exists
# sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
#else
# no arg is present, append it
# sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME_VALUE}}}/' "$file"
#fi

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably should be removed instead of having it commented out

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest commit.


# Correct the form of default kernel command line in GRUB /etc/default/grub and applies value through Grubby
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ ESCAPED_ARG_NAME }}}=.*"' '/etc/default/grub' ; then
# modify the GRUB command-line if an arg=value already exists
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' '/etc/default/grub'
else
# no audit=arg is present, append it
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/' '/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="{{{ ARG_NAME_VALUE }}}"' >> /etc/default/grub
fi

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/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

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

# configure the argument in kernel command line in /boot/grub2/grubenv
file="/boot/grub2/grubenv"
if grep -q '^.*{{{ARG_NAME}}}=.*' "$file"; then
# modify the GRUB command-line if the arg already exists
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
else
# no arg is present, append it
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME_VALUE}}}/' "$file"
fi


rm -f /boot/loader/entries/*.conf

# create an entry which references grubenv
{
echo 'title OS 1'
echo 'version 5.0'
echo 'linux /vmlinuz'
echo 'initrd /initramfs'
echo 'options root=UUID=abc-def rhgb ro quiet mock $kernelopts'
echo 'grub_users $grub_users'
echo 'grub_arg --unrestricted'
} > /boot/loader/entries/mock2.conf
Loading
Loading