6.2.11 Ensure local interactive users own their home directories

Information

The user home directory is space defined for the particular user to set local environment variables and to store personal files.

Since the user is accountable for files stored in the user home directory, the user must be the owner of the directory.

Solution

Change the ownership of any home directories that are not owned by the defined user to the correct user.

The following script will update local interactive user home directories to be owned by the user:

#!/usr/bin/env bash

{
output=""
valid_shells="^($( sed -rn '/^//{s,/,\\/,g;p}' /etc/shells | paste -s -d '|' - ))$"
awk -v pat="$valid_shells" -F: '$(NF) ~ pat { print $1 " " $(NF-1) }' /etc/passwd | while read -r user home; do
owner="$(stat -L -c "%U" "$home")"
if [ "$owner" != "$user" ]; then
echo -e "
- User \"$user\" home directory \"$home\" is owned by user \"$owner\"
- changing ownership to \"$user\"
"
chown "$user" "$home"
fi
done
}

See Also

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