Blame SOURCES/scap-security-guide-0.1.51-create_macro_selinux_remediation_PR_5785.patch

475544
From a5281d8361dd26217e6ee1c97d5beaae02af34bc Mon Sep 17 00:00:00 2001
475544
From: Gabriel Becker <ggasparb@redhat.com>
475544
Date: Tue, 26 May 2020 17:49:21 +0200
475544
Subject: [PATCH 1/2] Create macro for selinux ansible/bash remediation.
475544
475544
Affected rules:
475544
 - selinux_policytype
475544
 - selinux_state
475544
---
475544
 .../selinux/selinux_policytype/ansible/shared.yml |  9 ++-------
475544
 .../selinux/selinux_policytype/bash/shared.sh     |  5 +++--
475544
 .../tests/selinuxtype_minimum.fail.sh             | 10 ++++++++++
475544
 .../selinux/selinux_state/ansible/shared.yml      |  9 ++-------
475544
 .../system/selinux/selinux_state/bash/shared.sh   |  5 +++--
475544
 .../selinux_state/tests/selinux_missing.fail.sh   |  5 +++++
475544
 .../tests/selinux_permissive.fail.sh              | 10 ++++++++++
475544
 shared/macros-ansible.jinja                       | 11 +++++++++++
475544
 shared/macros-bash.jinja                          | 15 +++++++++++++++
475544
 9 files changed, 61 insertions(+), 18 deletions(-)
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_state/tests/selinux_missing.fail.sh
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_state/tests/selinux_permissive.fail.sh
475544
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml b/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
index 5c70cc9f7f..9f8cf66dfb 100644
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
@@ -3,11 +3,6 @@
475544
 # strategy = restrict
475544
 # complexity = low
475544
 # disruption = low
475544
 - (xccdf-var var_selinux_policy_name)
475544
 
475544
-- name: "{{{ rule_title }}}"
475544
-  lineinfile:
475544
-    path: /etc/sysconfig/selinux
475544
-    regexp: '^SELINUXTYPE='
475544
-    line: "SELINUXTYPE={{ var_selinux_policy_name }}"
475544
-    create: yes
475544
+{{{ ansible_selinux_config_set(parameter="SELINUXTYPE", value="{{ var_selinux_policy_name }}") }}}
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh b/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
index d0fbbf4446..2b5ce31b12 100644
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
@@ -1,7 +1,8 @@
475544
 # platform = multi_platform_wrlinux,multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv
475544
-#
475544
+
475544
 # Include source function library.
475544
 . /usr/share/scap-security-guide/remediation_functions
475544
+
475544
 populate var_selinux_policy_name
475544
 
475544
-replace_or_append '/etc/sysconfig/selinux' '^SELINUXTYPE=' $var_selinux_policy_name '@CCENUM@' '%s=%s'
475544
+{{{ bash_selinux_config_set(parameter="SELINUXTYPE", value="$var_selinux_policy_name") }}}
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh b/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
new file mode 100644
475544
index 0000000000..1a6eb94953
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
@@ -0,0 +1,10 @@
475544
+#!/bin/bash
475544
+# profiles = xccdf_org.ssgproject.content_profile_C2S, xccdf_org.ssgproject.content_profile_ospp
475544
+
475544
+SELINUX_FILE='/etc/selinux/config'
475544
+
475544
+if grep -s '^[[:space:]]*SELINUXTYPE' $SELINUX_FILE; then
475544
+	sed -i 's/^\([[:space:]]*SELINUXTYPE[[:space:]]*=[[:space:]]*\).*/\minimum/' $SELINUX_FILE
475544
+else
475544
+	echo 'SELINUXTYPE=minimum' >> $SELINUX_FILE
475544
+fi
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/ansible/shared.yml b/linux_os/guide/system/selinux/selinux_state/ansible/shared.yml
475544
index b465ac6729..1c1560a86c 100644
475544
--- a/linux_os/guide/system/selinux/selinux_state/ansible/shared.yml
475544
+++ b/linux_os/guide/system/selinux/selinux_state/ansible/shared.yml
475544
@@ -3,11 +3,6 @@
475544
 # strategy = restrict
