---
title: Ensure the Default Umask Is Set Correctly Where It Is First Set
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > OOTB Rules > Ensure the Default Umask Is Set
  Correctly Where It Is First Set
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Ensure the Default Umask Is Set Correctly Where It Is First Set
 
## Description{% #description %}

The default umask of a session comes from the first of these locations that sets it: a script matching `/etc/profile.d/*.sh`, `/etc/profile`, `/etc/bashrc`, `/etc/bash.bashrc`, the `pam_umask` line of `/etc/pam.d/postlogin`, `/etc/login.defs` and `/etc/default/login`. Reading them in this order, one of them has to set the umask to `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=`, and every `umask` command in the `/etc/profile.d` scripts and in the locations read before that one has to do the same. For example, add the following line to `/etc/profile.d/50-systemwide_umask.sh`:

```
umask 027
```

## Rationale{% #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{% #remediation %}

### Shell script{% #shell-script %}

The following script can be run on the host to remediate the issue.

```bash
#!/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 ))"
}

# Set every less restrictive umask of these locations to the variable, as
# the CIS remediation does, then set the umask in a profile.d script unless
# one already sets it. An octal or absolute symbolic value at least as
# restrictive stays.
floor=$(( 8#$var_accounts_user_umask ))
readarray -t profile_d_scripts < <(find /etc/profile.d/ -type f -name '*.sh' 2>/dev/null)

profile_d_set=0
for f in "${profile_d_scripts[@]}" /etc/profile /etc/bashrc /etc/bash.bashrc /etc/login.defs /etc/default/login; do
    [ -f "$f" ] || continue
    profile_d=0
    case $f in
    /etc/profile.d/*) profile_d=1 ;;
    esac
    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
            profile_d_set=$(( profile_d_set | profile_d ))
            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
        profile_d_set=$(( profile_d_set | profile_d ))
        dec=$(umask_to_dec "$value")
        if [ "$dec" != INVALID ] && (( (dec & floor) == 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 [ -f /etc/pam.d/postlogin ]; then
    while IFS=: read -r line value; do
        dec=$(umask_to_dec "$value")
        if [ "$dec" != INVALID ] && (( (dec & floor) == floor )); then
            continue
        fi
        sed -i -E "${line}s/(\bpam_umask\.so\s+(\S+\s+)*umask=)[^[:space:]#]+/\1${var_accounts_user_umask}/" /etc/pam.d/postlogin
    done < <(grep -nPo '^\h*session\h+[^#\n\r]+\h+pam_umask\.so\h+([^#\n\r]+\h+)?umask=\K[^\s#]+' /etc/pam.d/postlogin)
fi

if [ "$profile_d_set" -eq 0 ]; then
    echo "umask $var_accounts_user_umask" >> /etc/profile.d/50-systemwide_umask.sh
    chmod 0644 /etc/profile.d/50-systemwide_umask.sh
fi

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