Ensure the Default Umask is Set Correctly in /etc/profile.d

Description

Login shells run the scripts matching /etc/profile.d/*.sh after /etc/profile, so a umask command in them sets the default umask of the session. At least one of these scripts has to set the umask, and every umask command in them has to set 027 or a more restrictive value, written as three or four octal digits or as an absolute symbolic mode such as u=rwx,g=rx,o=. For example, add the following line to /etc/profile.d/60-default_umask.sh:

umask 027

Rationale

The umask value influences the permissions assigned to files when they are created. A misconfigured umask value could result in files with excessive permissions that can be read or written to by unauthorized users.

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

var_accounts_user_umask='027'


# umask_to_dec prints the bits that a umask argument denies, as a decimal
# number, or INVALID for a value the audit cannot evaluate.
umask_to_dec() {
    local arg=$1 u=0 g=0 o=0 clause who perms bits deny
    if [[ $arg =~ ^[0-7]{3,4}$ ]]; then
        printf '%d' "$(( 8#$arg ))"
        return
    fi
    local IFS=,
    local -a clauses
    set -f
    # shellcheck disable=SC2206 # split the clauses at the commas
    clauses=($arg)
    set +f
    for clause in "${clauses[@]}"; do
        if [[ $clause =~ ^([augo]+)=([rwx]*)$ ]]; then
            who=${BASH_REMATCH[1]} perms=${BASH_REMATCH[2]} bits=0
            [[ $perms == *r* ]] && bits=$(( bits | 4 ))
            [[ $perms == *w* ]] && bits=$(( bits | 2 ))
            [[ $perms == *x* ]] && bits=$(( bits | 1 ))
            deny=$(( 7 - bits ))
            [[ $who == *a* ]] && { u=$deny; g=$deny; o=$deny; }
            [[ $who == *u* ]] && u=$deny
            [[ $who == *g* ]] && g=$deny
            [[ $who == *o* ]] && o=$deny
        else
            printf 'INVALID'
            return
        fi
    done
    printf '%d' "$(( u * 64 + g * 8 + o ))"
}

floor=$(( 8#$var_accounts_user_umask ))
umask_set=0
for f in /etc/profile.d/*.sh; do
    [ -f "$f" ] || continue
    while IFS=: read -r line value; do
        # Leave a value that the shell computes, such as "$(cat file)", as
        # it is: an edit of part of it breaks the script.
        if [[ $value == *[\$\`]* ]]; then
            umask_set=1
            continue
        fi
        # The check reads the value up to a blank, a # or a semicolon, so put
        # a blank between the value and a command separator that follows it.
        sed -i -E "${line}s/^(\s*umask\s+[^[:space:]#;&|()<>]+)([;&|()<>])/\1 \2/I" "$f"
        value=${value%%[;&|()<>]*}
        [ -n "$value" ] || continue
        umask_set=1
        dec=$(umask_to_dec "$value")
        if [ "$dec" != INVALID ] && [ $(( dec & floor )) -eq "$floor" ]; then
            continue
        fi
        sed -i -E "${line}s/^(\s*umask\s+)[^[:space:]#;&|()<>]+/\1${var_accounts_user_umask}/I" "$f"
    done < <(grep -nPio '^\h*umask\h+\K[^\s#]+' "$f")
done

if [ "$umask_set" -eq 0 ]; then
    # End the file with a newline, so that the umask line starts a line of
    # its own.
    [ -z "$(tail -c 1 /etc/profile.d/60-default_umask.sh 2>/dev/null)" ] || echo >> /etc/profile.d/60-default_umask.sh
    echo "umask $var_accounts_user_umask" >> /etc/profile.d/60-default_umask.sh
    chmod 0644 /etc/profile.d/60-default_umask.sh
fi

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