Blame SOURCES/scap-security-guide-0.1.61-distributed-sshd-rekeylimit-PR_8148.patch

07cb6b
From f7a2fb33ad1507ad4ce3f7ec6534c06d4f6a7e83 Mon Sep 17 00:00:00 2001
07cb6b
From: Watson Sato <wsato@redhat.com>
07cb6b
Date: Fri, 4 Feb 2022 12:02:36 +0100
07cb6b
Subject: [PATCH 1/3] Add tests for distributed SSHD RekeyLimit config
07cb6b
07cb6b
---
07cb6b
 .../sshd_rekey_limit/tests/bad_size_directory.fail.sh | 10 ++++++++++
07cb6b
 .../sshd_rekey_limit/tests/bad_time_directory.fail.sh | 10 ++++++++++
07cb6b
 .../sshd_rekey_limit/tests/no_line_directory.fail.sh  |  8 ++++++++
07cb6b
 .../sshd_rekey_limit/tests/rhel8_ok.pass.sh           |  2 +-
07cb6b
 .../sshd_rekey_limit/tests/rhel9_ok.pass.sh           | 11 +++++++++++
07cb6b
 5 files changed, 40 insertions(+), 1 deletion(-)
07cb6b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_size_directory.fail.sh
07cb6b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_time_directory.fail.sh
07cb6b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/no_line_directory.fail.sh
07cb6b
 create mode 100644 linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel9_ok.pass.sh
07cb6b
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_size_directory.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_size_directory.fail.sh
07cb6b
new file mode 100644
07cb6b
index 00000000000..88c6420c5ca
07cb6b
--- /dev/null
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_size_directory.fail.sh
07cb6b
@@ -0,0 +1,10 @@
07cb6b
+# platform = multi_platform_fedora,Red Hat Enterprise Linux 9
07cb6b
+
07cb6b
+mkdir -p /etc/ssh/sshd_config.d
07cb6b
+touch /etc/ssh/sshd_config.d/nothing
07cb6b
+
07cb6b
+if grep -q "^\s*RekeyLimit" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
07cb6b
+	sed -i "/^\s*RekeyLimit.*/Id" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
07cb6b
+fi
07cb6b
+
07cb6b
+echo "RekeyLimit 812M 1h" > /etc/ssh/sshd_config.d/bad_config.conf
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_time_directory.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_time_directory.fail.sh
07cb6b
new file mode 100644
07cb6b
index 00000000000..3bb0926017c
07cb6b
--- /dev/null
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/bad_time_directory.fail.sh
07cb6b
@@ -0,0 +1,10 @@
07cb6b
+# platform = multi_platform_fedora,Red Hat Enterprise Linux 9
07cb6b
+
07cb6b
+mkdir -p /etc/ssh/sshd_config.d
07cb6b
+touch /etc/ssh/sshd_config.d/nothing
07cb6b
+
07cb6b
+if grep -q "^\s*RekeyLimit" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
07cb6b
+	sed -i "/^\s*RekeyLimit.*/Id" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
07cb6b
+fi
07cb6b
+
07cb6b
+echo "RekeyLimit 512M 2h" > /etc/ssh/sshd_config.d/bad_config.conf
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/no_line_directory.fail.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/no_line_directory.fail.sh
07cb6b
new file mode 100644
07cb6b
index 00000000000..00569de1b84
07cb6b
--- /dev/null
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/no_line_directory.fail.sh
07cb6b
@@ -0,0 +1,8 @@
07cb6b
+# platform = multi_platform_fedora,Red Hat Enterprise Linux 9
07cb6b
+
07cb6b
+mkdir -p /etc/ssh/sshd_config.d
07cb6b
+touch /etc/ssh/sshd_config.d/nothing
07cb6b
+
07cb6b
+if grep -q "^\s*RekeyLimit" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
07cb6b
+	sed -i "/^\s*RekeyLimit.*/Id" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
07cb6b
+fi
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel8_ok.pass.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel8_ok.pass.sh
07cb6b
index b9834e6d0b2..894c0ae4ba8 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel8_ok.pass.sh
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel8_ok.pass.sh
07cb6b
@@ -1,4 +1,4 @@
07cb6b
-# platform = Red Hat Enterprise Linux 8
07cb6b
+# platform = Red Hat Enterprise Linux 8,Red Hat Enterprise Linux 9
07cb6b
 # profiles = xccdf_org.ssgproject.content_profile_ospp
07cb6b
 
