Ensure User Dot Files Have Mode 0644 or Less Permissive

Description

The dot files in the home directories of interactive users, other than .bash_history and .netrc, should have mode 0644 or less permissive: no execute bit, and no write bit for the group or others. .bash_history and .netrc have their own, stricter rules. To set the mode of a dot file, run the following command:

$ sudo chmod u-x,go-wx /home/USER/.FILE

Rationale

A dot file that others can write lets them change the environment, the commands or the startup of the user’s sessions.

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

while IFS=: read -r _ _ uid _ _ home shell; do
    [ "$uid" -ge 1000 ] || continue
    case "$shell" in */nologin | */false) continue ;; esac
    [ -d "$home" ] || continue
    find "$home" -maxdepth 1 -type f -name '.*' ! -name .bash_history ! -name .netrc \
        -perm /7133 -exec chmod u-xs,g-wxs,o-wxt {} +
done < /etc/passwd

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