475544
 # complexity = low
475544
 # disruption = low
475544
 - (xccdf-var var_selinux_state)
475544
 
475544
-- name: "{{{ rule_title }}}"
475544
-  lineinfile:
475544
-    path: /etc/sysconfig/selinux
475544
-    regexp: '^SELINUX='
475544
-    line: "SELINUX={{ var_selinux_state }}"
475544
-    create: yes
475544
+{{{ ansible_selinux_config_set(parameter="SELINUX", value="{{ var_selinux_state }}") }}}
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/bash/shared.sh b/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
index 58193b5504..a402a861d7 100644
475544
--- a/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
+++ b/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
@@ -1,10 +1,11 @@
475544
 # platform = multi_platform_wrlinux,multi_platform_rhel,multi_platform_fedora,multi_platorm_ol,multi_platform_rhv
475544
-#
475544
+
475544
 # Include source function library.
475544
 . /usr/share/scap-security-guide/remediation_functions
475544
+
475544
 populate var_selinux_state
475544
 
475544
-replace_or_append '/etc/sysconfig/selinux' '^SELINUX=' $var_selinux_state '@CCENUM@' '%s=%s'
475544
+{{{ bash_selinux_config_set(parameter="SELINUX", value="$var_selinux_state") }}}
475544
 
475544
 fixfiles onboot
475544
 fixfiles -f relabel
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/tests/selinux_missing.fail.sh b/linux_os/guide/system/selinux/selinux_state/tests/selinux_missing.fail.sh
475544
new file mode 100644
475544
index 0000000000..180dd80791
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_state/tests/selinux_missing.fail.sh
475544
@@ -0,0 +1,5 @@
475544
+#!/bin/bash
475544
+# profiles = xccdf_org.ssgproject.content_profile_C2S, xccdf_org.ssgproject.content_profile_ospp
475544
+
475544
+SELINUX_FILE='/etc/selinux/config'
475544
+sed -i '/^[[:space:]]*SELINUX/d' $SELINUX_FILE
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/tests/selinux_permissive.fail.sh b/linux_os/guide/system/selinux/selinux_state/tests/selinux_permissive.fail.sh
475544
new file mode 100644
475544
index 0000000000..3db1e56b5f
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_state/tests/selinux_permissive.fail.sh
475544
@@ -0,0 +1,10 @@
475544
+#!/bin/bash
475544
+# profiles = xccdf_org.ssgproject.content_profile_C2S, xccdf_org.ssgproject.content_profile_ospp
475544
+
475544
+SELINUX_FILE='/etc/selinux/config'
475544
+
475544
+if grep -s '^[[:space:]]*SELINUX' $SELINUX_FILE; then
475544
+	sed -i 's/^\([[:space:]]*SELINUX[[:space:]]*=[[:space:]]*\).*/\permissive/' $SELINUX_FILE
475544
+else
475544
+	echo 'SELINUX=permissive' >> $SELINUX_FILE
475544
+fi
475544
diff --git a/shared/macros-ansible.jinja b/shared/macros-ansible.jinja
475544
index 6798a25d1f..01d3155b37 100644
475544
--- a/shared/macros-ansible.jinja
475544
+++ b/shared/macros-ansible.jinja
475544
@@ -217,6 +217,17 @@ value: "Setting={{ varname1 }}"
475544
 {{{ ansible_set_config_file(msg, "/etc/systemd/coredump.conf", parameter=parameter, value=value, create="no", separator="=", separator_regex="\s*=\s*") }}}
475544
 {{%- endmacro %}}
475544
 