07cb6b
 sed -e '/RekeyLimit/d' /etc/ssh/sshd_config
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel9_ok.pass.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel9_ok.pass.sh
07cb6b
new file mode 100644
07cb6b
index 00000000000..e183e8986dc
07cb6b
--- /dev/null
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/tests/rhel9_ok.pass.sh
07cb6b
@@ -0,0 +1,11 @@
07cb6b
+# platform = Red Hat Enterprise Linux 9
07cb6b
+# profiles = xccdf_org.ssgproject.content_profile_ospp
07cb6b
+
07cb6b
+mkdir -p /etc/ssh/sshd_config.d
07cb6b
+touch /etc/ssh/sshd_config.d/nothing
07cb6b
+
07cb6b
+if grep -q "^\s*RekeyLimit" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* ; then
07cb6b
+	sed -i "/^\s*RekeyLimit.*/Id" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
07cb6b
+fi
07cb6b
+
07cb6b
+echo "RekeyLimit 1G 1h" >> /etc/ssh/sshd_config.d/good_config.conf
07cb6b
07cb6b
From 782e3a6108ea377d526d0aed4e8c0cf019f3dcdd Mon Sep 17 00:00:00 2001
07cb6b
From: Watson Sato <wsato@redhat.com>
07cb6b
Date: Fri, 4 Feb 2022 12:06:45 +0100
07cb6b
Subject: [PATCH 2/3] Update rule to handle distributed config
07cb6b
07cb6b
Based on the template sshd_lineinfile, updated rule sshd_rekey_limit to
07cb6b
check and remediate SSHD configuration in products that support
07cb6b
/etc/sshd/sshd_config.d/
07cb6b
07cb6b
The rule cannot use the template as it relies on two external variables.
07cb6b
---
07cb6b
 .../sshd_rekey_limit/ansible/shared.yml       |  8 +++-
07cb6b
 .../sshd_rekey_limit/bash/shared.sh           |  2 +-
07cb6b
 .../sshd_rekey_limit/oval/shared.xml          | 46 ++++++++++++++-----
07cb6b
 .../ssh/ssh_server/sshd_rekey_limit/rule.yml  | 10 +++-
07cb6b
 4 files changed, 50 insertions(+), 16 deletions(-)
07cb6b
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/ansible/shared.yml b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/ansible/shared.yml
07cb6b
index 84a4f084d40..f30dcdb2ed3 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/ansible/shared.yml
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/ansible/shared.yml
07cb6b
@@ -5,4 +5,10 @@
07cb6b
 # disruption = low
07cb6b
 {{{ ansible_instantiate_variables("var_rekey_limit_size", "var_rekey_limit_time") }}}
07cb6b
 
07cb6b
-{{{ ansible_sshd_set(parameter="RekeyLimit", value="{{ var_rekey_limit_size }} {{ var_rekey_limit_time }}") }}}
07cb6b
+{{{
07cb6b
+    ansible_sshd_set(
07cb6b
+        parameter="RekeyLimit",
07cb6b
+        value="{{ var_rekey_limit_size }} {{ var_rekey_limit_time }}",
07cb6b
+        config_is_distributed=sshd_distributed_config
07cb6b
+    )
07cb6b
+}}}
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/bash/shared.sh b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/bash/shared.sh
07cb6b
index 4422f63472c..789358472a1 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/bash/shared.sh
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/bash/shared.sh
07cb6b
@@ -2,4 +2,4 @@
07cb6b
 
07cb6b
 {{{ bash_instantiate_variables("var_rekey_limit_size", "var_rekey_limit_time") }}}
07cb6b
 
07cb6b
-{{{ bash_sshd_config_set(parameter='RekeyLimit', value="$var_rekey_limit_size $var_rekey_limit_time") }}}
07cb6b
+{{{ bash_sshd_remediation(parameter='RekeyLimit', value="$var_rekey_limit_size $var_rekey_limit_time", config_is_distributed=sshd_distributed_config) -}}}
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
index f49d9ab5275..e109cbd3124 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
@@ -1,26 +1,49 @@
07cb6b
-{{% set filepath = "/etc/ssh/sshd_config" -%}}
07cb6b
-
07cb6b
+{{%- set parameter = "RekeyLimit" %}}
07cb6b
+{{%- set sshd_config_path = "/etc/ssh/sshd_config" %}}
07cb6b
+{{%- set sshd_config_dir = "/etc/ssh/sshd_config.d" -%}}
07cb6b
+{{%- set description = "Ensure RekeyLimit is configured with the appropriate value in " ~ sshd_config_path %}}
07cb6b
+{{%- if sshd_distributed_config == "true" %}}
07cb6b
+{{%- set description = description  ~ " or in " ~ sshd_config_dir -%}}
07cb6b
+{{%- endif %}}
07cb6b
 
07cb6b
 <def-group>
