6.2.7 Ensure users' dot files are not group or world writable

Information

While the system administrator can establish secure permissions for users' 'dot' files, the users can easily override these.

Rationale:

Group or world-writable user configuration files may enable malicious users to steal or modify other users' data or to gain another user's system privileges.

Solution

Making global modifications to users' files without alerting the user community can result in unexpected outages and unhappy users. Therefore, it is recommended that a monitoring policy be established to report user dot file permissions and determine the action to be taken in accordance with site policy.
The following script will remove excessive permissions on dot files within interactive users' home directories.

#!/bin/bash

awk -F: '($1!~/(halt|sync|shutdown)/ && $7!~/^(/usr)?/sbin/nologin(/)?$/ && $7!~/(/usr)?/bin/false(/)?$/) { print $1 ' ' $6 }' | while read -r user dir; do
if [ -d '$dir' ]; then
for file in '$dir'/.*; do
if [ ! -h '$file' ] && [ -f '$file' ]; then
fileperm=$(stat -L -c '%A' '$file')
if [ '$(echo '$fileperm' | cut -c6)' != '-' ] || [ '$(echo '$fileperm' | cut -c9)' != '-' ]; then
chmod go-w '$file'
fi
fi
done
fi
done

See Also

https://workbench.cisecurity.org/files/3219