diff --git a/docs/templates/template_reference.md b/docs/templates/template_reference.md index 695296df6a2..8677d64d593 100644 --- a/docs/templates/template_reference.md +++ b/docs/templates/template_reference.md @@ -934,7 +934,7 @@ The selected value can be changed in the profile (consult the actual variable fo the remediation scripts will set the variable with correct value to a drop-in file in `/etc/sysctl.d/var_name.conf` file. -- Languages: Ansible, Bash, OVAL +- Languages: Ansible, Bash, OVAL, SCE #### systemd_dropin_configuration - checks if a Systemd-style configuration exists either in the main file or in any file within specified dropin directory. diff --git a/shared/templates/sysctl/bash.template b/shared/templates/sysctl/bash.template index b3aafbc2757..a1149e40434 100644 --- a/shared/templates/sysctl/bash.template +++ b/shared/templates/sysctl/bash.template @@ -41,7 +41,9 @@ SYSCONFIG_FILE="/etc/sysctl.conf" # # Set runtime for {{{ SYSCTLVAR }}} # -/sbin/sysctl -q -n -w {{{ SYSCTLVAR }}}="$sysctl_{{{ SYSCTLID }}}_value" +if {{{ bash_not_bootc_build() }}} ; then + /sbin/sysctl -q -n -w {{{ SYSCTLVAR }}}="$sysctl_{{{ SYSCTLID }}}_value" +fi # # If {{{ SYSCTLVAR }}} present in /etc/sysctl.conf, change value to appropriate value @@ -57,7 +59,9 @@ sed -i "/^$SYSCONFIG_VAR/d" /etc/sysctl.conf # # Set runtime for {{{ SYSCTLVAR }}} # -/sbin/sysctl -q -n -w {{{ SYSCTLVAR }}}="{{{ SYSCTLVAL }}}" +if {{{ bash_not_bootc_build() }}} ; then + /sbin/sysctl -q -n -w {{{ SYSCTLVAR }}}="{{{ SYSCTLVAL }}}" +fi # # If {{{ SYSCTLVAR }}} present in /etc/sysctl.conf, change value to "{{{ SYSCTLVAL }}}" diff --git a/shared/templates/sysctl/oval.template b/shared/templates/sysctl/oval.template index 35a7e94d39e..21b09af5620 100644 --- a/shared/templates/sysctl/oval.template +++ b/shared/templates/sysctl/oval.template @@ -20,7 +20,7 @@ ^[\s]*{{{ SYSCTLVAR }}}[\s]*=[\s]*(.*\S)[\s]*$ 1 {{%- endmacro -%}} -{{%- if "P" in FLAGS -%}} +{{%- if IPV6 == "false" -%}} @@ -36,7 +36,7 @@ -{{%- elif "I" in FLAGS -%}} +{{%- else -%}} @@ -62,7 +62,6 @@ {{%- endif %}} -{{%- if "R" in FLAGS -%}} {{% if CHECK_RUNTIME == "true" %}} @@ -121,8 +120,6 @@ {{% endif %}} -{{%- endif -%}} -{{%- if "S" in FLAGS -%}} @@ -269,4 +266,3 @@ {{% endfor %}} {{% endif %}} -{{%- endif -%}} diff --git a/shared/templates/sysctl/sce-bash.template b/shared/templates/sysctl/sce-bash.template new file mode 100644 index 00000000000..8fc62bce409 --- /dev/null +++ b/shared/templates/sysctl/sce-bash.template @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +# check-import = stdout +{{% if SYSCTLVAL == "" %}} +# check-export = sysctl_{{{ SYSCTLID }}}_value=sysctl_{{{ SYSCTLID }}}_value +{{% endif %}} + +{{% if product in [ "ol7", "ol8", "ol9", "rhcos4", "rhel8", "rhel9", "rhel10", "ubuntu2004", "ubuntu2204"] %}} +FILES_NOT_MANAGED_BY_PACKAGES=("/etc/sysctl.conf" "/etc/sysctl.d/*.conf" "/usr/local/lib/sysctl.d/*.conf" "/run/sysctl.d/*.conf") +{{% else %}} +FILES_NOT_MANAGED_BY_PACKAGES=("/etc/sysctl.conf" "/etc/sysctl.d/*.conf" "/lib/sysctl.d/*.conf" "/usr/local/lib/sysctl.d/*.conf" "/run/sysctl.d/*.conf") +{{% endif %}} +FILES_MANAGED_BY_PACKAGES=("/usr/lib/sysctl.d/*.conf") + +function pass_if_set_correctly() +{ + local filelist="$1" + local regex="$2" + local expected_value="$3" + local found=0 + for files in $filelist ; do + [[ -e "$files" ]] || continue + found_value=$(grep -P "$regex" $files | sed -E "s/$regex/\1/") + if [[ -n "$found_value" ]] ; then + if [[ "$found_value" == "$expected_value" ]] ; then + found=1 + else + return 1 + fi + fi + done + if [[ $found == 1 ]] ; then + return 0 + fi + return 1 +} + +function pass_if_missing() +{ + local filelist="$1" + local regex="$2" + for files in $filelist ; do + [[ -e "$files" ]] || continue + if grep -P "$regex" $files ; then + return 1 + fi + done + return 0 +} + +function check_sysctl_configuration() +{ + local sysctlvar="$1" + local expected_value="$2" + + regex="^\s*$sysctlvar\s*=\s*(.*)\s*" + + # kernel static parameter $sysctlvar set to $sysctlvar in sysctl files not managed by packages + pass_if_set_correctly "${FILES_NOT_MANAGED_BY_PACKAGES[*]}" "$regex" "$expected_value" + set_correctly_in_not_managed="$?" + + # kernel static parameter $sysctlvar missing in sysctl files not managed by packages + pass_if_missing "${FILES_NOT_MANAGED_BY_PACKAGES[*]}" "$regex" + missing_in_not_managed="$?" + + # kernel static parameter $sysctlvar set to $sysctlval in sysctl files managed by packages + pass_if_set_correctly "${FILES_MANAGED_BY_PACKAGES[*]}" "$regex" "$expected_value" + set_correctly_in_managed="$?" + + if [[ "$set_correctly_in_not_managed" == 0 || ( "$missing_in_not_managed" == 0 && "$set_correctly_in_managed" == 0 ) ]] ; then + return 0 + fi + return 1 +} + +{{% if IPV6 == "true" -%}} +# pass if IPv6 is disabled +check_sysctl_configuration "net.ipv6.conf.all.disable_ipv6" "1" +if [[ $? == 0 ]] ; then + exit $XCCDF_RESULT_PASS +fi +{{% endif %}} + +{{% if SYSCTLVAL is string %}} +{{% if SYSCTLVAL == "" -%}} +expected_value="$XCCDF_VALUE_sysctl_{{{ SYSCTLID }}}_value" +{{%- else -%}} +expected_value="{{{ SYSCTLVAL }}}" +{{%- endif %}} +check_sysctl_configuration "{{{ SYSCTLVAR }}}" "$expected_value" +if [[ $? == 0 ]] ; then + exit $XCCDF_RESULT_PASS +fi +{{% elif SYSCTLVAL is sequence %}} +{{% for x in SYSCTLVAL %}} +check_sysctl_configuration "{{{ SYSCTLVAR }}}" "{{{ x }}}" +if [[ $? == 0 ]] ; then + exit $XCCDF_RESULT_PASS +fi +{{% endfor %}} +{{% endif %}} +exit $XCCDF_RESULT_FAIL diff --git a/shared/templates/sysctl/template.py b/shared/templates/sysctl/template.py index 23c6cc45d35..f03c22e5052 100644 --- a/shared/templates/sysctl/template.py +++ b/shared/templates/sysctl/template.py @@ -5,10 +5,10 @@ def preprocess(data, lang): data["sysctlid"] = ssg.utils.escape_id(data["sysctlvar"]) if not data.get("sysctlval"): data["sysctlval"] = "" - ipv6_flag = "P" if data["sysctlid"].find("ipv6") >= 0: - ipv6_flag = "I" - data["flags"] = "SR" + ipv6_flag + data["ipv6"] = "true" + else: + data["ipv6"] = "false" if "operation" not in data: data["operation"] = "equals" if isinstance(data["sysctlval"], list) and len(data["sysctlval"]) == 0: diff --git a/shared/templates/sysctl/template.yml b/shared/templates/sysctl/template.yml index b57de6fbb63..f084d352593 100644 --- a/shared/templates/sysctl/template.yml +++ b/shared/templates/sysctl/template.yml @@ -2,3 +2,4 @@ supported_languages: - ansible - bash - oval + - sce-bash