Ensure sudo Does Not Share Authentication Across Sessions - sudo timestamp_type
Description
The sudo timestamp_type option sets the scope of the cached
authentication that spares a user from typing the password again within
timestamp_timeout. With global, one authentication covers
every session of the user. Make sure that no Defaults entry in
/etc/sudoers or in the sudo configuration snippets in
/etc/sudoers.d/ sets timestamp_type to global. The
default, tty, keeps a separate timestamp for every terminal.
Rationale
With a global timestamp, a process in any other session of the user, such as a
background job or a second login, can run sudo without a password once the user
has authenticated anywhere.
Shell script
The following script can be run on the host to remediate the issue.
#!/bin/bash
# Remediation is applicable only in certain platforms
if rpm --quiet -q kernel-core && { rpm --quiet -q sudo; }; then
# Replace timestamp_type=global with timestamp_type=tty in the Defaults entries
for f in /etc/sudoers /etc/sudoers.d/*; do
# sudo skips the files of /etc/sudoers.d whose names contain a "." or end in "~"
case "$f" in
/etc/sudoers.d/*.* | *~) continue ;;
esac
if [ -f "$f" ] && grep -qE '^([[:blank:]]*Defaults\b[^#]*[[:space:],])timestamp_type[[:blank:]]*=[[:blank:]]*"?global\b"?' "$f"; then
# The copy sits beside the file, so visudo resolves relative includes
# as it does for the file, and sudo skips it for the "." in its name
sudoers_new=$(mktemp "$f.XXXXXX")
sed -E 's/^([[:blank:]]*Defaults\b[^#]*[[:space:],])timestamp_type[[:blank:]]*=[[:blank:]]*"?global\b"?/\1timestamp_type=tty/' "$f" > "$sudoers_new"
if /usr/sbin/visudo -qcf "$sudoers_new"; then
cat "$sudoers_new" > "$f"
else
echo "Fail to validate remediated $f, keeping the original file" >&2
fi
rm -f "$sudoers_new"
fi
done
else
>&2 echo 'Remediation is not applicable, nothing was done'
fi
Ansible playbook
The following playbook can be run with Ansible to remediate the issue.
- name: Gather the package facts
package_facts:
manager: auto
tags:
- CCE-86542-8
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_timestamp_type_global
- name: Find the /etc/sudoers.d/ files that sudo reads
ansible.builtin.find:
paths:
- /etc/sudoers.d/
excludes:
- '*.*'
- '*~'
register: sudoers
when:
- '"kernel-core" in ansible_facts.packages'
- '"sudo" in ansible_facts.packages'
tags:
- CCE-86542-8
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_timestamp_type_global
- name: Replace timestamp_type=global with timestamp_type=tty in the Defaults entries
of the sudoers files
ansible.builtin.replace:
regexp: ^([ \t]*Defaults\b[^#\n]*[\s,])timestamp_type[ \t]*=[ \t]*"?global\b"?
replace: \g<1>timestamp_type=tty
path: '{{ item.path }}'
validate: /usr/sbin/visudo -cf %s
with_items:
- path: /etc/sudoers
- '{{ sudoers.files }}'
when:
- '"kernel-core" in ansible_facts.packages'
- '"sudo" in ansible_facts.packages'
tags:
- CCE-86542-8
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_timestamp_type_global