Blame SOURCES/scap-security-guide-0.1.53-add-bash-platform_PR_6061.patch

973b04
From c05cce1a4a5eb95be857b07948fda0c95cdaa106 Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Tue, 8 Sep 2020 14:36:07 +0200
973b04
Subject: [PATCH 1/5] Align Bash applicability with CPE platform
973b04
973b04
Wraps the remediation of rules with Packager CPE Platform
973b04
with an if condition that checks for the respective
973b04
platforms's package.
973b04
---
973b04
 ssg/build_remediations.py | 45 +++++++++++++++++++++++++++++++++++++++
973b04
 1 file changed, 45 insertions(+)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index ccbdf9fc1f..2d4a805e78 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -27,6 +27,13 @@
973b04
     'kubernetes': '.yml'
973b04
 }
973b04
 
973b04
+PKG_MANAGER_TO_PACKAGE_CHECK_COMMAND = {
973b04
+    'apt_get': 'dpkg-query -s {} &>/dev/null',
973b04
+    'dnf': 'rpm --quiet -q {}',
973b04
+    'yum': 'rpm --quiet -q {}',
973b04
+    'zypper': 'rpm --quiet -q {}',
973b04
+}
973b04
+
973b04
 FILE_GENERATED_HASH_COMMENT = '# THIS FILE IS GENERATED'
973b04
 
973b04
 REMEDIATION_CONFIG_KEYS = ['complexity', 'disruption', 'platform', 'reboot',
973b04
@@ -262,6 +269,44 @@ class BashRemediation(Remediation):
973b04
     def __init__(self, file_path):
973b04
         super(BashRemediation, self).__init__(file_path, "bash")
973b04
 
973b04
+    def parse_from_file_with_jinja(self, env_yaml):
973b04
+        self.local_env_yaml.update(env_yaml)
973b04
+        result = super(BashRemediation, self).parse_from_file_with_jinja(self.local_env_yaml)
973b04
+
973b04
+        # There can be repeated inherited platforms and rule platforms
973b04
+        rule_platforms = set(self.associated_rule.inherited_platforms)
973b04
+        rule_platforms.add(self.associated_rule.platform)
973b04
+
973b04
+        platform_conditionals = []
973b04
+        for platform in rule_platforms:
973b04
+            if platform == "machine":
973b04
+                # Based on check installed_env_is_a_container
973b04
+                platform_conditionals.append('[ ! -f /.dockerenv -a ! -f /run/.containerenv ]')
973b04
+            elif platform is not None:
973b04
+                # Assume any other platform is a Package CPE
973b04
+
973b04
+                # Some package names are different from the platform names
973b04
+                if platform in self.local_env_yaml["platform_package_overrides"]:
973b04
+                    platform = self.local_env_yaml["platform_package_overrides"].get(platform)
973b04
+
973b04
+                # Adjust package check command according to the pkg_manager
973b04
+                pkg_manager = self.local_env_yaml["pkg_manager"]
973b04
+                pkg_check_command = PKG_MANAGER_TO_PACKAGE_CHECK_COMMAND[pkg_manager]
973b04
+                platform_conditionals.append(pkg_check_command.format(platform))
973b04
+
973b04
+        if platform_conditionals:
973b04
+            platform_fix_text = "# Remediation is applicable only in certain platforms\n"
973b04
+
973b04
+            cond = platform_conditionals.pop(0)
973b04
+            platform_fix_text += "if {}".format(cond)
973b04
+            for cond in platform_conditionals:
973b04
+                platform_fix_text += " && {}".format(cond)
973b04
+            platform_fix_text += '; then\n{}\nelse\necho "Remediation is not applicable, nothing was done"\nfi'.format(result.contents)
973b04
+
973b04
+            remediation = namedtuple('remediation', ['contents', 'config'])
973b04
+            result = remediation(contents=platform_fix_text, config=result.config)
973b04
+
973b04
+        return result
973b04
 
973b04
 class AnsibleRemediation(Remediation):
973b04
     def __init__(self, file_path):
973b04
973b04
From 19e0c3b709e091159655d37b8ce5d693750f0a81 Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Tue, 8 Sep 2020 14:41:01 +0200
973b04
Subject: [PATCH 2/5] Handle Bash platform wrapping in xccdf expansion
973b04
973b04
Adjust expansion of subs and variables not to remove the whole beginning
973b04
of the fix test. This was removing the package conditional wrapping.
973b04
---
973b04
 ssg/build_remediations.py | 21 ++++++++++++---------
973b04
 1 file changed, 12 insertions(+), 9 deletions(-)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index 2d4a805e78..49ec557000 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -736,14 +736,16 @@ def expand_xccdf_subs(fix, remediation_type, remediation_functions):
973b04
         patcomp = re.compile(pattern, re.DOTALL)
973b04
         fixparts = re.split(patcomp, fix.text)
973b04
         if fixparts[0] is not None:
973b04
-            # Split the portion of fix.text from fix start to first call of
973b04
-            # remediation function, keeping only the third part:
973b04
-            # * tail        to hold part of the fix.text after inclusion,
973b04
-            #               but before first call of remediation function
973b04
+            # Split the portion of fix.text at the string remediation_functions,
973b04
+            # and remove preceeding comment whenever it is there.
973b04
+            # * head        holds part of the fix.text before
973b04
+            #               remediation_functions string
973b04
+            # * tail        holds part of the fix.text after the
973b04
+            #               remediation_functions string
973b04
             try:
973b04
-                rfpattern = '(.*remediation_functions)(.*)'
973b04
-                rfpatcomp = re.compile(rfpattern, re.DOTALL)
973b04
-                _, _, tail, _ = re.split(rfpatcomp, fixparts[0], maxsplit=2)
973b04
+                rfpattern = r'((?:# Include source function library\.\n)?.*remediation_functions)'
973b04
+                rfpatcomp = re.compile(rfpattern)
973b04
+                head, _, tail = re.split(rfpatcomp, fixparts[0], maxsplit=1)
973b04
             except ValueError:
973b04
                 sys.stderr.write("Processing fix.text for: %s rule\n"
973b04
                                  % fix.get('rule'))
973b04
@@ -751,9 +753,10 @@ def expand_xccdf_subs(fix, remediation_type, remediation_functions):
973b04
                                  "after inclusion of remediation functions."
973b04
                                  " Aborting..\n")
