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

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