---
title: Ensure the Shell Initialization Files Set a Restrictive Umask
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > OOTB Rules > Ensure the Shell Initialization Files
  Set a Restrictive Umask
---

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

# Ensure the Shell Initialization Files Set a Restrictive Umask
 
## Description{% #description %}

Every `umask` command that starts a line in `/etc/profile`, `/etc/bashrc`, `/etc/bash.bashrc`, `/etc/profile.d/sh.local` or a file under `/etc/profile.d` whose name ends in `.sh` 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=`:

```
umask 027
```

A relative symbolic mode such as `g-w` fails, as its result depends on the umask before it. The `UMASK` of `/etc/login.defs` applies until one of these files sets the umask.

## Rationale{% #rationale %}

A shell initialization file that sets a permissive umask overrides the restrictive default of `/etc/login.defs` for every interactive session, and files created in those sessions become readable or writable by other 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 rpm --quiet -q bash; 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 ))
readarray -t profile_d_scripts < <(find /etc/profile.d/ -type f -name '*.sh' 2>/dev/null)

for f in /etc/profile /etc/bashrc /etc/bash.bashrc /etc/profile.d/sh.local "${profile_d_scripts[@]}"; 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.
        [[ $value == *[\$\`]* ]] && continue
        # The check reads the value up to a blank or a #, 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%%[;&|()<>]*}
        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

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