Blame SOURCES/scap-security-guide-0.1.61-update_RHEL_08_010385-PR_8220.patch

0c1482
From 573ae69742cf372d41da6c56a3051745326055cd Mon Sep 17 00:00:00 2001
0c1482
From: Gabriel Becker <ggasparb@redhat.com>
0c1482
Date: Mon, 14 Feb 2022 15:54:37 +0100
0c1482
Subject: [PATCH] Update RHEL-08-010385 to allow only one occurrence of config.
0c1482
0c1482
This configuration must appear at only one place so it doesn't get
0c1482
overriden by a different file that can loaded on a different order and
0c1482
the intended configuration is replaced by non-compliant value.
0c1482
---
0c1482
 .../ansible/shared.yml                        | 36 ++++++++++++++++++
0c1482
 .../bash/shared.sh                            | 38 +++++++++++++++++++
0c1482
 .../oval/shared.xml                           |  4 +-
0c1482
 .../sudo_require_reauthentication/rule.yml    | 14 +------
0c1482
 .../tests/multiple_correct_value.fail.sh      | 10 +++++
0c1482
 5 files changed, 87 insertions(+), 15 deletions(-)
0c1482
 create mode 100644 linux_os/guide/system/software/sudo/sudo_require_reauthentication/ansible/shared.yml
0c1482
 create mode 100644 linux_os/guide/system/software/sudo/sudo_require_reauthentication/bash/shared.sh
0c1482
 create mode 100644 linux_os/guide/system/software/sudo/sudo_require_reauthentication/tests/multiple_correct_value.fail.sh
0c1482
0c1482
diff --git a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/ansible/shared.yml b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/ansible/shared.yml
0c1482
new file mode 100644
0c1482
index 00000000000..b0c67a69af9
0c1482
--- /dev/null
0c1482
+++ b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/ansible/shared.yml
0c1482
@@ -0,0 +1,36 @@
0c1482
+# platform = multi_platform_all
0c1482
+# reboot = false
0c1482
+# strategy = restrict
0c1482
+# complexity = low
0c1482
+# disruption = low
0c1482
+
0c1482
+{{{ ansible_instantiate_variables("var_sudo_timestamp_timeout") }}}
0c1482
+- name: "Find out if /etc/sudoers.d/* files contain 'Defaults timestamp_timeout' to be deduplicated"
0c1482
+  find:
0c1482
+    path: "/etc/sudoers.d"
0c1482
+    patterns: "*"
0c1482
+    contains: '^[\s]*Defaults\s.*\btimestamp_timeout=.*'
0c1482
+  register: sudoers_d_defaults_timestamp_timeout
0c1482
+
0c1482
+- name: "Remove found occurrences of 'Defaults timestamp_timeout' from /etc/sudoers.d/* files"
0c1482
+  lineinfile:
0c1482
+    path: "{{ item.path }}"
0c1482
+    regexp: '^[\s]*Defaults\s.*\btimestamp_timeout=.*'
0c1482
+    state: absent
0c1482
+  with_items: "{{ sudoers_d_defaults_timestamp_timeout.files }}"
0c1482
+
0c1482
+- name: Ensure timestamp_timeout is enabled with the appropriate value in /etc/sudoers
0c1482
+  lineinfile:
0c1482
+    path: /etc/sudoers
0c1482
+    regexp: '^[\s]*Defaults\s(.*)\btimestamp_timeout=[-]?\w+\b(.*)$'
0c1482
+    line: 'Defaults \1timestamp_timeout={{ var_sudo_timestamp_timeout }}\2'
0c1482
+    validate: /usr/sbin/visudo -cf %s
0c1482
+    backrefs: yes
0c1482
+  register: edit_sudoers_timestamp_timeout_option
0c1482
+
0c1482
+- name: Enable timestamp_timeout option with appropriate value in /etc/sudoers
0c1482
+  lineinfile: # noqa 503
0c1482
+    path: /etc/sudoers
0c1482
+    line: 'Defaults timestamp_timeout={{ var_sudo_timestamp_timeout }}'
0c1482
+    validate: /usr/sbin/visudo -cf %s
0c1482
+  when: edit_sudoers_timestamp_timeout_option is defined and not edit_sudoers_timestamp_timeout_option.changed
0c1482
diff --git a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/bash/shared.sh b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/bash/shared.sh
0c1482
new file mode 100644
0c1482
index 00000000000..0b623ed4a49
0c1482
--- /dev/null
0c1482
+++ b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/bash/shared.sh
0c1482
@@ -0,0 +1,38 @@
0c1482
+# platform = multi_platform_all
0c1482
+# reboot = false
0c1482
+# strategy = restrict
0c1482
+# complexity = low
0c1482
+# disruption = low
0c1482
+
0c1482
+
0c1482
+{{{ bash_instantiate_variables("var_sudo_timestamp_timeout") }}}
0c1482
+
0c1482
+if grep -x '^[\s]*Defaults.*\btimestamp_timeout=.*' /etc/sudoers.d/*; then
0c1482
+    find /etc/sudoers.d/ -type f -exec sed -i "/^[\s]*Defaults.*\btimestamp_timeout=.*/d" {} \;
0c1482
+fi
0c1482
+
0c1482
+if /usr/sbin/visudo -qcf /etc/sudoers; then
0c1482
+    cp /etc/sudoers /etc/sudoers.bak
0c1482
+    if ! grep -P '^[\s]*Defaults.*\btimestamp_timeout=[-]?\w+\b\b.*$' /etc/sudoers; then
0c1482
+        # sudoers file doesn't define Option timestamp_timeout
0c1482
+        echo "Defaults timestamp_timeout=${var_sudo_timestamp_timeout}" >> /etc/sudoers
0c1482
+    else
0c1482
+        # sudoers file defines Option timestamp_timeout, remediate if appropriate value is not set
0c1482
+        if ! grep -P "^[\s]*Defaults.*\btimestamp_timeout=${var_sudo_timestamp_timeout}\b.*$" /etc/sudoers; then
0c1482
+            
0c1482
+            sed -Ei "s/(^[\s]*Defaults.*\btimestamp_timeout=)[-]?\w+(\b.*$)/\1${var_sudo_timestamp_timeout}\2/" /etc/sudoers
0c1482
+        fi
0c1482
+    fi
0c1482
+    
0c1482
+    # Check validity of sudoers and cleanup bak
0c1482
+    if /usr/sbin/visudo -qcf /etc/sudoers; then
0c1482
+        rm -f /etc/sudoers.bak
0c1482
+    else
0c1482
+        echo "Fail to validate remediated /etc/sudoers, reverting to original file."
0c1482
+        mv /etc/sudoers.bak /etc/sudoers
0c1482
+        false
0c1482
+    fi
0c1482
+else
0c1482
+    echo "Skipping remediation, /etc/sudoers failed to validate"
0c1482
+    false
0c1482
+fi
0c1482
diff --git a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/oval/shared.xml b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/oval/shared.xml
0c1482
index 8f404ca6065..dfc319b6f1f 100644
0c1482
--- a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/oval/shared.xml
0c1482
+++ b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/oval/shared.xml
0c1482
@@ -6,13 +6,13 @@
0c1482
     </criteria>
