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

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