973b04
                 sys.exit(1)
973b04
-            # If the 'tail' is not empty, make it new fix.text.
973b04
+            # If the 'head' is not empty, make it new fix.text.
973b04
             # Otherwise use ''
973b04
-            fix.text = tail if tail is not None else ''
973b04
+            fix.text = head if head is not None else ''
973b04
+            fix.text += tail if tail is not None else ''
973b04
             # Drop the first element of 'fixparts' since it has been processed
973b04
             fixparts.pop(0)
973b04
             # Perform sanity check on new 'fixparts' list content (to continue
973b04
973b04
From 1292b93dc35a9a308464f1effb7f10f8de6db457 Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Tue, 8 Sep 2020 20:56:17 +0200
973b04
Subject: [PATCH 3/5] Check if remediation has associated rule before use
973b04
973b04
---
973b04
 ssg/build_remediations.py | 8 +++++---
973b04
 1 file changed, 5 insertions(+), 3 deletions(-)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index 49ec557000..85f7139d8f 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -273,9 +273,11 @@ def parse_from_file_with_jinja(self, env_yaml):
973b04
         self.local_env_yaml.update(env_yaml)
973b04
         result = super(BashRemediation, self).parse_from_file_with_jinja(self.local_env_yaml)
973b04
 
973b04
-        # There can be repeated inherited platforms and rule platforms
973b04
-        rule_platforms = set(self.associated_rule.inherited_platforms)
973b04
-        rule_platforms.add(self.associated_rule.platform)
973b04
+        rule_platforms = set()
973b04
+        if self.associated_rule:
973b04
+            # There can be repeated inherited platforms and rule platforms
973b04
+            rule_platforms.update(self.associated_rule.inherited_platforms)
973b04
+            rule_platforms.add(self.associated_rule.platform)
973b04
 
973b04
         platform_conditionals = []
973b04
         for platform in rule_platforms:
973b04
973b04
From 7953a02e61bb56b501c56f46972247751292dcbb Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Thu, 10 Sep 2020 10:59:43 +0200
973b04
Subject: [PATCH 4/5] Fix python2 compat and improve code readability
973b04
973b04
---
973b04
 ssg/build_remediations.py | 29 ++++++++++++++++++-----------
973b04
 1 file changed, 18 insertions(+), 11 deletions(-)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index 85f7139d8f..673d6d0cc6 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -28,10 +28,10 @@
973b04
 }
973b04
 
973b04
 PKG_MANAGER_TO_PACKAGE_CHECK_COMMAND = {
973b04
-    'apt_get': 'dpkg-query -s {} &>/dev/null',
973b04
-    'dnf': 'rpm --quiet -q {}',
973b04
-    'yum': 'rpm --quiet -q {}',
973b04
-    'zypper': 'rpm --quiet -q {}',
973b04
+    'apt_get': 'dpkg-query -s {0} &>/dev/null',
973b04
+    'dnf': 'rpm --quiet -q {0}',
973b04
+    'yum': 'rpm --quiet -q {0}',
973b04
+    'zypper': 'rpm --quiet -q {0}',
973b04
 }
973b04
 
973b04
 FILE_GENERATED_HASH_COMMENT = '# THIS FILE IS GENERATED'
973b04
@@ -297,16 +297,23 @@ def parse_from_file_with_jinja(self, env_yaml):
973b04
                 platform_conditionals.append(pkg_check_command.format(platform))
973b04
 
973b04
         if platform_conditionals:
973b04
-            platform_fix_text = "# Remediation is applicable only in certain platforms\n"
973b04
+            wrapped_fix_text = ["# Remediation is applicable only in certain platforms"]
973b04
 
973b04
-            cond = platform_conditionals.pop(0)
973b04
-            platform_fix_text += "if {}".format(cond)
973b04
-            for cond in platform_conditionals:
973b04
-                platform_fix_text += " && {}".format(cond)
973b04
-            platform_fix_text += '; then\n{}\nelse\necho "Remediation is not applicable, nothing was done"\nfi'.format(result.contents)
973b04
+            all_conditions = " && ".join(platform_conditionals)
973b04
+            wrapped_fix_text.append("if {0}; then".format(all_conditions))
973b04
+
973b04
+            # Avoid adding extra blank line
973b04
+            if not result.contents.startswith("\n"):
973b04
+                wrapped_fix_text.append("")
973b04
+
973b04
+            wrapped_fix_text.append("{0}".format(result.contents))
973b04
+            wrapped_fix_text.append("")
973b04
+            wrapped_fix_text.append("else")
973b04
+            wrapped_fix_text.append("    >&2 echo 'Remediation is not applicable, nothing was done'")
973b04
+            wrapped_fix_text.append("fi")
973b04
 
973b04
             remediation = namedtuple('remediation', ['contents', 'config'])
973b04
-            result = remediation(contents=platform_fix_text, config=result.config)
973b04
+            result = remediation(contents="\n".join(wrapped_fix_text), config=result.config)
973b04
 
973b04
         return result
973b04
 
973b04
973b04
From 0bd3912651367c64789bb3d67b44c3b8848708c0 Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Thu, 10 Sep 2020 17:25:27 +0200
973b04
Subject: [PATCH 5/5] Document the perils of indenting wrapped Bash fixes
973b04
973b04
---
973b04
 ssg/build_remediations.py | 3 +++
973b04
 1 file changed, 3 insertions(+)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index 673d6d0cc6..f269d4d2d6 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -306,6 +306,9 @@ def parse_from_file_with_jinja(self, env_yaml):
973b04
             if not result.contents.startswith("\n"):
973b04
                 wrapped_fix_text.append("")
973b04
 
973b04
+            # It is possible to indent the original body of the remediation with textwrap.indent(),
973b04
+            # however, it is not supported by python2, and there is a risk of breaking remediations
973b04
+            # For example, remediations with a here-doc block could be affected.
973b04
             wrapped_fix_text.append("{0}".format(result.contents))
973b04
             wrapped_fix_text.append("")
973b04
             wrapped_fix_text.append("else")