Use of umask() with chmod-style Argument

Draft Variant
Structure: Simple
Description

This vulnerability occurs when a program incorrectly uses the `umask()` system call with an argument formatted for `chmod()`, leading to unintended and overly permissive file permissions.

Extended Description

The `umask()` function sets a process's file mode creation mask, which restricts permissions on newly created files. However, developers sometimes mistakenly pass it an octal value (like 0644 or 0777) intended for `chmod()`, which sets permissions directly. Since `umask()` interprets its argument as a mask that *blocks* permissions, this inversion results in files being created with far more access than intended, potentially exposing sensitive data. For example, passing `umask(0)` (a common `chmod`-style intent to grant all permissions) actually creates a mask of zero, meaning no permissions are blocked. This causes new files to be created with full read, write, and execute access for all users. The core issue is a semantic confusion between a mask that subtracts permissions (`umask`) and a mode that adds them (`chmod`), a mistake that directly weakens file system security.

Common Consequences 1
Scope: ConfidentialityIntegrityAccess Control

Impact: Read Files or DirectoriesModify Files or DirectoriesBypass Protection Mechanism

Potential Mitigations 2
Phase: Implementation
Use umask() with the correct argument.
Phase: Testing
If you suspect misuse of umask(), you can use grep to spot call instances of umask().
Applicable Platforms
Languages:
C : Undetermined
Modes of Introduction
Implementation
Notes
OtherSome umask() manual pages begin with the false statement: "umask sets the umask to mask & 0777" Although this behavior would better align with the usage of chmod(), where the user provided argument specifies the bits to enable on the specified file, the behavior of umask() is in fact opposite: umask() sets the umask to ~mask & 0777. The documentation goes on to describe the correct usage of umask(): "The umask is used by open() to set initial file permissions on a newly-created file. Specifically, permissions in the umask are turned off from the mode argument to open(2) (so, for example, the common umask default value of 022 results in new files being created with permissions 0666 & ~022 = 0644 = rw-r--r-- in the usual case where the mode is specified as 0666)."