475544
+{{#
475544
+  High level macro to set a parameter in /etc/selinux/config.
475544
+  Parameters:
475544
+  - msg: the name for the Ansible task
475544
+  - parameter: parameter to be set in the configuration file
475544
+  - value: value of the parameter
475544
+#}}
475544
+{{%- macro ansible_selinux_config_set(msg='', parameter='', value='') %}}
475544
+{{{ ansible_set_config_file(msg, "/etc/selinux/config", parameter=parameter, value=value, create="no", separator="=", separator_regex="\s*=\s*") }}}
475544
+{{%- endmacro %}}
475544
+
475544
 {{#
475544
   Generates an Ansible task that puts 'contents' into a file at 'filepath'
475544
   Parameters:
475544
diff --git a/shared/macros-bash.jinja b/shared/macros-bash.jinja
475544
index 3a94fe5dd8..2531d1c52d 100644
475544
--- a/shared/macros-bash.jinja
475544
+++ b/shared/macros-bash.jinja
475544
@@ -86,6 +86,21 @@ populate {{{ name }}}
475544
     }}}
475544
 {{%- endmacro -%}}
475544
 
475544
+{{%- macro bash_selinux_config_set(parameter, value) -%}}
475544
+{{{ set_config_file(
475544
+        path="/etc/selinux/config",
475544
+        parameter=parameter,
475544
+        value=value,
475544
+        create=true,
475544
+        insert_after="",
475544
+        insert_before="",
475544
+        insensitive=true,
475544
+        separator="=",
475544
+        separator_regex="\s*=\s*",
475544
+        prefix_regex="^\s*")
475544
+    }}}
475544
+{{%- endmacro -%}}
475544
+
475544
 {{#
475544
 # Install a package
475544
 # Uses the right command based on pkg_manger proprerty defined in product.yaml.
475544
475544
From 24c3c92007e6d3f8a684282b1351703523441389 Mon Sep 17 00:00:00 2001
475544
From: Gabriel Becker <ggasparb@redhat.com>
475544
Date: Wed, 27 May 2020 18:48:57 +0200
475544
Subject: [PATCH 2/2] Remediation requires reboot.
475544
475544
Update OVAL check to disallow spaces.
475544
Removed selinuxtype_minimum test scenario since breaks the system.
475544
---
475544
 .../selinux/selinux_policytype/ansible/shared.yml      |  2 +-
475544
 .../system/selinux/selinux_policytype/bash/shared.sh   |  4 ++++
475544
 .../system/selinux/selinux_policytype/oval/shared.xml  |  2 +-
475544
 .../tests/selinuxtype_minimum.fail.sh                  | 10 ----------
475544
 .../guide/system/selinux/selinux_state/bash/shared.sh  |  4 ++++
475544
 .../guide/system/selinux/selinux_state/oval/shared.xml |  2 +-
475544
 shared/macros-ansible.jinja                            |  2 +-
475544
 shared/macros-bash.jinja                               |  4 ++--
475544
 8 files changed, 14 insertions(+), 16 deletions(-)
475544
 delete mode 100644 linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml b/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
index 9f8cf66dfb..73e6ec7cd4 100644
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/ansible/shared.yml
475544
@@ -1,5 +1,5 @@
475544
 # platform = multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv
475544
-# reboot = false
475544
+# reboot = true
475544
 # strategy = restrict
475544
 # complexity = low
475544
 # disruption = low
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh b/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
index 2b5ce31b12..b4f79c97f9 100644
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/bash/shared.sh
475544
@@ -1,4 +1,8 @@
475544
 # platform = multi_platform_wrlinux,multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv
475544
+# reboot = true
475544
+# strategy = restrict
475544
+# complexity = low
475544
+# disruption = low
475544
 
475544
 # Include source function library.
475544
 . /usr/share/scap-security-guide/remediation_functions
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/oval/shared.xml b/linux_os/guide/system/selinux/selinux_policytype/oval/shared.xml
475544
index f1840a1290..3d69fff07f 100644
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/oval/shared.xml
475544
+++ b/linux_os/guide/system/selinux/selinux_policytype/oval/shared.xml
475544
@@ -27,7 +27,7 @@
475544
 
475544
   <ind:textfilecontent54_object id="obj_selinux_policy" version="1">
475544
     <ind:filepath>/etc/selinux/config</ind:filepath>
475544
-    <ind:pattern operation="pattern match">^[\s]*SELINUXTYPE[\s]*=[\s]*([^\s]*)</ind:pattern>
475544
+    <ind:pattern operation="pattern match">^SELINUXTYPE=(.*)$</ind:pattern>
475544
     <ind:instance datatype="int">1</ind:instance>
475544
   </ind:textfilecontent54_object>
475544
 </def-group>
475544
diff --git a/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh b/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
deleted file mode 100644
475544
index 1a6eb94953..0000000000
475544
--- a/linux_os/guide/system/selinux/selinux_policytype/tests/selinuxtype_minimum.fail.sh
475544
+++ /dev/null
475544
@@ -1,10 +0,0 @@
475544
-#!/bin/bash
475544
-# profiles = xccdf_org.ssgproject.content_profile_C2S, xccdf_org.ssgproject.content_profile_ospp
475544
-
475544
-SELINUX_FILE='/etc/selinux/config'
475544
-
475544
-if grep -s '^[[:space:]]*SELINUXTYPE' $SELINUX_FILE; then
475544
-	sed -i 's/^\([[:space:]]*SELINUXTYPE[[:space:]]*=[[:space:]]*\).*/\minimum/' $SELINUX_FILE
475544
-else
475544
-	echo 'SELINUXTYPE=minimum' >> $SELINUX_FILE
475544
-fi
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/bash/shared.sh b/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
index a402a861d7..645a7acab4 100644
475544
--- a/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
+++ b/linux_os/guide/system/selinux/selinux_state/bash/shared.sh
475544
@@ -1,4 +1,8 @@
475544
 # platform = multi_platform_wrlinux,multi_platform_rhel,multi_platform_fedora,multi_platorm_ol,multi_platform_rhv
475544
+# reboot = true
475544
+# strategy = restrict
475544
+# complexity = low
475544
+# disruption = low
475544
 
475544
 # Include source function library.
475544
 . /usr/share/scap-security-guide/remediation_functions
475544
diff --git a/linux_os/guide/system/selinux/selinux_state/oval/shared.xml b/linux_os/guide/system/selinux/selinux_state/oval/shared.xml
475544
index c0881696e1..8c328060af 100644
475544
--- a/linux_os/guide/system/selinux/selinux_state/oval/shared.xml
475544
+++ b/linux_os/guide/system/selinux/selinux_state/oval/shared.xml
475544
@@ -18,7 +18,7 @@
475544
 
475544
   <ind:textfilecontent54_object id="object_etc_selinux_config" version="1">
475544
     <ind:filepath>/etc/selinux/config</ind:filepath>
475544
-    <ind:pattern operation="pattern match">^[\s]*SELINUX[\s]*=[\s]*(.*)[\s]*$</ind:pattern>
475544
+    <ind:pattern operation="pattern match">^SELINUX=(.*)$</ind:pattern>
475544
     <ind:instance datatype="int">1</ind:instance>
475544
   </ind:textfilecontent54_object>
475544
 
475544
diff --git a/shared/macros-ansible.jinja b/shared/macros-ansible.jinja
475544
index 01d3155b37..580a0b948e 100644
475544
--- a/shared/macros-ansible.jinja
475544
+++ b/shared/macros-ansible.jinja
475544
@@ -225,7 +225,7 @@ value: "Setting={{ varname1 }}"
475544
   - value: value of the parameter
475544
 #}}
475544
 {{%- macro ansible_selinux_config_set(msg='', parameter='', value='') %}}
475544
-{{{ ansible_set_config_file(msg, "/etc/selinux/config", parameter=parameter, value=value, create="no", separator="=", separator_regex="\s*=\s*") }}}
475544
+{{{ ansible_set_config_file(msg, "/etc/selinux/config", parameter=parameter, value=value, create="yes", separator="=", separator_regex="=", prefix_regex='^') }}}
475544
 {{%- endmacro %}}
475544
 
475544
 {{#
475544
diff --git a/shared/macros-bash.jinja b/shared/macros-bash.jinja
475544
index 2531d1c52d..8abcc914d3 100644
475544
--- a/shared/macros-bash.jinja
475544
+++ b/shared/macros-bash.jinja
475544
@@ -96,8 +96,8 @@ populate {{{ name }}}
475544
         insert_before="",
475544
         insensitive=true,
475544
         separator="=",
475544
-        separator_regex="\s*=\s*",
475544
-        prefix_regex="^\s*")
475544
+        separator_regex="=",
475544
+        prefix_regex="^")
475544
     }}}
475544
 {{%- endmacro -%}}
475544