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

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