07cb6b
   <definition class="compliance" id="{{{ rule_id }}}" version="1">
07cb6b
-    {{{ oval_metadata("Ensure 'RekeyLimit' is configured with the correct value in '" + filepath + "'") }}}
07cb6b
-    <criteria comment="sshd is configured correctly or is not installed" operator="OR">
07cb6b
-        {{{- application_not_required_or_requirement_unset() }}}
07cb6b
-        {{{- application_required_or_requirement_unset() }}}
07cb6b
-        {{{- oval_line_in_file_criterion(filepath, "RekeyLimit") }}}
07cb6b
-    </criteria>
07cb6b
-    </criteria>
07cb6b
+  {{{ oval_metadata(description) }}}
07cb6b
+  <criteria comment="sshd is configured correctly or is not installed" operator="OR">
07cb6b
+    {{{- application_not_required_or_requirement_unset() }}}
07cb6b
+    {{{- application_required_or_requirement_unset() }}}
07cb6b
+      <criteria comment="sshd is configured corectly" operator="OR">
07cb6b
+        {{{- oval_line_in_file_criterion(sshd_config_path, parameter) }}}
07cb6b
+        {{%- if sshd_distributed_config %}}
07cb6b
+        {{{- oval_line_in_directory_criterion(sshd_config_dir, parameter) | indent(8) }}}
07cb6b
+        {{%- endif %}}
07cb6b
+      </criteria>
07cb6b
+    </criteria>
07cb6b
+  </criteria>
07cb6b
   </definition>
07cb6b
 
07cb6b
-  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of RekeyLimit setting in the  file" id="test_sshd_rekey_limit" version="1">
07cb6b
+  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of RekeyLimit setting in the file" id="test_sshd_rekey_limit" version="1">
07cb6b
      <ind:object object_ref="obj_sshd_rekey_limit"/>
07cb6b
   </ind:textfilecontent54_test>
07cb6b
 
07cb6b
   <ind:textfilecontent54_object id="obj_sshd_rekey_limit" version="1">
07cb6b
-     <ind:filepath>{{{ filepath }}}</ind:filepath>
07cb6b
+     <ind:filepath>{{{ sshd_config_path }}}</ind:filepath>
07cb6b
+     <ind:pattern var_ref="sshd_line_regex" operation="pattern match"></ind:pattern>
07cb6b
+     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
07cb6b
+  </ind:textfilecontent54_object>
07cb6b
+
07cb6b
+  {{%- if sshd_distributed_config %}}
07cb6b
+  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of RekeyLimit setting in SSHD config directory" id="test_sshd_rekey_limit_config_dir" version="1">
07cb6b
+     <ind:object object_ref="obj_sshd_rekey_limit_config_dir"/>
07cb6b
+  </ind:textfilecontent54_test>
07cb6b
+
07cb6b
+  <ind:textfilecontent54_object id="obj_sshd_rekey_limit_config_dir" version="1">
07cb6b
+     <ind:path>{{{ sshd_config_dir}}}</ind:path>
07cb6b
+     <ind:filename operation="pattern match">.*\.conf$</ind:filename>
07cb6b
      <ind:pattern var_ref="sshd_line_regex" operation="pattern match"></ind:pattern>
07cb6b
      <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
07cb6b
   </ind:textfilecontent54_object>
07cb6b
+  {{%- endif %}}
07cb6b
 
07cb6b
   <local_variable id="sshd_line_regex" datatype="string" comment="The regex of the directive" version="1">
07cb6b
     <concat>
07cb6b
@@ -35,4 +58,3 @@
07cb6b
   <external_variable comment="Size component of the rekey limit" datatype="string" id="var_rekey_limit_size" version="1" />
07cb6b
   <external_variable comment="Time component of the rekey limit" datatype="string" id="var_rekey_limit_time" version="1" />
07cb6b
 </def-group>
07cb6b
-
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/rule.yml b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/rule.yml
07cb6b
index 450f244de41..702cd0506d3 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/rule.yml
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/rule.yml
07cb6b
@@ -6,8 +6,10 @@ description: |-
07cb6b
     The <tt>RekeyLimit</tt> parameter specifies how often
07cb6b
     the session key of the is renegotiated, both in terms of
07cb6b
     amount of data that may be transmitted and the time
07cb6b
-    elapsed. To decrease the default limits, put line
07cb6b
-    <tt>RekeyLimit {{{ xccdf_value("var_rekey_limit_size") }}} {{{ xccdf_value("var_rekey_limit_time") }}}</tt> to file <tt>/etc/ssh/sshd_config</tt>.
07cb6b
+    elapsed.
07cb6b
+    To decrease the default limits, add or correct the following line in
07cb6b
+    {{{ sshd_config_file() }}}
07cb6b
+    
RekeyLimit {{{ xccdf_value("var_rekey_limit_size") }}} {{{ xccdf_value("var_rekey_limit_time") }}}
07cb6b
 