0c1482
   </definition>
0c1482
 
0c1482
-  <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="check correct configuration in /etc/sudoers" id="test_sudo_timestamp_timeout" version="1">
0c1482
+  <ind:textfilecontent54_test check="all" check_existence="only_one_exists" comment="check correct configuration in /etc/sudoers" id="test_sudo_timestamp_timeout" version="1">
0c1482
     <ind:object object_ref="obj_sudo_timestamp_timeout"/>
0c1482
     <ind:state state_ref="state_sudo_timestamp_timeout" />
0c1482
   </ind:textfilecontent54_test>
0c1482
 
0c1482
   <ind:textfilecontent54_object id="obj_sudo_timestamp_timeout" version="1">
0c1482
-    <ind:filepath>/etc/sudoers</ind:filepath>
0c1482
+    <ind:filepath operation="pattern match">^/etc/sudoers(\.d/.*)?$</ind:filepath>
0c1482
     <ind:pattern operation="pattern match">^[\s]*Defaults[\s]+timestamp_timeout=([-]?[\d]+)$</ind:pattern>
0c1482
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
0c1482
   </ind:textfilecontent54_object>
0c1482
diff --git a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/rule.yml b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/rule.yml
0c1482
index 42c6e28f9e6..eebb96678f1 100644
0c1482
--- a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/rule.yml
0c1482
+++ b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/rule.yml
0c1482
@@ -50,16 +50,4 @@ ocil: |-
0c1482
     
sudo grep -ri '^Defaults.*timestamp_timeout' /etc/sudoers /etc/sudoers.d
0c1482
     The output should be:
0c1482
     
/etc/sudoers:Defaults timestamp_timeout=0
or "timestamp_timeout" is set to a positive number.
0c1482
-
0c1482
-template:
0c1482
-    name: sudo_defaults_option
0c1482
-    vars:
0c1482
-        option: timestamp_timeout
0c1482
-        variable_name: "var_sudo_timestamp_timeout"
0c1482
-        # optional minus char added so remediation can detect properly if item is already configured
0c1482
-        option_regex_suffix: '=[-]?\w+\b'
0c1482
-    backends:
0c1482
-        # Template is not able to accomodate this particular check.
0c1482
-        # It needs to check for an integer greater than or equal to zero
0c1482
-        oval: "off"
0c1482
-        
0c1482
+    If results are returned from more than one file location, this is a finding.
0c1482
diff --git a/linux_os/guide/system/software/sudo/sudo_require_reauthentication/tests/multiple_correct_value.fail.sh b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/tests/multiple_correct_value.fail.sh
0c1482
new file mode 100644
0c1482
index 00000000000..a258d6632b5
0c1482
--- /dev/null
0c1482
+++ b/linux_os/guide/system/software/sudo/sudo_require_reauthentication/tests/multiple_correct_value.fail.sh
0c1482
@@ -0,0 +1,10 @@
0c1482
+#!/bin/bash
0c1482
+
0c1482
+
0c1482
+if grep -q 'timestamp_timeout' /etc/sudoers; then
0c1482
+	sed -i 's/.*timestamp_timeout.*/Defaults timestamp_timeout=3/' /etc/sudoers
0c1482
+else
0c1482
+	echo "Defaults timestamp_timeout=3" >> /etc/sudoers
0c1482
+fi
0c1482
+
0c1482
+echo "Defaults timestamp_timeout=3" > /etc/sudoers.d/00-complianceascode-test.conf