Set Interactive Session Timeout for CIS Benchmark
Description
The TMOUT variable sets the number of seconds after which an idle
interactive shell exits.
The CIS Benchmark requires at least one of the files /etc/*bashrc /etc/profile /etc/profile.d/*.sh
to set TMOUT, and every file that mentions it to set a value from 1 to
900, to mark it read only and to export it.
typeset -xr TMOUT does all three, as do the readonly and
export commands.
For example, add the following line to /etc/profile.d/tmout.sh:
readonly TMOUT=900 ; export TMOUT
Rationale
Terminating an idle session within a short time period reduces
the window of opportunity for unauthorized personnel to take control of a
management session enabled on the console or console port that has been
left unattended.
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 dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$'; then
var_accounts_tmout='900'
tmout_files=(/etc/*bashrc /etc/profile /etc/profile.d/*.sh)
tmout_readonly='^\h*(typeset\h-xr\hTMOUT=\d+|([^#\n\r]+)?\breadonly\h+TMOUT\b)'
tmout_export='^\h*(typeset\h-xr\hTMOUT=\d+|([^#\n\r]+)?\bexport\b([^#\n\r]+\b)?TMOUT\b)'
# An assignment of TMOUT starts a command, alone or after readonly, export,
# typeset or declare. Its value is literal when it is a number, quoted or
# not, or nothing. The shell computes the other values, such as
# "$((60 * 15))", and they stay as they are: an edit of part of one breaks
# the script. A TMOUT= inside a string, as in echo "TMOUT=0", stays too.
tmout_assignment='^(([^#]*[;&|(){])?[[:space:]]*((then|do|else)[[:space:]]+)?((readonly|export|typeset|declare)([[:space:]]+[^[:space:];&|#]+)*[[:space:]]+)?TMOUT=)'
tmout_literal="(\"[0-9]*\"|'[0-9]*'|[0-9]*)([[:space:];&|)]|\$)"
tmout_set=0
for f in "${tmout_files[@]}"; do
[ -f "$f" ] || continue
grep -Eq "$tmout_assignment" "$f" || continue
tmout_set=1
# A file whose values are all computed stays as it is.
grep -Eq "$tmout_assignment$tmout_literal" "$f" || continue
# A rewritten value carries a mark until its line is done, so that the
# loop goes on to the other assignments of the line.
sed -i -E -e ':a' -e "s/$tmout_assignment$tmout_literal/\\1\\x01$var_accounts_tmout\\9/" -e 'ta' -e 's/\x01//g' "$f"
# End the file with a newline, so that a line appended below starts a
# line of its own.
[ -z "$(tail -c 1 "$f")" ] || echo >> "$f"
grep -Pq "$tmout_readonly" "$f" || echo "readonly TMOUT" >> "$f"
grep -Pq "$tmout_export" "$f" || echo "export TMOUT" >> "$f"
done
if [ "$tmout_set" -eq 0 ]; then
[ -z "$(tail -c 1 /etc/profile.d/tmout.sh 2>/dev/null)" ] || echo >> /etc/profile.d/tmout.sh
printf '%s\n' "# Set the interactive shell timeout to $var_accounts_tmout seconds" \
"readonly TMOUT=$var_accounts_tmout ; export TMOUT" >> /etc/profile.d/tmout.sh
chmod 0644 /etc/profile.d/tmout.sh
fi
else
>&2 echo 'Remediation is not applicable, nothing was done'
fi