07cb6b
 rationale: |-
07cb6b
     By decreasing the limit based on the amount of data and enabling
07cb6b
@@ -32,6 +34,10 @@ ocil_clause: 'it is commented out or is not set'
07cb6b
 ocil: |-
07cb6b
     To check if RekeyLimit is set correctly, run the
07cb6b
     following command:
07cb6b
+    {{% if sshd_distributed_config == "true" %}}
07cb6b
+    
$ sudo grep RekeyLimit /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
07cb6b
+    {{% else %}}
07cb6b
     
$ sudo grep RekeyLimit /etc/ssh/sshd_config
07cb6b
+    {{% endif %}}
07cb6b
     If configured properly, output should be
07cb6b
     
RekeyLimit {{{ xccdf_value("var_rekey_limit_size") }}} {{{ xccdf_value("var_rekey_limit_time") }}}
07cb6b
07cb6b
From 78d6d40f280b0e43e6c8fd7d60cfd81e7979fb8f Mon Sep 17 00:00:00 2001
07cb6b
From: Watson Sato <wsato@redhat.com>
07cb6b
Date: Wed, 9 Feb 2022 16:59:53 +0100
07cb6b
Subject: [PATCH 3/3] Use the Jinja variable 'parameter' where applicable
07cb6b
07cb6b
---
07cb6b
 .../ssh/ssh_server/sshd_rekey_limit/oval/shared.xml       | 8 ++++----
07cb6b
 1 file changed, 4 insertions(+), 4 deletions(-)
07cb6b
07cb6b
diff --git a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
index e109cbd3124..d79ac7f2047 100644
07cb6b
--- a/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
+++ b/linux_os/guide/services/ssh/ssh_server/sshd_rekey_limit/oval/shared.xml
07cb6b
@@ -1,7 +1,7 @@
07cb6b
 {{%- set parameter = "RekeyLimit" %}}
07cb6b
 {{%- set sshd_config_path = "/etc/ssh/sshd_config" %}}
07cb6b
 {{%- set sshd_config_dir = "/etc/ssh/sshd_config.d" -%}}
07cb6b
-{{%- set description = "Ensure RekeyLimit is configured with the appropriate value in " ~ sshd_config_path %}}
07cb6b
+{{%- set description = "Ensure {{{ parameter }}} is configured with the appropriate value in " ~ sshd_config_path %}}
07cb6b
 {{%- if sshd_distributed_config == "true" %}}
07cb6b
 {{%- set description = description  ~ " or in " ~ sshd_config_dir -%}}
07cb6b
 {{%- endif %}}
07cb6b
@@ -22,7 +22,7 @@
07cb6b
   </criteria>
07cb6b
   </definition>
07cb6b
 
07cb6b
-  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of RekeyLimit setting in the file" id="test_sshd_rekey_limit" version="1">
07cb6b
+  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of {{{ parameter }}} setting in the file" id="test_sshd_rekey_limit" version="1">
07cb6b
      <ind:object object_ref="obj_sshd_rekey_limit"/>
07cb6b
   </ind:textfilecontent54_test>
07cb6b
 
07cb6b
@@ -33,7 +33,7 @@
07cb6b
   </ind:textfilecontent54_object>
07cb6b
 
07cb6b
   {{%- if sshd_distributed_config %}}
07cb6b
-  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of RekeyLimit setting in SSHD config directory" id="test_sshd_rekey_limit_config_dir" version="1">
07cb6b
+  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="tests the value of {{{ parameter }}} setting in SSHD config directory" id="test_sshd_rekey_limit_config_dir" version="1">
07cb6b
      <ind:object object_ref="obj_sshd_rekey_limit_config_dir"/>
07cb6b
   </ind:textfilecontent54_test>
07cb6b
 
07cb6b
@@ -47,7 +47,7 @@
07cb6b
 
07cb6b
   <local_variable id="sshd_line_regex" datatype="string" comment="The regex of the directive" version="1">
07cb6b
     <concat>
07cb6b
-      <literal_component>^[\s]*RekeyLimit[\s]+</literal_component>
07cb6b
+      <literal_component>^[\s]*{{{ parameter }}}[\s]+</literal_component>
07cb6b
       <variable_component var_ref="var_rekey_limit_size"/>
07cb6b
       <literal_component>[\s]+</literal_component>
07cb6b
       <variable_component var_ref="var_rekey_limit_time"/>