Configure Access to pam_motd Message Files

Description

The files that the lines of /etc/pam.d name with motd=PATH, commented lines included, must be owned by root, group-owned by root, and have mode 0644 or more restrictive. A motd= value can name several files, separated by colons.

Rationale

Restrictive ownership and permissions prevent unauthorized modification of text displayed to users at login.

Remediation

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}' 'libpam-runtime' 2>/dev/null | grep -q '^installed$'; then

# The value of motd= is a colon-separated list of files (pam_motd(8)).
while IFS= read -r motd_path; do
    motd_path=${motd_path#\"}; motd_path=${motd_path%\"}
    motd_path=${motd_path#\'}; motd_path=${motd_path%\'}
    [[ -e "$motd_path" ]] || continue
    chown root:root "$motd_path"
    chmod u-x,go-wx "$motd_path"
done < <(grep -hPo '\bmotd=\K("[^"]+"|'"'"'[^'"'"']+'"'"'|\S+)' /etc/pam.d/* 2>/dev/null | tr ':' '\n' | sort -u)

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi