← Rules Catalog
mediumaccess-control

Terminate idle interactive sessions after the configured period

shell-timeout · RHEL ≥ 8, UBUNTU ≥ 22 · 1 impl

Description

An idle interactive session must terminate after a period of inactivity. Either mechanism satisfies this: a positive TMOUT in the shell startup files, or StopIdleSessionSec in logind.conf. The accepted period is the shell_idle_timeout_seconds variable, so a site can declare its own policy instead of inheriting a benchmark constant.

Rationale

An unattended terminal left logged in is a standing grant of that user's privileges to anyone who walks up to it. Terminating the session closes that window without depending on the user to remember. The default of 600 seconds is the strictest value any mapped benchmark requires, so it satisfies all of them at once; a site with a shorter policy declares it and the check follows.

Check → Remediate

Checkcommand
limit={{ shell_idle_timeout_seconds }}
pass=0
# logind StopIdleSessionSec. systemd-analyze resolves drop-ins, so this
# sees the effective value rather than only /etc/systemd/logind.conf.
idle_sec=$(systemd-analyze cat-config systemd/logind.conf 2>/dev/null |
  grep -E '^[[:space:]]*StopIdleSessionSec[[:space:]]*=' | tail -1 |
  sed 's/^[^=]*=[[:space:]]*//')
if [ -n "$idle_sec" ] && [ "$idle_sec" != "infinity" ] &&
   [ "$idle_sec" -gt 0 ] 2>/dev/null && [ "$idle_sec" -le "$limit" ] 2>/dev/null; then
  pass=1
fi
# TMOUT, read the way a shell actually resolves it. Two things a naive
# grep gets wrong. Files are walked in SOURCING order, because `grep -r`
# returns readdir order, which is filesystem-dependent and not the order
# /etc/profile sources its drop-ins. And a `readonly` assignment LOCKS the
# value: every later assignment fails with an error, so the first readonly
# wins while an unlocked value is overridden by the last one. Reading
# either "first" or "last" unconditionally credits a timeout the session
# never adopts. Covers the RHEL layout (/etc/bashrc) and the Debian one
# (/etc/bash.bashrc).
tmout_val=""; locked=0
for f in /etc/profile $(ls -1 /etc/profile.d/*.sh 2>/dev/null | sort) \
         /etc/bashrc /etc/bash.bashrc; do
  [ -r "$f" ] || continue
  while IFS= read -r line; do
    case "$line" in \#*|'') continue ;; esac
    case "$line" in *TMOUT=*) ;; *) continue ;; esac
    v=$(printf '%s\n' "$line" | grep -oE 'TMOUT=[0-9]+' | head -1 | cut -d= -f2)
    [ -n "$v" ] || continue
    [ "$locked" -eq 1 ] && continue
    tmout_val="$v"
    printf '%s\n' "$line" |
      grep -qE '(readonly|typeset[[:space:]]+-[a-zA-Z]*r|declare[[:space:]]+-[a-zA-Z]*r)' &&
      locked=1
  done < "$f"
done
if [ -n "$tmout_val" ] && [ "$tmout_val" -gt 0 ] 2>/dev/null &&
   [ "$tmout_val" -le "$limit" ] 2>/dev/null; then
  pass=1
fi
# A writable TMOUT is one command from being switched off: `TMOUT=0` and
# `unset TMOUT` both succeed against a plain assignment and both fail
# against a readonly one. DISA requires the locked form on RHEL
# (`declare -xr TMOUT=600` in the RHEL 8, 9 and 10 STIG fix text) and does
# NOT ask for it on Ubuntu, whose STIG fix text is a plain `TMOUT=`. So
# the requirement is read from the platform rather than applied
# everywhere, which would fail Ubuntu hosts their own benchmark passes.
# This is not mechanism selection, which capabilities are for; the
# mechanism is identical and only the required strictness differs.
rhel_like=0
if [ -r /etc/os-release ]; then
  . /etc/os-release 2>/dev/null
  case " ${ID:-} ${ID_LIKE:-} " in *" rhel "*|*" fedora "*|*" centos "*) rhel_like=1 ;; esac
fi
# readonly is also legitimately written on its own line, as
# "TMOUT=600" followed by "readonly TMOUT". The walk above only sees the
# assignment line, so check for the bare declaration too before calling a
# host non-compliant.
if [ "$locked" -eq 0 ] && [ -n "$tmout_val" ]; then
  if grep -rhqE '^[[:space:]]*(readonly|declare[[:space:]]+-[a-zA-Z]*r|typeset[[:space:]]+-[a-zA-Z]*r)[[:space:]]+TMOUT([[:space:]]|$)' \
     /etc/profile /etc/profile.d/ /etc/bashrc /etc/bash.bashrc 2>/dev/null; then
    locked=1
  fi
fi
if [ "$pass" -eq 1 ] && [ -n "$tmout_val" ] && [ "$rhel_like" -eq 1 ] && [ "$locked" -eq 0 ]; then
  echo "FAIL: TMOUT=$tmout_val is within ${limit}s but is not readonly, so any user can run"
  echo "      'TMOUT=0' or 'unset TMOUT' and remove it. Set it with 'declare -xr' or 'readonly'."
  exit 1
fi
if [ "$pass" -eq 1 ]; then
  echo "OK: idle timeout within ${limit}s (logind=${idle_sec:-unset}, TMOUT=${tmout_val:-unset}, readonly=${locked})"
  exit 0
fi
echo "FAIL: no idle timeout within ${limit}s (logind=${idle_sec:-unset}, TMOUT=${tmout_val:-unset})"
exit 1
expected_exit:
0
Remediatefile_content
path:
/etc/profile.d/kensa-tmout.sh
content:
declare -xr TMOUT={{ shell_idle_timeout_seconds }}
mode:
0644

Framework references

CIS

rhel8 5.4.3.2rhel9 5.4.3.2rhel10 5.4.3.2

STIG

V-257258V-279929 / RHEL-08-020360V-258068 / RHEL-09-412035V-281295 / RHEL-10-700920V-260554 / UBTU-22-412030V-270680 / UBTU-24-200060

NIST 800-53

AC-11AC-12SC-10

NIST 800-171

3.1.11[b]3.13.9[c]

CMMC Level 2

AC.L2-3.1.11SC.L2-3.13.9
#shell#session#timeout#security